jiti-meet/modules/API/API.js

379 lines
9.4 KiB
JavaScript
Raw Normal View History

import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
import { parseJWTFromURLParams } from '../../react/features/jwt';
import { getJitsiMeetTransport } from '../transport';
2016-11-11 15:00:54 +00:00
import { API_ID } from './constants';
2016-06-13 21:11:44 +00:00
declare var APP: Object;
2015-01-09 13:39:32 +00:00
/**
* List of the available commands.
*/
2016-06-13 21:11:44 +00:00
let commands = {};
/**
2017-04-11 19:30:00 +00:00
* The state of screen sharing(started/stopped) before the screen sharing is
* enabled and initialized.
* NOTE: This flag help us to cache the state and use it if toggle-share-screen
* was received before the initialization.
2016-06-13 21:11:44 +00:00
*/
2017-04-11 19:30:00 +00:00
let initialScreenSharingState = false;
2016-06-13 21:11:44 +00:00
/**
* The transport instance used for communication with external apps.
*
* @type {Transport}
2016-06-13 21:11:44 +00:00
*/
const transport = getJitsiMeetTransport();
2017-08-04 08:15:11 +00:00
/**
* The current audio availability.
*
* @type {boolean}
*/
let audioAvailable = true;
/**
* The current video availability.
*
* @type {boolean}
*/
let videoAvailable = true;
/**
* Initializes supported commands.
*
* @returns {void}
*/
function initCommands() {
commands = {
'display-name':
APP.conference.changeLocalDisplayName.bind(APP.conference),
'toggle-audio': () => {
APP.conference.toggleAudioMuted(false /* no UI */);
},
'toggle-video': () => {
APP.conference.toggleVideoMuted(false /* no UI */);
},
'toggle-film-strip': APP.UI.toggleFilmstrip,
'toggle-chat': APP.UI.toggleChat,
'toggle-contact-list': APP.UI.toggleContactList,
'toggle-share-screen': toggleScreenSharing,
'video-hangup': () => APP.conference.hangup(),
'email': APP.conference.changeLocalEmail,
'avatar-url': APP.conference.changeLocalAvatarUrl
};
transport.on('event', ({ data, name }) => {
if (name && commands[name]) {
commands[name](...data);
return true;
}
return false;
});
2017-08-04 08:15:11 +00:00
transport.on('request', ({ data, name }, callback) => {
switch (name) {
case 'is-audio-muted':
callback(APP.conference.isLocalAudioMuted());
2017-08-04 08:15:11 +00:00
break;
case 'is-video-muted':
callback(APP.conference.isLocalVideoMuted());
2017-08-04 08:15:11 +00:00
break;
case 'is-audio-available':
callback(audioAvailable);
break;
case 'is-video-available':
callback(videoAvailable);
break;
default:
return false;
}
return true;
});
}
2015-01-09 13:39:32 +00:00
2017-04-11 19:30:00 +00:00
/**
* Listens for desktop/screen sharing enabled events and toggles the screen
* sharing if needed.
*
* @param {boolean} enabled - Current screen sharing enabled status.
* @returns {void}
*/
function onDesktopSharingEnabledChanged(enabled = false) {
if (enabled && initialScreenSharingState) {
toggleScreenSharing();
}
}
2016-01-14 15:05:54 +00:00
/**
* Check whether the API should be enabled or not.
*
2016-01-14 15:05:54 +00:00
* @returns {boolean}
*/
function shouldBeEnabled() {
return (
typeof API_ID === 'number'
// XXX Enable the API when a JSON Web Token (JWT) is specified in
// the location/URL because then it is very likely that the Jitsi
// Meet (Web) app is being used by an external/wrapping (Web) app
// and, consequently, the latter will need to communicate with the
// former. (The described logic is merely a heuristic though.)
|| parseJWTFromURLParams());
}
2016-01-14 15:05:54 +00:00
/**
2017-04-11 19:30:00 +00:00
* Executes on toggle-share-screen command.
*
* @returns {void}
2016-01-14 15:05:54 +00:00
*/
2017-04-11 19:30:00 +00:00
function toggleScreenSharing() {
if (APP.conference.isDesktopSharingEnabled) {
// eslint-disable-next-line no-empty-function
APP.conference.toggleScreenSharing().catch(() => {});
2017-04-11 19:30:00 +00:00
} else {
initialScreenSharingState = !initialScreenSharingState;
2016-01-14 15:05:54 +00:00
}
}
/**
2017-04-11 19:30:00 +00:00
* Implements API class that communicates with external API class and provides
* interface to access Jitsi Meet features by external applications that embed
* Jitsi Meet.
*/
class API {
2015-01-09 13:39:32 +00:00
/**
2017-04-11 19:30:00 +00:00
* Initializes the API. Setups message event listeners that will receive
* information from external applications that embed Jitsi Meet. It also
* sends a message to the external application that API is initialized.
*
* @param {Object} options - Optional parameters.
* @returns {void}
2015-01-09 13:39:32 +00:00
*/
init() {
if (!shouldBeEnabled()) {
2016-01-14 15:05:54 +00:00
return;
}
2016-06-13 21:11:44 +00:00
/**
* Current status (enabled/disabled) of API.
*
* @private
* @type {boolean}
*/
this._enabled = true;
APP.conference.addListener(
JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
onDesktopSharingEnabledChanged);
initCommands();
}
/**
* Sends event to the external application.
*
* @param {Object} event - The event to be sent.
* @returns {void}
*/
_sendEvent(event = {}) {
if (this._enabled) {
transport.sendEvent(event);
}
}
2016-01-14 15:05:54 +00:00
/**
* Notify external application (if API is enabled) that message was sent.
*
* @param {string} message - Message body.
* @returns {void}
2016-01-14 15:05:54 +00:00
*/
notifySendingChatMessage(message) {
this._sendEvent({
name: 'outgoing-message',
message
});
}
2016-06-13 21:11:44 +00:00
2015-01-09 13:39:32 +00:00
/**
2017-04-11 19:30:00 +00:00
* Notify external application (if API is enabled) that message was
* received.
*
* @param {Object} options - Object with the message properties.
* @returns {void}
2015-01-09 13:39:32 +00:00
*/
notifyReceivedChatMessage({ body, id, nick, ts } = {}) {
2016-01-14 15:05:54 +00:00
if (APP.conference.isLocalId(id)) {
return;
}
this._sendEvent({
name: 'incoming-message',
from: id,
message: body,
nick,
stamp: ts
});
}
2015-01-09 13:39:32 +00:00
/**
2017-04-11 19:30:00 +00:00
* Notify external application (if API is enabled) that user joined the
* conference.
*
* @param {string} id - User id.
* @returns {void}
2015-01-09 13:39:32 +00:00
*/
notifyUserJoined(id) {
this._sendEvent({
name: 'participant-joined',
id
});
}
2016-01-14 15:05:54 +00:00
/**
2017-04-11 19:30:00 +00:00
* Notify external application (if API is enabled) that user left the
* conference.
*
* @param {string} id - User id.
* @returns {void}
2016-01-14 15:05:54 +00:00
*/
notifyUserLeft(id) {
this._sendEvent({
name: 'participant-left',
id
});
}
2016-01-14 15:05:54 +00:00
/**
2017-04-11 19:30:00 +00:00
* Notify external application (if API is enabled) that user changed their
* nickname.
*
* @param {string} id - User id.
* @param {string} displayname - User nickname.
* @returns {void}
2016-01-14 15:05:54 +00:00
*/
notifyDisplayNameChanged(id, displayname) {
this._sendEvent({
name: 'display-name-change',
displayname,
id
});
}
/**
2017-04-11 19:30:00 +00:00
* Notify external application (if API is enabled) that the conference has
* been joined.
*
* @param {string} roomName - The room name.
* @returns {void}
*/
notifyConferenceJoined(roomName) {
this._sendEvent({
name: 'video-conference-joined',
roomName
});
}
/**
2017-04-11 19:30:00 +00:00
* Notify external application (if API is enabled) that user changed their
* nickname.
*
* @param {string} roomName - User id.
* @returns {void}
*/
notifyConferenceLeft(roomName) {
this._sendEvent({
name: 'video-conference-left',
roomName
});
}
2015-01-09 13:39:32 +00:00
/**
2017-04-11 19:30:00 +00:00
* Notify external application (if API is enabled) that we are ready to be
* closed.
*
* @returns {void}
*/
notifyReadyToClose() {
this._sendEvent({ name: 'video-ready-to-close' });
}
2017-08-04 08:15:11 +00:00
/**
* Notify external application (if API is enabled) for audio muted status
* changed.
*
* @param {boolean} muted - The new muted status.
* @returns {void}
*/
notifyAudioMutedStatusChanged(muted) {
this._sendEvent({
name: 'audio-mute-status-changed',
muted
});
}
/**
* Notify external application (if API is enabled) for video muted status
* changed.
*
* @param {boolean} muted - The new muted status.
* @returns {void}
*/
notifyVideoMutedStatusChanged(muted) {
this._sendEvent({
name: 'video-mute-status-changed',
muted
});
}
/**
* Notify external application (if API is enabled) for audio availability
* changed.
*
* @param {boolean} available - True if available and false otherwise.
* @returns {void}
*/
notifyAudioAvailabilityChanged(available) {
audioAvailable = available;
this._sendEvent({
name: 'audio-availability-changed',
available
});
}
/**
* Notify external application (if API is enabled) for video available
* status changed.
*
* @param {boolean} available - True if available and false otherwise.
* @returns {void}
*/
notifyVideoAvailabilityChanged(available) {
videoAvailable = available;
this._sendEvent({
name: 'video-availability-changed',
available
});
}
/**
* Disposes the allocated resources.
*
* @returns {void}
2015-01-09 13:39:32 +00:00
*/
dispose() {
if (this._enabled) {
this._enabled = false;
APP.conference.removeListener(
JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
2017-04-11 19:30:00 +00:00
onDesktopSharingEnabledChanged);
}
2015-01-09 13:39:32 +00:00
}
}
export default new API();