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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. // FIXME Why only video tracks?
  100. for (const track of this.stream.getVideoTracks()) {
  101. track[trackHandler2Prop[type]] = handler;
  102. }
  103. }
  104. }
  105. /**
  106. * Unregisters all event handlers bound to the underlying media stream/track
  107. * @private
  108. */
  109. _unregisterHandlers() {
  110. if (!this.stream) {
  111. logger.warn(
  112. `${this}: unable to unregister handlers - no stream object`);
  113. return;
  114. }
  115. for (const type of this.handlers.keys()) {
  116. // FIXME Why only video tracks?
  117. for (const videoTrack of this.stream.getVideoTracks()) {
  118. videoTrack[trackHandler2Prop[type]] = undefined;
  119. }
  120. }
  121. if (this._streamInactiveHandler) {
  122. addMediaStreamInactiveHandler(this.stream, undefined);
  123. }
  124. }
  125. /**
  126. * Sets the stream property of JitsiTrack object and sets all stored
  127. * handlers to it.
  128. *
  129. * @param {MediaStream} stream the new stream.
  130. * @protected
  131. */
  132. _setStream(stream) {
  133. if (this.stream === stream) {
  134. return;
  135. }
  136. this.stream = stream;
  137. // TODO Practically, that's like the opposite of _unregisterHandlers
  138. // i.e. may be abstracted into a function/method called
  139. // _registerHandlers for clarity and easing the maintenance of the two
  140. // pieces of source code.
  141. if (this.stream) {
  142. for (const type of this.handlers.keys()) {
  143. this._setHandler(type, this.handlers.get(type));
  144. }
  145. if (this._streamInactiveHandler) {
  146. addMediaStreamInactiveHandler(
  147. this.stream, this._streamInactiveHandler);
  148. }
  149. }
  150. }
  151. /**
  152. * Returns the type (audio or video) of this track.
  153. */
  154. getType() {
  155. return this.type;
  156. }
  157. /**
  158. * Check if this is an audio track.
  159. */
  160. isAudioTrack() {
  161. return this.getType() === MediaType.AUDIO;
  162. }
  163. /**
  164. * Checks whether the underlying WebRTC <tt>MediaStreamTrack</tt> is muted
  165. * according to it's 'muted' field status.
  166. * @return {boolean} <tt>true</tt> if the underlying
  167. * <tt>MediaStreamTrack</tt> is muted or <tt>false</tt> otherwise.
  168. */
  169. isWebRTCTrackMuted() {
  170. return this.track && this.track.muted;
  171. }
  172. /**
  173. * Check if this is a video track.
  174. */
  175. isVideoTrack() {
  176. return this.getType() === MediaType.VIDEO;
  177. }
  178. /**
  179. * Checks whether this is a local track.
  180. * @abstract
  181. * @return {boolean} 'true' if it's a local track or 'false' otherwise.
  182. */
  183. isLocal() {
  184. throw new Error('Not implemented by subclass');
  185. }
  186. /**
  187. * Returns the WebRTC MediaStream instance.
  188. */
  189. getOriginalStream() {
  190. return this.stream;
  191. }
  192. /**
  193. * Returns the ID of the underlying WebRTC Media Stream(if any)
  194. * @returns {String|null}
  195. */
  196. getStreamId() {
  197. return this.stream ? this.stream.id : null;
  198. }
  199. /**
  200. * Return the underlying WebRTC MediaStreamTrack
  201. * @returns {MediaStreamTrack}
  202. */
  203. getTrack() {
  204. return this.track;
  205. }
  206. /**
  207. * Returns the ID of the underlying WebRTC MediaStreamTrack(if any)
  208. * @returns {String|null}
  209. */
  210. getTrackId() {
  211. return this.track ? this.track.id : null;
  212. }
  213. /**
  214. * Return meaningful usage label for this track depending on it's media and
  215. * eventual video type.
  216. * @returns {string}
  217. */
  218. getUsageLabel() {
  219. if (this.isAudioTrack()) {
  220. return 'mic';
  221. }
  222. return this.videoType ? this.videoType : 'default';
  223. }
  224. /**
  225. * Eventually will trigger RTCEvents.TRACK_ATTACHED event.
  226. * @param container the video/audio container to which this stream is
  227. * attached and for which event will be fired.
  228. * @private
  229. */
  230. _maybeFireTrackAttached(container) {
  231. if (this.conference && container) {
  232. this.conference._onTrackAttach(this, container);
  233. }
  234. }
  235. /**
  236. * Attaches the MediaStream of this track to an HTML container.
  237. * Adds the container to the list of containers that are displaying the
  238. * track.
  239. *
  240. * @param container the HTML container which can be 'video' or 'audio'
  241. * element.
  242. *
  243. * @returns {void}
  244. */
  245. attach(container) {
  246. if (this.stream) {
  247. RTCUtils.attachMediaStream(container, this.stream);
  248. }
  249. this.containers.push(container);
  250. this._maybeFireTrackAttached(container);
  251. this._attachTTFMTracker(container);
  252. }
  253. /**
  254. * Removes this JitsiTrack from the passed HTML container.
  255. *
  256. * @param container the HTML container to detach from this JitsiTrack. If
  257. * <tt>null</tt> or <tt>undefined</tt>, all containers are removed. A
  258. * container can be a 'video', 'audio' or 'object' HTML element instance to
  259. * which this JitsiTrack is currently attached.
  260. */
  261. detach(container) {
  262. for (let cs = this.containers, i = cs.length - 1; i >= 0; --i) {
  263. const c = cs[i];
  264. if (!container) {
  265. RTCUtils.attachMediaStream(c, null);
  266. }
  267. if (!container || c === container) {
  268. cs.splice(i, 1);
  269. }
  270. }
  271. if (container) {
  272. RTCUtils.attachMediaStream(container, null);
  273. }
  274. }
  275. /**
  276. * Attach time to first media tracker only if there is conference and only
  277. * for the first element.
  278. *
  279. * @param {HTMLElement} container the HTML container which can be 'video' or
  280. * 'audio' element.
  281. * @private
  282. */
  283. _attachTTFMTracker(container) { // eslint-disable-line no-unused-vars
  284. // Should be defined by the classes that are extending JitsiTrack
  285. }
  286. /**
  287. * Removes attached event listeners.
  288. *
  289. * @returns {Promise}
  290. */
  291. dispose() {
  292. this.removeAllListeners();
  293. this.disposed = true;
  294. return Promise.resolve();
  295. }
  296. /**
  297. * Returns true if this is a video track and the source of the video is a
  298. * screen capture as opposed to a camera.
  299. */
  300. isScreenSharing() {
  301. // FIXME: Should be fixed or removed.
  302. }
  303. /**
  304. * Returns id of the track.
  305. * @returns {string|null} id of the track or null if this is fake track.
  306. */
  307. getId() {
  308. if (this.stream) {
  309. return RTCUtils.getStreamID(this.stream);
  310. }
  311. return null;
  312. }
  313. /**
  314. * Checks whether the MediaStream is active/not ended.
  315. * When there is no check for active we don't have information and so
  316. * will return that stream is active (in case of FF).
  317. * @returns {boolean} whether MediaStream is active.
  318. */
  319. isActive() {
  320. if (typeof this.stream.active !== 'undefined') {
  321. return this.stream.active;
  322. }
  323. return true;
  324. }
  325. /**
  326. * Sets the audio level for the stream
  327. * @param {number} audioLevel value between 0 and 1
  328. * @param {TraceablePeerConnection} [tpc] the peerconnection instance which
  329. * is source for the audio level. It can be <tt>undefined</tt> for
  330. * a local track if the audio level was measured outside of the
  331. * peerconnection (see /modules/statistics/LocalStatsCollector.js).
  332. */
  333. setAudioLevel(audioLevel, tpc) {
  334. if (this.audioLevel !== audioLevel) {
  335. this.audioLevel = audioLevel;
  336. this.emit(
  337. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  338. audioLevel,
  339. tpc);
  340. }
  341. }
  342. /**
  343. * Returns the msid of the stream attached to the JitsiTrack object or null
  344. * if no stream is attached.
  345. */
  346. getMSID() {
  347. const streamId = this.getStreamId();
  348. const trackId = this.getTrackId();
  349. return streamId && trackId ? `${streamId} ${trackId}` : null;
  350. }
  351. /**
  352. * Sets new audio output device for track's DOM elements. Video tracks are
  353. * ignored.
  354. * @param {string} audioOutputDeviceId - id of 'audiooutput' device from
  355. * navigator.mediaDevices.enumerateDevices(), '' for default device
  356. * @emits JitsiTrackEvents.TRACK_AUDIO_OUTPUT_CHANGED
  357. * @returns {Promise}
  358. */
  359. setAudioOutput(audioOutputDeviceId) {
  360. if (!RTCUtils.isDeviceChangeAvailable('output')) {
  361. return Promise.reject(
  362. new Error('Audio output device change is not supported'));
  363. }
  364. // All audio communication is done through audio tracks, so ignore
  365. // changing audio output for video tracks at all.
  366. if (this.isVideoTrack()) {
  367. return Promise.resolve();
  368. }
  369. return (
  370. Promise.all(
  371. this.containers.map(
  372. element =>
  373. element.setSinkId(audioOutputDeviceId)
  374. .catch(error => {
  375. logger.warn(
  376. 'Failed to change audio output device on'
  377. + ' element. Default or previously set'
  378. + ' audio output device will be used.',
  379. element,
  380. error);
  381. throw error;
  382. }))
  383. )
  384. .then(() => {
  385. this.emit(
  386. JitsiTrackEvents.TRACK_AUDIO_OUTPUT_CHANGED,
  387. audioOutputDeviceId);
  388. }));
  389. }
  390. }