2016-10-03 16:12:04 +00:00
|
|
|
/* global APP, JitsiMeetJS */
|
2016-06-13 11:49:00 +00:00
|
|
|
|
2019-05-29 21:17:07 +00:00
|
|
|
import {
|
|
|
|
getAudioOutputDeviceId,
|
|
|
|
notifyCameraError,
|
|
|
|
notifyMicError
|
|
|
|
} from '../../react/features/base/devices';
|
2019-05-07 08:53:01 +00:00
|
|
|
import {
|
|
|
|
getUserSelectedCameraDeviceId,
|
|
|
|
getUserSelectedMicDeviceId,
|
|
|
|
getUserSelectedOutputDeviceId
|
|
|
|
} from '../../react/features/base/settings';
|
2018-04-12 19:58:20 +00:00
|
|
|
|
2016-06-13 11:49:00 +00:00
|
|
|
/**
|
|
|
|
* Determines if currently selected audio output device should be changed after
|
|
|
|
* list of available devices has been changed.
|
|
|
|
* @param {MediaDeviceInfo[]} newDevices
|
|
|
|
* @returns {string|undefined} - ID of new audio output device to use, undefined
|
|
|
|
* if audio output device should not be changed.
|
|
|
|
*/
|
|
|
|
function getNewAudioOutputDevice(newDevices) {
|
|
|
|
if (!JitsiMeetJS.mediaDevices.isDeviceChangeAvailable('output')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-12 19:58:20 +00:00
|
|
|
const selectedAudioOutputDeviceId = getAudioOutputDeviceId();
|
2017-10-12 23:02:29 +00:00
|
|
|
const availableAudioOutputDevices = newDevices.filter(
|
2016-06-13 11:49:00 +00:00
|
|
|
d => d.kind === 'audiooutput');
|
|
|
|
|
|
|
|
// Switch to 'default' audio output device if we don't have the selected one
|
|
|
|
// available anymore.
|
2017-10-12 23:02:29 +00:00
|
|
|
if (selectedAudioOutputDeviceId !== 'default'
|
|
|
|
&& !availableAudioOutputDevices.find(d =>
|
2016-06-13 11:49:00 +00:00
|
|
|
d.deviceId === selectedAudioOutputDeviceId)) {
|
|
|
|
return 'default';
|
|
|
|
}
|
2019-05-03 17:25:33 +00:00
|
|
|
|
2019-05-07 08:53:01 +00:00
|
|
|
const preferredAudioOutputDeviceId = getUserSelectedOutputDeviceId(APP.store.getState());
|
2019-05-03 17:25:33 +00:00
|
|
|
|
|
|
|
// if the preferred one is not the selected and is available in the new devices
|
|
|
|
// we want to use it as it was just added
|
|
|
|
if (preferredAudioOutputDeviceId
|
|
|
|
&& preferredAudioOutputDeviceId !== selectedAudioOutputDeviceId
|
|
|
|
&& availableAudioOutputDevices.find(d => d.deviceId === preferredAudioOutputDeviceId)) {
|
|
|
|
return preferredAudioOutputDeviceId;
|
|
|
|
}
|
2016-06-13 11:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if currently selected audio input device should be changed after
|
|
|
|
* list of available devices has been changed.
|
|
|
|
* @param {MediaDeviceInfo[]} newDevices
|
|
|
|
* @param {JitsiLocalTrack} localAudio
|
|
|
|
* @returns {string|undefined} - ID of new microphone device to use, undefined
|
|
|
|
* if audio input device should not be changed.
|
|
|
|
*/
|
|
|
|
function getNewAudioInputDevice(newDevices, localAudio) {
|
2017-10-12 23:02:29 +00:00
|
|
|
const availableAudioInputDevices = newDevices.filter(
|
2016-06-13 11:49:00 +00:00
|
|
|
d => d.kind === 'audioinput');
|
2019-05-07 08:53:01 +00:00
|
|
|
const selectedAudioInputDeviceId = getUserSelectedMicDeviceId(APP.store.getState());
|
2017-10-12 23:02:29 +00:00
|
|
|
const selectedAudioInputDevice = availableAudioInputDevices.find(
|
2016-06-13 11:49:00 +00:00
|
|
|
d => d.deviceId === selectedAudioInputDeviceId);
|
|
|
|
|
|
|
|
// Here we handle case when no device was initially plugged, but
|
|
|
|
// then it's connected OR new device was connected when previous
|
|
|
|
// track has ended.
|
|
|
|
if (!localAudio || localAudio.disposed || localAudio.isEnded()) {
|
|
|
|
// If we have new audio device and permission to use it was granted
|
|
|
|
// (label is not an empty string), then we will try to use the first
|
|
|
|
// available device.
|
2019-04-12 16:10:38 +00:00
|
|
|
if (selectedAudioInputDevice && selectedAudioInputDeviceId) {
|
|
|
|
return selectedAudioInputDeviceId;
|
|
|
|
} else if (availableAudioInputDevices.length
|
2017-10-12 23:02:29 +00:00
|
|
|
&& availableAudioInputDevices[0].label !== '') {
|
2016-06-13 11:49:00 +00:00
|
|
|
return availableAudioInputDevices[0].deviceId;
|
|
|
|
}
|
2017-10-12 23:02:29 +00:00
|
|
|
} else if (selectedAudioInputDevice
|
|
|
|
&& selectedAudioInputDeviceId !== localAudio.getDeviceId()) {
|
|
|
|
|
2016-06-13 11:49:00 +00:00
|
|
|
// And here we handle case when we already have some device working,
|
|
|
|
// but we plug-in a "preferred" (previously selected in settings, stored
|
|
|
|
// in local storage) device.
|
2017-10-12 23:02:29 +00:00
|
|
|
return selectedAudioInputDeviceId;
|
2016-06-13 11:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if currently selected video input device should be changed after
|
|
|
|
* list of available devices has been changed.
|
|
|
|
* @param {MediaDeviceInfo[]} newDevices
|
|
|
|
* @param {JitsiLocalTrack} localVideo
|
|
|
|
* @returns {string|undefined} - ID of new camera device to use, undefined
|
|
|
|
* if video input device should not be changed.
|
|
|
|
*/
|
|
|
|
function getNewVideoInputDevice(newDevices, localVideo) {
|
2017-10-12 23:02:29 +00:00
|
|
|
const availableVideoInputDevices = newDevices.filter(
|
2016-06-13 11:49:00 +00:00
|
|
|
d => d.kind === 'videoinput');
|
2019-05-07 08:53:01 +00:00
|
|
|
const selectedVideoInputDeviceId = getUserSelectedCameraDeviceId(APP.store.getState());
|
2017-10-12 23:02:29 +00:00
|
|
|
const selectedVideoInputDevice = availableVideoInputDevices.find(
|
2016-06-13 11:49:00 +00:00
|
|
|
d => d.deviceId === selectedVideoInputDeviceId);
|
|
|
|
|
|
|
|
// Here we handle case when no video input device was initially plugged,
|
|
|
|
// but then device is connected OR new device was connected when
|
|
|
|
// previous track has ended.
|
|
|
|
if (!localVideo || localVideo.disposed || localVideo.isEnded()) {
|
|
|
|
// If we have new video device and permission to use it was granted
|
|
|
|
// (label is not an empty string), then we will try to use the first
|
|
|
|
// available device.
|
2019-04-12 16:10:38 +00:00
|
|
|
if (selectedVideoInputDevice && selectedVideoInputDeviceId) {
|
|
|
|
return selectedVideoInputDeviceId;
|
|
|
|
} else if (availableVideoInputDevices.length
|
2017-10-12 23:02:29 +00:00
|
|
|
&& availableVideoInputDevices[0].label !== '') {
|
2016-06-13 11:49:00 +00:00
|
|
|
return availableVideoInputDevices[0].deviceId;
|
|
|
|
}
|
2017-10-12 23:02:29 +00:00
|
|
|
} else if (selectedVideoInputDevice
|
|
|
|
&& selectedVideoInputDeviceId !== localVideo.getDeviceId()) {
|
2016-06-13 11:49:00 +00:00
|
|
|
// And here we handle case when we already have some device working,
|
|
|
|
// but we plug-in a "preferred" (previously selected in settings, stored
|
|
|
|
// in local storage) device.
|
2017-10-12 23:02:29 +00:00
|
|
|
return selectedVideoInputDeviceId;
|
2016-06-13 11:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
/**
|
|
|
|
* Determines if currently selected media devices should be changed after
|
|
|
|
* list of available devices has been changed.
|
|
|
|
* @param {MediaDeviceInfo[]} newDevices
|
|
|
|
* @param {boolean} isSharingScreen
|
|
|
|
* @param {JitsiLocalTrack} localVideo
|
|
|
|
* @param {JitsiLocalTrack} localAudio
|
|
|
|
* @returns {{
|
|
|
|
* audioinput: (string|undefined),
|
|
|
|
* videoinput: (string|undefined),
|
|
|
|
* audiooutput: (string|undefined)
|
|
|
|
* }}
|
|
|
|
*/
|
2017-10-12 23:02:29 +00:00
|
|
|
getNewMediaDevicesAfterDeviceListChanged( // eslint-disable-line max-params
|
2017-10-02 23:08:07 +00:00
|
|
|
newDevices,
|
|
|
|
isSharingScreen,
|
|
|
|
localVideo,
|
|
|
|
localAudio) {
|
2016-06-13 11:49:00 +00:00
|
|
|
return {
|
|
|
|
audioinput: getNewAudioInputDevice(newDevices, localAudio),
|
2019-04-12 16:10:38 +00:00
|
|
|
videoinput: isSharingScreen ? undefined : getNewVideoInputDevice(newDevices, localVideo),
|
2016-06-13 11:49:00 +00:00
|
|
|
audiooutput: getNewAudioOutputDevice(newDevices)
|
|
|
|
};
|
|
|
|
},
|
2017-10-12 23:02:29 +00:00
|
|
|
|
2016-06-13 11:49:00 +00:00
|
|
|
/**
|
|
|
|
* Tries to create new local tracks for new devices obtained after device
|
|
|
|
* list changed. Shows error dialog in case of failures.
|
|
|
|
* @param {function} createLocalTracks
|
|
|
|
* @param {string} (cameraDeviceId)
|
|
|
|
* @param {string} (micDeviceId)
|
|
|
|
* @returns {Promise.<JitsiLocalTrack[]>}
|
|
|
|
*/
|
|
|
|
createLocalTracksAfterDeviceListChanged(
|
2017-10-02 23:08:07 +00:00
|
|
|
createLocalTracks,
|
|
|
|
cameraDeviceId,
|
|
|
|
micDeviceId) {
|
2016-06-13 11:49:00 +00:00
|
|
|
let audioTrackError;
|
|
|
|
let videoTrackError;
|
2017-10-12 23:02:29 +00:00
|
|
|
const audioRequested = Boolean(micDeviceId);
|
|
|
|
const videoRequested = Boolean(cameraDeviceId);
|
2016-06-13 11:49:00 +00:00
|
|
|
|
|
|
|
if (audioRequested && videoRequested) {
|
|
|
|
// First we try to create both audio and video tracks together.
|
2017-10-02 23:08:07 +00:00
|
|
|
return (
|
|
|
|
createLocalTracks({
|
2017-10-12 23:02:29 +00:00
|
|
|
devices: [ 'audio', 'video' ],
|
|
|
|
cameraDeviceId,
|
|
|
|
micDeviceId
|
2017-10-02 23:08:07 +00:00
|
|
|
})
|
2017-10-12 23:02:29 +00:00
|
|
|
|
2017-10-02 23:08:07 +00:00
|
|
|
// If we fail to do this, try to create them separately.
|
|
|
|
.catch(() => Promise.all([
|
2017-10-12 23:02:29 +00:00
|
|
|
createAudioTrack(false).then(([ stream ]) => stream),
|
|
|
|
createVideoTrack(false).then(([ stream ]) => stream)
|
2017-10-02 23:08:07 +00:00
|
|
|
]))
|
|
|
|
.then(tracks => {
|
|
|
|
if (audioTrackError) {
|
2019-05-29 21:17:07 +00:00
|
|
|
APP.store.dispatch(notifyMicError(audioTrackError));
|
2017-10-02 23:08:07 +00:00
|
|
|
}
|
2017-07-31 18:36:41 +00:00
|
|
|
|
2017-10-02 23:08:07 +00:00
|
|
|
if (videoTrackError) {
|
2019-05-29 21:17:07 +00:00
|
|
|
APP.store.dispatch(notifyCameraError(videoTrackError));
|
2017-10-02 23:08:07 +00:00
|
|
|
}
|
2016-06-13 11:49:00 +00:00
|
|
|
|
2017-10-02 23:08:07 +00:00
|
|
|
return tracks.filter(t => typeof t !== 'undefined');
|
|
|
|
}));
|
2016-06-13 11:49:00 +00:00
|
|
|
} else if (videoRequested && !audioRequested) {
|
|
|
|
return createVideoTrack();
|
|
|
|
} else if (audioRequested && !videoRequested) {
|
|
|
|
return createAudioTrack();
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:02:29 +00:00
|
|
|
return Promise.resolve([]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-04-12 16:10:38 +00:00
|
|
|
function createAudioTrack(showError = true) {
|
2017-10-02 23:08:07 +00:00
|
|
|
return (
|
|
|
|
createLocalTracks({
|
2017-10-12 23:02:29 +00:00
|
|
|
devices: [ 'audio' ],
|
2016-06-24 09:47:13 +00:00
|
|
|
cameraDeviceId: null,
|
2017-10-12 23:02:29 +00:00
|
|
|
micDeviceId
|
2016-06-24 09:47:13 +00:00
|
|
|
})
|
2016-06-13 11:49:00 +00:00
|
|
|
.catch(err => {
|
|
|
|
audioTrackError = err;
|
2019-05-29 21:17:07 +00:00
|
|
|
showError && APP.store.disptach(notifyMicError(err));
|
2017-10-12 23:02:29 +00:00
|
|
|
|
2016-06-13 11:49:00 +00:00
|
|
|
return [];
|
2017-10-02 23:08:07 +00:00
|
|
|
}));
|
2016-06-13 11:49:00 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 23:02:29 +00:00
|
|
|
/**
|
2017-11-16 18:26:14 +00:00
|
|
|
*
|
2017-10-12 23:02:29 +00:00
|
|
|
*/
|
2019-04-12 16:10:38 +00:00
|
|
|
function createVideoTrack(showError = true) {
|
2017-10-02 23:08:07 +00:00
|
|
|
return (
|
|
|
|
createLocalTracks({
|
2017-10-12 23:02:29 +00:00
|
|
|
devices: [ 'video' ],
|
|
|
|
cameraDeviceId,
|
2016-06-24 09:47:13 +00:00
|
|
|
micDeviceId: null
|
|
|
|
})
|
2016-06-13 11:49:00 +00:00
|
|
|
.catch(err => {
|
|
|
|
videoTrackError = err;
|
2019-05-29 21:17:07 +00:00
|
|
|
showError && APP.store.dispatch(notifyCameraError(err));
|
2017-10-12 23:02:29 +00:00
|
|
|
|
2016-06-13 11:49:00 +00:00
|
|
|
return [];
|
2017-10-02 23:08:07 +00:00
|
|
|
}));
|
2016-06-13 11:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-03 16:12:04 +00:00
|
|
|
};
|