forked from lynckia/licode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneToManyProcessor.cc
197 lines (157 loc) · 6.58 KB
/
OneToManyProcessor.cc
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
#ifndef BUILDING_NODE_EXTENSION
#define BUILDING_NODE_EXTENSION
#endif
#include "OneToManyProcessor.h"
using v8::Local;
using v8::Value;
using v8::Function;
using v8::FunctionTemplate;
using v8::HandleScope;
Nan::Persistent<Function> OneToManyProcessor::constructor;
// Async Delete OTM
// Classes for Async (not in node main thread) operations
class AsyncDeleter : public Nan::AsyncWorker {
public:
AsyncDeleter(erizo::OneToManyProcessor* otm, Nan::Callback *callback):
AsyncWorker(callback), otmToDelete_(otm) {
}
~AsyncDeleter() {}
void Execute() {
otmToDelete_->close();
delete otmToDelete_;
}
void HandleOKCallback() {
Nan::HandleScope scope;
std::string msg("OK");
if (callback) {
Local<Value> argv[] = {
Nan::New(msg.c_str()).ToLocalChecked()
};
callback->Call(1, argv);
}
}
private:
erizo::OneToManyProcessor* otmToDelete_;
Nan::Callback* callback_;
};
class AsyncRemoveSubscriber : public Nan::AsyncWorker{
public:
AsyncRemoveSubscriber(erizo::OneToManyProcessor* otm , const std::string& peerId, Nan::Callback *callback):
AsyncWorker(callback), otm_(otm), peerId_(peerId), callback_(callback) {
}
~AsyncRemoveSubscriber() {}
void Execute() {
otm_->removeSubscriber(peerId_);
}
void HandleOKCallback() {
// We're not doing anything here ATM
}
private:
erizo::OneToManyProcessor* otm_;
std::string peerId_;
Nan::Callback* callback_;
};
OneToManyProcessor::OneToManyProcessor() {
}
OneToManyProcessor::~OneToManyProcessor() {
}
NAN_MODULE_INIT(OneToManyProcessor::Init) {
// Prepare constructor template
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("OneToManyProcessor").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
Nan::SetPrototypeMethod(tpl, "close", close);
Nan::SetPrototypeMethod(tpl, "setPublisher", setPublisher);
Nan::SetPrototypeMethod(tpl, "addExternalOutput", addExternalOutput);
Nan::SetPrototypeMethod(tpl, "setExternalPublisher", setExternalPublisher);
Nan::SetPrototypeMethod(tpl, "getPublisherState", getPublisherState);
Nan::SetPrototypeMethod(tpl, "hasPublisher", hasPublisher);
Nan::SetPrototypeMethod(tpl, "addSubscriber", addSubscriber);
Nan::SetPrototypeMethod(tpl, "removeSubscriber", removeSubscriber);
constructor.Reset(tpl->GetFunction());
Nan::Set(target, Nan::New("OneToManyProcessor").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
}
NAN_METHOD(OneToManyProcessor::New) {
OneToManyProcessor* obj = new OneToManyProcessor();
obj->me = new erizo::OneToManyProcessor();
obj->msink = obj->me;
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
NAN_METHOD(OneToManyProcessor::close) {
OneToManyProcessor* obj = Nan::ObjectWrap::Unwrap<OneToManyProcessor>(info.Holder());
erizo::OneToManyProcessor *me = (erizo::OneToManyProcessor*)obj->me;
Nan::Callback *callback;
if (info.Length() >= 1) {
callback = new Nan::Callback(info[0].As<Function>());
} else {
callback = NULL;
}
Nan::AsyncQueueWorker(new AsyncDeleter(me, callback));
}
NAN_METHOD(OneToManyProcessor::setPublisher) {
OneToManyProcessor* obj = Nan::ObjectWrap::Unwrap<OneToManyProcessor>(info.Holder());
erizo::OneToManyProcessor *me = (erizo::OneToManyProcessor*)obj->me;
WebRtcConnection* param = Nan::ObjectWrap::Unwrap<WebRtcConnection>(Nan::To<v8::Object>(info[0]).ToLocalChecked());
auto wr = std::shared_ptr<erizo::WebRtcConnection>(param->me);
std::shared_ptr<erizo::MediaSource> ms = std::dynamic_pointer_cast<erizo::MediaSource>(wr);
me->setPublisher(ms);
}
NAN_METHOD(OneToManyProcessor::addExternalOutput) {
OneToManyProcessor* obj = Nan::ObjectWrap::Unwrap<OneToManyProcessor>(info.Holder());
erizo::OneToManyProcessor *me = (erizo::OneToManyProcessor*)obj->me;
ExternalOutput* param = Nan::ObjectWrap::Unwrap<ExternalOutput>(Nan::To<v8::Object>(info[0]).ToLocalChecked());
std::shared_ptr<erizo::ExternalOutput> wr = param->me;
auto ms = std::dynamic_pointer_cast<erizo::MediaSink>(wr);
// get the param
v8::String::Utf8Value param1(Nan::To<v8::String>(info[1]).ToLocalChecked());
// convert it to string
std::string peerId = std::string(*param1);
me->addSubscriber(ms, peerId);
}
NAN_METHOD(OneToManyProcessor::setExternalPublisher) {
OneToManyProcessor* obj = Nan::ObjectWrap::Unwrap<OneToManyProcessor>(info.Holder());
erizo::OneToManyProcessor *me = (erizo::OneToManyProcessor*)obj->me;
ExternalInput* param = Nan::ObjectWrap::Unwrap<ExternalInput>(Nan::To<v8::Object>(info[0]).ToLocalChecked());
std::shared_ptr<erizo::ExternalInput> wr = param->me;
std::shared_ptr<erizo::MediaSource> ms = std::dynamic_pointer_cast<erizo::MediaSource>(wr);
me->setPublisher(ms);
}
NAN_METHOD(OneToManyProcessor::getPublisherState) {
OneToManyProcessor* obj = Nan::ObjectWrap::Unwrap<OneToManyProcessor>(info.Holder());
erizo::OneToManyProcessor *me = (erizo::OneToManyProcessor*)obj->me;
auto wr = std::dynamic_pointer_cast<erizo::WebRtcConnection>(me->publisher);
int state = wr->getCurrentState();
info.GetReturnValue().Set(Nan::New(state));
}
NAN_METHOD(OneToManyProcessor::hasPublisher) {
OneToManyProcessor* obj = Nan::ObjectWrap::Unwrap<OneToManyProcessor>(info.Holder());
erizo::OneToManyProcessor *me = (erizo::OneToManyProcessor*)obj->me;
bool p = true;
if (me->publisher == NULL) {
p = false;
}
info.GetReturnValue().Set(Nan::New(p));
}
NAN_METHOD(OneToManyProcessor::addSubscriber) {
OneToManyProcessor* obj = Nan::ObjectWrap::Unwrap<OneToManyProcessor>(info.Holder());
erizo::OneToManyProcessor *me = (erizo::OneToManyProcessor*)obj->me;
WebRtcConnection* param = Nan::ObjectWrap::Unwrap<WebRtcConnection>(Nan::To<v8::Object>(info[0]).ToLocalChecked());
auto wr = std::shared_ptr<erizo::WebRtcConnection>(param->me);
std::shared_ptr<erizo::MediaSink> ms = std::dynamic_pointer_cast<erizo::MediaSink>(wr);
// get the param
v8::String::Utf8Value param1(Nan::To<v8::String>(info[1]).ToLocalChecked());
// convert it to string
std::string peerId = std::string(*param1);
me->addSubscriber(ms, peerId);
}
NAN_METHOD(OneToManyProcessor::removeSubscriber) {
OneToManyProcessor* obj = Nan::ObjectWrap::Unwrap<OneToManyProcessor>(info.Holder());
erizo::OneToManyProcessor *me = (erizo::OneToManyProcessor*)obj->me;
// get the param
v8::String::Utf8Value param1(Nan::To<v8::String>(info[0]).ToLocalChecked());
// convert it to string
std::string peerId = std::string(*param1);
Nan::AsyncQueueWorker(new AsyncRemoveSubscriber(me, peerId, NULL));
}