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 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* global APP, JitsiMeetJS */
  2. import { getAudioOutputDeviceId } from '../../react/features/base/devices';
  3. /**
  4. * Determines if currently selected audio output device should be changed after
  5. * list of available devices has been changed.
  6. * @param {MediaDeviceInfo[]} newDevices
  7. * @returns {string|undefined} - ID of new audio output device to use, undefined
  8. * if audio output device should not be changed.
  9. */
  10. function getNewAudioOutputDevice(newDevices) {
  11. if (!JitsiMeetJS.mediaDevices.isDeviceChangeAvailable('output')) {
  12. return;
  13. }
  14. const selectedAudioOutputDeviceId = getAudioOutputDeviceId();
  15. const availableAudioOutputDevices = newDevices.filter(
  16. d => d.kind === 'audiooutput');
  17. // Switch to 'default' audio output device if we don't have the selected one
  18. // available anymore.
  19. if (selectedAudioOutputDeviceId !== 'default'
  20. && !availableAudioOutputDevices.find(d =>
  21. d.deviceId === selectedAudioOutputDeviceId)) {
  22. return 'default';
  23. }
  24. }
  25. /**
  26. * Determines if currently selected audio input device should be changed after
  27. * list of available devices has been changed.
  28. * @param {MediaDeviceInfo[]} newDevices
  29. * @param {JitsiLocalTrack} localAudio
  30. * @returns {string|undefined} - ID of new microphone device to use, undefined
  31. * if audio input device should not be changed.
  32. */
  33. function getNewAudioInputDevice(newDevices, localAudio) {
  34. const availableAudioInputDevices = newDevices.filter(
  35. d => d.kind === 'audioinput');
  36. const settings = APP.store.getState()['features/base/settings'];
  37. const selectedAudioInputDeviceId = settings.micDeviceId;
  38. const selectedAudioInputDevice = availableAudioInputDevices.find(
  39. d => d.deviceId === selectedAudioInputDeviceId);
  40. // Here we handle case when no device was initially plugged, but
  41. // then it's connected OR new device was connected when previous
  42. // track has ended.
  43. if (!localAudio || localAudio.disposed || localAudio.isEnded()) {
  44. // If we have new audio device and permission to use it was granted
  45. // (label is not an empty string), then we will try to use the first
  46. // available device.
  47. if (availableAudioInputDevices.length
  48. && availableAudioInputDevices[0].label !== '') {
  49. return availableAudioInputDevices[0].deviceId;
  50. }
  51. } else if (selectedAudioInputDevice
  52. && selectedAudioInputDeviceId !== localAudio.getDeviceId()) {
  53. // And here we handle case when we already have some device working,
  54. // but we plug-in a "preferred" (previously selected in settings, stored
  55. // in local storage) device.
  56. return selectedAudioInputDeviceId;
  57. }
  58. }
  59. /**
  60. * Determines if currently selected video input device should be changed after
  61. * list of available devices has been changed.
  62. * @param {MediaDeviceInfo[]} newDevices
  63. * @param {JitsiLocalTrack} localVideo
  64. * @returns {string|undefined} - ID of new camera device to use, undefined
  65. * if video input device should not be changed.
  66. */
  67. function getNewVideoInputDevice(newDevices, localVideo) {
  68. const availableVideoInputDevices = newDevices.filter(
  69. d => d.kind === 'videoinput');
  70. const settings = APP.store.getState()['features/base/settings'];
  71. const selectedVideoInputDeviceId = settings.cameraDeviceId;
  72. const selectedVideoInputDevice = availableVideoInputDevices.find(
  73. d => d.deviceId === selectedVideoInputDeviceId);
  74. // Here we handle case when no video input device was initially plugged,
  75. // but then device is connected OR new device was connected when
  76. // previous track has ended.
  77. if (!localVideo || localVideo.disposed || localVideo.isEnded()) {
  78. // If we have new video device and permission to use it was granted
  79. // (label is not an empty string), then we will try to use the first
  80. // available device.
  81. if (availableVideoInputDevices.length
  82. && availableVideoInputDevices[0].label !== '') {
  83. return availableVideoInputDevices[0].deviceId;
  84. }
  85. } else if (selectedVideoInputDevice
  86. && selectedVideoInputDeviceId !== localVideo.getDeviceId()) {
  87. // And here we handle case when we already have some device working,
  88. // but we plug-in a "preferred" (previously selected in settings, stored
  89. // in local storage) device.
  90. return selectedVideoInputDeviceId;
  91. }
  92. }
  93. export default {
  94. /**
  95. * Determines if currently selected media devices should be changed after
  96. * list of available devices has been changed.
  97. * @param {MediaDeviceInfo[]} newDevices
  98. * @param {boolean} isSharingScreen
  99. * @param {JitsiLocalTrack} localVideo
  100. * @param {JitsiLocalTrack} localAudio
  101. * @returns {{
  102. * audioinput: (string|undefined),
  103. * videoinput: (string|undefined),
  104. * audiooutput: (string|undefined)
  105. * }}
  106. */
  107. getNewMediaDevicesAfterDeviceListChanged( // eslint-disable-line max-params
  108. newDevices,
  109. isSharingScreen,
  110. localVideo,
  111. localAudio) {
  112. return {
  113. audioinput: getNewAudioInputDevice(newDevices, localAudio),
  114. videoinput: !isSharingScreen
  115. && getNewVideoInputDevice(newDevices, localVideo),
  116. audiooutput: getNewAudioOutputDevice(newDevices)
  117. };
  118. },
  119. /**
  120. * Tries to create new local tracks for new devices obtained after device
  121. * list changed. Shows error dialog in case of failures.
  122. * @param {function} createLocalTracks
  123. * @param {string} (cameraDeviceId)
  124. * @param {string} (micDeviceId)
  125. * @returns {Promise.<JitsiLocalTrack[]>}
  126. */
  127. createLocalTracksAfterDeviceListChanged(
  128. createLocalTracks,
  129. cameraDeviceId,
  130. micDeviceId) {
  131. let audioTrackError;
  132. let videoTrackError;
  133. const audioRequested = Boolean(micDeviceId);
  134. const videoRequested = Boolean(cameraDeviceId);
  135. if (audioRequested && videoRequested) {
  136. // First we try to create both audio and video tracks together.
  137. return (
  138. createLocalTracks({
  139. devices: [ 'audio', 'video' ],
  140. cameraDeviceId,
  141. micDeviceId
  142. })
  143. // If we fail to do this, try to create them separately.
  144. .catch(() => Promise.all([
  145. createAudioTrack(false).then(([ stream ]) => stream),
  146. createVideoTrack(false).then(([ stream ]) => stream)
  147. ]))
  148. .then(tracks => {
  149. if (audioTrackError) {
  150. APP.UI.showMicErrorNotification(audioTrackError);
  151. }
  152. if (videoTrackError) {
  153. APP.UI.showCameraErrorNotification(videoTrackError);
  154. }
  155. return tracks.filter(t => typeof t !== 'undefined');
  156. }));
  157. } else if (videoRequested && !audioRequested) {
  158. return createVideoTrack();
  159. } else if (audioRequested && !videoRequested) {
  160. return createAudioTrack();
  161. }
  162. return Promise.resolve([]);
  163. /**
  164. *
  165. */
  166. function createAudioTrack(showError) {
  167. return (
  168. createLocalTracks({
  169. devices: [ 'audio' ],
  170. cameraDeviceId: null,
  171. micDeviceId
  172. })
  173. .catch(err => {
  174. audioTrackError = err;
  175. showError && APP.UI.showMicErrorNotification(err);
  176. return [];
  177. }));
  178. }
  179. /**
  180. *
  181. */
  182. function createVideoTrack(showError) {
  183. return (
  184. createLocalTracks({
  185. devices: [ 'video' ],
  186. cameraDeviceId,
  187. micDeviceId: null
  188. })
  189. .catch(err => {
  190. videoTrackError = err;
  191. showError && APP.UI.showCameraErrorNotification(err);
  192. return [];
  193. }));
  194. }
  195. }
  196. };