-
Notifications
You must be signed in to change notification settings - Fork 8
/
iext_test.go
217 lines (164 loc) · 5.54 KB
/
iext_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
package sfu
import (
"context"
"testing"
"time"
"github.com/pion/webrtc/v4"
"github.com/stretchr/testify/require"
)
type testManagerExtension struct {
onGetRoom bool
onBeforeNewRoom bool
onNewRoom bool
onRoomClosed bool
}
type testExtension struct {
onBeforeClientAdded bool
onClientAdded bool
onClientRemoved bool
}
func NewTestExtension() *testExtension {
return &testExtension{}
}
func (t *testExtension) OnBeforeClientAdded(room *Room, id string) error {
t.onBeforeClientAdded = true
return nil
}
func (t *testExtension) OnClientAdded(room *Room, client *Client) {
t.onClientAdded = true
}
func (t *testExtension) OnClientRemoved(room *Room, client *Client) {
t.onClientRemoved = true
}
func NewTestManagerExtension() *testManagerExtension {
return &testManagerExtension{}
}
func (t *testManagerExtension) OnGetRoom(manager *Manager, roomID string) (*Room, error) {
t.onGetRoom = true
return nil, nil
}
func (t *testManagerExtension) OnBeforeNewRoom(id, name, roomType string) error {
t.onBeforeNewRoom = true
return nil
}
func (t *testManagerExtension) OnNewRoom(manager *Manager, room *Room) {
t.onNewRoom = true
}
func (t *testManagerExtension) OnRoomClosed(manager *Manager, room *Room) {
t.onRoomClosed = true
}
func TestManagerExtension(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()
managerExt := NewTestManagerExtension()
roomManager.AddExtension(managerExt)
roomID := roomManager.CreateRoomID()
roomOpts := DefaultRoomOptions()
roomOpts.Codecs = &[]string{webrtc.MimeTypeH264, webrtc.MimeTypeOpus}
room, err := roomManager.NewRoom(roomID, "test", "p2p", roomOpts)
require.NotNil(t, room, "room is nil")
require.NoError(t, err, "error creating room")
room.Close()
_, err = roomManager.GetRoom("wrong-room-id")
require.NoError(t, err, "error getting room")
require.True(t, managerExt.onGetRoom, "OnGetRoom is not called")
require.True(t, managerExt.onBeforeNewRoom, "OnBeforeNewRoom is not called")
require.True(t, managerExt.onNewRoom, "OnNewRoom is not called")
require.True(t, managerExt.onRoomClosed, "OnRoomClosed is not called")
}
func TestExtension(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()
// create new room
roomOpts := DefaultRoomOptions()
roomOpts.Codecs = &[]string{webrtc.MimeTypeH264, webrtc.MimeTypeOpus}
testRoom, err := roomManager.NewRoom("test", "test", RoomTypeLocal, roomOpts)
ext := NewTestExtension()
testRoom.AddExtension(ext)
require.NoError(t, err, "error creating room: %v", err)
leftChan := make(chan bool)
joinChan := make(chan bool)
peerCount := 0
iceConnectedCtx, iceConnectedCtxCancel := context.WithCancel(ctx)
tracks, _ := GetStaticTracks(ctx, iceConnectedCtx, "test", true)
mediaEngine := GetMediaEngine()
testRoom.OnClientLeft(func(client *Client) {
leftChan <- true
})
testRoom.OnClientJoined(func(client *Client) {
joinChan <- true
})
// add a new client to room
// you can also get the client by using r.GetClient(clientID)
id := testRoom.CreateClientID()
client1, _ := testRoom.AddClient(id, id, DefaultClientOptions())
webrtcAPI := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine))
peer1, err := webrtcAPI.NewPeerConnection(webrtc.Configuration{
ICEServers: DefaultTestIceServers(),
})
peer1.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
if connectionState == webrtc.ICEConnectionStateConnected {
iceConnectedCtxCancel()
}
})
client1.OnIceCandidate(func(ctx context.Context, candidate *webrtc.ICECandidate) {
if candidate == nil {
return
}
err = peer1.AddICECandidate(candidate.ToJSON())
require.NoErrorf(t, err, "error adding ice candidate: %v", err)
})
require.NoErrorf(t, err, "error creating peer connection: %v", err)
SetPeerConnectionTracks(ctx, peer1, tracks)
offer, err := peer1.CreateOffer(nil)
require.NoErrorf(t, err, "error creating offer: %v", err)
err = peer1.SetLocalDescription(offer)
require.NoErrorf(t, err, "error setting local description: %v", err)
answer, err := client1.Negotiate(offer)
require.NoErrorf(t, err, "error negotiating offer: %v", err)
err = peer1.SetRemoteDescription(*answer)
require.NoErrorf(t, err, "error setting remote description: %v", err)
peer1.OnICECandidate(func(candidate *webrtc.ICECandidate) {
if candidate == nil {
return
}
err = client1.PeerConnection().PC().AddICECandidate(candidate.ToJSON())
require.NoErrorf(t, err, "error adding ice candidate: %v", err)
})
timeout, cancelTimeout := context.WithTimeout(ctx, 30*time.Second)
defer cancelTimeout()
for {
select {
case <-timeout.Done():
t.Fatal("timeout waiting for client left event")
case <-leftChan:
t.Logf("client left")
peerCount--
case <-joinChan:
t.Logf("client joined")
peerCount++
// stop client in go routine so we can receive left event
go func() {
_ = testRoom.StopClient(client1.ID())
}()
}
t.Log("peer count", peerCount)
if peerCount == 0 {
break
}
}
_ = testRoom.Close()
require.True(t, ext.onBeforeClientAdded, "OnBeforeClientAdded is not called")
require.True(t, ext.onClientAdded, "OnClientAdded is not called")
require.True(t, ext.onClientRemoved, "OnClientRemoved is not called")
}