This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 142
/
RTCMultiConnection.js
6122 lines (5026 loc) · 238 KB
/
RTCMultiConnection.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
// Last time updated at Sep 21, 2015, 08:32:23
// Quick-Demo for newbies: http://jsfiddle.net/c46de0L8/
// Another simple demo: http://jsfiddle.net/zar6fg60/
// Latest file can be found here: https://cdn.webrtc-experiment.com/RTCMultiConnection.js
// Muaz Khan - www.MuazKhan.com
// MIT License - www.WebRTC-Experiment.com/licence
// Documentation - www.RTCMultiConnection.org/docs
// FAQ - www.RTCMultiConnection.org/FAQ
// Changes log - www.RTCMultiConnection.org/changes-log/
// Demos - www.WebRTC-Experiment.com/RTCMultiConnection
// _________________________
// RTCMultiConnection-v2.2.2
(function() {
// RMC == RTCMultiConnection
// usually page-URL is used as channel-id
// you can always override it!
// www.RTCMultiConnection.org/docs/channel-id/
window.RMCDefaultChannel = location.href.replace(/\/|:|#|\?|\$|\^|%|\.|`|~|!|@|\[|\||]|\|*. /g, '').split('\n').join('').split('\r').join('');
// www.RTCMultiConnection.org/docs/constructor/
window.RTCMultiConnection = function(channel) {
// an instance of constructor
var connection = this;
// a reference to RTCMultiSession
var rtcMultiSession;
// setting default channel or channel passed through constructor
connection.channel = channel || RMCDefaultChannel;
// to allow single user to join multiple rooms;
// you can change this property at runtime!
connection.isAcceptNewSession = true;
// www.RTCMultiConnection.org/docs/open/
connection.open = function(args) {
connection.isAcceptNewSession = false;
// www.RTCMultiConnection.org/docs/session-initiator/
// you can always use this property to determine room owner!
connection.isInitiator = true;
var dontTransmit = false;
// a channel can contain multiple rooms i.e. sessions
if (args) {
if (isString(args)) {
connection.sessionid = args;
} else {
if (!isNull(args.transmitRoomOnce)) {
connection.transmitRoomOnce = args.transmitRoomOnce;
}
if (!isNull(args.dontTransmit)) {
dontTransmit = args.dontTransmit;
}
if (!isNull(args.sessionid)) {
connection.sessionid = args.sessionid;
}
}
}
// if firebase && if session initiator
if (connection.socket && connection.socket.remove) {
connection.socket.remove();
}
if (!connection.sessionid) connection.sessionid = connection.channel;
connection.sessionDescription = {
sessionid: connection.sessionid,
userid: connection.userid,
session: connection.session,
extra: connection.extra
};
if (!connection.sessionDescriptions[connection.sessionDescription.sessionid]) {
connection.numberOfSessions++;
connection.sessionDescriptions[connection.sessionDescription.sessionid] = connection.sessionDescription;
}
// connect with signaling channel
initRTCMultiSession(function() {
// "captureUserMediaOnDemand" is disabled by default.
// invoke "getUserMedia" only when first participant found.
rtcMultiSession.captureUserMediaOnDemand = args ? !!args.captureUserMediaOnDemand : false;
if (args && args.onMediaCaptured) {
connection.onMediaCaptured = args.onMediaCaptured;
}
// for session-initiator, user-media is captured as soon as "open" is invoked.
if (!rtcMultiSession.captureUserMediaOnDemand) captureUserMedia(function() {
rtcMultiSession.initSession({
sessionDescription: connection.sessionDescription,
dontTransmit: dontTransmit
});
invokeMediaCaptured(connection);
});
if (rtcMultiSession.captureUserMediaOnDemand) {
rtcMultiSession.initSession({
sessionDescription: connection.sessionDescription,
dontTransmit: dontTransmit
});
}
});
return connection.sessionDescription;
};
// www.RTCMultiConnection.org/docs/connect/
connection.connect = function(sessionid) {
// a channel can contain multiple rooms i.e. sessions
if (sessionid) {
connection.sessionid = sessionid;
}
// connect with signaling channel
initRTCMultiSession(function() {
log('Signaling channel is ready.');
});
return this;
};
// www.RTCMultiConnection.org/docs/join/
connection.join = joinSession;
// www.RTCMultiConnection.org/docs/send/
connection.send = function(data, _channel) {
if (connection.numberOfConnectedUsers <= 0) {
// no connections
setTimeout(function() {
// try again
connection.send(data, _channel);
}, 1000);
return;
}
// send file/data or /text
if (!data)
throw 'No file, data or text message to share.';
// connection.send([file1, file2, file3])
// you can share multiple files, strings or data objects using "send" method!
if (data instanceof Array && !isNull(data[0].size) && !isNull(data[0].type)) {
// this mechanism can cause failure for subsequent packets/data
// on Firefox especially; and on chrome as well!
// todo: need to use setTimeout instead.
for (var i = 0; i < data.length; i++) {
data[i].size && data[i].type && connection.send(data[i], _channel);
}
return;
}
// File or Blob object MUST have "type" and "size" properties
if (!isNull(data.size) && !isNull(data.type)) {
if (!connection.enableFileSharing) {
throw '"enableFileSharing" boolean MUST be "true" to support file sharing.';
}
if (!rtcMultiSession.fileBufferReader) {
initFileBufferReader(connection, function(fbr) {
rtcMultiSession.fileBufferReader = fbr;
connection.send(data, _channel);
});
return;
}
var extra = merge({
userid: connection.userid
}, data.extra || connection.extra);
rtcMultiSession.fileBufferReader.readAsArrayBuffer(data, function(uuid) {
rtcMultiSession.fileBufferReader.getNextChunk(uuid, function(nextChunk, isLastChunk, extra) {
if (_channel) _channel.send(nextChunk);
else rtcMultiSession.send(nextChunk);
});
}, extra);
} else {
// to allow longest string messages
// and largest data objects
// or anything of any size!
// to send multiple data objects concurrently!
TextSender.send({
text: data,
channel: rtcMultiSession,
_channel: _channel,
connection: connection
});
}
};
function initRTCMultiSession(onSignalingReady) {
if (screenFrame) {
loadScreenFrame();
}
// RTCMultiSession is the backbone object;
// this object MUST be initialized once!
if (rtcMultiSession) return onSignalingReady();
// your everything is passed over RTCMultiSession constructor!
rtcMultiSession = new RTCMultiSession(connection, onSignalingReady);
}
connection.disconnect = function() {
if (rtcMultiSession) rtcMultiSession.disconnect();
rtcMultiSession = null;
};
function joinSession(session, joinAs) {
if (isString(session)) {
connection.skipOnNewSession = true;
}
if (!rtcMultiSession) {
log('Signaling channel is not ready. Connecting...');
// connect with signaling channel
initRTCMultiSession(function() {
log('Signaling channel is connected. Joining the session again...');
setTimeout(function() {
joinSession(session, joinAs);
}, 1000);
});
return;
}
// connection.join('sessionid');
if (isString(session)) {
if (connection.sessionDescriptions[session]) {
session = connection.sessionDescriptions[session];
} else
return setTimeout(function() {
log('Session-Descriptions not found. Rechecking..');
joinSession(session, joinAs);
}, 1000);
}
// connection.join('sessionid', { audio: true });
if (joinAs) {
return captureUserMedia(function() {
session.oneway = true;
joinSession(session);
}, joinAs);
}
if (!session || !session.userid || !session.sessionid) {
error('missing arguments', arguments);
var error = 'Invalid data passed over "connection.join" method.';
connection.onstatechange({
userid: 'browser',
extra: {},
name: 'Unexpected data detected.',
reason: error
});
throw error;
}
if (!connection.dontOverrideSession) {
connection.session = session.session;
}
var extra = connection.extra || session.extra || {};
// todo: need to verify that if-block statement works as expected.
// expectations: if it is oneway streaming; or if it is data-only connection
// then, it shouldn't capture user-media on participant's side.
if (session.oneway || isData(session)) {
rtcMultiSession.joinSession(session, extra);
} else {
captureUserMedia(function() {
rtcMultiSession.joinSession(session, extra);
});
}
}
var isFirstSession = true;
// www.RTCMultiConnection.org/docs/captureUserMedia/
function captureUserMedia(callback, _session, dontCheckChromExtension) {
// capture user's media resources
var session = _session || connection.session;
if (isEmpty(session)) {
if (callback) callback();
return;
}
// you can force to skip media capturing!
if (connection.dontCaptureUserMedia) {
return callback();
}
// if it is data-only connection
// if it is one-way connection and current user is participant
if (isData(session) || (!connection.isInitiator && session.oneway)) {
// www.RTCMultiConnection.org/docs/attachStreams/
connection.attachStreams = [];
return callback();
}
var constraints = {
audio: !!session.audio ? {
mandatory: {},
optional: [{
chromeRenderToAssociatedSink: true
}]
} : false,
video: !!session.video
};
// if custom audio device is selected
if (connection._mediaSources.audio) {
constraints.audio.optional.push({
sourceId: connection._mediaSources.audio
});
}
// if custom video device is selected
if (connection._mediaSources.video) {
constraints.video = {
optional: [{
sourceId: connection._mediaSources.video
}]
};
}
// for connection.session = {};
if (!session.screen && !constraints.audio && !constraints.video) {
return callback();
}
var screen_constraints = {
audio: false,
video: {
mandatory: {
chromeMediaSource: DetectRTC.screen.chromeMediaSource,
maxWidth: screen.width > 1920 ? screen.width : 1920,
maxHeight: screen.height > 1080 ? screen.height : 1080
},
optional: []
}
};
if (isFirefox && session.screen) {
if (location.protocol !== 'https:') {
return error(SCREEN_COMMON_FAILURE);
}
warn(Firefox_Screen_Capturing_Warning);
screen_constraints.video = merge(screen_constraints.video.mandatory, {
mozMediaSource: 'window', // mozMediaSource is redundant here
mediaSource: 'window' // 'screen' || 'window'
});
// Firefox is supporting audio+screen from single getUserMedia request
// audio+video+screen will become audio+screen for Firefox
// because Firefox isn't supporting multi-streams feature
if (constraints.audio /* && !session.video */ ) {
screen_constraints.audio = true;
constraints = {};
}
delete screen_constraints.video.chromeMediaSource;
}
// if screen is prompted
if (session.screen) {
if (isChrome && DetectRTC.screen.extensionid != ReservedExtensionID) {
useCustomChromeExtensionForScreenCapturing = true;
}
if (isChrome && !useCustomChromeExtensionForScreenCapturing && !dontCheckChromExtension && !DetectRTC.screen.sourceId) {
listenEventHandler('message', onIFrameCallback);
function onIFrameCallback(event) {
if (event.data && event.data.chromeMediaSourceId) {
// this event listener is no more needed
window.removeEventListener('message', onIFrameCallback);
var sourceId = event.data.chromeMediaSourceId;
DetectRTC.screen.sourceId = sourceId;
DetectRTC.screen.chromeMediaSource = 'desktop';
if (sourceId == 'PermissionDeniedError') {
var mediaStreamError = {
message: location.protocol == 'https:' ? 'User denied to share content of his screen.' : SCREEN_COMMON_FAILURE,
name: 'PermissionDeniedError',
constraintName: screen_constraints,
session: session
};
currentUserMediaRequest.mutex = false;
DetectRTC.screen.sourceId = null;
return connection.onMediaError(mediaStreamError);
}
captureUserMedia(callback, _session);
}
if (event.data && event.data.chromeExtensionStatus) {
warn('Screen capturing extension status is:', event.data.chromeExtensionStatus);
DetectRTC.screen.chromeMediaSource = 'screen';
captureUserMedia(callback, _session, true);
}
}
if (!screenFrame) {
loadScreenFrame();
}
screenFrame.postMessage();
return;
}
// check if screen capturing extension is installed.
if (isChrome && useCustomChromeExtensionForScreenCapturing && !dontCheckChromExtension && DetectRTC.screen.chromeMediaSource == 'screen' && DetectRTC.screen.extensionid) {
if (DetectRTC.screen.extensionid == ReservedExtensionID && document.domain.indexOf('webrtc-experiment.com') == -1) {
return captureUserMedia(callback, _session, true);
}
log('checking if chrome extension is installed.');
DetectRTC.screen.getChromeExtensionStatus(function(status) {
if (status == 'installed-enabled') {
DetectRTC.screen.chromeMediaSource = 'desktop';
}
captureUserMedia(callback, _session, true);
log('chrome extension is installed?', DetectRTC.screen.chromeMediaSource == 'desktop');
});
return;
}
if (isChrome && useCustomChromeExtensionForScreenCapturing && DetectRTC.screen.chromeMediaSource == 'desktop' && !DetectRTC.screen.sourceId) {
DetectRTC.screen.getSourceId(function(sourceId) {
if (sourceId == 'PermissionDeniedError') {
var mediaStreamError = {
message: 'User denied to share content of his screen.',
name: 'PermissionDeniedError',
constraintName: screen_constraints,
session: session
};
currentUserMediaRequest.mutex = false;
DetectRTC.screen.chromeMediaSource = 'desktop';
return connection.onMediaError(mediaStreamError);
}
if (sourceId == 'No-Response') {
error('Chrome extension seems not available. Make sure that manifest.json#L16 has valid content-script matches pointing to your URL.');
DetectRTC.screen.chromeMediaSource = 'screen';
return captureUserMedia(callback, _session, true);
}
captureUserMedia(callback, _session, true);
});
return;
}
if (isChrome && DetectRTC.screen.chromeMediaSource == 'desktop') {
screen_constraints.video.mandatory.chromeMediaSourceId = DetectRTC.screen.sourceId;
}
var _isFirstSession = isFirstSession;
_captureUserMedia(screen_constraints, constraints.audio || constraints.video ? function() {
if (_isFirstSession) isFirstSession = true;
_captureUserMedia(constraints, callback);
} : callback);
} else _captureUserMedia(constraints, callback, session.audio && !session.video);
function _captureUserMedia(forcedConstraints, forcedCallback, isRemoveVideoTracks, dontPreventSSLAutoAllowed) {
connection.onstatechange({
userid: 'browser',
extra: {},
name: 'fetching-usermedia',
reason: 'About to capture user-media with constraints: ' + toStr(forcedConstraints)
});
if (connection.preventSSLAutoAllowed && !dontPreventSSLAutoAllowed && isChrome) {
// if navigator.customGetUserMediaBar.js is missing
if (!navigator.customGetUserMediaBar) {
loadScript(connection.resources.customGetUserMediaBar, function() {
_captureUserMedia(forcedConstraints, forcedCallback, isRemoveVideoTracks, dontPreventSSLAutoAllowed);
});
return;
}
navigator.customGetUserMediaBar(forcedConstraints, function() {
_captureUserMedia(forcedConstraints, forcedCallback, isRemoveVideoTracks, true);
}, function() {
connection.onMediaError({
name: 'PermissionDeniedError',
message: 'User denied permission.',
constraintName: forcedConstraints,
session: session
});
});
return;
}
var mediaConfig = {
onsuccess: function(stream, returnBack, idInstance, streamid) {
onStreamSuccessCallback(stream, returnBack, idInstance, streamid, forcedConstraints, forcedCallback, isRemoveVideoTracks, screen_constraints, constraints, session);
},
onerror: function(e, constraintUsed) {
// http://goo.gl/hrwF1a
if (isFirefox) {
if (e == 'PERMISSION_DENIED') {
e = {
message: '',
name: 'PermissionDeniedError',
constraintName: constraintUsed,
session: session
};
}
}
if (isFirefox && constraintUsed.video && constraintUsed.video.mozMediaSource) {
mediaStreamError = {
message: Firefox_Screen_Capturing_Warning,
name: e.name || 'PermissionDeniedError',
constraintName: constraintUsed,
session: session
};
connection.onMediaError(mediaStreamError);
return;
}
if (isString(e)) {
return connection.onMediaError({
message: 'Unknown Error',
name: e,
constraintName: constraintUsed,
session: session
});
}
// it seems that chrome 35+ throws "DevicesNotFoundError" exception
// when any of the requested media is either denied or absent
if (e.name && (e.name == 'PermissionDeniedError' || e.name == 'DevicesNotFoundError')) {
var mediaStreamError = 'Either: ';
mediaStreamError += '\n Media resolutions are not permitted.';
mediaStreamError += '\n Another application is using same media device.';
mediaStreamError += '\n Media device is not attached or drivers not installed.';
mediaStreamError += '\n You denied access once and it is still denied.';
if (e.message && e.message.length) {
mediaStreamError += '\n ' + e.message;
}
mediaStreamError = {
message: mediaStreamError,
name: e.name,
constraintName: constraintUsed,
session: session
};
connection.onMediaError(mediaStreamError);
if (isChrome && (session.audio || session.video)) {
// todo: this snippet fails if user has two or more
// microphone/webcam attached.
DetectRTC.load(function() {
// it is possible to check presence of the microphone before using it!
if (session.audio && !DetectRTC.hasMicrophone) {
warn('It seems that you have no microphone attached to your device/system.');
session.audio = session.audio = false;
if (!session.video) {
alert('It seems that you are capturing microphone and there is no device available or access is denied. Reloading...');
location.reload();
}
}
// it is possible to check presence of the webcam before using it!
if (session.video && !DetectRTC.hasWebcam) {
warn('It seems that you have no webcam attached to your device/system.');
session.video = session.video = false;
if (!session.audio) {
alert('It seems that you are capturing webcam and there is no device available or access is denied. Reloading...');
location.reload();
}
}
if (!DetectRTC.hasMicrophone && !DetectRTC.hasWebcam) {
alert('It seems that either both microphone/webcam are not available or access is denied. Reloading...');
location.reload();
} else if (!connection.getUserMediaPromptedOnce) {
// make maximum two tries!
connection.getUserMediaPromptedOnce = true;
captureUserMedia(callback, session);
}
});
}
}
if (e.name && e.name == 'ConstraintNotSatisfiedError') {
var mediaStreamError = 'Either: ';
mediaStreamError += '\n You are prompting unknown media resolutions.';
mediaStreamError += '\n You are using invalid media constraints.';
if (e.message && e.message.length) {
mediaStreamError += '\n ' + e.message;
}
mediaStreamError = {
message: mediaStreamError,
name: e.name,
constraintName: constraintUsed,
session: session
};
connection.onMediaError(mediaStreamError);
}
if (session.screen) {
if (isFirefox) {
error(Firefox_Screen_Capturing_Warning);
} else if (location.protocol !== 'https:') {
if (!isNodeWebkit && (location.protocol == 'file:' || location.protocol == 'http:')) {
error('You cannot use HTTP or file protocol for screen capturing. You must either use HTTPs or chrome extension page or Node-Webkit page.');
}
} else {
error('Unable to detect actual issue. Maybe "deprecated" screen capturing flag was not set using command line or maybe you clicked "No" button or maybe chrome extension returned invalid "sourceId". Please install chrome-extension: http://bit.ly/webrtc-screen-extension');
}
}
currentUserMediaRequest.mutex = false;
// to make sure same stream can be captured again!
var idInstance = JSON.stringify(constraintUsed);
if (currentUserMediaRequest.streams[idInstance]) {
delete currentUserMediaRequest.streams[idInstance];
}
},
mediaConstraints: connection.mediaConstraints || {}
};
mediaConfig.constraints = forcedConstraints || constraints;
mediaConfig.connection = connection;
getUserMedia(mediaConfig);
}
}
function onStreamSuccessCallback(stream, returnBack, idInstance, streamid, forcedConstraints, forcedCallback, isRemoveVideoTracks, screen_constraints, constraints, session) {
if (!streamid) streamid = getRandomString();
connection.onstatechange({
userid: 'browser',
extra: {},
name: 'usermedia-fetched',
reason: 'Captured user media using constraints: ' + toStr(forcedConstraints)
});
if (isRemoveVideoTracks) {
stream = convertToAudioStream(stream);
}
connection.localStreamids.push(streamid);
stream.onended = function() {
if (streamedObject.mediaElement && !streamedObject.mediaElement.parentNode && document.getElementById(stream.streamid)) {
streamedObject.mediaElement = document.getElementById(stream.streamid);
}
// when a stream is stopped; it must be removed from "attachStreams" array
connection.attachStreams.forEach(function(_stream, index) {
if (_stream == stream) {
delete connection.attachStreams[index];
connection.attachStreams = swap(connection.attachStreams);
}
});
onStreamEndedHandler(streamedObject, connection);
if (connection.streams[streamid]) {
connection.removeStream(streamid);
}
// if user clicks "stop" button to close screen sharing
var _stream = connection.streams[streamid];
if (_stream && _stream.sockets.length) {
_stream.sockets.forEach(function(socket) {
socket.send({
streamid: _stream.streamid,
stopped: true
});
});
}
currentUserMediaRequest.mutex = false;
// to make sure same stream can be captured again!
if (currentUserMediaRequest.streams[idInstance]) {
delete currentUserMediaRequest.streams[idInstance];
}
// to allow re-capturing of the screen
DetectRTC.screen.sourceId = null;
};
if (!isIE) {
stream.streamid = streamid;
stream.isScreen = forcedConstraints == screen_constraints;
stream.isVideo = forcedConstraints == constraints && !!constraints.video;
stream.isAudio = forcedConstraints == constraints && !!constraints.audio && !constraints.video;
// if muted stream is negotiated
stream.preMuted = {
audio: stream.getAudioTracks().length && !stream.getAudioTracks()[0].enabled,
video: stream.getVideoTracks().length && !stream.getVideoTracks()[0].enabled
};
}
var mediaElement = createMediaElement(stream, session);
mediaElement.muted = true;
var streamedObject = {
stream: stream,
streamid: streamid,
mediaElement: mediaElement,
blobURL: mediaElement.mozSrcObject ? URL.createObjectURL(stream) : mediaElement.src,
type: 'local',
userid: connection.userid,
extra: connection.extra,
session: session,
isVideo: !!stream.isVideo,
isAudio: !!stream.isAudio,
isScreen: !!stream.isScreen,
isInitiator: !!connection.isInitiator,
rtcMultiConnection: connection
};
if (isFirstSession) {
connection.attachStreams.push(stream);
}
isFirstSession = false;
connection.streams[streamid] = connection._getStream(streamedObject);
if (!returnBack) {
connection.onstream(streamedObject);
}
if (connection.setDefaultEventsForMediaElement) {
connection.setDefaultEventsForMediaElement(mediaElement, streamid);
}
if (forcedCallback) forcedCallback(stream, streamedObject);
if (connection.onspeaking) {
initHark({
stream: stream,
streamedObject: streamedObject,
connection: connection
});
}
}
// www.RTCMultiConnection.org/docs/captureUserMedia/
connection.captureUserMedia = captureUserMedia;
// www.RTCMultiConnection.org/docs/leave/
connection.leave = function(userid) {
if (!rtcMultiSession) return;
isFirstSession = true;
if (userid) {
connection.eject(userid);
return;
}
rtcMultiSession.leave();
};
// www.RTCMultiConnection.org/docs/eject/
connection.eject = function(userid) {
if (!connection.isInitiator) throw 'Only session-initiator can eject a user.';
if (!connection.peers[userid]) throw 'You ejected invalid user.';
connection.peers[userid].sendCustomMessage({
ejected: true
});
};
// www.RTCMultiConnection.org/docs/close/
connection.close = function() {
// close entire session
connection.autoCloseEntireSession = true;
connection.leave();
};
// www.RTCMultiConnection.org/docs/renegotiate/
connection.renegotiate = function(stream, session) {
if (connection.numberOfConnectedUsers <= 0) {
// no connections
setTimeout(function() {
// try again
connection.renegotiate(stream, session);
}, 1000);
return;
}
rtcMultiSession.addStream({
renegotiate: session || merge({
oneway: true
}, connection.session),
stream: stream
});
};
connection.attachExternalStream = function(stream, isScreen) {
var constraints = {};
if (stream.getAudioTracks && stream.getAudioTracks().length) {
constraints.audio = true;
}
if (stream.getVideoTracks && stream.getVideoTracks().length) {
constraints.video = true;
}
var screen_constraints = {
video: {
chromeMediaSource: 'fake'
}
};
var forcedConstraints = isScreen ? screen_constraints : constraints;
onStreamSuccessCallback(stream, false, '', null, forcedConstraints, false, false, screen_constraints, constraints, constraints);
};
// www.RTCMultiConnection.org/docs/addStream/
connection.addStream = function(session, socket) {
// www.RTCMultiConnection.org/docs/renegotiation/
if (connection.numberOfConnectedUsers <= 0) {
// no connections
setTimeout(function() {
// try again
connection.addStream(session, socket);
}, 1000);
return;
}
// renegotiate new media stream
if (session) {
var isOneWayStreamFromParticipant;
if (!connection.isInitiator && session.oneway) {
session.oneway = false;
isOneWayStreamFromParticipant = true;
}
captureUserMedia(function(stream) {
if (isOneWayStreamFromParticipant) {
session.oneway = true;
}
addStream(stream);
}, session);
} else addStream();
function addStream(stream) {
rtcMultiSession.addStream({
stream: stream,
renegotiate: session || connection.session,
socket: socket
});
}
};
// www.RTCMultiConnection.org/docs/removeStream/
connection.removeStream = function(streamid, dontRenegotiate) {
if (connection.numberOfConnectedUsers <= 0) {
// no connections
setTimeout(function() {
// try again
connection.removeStream(streamid, dontRenegotiate);
}, 1000);
return;
}
if (!streamid) streamid = 'all';
if (!isString(streamid) || streamid.search(/all|audio|video|screen/gi) != -1) {
for (var stream in connection.streams) {
if (connection._skip.indexOf(stream) == -1) {
_stream = connection.streams[stream];
if (streamid == 'all') _detachStream(_stream, {
audio: true,
video: true,
screen: true
});
else if (isString(streamid)) {
// connection.removeStream('screen');
var config = {};
config[streamid] = true;
_detachStream(_stream, config);
} else _detachStream(_stream, streamid);
}
}
if (!dontRenegotiate && connection.detachStreams.length) {
connection.renegotiate();
}
function _detachStream(_stream, config) {
if (config.local && _stream.type != 'local') return;
if (config.remote && _stream.type != 'remote') return;
// connection.removeStream({screen:true});
if (config.screen && !!_stream.isScreen) {
connection.detachStreams.push(_stream.streamid);
}
// connection.removeStream({audio:true});
if (config.audio && !!_stream.isAudio) {
connection.detachStreams.push(_stream.streamid);
}
// connection.removeStream({video:true});
if (config.video && !!_stream.isVideo) {
connection.detachStreams.push(_stream.streamid);
}
// connection.removeStream({});
if (!config.audio && !config.video && !config.screen) {
connection.detachStreams.push(_stream.streamid);
}
if (connection.detachStreams.indexOf(_stream.streamid) != -1) {
log('removing stream', _stream.streamid);
onStreamEndedHandler(_stream, connection);
if (config.stop) {
connection.stopMediaStream(_stream.stream);
}
}
}
return;
}
var stream = connection.streams[streamid];
// detach pre-attached streams
if (!stream) return warn('No such stream exists. Stream-id:', streamid);
// www.RTCMultiConnection.org/docs/detachStreams/
connection.detachStreams.push(stream.streamid);
log('removing stream', stream.streamid);
onStreamEndedHandler(stream, connection);
// todo: how to allow "stop" function?
// connection.stopMediaStream(stream.stream)
if (!dontRenegotiate) {
connection.renegotiate();
}
};
connection.switchStream = function(session) {
if (connection.numberOfConnectedUsers <= 0) {
// no connections
setTimeout(function() {
// try again
connection.switchStream(session);
}, 1000);
return;
}
connection.removeStream('all', true);
connection.addStream(session);
};
// www.RTCMultiConnection.org/docs/sendCustomMessage/
connection.sendCustomMessage = function(message) {
if (!rtcMultiSession || !rtcMultiSession.defaultSocket) {
return setTimeout(function() {
connection.sendCustomMessage(message);
}, 1000);
}
rtcMultiSession.defaultSocket.send({
customMessage: true,
message: message
});
};