2015-12-30 12:14:56 +00:00
|
|
|
/* global APP, JitsiMeetJS, config */
|
2015-08-24 21:03:50 +00:00
|
|
|
var EventEmitter = require("events");
|
2015-12-30 13:28:56 +00:00
|
|
|
import DSEvents from '../../service/desktopsharing/DesktopSharingEventTypes';
|
2015-08-24 21:03:50 +00:00
|
|
|
|
2015-12-30 12:14:56 +00:00
|
|
|
const TrackEvents = JitsiMeetJS.events.track;
|
|
|
|
|
2014-03-13 12:22:54 +00:00
|
|
|
/**
|
2015-08-25 19:59:33 +00:00
|
|
|
* Indicates that desktop stream is currently in use (for toggle purpose).
|
2014-03-13 12:22:54 +00:00
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
var isUsingScreenStream = false;
|
2015-08-25 19:59:33 +00:00
|
|
|
|
2014-03-13 12:22:54 +00:00
|
|
|
/**
|
2015-03-18 19:27:04 +00:00
|
|
|
* Indicates that switch stream operation is in progress and prevent from
|
|
|
|
* triggering new events.
|
2014-03-13 12:22:54 +00:00
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
var switchInProgress = false;
|
|
|
|
|
2014-03-17 09:02:40 +00:00
|
|
|
/**
|
2015-12-29 22:30:50 +00:00
|
|
|
* true if desktop sharing is enabled and false otherwise.
|
2015-03-18 19:27:04 +00:00
|
|
|
*/
|
2015-12-29 22:30:50 +00:00
|
|
|
var isEnabled = false;
|
2015-03-18 19:27:04 +00:00
|
|
|
|
2015-01-13 13:11:05 +00:00
|
|
|
var eventEmitter = new EventEmitter();
|
|
|
|
|
2015-08-25 19:59:33 +00:00
|
|
|
function streamSwitchDone() {
|
|
|
|
switchInProgress = false;
|
2015-12-30 13:28:56 +00:00
|
|
|
eventEmitter.emit(DSEvents.SWITCHING_DONE, isUsingScreenStream);
|
2014-04-01 11:19:11 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 22:30:50 +00:00
|
|
|
function newStreamCreated(track) {
|
2015-12-30 13:28:56 +00:00
|
|
|
eventEmitter.emit(DSEvents.NEW_STREAM_CREATED, track, streamSwitchDone);
|
2015-07-10 09:57:20 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 12:14:56 +00:00
|
|
|
function getVideoStreamFailed() {
|
|
|
|
console.error("Failed to obtain the stream to switch to");
|
2014-04-13 12:30:47 +00:00
|
|
|
switchInProgress = false;
|
2015-03-24 15:43:33 +00:00
|
|
|
isUsingScreenStream = false;
|
|
|
|
newStreamCreated(null);
|
2014-04-13 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 12:14:56 +00:00
|
|
|
function getDesktopStreamFailed() {
|
|
|
|
console.error("Failed to obtain the stream to switch to");
|
2015-03-27 14:23:48 +00:00
|
|
|
switchInProgress = false;
|
|
|
|
}
|
|
|
|
|
2015-12-29 22:30:50 +00:00
|
|
|
function onEndedHandler() {
|
2015-07-10 09:57:20 +00:00
|
|
|
if (!switchInProgress && isUsingScreenStream) {
|
|
|
|
APP.desktopsharing.toggleScreenSharing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 13:11:05 +00:00
|
|
|
module.exports = {
|
|
|
|
isUsingScreenStream: function () {
|
|
|
|
return isUsingScreenStream;
|
|
|
|
},
|
2015-12-29 22:30:50 +00:00
|
|
|
/**
|
|
|
|
* Initializes the desktop sharing module.
|
|
|
|
* @param {boolean} <tt>true</tt> if desktop sharing feature is available
|
|
|
|
* and enabled.
|
|
|
|
*/
|
|
|
|
init: function (enabled) {
|
|
|
|
isEnabled = enabled;
|
|
|
|
},
|
2015-01-13 13:11:05 +00:00
|
|
|
/**
|
2015-03-18 19:27:04 +00:00
|
|
|
* @returns {boolean} <tt>true</tt> if desktop sharing feature is available
|
|
|
|
* and enabled.
|
2015-01-13 13:11:05 +00:00
|
|
|
*/
|
2015-08-25 19:59:33 +00:00
|
|
|
isDesktopSharingEnabled: function () {
|
2015-12-29 22:30:50 +00:00
|
|
|
return isEnabled;
|
2015-08-25 19:59:33 +00:00
|
|
|
},
|
2015-08-24 19:59:26 +00:00
|
|
|
addListener: function (type, listener) {
|
2015-01-13 13:11:05 +00:00
|
|
|
eventEmitter.on(type, listener);
|
|
|
|
},
|
|
|
|
|
2015-08-24 19:59:26 +00:00
|
|
|
removeListener: function (type, listener) {
|
2015-01-13 13:11:05 +00:00
|
|
|
eventEmitter.removeListener(type, listener);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Toggles screen sharing.
|
|
|
|
*/
|
|
|
|
toggleScreenSharing: function () {
|
2015-08-25 19:59:33 +00:00
|
|
|
if (switchInProgress) {
|
|
|
|
console.warn("Switch in progress.");
|
|
|
|
return;
|
2015-12-29 22:30:50 +00:00
|
|
|
} else if (!this.isDesktopSharingEnabled()) {
|
2015-08-25 19:59:33 +00:00
|
|
|
console.warn("Cannot toggle screen sharing: not supported.");
|
2015-01-13 13:11:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switchInProgress = true;
|
2015-12-30 12:14:56 +00:00
|
|
|
let type;
|
2015-08-25 19:59:33 +00:00
|
|
|
if (!isUsingScreenStream) {
|
2015-01-13 13:11:05 +00:00
|
|
|
// Switch to desktop stream
|
2015-12-29 22:30:50 +00:00
|
|
|
type = "desktop";
|
2015-01-13 13:11:05 +00:00
|
|
|
} else {
|
2015-12-29 22:30:50 +00:00
|
|
|
type = "video";
|
2015-01-13 13:11:05 +00:00
|
|
|
}
|
2015-12-31 15:23:23 +00:00
|
|
|
APP.conference.createLocalTracks(type).then(function (tracks) {
|
2015-12-30 12:14:56 +00:00
|
|
|
if (!tracks.length) {
|
|
|
|
if (type === 'desktop') {
|
|
|
|
getDesktopStreamFailed();
|
|
|
|
} else {
|
|
|
|
getVideoStreamFailed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let stream = tracks[0];
|
|
|
|
|
|
|
|
// We now use screen stream
|
|
|
|
isUsingScreenStream = type === "desktop";
|
|
|
|
if (isUsingScreenStream) {
|
|
|
|
stream.on(TrackEvents.TRACK_STOPPED, onEndedHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
newStreamCreated(stream);
|
|
|
|
});
|
2015-09-02 22:29:53 +00:00
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Exports the event emitter to allow use by ScreenObtainer. Not for outside
|
|
|
|
* use.
|
|
|
|
*/
|
|
|
|
eventEmitter: eventEmitter
|
2015-01-13 13:11:05 +00:00
|
|
|
};
|