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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. } else {
  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. if (selectedAudioInputDevice &&
  57. selectedAudioInputDeviceId !== localAudio.getDeviceId()) {
  58. return selectedAudioInputDeviceId;
  59. }
  60. }
  61. }
  62. /**
  63. * Determines if currently selected video input device should be changed after
  64. * list of available devices has been changed.
  65. * @param {MediaDeviceInfo[]} newDevices
  66. * @param {JitsiLocalTrack} localVideo
  67. * @returns {string|undefined} - ID of new camera device to use, undefined
  68. * if video input device should not be changed.
  69. */
  70. function getNewVideoInputDevice(newDevices, localVideo) {
  71. let availableVideoInputDevices = newDevices.filter(
  72. d => d.kind === 'videoinput');
  73. let selectedVideoInputDeviceId = APP.settings.getCameraDeviceId();
  74. let selectedVideoInputDevice = availableVideoInputDevices.find(
  75. d => d.deviceId === selectedVideoInputDeviceId);
  76. // Here we handle case when no video input device was initially plugged,
  77. // but then device is connected OR new device was connected when
  78. // previous track has ended.
  79. if (!localVideo || localVideo.disposed || localVideo.isEnded()) {
  80. // If we have new video device and permission to use it was granted
  81. // (label is not an empty string), then we will try to use the first
  82. // available device.
  83. if (availableVideoInputDevices.length &&
  84. availableVideoInputDevices[0].label !== '') {
  85. return availableVideoInputDevices[0].deviceId;
  86. }
  87. } else {
  88. // And here we handle case when we already have some device working,
  89. // but we plug-in a "preferred" (previously selected in settings, stored
  90. // in local storage) device.
  91. if (selectedVideoInputDevice &&
  92. selectedVideoInputDeviceId !== localVideo.getDeviceId()) {
  93. return selectedVideoInputDeviceId;
  94. }
  95. }
  96. }
  97. export default {
  98. /**
  99. * Returns list of devices of single kind.
  100. * @param {MediaDeviceInfo[]} devices
  101. * @param {'audioinput'|'audiooutput'|'videoinput'} kind
  102. * @returns {MediaDeviceInfo[]}
  103. */
  104. getDevicesFromListByKind(devices, kind) {
  105. return devices.filter(d => d.kind === kind);
  106. },
  107. /**
  108. * Stores lists of current 'audioinput', 'videoinput' and 'audiooutput'
  109. * devices.
  110. * @param {MediaDeviceInfo[]} devices
  111. */
  112. setCurrentMediaDevices(devices) {
  113. currentAudioInputDevices =
  114. this.getDevicesFromListByKind(devices, 'audioinput');
  115. currentVideoInputDevices =
  116. this.getDevicesFromListByKind(devices, 'videoinput');
  117. currentAudioOutputDevices =
  118. this.getDevicesFromListByKind(devices, 'audiooutput');
  119. },
  120. /**
  121. * Returns lists of current 'audioinput', 'videoinput' and 'audiooutput'
  122. * devices.
  123. * @returns {{
  124. * audioinput: (MediaDeviceInfo[]|undefined),
  125. * videoinput: (MediaDeviceInfo[]|undefined),
  126. * audiooutput: (MediaDeviceInfo[]|undefined),
  127. * }}
  128. */
  129. getCurrentMediaDevices() {
  130. return {
  131. audioinput: currentAudioInputDevices,
  132. videoinput: currentVideoInputDevices,
  133. audiooutput: currentAudioOutputDevices
  134. };
  135. },
  136. /**
  137. * Determines if currently selected media devices should be changed after
  138. * list of available devices has been changed.
  139. * @param {MediaDeviceInfo[]} newDevices
  140. * @param {boolean} isSharingScreen
  141. * @param {JitsiLocalTrack} localVideo
  142. * @param {JitsiLocalTrack} localAudio
  143. * @returns {{
  144. * audioinput: (string|undefined),
  145. * videoinput: (string|undefined),
  146. * audiooutput: (string|undefined)
  147. * }}
  148. */
  149. getNewMediaDevicesAfterDeviceListChanged(
  150. newDevices,
  151. isSharingScreen,
  152. localVideo,
  153. localAudio) {
  154. return {
  155. audioinput: getNewAudioInputDevice(newDevices, localAudio),
  156. videoinput: !isSharingScreen &&
  157. getNewVideoInputDevice(newDevices, localVideo),
  158. audiooutput: getNewAudioOutputDevice(newDevices)
  159. };
  160. },
  161. /**
  162. * Tries to create new local tracks for new devices obtained after device
  163. * list changed. Shows error dialog in case of failures.
  164. * @param {function} createLocalTracks
  165. * @param {string} (cameraDeviceId)
  166. * @param {string} (micDeviceId)
  167. * @returns {Promise.<JitsiLocalTrack[]>}
  168. */
  169. createLocalTracksAfterDeviceListChanged(
  170. createLocalTracks,
  171. cameraDeviceId,
  172. 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 (
  180. createLocalTracks({
  181. devices: ['audio', 'video'],
  182. cameraDeviceId: cameraDeviceId,
  183. micDeviceId: micDeviceId
  184. })
  185. // If we fail to do this, try to create them separately.
  186. .catch(() => Promise.all([
  187. createAudioTrack(false).then(([stream]) => stream),
  188. createVideoTrack(false).then(([stream]) => stream)
  189. ]))
  190. .then(tracks => {
  191. if (audioTrackError) {
  192. APP.UI.showMicErrorNotification(audioTrackError);
  193. }
  194. if (videoTrackError) {
  195. APP.UI.showCameraErrorNotification(videoTrackError);
  196. }
  197. return tracks.filter(t => typeof t !== 'undefined');
  198. }));
  199. } else if (videoRequested && !audioRequested) {
  200. return createVideoTrack();
  201. } else if (audioRequested && !videoRequested) {
  202. return createAudioTrack();
  203. } else {
  204. return Promise.resolve([]);
  205. }
  206. function createAudioTrack(showError) {
  207. return (
  208. createLocalTracks({
  209. devices: ['audio'],
  210. cameraDeviceId: null,
  211. micDeviceId: micDeviceId
  212. })
  213. .catch(err => {
  214. audioTrackError = err;
  215. showError && APP.UI.showMicErrorNotification(err);
  216. return [];
  217. }));
  218. }
  219. function createVideoTrack(showError) {
  220. return (
  221. createLocalTracks({
  222. devices: ['video'],
  223. cameraDeviceId: cameraDeviceId,
  224. micDeviceId: null
  225. })
  226. .catch(err => {
  227. videoTrackError = err;
  228. showError && APP.UI.showCameraErrorNotification(err);
  229. return [];
  230. }));
  231. }
  232. }
  233. };