-
Notifications
You must be signed in to change notification settings - Fork 8
/
client_test.go
273 lines (207 loc) · 6.65 KB
/
client_test.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package sfu
import (
"context"
"fmt"
"testing"
"time"
"github.com/pion/webrtc/v4"
"github.com/stretchr/testify/require"
)
func TestTracksSubscribe(t *testing.T) {
report := CheckRoutines(t)
defer report()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create room manager first before create new room
roomManager := NewManager(ctx, "test", sfuOpts)
defer roomManager.Close()
roomID := roomManager.CreateRoomID()
roomName := "test-room"
peerCount := 5
// create new room
roomOpts := DefaultRoomOptions()
roomOpts.Codecs = &[]string{webrtc.MimeTypeH264, webrtc.MimeTypeOpus}
testRoom, err := roomManager.NewRoom(roomID, roomName, RoomTypeLocal, roomOpts)
defer testRoom.Close()
require.NoError(t, err, "error creating room: %v", err)
tracksAddedChan := make(chan int)
tracksAvailableChan := make(chan int)
trackChan := make(chan bool)
peers := make([]*PC, 0)
clients := make([]*Client, 0)
for i := 0; i < peerCount; i++ {
pc, client, _, _ := CreatePeerPair(ctx, TestLogger, testRoom, DefaultTestIceServers(), fmt.Sprintf("peer-%d", i), true, false, true)
peers = append(peers, pc)
clients = append(clients, client)
client.OnTracksAdded(func(addedTracks []ITrack) {
tracksAddedChan <- len(addedTracks)
setTracks := make(map[string]TrackType)
for _, track := range addedTracks {
setTracks[track.ID()] = TrackTypeMedia
}
client.SetTracksSourceType(setTracks)
})
pc.PeerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
trackChan <- true
})
}
timeout, cancelTimeout := context.WithTimeout(ctx, 30*time.Second)
defer cancelTimeout()
tracksAdded := 0
tracksAvailable := 0
trackReceived := 0
expectedTracks := (peerCount * 2) * (peerCount - 1)
Loop:
for {
select {
case <-timeout.Done():
break Loop
case added := <-tracksAddedChan:
tracksAdded += added
case available := <-tracksAvailableChan:
tracksAvailable += available
case <-trackChan:
trackReceived++
if trackReceived == expectedTracks {
break Loop
}
}
}
require.Equal(t, peerCount*2, tracksAdded)
require.Equal(t, expectedTracks, trackReceived)
for _, client := range clients {
require.NoError(t, testRoom.StopClient(client.id))
}
for _, pc := range peers {
require.NoError(t, pc.PeerConnection.Close())
}
}
// TODO: this is can't be work without a new SimulcastLocalTrack that can add header extension to the packet
func TestSimulcastTrack(t *testing.T) {
report := CheckRoutines(t)
defer report()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create room manager first before create new room
roomManager := NewManager(ctx, "test", sfuOpts)
defer roomManager.Close()
roomID := roomManager.CreateRoomID()
roomName := "test-room"
// create new room
roomOpts := DefaultRoomOptions()
roomOpts.Codecs = &[]string{webrtc.MimeTypeH264, webrtc.MimeTypeOpus}
testRoom, err := roomManager.NewRoom(roomID, roomName, RoomTypeLocal, roomOpts)
require.NoError(t, err, "error creating room: %v", err)
simulcastChan := make(chan *SimulcastTrack)
client1, pc1 := addSimulcastPair(t, ctx, testRoom, "peer1", simulcastChan)
client2, pc2 := addSimulcastPair(t, ctx, testRoom, "peer2", simulcastChan)
defer func() {
_ = testRoom.StopClient(client1.id)
_ = testRoom.StopClient(client2.id)
}()
trackChan := make(chan *webrtc.TrackRemote)
pc1.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
trackChan <- track
})
pc2.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
trackChan <- track
})
// wait for track added
timeout, cancelTimeout := context.WithTimeout(ctx, 30*time.Second)
defer cancelTimeout()
trackCount := 0
simulcastCount := 0
Loop:
for {
select {
case <-timeout.Done():
t.Fatal("timeout waiting for track added")
break Loop
case <-trackChan:
trackCount++
t.Log("track added ", trackCount)
case simulcastTrack := <-simulcastChan:
go func() {
ctxx, cancell := context.WithCancel(ctx)
defer cancell()
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctxx.Done():
return
case <-ticker.C:
if simulcastTrack.remoteTrackHigh != nil &&
simulcastTrack.remoteTrackMid != nil &&
simulcastTrack.remoteTrackLow != nil {
simulcastCount++
t.Log("simulcast track complete ", simulcastCount)
return
}
}
}
}()
default:
if trackCount == 2 && simulcastCount == 2 {
break Loop
}
}
}
require.Equal(t, 2, trackCount)
require.Equal(t, 2, simulcastCount)
}
func addSimulcastPair(t *testing.T, ctx context.Context, room *Room, peerName string, simulcastTrackChan chan *SimulcastTrack) (*Client, *webrtc.PeerConnection) {
pc, client, _, _ := CreatePeerPair(ctx, TestLogger, room, DefaultTestIceServers(), peerName, true, true, true)
client.OnTracksAvailable(func(availableTracks []ITrack) {
for _, track := range availableTracks {
if track.IsSimulcast() {
simulcastTrackChan <- track.(*SimulcastTrack)
}
}
})
client.OnTracksAdded(func(addedTracks []ITrack) {
setTracks := make(map[string]TrackType, 0)
for _, track := range addedTracks {
setTracks[track.ID()] = TrackTypeMedia
}
client.SetTracksSourceType(setTracks)
})
pc.PeerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
t.Log("test: on track", track.Msid())
})
return client, pc.PeerConnection
}
func TestClientDataChannel(t *testing.T) {
report := CheckRoutines(t)
defer report()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create room manager first before create new room
roomManager := NewManager(ctx, "test", sfuOpts)
defer roomManager.Close()
roomID := roomManager.CreateRoomID()
roomName := "test-room"
// create new room
roomOpts := DefaultRoomOptions()
roomOpts.Codecs = &[]string{webrtc.MimeTypeH264, webrtc.MimeTypeOpus}
testRoom, err := roomManager.NewRoom(roomID, roomName, RoomTypeLocal, roomOpts)
require.NoError(t, err, "error creating room: %v", err)
dcChan := make(chan *webrtc.DataChannel)
pc, client, _, connChan := CreateDataPair(ctx, TestLogger, testRoom, roomManager.options.IceServers, "peer1", func(c *webrtc.DataChannel) {
dcChan <- c
})
timeout, cancelTimeout := context.WithTimeout(ctx, 30*time.Second)
defer cancelTimeout()
defer pc.Close()
select {
case <-timeout.Done():
t.Fatal("timeout waiting for data channel")
case state := <-connChan:
if state == webrtc.PeerConnectionStateConnected {
_, _ = pc.CreateDataChannel("test", nil)
negotiate(pc, client, TestLogger, true)
}
case dc := <-dcChan:
require.Equal(t, "internal", dc.Label())
}
}