2021-02-04 23:04:36 +00:00
|
|
|
/* global $, config, JitsiMeetJS */
|
|
|
|
import 'jquery';
|
2021-02-05 21:00:28 +00:00
|
|
|
import { setConfigFromURLParams } from '../../react/features/base/config/functions';
|
2021-02-05 15:12:45 +00:00
|
|
|
import { parseURLParams } from '../../react/features/base/util/parseURLParams';
|
|
|
|
import { parseURIString } from '../../react/features/base/util/uri';
|
2021-04-01 14:30:23 +00:00
|
|
|
import { validateLastNLimits, limitLastN } from '../../react/features/base/lastn/functions';
|
2021-02-04 23:04:36 +00:00
|
|
|
|
2021-02-05 21:00:28 +00:00
|
|
|
setConfigFromURLParams(config, {}, {}, window.location);
|
|
|
|
|
2021-02-04 23:04:36 +00:00
|
|
|
const params = parseURLParams(window.location, false, 'hash');
|
|
|
|
const { isHuman = false } = params;
|
|
|
|
const {
|
2021-02-05 21:00:28 +00:00
|
|
|
localVideo = config.startWithVideoMuted !== true,
|
2021-02-04 23:04:36 +00:00
|
|
|
remoteVideo = isHuman,
|
2021-02-05 21:00:28 +00:00
|
|
|
remoteAudio = isHuman,
|
2021-02-17 16:28:01 +00:00
|
|
|
autoPlayVideo = config.testing.noAutoPlayVideo !== true,
|
2021-04-06 20:31:26 +00:00
|
|
|
stageView = config.disableTileView
|
2021-02-04 23:04:36 +00:00
|
|
|
} = params;
|
|
|
|
|
2021-03-18 13:39:13 +00:00
|
|
|
let {
|
|
|
|
localAudio = config.startWithAudioMuted !== true,
|
|
|
|
} = params;
|
|
|
|
|
2021-02-04 23:04:36 +00:00
|
|
|
const { room: roomName } = parseURIString(window.location.toString());
|
|
|
|
|
|
|
|
let connection = null;
|
|
|
|
|
2021-04-05 21:17:25 +00:00
|
|
|
let connected = false;
|
|
|
|
|
2021-02-04 23:04:36 +00:00
|
|
|
let room = null;
|
|
|
|
|
|
|
|
let numParticipants = 1;
|
|
|
|
|
|
|
|
let localTracks = [];
|
|
|
|
const remoteTracks = {};
|
|
|
|
|
2021-02-08 18:35:03 +00:00
|
|
|
let maxFrameHeight = 0;
|
|
|
|
|
2021-04-06 20:31:26 +00:00
|
|
|
let selectedParticipant = null;
|
|
|
|
|
2021-02-04 23:04:36 +00:00
|
|
|
window.APP = {
|
|
|
|
conference: {
|
|
|
|
getStats() {
|
|
|
|
return room.connectionQuality.getStats();
|
|
|
|
},
|
|
|
|
getConnectionState() {
|
|
|
|
return room && room.getConnectionState();
|
2021-03-15 19:37:58 +00:00
|
|
|
},
|
|
|
|
muteAudio(mute) {
|
|
|
|
localAudio = mute;
|
|
|
|
for (let i = 0; i < localTracks.length; i++) {
|
|
|
|
if (localTracks[i].getType() === 'audio') {
|
|
|
|
if (mute) {
|
|
|
|
localTracks[i].mute();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
localTracks[i].unmute();
|
2021-03-22 19:39:57 +00:00
|
|
|
|
|
|
|
// if track was not added we need to add it to the peerconnection
|
|
|
|
if (!room.getLocalAudioTrack()) {
|
|
|
|
room.replaceTrack(null, localTracks[i]);
|
|
|
|
}
|
2021-03-15 19:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-04 23:04:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get room() {
|
|
|
|
return room;
|
|
|
|
},
|
|
|
|
get connection() {
|
|
|
|
return connection;
|
|
|
|
},
|
|
|
|
get numParticipants() {
|
|
|
|
return numParticipants;
|
|
|
|
},
|
|
|
|
get localTracks() {
|
|
|
|
return localTracks;
|
|
|
|
},
|
|
|
|
get remoteTracks() {
|
|
|
|
return remoteTracks;
|
|
|
|
},
|
|
|
|
get params() {
|
|
|
|
return {
|
|
|
|
roomName,
|
|
|
|
localAudio,
|
|
|
|
localVideo,
|
|
|
|
remoteVideo,
|
2021-02-05 21:00:28 +00:00
|
|
|
remoteAudio,
|
2021-04-06 20:31:26 +00:00
|
|
|
autoPlayVideo,
|
|
|
|
stageView
|
2021-02-04 23:04:36 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-08 18:35:03 +00:00
|
|
|
/**
|
|
|
|
* Simple emulation of jitsi-meet's screen layout behavior
|
|
|
|
*/
|
|
|
|
function updateMaxFrameHeight() {
|
2021-04-05 21:17:25 +00:00
|
|
|
if (!connected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-08 18:35:03 +00:00
|
|
|
let newMaxFrameHeight;
|
|
|
|
|
2021-04-06 20:31:26 +00:00
|
|
|
if (stageView) {
|
|
|
|
newMaxFrameHeight = 2160;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (numParticipants <= 2) {
|
|
|
|
newMaxFrameHeight = 720;
|
|
|
|
} else if (numParticipants <= 4) {
|
|
|
|
newMaxFrameHeight = 360;
|
|
|
|
} else {
|
|
|
|
newMaxFrameHeight = 180;
|
|
|
|
}
|
2021-02-08 18:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (room && maxFrameHeight !== newMaxFrameHeight) {
|
|
|
|
maxFrameHeight = newMaxFrameHeight;
|
|
|
|
room.setReceiverVideoConstraint(maxFrameHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 14:30:23 +00:00
|
|
|
/**
|
|
|
|
* Simple emulation of jitsi-meet's lastN behavior
|
|
|
|
*/
|
|
|
|
function updateLastN() {
|
2021-04-05 21:17:25 +00:00
|
|
|
if (!connected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-01 14:30:23 +00:00
|
|
|
let lastN = typeof config.channelLastN === 'undefined' ? -1 : config.channelLastN;
|
|
|
|
|
|
|
|
const limitedLastN = limitLastN(numParticipants, validateLastNLimits(config.lastNLimits));
|
|
|
|
|
|
|
|
if (limitedLastN !== undefined) {
|
|
|
|
lastN = lastN === -1 ? limitedLastN : Math.min(limitedLastN, lastN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastN === room.getLastN()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
room.setLastN(lastN);
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:31:26 +00:00
|
|
|
/**
|
|
|
|
* Helper function to query whether a participant ID is a valid ID
|
|
|
|
* for stage view.
|
|
|
|
*/
|
|
|
|
function isValidStageViewParticipant(id) {
|
|
|
|
return (id !== room.myUserId() && room.getParticipantById(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple emulation of jitsi-meet's stage view participant selection behavior.
|
|
|
|
* Doesn't take into account pinning or screen sharing, and the initial behavior
|
|
|
|
* is slightly different.
|
|
|
|
* @returns Whether the selected participant changed.
|
|
|
|
*/
|
|
|
|
function selectStageViewParticipant(selected, previous) {
|
|
|
|
let newSelectedParticipant;
|
|
|
|
|
|
|
|
if (isValidStageViewParticipant(selected)) {
|
|
|
|
newSelectedParticipant = selected;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newSelectedParticipant = previous.find(isValidStageViewParticipant);
|
|
|
|
}
|
|
|
|
if (newSelectedParticipant && newSelectedParticipant !== selectedParticipant) {
|
|
|
|
selectedParticipant = newSelectedParticipant;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-04 23:04:36 +00:00
|
|
|
/**
|
2021-04-05 21:17:25 +00:00
|
|
|
* Simple emulation of jitsi-meet's selectParticipants behavior
|
2021-02-04 23:04:36 +00:00
|
|
|
*/
|
2021-04-05 21:17:25 +00:00
|
|
|
function selectParticipants() {
|
|
|
|
if (!connected) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-06 20:31:26 +00:00
|
|
|
if (stageView) {
|
|
|
|
if (selectedParticipant) {
|
|
|
|
room.selectParticipants([selectedParticipant]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* jitsi-meet's current Tile View behavior. */
|
|
|
|
const ids = room.getParticipants().map(participant => participant.getId());
|
|
|
|
room.selectParticipants(ids);
|
|
|
|
}
|
2021-04-05 21:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when number of participants changes.
|
|
|
|
*/
|
|
|
|
function setNumberOfParticipants() {
|
|
|
|
$('#participants').text(numParticipants);
|
2021-04-06 20:31:26 +00:00
|
|
|
if (!stageView) {
|
|
|
|
selectParticipants();
|
|
|
|
updateMaxFrameHeight();
|
|
|
|
}
|
2021-04-05 21:17:25 +00:00
|
|
|
updateLastN();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when ICE connects
|
|
|
|
*/
|
|
|
|
function onConnectionEstablished() {
|
|
|
|
connected = true;
|
|
|
|
|
|
|
|
selectParticipants();
|
2021-02-08 18:35:03 +00:00
|
|
|
updateMaxFrameHeight();
|
2021-04-01 14:30:23 +00:00
|
|
|
updateLastN();
|
2021-02-04 23:04:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 20:31:26 +00:00
|
|
|
/**
|
|
|
|
* Handles dominant speaker changed.
|
|
|
|
* @param id
|
|
|
|
*/
|
|
|
|
function onDominantSpeakerChanged(selected, previous) {
|
|
|
|
if (selectStageViewParticipant(selected, previous)) {
|
|
|
|
selectParticipants();
|
|
|
|
}
|
|
|
|
updateMaxFrameHeight();
|
|
|
|
}
|
|
|
|
|
2021-02-04 23:04:36 +00:00
|
|
|
/**
|
|
|
|
* Handles local tracks.
|
|
|
|
* @param tracks Array with JitsiTrack objects
|
|
|
|
*/
|
|
|
|
function onLocalTracks(tracks = []) {
|
|
|
|
localTracks = tracks;
|
|
|
|
for (let i = 0; i < localTracks.length; i++) {
|
|
|
|
if (localTracks[i].getType() === 'video') {
|
2021-02-05 21:00:28 +00:00
|
|
|
$('body').append(`<video ${autoPlayVideo ? 'autoplay="1" ' : ''}id='localVideo${i}' />`);
|
2021-02-04 23:04:36 +00:00
|
|
|
localTracks[i].attach($(`#localVideo${i}`)[0]);
|
2021-03-22 19:39:57 +00:00
|
|
|
|
|
|
|
room.addTrack(localTracks[i]);
|
2021-02-04 23:04:36 +00:00
|
|
|
} else {
|
2021-03-22 19:39:57 +00:00
|
|
|
if (localAudio) {
|
|
|
|
room.addTrack(localTracks[i]);
|
|
|
|
} else {
|
2021-02-11 23:25:46 +00:00
|
|
|
localTracks[i].mute();
|
|
|
|
}
|
|
|
|
|
2021-02-04 23:04:36 +00:00
|
|
|
$('body').append(
|
|
|
|
`<audio autoplay='1' muted='true' id='localAudio${i}' />`);
|
|
|
|
localTracks[i].attach($(`#localAudio${i}`)[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles remote tracks
|
|
|
|
* @param track JitsiTrack object
|
|
|
|
*/
|
|
|
|
function onRemoteTrack(track) {
|
|
|
|
if (track.isLocal()
|
|
|
|
|| (track.getType() === 'video' && !remoteVideo) || (track.getType() === 'audio' && !remoteAudio)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const participant = track.getParticipantId();
|
|
|
|
|
|
|
|
if (!remoteTracks[participant]) {
|
|
|
|
remoteTracks[participant] = [];
|
|
|
|
}
|
|
|
|
const idx = remoteTracks[participant].push(track);
|
|
|
|
const id = participant + track.getType() + idx;
|
|
|
|
|
|
|
|
if (track.getType() === 'video') {
|
|
|
|
$('body').append(`<video autoplay='1' id='${id}' />`);
|
|
|
|
} else {
|
|
|
|
$('body').append(`<audio autoplay='1' id='${id}' />`);
|
|
|
|
}
|
|
|
|
track.attach($(`#${id}`)[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* That function is executed when the conference is joined
|
|
|
|
*/
|
|
|
|
function onConferenceJoined() {
|
2021-02-11 23:25:46 +00:00
|
|
|
console.log('Conference joined');
|
2021-02-04 23:04:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 16:28:01 +00:00
|
|
|
/**
|
|
|
|
* Handles start muted events, when audio and/or video are muted due to
|
|
|
|
* startAudioMuted or startVideoMuted policy.
|
|
|
|
*/
|
|
|
|
function onStartMuted() {
|
|
|
|
// Give it some time, as it may be currently in the process of muting
|
|
|
|
setTimeout(() => {
|
|
|
|
const localAudioTrack = room.getLocalAudioTrack();
|
|
|
|
|
|
|
|
if (localAudio && localAudioTrack && localAudioTrack.isMuted()) {
|
|
|
|
localAudioTrack.unmute();
|
|
|
|
}
|
|
|
|
|
|
|
|
const localVideoTrack = room.getLocalVideoTrack();
|
|
|
|
|
|
|
|
if (localVideo && localVideoTrack && localVideoTrack.isMuted()) {
|
|
|
|
localVideoTrack.unmute();
|
|
|
|
}
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
|
2021-04-01 14:30:23 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
*/
|
|
|
|
function onUserJoined(id) {
|
|
|
|
numParticipants++;
|
|
|
|
setNumberOfParticipants();
|
|
|
|
remoteTracks[id] = [];
|
|
|
|
}
|
|
|
|
|
2021-02-04 23:04:36 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
*/
|
|
|
|
function onUserLeft(id) {
|
|
|
|
numParticipants--;
|
|
|
|
setNumberOfParticipants();
|
|
|
|
if (!remoteTracks[id]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const tracks = remoteTracks[id];
|
|
|
|
|
|
|
|
for (let i = 0; i < tracks.length; i++) {
|
|
|
|
const container = $(`#${id}${tracks[i].getType()}${i + 1}`)[0];
|
|
|
|
|
|
|
|
if (container) {
|
|
|
|
tracks[i].detach(container);
|
|
|
|
container.parentElement.removeChild(container);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 17:13:34 +00:00
|
|
|
/**
|
|
|
|
* Handles private messages.
|
|
|
|
*
|
|
|
|
* @param {string} id - The sender ID.
|
|
|
|
* @param {string} text - The message.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function onPrivateMessage(id, text) {
|
|
|
|
switch(text) {
|
|
|
|
case 'video on':
|
|
|
|
onVideoOnMessage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles 'video on' private messages.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function onVideoOnMessage() {
|
|
|
|
console.debug('Turning my video on!');
|
|
|
|
|
|
|
|
const localVideoTrack = room.getLocalVideoTrack();
|
|
|
|
|
|
|
|
if (localVideoTrack && localVideoTrack.isMuted()) {
|
|
|
|
console.debug('Unmuting existing video track.');
|
|
|
|
localVideoTrack.unmute();
|
|
|
|
} else if (!localVideoTrack) {
|
|
|
|
JitsiMeetJS.createLocalTracks({ devices: [ 'video' ] })
|
|
|
|
.then(([ videoTrack ]) => videoTrack)
|
|
|
|
.catch(console.error)
|
|
|
|
.then(videoTrack => {
|
|
|
|
return room.replaceTrack(null, videoTrack);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
console.debug('Successfully added a new video track for unmute.');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log('No-op! We are already video unmuted!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 23:04:36 +00:00
|
|
|
/**
|
|
|
|
* That function is called when connection is established successfully
|
|
|
|
*/
|
|
|
|
function onConnectionSuccess() {
|
2021-02-09 20:42:06 +00:00
|
|
|
room = connection.initJitsiConference(roomName.toLowerCase(), config);
|
2021-02-17 16:28:01 +00:00
|
|
|
room.on(JitsiMeetJS.events.conference.STARTED_MUTED, onStartMuted);
|
2021-02-04 23:04:36 +00:00
|
|
|
room.on(JitsiMeetJS.events.conference.TRACK_ADDED, onRemoteTrack);
|
|
|
|
room.on(JitsiMeetJS.events.conference.CONFERENCE_JOINED, onConferenceJoined);
|
2021-04-05 21:17:25 +00:00
|
|
|
room.on(JitsiMeetJS.events.conference.CONNECTION_ESTABLISHED, onConnectionEstablished);
|
2021-04-01 14:30:23 +00:00
|
|
|
room.on(JitsiMeetJS.events.conference.USER_JOINED, onUserJoined);
|
2021-02-04 23:04:36 +00:00
|
|
|
room.on(JitsiMeetJS.events.conference.USER_LEFT, onUserLeft);
|
2021-11-24 17:13:34 +00:00
|
|
|
room.on(JitsiMeetJS.events.conference.PRIVATE_MESSAGE_RECEIVED, onPrivateMessage);
|
2021-04-06 20:31:26 +00:00
|
|
|
if (stageView) {
|
|
|
|
room.on(JitsiMeetJS.events.conference.DOMINANT_SPEAKER_CHANGED, onDominantSpeakerChanged);
|
|
|
|
}
|
2021-02-11 20:56:19 +00:00
|
|
|
|
|
|
|
const devices = [];
|
|
|
|
|
|
|
|
if (localVideo) {
|
|
|
|
devices.push('video');
|
|
|
|
}
|
2021-02-17 16:28:01 +00:00
|
|
|
|
2021-03-22 19:39:57 +00:00
|
|
|
// we always create audio local tracks
|
|
|
|
devices.push('audio');
|
2021-02-11 23:25:46 +00:00
|
|
|
|
2021-02-11 20:56:19 +00:00
|
|
|
if (devices.length > 0) {
|
|
|
|
JitsiMeetJS.createLocalTracks({ devices })
|
|
|
|
.then(onLocalTracks)
|
|
|
|
.then(() => {
|
|
|
|
room.join();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
room.join();
|
|
|
|
}
|
|
|
|
|
2021-02-08 18:35:03 +00:00
|
|
|
updateMaxFrameHeight();
|
2021-02-04 23:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is called when the connection fail.
|
|
|
|
*/
|
|
|
|
function onConnectionFailed() {
|
|
|
|
console.error('Connection Failed!');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is called when we disconnect.
|
|
|
|
*/
|
|
|
|
function disconnect() {
|
|
|
|
console.log('disconnect!');
|
|
|
|
connection.removeEventListener(
|
|
|
|
JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED,
|
|
|
|
onConnectionSuccess);
|
|
|
|
connection.removeEventListener(
|
|
|
|
JitsiMeetJS.events.connection.CONNECTION_FAILED,
|
|
|
|
onConnectionFailed);
|
|
|
|
connection.removeEventListener(
|
|
|
|
JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED,
|
|
|
|
disconnect);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function unload() {
|
|
|
|
for (let i = 0; i < localTracks.length; i++) {
|
|
|
|
localTracks[i].dispose();
|
|
|
|
}
|
|
|
|
room.leave();
|
|
|
|
connection.disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
$(window).bind('beforeunload', unload);
|
|
|
|
$(window).bind('unload', unload);
|
|
|
|
|
|
|
|
JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.ERROR);
|
|
|
|
|
|
|
|
JitsiMeetJS.init(config);
|
|
|
|
|
2021-02-09 20:42:06 +00:00
|
|
|
config.serviceUrl = config.bosh = `${config.websocket || config.bosh}?room=${roomName.toLowerCase()}`;
|
2021-02-12 18:14:32 +00:00
|
|
|
if (config.websocketKeepAliveUrl) {
|
|
|
|
config.websocketKeepAliveUrl += `?room=${roomName.toLowerCase()}`;
|
|
|
|
}
|
2021-02-09 20:42:06 +00:00
|
|
|
|
2021-02-04 23:04:36 +00:00
|
|
|
connection = new JitsiMeetJS.JitsiConnection(null, null, config);
|
|
|
|
connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED, onConnectionSuccess);
|
|
|
|
connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_FAILED, onConnectionFailed);
|
|
|
|
connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED, disconnect);
|
|
|
|
connection.connect();
|