-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathabscapturetimeextension_test.go
More file actions
138 lines (113 loc) · 3.92 KB
/
abscapturetimeextension_test.go
File metadata and controls
138 lines (113 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package rtp
import (
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestAbsCaptureTimeExtension_Roundtrip(t *testing.T) { //nolint:cyclop
t.Run("positive captureClockOffset", func(t *testing.T) {
t0 := time.Now()
e1 := NewAbsCaptureTimeExtension(t0)
b1, err := e1.Marshal()
assert.NoError(t, err)
var o1 AbsCaptureTimeExtension
assert.NoError(t, o1.Unmarshal(b1))
dt1 := o1.CaptureTime().Sub(t0).Seconds()
assert.GreaterOrEqual(t, dt1, -0.001)
assert.LessOrEqual(t, dt1, 0.001)
assert.Nil(t, o1.EstimatedCaptureClockOffsetDuration())
e2 := NewAbsCaptureTimeExtensionWithCaptureClockOffset(t0, 1250*time.Millisecond)
b2, err := e2.Marshal()
assert.NoError(t, err)
var o2 AbsCaptureTimeExtension
assert.NoError(t, o2.Unmarshal(b2))
dt2 := o1.CaptureTime().Sub(t0).Seconds()
assert.GreaterOrEqual(t, dt2, -0.001)
assert.LessOrEqual(t, dt2, 0.001)
assert.Equal(t, 1250*time.Millisecond, *o2.EstimatedCaptureClockOffsetDuration())
})
// This test can verify the for for the issue 247
t.Run("negative captureClockOffset", func(t *testing.T) {
t0 := time.Now()
e1 := NewAbsCaptureTimeExtension(t0)
b1, err := e1.Marshal()
assert.NoError(t, err)
var o1 AbsCaptureTimeExtension
assert.NoError(t, o1.Unmarshal(b1))
dt1 := o1.CaptureTime().Sub(t0).Seconds()
assert.GreaterOrEqual(t, dt1, -0.001)
assert.LessOrEqual(t, dt1, 0.001)
assert.Nil(t, o1.EstimatedCaptureClockOffsetDuration())
e2 := NewAbsCaptureTimeExtensionWithCaptureClockOffset(t0, -250*time.Millisecond)
b2, err := e2.Marshal()
assert.NoError(t, err)
var o2 AbsCaptureTimeExtension
assert.NoError(t, o2.Unmarshal(b2))
dt2 := o1.CaptureTime().Sub(t0).Seconds()
assert.GreaterOrEqual(t, dt2, -0.001)
assert.LessOrEqual(t, dt2, 0.001)
assert.Equal(t, -250*time.Millisecond, *o2.EstimatedCaptureClockOffsetDuration())
})
}
func TestAbsCaptureTimeExtensionMarshalTo(t *testing.T) {
t.Run("without offset", func(t *testing.T) {
ext := NewAbsCaptureTimeExtension(time.Now())
buf := make([]byte, ext.MarshalSize())
n, err := ext.MarshalTo(buf)
assert.NoError(t, err)
assert.Equal(t, ext.MarshalSize(), n)
expected, _ := ext.Marshal()
assert.Equal(t, expected, buf)
_, err = ext.MarshalTo(nil)
assert.ErrorIs(t, err, io.ErrShortBuffer)
})
t.Run("with offset", func(t *testing.T) {
ext := NewAbsCaptureTimeExtensionWithCaptureClockOffset(time.Now(), 100*time.Millisecond)
buf := make([]byte, ext.MarshalSize())
n, err := ext.MarshalTo(buf)
assert.NoError(t, err)
assert.Equal(t, ext.MarshalSize(), n)
expected, _ := ext.Marshal()
assert.Equal(t, expected, buf)
_, err = ext.MarshalTo(nil)
assert.ErrorIs(t, err, io.ErrShortBuffer)
})
}
//nolint:gochecknoglobals
var (
absCaptureTimeSink []byte
absCaptureTimeBuf = make([]byte, absCaptureTimeExtensionSize)
absCaptureTimeExtendedBuf = make([]byte, absCaptureTimeExtendedExtensionSize)
absCaptureTimeSinkInt int
)
func BenchmarkAbsCaptureTimeExtension_Marshal(b *testing.B) {
ext := NewAbsCaptureTimeExtension(time.Now())
b.ReportAllocs()
for b.Loop() {
absCaptureTimeSink, _ = ext.Marshal()
}
}
func BenchmarkAbsCaptureTimeExtension_MarshalTo(b *testing.B) {
ext := NewAbsCaptureTimeExtension(time.Now())
b.ReportAllocs()
for b.Loop() {
absCaptureTimeSinkInt, _ = ext.MarshalTo(absCaptureTimeBuf)
}
}
func BenchmarkAbsCaptureTimeExtensionWithOffset_Marshal(b *testing.B) {
ext := NewAbsCaptureTimeExtensionWithCaptureClockOffset(time.Now(), 100*time.Millisecond)
b.ReportAllocs()
for b.Loop() {
absCaptureTimeSink, _ = ext.Marshal()
}
}
func BenchmarkAbsCaptureTimeExtensionWithOffset_MarshalTo(b *testing.B) {
ext := NewAbsCaptureTimeExtensionWithCaptureClockOffset(time.Now(), 100*time.Millisecond)
b.ReportAllocs()
for b.Loop() {
absCaptureTimeSinkInt, _ = ext.MarshalTo(absCaptureTimeExtendedBuf)
}
}