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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import JitsiMeetJS from '../lib-jitsi-meet';
  2. import {
  3. getUserSelectedOutputDeviceId,
  4. updateSettings
  5. } from '../settings';
  6. import {
  7. ADD_PENDING_DEVICE_REQUEST,
  8. CHECK_AND_NOTIFY_FOR_NEW_DEVICE,
  9. NOTIFY_CAMERA_ERROR,
  10. NOTIFY_MIC_ERROR,
  11. REMOVE_PENDING_DEVICE_REQUESTS,
  12. SET_AUDIO_INPUT_DEVICE,
  13. SET_VIDEO_INPUT_DEVICE,
  14. UPDATE_DEVICE_LIST
  15. } from './actionTypes';
  16. import {
  17. areDeviceLabelsInitialized,
  18. getDeviceIdByLabel,
  19. getDeviceLabelById,
  20. getDevicesFromURL,
  21. setAudioOutputDeviceId
  22. } from './functions';
  23. import logger from './logger';
  24. /**
  25. * Maps the WebRTC string for device type to the keys used to store configure,
  26. * within redux, which devices should be used by default.
  27. */
  28. const DEVICE_TYPE_TO_SETTINGS_KEYS = {
  29. audioInput: {
  30. currentDeviceId: 'micDeviceId',
  31. userSelectedDeviceId: 'userSelectedMicDeviceId',
  32. userSelectedDeviceLabel: 'userSelectedMicDeviceLabel'
  33. },
  34. audioOutput: {
  35. currentDeviceId: 'audioOutputDeviceId',
  36. userSelectedDeviceId: 'userSelectedAudioOutputDeviceId',
  37. userSelectedDeviceLabel: 'userSelectedAudioOutputDeviceLabel'
  38. },
  39. videoInput: {
  40. currentDeviceId: 'audioOutputDeviceId',
  41. userSelectedDeviceId: 'userSelectedCameraDeviceId',
  42. userSelectedDeviceLabel: 'userSelectedCameraDeviceLabel'
  43. }
  44. };
  45. /**
  46. * Adds a pending device request.
  47. *
  48. * @param {Object} request - The request to be added.
  49. * @returns {{
  50. * type: ADD_PENDING_DEVICE_REQUEST,
  51. * request: Object
  52. * }}
  53. */
  54. export function addPendingDeviceRequest(request) {
  55. return {
  56. type: ADD_PENDING_DEVICE_REQUEST,
  57. request
  58. };
  59. }
  60. /**
  61. * Configures the initial A/V devices before the conference has started.
  62. *
  63. * @returns {Function}
  64. */
  65. export function configureInitialDevices() {
  66. return (dispatch, getState) => {
  67. const deviceLabels = getDevicesFromURL(getState());
  68. let updateSettingsPromise;
  69. if (deviceLabels) {
  70. updateSettingsPromise = dispatch(getAvailableDevices()).then(() => {
  71. const state = getState();
  72. if (!areDeviceLabelsInitialized(state)) {
  73. // The labels are not available if the A/V permissions are
  74. // not yet granted.
  75. Object.keys(deviceLabels).forEach(key => {
  76. dispatch(addPendingDeviceRequest({
  77. type: 'devices',
  78. name: 'setDevice',
  79. device: {
  80. kind: key.toLowerCase(),
  81. label: deviceLabels[key]
  82. },
  83. // eslint-disable-next-line no-empty-function
  84. responseCallback() {}
  85. }));
  86. });
  87. return;
  88. }
  89. const newSettings = {};
  90. Object.keys(deviceLabels).forEach(key => {
  91. const label = deviceLabels[key];
  92. const deviceId = getDeviceIdByLabel(state, label, key);
  93. if (deviceId) {
  94. const settingsTranslationMap = DEVICE_TYPE_TO_SETTINGS_KEYS[key];
  95. newSettings[settingsTranslationMap.currentDeviceId] = deviceId;
  96. newSettings[settingsTranslationMap.userSelectedDeviceId] = deviceId;
  97. newSettings[settingsTranslationMap.userSelectedDeviceLabel] = label;
  98. }
  99. });
  100. dispatch(updateSettings(newSettings));
  101. });
  102. } else {
  103. updateSettingsPromise = Promise.resolve();
  104. }
  105. return updateSettingsPromise
  106. .then(() => {
  107. const userSelectedAudioOutputDeviceId = getUserSelectedOutputDeviceId(getState());
  108. return setAudioOutputDeviceId(userSelectedAudioOutputDeviceId, dispatch)
  109. .catch(ex => logger.warn(`Failed to set audio output device.
  110. Default audio output device will be used instead ${ex}`));
  111. });
  112. };
  113. }
  114. /**
  115. * Queries for connected A/V input and output devices and updates the redux
  116. * state of known devices.
  117. *
  118. * @returns {Function}
  119. */
  120. export function getAvailableDevices() {
  121. return dispatch => new Promise(resolve => {
  122. const { mediaDevices } = JitsiMeetJS;
  123. if (mediaDevices.isDeviceListAvailable()
  124. && mediaDevices.isDeviceChangeAvailable()) {
  125. mediaDevices.enumerateDevices(devices => {
  126. dispatch(updateDeviceList(devices));
  127. resolve(devices);
  128. });
  129. } else {
  130. resolve([]);
  131. }
  132. });
  133. }
  134. /**
  135. * Signals that an error occurred while trying to obtain a track from a camera.
  136. *
  137. * @param {Object} error - The device error, as provided by lib-jitsi-meet.
  138. * @param {string} error.name - The constant for the type of the error.
  139. * @param {string} error.message - Optional additional information about the
  140. * error.
  141. * @returns {{
  142. * type: NOTIFY_CAMERA_ERROR,
  143. * error: Object
  144. * }}
  145. */
  146. export function notifyCameraError(error) {
  147. return {
  148. type: NOTIFY_CAMERA_ERROR,
  149. error
  150. };
  151. }
  152. /**
  153. * Signals that an error occurred while trying to obtain a track from a mic.
  154. *
  155. * @param {Object} error - The device error, as provided by lib-jitsi-meet.
  156. * @param {Object} error.name - The constant for the type of the error.
  157. * @param {string} error.message - Optional additional information about the
  158. * error.
  159. * @returns {{
  160. * type: NOTIFY_MIC_ERROR,
  161. * error: Object
  162. * }}
  163. */
  164. export function notifyMicError(error) {
  165. return {
  166. type: NOTIFY_MIC_ERROR,
  167. error
  168. };
  169. }
  170. /**
  171. * Remove all pending device requests.
  172. *
  173. * @returns {{
  174. * type: REMOVE_PENDING_DEVICE_REQUESTS
  175. * }}
  176. */
  177. export function removePendingDeviceRequests() {
  178. return {
  179. type: REMOVE_PENDING_DEVICE_REQUESTS
  180. };
  181. }
  182. /**
  183. * Signals to update the currently used audio input device.
  184. *
  185. * @param {string} deviceId - The id of the new audio input device.
  186. * @returns {{
  187. * type: SET_AUDIO_INPUT_DEVICE,
  188. * deviceId: string
  189. * }}
  190. */
  191. export function setAudioInputDevice(deviceId) {
  192. return {
  193. type: SET_AUDIO_INPUT_DEVICE,
  194. deviceId
  195. };
  196. }
  197. /**
  198. * Sets the audio input device id and updates the settings
  199. * so they are persisted across sessions.
  200. *
  201. * @param {string} deviceId - The id of the new audio input device.
  202. * @returns {Function}
  203. */
  204. export function setAudioInputDeviceAndUpdateSettings(deviceId) {
  205. return function(dispatch, getState) {
  206. const deviceLabel = getDeviceLabelById(getState(), deviceId, 'audioInput');
  207. dispatch(setAudioInputDevice(deviceId));
  208. dispatch(updateSettings({
  209. userSelectedMicDeviceId: deviceId,
  210. userSelectedMicDeviceLabel: deviceLabel
  211. }));
  212. };
  213. }
  214. /**
  215. * Updates the output device id.
  216. *
  217. * @param {string} deviceId - The id of the new output device.
  218. * @returns {Function}
  219. */
  220. export function setAudioOutputDevice(deviceId) {
  221. return function(dispatch, getState) {
  222. const deviceLabel = getDeviceLabelById(getState(), deviceId, 'audioOutput');
  223. return setAudioOutputDeviceId(deviceId, dispatch, true, deviceLabel);
  224. };
  225. }
  226. /**
  227. * Signals to update the currently used video input device.
  228. *
  229. * @param {string} deviceId - The id of the new video input device.
  230. * @returns {{
  231. * type: SET_VIDEO_INPUT_DEVICE,
  232. * deviceId: string
  233. * }}
  234. */
  235. export function setVideoInputDevice(deviceId) {
  236. return {
  237. type: SET_VIDEO_INPUT_DEVICE,
  238. deviceId
  239. };
  240. }
  241. /**
  242. * Sets the video input device id and updates the settings
  243. * so they are persisted across sessions.
  244. *
  245. * @param {string} deviceId - The id of the new video input device.
  246. * @returns {Function}
  247. */
  248. export function setVideoInputDeviceAndUpdateSettings(deviceId) {
  249. return function(dispatch, getState) {
  250. const deviceLabel = getDeviceLabelById(getState(), deviceId, 'videoInput');
  251. dispatch(setVideoInputDevice(deviceId));
  252. dispatch(updateSettings({
  253. userSelectedCameraDeviceId: deviceId,
  254. userSelectedCameraDeviceLabel: deviceLabel
  255. }));
  256. };
  257. }
  258. /**
  259. * Signals to update the list of known audio and video devices.
  260. *
  261. * @param {Array<MediaDeviceInfo>} devices - All known available audio input,
  262. * audio output, and video input devices.
  263. * @returns {{
  264. * type: UPDATE_DEVICE_LIST,
  265. * devices: Array<MediaDeviceInfo>
  266. * }}
  267. */
  268. export function updateDeviceList(devices) {
  269. return {
  270. type: UPDATE_DEVICE_LIST,
  271. devices
  272. };
  273. }
  274. /**
  275. * Signals to check new and old devices for newly added devices and notify.
  276. *
  277. * @param {Array<MediaDeviceInfo>} newDevices - Array of the new devices.
  278. * @param {Array<MediaDeviceInfo>} oldDevices - Array of the old devices.
  279. * @returns {{
  280. * type: CHECK_AND_NOTIFY_FOR_NEW_DEVICE,
  281. * newDevices: Array<MediaDeviceInfo>,
  282. * oldDevices: Array<MediaDeviceInfo>
  283. * }}
  284. */
  285. export function checkAndNotifyForNewDevice(newDevices, oldDevices) {
  286. return {
  287. type: CHECK_AND_NOTIFY_FOR_NEW_DEVICE,
  288. newDevices,
  289. oldDevices
  290. };
  291. }