-
Notifications
You must be signed in to change notification settings - Fork 41
/
opentok-api.js
402 lines (365 loc) · 9.78 KB
/
opentok-api.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
'use strict';
const { apiKey, apiSecret, production_url } = require('../config');
const jwt = require('jsonwebtoken');
const OpenTok = require('opentok');
const OT = new OpenTok(apiKey, apiSecret);
const axios = require('axios');
const defaultSessionOptions = { mediaMode: 'routed' };
const customStyle = `
stream {
width: 15%;
height: 15%;
}
stream.focus {
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
z-index: 1;
}
stream.sub {
position: absolute;
z-index: 5;
}
stream.item0 {
top: 85%;
left: 0;
}
stream.item1 {
top: 85%;
right: 0;
}
stream.item2 {
top: 70%;
left: 0;
}
stream.item3 {
top: 70%;
right: 0;
}
stream.item4 {
top: 85%;
left: 15%;
}
stream.item5 {
top: 85%;
right: 15%;
}
stream.item6 {
top: 85%;
left: 30%;
}
stream.item7 {
top: 85%;
right: 30%;
}`;
/**
* There is currently a ~15 second delay between the interactive session due to the
* encoding process and the time it takes to upload the video to the CDN. Currently
* using a 20-second delay to be safe.
*/
const broadcastDelay = 20 * 1000;
let activeSession;
const activeBroadcast = {};
/**
* Returns options for token creation based on user type
* @param {String} userType Host, guest, or viewer
*/
const tokenOptions = (userType) => {
const role = {
host: 'moderator',
guest: 'publisher',
viewer: 'subscriber',
}[userType];
return { role };
};
/**
* Create an OpenTok session
* @param {Object} [options]
* @returns {Promise} <Resolve => {Object}, Reject => {Error}>
*/
const createSession = async (options) => {
return new Promise((resolve, reject) => {
try {
OT.createSession({ ...defaultSessionOptions, ...options }, (err, session) => {
if (err) resolve(err);
resolve(session);
});
} catch (err) {
reject(err);
}
});
};
/**
* Create an OpenTok token
* @param {String} userType Host, guest, or viewer
* @returns {String}
*/
const createToken = (userType, sessionId) =>
OT.generateToken(sessionId, Object.assign({ initialLayoutClassList: ['full', 'focus'] }, tokenOptions(userType)));
/**
* Creates an OpenTok session and generates an associated token
* @param {String} userType Host, guest, or viewer
* @returns {Promise} <Resolve => {Object}, Reject => {Error}>
*/
const getCredentials = async (userType) => {
return new Promise(async (resolve, reject) => {
try {
const session = await createSession();
const token = createToken(userType, session.sessionId);
resolve({ apiKey, sessionId: session.sessionId, token });
} catch (err) {
reject(err);
}
});
};
/**
* Start the broadcast and keep the active broadcast in memory
* @param {String} [rtmp] - The (optional) RTMP stream url
* @param {Boolean} [lowLatency] - The (optional) low Latency option for HLS
* @param {fhd} [fhd] - The (optional) Full HD parameter for streaming
* @param {dvr} [dvr] - The (optional) DVR option for HLS
* @param {sessionId} [sessionId] - The sessionId to start the broadcast for
* @returns {Promise} <Resolve => {Object} Broadcast data, Reject => {Error}>
*/
const startBroadcast = async (rtmp, lowLatency, fhd = false, dvr = false, sessionId, streamMode) => {
return new Promise((resolve, reject) => {
console.log('StartBroadcast API');
const layout = {
type: 'bestFit',
screenshareType: 'verticalPresentation',
};
let dvrConfig = dvr;
let lowLatencyConfig = lowLatency;
if (dvrConfig) {
lowLatencyConfig = false; // DVR and LL are not compatible
}
let outputs = {
hls: {
dvr: dvrConfig,
lowLatency: lowLatencyConfig,
},
};
const { serverUrl, streamName } = rtmp;
if (serverUrl && streamName) {
outputs.rtmp = [rtmp];
console.log(outputs);
}
const resolution = fhd ? '1920x1080' : process.env.broadcastDefaultResolution ? process.env.broadcastDefaultResolution : '1280x720';
try {
console.log('starting a streamMode' + streamMode);
OT.startBroadcast(sessionId, { layout, outputs, resolution, streamMode }, function (err, broadcast) {
if (err) {
console.log('error starting broadcast ' + err);
reject(err);
}
activeBroadcast[sessionId] = {
id: broadcast.id,
session: broadcast.sessionId,
rtmp: broadcast.broadcastUrls.rtmp,
url: broadcast.broadcastUrls.hls,
apiKey: apiKey,
availableAt: broadcast.createdAt + broadcastDelay,
};
console.log('activeBroadcast', activeBroadcast);
resolve(activeBroadcast[sessionId]);
});
} catch (err) {
reject(err);
}
});
};
/**
* End the broadcast
* @param {String} [sessionId] - The sessionId to stop the broadcast
* @returns {Promise} <Resolve => {Object}, Reject => {Error}>
*/
const stopBroadcast = async (sessionId) => {
return new Promise((resolve, reject) => {
console.log('StopBroadcast API');
if (!activeBroadcast[sessionId]) {
reject({ error: 'No active broadcast session found' });
}
try {
OT.stopBroadcast(activeBroadcast[sessionId].id, function (err, broadcast) {
if (err) reject(err);
resolve(broadcast);
});
} catch (err) {
reject(err);
} finally {
activeBroadcast[sessionId] = null;
}
});
};
/**
* Add stream to broadcast (Manual)
* @param {String} broadcastId
* * @param {String} streamId
* @returns {Promise} <Resolve => {Object}, Reject => {Error}>
*/
const addStreamToBroadcast = async (broadcastId, streamId) => {
return new Promise((resolve, reject) => {
try {
OT.addBroadcastStream(broadcastId, streamId, { hasAudio: true, hasVideo: true }, function (err, broadcast) {
if (err) {
console.log(err.message);
reject(err);
}
resolve();
});
} catch (err) {
console.log(err);
reject(err);
}
});
};
/**
* Dynamically update the broadcast layout
* @param {Number} streams - The number of active streams in the broadcast session
* @param {String} type - OPTIONAL: type of layout
* @returns {Promise} <Resolve => null, Reject => {Error}>
*/
const updateLayout = async (streams, type, sessionId) => {
return new Promise((resolve, reject) => {
if (!activeBroadcast) {
reject({ error: 'No active broadcast session found' });
}
let stylesheet;
if (!type) {
if (streams > 3) {
type = 'bestFit';
} else {
type = 'custom';
stylesheet = customStyle;
}
} else if (type === 'custom') {
stylesheet = customStyle;
}
try {
OT.setBroadcastLayout(activeBroadcast[sessionId].id, type, stylesheet, function (err) {
if (err) reject(err);
resolve();
});
} catch (err) {
reject(err);
}
});
};
const generateRestToken = () => {
return new Promise((res, rej) => {
jwt.sign(
{
iss: apiKey,
ist: 'project',
exp: Date.now() + 200,
jti: Math.random() * 132,
},
apiSecret,
{ algorithm: 'HS256' },
function (err, token) {
if (token) {
res(token);
} else {
console.log('\n Unable to fetch token, error:', err);
rej(err);
}
}
);
});
};
/**
* Dynamically update class lists for streams
* @param {Array} classListArray - The number of active streams in the broadcast session
* <pre>
* const classListArray = [
* { id: '7b09ec3c-26f9-43d7-8197-f608f13d4fb6', layoutClassList: ['focus'] },
* { id: '567bc941-6ea0-4c69-97fc-70a740b68976', layoutClassList: ['top'] },
* { id: '307dc941-0450-4c09-975c-705740d08970', layoutClassList: ['bottom'] }
* ];
* </pre>
* @returns {Promise} <Resolve => null, Reject => {Error}>
*/
const updateStreamClassList = async (classListArray, sessionId) => {
return new Promise((resolve, reject) => {
try {
OT.setStreamClassLists(sessionId, classListArray, function (err) {
if (err) reject(err);
resolve();
});
} catch (err) {
reject(err);
}
});
};
/**
* Dynamically update the broadcast layout
* @param {String} sessionId - The sessionId to start the experience composer render
* @returns {Promise} <Resolve => null, Reject => {Error}>
*/
const createRender = async (sessionId, roomName, bgChoice, round) => {
try {
console.log('creating render');
const token = createToken('subscriber', sessionId);
const data = JSON.stringify({
url: `${production_url}/ec?room=${roomName}&bg=${bgChoice}&round=${round}`,
sessionId: sessionId,
token: token,
projectId: apiKey,
properties: {
name: 'EC',
},
});
const config = {
method: 'post',
url: `https://api.opentok.com/v2/project/${apiKey}/render`,
headers: {
'X-OPENTOK-AUTH': await generateRestToken(),
'Content-Type': 'application/json',
},
data: data,
};
const response = await axios(config);
return response.data;
} catch (e) {
console.log(e);
return e;
}
};
/**
* Dynamically update the broadcast layout
* @param {String} id - The id to stop the experience composer render
* @returns {Promise} <Resolve => null, Reject => {Error}>
*/
const deleteRender = async (id) => {
console.log('trying to stop render ' + id);
const config = {
method: 'delete',
url: `https://api.opentok.com/v2/project/${apiKey}/render/${id}`,
headers: {
'X-OPENTOK-AUTH': await generateRestToken(),
'Content-Type': 'application/json',
},
};
try {
const response = await axios(config);
return response.data;
} catch (e) {
console.log(e);
return e;
}
};
module.exports = {
getCredentials,
startBroadcast,
stopBroadcast,
updateLayout,
updateStreamClassList,
createToken,
apiKey,
activeBroadcast,
createRender,
deleteRender,
addStreamToBroadcast,
};