Browse Source

rtmp: fix timestamp conversion from RTSP/HLS to RTMP (#1899)

this was causing moments of silence and timing errors when reading with
RTMP a stream originally published with RTSP or HLS.
pull/1898/head
Alessandro Ros 3 years ago committed by GitHub
parent
commit
efda44cfae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      internal/rtmp/rawmessage/writer.go

20
internal/rtmp/rawmessage/writer.go

@ -14,8 +14,8 @@ type writerChunkStream struct {
lastMessageStreamID *uint32 lastMessageStreamID *uint32
lastType *uint8 lastType *uint8
lastBodyLen *uint32 lastBodyLen *uint32
lastTimestamp *time.Duration lastTimestamp *int64
lastTimestampDelta *time.Duration lastTimestampDelta *int64
} }
func (wc *writerChunkStream) writeChunk(c chunk.Chunk) error { func (wc *writerChunkStream) writeChunk(c chunk.Chunk) error {
@ -46,9 +46,13 @@ func (wc *writerChunkStream) writeMessage(msg *Message) error {
pos := uint32(0) pos := uint32(0)
firstChunk := true firstChunk := true
var timestampDelta *time.Duration // convert timestamp to milliseconds before splitting message in chunks
/// otherwise timestampDelta gets messed up.
timestamp := int64(msg.Timestamp / time.Millisecond)
var timestampDelta *int64
if wc.lastTimestamp != nil { if wc.lastTimestamp != nil {
diff := msg.Timestamp - *wc.lastTimestamp diff := timestamp - *wc.lastTimestamp
// use delta only if it is positive // use delta only if it is positive
if diff >= 0 { if diff >= 0 {
@ -69,7 +73,7 @@ func (wc *writerChunkStream) writeMessage(msg *Message) error {
case wc.lastMessageStreamID == nil || timestampDelta == nil || *wc.lastMessageStreamID != msg.MessageStreamID: case wc.lastMessageStreamID == nil || timestampDelta == nil || *wc.lastMessageStreamID != msg.MessageStreamID:
err := wc.writeChunk(&chunk.Chunk0{ err := wc.writeChunk(&chunk.Chunk0{
ChunkStreamID: msg.ChunkStreamID, ChunkStreamID: msg.ChunkStreamID,
Timestamp: uint32(msg.Timestamp / time.Millisecond), Timestamp: uint32(timestamp),
Type: msg.Type, Type: msg.Type,
MessageStreamID: msg.MessageStreamID, MessageStreamID: msg.MessageStreamID,
BodyLen: (bodyLen), BodyLen: (bodyLen),
@ -82,7 +86,7 @@ func (wc *writerChunkStream) writeMessage(msg *Message) error {
case *wc.lastType != msg.Type || *wc.lastBodyLen != bodyLen: case *wc.lastType != msg.Type || *wc.lastBodyLen != bodyLen:
err := wc.writeChunk(&chunk.Chunk1{ err := wc.writeChunk(&chunk.Chunk1{
ChunkStreamID: msg.ChunkStreamID, ChunkStreamID: msg.ChunkStreamID,
TimestampDelta: uint32(*timestampDelta / time.Millisecond), TimestampDelta: uint32(*timestampDelta),
Type: msg.Type, Type: msg.Type,
BodyLen: (bodyLen), BodyLen: (bodyLen),
Body: msg.Body[pos : pos+chunkBodyLen], Body: msg.Body[pos : pos+chunkBodyLen],
@ -94,7 +98,7 @@ func (wc *writerChunkStream) writeMessage(msg *Message) error {
case wc.lastTimestampDelta == nil || *wc.lastTimestampDelta != *timestampDelta: case wc.lastTimestampDelta == nil || *wc.lastTimestampDelta != *timestampDelta:
err := wc.writeChunk(&chunk.Chunk2{ err := wc.writeChunk(&chunk.Chunk2{
ChunkStreamID: msg.ChunkStreamID, ChunkStreamID: msg.ChunkStreamID,
TimestampDelta: uint32(*timestampDelta / time.Millisecond), TimestampDelta: uint32(*timestampDelta),
Body: msg.Body[pos : pos+chunkBodyLen], Body: msg.Body[pos : pos+chunkBodyLen],
}) })
if err != nil { if err != nil {
@ -117,7 +121,7 @@ func (wc *writerChunkStream) writeMessage(msg *Message) error {
wc.lastType = &v2 wc.lastType = &v2
v3 := bodyLen v3 := bodyLen
wc.lastBodyLen = &v3 wc.lastBodyLen = &v3
v4 := msg.Timestamp v4 := timestamp
wc.lastTimestamp = &v4 wc.lastTimestamp = &v4
if timestampDelta != nil { if timestampDelta != nil {

Loading…
Cancel
Save