diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index b649d262..ef36a3f1 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -444,6 +444,7 @@ func C_ReadN(r *C_RTMP, buf []byte) error { // int WriteN(RTMP* r, const char* buffer, int n); // rtmp.c +1502 func C_WriteN(r *C_RTMP, buf []byte) error { + //ToDo: consider using a different timeout for writes than for reads err := r.Link.conn.SetWriteDeadline(time.Now().Add(time.Second * time.Duration(r.Link.timeout))) if err != nil { return err @@ -1366,11 +1367,14 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) error { hSize += cSize } - t := uint32(int(packet.m_nTimeStamp) - last) - if t >= 0xffffff { + var ts uint32 + if prevPacket != nil { + ts = uint32(int(packet.m_nTimeStamp) - last) + } + if ts >= 0xffffff { origIdx -= 4 hSize += 4 - log.Printf("Larger timestamp than 24-bit: 0x%v", t) + log.Printf("Larger timestamp than 24-bit: 0x%v", ts) } headerIdx := origIdx @@ -1399,8 +1403,8 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) error { } if packetSize[packet.m_headerType] > 1 { - res := t - if t > 0xffffff { + res := ts + if ts > 0xffffff { res = 0xffffff } C_AMF_EncodeInt24(headBytes[headerIdx:], int32(res)) @@ -1419,8 +1423,8 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) error { headerIdx += n } - if t >= 0xffffff { - C_AMF_EncodeInt32(headBytes[headerIdx:], int32(t)) + if ts >= 0xffffff { + C_AMF_EncodeInt32(headBytes[headerIdx:], int32(ts)) headerIdx += 4 // 32bits } @@ -1429,7 +1433,7 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) error { if debugMode { if r.Link.conn != nil { - log.Printf("C_RTMP_SendPacket: %v=>%v, size=%v", r.Link.conn.LocalAddr(), r.Link.conn.RemoteAddr(), nSize) + log.Printf("C_RTMP_SendPacket: %v->%v, size=%v", r.Link.conn.LocalAddr(), r.Link.conn.RemoteAddr(), nSize) } } @@ -1453,7 +1457,7 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) error { origIdx -= 1 + cSize hSize = 1 + cSize - if t >= 0xffffff { + if ts >= 0xffffff { origIdx -= 4 hSize += 4 } @@ -1468,9 +1472,9 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) error { headBytes[origIdx+2] = byte(tmp >> 8) } } - if t >= 0xffffff { + if ts >= 0xffffff { extendedTimestamp := headBytes[origIdx+1+cSize:] - C_AMF_EncodeInt32(extendedTimestamp[:4], int32(t)) + C_AMF_EncodeInt32(extendedTimestamp[:4], int32(ts)) } } }