1- var videoClient ;
21var activeRoom ;
3- var previewMedia ;
2+ var previewTracks ;
43var identity ;
54var roomName ;
65
6+ function attachTracks ( tracks , container ) {
7+ tracks . forEach ( function ( track ) {
8+ container . appendChild ( track . attach ( ) ) ;
9+ } ) ;
10+ }
11+
12+ function attachParticipantTracks ( participant , container ) {
13+ var tracks = Array . from ( participant . tracks . values ( ) ) ;
14+ attachTracks ( tracks , container ) ;
15+ }
16+
17+ function detachTracks ( tracks ) {
18+ tracks . forEach ( function ( track ) {
19+ track . detach ( ) . forEach ( function ( detachedElement ) {
20+ detachedElement . remove ( ) ;
21+ } ) ;
22+ } ) ;
23+ }
24+
25+ function detachParticipantTracks ( participant ) {
26+ var tracks = Array . from ( participant . tracks . values ( ) ) ;
27+ detachTracks ( tracks ) ;
28+ }
29+
730// Check for WebRTC
831if ( ! navigator . webkitGetUserMedia && ! navigator . mozGetUserMedia ) {
932 alert ( 'WebRTC is not available in your browser.' ) ;
@@ -13,11 +36,9 @@ if (!navigator.webkitGetUserMedia && !navigator.mozGetUserMedia) {
1336// from the room, if joined.
1437window . addEventListener ( 'beforeunload' , leaveRoomIfJoined ) ;
1538
16- $ . getJSON ( '/token' , function ( data ) {
39+ $ . getJSON ( '/token' , function ( data ) {
1740 identity = data . identity ;
1841
19- // Create a Video Client and connect to Twilio
20- videoClient = new Twilio . Video . Client ( data . token ) ;
2142 document . getElementById ( 'room-controls' ) . style . display = 'block' ;
2243
2344 // Bind button to join room
@@ -26,10 +47,14 @@ $.getJSON('/token', function (data) {
2647 if ( roomName ) {
2748 log ( "Joining room '" + roomName + "'..." ) ;
2849
29- videoClient . connect ( { to : roomName } ) . then ( roomJoined ,
30- function ( error ) {
31- log ( 'Could not connect to Twilio: ' + error . message ) ;
32- } ) ;
50+ var connectOptions = { name : roomName , logLevel : 'debug' } ;
51+ if ( previewTracks ) {
52+ connectOptions . tracks = previewTracks ;
53+ }
54+
55+ Twilio . Video . connect ( data . token , connectOptions ) . then ( roomJoined , function ( error ) {
56+ log ( 'Could not connect to Twilio: ' + error . message ) ;
57+ } ) ;
3358 } else {
3459 alert ( 'Please enter a room name.' ) ;
3560 }
@@ -51,55 +76,67 @@ function roomJoined(room) {
5176 document . getElementById ( 'button-leave' ) . style . display = 'inline' ;
5277
5378 // Draw local video, if not already previewing
54- if ( ! previewMedia ) {
55- room . localParticipant . media . attach ( '#local-media' ) ;
79+ var previewContainer = document . getElementById ( 'local-media' ) ;
80+ if ( ! previewContainer . querySelector ( 'video' ) ) {
81+ attachParticipantTracks ( room . localParticipant , previewContainer ) ;
5682 }
5783
58- room . participants . forEach ( function ( participant ) {
84+ room . participants . forEach ( function ( participant ) {
5985 log ( "Already in Room: '" + participant . identity + "'" ) ;
60- participant . media . attach ( '#remote-media' ) ;
86+ var previewContainer = document . getElementById ( 'remote-media' ) ;
87+ attachParticipantTracks ( participant , previewContainer ) ;
6188 } ) ;
6289
6390 // When a participant joins, draw their video on screen
64- room . on ( 'participantConnected' , function ( participant ) {
91+ room . on ( 'participantConnected' , function ( participant ) {
6592 log ( "Joining: '" + participant . identity + "'" ) ;
66- participant . media . attach ( '#remote-media' ) ;
93+ } ) ;
94+
95+ room . on ( 'trackAdded' , function ( track , participant ) {
96+ log ( participant . identity + " added track: " + track . kind ) ;
97+ var previewContainer = document . getElementById ( 'remote-media' ) ;
98+ attachTracks ( [ track ] , previewContainer ) ;
99+ } ) ;
100+
101+ room . on ( 'trackRemoved' , function ( track , participant ) {
102+ log ( participant . identity + " removed track: " + track . kind ) ;
103+ detachTracks ( [ track ] ) ;
67104 } ) ;
68105
69106 // When a participant disconnects, note in log
70- room . on ( 'participantDisconnected' , function ( participant ) {
107+ room . on ( 'participantDisconnected' , function ( participant ) {
71108 log ( "Participant '" + participant . identity + "' left the room" ) ;
72- participant . media . detach ( ) ;
109+ detachParticipantTracks ( participant ) ;
73110 } ) ;
74111
75112 // When we are disconnected, stop capturing local video
76113 // Also remove media for all remote participants
77- room . on ( 'disconnected' , function ( ) {
114+ room . on ( 'disconnected' , function ( ) {
78115 log ( 'Left' ) ;
79- room . localParticipant . media . detach ( ) ;
80- room . participants . forEach ( function ( participant ) {
81- participant . media . detach ( ) ;
82- } ) ;
116+ detachParticipantTracks ( room . localParticipant ) ;
117+ room . participants . forEach ( detachParticipantTracks ) ;
83118 activeRoom = null ;
84119 document . getElementById ( 'button-join' ) . style . display = 'inline' ;
85120 document . getElementById ( 'button-leave' ) . style . display = 'none' ;
86121 } ) ;
87122}
88123
89124// Local video preview
90- document . getElementById ( 'button-preview' ) . onclick = function ( ) {
91- if ( ! previewMedia ) {
92- previewMedia = new Twilio . Video . LocalMedia ( ) ;
93- Twilio . Video . getUserMedia ( ) . then (
94- function ( mediaStream ) {
95- previewMedia . addStream ( mediaStream ) ;
96- previewMedia . attach ( '#local-media' ) ;
97- } ,
98- function ( error ) {
99- console . error ( 'Unable to access local media' , error ) ;
100- log ( 'Unable to access Camera and Microphone' ) ;
101- } ) ;
102- } ;
125+ document . getElementById ( 'button-preview' ) . onclick = function ( ) {
126+ var localTracksPromise = previewTracks
127+ ? Promise . resolve ( previewTracks )
128+ : Twilio . Video . createLocalTracks ( ) ;
129+
130+ localTracksPromise . then ( function ( tracks ) {
131+ previewTracks = tracks ;
132+ var previewContainer = document . getElementById ( 'local-media' ) ;
133+ if ( ! previewContainer . querySelector ( 'video' ) ) {
134+ attachTracks ( tracks , previewContainer ) ;
135+ }
136+ } , function ( error ) {
137+ console . error ( 'Unable to access local media' , error ) ;
138+ log ( 'Unable to access Camera and Microphone' ) ;
139+ } ) ;
103140} ;
104141
105142// Activity log
@@ -113,4 +150,4 @@ function leaveRoomIfJoined() {
113150 if ( activeRoom ) {
114151 activeRoom . disconnect ( ) ;
115152 }
116- }
153+ }
0 commit comments