Skip to content
Navigation Menu
{{ message }}
forked from Unity-Technologies/UnityRenderStreaming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignaling.cs
More file actions
272 lines (240 loc) · 8.45 KB
/
Copy pathSignaling.cs
File metadata and controls
272 lines (240 loc) · 8.45 KB
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
using System;
using UnityEngine;
using UnityEngine.Networking;
namespace Unity.RenderStreaming
{
public class DownloadHandlerJson<T> : DownloadHandlerScript
{
private T m_obj;
public DownloadHandlerJson() : base()
{
}
public DownloadHandlerJson(byte[] buffer) : base(buffer)
{
}
protected override byte[] GetData() { return null; }
protected override bool ReceiveData(byte[] data, int dataLength)
{
if (data == null || data.Length < 1)
{
return false;
}
var text = System.Text.Encoding.UTF8.GetString(data);
try
{
m_obj = JsonUtility.FromJson<T>(text);
}
catch(Exception e)
{
Debug.LogError(text);
throw e;
}
return true;
}
public T GetObject()
{
return m_obj;
}
}
static class DownloadHandlerExtension
{
public static T FromJson<T>(this DownloadHandler handler)
{
return JsonUtility.FromJson<T>(handler.text);
}
}
static class UnityWebRequestExtension
{
public static UnityWebRequestAsyncOperation SendWebRequest<T>(this UnityWebRequest own)
{
if (typeof(T) != typeof(None))
{
own.downloadHandler = new DownloadHandlerJson<T>();
}
if (own.uri.Scheme == "https")
{
own = own.ToHttps();
}
var req = own.SendWebRequest();
return req;
}
public static DownloadHandlerJson<T> DownloadHandlerJson<T>(this UnityWebRequest own)
{
return own.downloadHandler as DownloadHandlerJson<T>;
}
public static UnityWebRequest ToHttps(this UnityWebRequest own)
{
own.certificateHandler = new AcceptAllCertificateHandler();
return own;
}
class AcceptAllCertificateHandler : CertificateHandler
{
protected override bool ValidateCertificate(byte[] certificateData)
{
// Workaround for Non-Secure web page
// You should implement the validation when you release your service publicly.
return true;
}
}
}
class None {}
#pragma warning disable 0649
[Serializable]
class NewResData
{
public string sessionId;
}
[Serializable]
class CreateConnectionResData
{
public string connectionId;
}
[Serializable]
class OfferResDataList
{
public OfferResData[] offers;
}
[Serializable]
class CandidateContainerResDataList
{
public CandidateContainerResData[] candidates;
}
[Serializable]
class OfferResData
{
public string connectionId;
public string sdp;
}
[Serializable]
class AnswerResData
{
public string connectionId;
public string sdp;
}
[Serializable]
class CandidateContainerResData
{
public string connectionId;
public CandidateResData[] candidates;
}
[Serializable]
class CandidateResData
{
public string candidate;
public string sdpMid;
public int sdpMLineIndex;
}
#pragma warning restore 0649
public class Signaling
{
public string Url { get; }
public Signaling(string url)
{
Url = url;
}
[Serializable]
class OfferReqData
{
public string connectionId;
public string sdp;
}
[Serializable]
class AnswerReqData
{
public string connectionId;
public string sdp;
}
[Serializable]
class CandidateReqData
{
public string connectionId;
public string candidate;
public string sdpMid;
public int sdpMLineIndex;
}
public UnityWebRequestAsyncOperation Create()
{
var req = new UnityWebRequest($"{Url}/signaling", "PUT");
var op = req.SendWebRequest<NewResData>();
return op;
}
public UnityWebRequestAsyncOperation Delete()
{
var req = new UnityWebRequest($"{Url}/signaling", "DELETE");
var op = req.SendWebRequest<None>();
return op;
}
public UnityWebRequestAsyncOperation CreateConnection(string sessionId)
{
var req = new UnityWebRequest($"{Url}/signaling/connection", "PUT");
req.SetRequestHeader("Session-Id", sessionId);
var op = req.SendWebRequest<CreateConnectionResData>();
return op;
}
public UnityWebRequestAsyncOperation DeleteConnection(string sessionId, string connectionId)
{
var obj = new AnswerReqData { connectionId = connectionId };
var data = new System.Text.UTF8Encoding().GetBytes(JsonUtility.ToJson(obj));
var req = new UnityWebRequest($"{Url}/signaling/connection", "DELETE");
req.SetRequestHeader("Session-Id", sessionId);
req.SetRequestHeader("Content-Type", "application/json");
req.uploadHandler = new UploadHandlerRaw(data);
var op = req.SendWebRequest<None>();
return op;
}
public UnityWebRequestAsyncOperation PostOffer(string sessionId, string connectionId, string sdp)
{
var obj = new OfferReqData { connectionId = connectionId, sdp = sdp };
var data = new System.Text.UTF8Encoding().GetBytes(JsonUtility.ToJson(obj));
var req = new UnityWebRequest($"{Url}/signaling/offer", "POST");
req.SetRequestHeader("Session-Id", sessionId);
req.SetRequestHeader("Content-Type", "application/json");
req.uploadHandler = new UploadHandlerRaw(data);
var op = req.SendWebRequest<None>();
return op;
}
public UnityWebRequestAsyncOperation GetOffer(string sessionId, long fromTime=0)
{
var req = new UnityWebRequest($"{Url}/signaling/offer?fromtime={fromTime}", "GET");
req.SetRequestHeader("Session-Id", sessionId);
var op = req.SendWebRequest<OfferResDataList>();
return op;
}
public UnityWebRequestAsyncOperation PostAnswer(string sessionId, string connectionId, string sdp)
{
var obj = new AnswerReqData { connectionId = connectionId, sdp = sdp };
var data = new System.Text.UTF8Encoding().GetBytes(JsonUtility.ToJson(obj));
var req = new UnityWebRequest($"{Url}/signaling/answer", "POST");
req.SetRequestHeader("Session-Id", sessionId);
req.SetRequestHeader("Content-Type", "application/json");
req.uploadHandler = new UploadHandlerRaw(data);
var op = req.SendWebRequest<None>();
return op;
}
public UnityWebRequestAsyncOperation GetAnswer(string sessionId, string connectionId, long fromTime = 0)
{
var req = new UnityWebRequest($"{Url}/signaling/answer?fromtime={fromTime}", "GET");
req.SetRequestHeader("Session-Id", sessionId);
var op = req.SendWebRequest<AnswerResData>();
return op;
}
public UnityWebRequestAsyncOperation PostCandidate(string sessionId, string connectionId, string candidate, string sdpMid, int sdpMlineIndex)
{
var obj = new CandidateReqData { connectionId = connectionId, candidate = candidate, sdpMid = sdpMid, sdpMLineIndex = sdpMlineIndex };
var data = new System.Text.UTF8Encoding().GetBytes(JsonUtility.ToJson(obj));
var req = new UnityWebRequest($"{Url}/signaling/candidate", "POST");
req.SetRequestHeader("Session-Id", sessionId);
req.SetRequestHeader("Content-Type", "application/json");
req.uploadHandler = new UploadHandlerRaw(data);
var op = req.SendWebRequest<None>();
return op;
}
public UnityWebRequestAsyncOperation GetCandidate(string sessionId, long fromTime = 0)
{
var req = new UnityWebRequest($"{Url}/signaling/candidate?fromtime={fromTime}", "GET");
req.SetRequestHeader("Session-Id", sessionId);
var op = req.SendWebRequest<CandidateContainerResDataList>();
return op;
}
}
}
You can’t perform that action at this time.
