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

JitsiMediaDevices.js 5.2KB

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