您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JitsiMediaDevices.js 4.5KB

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