contrib: Make the RTPReplay a a class so it can be shared

This commit is contained in:
Holger Hans Peter Freyther 2012-08-05 09:25:25 +02:00
parent 59043d8c94
commit 4552d8061a
1 changed files with 62 additions and 41 deletions

View File

@ -4,53 +4,74 @@ Simple UDP replay from the state files
PackageLoader fileInPackage: #Sockets. PackageLoader fileInPackage: #Sockets.
Eval [ Object subclass: RTPReplay [
| last_time last_image udp_send socket dest | | filename |
RTPReplay class >> on: aFile [
^ self new
file: aFile; yourself
]
last_time := nil. file: aFile [
last_image := nil. filename := aFile
file := FileStream open: 'rtp_ssrc13529910.240.240.1_to_10.240.240.50.state'. ]
"Send the payload" streamAudio: aHost port: aPort [
dest := Sockets.SocketAddress byName: '127.0.0.1'. | file last_time last_image udp_send socket dest |
socket := Sockets.DatagramSocket new.
udp_send := [:payload | | datagram |
datagram := Sockets.Datagram data: payload contents address: dest port: 4000.
socket nextPut: datagram
].
[file atEnd] whileFalse: [ last_time := nil.
| lineStream time data now_image | last_image := nil.
lineStream := file nextLine readStream. file := FileStream open: filename.
"Read the time, skip the blank, parse the data" "Send the payload"
time := Number readFrom: lineStream. dest := Sockets.SocketAddress byName: aHost.
lineStream skip: 1. socket := Sockets.DatagramSocket new.
udp_send := [:payload | | datagram |
data := WriteStream on: (ByteArray new: 30). datagram := Sockets.Datagram data: payload contents address: dest port: aPort.
[lineStream atEnd] whileFalse: [ socket nextPut: datagram
| hex |
hex := lineStream next: 2.
data nextPut: (Number readFrom: hex readStream radix: 16).
]. ].
last_time isNil [file atEnd] whileFalse: [
ifTrue: [ | lineStream time data now_image |
"First time, send it right now" lineStream := file nextLine readStream.
last_time := time.
last_image := Time millisecondClockValue.
udp_send value: data.
]
ifFalse: [
| wait_image new_image_time |
"How long to wait?" "Read the time, skip the blank, parse the data"
wait_image := last_image + ((time - last_time) * 1000). time := Number readFrom: lineStream.
[ wait_image > Time millisecondClockValue ] whileTrue: []. lineStream skip: 1.
udp_send value: data. data := WriteStream on: (ByteArray new: 30).
last_time := time. [lineStream atEnd] whileFalse: [
last_image := wait_image. | hex |
] hex := lineStream next: 2.
]. data nextPut: (Number readFrom: hex readStream radix: 16).
].
last_time isNil
ifTrue: [
"First time, send it right now"
last_time := time.
last_image := Time millisecondClockValue.
udp_send value: data.
]
ifFalse: [
| wait_image new_image_time |
"How long to wait?"
wait_image := last_image + ((time - last_time) * 1000).
[ wait_image > Time millisecondClockValue ] whileTrue: [].
udp_send value: data.
last_time := time.
last_image := wait_image.
]
]
]
]
Eval [
| replay |
replay := RTPReplay on: 'rtp_ssrc6976010.240.240.1_to_10.240.240.50.state'.
Transcript nextPutAll: 'Going to stream now'; nl.
replay streamAudio: '127.0.0.1' port: 4000.
] ]