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.

JitsiMediaDevices.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import EventEmitter from 'events';
  2. import * as JitsiMediaDevicesEvents from './JitsiMediaDevicesEvents';
  3. import * as MediaType from './service/RTC/MediaType';
  4. import RTC from './modules/RTC/RTC';
  5. import RTCEvents from './service/RTC/RTCEvents';
  6. import Statistics from './modules/statistics/statistics';
  7. const eventEmitter = new EventEmitter();
  8. RTC.addListener(
  9. RTCEvents.DEVICE_LIST_CHANGED,
  10. devices =>
  11. eventEmitter.emit(
  12. JitsiMediaDevicesEvents.DEVICE_LIST_CHANGED,
  13. devices));
  14. RTC.addListener(RTCEvents.DEVICE_LIST_AVAILABLE,
  15. devices => {
  16. // log output device
  17. logOutputDevice(
  18. JitsiMediaDevices.getAudioOutputDevice(),
  19. devices);
  20. });
  21. /**
  22. * Gathers data and sends it to statistics.
  23. * @param deviceID the device id to log
  24. * @param devices list of devices
  25. */
  26. function logOutputDevice(deviceID, devices) {
  27. const device
  28. = devices.find(
  29. d => d.kind === 'audiooutput' && d.deviceId === deviceID);
  30. if (device) {
  31. Statistics.sendActiveDeviceListEvent(
  32. RTC.getEventDataForActiveDevice(device));
  33. }
  34. }
  35. const JitsiMediaDevices = {
  36. /**
  37. * Executes callback with list of media devices connected.
  38. * @param {function} callback
  39. */
  40. enumerateDevices(callback) {
  41. RTC.enumerateDevices(callback);
  42. },
  43. /**
  44. * Checks if its possible to enumerate available cameras/micropones.
  45. * @returns {Promise<boolean>} a Promise which will be resolved only once
  46. * the WebRTC stack is ready, either with true if the device listing is
  47. * available available or with false otherwise.
  48. */
  49. isDeviceListAvailable() {
  50. return RTC.isDeviceListAvailable();
  51. },
  52. /**
  53. * Returns true if changing the input (camera / microphone) or output
  54. * (audio) device is supported and false if not.
  55. * @param {string} [deviceType] - type of device to change. Default is
  56. * undefined or 'input', 'output' - for audio output device change.
  57. * @returns {boolean} true if available, false otherwise.
  58. */
  59. isDeviceChangeAvailable(deviceType) {
  60. return RTC.isDeviceChangeAvailable(deviceType);
  61. },
  62. /**
  63. * Returns true if user granted permission to media devices.
  64. * @param {'audio'|'video'} [type] - type of devices to check,
  65. * undefined stands for both 'audio' and 'video' together
  66. * @returns {boolean}
  67. */
  68. isDevicePermissionGranted(type) {
  69. const permissions = RTC.getDeviceAvailability();
  70. switch(type) {
  71. case MediaType.VIDEO:
  72. return permissions.video === true;
  73. case MediaType.AUDIO:
  74. return permissions.audio === true;
  75. default:
  76. return permissions.video === true && permissions.audio === true;
  77. }
  78. },
  79. /**
  80. * Returns currently used audio output device id, 'default' stands
  81. * for default device
  82. * @returns {string}
  83. */
  84. getAudioOutputDevice() {
  85. return RTC.getAudioOutputDevice();
  86. },
  87. /**
  88. * Sets current audio output device.
  89. * @param {string} deviceId - id of 'audiooutput' device from
  90. * navigator.mediaDevices.enumerateDevices(), 'default' is for
  91. * default device
  92. * @returns {Promise} - resolves when audio output is changed, is rejected
  93. * otherwise
  94. */
  95. setAudioOutputDevice(deviceId) {
  96. const availableDevices = RTC.getCurrentlyAvailableMediaDevices();
  97. if (availableDevices && availableDevices.length > 0) {
  98. // if we have devices info report device to stats
  99. // normally this will not happen on startup as this method is called
  100. // too early. This will happen only on user selection of new device
  101. logOutputDevice(deviceId, RTC.getCurrentlyAvailableMediaDevices());
  102. }
  103. return RTC.setAudioOutputDevice(deviceId);
  104. },
  105. /**
  106. * Adds an event handler.
  107. * @param {string} event - event name
  108. * @param {function} handler - event handler
  109. */
  110. addEventListener(event, handler) {
  111. eventEmitter.addListener(event, handler);
  112. },
  113. /**
  114. * Removes event handler.
  115. * @param {string} event - event name
  116. * @param {function} handler - event handler
  117. */
  118. removeEventListener(event, handler) {
  119. eventEmitter.removeListener(event, handler);
  120. },
  121. /**
  122. * Emits an event.
  123. * @param {string} event - event name
  124. */
  125. emitEvent(event, ...args) {
  126. eventEmitter.emit(event, ...args);
  127. }
  128. };
  129. module.exports = JitsiMediaDevices;