forked from lynckia/licode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnativeClient.js
249 lines (217 loc) · 7.59 KB
/
nativeClient.js
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
const addon = require('./../erizoAPI/build/Release/addon'); // eslint-disable-line import/no-unresolved
const licodeConfig = require('./../licode_config');
const mediaConfig = require('./../rtp_media_config');
const logger = require('./logger').logger;
const SessionDescription = require('./../erizo_controller/erizoJS/models/SessionDescription');
const SemanticSdp = require('./../erizo_controller/common/semanticSdp/SemanticSdp');
const log = logger.getLogger('NativeClient');
global.config = licodeConfig || {};
global.mediaConfig = mediaConfig || {};
const threadPool = new addon.ThreadPool(10);
threadPool.start();
const ioThreadPool = new addon.IOThreadPool(1);
if (global.config.erizo.useNicer) {
ioThreadPool.start();
}
exports.ErizoNativeConnection = (config) => {
const that = {};
let wrtc;
let mediaStream;
let streamId;
let syntheticInput;
let externalInput;
let oneToMany;
let externalOutput;
let firstOfferSent = false;
const configuration = Object.assign({}, config);
that.connected = false;
const CONN_INITIAL = 101;
// CONN_STARTED = 102
const CONN_GATHERED = 103;
const CONN_READY = 104;
// CONN_FINISHED = 105,
const CONN_CANDIDATE = 201;
const CONN_SDP = 202;
const CONN_SDP_PROCESSED = 203;
const CONN_FAILED = 500;
const generatePLIs = () => {
externalOutput.interval = setInterval(() => {
mediaStream.generatePLIPacket();
}, 1000);
};
log.info('NativeConnection constructor', configuration);
const initWebRtcConnection = (initConnectionCallback) => {
wrtc.init((newStatus, mess) => {
log.info('webrtc Addon status ', newStatus);
switch (newStatus) {
case CONN_INITIAL:
if (configuration.video && configuration.video.file && !externalInput) {
that.prepareVideo(configuration.video.file);
} else if (configuration.video && configuration.video.recording && !externalOutput) {
that.prepareRecording(configuration.video.recording);
} else if (configuration.video && configuration.video.synthetic && !syntheticInput) {
that.prepareSynthetic(configuration.video.synthetic);
} else {
that.prepareNull();
}
initConnectionCallback('callback', { type: 'started' });
break;
case CONN_SDP:
case CONN_GATHERED:
case CONN_SDP_PROCESSED:
setTimeout(() => {
wrtc.localDescription = new SessionDescription(wrtc.getLocalDescription());
const sdp = wrtc.localDescription.getSdp();
initConnectionCallback('callback', { type: 'offer', sdp: sdp.toString() });
}, 100);
break;
case CONN_CANDIDATE:
initConnectionCallback('callback', { type: 'candidate', sdp: mess });
break;
case CONN_FAILED:
log.warn('Connection failed the ICE process');
that.connected = false;
// callback('callback', {type: 'failed', sdp: mess});
break;
case CONN_READY:
log.info('Connection ready');
that.connected = true;
if (externalInput !== undefined) {
log.info('Will start External Input');
externalInput.init();
}
if (syntheticInput !== undefined) {
log.info('Will start Synthetic Input');
syntheticInput.init();
}
if (externalOutput !== undefined) {
log.info('Will start External Output');
externalOutput.init();
generatePLIs();
}
break;
default:
log.error('Unknown status', newStatus);
break;
}
});
};
wrtc = new addon.WebRtcConnection(threadPool, ioThreadPool, `spine_${configuration.sessionId}`,
global.config.erizo.stunserver,
global.config.erizo.stunport,
global.config.erizo.minport,
global.config.erizo.maxport,
false,
JSON.stringify(global.mediaConfig),
global.config.erizo.useNicer,
global.config.erizo.useConnectionQualityCheck,
global.config.erizo.turnserver,
global.config.erizo.turnport,
global.config.erizo.turnusername,
global.config.erizo.turnpass,
global.config.erizo.networkinterface);
streamId = `spine_${Math.floor(Math.random() * 1000)}`;
mediaStream = new addon.MediaStream(threadPool, wrtc,
streamId, config.label,
JSON.stringify(global.mediaConfig));
wrtc.addMediaStream(mediaStream);
that.createOffer = () => {
};
that.prepareNull = () => {
log.info('Preparing null output');
oneToMany = new addon.OneToManyProcessor();
mediaStream.setVideoReceiver(oneToMany);
mediaStream.setAudioReceiver(oneToMany);
oneToMany.setPublisher(mediaStream);
};
that.prepareVideo = (url) => {
log.info('Preparing video', url);
externalInput = new addon.ExternalInput(url);
externalInput.setAudioReceiver(mediaStream);
externalInput.setVideoReceiver(mediaStream);
};
that.prepareSynthetic = (spec) => {
log.info('Preparing synthetic video', config);
syntheticInput = new addon.SyntheticInput(threadPool,
spec.audioBitrate,
spec.minVideoBitrate,
spec.maxVideoBitrate);
syntheticInput.setAudioReceiver(mediaStream);
syntheticInput.setVideoReceiver(mediaStream);
syntheticInput.setFeedbackSource(mediaStream);
};
that.prepareRecording = (url) => {
log.info('Preparing Recording', url);
externalOutput = new addon.ExternalOutput(url, JSON.stringify(global.mediaConfig));
mediaStream.setVideoReceiver(externalOutput);
mediaStream.setAudioReceiver(externalOutput);
};
that.setRemoteDescription = () => {
log.info('RemoteDescription');
};
that.processSignallingMessage = (signalingMsg) => {
log.info('Receiving message', signalingMsg.type);
if (signalingMsg.type === 'started') {
initWebRtcConnection((mess, info) => {
log.info('Message from wrtc', info.type);
if (info.type === 'offer' && !firstOfferSent) {
firstOfferSent = true;
configuration.callback({ type: info.type, sdp: info.sdp });
}
}, {});
const audioEnabled = true;
const videoEnabled = true;
const bundle = true;
wrtc.createOffer(audioEnabled, videoEnabled, bundle);
} else if (signalingMsg.type === 'answer') {
setTimeout(() => {
log.info('Passing delayed answer');
const sdp = SemanticSdp.SDPInfo.processString(signalingMsg.sdp);
this.remoteDescription = new SessionDescription(sdp, 'default');
wrtc.setRemoteDescription(this.remoteDescription.connectionDescription, streamId);
sdp.streams.forEach((stream) => {
let label = '';
for (const track of stream.tracks.values()) {
track.ssrcs.forEach(ssrc => {
label = ssrc.mslabel;
});
}
that.onaddstream({ stream: { active: true, id: label } });
});
}, 10);
}
};
that.getStats = () => {
log.info('Requesting stats');
return new Promise((resolve, reject) => {
if (!mediaStream) {
reject('No stream present');
return;
}
mediaStream.getStats((statsString) => {
resolve(JSON.parse(statsString));
});
});
};
that.close = () => {
log.info('Closing nativeConnection');
if (externalOutput) {
externalOutput.close();
if (externalOutput.interval) {
clearInterval(externalOutput.interval);
}
}
if (externalInput) {
externalInput.close();
}
if (syntheticInput) {
syntheticInput.close();
}
that.connected = false;
wrtc.close();
mediaStream.close();
wrtc = undefined;
mediaStream = undefined;
};
return that;
};