-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathopentok.js
2149 lines (2042 loc) · 78.9 KB
/
opentok.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* OpenTok server-side SDK
*/
// Dependencies
var net = require('net');
var _ = require('lodash');
var encodeToken = require('opentok-token');
var Client = require('./client');
var Session = require('./session');
var Stream = require('./stream');
var archiving = require('./archiving');
var Broadcast = require('./broadcast');
var SipInterconnect = require('./sipInterconnect');
var moderation = require('./moderation');
var signaling = require('./signaling');
var errors = require('./errors');
var callbacks = require('./callbacks');
var generateJwt = require('./generateJwt');
var render = require('./render.js');
var OpenTok;
var key;
/*
* decodes a sessionId into the metadata that it contains
* @param {string} sessionId
* @returns {?SessionInfo} sessionInfo
*/
function decodeSessionId(sessionId) {
var fields;
// remove sentinal (e.g. '1_', '2_')
sessionId = sessionId.substring(2);
// replace invalid base64 chars
sessionId = sessionId.replace(/-/g, '+').replace(/_/g, '/');
// base64 decode
if (typeof Buffer.from === 'function') {
sessionId = Buffer.from(sessionId, 'base64').toString('ascii');
}
else {
sessionId = Buffer.from(sessionId, 'base64').toString('ascii');
}
// separate fields
fields = sessionId.split('~');
return {
apiKey: fields[1],
location: fields[2],
create_time: new Date(fields[3])
};
}
/**
* Contains methods for creating OpenTok sessions, generating tokens, working with archives, and more.
* <p>
* To create a new OpenTok object, call the OpenTok constructor with your OpenTok API key
* and the API secret for your <a href="https://tokbox.com/account">TokBox account</a>.
* Do not publicly share your API secret. You will use it with the OpenTok constructor
* (only on your web server) to create OpenTok sessions.
* <p>
* Be sure to include the entire OpenTok Node.js SDK on your web server.
*
* @class OpenTok
*
* @param apiKey {String} Your OpenTok API key. (See your
* <a href="https://tokbox.com/account">TokBox account page</a>.)
* @param apiSecret {String} Your OpenTok API secret. (See your
* <a href="https://tokbox.com/account">TokBox account page</a>.)
*/
// eslint-disable-next-line consistent-return
OpenTok = function (apiKey, apiSecret, env) {
var apiConfig;
var clientConfig;
var config;
// we're loose about calling this constructor with `new`, we got your back
if (!(this instanceof OpenTok)) return new OpenTok(apiKey, apiSecret, env);
// validate arguments: apiKey := Number|String, apiSecret := String
if (!(_.isNumber(apiKey) || _.isString(apiKey)) || !_.isString(apiSecret)) {
throw new Error('Invalid arguments when initializing OpenTok: apiKey='
+ apiKey
+ ', apiSecret='
+ apiSecret);
}
// apiKey argument can be a Number, but we will internally store it as a String
if (_.isNumber(apiKey)) apiKey = apiKey.toString();
this.client = new Client({ apiKey: apiKey, apiSecret: apiSecret });
this.apiKey = apiKey;
this.apiSecret = apiSecret;
// TODO: this is a pretty obvious seam, the integration could be more smooth
apiConfig = {
apiEndpoint: 'https://api.opentok.com',
apiKey: apiKey,
apiSecret: apiSecret,
auth: {
expire: 300
}
};
// env can be either an object with a bunch of DI options, or a simple string for the apiUrl
clientConfig = {
request: {}
};
if (_.isString(env)) {
clientConfig.apiUrl = env;
apiConfig.apiEndpoint = env;
}
else if (_.isObject(env) && !_.isFunction(env) && !_.isArray(env)) {
if (_.isString(env.apiUrl)) {
clientConfig.apiUrl = env.apiUrl;
apiConfig.apiEndpoint = env.apiUrl;
}
if (_.isString(env.proxy)) {
clientConfig.request.proxy = env.proxy;
apiConfig.proxy = env.proxy;
}
if (_.isString(env.uaAddendum)) {
clientConfig.uaAddendum = env.uaAddendum;
apiConfig.uaAddendum = env.uaAddendum;
}
if (parseInt(env.timeout, 10)) {
clientConfig.request.timeout = parseInt(env.timeout, 10);
}
}
config = this.client.config(clientConfig);
this.apiUrl = config.apiUrl;
/**
* Starts archiving an OpenTok session.
* <p>
* Clients must be actively connected to the OpenTok session for you to successfully start
* recording an archive.
* <p>
* You can only record one archive at a time for a given session. You can only record archives
* of sessions that uses the OpenTok Media Router (sessions with the media mode set to routed);
* you cannot archive sessions with the media mode set to relayed.
*
* @param sessionId The session ID of the OpenTok session to archive.
*
* @param options {Object} An optional options object with the following properties (each
* of which is optional):
* <p>
* <ul>
* <li>
* <code>name</code> (String) — the name of the archive, which you can use to identify
* the archive. The name is set as a property of the Archive object, and it is a property of
* archive-related events in the OpenTok client libraries.
* </li>
* <li>
* <code>hasAudio</code> (Boolean) — Whether the archive will include an audio track
* (<code>true</code>) or not (<code>false</code>). The default value is <code>true</code>
* (an audio track is included). If you set both <code>hasAudio</code> and
* <code>hasVideo</code> to <code>false</code>, the call to the <code>startArchive()</code>
* method results in an error.
* </li>
* <li>
* <code>hasVideo</code> (Boolean) — Whether the archive will include a video track
* (<code>true</code>) or (not <code>false</code>). The default value is <code>true</code>
* (a video track is included). If you set both <code>hasAudio</code> and
* <code>hasVideo</code> to <code>false</code>, the call to the <code>startArchive()</code>
* method results in an error.
* </li>
* <li>
* <code>outputMode</code> (String) — Whether all streams in the archive are recorded
* to a single file ("composed", the default) or to individual files ("individual").
* </li>
* <li>
* <code>layout</code> (Object) — An object defining the initial layout options
* for a composed archive. This object has three properties: <code>type</code>,
* <code>stylesheet</code>, and <code>screenshareType</code>, which are each strings.
* Set <code>type</code> to "bestFit",
* "pip", "verticalPresentation", "horizontalPresentation", or "custom". Set the
* <code>stylesheet</code> property if <code>type</code> is set to "custom", and
* set it to the stylesheet defining the custom layout. For example, set the
* <code>layout</code> object to <code>{ type: "pip" }</code> to set the initial layout
* of the archive to picture-in-picture. Set the <code>screenshareType</code> property
* to the layout type to use when there is a screen-sharing stream in the session
* (This property is optional.) Note if you set the <code>screenshareType</code> property,
* you must set the <code>type</code> property to "bestFit" and leave
* the <code>stylesheet</code> property unset. For details, see
* <a href="https://tokbox.com/developer/guides/archiving/layout-control.html">Customizing
* the video layout for composed archives</a>.
* </li>
* <li>
* <code>resolution</code> (String) — For a composed archive, set this to the
* resolution of the archive. Valid values are "1280x720" or "640x480" (the default).
* </li>
* <li>
* <code>streamMode</code> (optional) — The stream mode for the archive. This can be
* set to one of the the following:
*
* <ul>
* <li> "auto" — Streams included in the archive are selected automatically
* (the default).</li>
*
* <li> "manual" — Specify streams to be included based on calls to the
* {@link OpenTok#addArchivetStream OpenTok.addArchiveStream()} and
* {@link OpenTok#removeArchiveStream OpenTok.removeArchiveStream()} methods.</li>
* </ul>
* </li>
* <li>
* <code>multiArchiveTag</code> (String) — Set this to support recording
* multiple archives for the same session simultaneously. Set this to a unique string
* for each simultaneous archive of an ongoing session. You must also set this option
* when manually starting an archive that is automatically archived. Note that the
* <code>multiArchiveTag</code> value is not included in the response for
* the methods to list archives and retrieve archive information. If you do not specify a
* unique <code>multiArchiveTag</code> you can only record one archive at a time for a
* given session.
* </li>
* </ul>
*
* For more information on archiving and the archive file formats, see the
* <a href="https://tokbox.com/opentok/tutorials/archiving/">OpenTok archiving</a>
* programming guide.
*
* @param callback {Function} The function to call upon completing the operation. Two arguments
* are passed to the function:
*
* <ul>
*
* <li>
* <code>error</code> — An error object (if the call to the method fails).
* </li>
*
* <li>
* <code>archive</code> — The {@link Archive} object. This object includes properties
* defining the archive, including the archive ID.
* </li>
*
* </ul>
*
* @method #startArchive
* @memberof OpenTok
*/
this.startArchive = archiving.startArchive.bind(null, this, apiConfig);
/**
* Stops an OpenTok archive that is being recorded.
* <p>
* Archives automatically stop recording after 120 minutes or when all clients have disconnected
* from the session being archived.
* <p>
* You cannot stop an archive that is not being recorded.
*
* @param archiveId {String} The archive ID of the archive you want to stop recording.
* @return The {@link Archive} object corresponding to the archive being STOPPED.
*
* @param callback {Function} The function to call upon completing the operation. Two arguments
* are passed to the function:
*
* <ul>
*
* <li>
* <code>error</code> — An error object (if the call to the method fails).
* </li>
*
* <li>
* <code>archive</code> — The {@link Archive} object.
* </li>
*
* </ul>
*
* @method #stopArchive
* @memberof OpenTok
*/
this.stopArchive = archiving.stopArchive.bind(null, apiConfig);
/**
* Gets an {@link Archive} object for the given archive ID.
*
* @param archiveId {String} The archive ID.
*
* @param callback {Function} The function to call upon completing the operation. Two arguments
* are passed to the function:
* <ul>
* <li><code>error</code> — An error object (if the call to the method fails). </li>
* <li><code>archive</code> — The {@link Archive} object.</li>
* </ul>
*
* @method #getArchive
* @memberof OpenTok
*/
this.getArchive = archiving.getArchive.bind(null, apiConfig);
/**
* Deletes an OpenTok archive.
* <p>
* You can only delete an archive which has a status of "available" or "uploaded". Deleting an
* archive removes its record from the list of archives. For an "available" archive, it also
* removes the archive file, making it unavailable for download.
*
* @param {String} archiveId The archive ID of the archive you want to delete.
*
* @param callback {Function} The function to call upon completing the operation. On successfully
* deleting the archive, the function is called with no arguments passed in. On failure, an error
* object is passed into the function.
*
* @method #deleteArchive
* @memberof OpenTok
*/
this.deleteArchive = archiving.deleteArchive.bind(null, apiConfig);
/**
* Retrieves a List of {@link Archive} objects, representing archives that are both
* completed and in-progress, for your API key.
*
* @param options {Object} An options parameter with three properties:
*
* <ul>
*
* <li>
* <code>count</code> — The maximum number of archives to return. The default number of
* archives returned is 50 (or fewer, if there are fewer than 50 archives). The method returns
* a maximum of 1000 archives.
* </li>
*
* <li>
* <code>offset</code> — The offset for the first archive to list (starting with the
* first archive recorded as offset 0). 1 is the offset of the archive that started prior
* to the most recent archive. This property is optional; the default is 0.
* </li>
*
* <li>
* <code>sessionId</code> — Specify the ID of a session in order to retrieve archives
* specifically for that session. This property is optional. When no session ID is specified,
* then the method will return archives from any session created with your API key.
* </li>
*
* </ul>
*
* <p>If you don't pass in an <code>options</code> argument,
* the method returns up to 1000 archives
* starting with the first archive recorded.
*
* @param callback {Function} The function to call upon completing the operation. Two arguments
* are passed to the function:
*
* <ul>
*
* <li>
* <code>error</code> — An error object (if the call to the method fails).
* </li>
*
* <li>
* <code>archives</code> — An array of {@link Archive} objects.
* </li>
*
* </ul>
*
* @method #listArchives
* @memberof OpenTok
*/
this.listArchives = archiving.listArchives.bind(null, apiConfig);
/**
* Adds a stream to an archive that has the streamMode set to manual.
* You can call the method repeatedly with the same stream ID, to toggle
* the stream's audio or video in the archive.
*
* @param archiveId {String} The archive ID.
*
* @param streamId {String} The stream ID to add to archive.
*
* @param archiveOptions {Object} An object that has these properties:
*
* <ul>
*
* <li>
* <code>hasAudio</code> — Whether the composed archive should include the stream's audio
* (true, the default) or not (false).
* </li>
*
* <li>
* <code>hasVideo</code> — Whether the composed archive should include the stream's video
* (true, the default) or not (false).
* </li>
*
* </ul>
*
* @param callback {Function} The function to call upon completing the operation. One argument is
* passed to the function
*
* <ul>
*
* <li>
* <code>error</code> — An error object (if the call to the method fails).
* </li>
*
* </ul>
*
* @method #addArchiveStream
* @memberof OpenTok
*/
this.addArchiveStream = archiving.addArchiveStream.bind(null, apiConfig);
/**
* Removes a stream from a composed archive that has the streamMode set to manual.
*
* @param archiveId {String} The archive ID.
*
* @param streamId {String} The stream ID to remove from the archive.
*
* @param callback {Function} The function to call upon completing the operation. An error is
* passed into the function if the call fails.
*
* @method #removeArchiveStream
* @memberof OpenTok
*/
this.removeArchiveStream = archiving.removeArchiveStream.bind(null, apiConfig);
/**
* Sets the layout type for a composed archive. For a description of layout types, see
* <a href="https://tokbox.com/developer/guides/archiving/layout-control.html">Customizing
* the video layout for composed archives</a>.
*
* @param archiveId {String} The archive ID.
*
* @param type {String} The layout type. Set this to "bestFit", "pip", "verticalPresentation",
* "horizontalPresentation", "focus", or "custom". For a description of these layout types, see
* <a href="https://tokbox.com/developer/guides/archiving/layout-control.html">Customizing
* the video layout for composed archives</a>.
*
* @param stylesheet {String} (Optional) The stylesheet for a custom layout. Set this parameter
* if you set <code>type</code> to <code>"custom"</code>. Otherwise, leave it undefined or set
* to null.
*
* @param screenshareType {String} (Optional) The layout type to use when
* there is a screen-sharing
* stream in the session. Note that to use this parameter, you must set the <code>type</code>
* parameter to "bestFit" and set the <code>stylesheet</code> parameter to <code>null</code>.
*
* @param callback {Function} The function to call upon completing the operation. Upon error,
* an <code>error</code> object is passed into the function. Upon success, the function is called
* with no error object passed in.
*
* @method #setArchiveLayout
* @memberof OpenTok
*/
this.setArchiveLayout = function setArchiveLayout(
archiveId,
type,
stylesheet,
screenshareType,
callback
) {
if (typeof archiveId !== 'string') {
return callback(new Error('Invalid arguments -- must provide an archiveId string.'));
}
if (typeof type !== 'string') {
return callback(new Error('Invalid arguments -- must provide a type string.'));
}
if (typeof stylesheet === 'function') {
if (callback) {
return callback(new Error('Invalid arguments -- stylesheet cannot be a function.'));
}
callback = stylesheet; // eslint-disable-line no-param-reassign
}
else if (stylesheet && typeof stylesheet !== 'string') {
return callback(new Error('Invalid arguments -- stylesheet must be a string.'));
}
if (typeof screenshareType === 'function') {
if (callback) {
return callback(new Error('Invalid arguments -- screenshareType cannot be a function.'));
}
callback = screenshareType; // eslint-disable-line no-param-reassign
}
else if (screenshareType && typeof screenshareType !== 'string') {
return callback(new Error('Invalid arguments -- screenshareType must be a string.'));
}
else if (screenshareType && type !== 'bestFit') {
return callback(new Error('Invalid arguments -- type must be set to "bestFit" if you set screenshareType.'));
}
if (typeof callback !== 'function') {
return callback(new Error('Invalid arguments -- must provide a callback function.'));
}
return this.client.setArchiveLayout(
{
archiveId: archiveId,
type: type,
stylesheet: stylesheet,
screenshareType: screenshareType
},
callback
);
};
/**
* Retrieves a List of {@link Render} objects, representing any renders in the starting,
* started, stopped or failed state, for your API key.
*
* @param options {Object} An options parameter with three properties:
*
* <ul>
*
* <li>
* <code>count</code> — The maximum number of renders to return. The default number of
* renders returned is 50 (or fewer, if there are fewer than 50 renders). The method returns
* a maximum of 1000 renders.
* </li>
*
* <li>
* <code>offset</code> — The offset for the first render to list (starting with the
* first render recorded as offset 0). 1 is the offset of the render that started prior
* to the most recent render. This property is optional; the default is 0.
* </li>
*
* <li>
* <code>sessionId</code> — Specify the ID of a session in order to retrieve renders
* specifically for that session. This property is optional. When no session ID is specified,
* then the method will return renders from any session created with your API key.
* </li>
*
* </ul>
*
* <p>If you don't pass in an <code>options</code> argument,
* the method returns up to 1000 renders, starting with the first render recorded.
*
* @param callback {Function} The function to call upon completing the operation. Two arguments
* are passed to the function:
*
* <ul>
*
* <li>
* <code>error</code> — An error object (if the call to the method fails).
* </li>
*
* <li>
* <code>renders</code> — An array of {@link Render} objects.
* </li>
*
* </ul>
*
* @method #listRenders
* @memberof OpenTok
*/
this.listRenders = render.listRenders.bind(null, apiConfig);
/**
* Gets a {@link Render} object for the given render ID.
*
* @param renderId {String} The render ID.
*
* @param callback {Function} The function to call upon completing the operation. Two arguments
* are passed to the function:
* <ul>
* <li><code>error</code> — An error object (if the call to the method fails). </li>
* <li><code>render</code> — The {@link Render} object.</li>
* </ul>
*
* @method #getRender
* @memberof OpenTok
*/
this.getRender = render.getRender.bind(null, apiConfig);
/**
* Starts an Experience Composer for an OpenTok session. The Experience Composer
* instance is represented as a Render object.
* For more information, see the
* <a href="https://tokbox.com/developer/guides/experience-composer/">Experience Composer developer guide</a>.
* <p>
* Clients must be actively connected to the OpenTok session for you to successfully start
* rendering a session.
* <p>
*
* @param options {Object} An optional options object with the following properties (each
* of which is optional):
* <p>
* <ul>
* <li>
* <code>sessionId</code> (String) — The ID of a session (generated with the same
* `APIKEY`as specified in the URL) which you wish to start rendering into
* </li>
* <li>
* <code>token</code> (String) — A valid OpenTok token with a Publisher role and
* (optionally) connection data to be associated with the output stream.
* </li>
*
* <li>
* <code>url</code> (String) — A publically reachable URL controlled by the
* customer and capable of generating the content to be rendered without user intervention.
* </li>
*
* <li>
* <code>properties</code> (Object) — Initial configuration of Publisher properties for
* the composed output stream.
* </li>
*
* <li>
* <code>maxDuration</code> (Number) — The maximum time allowed for the Render, in
* seconds. After this time, the Render will be stopped automatically, if it is still running.
* </li>
*
* <li>
* <code>resolution</code> (String) — Resolution of the display area for the composition.
* </li>
*
* </ul>
*
* @param callback {Function} The function to call upon completing the operation. Two arguments
* are passed to the function:
*
* <ul>
*
* <li>
* <code>error</code> — An error object (if the call to the method fails).
* </li>
*
* <li>
* <code>render</code> — The {@link Render} object. This object includes properties
* defining the render, including the render ID.
* </li>
*
* </ul>
*
* @method #startRender
* @memberof OpenTok
*/
this.startRender = render.startRender.bind(null, apiConfig);
/**
* Stops an Experience Composer that is being rendered.
*
* @param renderId {String} The ID of the render you want to stop rendering.
* @return The {@link Archive} object corresponding to the archive being STOPPED.
*
* @param callback {Function} The function to call upon completing the operation. Two arguments
* are passed to the function:
*
* <ul>
*
* <li>
* <code>error</code> — An error object (if the call to the method fails).
* </li>
*
* <li>
* <code>render</code> — The {@link Render} object.
* </li>
*
* </ul>
*
* @method #stopRender
* @memberof OpenTok
*/
this.stopRender = render.stopRender.bind(null, apiConfig);
/**
* Starts a live streaming broadcast. See the
* <a href="https://tokbox.com/developer/guides/broadcast/live-streaming/">live
* streaming broadcast</a> developer guide.
*
* @param {String} sessionId The ID of the session to broadcast.
*
* @param {Object} options An object with the following form:
*
* <pre><code>{
* outputs: {
* hls: {
* dvr: false,
* lowLatency: false,
* },
* rtmp: [{
* id: "foo",
* serverUrl: "rtmp://myfooserver/myfooapp",
* streamName: "myfoostream"
* },
* {
* id: "bar",
* serverUrl: "rtmp://mybarserver/mybarapp",
* streamName: "mybarstream"
* }]
* },
* maxDuration: 5400,
* resolution: "640x480",
* layout: {
* type: "custom",
* stylesheet: "the layout stylesheet (only used with type == custom)",
* screenshareType: "the layout type to use when there is a screen-sharing stream (optional)"
* },
* streamMode: "manual"
* }
* </code></pre>
*
* <p>
* The <code>options</code> object includes the following properties:
*
* <ul>
* <li>
* <p>
* <code>outputs</code> (required) — This object defines the types of
* broadcast streams you want to start. You can include HLS, RTMP, or both
* as broadcast streams. If you include RTMP streaming, you can specify up to five
* target RTMP streams (or just one).
* </p>
* <p>
* For HLS, include a single <code>hls</code> property in the outputs object. This object
* includes the following optional properties:
* <ul>
* <li>
* <code>dvr</code> (Boolean) — Whether to enable <a href="https://tokbox.com/developer/guides/broadcast/live-streaming/#dvr">DVR functionality</a> — rewinding, pausing,
* and resuming — in players that support it (<code>true</code>), or not
* (<code>false</code>, the default). With DVR enabled, the HLS URL will include a
* <code>?DVR</code> query string appended to the end.
* </li>
* <li>
* <code>lowLatency</code> (Boolean) — Whether to enable <a href="https://tokbox.com/developer/guides/broadcast/live-streaming/#low-latency">low-latency mode</a> for the HLS
* stream. Some HLS players do not support low-latency mode. This feature is incompatible
* with DVR mode HLS broadcasts.
* </li>
* </ul>
* The HLS URL is returned in the <code>broadcastUrls</code> as the <code>hls</code>
* property in the {@link Broadcast} object passed into the callback methods of the
* {@link OpenTok#getBroadcast} and {@link OpenTok#listBroadcast} methods.
* </p>
* <p>
* For each RTMP stream, specify <code>serverUrl</code> (the RTMP server URL),
* <code>streamName</code> (the stream name, such as the YouTube Live stream name or
* the Facebook stream key), and (optionally) <code>id</code> (a unique ID for the stream).
* If you specify an ID, it will be included as the <code>id</code> property of the
* {@link Broadcast} object passed into the callback methods of the
* <code>startBroadcast()</code> method and the
* {@link OpenTok#getBroadcast OpenTok.getBroadcast()} method. OpenTok streams
* the session to each RTMP URL you specify. Note that OpenTok live streaming
* supports RTMP and RTMPS.
* </p>
* </li>
* <li>
* <code>maxDuration</code> (optional) — The maximum duration for the broadcast, in
* seconds. The broadcast will automatically stop when the maximum duration is reached.
* You can set the maximum duration to a value from 60 (60 seconds) to 36000 (10 hours).
* The default maximum duration is 4 hours (14,400 seconds).
* </li>
* <li>
* <code>resolution</code> (optional) — The resolution of the broadcast: either
* <code>"640x480"</code> (SD, the default) or <code>"1280x720"</code> (HD).
* </li>
* </li>
* <code>layout</code> (optional) — Specify this to assign the initial layout type for
* the broadcast. This object has three properties: <code>type</code>,
* <code>stylesheet</code>, and <code>screenshareType</code>, which are each strings.
* Valid values for the <code>type</code> property are <code>"bestFit"</code>
* (best fit), <code>"custom"</code> (custom), <code>"horizontalPresentation"</code>
* (horizontal presentation), <code>"pip"</code> (picture-in-picture), and
* <code>"verticalPresentation"</code> (vertical presentation)). If you specify
* a <code>"custom"</code> layout type, set the <code>stylesheet</code> property of
* the <code>layout</code> object to the stylesheet. (For other layout types, do not set
* a <code>stylesheet</code> property.) If you do not specify an initial layout type,
* the broadcast stream uses the Best Fit layout type. Set the <code>screenshareType</code>
* property to the layout type to use when there is a screen-sharing stream in the session.
* (This property is optional.) Note if you set the <code>screenshareType</code> property,
* you must set the <code>type</code> property to "bestFit" and leave the
* <code>stylesheet</code> property unset. For more information, see
* <a href="https://tokbox.com/developer/guides/broadcast/live-streaming/#configuring-video-layout-for-opentok-live-streaming-broadcasts)">Configuring
* video layout for OpenTok live streaming broadcasts</a>.
* </li>
* <li>
* <code>streamMode</code> (optional) — The stream mode for the broadcast. This can be
* set to one of the the following:
*
* <ul>
* <li> "auto" — Streams included in the broadcast are selected automatically
* (the default).</li>
*
* <li> "manual" — Specify streams to be included based on calls to the
* {@link OpenTok#addBroadcastStream OpenTok.addBroadcastStream()} and
* {@link OpenTok#removeBroadcastStream OpenTok.removeBroadcastStream()} methods.</li>
* </ul>
* </li>
* <li>
* <code>multiBroadcastTag</code> (optional) — Set this to support multiple
* broadcasts for the same session simultaneously. Set this to a unique string for
* each simultaneous broadcast of an ongoing session.
* Note that the <code>multiBroadcastTag</code> value is not included in the response
* for the methods to list live streaming broadcasts and get information about a live
* streaming broadcast.
* </li>
* </ul>
*
* @param {Function} callback A callback method that takes two parameters:
* <code>error</code>, which is set to an Error object on error, and
* <code>broadcast</code>, which is set to a {@link Broadcast} object on success.
*
* @method #startBroadcast
* @memberof OpenTok
*/
OpenTok.prototype.startBroadcast = function (sessionId, options, callback) {
var client = this.client;
if (typeof callback !== 'function') {
throw new errors.ArgumentError('No callback given to startBroadcast');
}
if (sessionId == null || sessionId.length === 0) {
callback(new errors.ArgumentError('No sessionId given to startBroadcast'));
}
else if (!options || typeof options !== 'object') {
callback(new errors.ArgumentError('No options given to startBroadcast'));
}
else {
options.sessionId = sessionId;
if (!options.streamMode) {
options.streamMode = 'auto';
}
if (options.outputs && options.outputs.hls) {
if (options.outputs.hls.dvr && options.outputs.hls.lowLatency) {
callback(new errors.ArgumentError('Cannot set both dvr and lowLatency on HLS'));
return;
}
}
client.startBroadcast(options, function (err, json) {
if (err) { return callback(new Error('Failed to start broadcast. ' + err)); }
return callback(null, new Broadcast(client, json));
});
}
};
/**
* Adds a stream to a broadcast that has the streamMode set to manual.
* You can call the method repeatedly with the same stream ID, to toggle
* the stream's audio or video in the broadcast.
*
* @param broadcastId {String} The broadcast ID.
*
* @param streamId {String} The stream ID to add to broadcast.
*
* @param broadcastOptions {Object} An object that has these properties:
*
* <ul>
*
* <li>
* <code>hasAudio</code> — Whether the broadcast should include the stream's audio
* (true, the default) or not (false).
* </li>
*
* <li>
* <code>hasVideo</code> — Whether the broadcast should include the stream's video
* (true, the default) or not (false).
* </li>
*
* </ul>
*
* @method #addBroadcastStream
* @memberof OpenTok
*/
this.addBroadcastStream = function addBroadcastStream(
broadcastId,
streamId,
broadcastOptions,
callback
) {
var client = this.client;
if (typeof broadcastOptions === 'function') {
callback = broadcastOptions;
broadcastOptions = {};
}
if (typeof callback !== 'function') {
throw (new errors.ArgumentError('No callback given to addBroadcastStream'));
}
if (broadcastId == null || broadcastId.length === 0) {
callback(new errors.ArgumentError('No broadcastId given to addBroadcastStream'));
}
if (streamId == null || streamId.length === 0) {
callback(new errors.ArgumentError('No streamId given to addBroadcastStream'));
}
broadcastOptions = {
addStream: streamId,
hasAudio: broadcastOptions.hasAudio || true,
hasVideo: broadcastOptions.hasVideo || true
};
client.patchBroadcast(broadcastId, broadcastOptions, callback);
};
/**
* Removes a stream from a broadcast that has the streamMode set to manual.
*
* @param broadcastId {String} The broadcast ID.
*
* @param streamId {String} The stream ID to remove from the broadcast.
*
* @param callback {Function} The function to call upon completing the operation. An error is
* passed into the function if the call fails.
*
* @method #removeBroadcastStream
* @memberof OpenTok
*/
this.removeBroadcastStream = function removeBroadcastStream(broadcastId, streamId, callback) {
var client = this.client;
if (typeof callback !== 'function') {
throw new errors.ArgumentError('No callback given to removeBroadcastStream');
}
if (broadcastId == null || broadcastId.length === 0) {
callback(new errors.ArgumentError('No archiveId provided'));
return;
}
if (streamId == null || streamId.length === 0) {
callback(new errors.ArgumentError('No streamId provided'));
return;
}
client.patchBroadcast(broadcastId, { removeStream: streamId }, callback);
};
/**
* Stops a live streaming broadcast.
*
* @param {String} broadcastId The ID of the broadcast.
*
* @param {Function} callback A callback method that takes two parameters:
* <code>error</code>, which is set to an Error object on error, and
* <code>broadcast</code>, which is set to a {@link Broadcast} object on success.
*
* @method #stopBroadcast
* @memberof OpenTok
*/
this.stopBroadcast = function stopBroadcast(broadcastId, callback) {
var client = this.client;
if (broadcastId === null || broadcastId.length === 0) {
callback(new errors.ArgumentError('No broadcast ID given'));
return;
}
if (typeof callback !== 'function') {
throw new errors.ArgumentError('No callback given to stopBroadcast');
}
client.stopBroadcast(broadcastId, function (err, json) {
if (err) return callback(new Error('Failed to stop broadcast. ' + err));
return callback(null, new Broadcast(client, json));
});
};
/**
* Returns information about a live streaming broadcast.
*
* @param {String} broadcastId The ID of the broadcast.
*
* @param {Function} callback A callback method that takes two parameters:
* <code>error</code>, which is set to an Error object on error, and
* <code>broadcast</code>, which is set to a {@link Broadcast} object on success.
*
* @method #getBroadcast
* @memberof OpenTok
*/
this.getBroadcast = function getBroadcast(broadcastId, callback) {
var client = this.client;
if (broadcastId === null || broadcastId.length === 0) {
callback(new errors.ArgumentError('No broadcast ID given'));
return;
}
if (typeof callback !== 'function') {
throw new errors.ArgumentError('No callback given to getBroadcast');
}
client.getBroadcast(broadcastId, function (err, json) {
if (err) return callback(new Error('Failed to get broadcast. ' + err));
return callback(null, new Broadcast(client, json));
});
};
/**
* Retrieves a List of {@link Broadcast} objects, representing broadcasts that are both
* completed and in-progress, for your API key.
*
* @param options {Object} An options parameter with three properties:
*
* <ul>
*
* <li>
* <code>count</code> — The maximum number of broadcasts to return.
* The default number of
* broadcasts returned is 50 (or fewer, if there are fewer than 50 broadcasts).
* The method returns a maximum of 1000 broadcasts.
* </li>
*
* <li>
* <code>offset</code> — The offset for the first broadcast to list (starting with the
* first broadcast recorded as offset 0). 1 is the offset of the broadcast that started prior
* to the most recent broadcast. This property is optional; the default is 0.
* </li>
*
* <li>
* <code>sessionId</code> — Specify the ID of a session in order to retrieve broadcasts
* specifically for that session. This property is optional. When no session ID is specified,
* then the method will return broadcasts from any session created with your API key.
* </li>
*
* </ul>
*
* <p>If you don't pass in an <code>options</code> argument,
* the method returns up to 1000 broadcasts starting with the first broadcast recorded.