Fix timestamp processing for HTTP-FLV

This commit is contained in:
Alex X
2025-11-19 12:58:23 +03:00
parent 42e7a03534
commit 68036b68c1
2 changed files with 27 additions and 2 deletions
+21
View File
@@ -0,0 +1,21 @@
package flv
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestTimeToRTP(t *testing.T) {
// Reolink camera has 20 FPS
// Video timestamp increases by 50ms, SampleRate 90000, RTP timestamp increases by 4500
// Audio timestamp increases by 64ms, SampleRate 16000, RTP timestamp increases by 1024
frameN := 1
for i := 0; i < 32; i++ {
// 1000ms/(90000/4500) = 50ms
require.Equal(t, uint32(frameN*4500), TimeToRTP(uint32(frameN*50), 90000))
// 1000ms/(16000/1024) = 64ms
require.Equal(t, uint32(frameN*1024), TimeToRTP(uint32(frameN*64), 16000))
frameN *= 2
}
}
+6 -2
View File
@@ -299,8 +299,12 @@ func (c *Producer) readPacket() (*rtp.Packet, error) {
return pkt, nil
}
func TimeToRTP(timeMS uint32, clockRate uint32) uint32 {
return timeMS * clockRate / 1000
// TimeToRTP convert time in milliseconds to RTP time
func TimeToRTP(timeMS, clockRate uint32) uint32 {
// for clockRates 90000, 16000, 8000, etc. - we can use:
// return timeMS * (clockRate / 1000)
// but for clockRates 44100, 22050, 11025 - we should use:
return uint32(uint64(timeMS) * uint64(clockRate) / 1000)
}
func isExHeader(data []byte) bool {