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.

JitsiTrack.js 14KB

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