diff --git a/pkg/flv/flv_test.go b/pkg/flv/flv_test.go new file mode 100644 index 00000000..389272b0 --- /dev/null +++ b/pkg/flv/flv_test.go @@ -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 + } +} diff --git a/pkg/flv/producer.go b/pkg/flv/producer.go index 9748c94a..38c601d5 100644 --- a/pkg/flv/producer.go +++ b/pkg/flv/producer.go @@ -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 {