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

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