forked from Unity-Technologies/com.unity.webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaStreamSample.cs
321 lines (287 loc) · 10 KB
/
MediaStreamSample.cs
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.WebRTC;
using UnityEngine.UI;
using System.Text;
[RequireComponent(typeof(AudioListener))]
public class MediaStreamSample : MonoBehaviour
{
#pragma warning disable 0649
[SerializeField] private Button callButton;
[SerializeField] private Button addTracksButton;
[SerializeField] private Button removeTracksButton;
[SerializeField] private Camera cam;
[SerializeField] private InputField infoText;
[SerializeField] private RawImage RtImage;
#pragma warning restore 0649
private RTCPeerConnection _pc1, _pc2;
private List<RTCRtpSender> pc1Senders, pc2Senders;
private MediaStream audioStream, videoStream;
private RTCDataChannel remoteDataChannel;
private Coroutine sdpCheck;
private string msg;
private DelegateOnIceConnectionChange pc1OnIceConnectionChange;
private DelegateOnIceConnectionChange pc2OnIceConnectionChange;
private DelegateOnIceCandidate pc1OnIceCandidate;
private DelegateOnIceCandidate pc2OnIceCandidate;
private DelegateOnTrack pc2Ontrack;
private DelegateOnNegotiationNeeded pc1OnNegotiationNeeded;
private StringBuilder trackInfos;
private bool videoUpdateStarted;
private RTCOfferOptions _offerOptions = new RTCOfferOptions
{
iceRestart = false,
offerToReceiveAudio = true,
offerToReceiveVideo = false
};
private RTCAnswerOptions _answerOptions = new RTCAnswerOptions
{
iceRestart = false,
};
private void Awake()
{
WebRTC.Initialize();
callButton.onClick.AddListener(Call);
addTracksButton.onClick.AddListener(AddTracks);
removeTracksButton.onClick.AddListener(RemoveTracks);
}
private void OnDestroy()
{
Audio.Stop();
WebRTC.Finalize();
}
private void Start()
{
trackInfos = new StringBuilder();
pc1Senders = new List<RTCRtpSender>();
pc2Senders = new List<RTCRtpSender>();
callButton.interactable = true;
pc1OnIceConnectionChange = state => { OnIceConnectionChange(_pc1, state); };
pc2OnIceConnectionChange = state => { OnIceConnectionChange(_pc2, state); };
pc1OnIceCandidate = candidate => { OnIceCandidate(_pc1, candidate); };
pc2OnIceCandidate = candidate => { OnIceCandidate(_pc2, candidate); };
pc2Ontrack = e => { OnTrack(_pc2, e); };
pc1OnNegotiationNeeded = () => { StartCoroutine(Pc1OnNegotiationNeeded()); };
infoText.text = !WebRTC.SupportHardwareEncoder ? "Current GPU doesn't support encoder" : "Current GPU supports encoder";
}
private static RTCConfiguration GetSelectedSdpSemantics()
{
RTCConfiguration config = default;
config.iceServers = new[]
{
new RTCIceServer { urls = new[] { "stun:stun.l.google.com:19302" } }
};
return config;
}
private void OnIceConnectionChange(RTCPeerConnection pc, RTCIceConnectionState state)
{
switch (state)
{
case RTCIceConnectionState.New:
Debug.Log($"{GetName(pc)} IceConnectionState: New");
break;
case RTCIceConnectionState.Checking:
Debug.Log($"{GetName(pc)} IceConnectionState: Checking");
break;
case RTCIceConnectionState.Closed:
Debug.Log($"{GetName(pc)} IceConnectionState: Closed");
break;
case RTCIceConnectionState.Completed:
Debug.Log($"{GetName(pc)} IceConnectionState: Completed");
break;
case RTCIceConnectionState.Connected:
Debug.Log($"{GetName(pc)} IceConnectionState: Connected");
break;
case RTCIceConnectionState.Disconnected:
Debug.Log($"{GetName(pc)} IceConnectionState: Disconnected");
break;
case RTCIceConnectionState.Failed:
Debug.Log($"{GetName(pc)} IceConnectionState: Failed");
break;
case RTCIceConnectionState.Max:
Debug.Log($"{GetName(pc)} IceConnectionState: Max");
break;
default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}
}
IEnumerator Pc1OnNegotiationNeeded()
{
Debug.Log("pc1 createOffer start");
var op = _pc1.CreateOffer(ref _offerOptions);
yield return op;
if (!op.isError)
{
yield return StartCoroutine(OnCreateOfferSuccess(op.desc));
}
else
{
OnCreateSessionDescriptionError(op.error);
}
}
private void AddTracks()
{
foreach (var track in audioStream.GetTracks())
{
pc1Senders.Add (_pc1.AddTrack(track));
}
foreach(var track in videoStream.GetTracks())
{
pc1Senders.Add(_pc1.AddTrack(track));
}
if(!videoUpdateStarted)
{
StartCoroutine(WebRTC.Update());
videoUpdateStarted = true;
}
addTracksButton.interactable = false;
removeTracksButton.interactable = true;
}
private void RemoveTracks()
{
foreach(var sender in pc1Senders)
{
_pc1.RemoveTrack(sender);
}
foreach (var sender in pc2Senders)
{
_pc2.RemoveTrack(sender);
}
pc1Senders.Clear();
pc2Senders.Clear();
addTracksButton.interactable = true;
removeTracksButton.interactable = false;
trackInfos.Clear();
infoText.text = "";
}
private void Call()
{
callButton.interactable = false;
Debug.Log("GetSelectedSdpSemantics");
var configuration = GetSelectedSdpSemantics();
_pc1 = new RTCPeerConnection(ref configuration);
Debug.Log("Created local peer connection object pc1");
_pc1.OnIceCandidate = pc1OnIceCandidate;
_pc1.OnIceConnectionChange = pc1OnIceConnectionChange;
_pc1.OnNegotiationNeeded = pc1OnNegotiationNeeded;
_pc2 = new RTCPeerConnection(ref configuration);
Debug.Log("Created remote peer connection object pc2");
_pc2.OnIceCandidate = pc2OnIceCandidate;
_pc2.OnIceConnectionChange = pc2OnIceConnectionChange;
_pc2.OnTrack = pc2Ontrack;
RTCDataChannelInit conf = new RTCDataChannelInit(true);
_pc1.CreateDataChannel("data", ref conf);
audioStream = Audio.CaptureStream();
videoStream = cam.CaptureStream(1280, 720);
RtImage.texture = cam.targetTexture;
}
private void OnIceCandidate(RTCPeerConnection pc, RTCIceCandidate candidate)
{
GetOtherPc(pc).AddIceCandidate(ref candidate);
Debug.Log($"{GetName(pc)} ICE candidate:\n {candidate.candidate}");
}
private void OnTrack(RTCPeerConnection pc, RTCTrackEvent e)
{
pc2Senders.Add(pc.AddTrack(e.Track));
trackInfos.Append($"{GetName(pc)} receives remote track:\r\n");
trackInfos.Append($"Track kind: {e.Track.Kind}\r\n");
trackInfos.Append($"Track id: {e.Track.Id}\r\n");
infoText.text = trackInfos.ToString();
}
private string GetName(RTCPeerConnection pc)
{
return (pc == _pc1) ? "pc1" : "pc2";
}
private RTCPeerConnection GetOtherPc(RTCPeerConnection pc)
{
return (pc == _pc1) ? _pc2 : _pc1;
}
private IEnumerator OnCreateOfferSuccess(RTCSessionDescription desc)
{
Debug.Log($"Offer from pc1\n{desc.sdp}");
Debug.Log("pc1 setLocalDescription start");
var op = _pc1.SetLocalDescription(ref desc);
yield return op;
if (!op.isError)
{
OnSetLocalSuccess(_pc1);
}
else
{
OnSetSessionDescriptionError(ref op.error);
}
Debug.Log("pc2 setRemoteDescription start");
var op2 = _pc2.SetRemoteDescription(ref desc);
yield return op2;
if (!op2.isError)
{
OnSetRemoteSuccess(_pc2);
}
else
{
OnSetSessionDescriptionError(ref op2.error);
}
Debug.Log("pc2 createAnswer start");
// Since the 'remote' side has no media stream we need
// to pass in the right constraints in order for it to
// accept the incoming offer of audio and video.
var op3 = _pc2.CreateAnswer(ref _answerOptions);
yield return op3;
if (!op3.isError)
{
yield return OnCreateAnswerSuccess(op3.desc);
}
else
{
OnCreateSessionDescriptionError(op3.error);
}
}
private void OnAudioFilterRead(float[] data, int channels)
{
Audio.Update(data, data.Length);
}
private void OnSetLocalSuccess(RTCPeerConnection pc)
{
Debug.Log($"{GetName(pc)} SetLocalDescription complete");
}
static void OnSetSessionDescriptionError(ref RTCError error)
{
Debug.LogError($"Error Detail Type: {error.errorDetail}");
}
private void OnSetRemoteSuccess(RTCPeerConnection pc)
{
Debug.Log($"{GetName(pc)} SetRemoteDescription complete");
}
IEnumerator OnCreateAnswerSuccess(RTCSessionDescription desc)
{
Debug.Log($"Answer from pc2:\n{desc.sdp}");
Debug.Log("pc2 setLocalDescription start");
var op = _pc2.SetLocalDescription(ref desc);
yield return op;
if (!op.isError)
{
OnSetLocalSuccess(_pc2);
}
else
{
OnSetSessionDescriptionError(ref op.error);
}
Debug.Log("pc1 setRemoteDescription start");
var op2 = _pc1.SetRemoteDescription(ref desc);
yield return op2;
if (!op2.isError)
{
OnSetRemoteSuccess(_pc1);
}
else
{
OnSetSessionDescriptionError(ref op2.error);
}
}
private static void OnCreateSessionDescriptionError(RTCError error)
{
Debug.LogError($"Error Detail Type: {error.errorDetail}");
}
}