Skip to content

Commit 9d4ff17

Browse files
committed
Reuse NTP time conversion in report package
Reduces some code duplication
1 parent 8cf0fda commit 9d4ff17

File tree

4 files changed

+9
-16
lines changed

4 files changed

+9
-16
lines changed

pkg/report/receiver_interceptor_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/pion/interceptor"
8+
"github.com/pion/interceptor/internal/ntp"
89
"github.com/pion/interceptor/internal/test"
910
"github.com/pion/logging"
1011
"github.com/pion/rtcp"
@@ -123,7 +124,7 @@ func TestReceiverInterceptor(t *testing.T) {
123124
stream.ReceiveRTCP([]rtcp.Packet{
124125
&rtcp.SenderReport{
125126
SSRC: 123456,
126-
NTPTime: ntpTime(now),
127+
NTPTime: ntp.ToNTP(now),
127128
RTPTime: 987654321 + uint32(now.Sub(rtpTime).Seconds()*90000),
128129
PacketCount: 10,
129130
OctetCount: 0,
@@ -237,7 +238,7 @@ func TestReceiverInterceptor(t *testing.T) {
237238
stream.ReceiveRTCP([]rtcp.Packet{
238239
&rtcp.SenderReport{
239240
SSRC: 123456,
240-
NTPTime: ntpTime(now),
241+
NTPTime: ntp.ToNTP(now),
241242
RTPTime: 987654321 + uint32(now.Sub(rtpTime).Seconds()*90000),
242243
PacketCount: 10,
243244
OctetCount: 0,
@@ -419,7 +420,7 @@ func TestReceiverInterceptor(t *testing.T) {
419420
stream.ReceiveRTCP([]rtcp.Packet{
420421
&rtcp.SenderReport{
421422
SSRC: 123456,
422-
NTPTime: ntpTime(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)),
423+
NTPTime: ntp.ToNTP(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)),
423424
RTPTime: 987654321,
424425
PacketCount: 0,
425426
OctetCount: 0,

pkg/report/sender_interceptor.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,3 @@ func (s *SenderInterceptor) BindLocalStream(info *interceptor.StreamInfo, writer
126126
return writer.Write(header, payload, a)
127127
})
128128
}
129-
130-
func ntpTime(t time.Time) uint64 {
131-
// seconds since 1st January 1900
132-
s := (float64(t.UnixNano()) / 1000000000) + 2208988800
133-
134-
// higher 32 bits are the integer part, lower 32 bits are the fractional part
135-
integerPart := uint32(s)
136-
fractionalPart := uint32((s - float64(integerPart)) * 0xFFFFFFFF)
137-
return uint64(integerPart)<<32 | uint64(fractionalPart)
138-
}

pkg/report/sender_interceptor_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/pion/interceptor"
8+
"github.com/pion/interceptor/internal/ntp"
89
"github.com/pion/interceptor/internal/test"
910
"github.com/pion/logging"
1011
"github.com/pion/rtcp"
@@ -40,7 +41,7 @@ func TestSenderInterceptor(t *testing.T) {
4041
assert.True(t, ok)
4142
assert.Equal(t, &rtcp.SenderReport{
4243
SSRC: 123456,
43-
NTPTime: ntpTime(mt.Now()),
44+
NTPTime: ntp.ToNTP(mt.Now()),
4445
RTPTime: 2269117121,
4546
PacketCount: 0,
4647
OctetCount: 0,
@@ -81,7 +82,7 @@ func TestSenderInterceptor(t *testing.T) {
8182
assert.True(t, ok)
8283
assert.Equal(t, &rtcp.SenderReport{
8384
SSRC: 123456,
84-
NTPTime: ntpTime(mt.Now()),
85+
NTPTime: ntp.ToNTP(mt.Now()),
8586
RTPTime: 2269117121,
8687
PacketCount: 10,
8788
OctetCount: 20,

pkg/report/sender_stream.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"sync"
55
"time"
66

7+
"github.com/pion/interceptor/internal/ntp"
78
"github.com/pion/rtcp"
89
"github.com/pion/rtp"
910
)
@@ -45,7 +46,7 @@ func (stream *senderStream) generateReport(now time.Time) *rtcp.SenderReport {
4546

4647
return &rtcp.SenderReport{
4748
SSRC: stream.ssrc,
48-
NTPTime: ntpTime(now),
49+
NTPTime: ntp.ToNTP(now),
4950
RTPTime: stream.lastRTPTimeRTP + uint32(now.Sub(stream.lastRTPTimeTime).Seconds()*stream.clockRate),
5051
PacketCount: stream.packetCount,
5152
OctetCount: stream.octetCount,

0 commit comments

Comments
 (0)