-
Notifications
You must be signed in to change notification settings - Fork 8
/
track_test.go
151 lines (115 loc) · 3.64 KB
/
track_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
package sfu
import (
"context"
"errors"
"testing"
"github.com/pion/interceptor"
"github.com/pion/rtp"
"github.com/pion/webrtc/v4"
)
func TestVoiceActivityDetection(t *testing.T) {
}
func createPeerAudio(ctx context.Context, room *Room, iceServers []webrtc.ICEServer, peerName string) (*webrtc.PeerConnection, *Client, chan *webrtc.TrackRemote) {
var (
client *Client
mediaEngine *webrtc.MediaEngine = GetMediaEngine()
)
if len(iceServers) == 0 {
iceServers = []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302", "stun:127.0.0.1:3478"},
Username: "user",
Credential: "pass",
CredentialType: webrtc.ICECredentialTypePassword,
},
}
}
i := &interceptor.Registry{}
// Use the default set of Interceptors
if err := webrtc.RegisterDefaultInterceptors(mediaEngine, i); err != nil {
panic(err)
}
webrtcAPI := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine), webrtc.WithInterceptorRegistry(i))
pc, _ := webrtcAPI.NewPeerConnection(webrtc.Configuration{
ICEServers: iceServers,
})
trackChan := make(chan *webrtc.TrackRemote)
pc.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
trackChan <- track
})
audioTrack, _ := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: "audio/opus"}, "audio", "pion")
_, err := pc.AddTrack(audioTrack)
if err != nil {
panic(err)
}
go sendAudioPackets(ctx, audioTrack)
// add a new client to room
// you can also get the client by using r.GetClient(clientID)
id := room.CreateClientID()
opts := DefaultClientOptions()
opts.EnableVoiceDetection = true
client, _ = room.AddClient(id, id, opts)
client.OnAllowedRemoteRenegotiation(func() {
TestLogger.Info("allowed remote renegotiation")
negotiate(pc, client, TestLogger, true)
})
client.OnIceCandidate(func(ctx context.Context, candidate *webrtc.ICECandidate) {
if candidate == nil {
return
}
_ = pc.AddICECandidate(candidate.ToJSON())
})
client.OnRenegotiation(func(ctx context.Context, offer webrtc.SessionDescription) (answer webrtc.SessionDescription, e error) {
if client.state.Load() == ClientStateEnded {
TestLogger.Info("test: renegotiation canceled because client has ended")
return webrtc.SessionDescription{}, errors.New("client ended")
}
currentTranscv := len(pc.GetTransceivers())
TestLogger.Infof("test: got renegotiation ", peerName)
defer TestLogger.Infof("test: renegotiation done ", peerName)
if err := pc.SetRemoteDescription(offer); err != nil {
return webrtc.SessionDescription{}, err
}
answer, _ = pc.CreateAnswer(nil)
if err := pc.SetLocalDescription(answer); err != nil {
return webrtc.SessionDescription{}, err
}
newTcv := len(pc.GetTransceivers()) - currentTranscv
TestLogger.Infof("test: new transceiver ", newTcv, " total tscv ", len(pc.GetTransceivers()))
return *pc.LocalDescription(), nil
})
negotiate(pc, client, TestLogger, true)
pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
if candidate == nil {
return
}
client.PeerConnection().PC().AddICECandidate(candidate.ToJSON())
})
return pc, client, trackChan
}
func sendAudioPackets(ctx context.Context, track *webrtc.TrackLocalStaticRTP) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
i := 0
for {
select {
case <-ctx.Done():
return
default:
if err := track.WriteRTP(&rtp.Packet{
Header: rtp.Header{
Version: 2,
PayloadType: 96,
SequenceNumber: uint16(i),
Timestamp: uint32(i),
SSRC: 1,
},
Payload: []byte{0x00},
}); err != nil {
TestLogger.Errorf("error writing rtp packet: ", err.Error())
return
}
}
i++
}
}