forked from Unity-Technologies/com.unity.webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataChannelSample.cs
288 lines (255 loc) · 8.86 KB
/
DataChannelSample.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
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using Unity.WebRTC;
using System;
public class DataChannelSample : MonoBehaviour
{
#pragma warning disable 0649
[SerializeField] private Button callButton;
[SerializeField] private Button sendButton;
[SerializeField] private InputField textSend;
[SerializeField] private InputField textReceive;
#pragma warning restore 0649
private RTCPeerConnection pc1, pc2;
private RTCDataChannel dataChannel, remoteDataChannel;
private Coroutine sdpCheck;
private string msg;
private DelegateOnIceConnectionChange pc1OnIceConnectionChange;
private DelegateOnIceConnectionChange pc2OnIceConnectionChange;
private DelegateOnIceCandidate pc1OnIceCandidate;
private DelegateOnIceCandidate pc2OnIceCandidate;
private DelegateOnMessage onDataChannelMessage;
private DelegateOnOpen onDataChannelOpen;
private DelegateOnClose onDataChannelClose;
private DelegateOnDataChannel onDataChannel;
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(() => { StartCoroutine(Call()); });
}
private void OnDestroy()
{
WebRTC.Finalize();
}
private void Start()
{
callButton.interactable = true;
pc1OnIceConnectionChange = new DelegateOnIceConnectionChange(state => { OnIceConnectionChange(pc1, state); });
pc2OnIceConnectionChange = new DelegateOnIceConnectionChange(state => { OnIceConnectionChange(pc2, state); });
pc1OnIceCandidate = new DelegateOnIceCandidate(candidate => { OnIceCandidate(pc1, candidate); });
pc2OnIceCandidate = new DelegateOnIceCandidate(candidate => { OnIceCandidate(pc1, candidate); });
onDataChannel = new DelegateOnDataChannel(channel =>
{
remoteDataChannel = channel;
remoteDataChannel.OnMessage = onDataChannelMessage;
});
onDataChannelMessage = new DelegateOnMessage(bytes => { textReceive.text = System.Text.Encoding.UTF8.GetString(bytes); });
onDataChannelOpen = new DelegateOnOpen(()=> { sendButton.interactable = true; });
onDataChannelClose = new DelegateOnClose(() => { sendButton.interactable = false; });
}
RTCConfiguration GetSelectedSdpSemantics()
{
RTCConfiguration config = default;
config.iceServers = new RTCIceServer[]
{
new RTCIceServer { urls = new string[] { "stun:stun.l.google.com:19302" } }
};
return config;
}
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:
break;
}
}
void Pc1OnIceConnectinChange(RTCIceConnectionState state)
{
OnIceConnectionChange(pc1, state);
}
void Pc2OnIceConnectionChange(RTCIceConnectionState state)
{
OnIceConnectionChange(pc2, state);
}
void Pc1OnIceCandidate(RTCIceCandidate candidate)
{
OnIceCandidate(pc1, candidate);
}
void Pc2OnIceCandidate(RTCIceCandidate candidate)
{
OnIceCandidate(pc2, candidate);
}
IEnumerator 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;
pc2 = new RTCPeerConnection(ref configuration);
Debug.Log("Created remote peer connection object pc2");
pc2.OnIceCandidate = pc2OnIceCandidate;
pc2.OnIceConnectionChange = pc2OnIceConnectionChange;
pc2.OnDataChannel = onDataChannel;
RTCDataChannelInit conf = new RTCDataChannelInit(true);
dataChannel = pc1.CreateDataChannel("data", ref conf);
dataChannel.OnOpen = onDataChannelOpen;
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);
}
}
/// <summary>
///
/// </summary>
/// <param name="pc"></param>
/// <param name="streamEvent"></param>
void OnIceCandidate(RTCPeerConnection pc, RTCIceCandidate candidate)
{
GetOtherPc(pc).AddIceCandidate(ref candidate);
Debug.Log($"{GetName(pc)} ICE candidate:\n {candidate.candidate}");
}
public void SendMsg()
{
dataChannel.Send(textSend.text);
}
string GetName(RTCPeerConnection pc)
{
return (pc == pc1) ? "pc1" : "pc2";
}
RTCPeerConnection GetOtherPc(RTCPeerConnection pc)
{
return (pc == pc1) ? pc2 : pc1;
}
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);
}
}
void OnSetLocalSuccess(RTCPeerConnection pc)
{
Debug.Log($"{GetName(pc)} SetLocalDescription complete");
}
void OnSetSessionDescriptionError(ref RTCError error) { }
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);
}
}
void OnAddIceCandidateSuccess(RTCPeerConnection pc)
{
Debug.Log($"{GetName(pc)} addIceCandidate success");
}
void OnAddIceCandidateError(RTCPeerConnection pc, RTCError error)
{
Debug.Log($"{GetName(pc)} failed to add ICE Candidate: ${error}");
}
void OnCreateSessionDescriptionError(RTCError e)
{
}
}