Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

JitsiTrack.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* global __filename, module */
  2. import EventEmitter from 'events';
  3. import { getLogger } from 'jitsi-meet-logger';
  4. import * as JitsiTrackEvents from '../../JitsiTrackEvents';
  5. import * as MediaType from '../../service/RTC/MediaType';
  6. import RTCBrowserType from './RTCBrowserType';
  7. import RTCUtils from './RTCUtils';
  8. const logger = getLogger(__filename);
  9. /**
  10. * Maps our handler types to MediaStreamTrack properties.
  11. */
  12. const trackHandler2Prop = {
  13. 'track_mute': 'onmute', // Not supported on FF
  14. 'track_unmute': 'onunmute',
  15. 'track_ended': 'onended'
  16. };
  17. /**
  18. * This implements 'onended' callback normally fired by WebRTC after the stream
  19. * is stopped. There is no such behaviour yet in FF, so we have to add it.
  20. * @param jitsiTrack our track object holding the original WebRTC stream object
  21. * to which 'onended' handling will be added.
  22. */
  23. function implementOnEndedHandling(jitsiTrack) {
  24. const stream = jitsiTrack.getOriginalStream();
  25. if (!stream) {
  26. return;
  27. }
  28. const originalStop = stream.stop;
  29. stream.stop = function() {
  30. originalStop.apply(stream);
  31. if (jitsiTrack.isActive()) {
  32. stream.onended();
  33. }
  34. };
  35. }
  36. /**
  37. * Adds onended/oninactive handler to a MediaStream.
  38. * @param mediaStream a MediaStream to attach onended/oninactive handler
  39. * @param handler the handler
  40. */
  41. function addMediaStreamInactiveHandler(mediaStream, handler) {
  42. // Temasys will use onended
  43. if (typeof mediaStream.active === 'undefined') {
  44. mediaStream.onended = handler;
  45. } else {
  46. mediaStream.oninactive = handler;
  47. }
  48. }
  49. /* eslint-disable max-params */
  50. /**
  51. * Represents a single media track (either audio or video).
  52. * @constructor
  53. * @param rtc the rtc instance
  54. * @param stream the WebRTC MediaStream instance
  55. * @param track the WebRTC MediaStreamTrack instance, must be part of
  56. * the given <tt>stream</tt>.
  57. * @param streamInactiveHandler the function that will handle
  58. * onended/oninactive events of the stream.
  59. * @param trackMediaType the media type of the JitsiTrack
  60. * @param videoType the VideoType for this track if any
  61. * @param ssrc the SSRC of this track if known
  62. */
  63. function JitsiTrack(
  64. conference,
  65. stream,
  66. track,
  67. streamInactiveHandler,
  68. trackMediaType,
  69. videoType,
  70. ssrc) {
  71. /**
  72. * Array with the HTML elements that are displaying the streams.
  73. * @type {Array}
  74. */
  75. this.containers = [];
  76. this.conference = conference;
  77. this.stream = stream;
  78. this.ssrc = ssrc;
  79. this.eventEmitter = new EventEmitter();
  80. this.audioLevel = -1;
  81. this.type = trackMediaType;
  82. this.track = track;
  83. this.videoType = videoType;
  84. this.handlers = {};
  85. /**
  86. * Indicates whether this JitsiTrack has been disposed. If true, this
  87. * JitsiTrack is to be considered unusable and operations involving it are
  88. * to fail (e.g. {@link JitsiConference#addTrack(JitsiTrack)},
  89. * {@link JitsiConference#removeTrack(JitsiTrack)}).
  90. * @type {boolean}
  91. */
  92. this.disposed = false;
  93. this._setHandler('inactive', streamInactiveHandler);
  94. }
  95. /* eslint-enable max-params */
  96. /**
  97. * Sets handler to the WebRTC MediaStream or MediaStreamTrack object depending
  98. * on the passed type.
  99. * @param {string} type the type of the handler that is going to be set
  100. * @param {Function} handler the handler.
  101. */
  102. JitsiTrack.prototype._setHandler = function(type, handler) {
  103. this.handlers[type] = handler;
  104. if (!this.stream) {
  105. return;
  106. }
  107. if (type === 'inactive') {
  108. if (RTCBrowserType.isFirefox()) {
  109. implementOnEndedHandling(this);
  110. }
  111. addMediaStreamInactiveHandler(this.stream, handler);
  112. } else if (trackHandler2Prop.hasOwnProperty(type)) {
  113. this.stream.getVideoTracks().forEach(track => {
  114. track[trackHandler2Prop[type]] = handler;
  115. }, this);
  116. }
  117. };
  118. /**
  119. * Sets the stream property of JitsiTrack object and sets all stored handlers
  120. * to it.
  121. * @param {MediaStream} stream the new stream.
  122. */
  123. JitsiTrack.prototype._setStream = function(stream) {
  124. this.stream = stream;
  125. Object.keys(this.handlers).forEach(function(type) {
  126. typeof this.handlers[type] === 'function'
  127. && this._setHandler(type, this.handlers[type]);
  128. }, this);
  129. };
  130. /**
  131. * Returns the type (audio or video) of this track.
  132. */
  133. JitsiTrack.prototype.getType = function() {
  134. return this.type;
  135. };
  136. /**
  137. * Check if this is an audio track.
  138. */
  139. JitsiTrack.prototype.isAudioTrack = function() {
  140. return this.getType() === MediaType.AUDIO;
  141. };
  142. /**
  143. * Checks whether the underlying WebRTC <tt>MediaStreamTrack</tt> is muted
  144. * according to it's 'muted' field status.
  145. * @return {boolean} <tt>true</tt> if the underlying <tt>MediaStreamTrack</tt>
  146. * is muted or <tt>false</tt> otherwise.
  147. */
  148. JitsiTrack.prototype.isWebRTCTrackMuted = function() {
  149. return this.track && this.track.muted;
  150. };
  151. /**
  152. * Check if this is a video track.
  153. */
  154. JitsiTrack.prototype.isVideoTrack = function() {
  155. return this.getType() === MediaType.VIDEO;
  156. };
  157. /**
  158. * Checks whether this is a local track.
  159. * @abstract
  160. * @return {boolean} 'true' if it's a local track or 'false' otherwise.
  161. */
  162. JitsiTrack.prototype.isLocal = function() {
  163. throw new Error('Not implemented by subclass');
  164. };
  165. /**
  166. * Returns the WebRTC MediaStream instance.
  167. */
  168. JitsiTrack.prototype.getOriginalStream = function() {
  169. return this.stream;
  170. };
  171. /**
  172. * Returns the ID of the underlying WebRTC Media Stream(if any)
  173. * @returns {String|null}
  174. */
  175. JitsiTrack.prototype.getStreamId = function() {
  176. return this.stream ? this.stream.id : null;
  177. };
  178. /**
  179. * Return the underlying WebRTC MediaStreamTrack
  180. * @returns {MediaStreamTrack}
  181. */
  182. JitsiTrack.prototype.getTrack = function() {
  183. return this.track;
  184. };
  185. /**
  186. * Returns the ID of the underlying WebRTC MediaStreamTrack(if any)
  187. * @returns {String|null}
  188. */
  189. JitsiTrack.prototype.getTrackId = function() {
  190. return this.track ? this.track.id : null;
  191. };
  192. /**
  193. * Return meaningful usage label for this track depending on it's media and
  194. * eventual video type.
  195. * @returns {string}
  196. */
  197. JitsiTrack.prototype.getUsageLabel = function() {
  198. if (this.isAudioTrack()) {
  199. return 'mic';
  200. }
  201. return this.videoType ? this.videoType : 'default';
  202. };
  203. /**
  204. * Eventually will trigger RTCEvents.TRACK_ATTACHED event.
  205. * @param container the video/audio container to which this stream is attached
  206. * and for which event will be fired.
  207. * @private
  208. */
  209. JitsiTrack.prototype._maybeFireTrackAttached = function(container) {
  210. if (this.conference && container) {
  211. this.conference._onTrackAttach(this, container);
  212. }
  213. };
  214. /**
  215. * Attaches the MediaStream of this track to an HTML container.
  216. * Adds the container to the list of containers that are displaying the track.
  217. * Note that Temasys plugin will replace original audio/video element with
  218. * 'object' when stream is being attached to the container for the first time.
  219. *
  220. * * NOTE * if given container element is not visible when the stream is being
  221. * attached it will be shown back given that Temasys plugin is currently in use.
  222. *
  223. * @param container the HTML container which can be 'video' or 'audio' element.
  224. * It can also be 'object' element if Temasys plugin is in use and this
  225. * method has been called previously on video or audio HTML element.
  226. *
  227. * @returns potentially new instance of container if it was replaced by the
  228. * library. That's the case when Temasys plugin is in use.
  229. */
  230. JitsiTrack.prototype.attach = function(container) {
  231. let c;
  232. if (this.stream) {
  233. c = RTCUtils.attachMediaStream(container, this.stream);
  234. }
  235. this.containers.push(c);
  236. this._maybeFireTrackAttached(c);
  237. this._attachTTFMTracker(c);
  238. return c;
  239. };
  240. /**
  241. * Removes this JitsiTrack from the passed HTML container.
  242. *
  243. * @param container the HTML container to detach from this JitsiTrack. If
  244. * <tt>null</tt> or <tt>undefined</tt>, all containers are removed. A container
  245. * can be a 'video', 'audio' or 'object' HTML element instance to which this
  246. * JitsiTrack is currently attached.
  247. */
  248. JitsiTrack.prototype.detach = function(container) {
  249. for (let cs = this.containers, i = cs.length - 1; i >= 0; --i) {
  250. const c = cs[i];
  251. if (!container) {
  252. RTCUtils.attachMediaStream(c, null);
  253. }
  254. if (!container || c === container) {
  255. cs.splice(i, 1);
  256. }
  257. }
  258. if (container) {
  259. RTCUtils.attachMediaStream(container, null);
  260. }
  261. };
  262. /**
  263. * Attach time to first media tracker only if there is conference and only
  264. * for the first element.
  265. * @param container the HTML container which can be 'video' or 'audio' element.
  266. * It can also be 'object' element if Temasys plugin is in use and this
  267. * method has been called previously on video or audio HTML element.
  268. * @private
  269. */
  270. // eslint-disable-next-line no-unused-vars
  271. JitsiTrack.prototype._attachTTFMTracker = function(container) {
  272. // Should be defined by the classes that are extending JitsiTrack
  273. };
  274. /**
  275. * Removes attached event listeners.
  276. *
  277. * @returns {Promise}
  278. */
  279. JitsiTrack.prototype.dispose = function() {
  280. this.eventEmitter.removeAllListeners();
  281. this.disposed = true;
  282. return Promise.resolve();
  283. };
  284. /**
  285. * Returns true if this is a video track and the source of the video is a
  286. * screen capture as opposed to a camera.
  287. */
  288. JitsiTrack.prototype.isScreenSharing = function() {
  289. // FIXME: Should be fixed or removed.
  290. };
  291. /**
  292. * Returns id of the track.
  293. * @returns {string|null} id of the track or null if this is fake track.
  294. */
  295. JitsiTrack.prototype.getId = function() {
  296. if (this.stream) {
  297. return RTCUtils.getStreamID(this.stream);
  298. }
  299. return null;
  300. };
  301. /**
  302. * Checks whether the MediaStream is active/not ended.
  303. * When there is no check for active we don't have information and so
  304. * will return that stream is active (in case of FF).
  305. * @returns {boolean} whether MediaStream is active.
  306. */
  307. JitsiTrack.prototype.isActive = function() {
  308. if (typeof this.stream.active !== 'undefined') {
  309. return this.stream.active;
  310. }
  311. return true;
  312. };
  313. /**
  314. * Attaches a handler for events(For example - "audio level changed".).
  315. * All possible event are defined in JitsiTrackEvents.
  316. * @param eventId the event ID.
  317. * @param handler handler for the event.
  318. */
  319. JitsiTrack.prototype.on = function(eventId, handler) {
  320. if (this.eventEmitter) {
  321. this.eventEmitter.on(eventId, handler);
  322. }
  323. };
  324. /**
  325. * Removes event listener
  326. * @param eventId the event ID.
  327. * @param [handler] optional, the specific handler to unbind
  328. */
  329. JitsiTrack.prototype.off = function(eventId, handler) {
  330. if (this.eventEmitter) {
  331. this.eventEmitter.removeListener(eventId, handler);
  332. }
  333. };
  334. // Common aliases for event emitter
  335. JitsiTrack.prototype.addEventListener = JitsiTrack.prototype.on;
  336. JitsiTrack.prototype.removeEventListener = JitsiTrack.prototype.off;
  337. /**
  338. * Sets the audio level for the stream
  339. * @param audioLevel the new audio level
  340. */
  341. JitsiTrack.prototype.setAudioLevel = function(audioLevel) {
  342. if (this.audioLevel !== audioLevel) {
  343. this.eventEmitter.emit(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  344. audioLevel);
  345. this.audioLevel = audioLevel;
  346. }
  347. };
  348. /**
  349. * Returns the msid of the stream attached to the JitsiTrack object or null if
  350. * no stream is attached.
  351. */
  352. JitsiTrack.prototype.getMSID = function() {
  353. const streamId = this.getStreamId();
  354. const trackId = this.getTrackId();
  355. return streamId && trackId ? `${streamId} ${trackId}` : null;
  356. };
  357. /**
  358. * Sets new audio output device for track's DOM elements. Video tracks are
  359. * ignored.
  360. * @param {string} audioOutputDeviceId - id of 'audiooutput' device from
  361. * navigator.mediaDevices.enumerateDevices(), '' for default device
  362. * @emits JitsiTrackEvents.TRACK_AUDIO_OUTPUT_CHANGED
  363. * @returns {Promise}
  364. */
  365. JitsiTrack.prototype.setAudioOutput = function(audioOutputDeviceId) {
  366. const self = this;
  367. if (!RTCUtils.isDeviceChangeAvailable('output')) {
  368. return Promise.reject(
  369. new Error('Audio output device change is not supported'));
  370. }
  371. // All audio communication is done through audio tracks, so ignore changing
  372. // audio output for video tracks at all.
  373. if (this.isVideoTrack()) {
  374. return Promise.resolve();
  375. }
  376. return (
  377. Promise.all(
  378. this.containers.map(
  379. element =>
  380. element.setSinkId(audioOutputDeviceId)
  381. .catch(error => {
  382. logger.warn(
  383. 'Failed to change audio output device on'
  384. + ' element. Default or previously set'
  385. + ' audio output device will be used.',
  386. element,
  387. error);
  388. throw error;
  389. })))
  390. .then(() => {
  391. self.eventEmitter.emit(
  392. JitsiTrackEvents.TRACK_AUDIO_OUTPUT_CHANGED,
  393. audioOutputDeviceId);
  394. }));
  395. };
  396. module.exports = JitsiTrack;