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

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