forked from sadam1807/twilio_integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickstart.js
279 lines (236 loc) · 8.43 KB
/
quickstart.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
var activeRoom;
var previewTracks;
var identity;
var roomName;
var activeScreenTrack;
function attachTrack(track, container) {
container.appendChild(track.attach());
}
// Attach array of Tracks to the DOM.
function attachTracks(tracks, container) {
tracks.forEach(function(track) {
attachTrack(track, container);
});
}
// Detach given track from the DOM.
function detachTrack(track) {
track.detach().forEach(function(element) {
element.remove();
});
}
// Appends remoteParticipant name to the DOM.
function appendName(identity, container) {
const name = document.createElement('p');
name.id = `participantName-${identity}`;
name.className = 'instructions';
name.textContent = identity;
container.appendChild(name);
}
// Removes remoteParticipant container from the DOM.
function removeName(participant) {
if (participant) {
let { identity } = participant;
const container = document.getElementById(
`participantContainer-${identity}`
);
container.parentNode.removeChild(container);
}
}
// A new RemoteTrack was published to the Room.
function trackPublished(publication, container) {
if (publication.isSubscribed) {
attachTrack(publication.track, container);
}
publication.on('subscribed', function(track) {
log('Subscribed to ' + publication.kind + ' track');
attachTrack(track, container);
});
publication.on('unsubscribed', detachTrack);
}
// A RemoteTrack was unpublished from the Room.
function trackUnpublished(publication) {
log(publication.kind + ' track was unpublished.');
}
// A new RemoteParticipant joined the Room
function participantConnected(participant, container) {
let selfContainer = document.createElement('div');
selfContainer.id = `participantContainer-${participant.identity}`;
container.appendChild(selfContainer);
appendName(participant.identity, selfContainer);
participant.tracks.forEach(function(publication) {
trackPublished(publication, selfContainer);
});
participant.on('trackPublished', function(publication) {
trackPublished(publication, selfContainer);
});
participant.on('trackUnpublished', trackUnpublished);
}
// Detach the Participant's Tracks from the DOM.
function detachParticipantTracks(participant) {
var tracks = getTracks(participant);
tracks.forEach(detachTrack);
}
// Check for WebRTC
if (!navigator.webkitGetUserMedia && !navigator.mozGetUserMedia) {
alert('WebRTC is not available in your browser.');
}
// When we are about to transition away from this page, disconnect
// from the room, if joined.
window.addEventListener('beforeunload', leaveRoomIfJoined);
$.getJSON('/token.php', function(data) {
identity = data.identity;
document.getElementById('room-controls').style.display = 'block';
// Bind button to join room
document.getElementById('button-join').onclick = function () {
roomName = document.getElementById('room-name').value;
if (roomName) {
log("Joining room '" + roomName + "'...");
var connectOptions = { name: roomName, logLevel: 'debug' };
if (previewTracks) {
connectOptions.tracks = previewTracks;
}
Twilio.Video.connect(data.token, connectOptions).then(roomJoined, function(error) {
log('Could not connect to Twilio: ' + error.message);
});
} else {
alert('Please enter a room name.');
}
};
// Bind button to leave room
document.getElementById('button-leave').onclick = function () {
log('Leaving room...');
activeRoom.disconnect();
};
document.getElementById('button-share').onclick = function () {
shareScreen();
};
// document.getElementById('button-stop').onclick = function () {
// stopScreen();
// }
document.getElementById('button-mute-audio').onclick = function () {
muteUnmuteAudio();
}
document.getElementById('button-mute-video').onclick = function () {
muteUnmuteVideo();
}
});
function getTracks(participant) {
return Array.from(participant.tracks.values()).filter(function(publication) {
return publication.track;
}).map(function(publication) {
return publication.track;
});
}
// Successfully connected!
function roomJoined(room) {
window.room = activeRoom = room;
log("Joined as '" + identity + "'");
document.getElementById('button-join').style.display = 'none';
document.getElementById('button-leave').style.display = 'inline';
document.getElementById('button-share').style.display = 'inline';
document.getElementById('button-mute-audio').style.display = 'inline';
document.getElementById('button-mute-video').style.display = 'inline';
var previewContainer = document.getElementById('local-media');
if (!previewContainer.querySelector('video')) {
attachTracks(getTracks(room.localParticipant), previewContainer);
}
// Attach the Tracks of the Room's Participants.
var remoteMediaContainer = document.getElementById('remote-media');
room.participants.forEach(function(participant) {
log("Already in Room: '" + participant.identity + "'");
participantConnected(participant, remoteMediaContainer);
});
// When a Participant joins the Room, log the event.
room.on('participantConnected', function(participant) {
log("Joining: '" + participant.identity + "'");
participantConnected(participant, remoteMediaContainer);
});
// When a Participant leaves the Room, detach its Tracks.
room.on('participantDisconnected', function(participant) {
log("RemoteParticipant '" + participant.identity + "' left the room");
detachParticipantTracks(participant);
removeName(participant);
});
// Once the LocalParticipant leaves the room, detach the Tracks
// of all Participants, including that of the LocalParticipant.
room.on('disconnected', function() {
log('Left');
if (previewTracks) {
previewTracks.forEach(function(track) {
track.stop();
});
previewTracks = null;
}
detachParticipantTracks(room.localParticipant);
room.participants.forEach(detachParticipantTracks);
room.participants.forEach(removeName);
activeRoom = null;
document.getElementById('button-join').style.display = 'block';
document.getElementById('button-leave').style.display = 'none';
});
}
function muteUnmuteAudio(){
//var localParticipant = activeRoom.localParticipant;
activeRoom.localParticipant.audioTracks.forEach(function(track) {
if ( track.track.isEnabled == true ) {
document.getElementById("button-mute-audio").innerHTML="Unmute Audio";
track.track.disable();
} else {
document.getElementById("button-mute-audio").innerHTML="Mute Audio";
track.track.enable();
}
});
}
function muteUnmuteVideo(){
activeRoom.localParticipant.videoTracks.forEach(function(track) {
if ( track.track.isEnabled == true ) {
document.getElementById("button-mute-video").innerHTML="Unmute Video";
track.track.disable();
} else {
document.getElementById("button-mute-video").innerHTML="Mute Video";
track.track.enable();
}
});
}
async function shareScreen(){
const stream = await navigator.mediaDevices.getDisplayMedia()
const screenTrack = new Twilio.Video.LocalVideoTrack(stream.getTracks()[0]);
activeScreenTrack = screenTrack
activeRoom.localParticipant.publishTrack(screenTrack);
document.getElementById("button-share").innerHTML="Stop Sharing";
screenTrack.once('stopped', () => {
activeRoom.localParticipant.removeTrack(screenTrack);
document.getElementById("button-share").innerHTML="Share Screen";
});
}
function stopScreen(){
activeRoom.localParticipant.removeTrack(activeScreenTrack);
document.getElementById("button-share").innerHTML="Share Screen";
}
// Local video preview
document.getElementById('button-preview').onclick = function() {
var localTracksPromise = previewTracks
? Promise.resolve(previewTracks)
: Twilio.Video.createLocalTracks();
localTracksPromise.then(function(tracks) {
previewTracks = tracks;
var previewContainer = document.getElementById('local-media');
if (!previewContainer.querySelector('video')) {
attachTracks(tracks, previewContainer);
}
}, function(error) {
console.error('Unable to access local media', error);
log('Unable to access Camera and Microphone');
});
};
// Activity log
function log(message) {
var logDiv = document.getElementById('log');
logDiv.innerHTML += '<p>> ' + message + '</p>';
logDiv.scrollTop = logDiv.scrollHeight;
}
function leaveRoomIfJoined() {
if (activeRoom) {
activeRoom.disconnect();
}
}