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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. Statistics.sendАctiveDeviceListEvent(
  29. RTC.getEventDataForActiveDevice(device));
  30. }
  31. var JitsiMediaDevices = {
  32. /**
  33. * Executes callback with list of media devices connected.
  34. * @param {function} callback
  35. */
  36. enumerateDevices: function (callback) {
  37. RTC.enumerateDevices(callback);
  38. },
  39. /**
  40. * Checks if its possible to enumerate available cameras/micropones.
  41. * @returns {boolean} true if available, false otherwise.
  42. */
  43. isDeviceListAvailable: function () {
  44. return RTC.isDeviceListAvailable();
  45. },
  46. /**
  47. * Returns true if changing the input (camera / microphone) or output
  48. * (audio) device is supported and false if not.
  49. * @param {string} [deviceType] - type of device to change. Default is
  50. * undefined or 'input', 'output' - for audio output device change.
  51. * @returns {boolean} true if available, false otherwise.
  52. */
  53. isDeviceChangeAvailable: function (deviceType) {
  54. return RTC.isDeviceChangeAvailable(deviceType);
  55. },
  56. /**
  57. * Returns true if user granted permission to media devices.
  58. * @param {'audio'|'video'} [type] - type of devices to check,
  59. * undefined stands for both 'audio' and 'video' together
  60. * @returns {boolean}
  61. */
  62. isDevicePermissionGranted: function (type) {
  63. var permissions = RTC.getDeviceAvailability();
  64. switch(type) {
  65. case MediaType.VIDEO:
  66. return permissions.video === true;
  67. case MediaType.AUDIO:
  68. return permissions.audio === true;
  69. default:
  70. return permissions.video === true && permissions.audio === true;
  71. }
  72. },
  73. /**
  74. * Returns currently used audio output device id, 'default' stands
  75. * for default device
  76. * @returns {string}
  77. */
  78. getAudioOutputDevice: function () {
  79. return RTC.getAudioOutputDevice();
  80. },
  81. /**
  82. * Sets current audio output device.
  83. * @param {string} deviceId - id of 'audiooutput' device from
  84. * navigator.mediaDevices.enumerateDevices(), 'default' is for
  85. * default device
  86. * @returns {Promise} - resolves when audio output is changed, is rejected
  87. * otherwise
  88. */
  89. setAudioOutputDevice: function (deviceId) {
  90. if (RTC.getCurrentlyAvailableMediaDevices().length > 0)
  91. {
  92. // if we have devices info report device to stats
  93. // normally this will not happen on startup as this method is called
  94. // too early. This will happen only on user selection of new device
  95. logOutputDevice(deviceId, RTC.getCurrentlyAvailableMediaDevices());
  96. }
  97. return RTC.setAudioOutputDevice(deviceId);
  98. },
  99. /**
  100. * Adds an event handler.
  101. * @param {string} event - event name
  102. * @param {function} handler - event handler
  103. */
  104. addEventListener: function (event, handler) {
  105. eventEmitter.addListener(event, handler);
  106. },
  107. /**
  108. * Removes event handler.
  109. * @param {string} event - event name
  110. * @param {function} handler - event handler
  111. */
  112. removeEventListener: function (event, handler) {
  113. eventEmitter.removeListener(event, handler);
  114. }
  115. };
  116. module.exports = JitsiMediaDevices;