You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mediaDeviceHelper.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* global APP, JitsiMeetJS */
  2. import {
  3. getAudioOutputDeviceId,
  4. notifyCameraError,
  5. notifyMicError
  6. } from '../../react/features/base/devices';
  7. import {
  8. getUserSelectedCameraDeviceId,
  9. getUserSelectedMicDeviceId,
  10. getUserSelectedOutputDeviceId,
  11. updateSettings
  12. } from '../../react/features/base/settings';
  13. /**
  14. * Determines if currently selected audio output device should be changed after
  15. * list of available devices has been changed.
  16. * @param {MediaDeviceInfo[]} newDevices
  17. * @returns {string|undefined} - ID of new audio output device to use, undefined
  18. * if audio output device should not be changed.
  19. */
  20. function getNewAudioOutputDevice(newDevices) {
  21. if (!JitsiMeetJS.mediaDevices.isDeviceChangeAvailable('output')) {
  22. return;
  23. }
  24. const selectedAudioOutputDeviceId = getAudioOutputDeviceId();
  25. const availableAudioOutputDevices = newDevices.filter(
  26. d => d.kind === 'audiooutput');
  27. // Switch to 'default' audio output device if we don't have the selected one
  28. // available anymore.
  29. if (selectedAudioOutputDeviceId !== 'default'
  30. && !availableAudioOutputDevices.find(d =>
  31. d.deviceId === selectedAudioOutputDeviceId)) {
  32. return 'default';
  33. }
  34. const preferredAudioOutputDeviceId = getUserSelectedOutputDeviceId(APP.store.getState());
  35. // if the preferred one is not the selected and is available in the new devices
  36. // we want to use it as it was just added
  37. if (preferredAudioOutputDeviceId
  38. && preferredAudioOutputDeviceId !== selectedAudioOutputDeviceId
  39. && availableAudioOutputDevices.find(d => d.deviceId === preferredAudioOutputDeviceId)) {
  40. return preferredAudioOutputDeviceId;
  41. }
  42. }
  43. /**
  44. * Determines if currently selected audio input device should be changed after
  45. * list of available devices has been changed.
  46. * @param {MediaDeviceInfo[]} newDevices
  47. * @param {JitsiLocalTrack} localAudio
  48. * @param {boolean} newLabel
  49. * @returns {string|undefined} - ID of new microphone device to use, undefined
  50. * if audio input device should not be changed.
  51. */
  52. function getNewAudioInputDevice(newDevices, localAudio, newLabel) {
  53. const availableAudioInputDevices = newDevices.filter(
  54. d => d.kind === 'audioinput');
  55. const selectedAudioInputDeviceId = getUserSelectedMicDeviceId(APP.store.getState());
  56. const selectedAudioInputDevice = availableAudioInputDevices.find(
  57. d => d.deviceId === selectedAudioInputDeviceId);
  58. const localAudioDeviceId = localAudio?.getDeviceId();
  59. const localAudioDevice = availableAudioInputDevices.find(
  60. d => d.deviceId === localAudioDeviceId);
  61. // Here we handle case when no device was initially plugged, but
  62. // then it's connected OR new device was connected when previous
  63. // track has ended.
  64. if (!localAudio || localAudio.disposed || localAudio.isEnded()) {
  65. // If we have new audio device and permission to use it was granted
  66. // (label is not an empty string), then we will try to use the first
  67. // available device.
  68. if (selectedAudioInputDevice && selectedAudioInputDeviceId) {
  69. return selectedAudioInputDeviceId;
  70. } else if (availableAudioInputDevices.length
  71. && availableAudioInputDevices[0].label !== '') {
  72. return availableAudioInputDevices[0].deviceId;
  73. }
  74. } else if (selectedAudioInputDevice
  75. && selectedAudioInputDeviceId !== localAudioDeviceId) {
  76. if (newLabel) {
  77. // If a Firefox user with manual permission prompt chose a different
  78. // device from what we have stored as the preferred device we accept
  79. // and store that as the new preferred device.
  80. APP.store.dispatch(updateSettings({
  81. userSelectedMicDeviceId: localAudioDeviceId,
  82. userSelectedMicDeviceLabel: localAudioDevice.label
  83. }));
  84. } else {
  85. // And here we handle case when we already have some device working,
  86. // but we plug-in a "preferred" (previously selected in settings, stored
  87. // in local storage) device.
  88. return selectedAudioInputDeviceId;
  89. }
  90. }
  91. }
  92. /**
  93. * Determines if currently selected video input device should be changed after
  94. * list of available devices has been changed.
  95. * @param {MediaDeviceInfo[]} newDevices
  96. * @param {JitsiLocalTrack} localVideo
  97. * @param {boolean} newLabel
  98. * @returns {string|undefined} - ID of new camera device to use, undefined
  99. * if video input device should not be changed.
  100. */
  101. function getNewVideoInputDevice(newDevices, localVideo, newLabel) {
  102. const availableVideoInputDevices = newDevices.filter(
  103. d => d.kind === 'videoinput');
  104. const selectedVideoInputDeviceId = getUserSelectedCameraDeviceId(APP.store.getState());
  105. const selectedVideoInputDevice = availableVideoInputDevices.find(
  106. d => d.deviceId === selectedVideoInputDeviceId);
  107. const localVideoDeviceId = localVideo?.getDeviceId();
  108. const localVideoDevice = availableVideoInputDevices.find(
  109. d => d.deviceId === localVideoDeviceId);
  110. // Here we handle case when no video input device was initially plugged,
  111. // but then device is connected OR new device was connected when
  112. // previous track has ended.
  113. if (!localVideo || localVideo.disposed || localVideo.isEnded()) {
  114. // If we have new video device and permission to use it was granted
  115. // (label is not an empty string), then we will try to use the first
  116. // available device.
  117. if (selectedVideoInputDevice && selectedVideoInputDeviceId) {
  118. return selectedVideoInputDeviceId;
  119. } else if (availableVideoInputDevices.length
  120. && availableVideoInputDevices[0].label !== '') {
  121. return availableVideoInputDevices[0].deviceId;
  122. }
  123. } else if (selectedVideoInputDevice
  124. && selectedVideoInputDeviceId !== localVideoDeviceId) {
  125. if (newLabel) {
  126. // If a Firefox user with manual permission prompt chose a different
  127. // device from what we have stored as the preferred device we accept
  128. // and store that as the new preferred device.
  129. APP.store.dispatch(updateSettings({
  130. userSelectedCameraDeviceId: localVideoDeviceId,
  131. userSelectedCameraDeviceLabel: localVideoDevice.label
  132. }));
  133. } else {
  134. // And here we handle case when we already have some device working,
  135. // but we plug-in a "preferred" (previously selected in settings, stored
  136. // in local storage) device.
  137. return selectedVideoInputDeviceId;
  138. }
  139. }
  140. }
  141. export default {
  142. /**
  143. * Determines if currently selected media devices should be changed after
  144. * list of available devices has been changed.
  145. * @param {MediaDeviceInfo[]} newDevices
  146. * @param {boolean} isSharingScreen
  147. * @param {JitsiLocalTrack} localVideo
  148. * @param {JitsiLocalTrack} localAudio
  149. * @returns {{
  150. * audioinput: (string|undefined),
  151. * videoinput: (string|undefined),
  152. * audiooutput: (string|undefined)
  153. * }}
  154. */
  155. getNewMediaDevicesAfterDeviceListChanged( // eslint-disable-line max-params
  156. newDevices,
  157. isSharingScreen,
  158. localVideo,
  159. localAudio,
  160. newLabels) {
  161. return {
  162. audioinput: getNewAudioInputDevice(newDevices, localAudio, newLabels),
  163. videoinput: isSharingScreen ? undefined : getNewVideoInputDevice(newDevices, localVideo, newLabels),
  164. audiooutput: getNewAudioOutputDevice(newDevices)
  165. };
  166. },
  167. /**
  168. * Checks if the only difference between an object of known devices compared
  169. * to an array of new devices are only the labels for the devices.
  170. * @param {Object} oldDevices
  171. * @param {MediaDeviceInfo[]} newDevices
  172. * @returns {boolean}
  173. */
  174. newDeviceListAddedLabelsOnly(oldDevices, newDevices) {
  175. const oldDevicesFlattend = oldDevices.audioInput.concat(oldDevices.audioOutput).concat(oldDevices.videoInput);
  176. if (oldDevicesFlattend.length !== newDevices.length) {
  177. return false;
  178. }
  179. oldDevicesFlattend.forEach(oldDevice => {
  180. if (oldDevice.label !== '') {
  181. return false;
  182. }
  183. const newDevice = newDevices.find(nd => nd.deviceId === oldDevice.deviceId);
  184. if (!newDevice || newDevice.label === '') {
  185. return false;
  186. }
  187. });
  188. return true;
  189. },
  190. /**
  191. * Tries to create new local tracks for new devices obtained after device
  192. * list changed. Shows error dialog in case of failures.
  193. * @param {function} createLocalTracks
  194. * @param {string} (cameraDeviceId)
  195. * @param {string} (micDeviceId)
  196. * @returns {Promise.<JitsiLocalTrack[]>}
  197. */
  198. createLocalTracksAfterDeviceListChanged(
  199. createLocalTracks,
  200. cameraDeviceId,
  201. micDeviceId) {
  202. let audioTrackError;
  203. let videoTrackError;
  204. const audioRequested = Boolean(micDeviceId);
  205. const videoRequested = Boolean(cameraDeviceId);
  206. if (audioRequested && videoRequested) {
  207. // First we try to create both audio and video tracks together.
  208. return (
  209. createLocalTracks({
  210. devices: [ 'audio', 'video' ],
  211. cameraDeviceId,
  212. micDeviceId
  213. })
  214. // If we fail to do this, try to create them separately.
  215. .catch(() => Promise.all([
  216. createAudioTrack(false).then(([ stream ]) => stream),
  217. createVideoTrack(false).then(([ stream ]) => stream)
  218. ]))
  219. .then(tracks => {
  220. if (audioTrackError) {
  221. APP.store.dispatch(notifyMicError(audioTrackError));
  222. }
  223. if (videoTrackError) {
  224. APP.store.dispatch(notifyCameraError(videoTrackError));
  225. }
  226. return tracks.filter(t => typeof t !== 'undefined');
  227. }));
  228. } else if (videoRequested && !audioRequested) {
  229. return createVideoTrack();
  230. } else if (audioRequested && !videoRequested) {
  231. return createAudioTrack();
  232. }
  233. return Promise.resolve([]);
  234. /**
  235. *
  236. */
  237. function createAudioTrack(showError = true) {
  238. return (
  239. createLocalTracks({
  240. devices: [ 'audio' ],
  241. cameraDeviceId: null,
  242. micDeviceId
  243. })
  244. .catch(err => {
  245. audioTrackError = err;
  246. showError && APP.store.dispatch(notifyMicError(err));
  247. return [];
  248. }));
  249. }
  250. /**
  251. *
  252. */
  253. function createVideoTrack(showError = true) {
  254. return (
  255. createLocalTracks({
  256. devices: [ 'video' ],
  257. cameraDeviceId,
  258. micDeviceId: null
  259. })
  260. .catch(err => {
  261. videoTrackError = err;
  262. showError && APP.store.dispatch(notifyCameraError(err));
  263. return [];
  264. }));
  265. }
  266. }
  267. };