Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstream.cpp
More file actions
365 lines (257 loc) · 9.45 KB
/
Copy pathstream.cpp
File metadata and controls
365 lines (257 loc) · 9.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "stream.h"
#include <node_buffer.h>
#include <glib.h>
#include "agent.h"
#include "helper.h"
using namespace v8;
v8::Persistent<v8::Function> Stream::constructor;
// helper
const char* state_to_string(int state_) {
// to get notified if we miss a state
const NiceComponentState state = (NiceComponentState) state_;
switch(state) {
case NICE_COMPONENT_STATE_DISCONNECTED:
return "disconnected";
case NICE_COMPONENT_STATE_GATHERING:
return "gathering";
case NICE_COMPONENT_STATE_CONNECTING:
return "connecting";
case NICE_COMPONENT_STATE_CONNECTED:
return "connected";
case NICE_COMPONENT_STATE_READY:
return "ready";
case NICE_COMPONENT_STATE_FAILED:
return "failed";
case NICE_COMPONENT_STATE_LAST:
// not really a state
break;
}
return "unknown";
}
// lifecycle
void Stream::init(v8::Handle<v8::Object> exports) {
// Prepare constructor template
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->SetClassName(String::NewSymbol("NiceStream"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// protoype
NODE_SET_PROTOTYPE_METHOD(tpl, "gatherCandidates", gatherCandidates);
NODE_SET_PROTOTYPE_METHOD(tpl, "setRemoteCredentials", setRemoteCredentials);
NODE_SET_PROTOTYPE_METHOD(tpl, "getLocalCredentials", getLocalCredentials);
NODE_SET_PROTOTYPE_METHOD(tpl, "addRemoteIceCandidate", addRemoteIceCandidate);
NODE_SET_PROTOTYPE_METHOD(tpl, "getLocalIceCandidates", getLocalIceCandidates);
NODE_SET_PROTOTYPE_METHOD(tpl, "send", send);
NODE_SET_PROTOTYPE_METHOD(tpl, "setTos", setTos);
NODE_SET_PROTOTYPE_METHOD(tpl, "close", close);
constructor = Persistent<Function>::New(tpl->GetFunction());
// export
exports->Set(String::NewSymbol("NiceStream"), constructor);
}
Stream::Stream(Handle<Object> js_agent, int stream_id, int components)
: _js_agent(Persistent<Object>::New(js_agent)), _stream_id(stream_id), _components(components) {
DEBUG("stream " << stream_id << " with " << components << " components created");
Agent *agent = node::ObjectWrap::Unwrap<Agent>(js_agent);
_nice_agent = agent->agent();
}
Stream::~Stream() {
DEBUG("stream " << _stream_id << " is dying");
Agent *agent = node::ObjectWrap::Unwrap<Agent>(_js_agent);
agent->removeStream(_stream_id);
_js_agent.Dispose();
}
// callback forwarder
void Stream::receive(int component, const char* buf, size_t size) {
HandleScope scope;
const int argc = 3;
Handle<Value> argv[argc] = {
String::New("receive"),
Integer::New(component),
node::Buffer::New(buf, size)->handle_,
};
node::MakeCallback(handle_, "emit", argc, argv);
}
void Stream::stateChanged(int component, int state) {
HandleScope scope;
if(state == NICE_COMPONENT_STATE_DISCONNECTED || state == NICE_COMPONENT_STATE_FAILED) {
_working.erase(component);
}
checkIndependence();
const int argc = 3;
Handle<Value> argv[argc] = {
String::New("stateChanged"),
Integer::New(component),
String::New(state_to_string(state)),
};
node::MakeCallback(handle_, "emit", argc, argv);
}
void Stream::gatheringDone() {
HandleScope scope;
const int argc = 2;
Handle<Value> argv[argc] = {
String::New("gatheringDone"),
getLocalIceCandidates(),
};
node::MakeCallback(handle_, "emit", argc, argv);
}
// js functions
v8::Handle<v8::Value> Stream::New(const v8::Arguments& args) {
HandleScope scope;
if (args.IsConstructCall()) {
// Invoked as constructor: `new MyObject(...)`
Stream* obj = new Stream(args[0]->ToObject(), args[1]->IntegerValue(), args[2]->IntegerValue());
obj->Wrap(args.This());
return args.This();
} else {
// Invoked as plain function `MyObject(...)`, turn into construct call.
const int argc = 1;
Local<Value> argv[1] = { argv[0] };
return scope.Close(constructor->NewInstance(argc, argv));
}
}
v8::Handle<v8::Value> Stream::gatherCandidates(const v8::Arguments& args) {
HandleScope scope;
Stream *stream = node::ObjectWrap::Unwrap<Stream>(args.This()->ToObject());
NiceAgent *nice_agent = stream->_nice_agent;
int stream_id = stream->_stream_id;
for(int i = 1; i <= stream->_components; ++i) {
stream->_working.insert(i);
}
stream->checkIndependence();
bool res = nice_agent_gather_candidates(nice_agent, stream_id);
return scope.Close(Boolean::New(res));
}
v8::Handle<v8::Value> Stream::setRemoteCredentials(const v8::Arguments& args) {
HandleScope scope;
Stream *stream = node::ObjectWrap::Unwrap<Stream>(args.This()->ToObject());
NiceAgent *nice_agent = stream->_nice_agent;
int stream_id = stream->_stream_id;
v8::String::Utf8Value ufrag(args[0]->ToString());
v8::String::Utf8Value pwd(args[1]->ToString());
DEBUG("set remote credentials on stream " << stream_id << " to " << *ufrag << " " << *pwd);
bool res = nice_agent_set_remote_credentials(nice_agent, stream_id, *ufrag, *pwd);
return scope.Close(Boolean::New(res));
}
v8::Handle<v8::Value> Stream::getLocalCredentials(const v8::Arguments& args) {
HandleScope scope;
Stream *stream = node::ObjectWrap::Unwrap<Stream>(args.This()->ToObject());
NiceAgent *nice_agent = stream->_nice_agent;
int stream_id = stream->_stream_id;
gchar *ufrag, *pwd;
nice_agent_get_local_credentials(nice_agent, stream_id, &ufrag, &pwd);
Local<Object> res = Object::New();
res->Set(String::New("ufrag"), String::New(ufrag));
res->Set(String::New("pwd"), String::New(pwd));
DEBUG("local credentials are '" << ufrag << "' and '" << pwd << "'");
g_free(ufrag);
g_free(pwd);
return scope.Close(res);
}
v8::Handle<v8::Value> Stream::addRemoteIceCandidate(const v8::Arguments& args) {
HandleScope scope;
Stream *stream = node::ObjectWrap::Unwrap<Stream>(args.This()->ToObject());
v8::String::Utf8Value sdp(args[0]->ToString());
bool res = stream->addRemoteIceCandidate(*sdp);
return scope.Close(Boolean::New(res));
}
v8::Handle<v8::Value> Stream::getLocalIceCandidates(const v8::Arguments& args) {
HandleScope scope;
Stream *stream = node::ObjectWrap::Unwrap<Stream>(args.This()->ToObject());
Handle<Value> res = stream->getLocalIceCandidates();
return scope.Close(res);
}
v8::Handle<v8::Value> Stream::send(const v8::Arguments& args) {
HandleScope scope;
Stream *stream = node::ObjectWrap::Unwrap<Stream>(args.This()->ToObject());
NiceAgent *nice_agent = stream->_nice_agent;
int stream_id = stream->_stream_id;
int component = args[0]->IntegerValue();
if(!node::Buffer::HasInstance(args[1])) {
return ThrowException(Exception::TypeError(String::New("Expected buffer")));
}
Local<Object> buffer = args[1]->ToObject();
size_t size = node::Buffer::Length(buffer);
char* buf = node::Buffer::Data(buffer);
//DEBUG("sending " << size << " bytes");
int ret = nice_agent_send(nice_agent, stream_id, component, size, buf);
return scope.Close(Integer::New(ret));
}
v8::Handle<v8::Value> Stream::setTos(const v8::Arguments& args) {
HandleScope scope;
Stream *stream = node::ObjectWrap::Unwrap<Stream>(args.This()->ToObject());
NiceAgent *nice_agent = stream->_nice_agent;
int stream_id = stream->_stream_id;
int tos = args[0]->IntegerValue();
nice_agent_set_stream_tos(nice_agent, stream_id, tos);
return scope.Close(Undefined());
}
v8::Handle<v8::Value> Stream::close(const v8::Arguments& args) {
HandleScope scope;
Stream *stream = node::ObjectWrap::Unwrap<Stream>(args.This()->ToObject());
Agent *agent = node::ObjectWrap::Unwrap<Agent>(stream->_js_agent);
int stream_id = stream->_stream_id;
bool res = agent->removeStream(stream_id);
stream->_working.clear();
stream->checkIndependence();
DEBUG("closing stream " << stream->_stream_id);
return scope.Close(Boolean::New(res));
}
// helpers
v8::Handle<v8::Value> Stream::getLocalIceCandidates() {
HandleScope scope;
Local<Array> res = Array::New();
Local<Function> push = Local<Function>::Cast(res->Get(String::New("push")));
for(int i = 1; i <= _components; ++i) {
GSList* root = nice_agent_get_local_candidates(_nice_agent, _stream_id, i);
for(GSList* it = root; it; it = g_slist_next(it)) {
auto candidate = reinterpret_cast<NiceCandidate*>(it->data);
auto str = nice_agent_generate_local_candidate_sdp(_nice_agent, candidate);
const int argc = 1;
Local<Value> argv[argc] = {
String::New(str),
};
push->Call(res, argc, argv);
g_free(str);
}
g_slist_free(root);
}
return scope.Close(res);
}
bool Stream::addRemoteIceCandidate(const char* sdp_) {
// sanitize
std::string sdp(sdp_);
DEBUG("adding candidate '" << trim(sdp) << "' to stream " << _stream_id);
size_t small_udp = sdp.find(" udp ");
if(small_udp != std::string::npos) {
sdp.replace(small_udp, 5, " UDP ");
}
// parse sdp
auto nice_candidate = nice_agent_parse_remote_candidate_sdp(_nice_agent, _stream_id, sdp.c_str());
if(nice_candidate == NULL) {
DEBUG("was unable to parse the candidate");
return false;
}
if(nice_candidate->component_id > _components) {
DEBUG("component was invalid");
return false;
}
// insert into canidate list
GSList *candidates = nice_agent_get_remote_candidates(_nice_agent, _stream_id, nice_candidate->component_id);
candidates = g_slist_append(candidates, nice_candidate);
nice_agent_set_remote_candidates(_nice_agent, _stream_id, nice_candidate->component_id, candidates);
g_slist_free(candidates);
return true;
}
void Stream::checkIndependence() {
if(_working.size() > 0) {
if(_self.IsEmpty()) {
DEBUG("stream " << _stream_id << " got independent because it has work to do");
_self = Persistent<Object>::New(handle_);
}
} else {
if(!_self.IsEmpty()) {
DEBUG("stream " << _stream_id << " lost its independence");
_self.Dispose();
_self = Persistent<Object>();
}
}
}
You can’t perform that action at this time.
