2017-04-17 15:52:31 +00:00
|
|
|
import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
|
2017-05-26 22:11:33 +00:00
|
|
|
import { parseJWTFromURLParams } from '../../react/features/jwt';
|
2017-04-28 20:24:20 +00:00
|
|
|
import { getJitsiMeetTransport } from '../transport';
|
2016-11-11 15:00:54 +00:00
|
|
|
|
2017-04-17 15:52:31 +00:00
|
|
|
import { API_ID } from './constants';
|
2016-06-13 21:11:44 +00:00
|
|
|
|
2017-04-17 15:52:31 +00:00
|
|
|
declare var APP: Object;
|
2017-04-06 22:09:55 +00:00
|
|
|
|
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-28 20:24:20 +00:00
|
|
|
* The transport instance used for communication with external apps.
|
|
|
|
*
|
|
|
|
* @type {Transport}
|
2016-06-13 21:11:44 +00:00
|
|
|
*/
|
2017-04-28 20:24:20 +00:00
|
|
|
const transport = getJitsiMeetTransport();
|
2017-04-06 22:09:55 +00:00
|
|
|
|
2017-04-11 18:02:59 +00:00
|
|
|
/**
|
|
|
|
* Initializes supported commands.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2015-07-02 21:33:06 +00:00
|
|
|
function initCommands() {
|
2015-07-28 18:22:11 +00:00
|
|
|
commands = {
|
2017-04-11 18:02:59 +00:00
|
|
|
'display-name':
|
2017-03-23 17:45:51 +00:00
|
|
|
APP.conference.changeLocalDisplayName.bind(APP.conference),
|
2017-04-11 19:31:05 +00:00
|
|
|
'toggle-audio': () => APP.conference.toggleAudioMuted(true),
|
|
|
|
'toggle-video': () => APP.conference.toggleVideoMuted(true),
|
2017-04-11 18:02:59 +00:00
|
|
|
'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,
|
2017-04-17 19:59:24 +00:00
|
|
|
'avatar-url': APP.conference.changeLocalAvatarUrl
|
2015-07-02 21:33:06 +00:00
|
|
|
};
|
2017-04-27 20:21:01 +00:00
|
|
|
transport.on('event', ({ data, name }) => {
|
2017-04-17 15:52:31 +00:00
|
|
|
if (name && commands[name]) {
|
|
|
|
commands[name](...data);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
2015-07-02 21:33:06 +00:00
|
|
|
}
|
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.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
2016-01-14 15:05:54 +00:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2017-04-11 18:02:59 +00:00
|
|
|
function shouldBeEnabled() {
|
2017-05-26 22:11:33 +00:00
|
|
|
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());
|
2015-01-23 15:36:17 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 15:05:54 +00:00
|
|
|
/**
|
2017-04-11 19:30:00 +00:00
|
|
|
* Executes on toggle-share-screen command.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
|
|
|
* @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 18:02:59 +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.
|
2017-04-11 18:02:59 +00:00
|
|
|
*/
|
2016-12-20 22:15:13 +00:00
|
|
|
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.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
|
|
|
* @param {Object} options - Optional parameters.
|
|
|
|
* @returns {void}
|
2015-01-09 13:39:32 +00:00
|
|
|
*/
|
2017-05-26 22:11:33 +00:00
|
|
|
init() {
|
|
|
|
if (!shouldBeEnabled()) {
|
2016-01-14 15:05:54 +00:00
|
|
|
return;
|
2017-04-11 18:02:59 +00:00
|
|
|
}
|
2016-06-13 21:11:44 +00:00
|
|
|
|
2017-04-17 15:52:31 +00:00
|
|
|
/**
|
|
|
|
* Current status (enabled/disabled) of API.
|
2017-04-27 20:21:01 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @type {boolean}
|
2017-04-17 15:52:31 +00:00
|
|
|
*/
|
2017-04-27 20:21:01 +00:00
|
|
|
this._enabled = true;
|
2017-04-17 15:52:31 +00:00
|
|
|
|
|
|
|
APP.conference.addListener(
|
|
|
|
JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
|
|
|
|
onDesktopSharingEnabledChanged);
|
|
|
|
|
|
|
|
initCommands();
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-01 20:59:18 +00:00
|
|
|
* Sends event to the external application.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
2017-05-01 20:59:18 +00:00
|
|
|
* @param {Object} event - The event to be sent.
|
2017-04-11 18:02:59 +00:00
|
|
|
* @returns {void}
|
2016-12-20 22:15:13 +00:00
|
|
|
*/
|
2017-05-01 20:59:18 +00:00
|
|
|
_sendEvent(event = {}) {
|
2017-04-27 20:21:01 +00:00
|
|
|
if (this._enabled) {
|
2017-05-01 20:59:18 +00:00
|
|
|
transport.sendEvent(event);
|
2017-04-11 18:02:59 +00:00
|
|
|
}
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
2016-01-14 15:05:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify external application (if API is enabled) that message was sent.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
2017-04-17 15:52:31 +00:00
|
|
|
* @param {string} message - Message body.
|
2017-04-11 18:02:59 +00:00
|
|
|
* @returns {void}
|
2016-01-14 15:05:54 +00:00
|
|
|
*/
|
2017-04-17 15:52:31 +00:00
|
|
|
notifySendingChatMessage(message) {
|
2017-05-01 20:59:18 +00:00
|
|
|
this._sendEvent({
|
|
|
|
name: 'outgoing-message',
|
|
|
|
message
|
|
|
|
});
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
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.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
|
|
|
* @param {Object} options - Object with the message properties.
|
|
|
|
* @returns {void}
|
2015-01-09 13:39:32 +00:00
|
|
|
*/
|
2017-04-27 20:21:01 +00:00
|
|
|
notifyReceivedChatMessage({ body, id, nick, ts } = {}) {
|
2016-01-14 15:05:54 +00:00
|
|
|
if (APP.conference.isLocalId(id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-01 20:59:18 +00:00
|
|
|
this._sendEvent({
|
|
|
|
name: 'incoming-message',
|
2017-04-17 15:52:31 +00:00
|
|
|
from: id,
|
|
|
|
message: body,
|
2017-04-27 20:21:01 +00:00
|
|
|
nick,
|
2017-04-17 15:52:31 +00:00
|
|
|
stamp: ts
|
|
|
|
});
|
2016-12-20 22:15:13 +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 user joined the
|
|
|
|
* conference.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
|
|
|
* @param {string} id - User id.
|
|
|
|
* @returns {void}
|
2015-01-09 13:39:32 +00:00
|
|
|
*/
|
2017-04-11 18:02:59 +00:00
|
|
|
notifyUserJoined(id) {
|
2017-05-01 20:59:18 +00:00
|
|
|
this._sendEvent({
|
|
|
|
name: 'participant-joined',
|
|
|
|
id
|
|
|
|
});
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
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.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
|
|
|
* @param {string} id - User id.
|
|
|
|
* @returns {void}
|
2016-01-14 15:05:54 +00:00
|
|
|
*/
|
2017-04-11 18:02:59 +00:00
|
|
|
notifyUserLeft(id) {
|
2017-05-01 20:59:18 +00:00
|
|
|
this._sendEvent({
|
|
|
|
name: 'participant-left',
|
|
|
|
id
|
|
|
|
});
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
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.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
|
|
|
* @param {string} id - User id.
|
2017-04-17 15:52:31 +00:00
|
|
|
* @param {string} displayname - User nickname.
|
2017-04-11 18:02:59 +00:00
|
|
|
* @returns {void}
|
2016-01-14 15:05:54 +00:00
|
|
|
*/
|
2017-04-17 15:52:31 +00:00
|
|
|
notifyDisplayNameChanged(id, displayname) {
|
2017-05-02 22:39:36 +00:00
|
|
|
this._sendEvent({
|
|
|
|
name: 'display-name-change',
|
2017-04-27 20:21:01 +00:00
|
|
|
displayname,
|
|
|
|
id
|
2017-04-17 15:52:31 +00:00
|
|
|
});
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
2016-06-17 20:35:40 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-11 19:30:00 +00:00
|
|
|
* Notify external application (if API is enabled) that the conference has
|
|
|
|
* been joined.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
2017-04-17 15:52:31 +00:00
|
|
|
* @param {string} roomName - The room name.
|
2017-04-11 18:02:59 +00:00
|
|
|
* @returns {void}
|
2016-06-17 20:35:40 +00:00
|
|
|
*/
|
2017-04-17 15:52:31 +00:00
|
|
|
notifyConferenceJoined(roomName) {
|
2017-05-01 20:59:18 +00:00
|
|
|
this._sendEvent({
|
|
|
|
name: 'video-conference-joined',
|
|
|
|
roomName
|
|
|
|
});
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
2016-06-17 20:35:40 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-11 19:30:00 +00:00
|
|
|
* Notify external application (if API is enabled) that user changed their
|
|
|
|
* nickname.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
2017-04-17 15:52:31 +00:00
|
|
|
* @param {string} roomName - User id.
|
2017-04-11 18:02:59 +00:00
|
|
|
* @returns {void}
|
2016-06-17 20:35:40 +00:00
|
|
|
*/
|
2017-04-17 15:52:31 +00:00
|
|
|
notifyConferenceLeft(roomName) {
|
2017-05-01 20:59:18 +00:00
|
|
|
this._sendEvent({
|
|
|
|
name: 'video-conference-left',
|
|
|
|
roomName
|
|
|
|
});
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
2015-01-09 13:39:32 +00:00
|
|
|
|
2016-10-05 21:33:09 +00:00
|
|
|
/**
|
2017-04-11 19:30:00 +00:00
|
|
|
* Notify external application (if API is enabled) that we are ready to be
|
|
|
|
* closed.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
|
|
|
* @returns {void}
|
2016-10-05 21:33:09 +00:00
|
|
|
*/
|
2017-04-11 18:02:59 +00:00
|
|
|
notifyReadyToClose() {
|
2017-05-01 20:59:18 +00:00
|
|
|
this._sendEvent({ name: 'video-ready-to-close' });
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
2016-10-05 21:33:09 +00:00
|
|
|
|
2016-12-09 23:15:04 +00:00
|
|
|
/**
|
2017-04-17 15:52:31 +00:00
|
|
|
* Disposes the allocated resources.
|
2017-04-11 18:02:59 +00:00
|
|
|
*
|
|
|
|
* @returns {void}
|
2015-01-09 13:39:32 +00:00
|
|
|
*/
|
2017-04-11 18:02:59 +00:00
|
|
|
dispose() {
|
2017-04-27 20:21:01 +00:00
|
|
|
if (this._enabled) {
|
|
|
|
this._enabled = false;
|
2017-04-06 22:09:55 +00:00
|
|
|
APP.conference.removeListener(
|
|
|
|
JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
|
2017-04-11 19:30:00 +00:00
|
|
|
onDesktopSharingEnabledChanged);
|
2017-04-06 22:09:55 +00:00
|
|
|
}
|
2015-01-09 13:39:32 +00:00
|
|
|
}
|
2016-12-20 22:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new API();
|