選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

JitsiMediaDevices.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 {boolean} true if available, false otherwise.
  44. */
  45. isDeviceListAvailable: function () {
  46. return RTC.isDeviceListAvailable();
  47. },
  48. /**
  49. * Returns true if changing the input (camera / microphone) or output
  50. * (audio) device is supported and false if not.
  51. * @param {string} [deviceType] - type of device to change. Default is
  52. * undefined or 'input', 'output' - for audio output device change.
  53. * @returns {boolean} true if available, false otherwise.
  54. */
  55. isDeviceChangeAvailable: function (deviceType) {
  56. return RTC.isDeviceChangeAvailable(deviceType);
  57. },
  58. /**
  59. * Returns true if user granted permission to media devices.
  60. * @param {'audio'|'video'} [type] - type of devices to check,
  61. * undefined stands for both 'audio' and 'video' together
  62. * @returns {boolean}
  63. */
  64. isDevicePermissionGranted: function (type) {
  65. var permissions = RTC.getDeviceAvailability();
  66. switch(type) {
  67. case MediaType.VIDEO:
  68. return permissions.video === true;
  69. case MediaType.AUDIO:
  70. return permissions.audio === true;
  71. default:
  72. return permissions.video === true && permissions.audio === true;
  73. }
  74. },
  75. /**
  76. * Returns currently used audio output device id, 'default' stands
  77. * for default device
  78. * @returns {string}
  79. */
  80. getAudioOutputDevice: function () {
  81. return RTC.getAudioOutputDevice();
  82. },
  83. /**
  84. * Sets current audio output device.
  85. * @param {string} deviceId - id of 'audiooutput' device from
  86. * navigator.mediaDevices.enumerateDevices(), 'default' is for
  87. * default device
  88. * @returns {Promise} - resolves when audio output is changed, is rejected
  89. * otherwise
  90. */
  91. setAudioOutputDevice: function (deviceId) {
  92. var availableDevices = RTC.getCurrentlyAvailableMediaDevices();
  93. if (availableDevices && availableDevices.length > 0)
  94. {
  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: function (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: function (event, handler) {
  116. eventEmitter.removeListener(event, handler);
  117. },
  118. /**
  119. * Emits an event.
  120. * @param {string} event - event name
  121. */
  122. emitEvent: function (event) {
  123. eventEmitter.emit.apply(eventEmitter, arguments);
  124. }
  125. };
  126. module.exports = JitsiMediaDevices;