Changes after code review

This commit is contained in:
tsareg 2016-06-23 11:03:26 +03:00
parent 9d3b2aee02
commit 8ca282079a
1 changed files with 83 additions and 73 deletions

View File

@ -58,6 +58,64 @@ function connect(roomName) {
}); });
} }
/**
* Creates local media tracks and connects to room. Will show error
* dialogs in case if accessing local microphone and/or camera failed. Will
* show guidance overlay for users on how to give access to camera and/or
* microphone,
* @param {string} roomName
* @returns {Promise.<JitsiLocalTrack[], JitsiConnection>}
*/
function createInitialLocalTracksAndConnect(roomName) {
let audioAndVideoError,
audioOnlyError,
tracksCreated;
// First try to retrieve both audio and video.
let tryCreateLocalTracks = createLocalTracks(['audio', 'video'])
.catch(err => {
// If failed then try to retrieve only audio.
audioAndVideoError = err;
return createLocalTracks(['audio']);
})
.catch(err => {
// If audio failed too then just return empty array for tracks.
audioOnlyError = err;
return [];
})
.then(tracks => {
tracksCreated = true;
return tracks;
});
window.setTimeout(() => {
if (!audioAndVideoError && !audioOnlyError && !tracksCreated) {
APP.UI.showUserMediaPermissionsGuidanceOverlay();
}
}, USER_MEDIA_PERMISSIONS_GUIDANCE_OVERLAY_TIMEOUT);
return Promise.all([ tryCreateLocalTracks, connect(roomName) ])
.then(([tracks, con]) => {
APP.UI.hideUserMediaPermissionsGuidanceOverlay();
if (audioAndVideoError) {
if (audioOnlyError) {
// If both requests for 'audio' + 'video' and 'audio' only
// failed, we assume that there is some problems with user's
// microphone and show corresponding dialog.
APP.UI.showDeviceErrorDialog(audioOnlyError, null);
} else {
// If request for 'audio' + 'video' failed, but request for
// 'audio' only was OK, we assume that we had problems with
// camera and show corresponding dialog.
APP.UI.showDeviceErrorDialog(null, audioAndVideoError);
}
}
return [tracks, con];
});
}
/** /**
* Share data to other users. * Share data to other users.
* @param command the command * @param command the command
@ -403,7 +461,6 @@ export default {
* @returns {Promise} * @returns {Promise}
*/ */
init(options) { init(options) {
let self = this;
this.roomName = options.roomName; this.roomName = options.roomName;
JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.TRACE); JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.TRACE);
@ -429,85 +486,38 @@ export default {
}; };
} }
let audioAndVideoError, return JitsiMeetJS.init(config)
audioOnlyError, .then(() => createInitialLocalTracksAndConnect(options.roomName))
tracksCreated; .then(([tracks, con]) => {
console.log('initialized with %s local tracks', tracks.length);
APP.connection = connection = con;
this._createRoom(tracks);
this.isDesktopSharingEnabled =
JitsiMeetJS.isDesktopSharingEnabled();
if(this.isDesktopSharingEnabled)
APP.API.addPostisMessageListener('toggle-share-screen',
() => this.toggleScreenSharing());
return JitsiMeetJS.init(config).then(() => { // if user didn't give access to mic or camera or doesn't have
let tryCreateLocalTracks = // them at all, we disable corresponding toolbar buttons
// try to retrieve audio and video if (!tracks.find((t) => t.isAudioTrack())) {
createLocalTracks(['audio', 'video']) APP.UI.disableMicrophoneButton();
// if failed then try to retrieve only audio
.catch(err => {
audioAndVideoError = err;
return createLocalTracks(['audio']);
})
// if audio also failed then just return empty array
.catch(err => {
audioOnlyError = err;
return [];
})
.then(tracks => {
tracksCreated = true;
return tracks;
});
window.setTimeout(() => {
if (!audioAndVideoError && !audioOnlyError && !tracksCreated) {
APP.UI.showUserMediaPermissionsGuidanceOverlay();
} }
}, USER_MEDIA_PERMISSIONS_GUIDANCE_OVERLAY_TIMEOUT);
return Promise.all([ if (!tracks.find((t) => t.isVideoTrack())) {
tryCreateLocalTracks, APP.UI.disableCameraButton();
connect(options.roomName)
]);
}).then(([tracks, con]) => {
APP.UI.hideUserMediaPermissionsGuidanceOverlay();
if (audioAndVideoError) {
if (audioOnlyError) {
// If both requests for 'audio' + 'video' and 'audio' only
// failed, we assume that there is some problems with user's
// microphone and show corresponding dialog.
APP.UI.showDeviceErrorDialog(audioOnlyError, null);
} else {
// If request for 'audio' + 'video' failed, but request for
// 'audio' only was OK, we assume that we had problems with
// camera and show corresponding dialog.
APP.UI.showDeviceErrorDialog(null, audioAndVideoError);
} }
}
console.log('initialized with %s local tracks', tracks.length); this._initDeviceList();
APP.connection = connection = con;
this._createRoom(tracks);
this.isDesktopSharingEnabled =
JitsiMeetJS.isDesktopSharingEnabled();
if(this.isDesktopSharingEnabled)
APP.API.addPostisMessageListener('toggle-share-screen',
() => this.toggleScreenSharing());
// if user didn't give access to mic or camera or doesn't have if (config.iAmRecorder)
// them at all, we disable corresponding toolbar buttons this.recorder = new Recorder();
if (!tracks.find((t) => t.isAudioTrack())) {
APP.UI.disableMicrophoneButton();
}
if (!tracks.find((t) => t.isVideoTrack())) { // XXX The API will take care of disconnecting from the XMPP
APP.UI.disableCameraButton(); // server (and, thus, leaving the room) on unload.
} return new Promise((resolve, reject) => {
(new ConferenceConnector(resolve, reject)).connect();
this._initDeviceList(); });
if (config.iAmRecorder)
this.recorder = new Recorder();
// XXX The API will take care of disconnecting from the XMPP server
// (and, thus, leaving the room) on unload.
return new Promise((resolve, reject) => {
(new ConferenceConnector(resolve, reject)).connect();
});
}); });
}, },
/** /**