jiti-meet/modules/API/API.js

310 lines
7.7 KiB
JavaScript
Raw Normal View History

/* global APP, getConfigParamsFromUrl */
2016-11-11 15:00:54 +00:00
import postisInit from 'postis';
2016-06-13 21:11:44 +00:00
import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
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
/**
2017-04-11 19:30:00 +00:00
* JitsiMeetExternalAPI id - unique for a webpage.
2016-06-13 21:11:44 +00:00
*/
2017-04-11 19:30:00 +00:00
const jitsiMeetExternalApiId
= getConfigParamsFromUrl().jitsi_meet_external_api_id;
2016-06-13 21:11:44 +00:00
/**
2017-04-11 19:30:00 +00:00
* Postis instance. Used to communicate with the external application. If
* undefined, then API is disabled.
2016-06-13 21:11:44 +00:00
*/
let postis;
2016-06-13 21:11:44 +00:00
/**
2017-04-11 19:30:00 +00:00
* Object that will execute sendMessage.
2016-06-13 21:11:44 +00:00
*/
2017-04-11 19:30:00 +00:00
const target = window.opener || window.parent;
/**
* Initializes supported commands.
*
* @returns {void}
*/
function initCommands() {
commands = {
'display-name':
APP.conference.changeLocalDisplayName.bind(APP.conference),
'toggle-audio': () => APP.conference.toggleAudioMuted(true),
'toggle-video': () => APP.conference.toggleVideoMuted(true),
'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,
'remote-control-event':
event => APP.remoteControl.onRemoteControlAPIEvent(event)
};
Object.keys(commands).forEach(
key => postis.listen(key, args => commands[key](...args)));
}
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-06-13 21:11:44 +00:00
/**
* Sends message to the external application.
*
* @param {Object} message - The message to be sent.
* @returns {void}
2016-06-13 21:11:44 +00:00
*/
function sendMessage(message) {
2017-04-11 19:30:00 +00:00
if (postis) {
postis.send(message);
}
2016-06-13 21:11:44 +00:00
}
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 jitsiMeetExternalApiId === 'number';
}
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) {
APP.conference.toggleScreenSharing();
} else {
initialScreenSharingState = !initialScreenSharingState;
2016-01-14 15:05:54 +00:00
}
}
/**
2017-04-11 19:30:00 +00:00
* Sends event object to the external application that has been subscribed for
* that event.
*
2017-04-11 19:30:00 +00:00
* @param {string} name - The name event.
* @param {Object} object - Data associated with the event.
* @returns {void}
*/
2017-04-11 19:30:00 +00:00
function triggerEvent(name, object) {
sendMessage({
method: name,
params: object
});
}
/**
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.
2017-04-11 19:30:00 +00:00
* @param {boolean} options.forceEnable - True to forcefully enable the
* module.
* @returns {void}
2015-01-09 13:39:32 +00:00
*/
init(options = {}) {
if (!shouldBeEnabled() && !options.forceEnable) {
2016-01-14 15:05:54 +00:00
return;
}
2016-06-13 21:11:44 +00:00
2017-04-11 19:30:00 +00:00
if (!postis) {
APP.conference.addListener(
JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
2017-04-11 19:30:00 +00:00
onDesktopSharingEnabledChanged);
this._initPostis();
}
}
/**
* Initializes postis library.
*
* @returns {void}
*
* @private
*/
_initPostis() {
const postisOptions = {
window: target
};
if (typeof jitsiMeetExternalApiId === 'number') {
postisOptions.scope
= `jitsi_meet_external_api_${jitsiMeetExternalApiId}`;
}
postis = postisInit(postisOptions);
initCommands();
}
2016-01-14 15:05:54 +00:00
/**
* Notify external application (if API is enabled) that message was sent.
*
* @param {string} body - Message body.
* @returns {void}
2016-01-14 15:05:54 +00:00
*/
notifySendingChatMessage(body) {
triggerEvent('outgoing-message', { 'message': body });
}
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(options = {}) {
const { id, nick, body, ts } = options;
2016-01-14 15:05:54 +00:00
if (APP.conference.isLocalId(id)) {
return;
}
triggerEvent(
'incoming-message',
2017-04-11 19:30:00 +00:00
{
'from': id,
'message': body,
2017-04-11 19:30:00 +00:00
'nick': 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) {
triggerEvent('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) {
triggerEvent('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) {
2017-04-11 19:30:00 +00:00
triggerEvent(
'display-name-change',
{
displayname: 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} room - The room name.
* @returns {void}
*/
notifyConferenceJoined(room) {
triggerEvent('video-conference-joined', { roomName: room });
}
/**
2017-04-11 19:30:00 +00:00
* Notify external application (if API is enabled) that user changed their
* nickname.
*
* @param {string} room - User id.
* @param {string} displayName - User nickname.
* @returns {void}
*/
notifyConferenceLeft(room) {
triggerEvent('video-conference-left', { roomName: room });
}
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() {
triggerEvent('video-ready-to-close', {});
}
/**
* Sends remote control event.
*
* @param {RemoteControlEvent} event - The remote control event.
* @returns {void}
*/
sendRemoteControlEvent(event) {
2017-04-11 19:30:00 +00:00
sendMessage({
method: 'remote-control-event',
params: event
});
}
2015-01-09 13:39:32 +00:00
/**
* Removes the listeners.
*
* @returns {void}
2015-01-09 13:39:32 +00:00
*/
dispose() {
2017-04-11 19:30:00 +00:00
if (postis) {
postis.destroy();
2017-04-11 19:30:00 +00:00
postis = undefined;
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();