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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. * Returns the ID of the underlying WebRTC MediaStreamTrack(if any)
  207. * @returns {String|null}
  208. */
  209. getTrackId() {
  210. return this.track ? this.track.id : null;
  211. }
  212. /**
  213. * Return meaningful usage label for this track depending on it's media and
  214. * eventual video type.
  215. * @returns {string}
  216. */
  217. getUsageLabel() {
  218. if (this.isAudioTrack()) {
  219. return 'mic';
  220. }
  221. return this.videoType ? this.videoType : 'default';
  222. }
  223. /**
  224. * Eventually will trigger RTCEvents.TRACK_ATTACHED event.
  225. * @param container the video/audio container to which this stream is
  226. * attached and for which event will be fired.
  227. * @private
  228. */
  229. _maybeFireTrackAttached(container) {
  230. if (this.conference && container) {
  231. this.conference._onTrackAttach(this, container);
  232. }
  233. }
  234. /**
  235. * Attaches the MediaStream of this track to an HTML container.
  236. * Adds the container to the list of containers that are displaying the
  237. * track.
  238. *
  239. * @param container the HTML container which can be 'video' or 'audio'
  240. * element.
  241. *
  242. * @returns {void}
  243. */
  244. attach(container) {
  245. if (this.stream) {
  246. this._onTrackAttach(container);
  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. this._onTrackDetach(c);
  266. RTCUtils.attachMediaStream(c, null);
  267. }
  268. if (!container || c === container) {
  269. cs.splice(i, 1);
  270. }
  271. }
  272. if (container) {
  273. this._onTrackDetach(container);
  274. RTCUtils.attachMediaStream(container, null);
  275. }
  276. }
  277. /**
  278. * Called when the track has been attached to a new container.
  279. *
  280. * @param {HTMLElement} container the HTML container which can be 'video' or
  281. * 'audio' element.
  282. * @private
  283. */
  284. _onTrackAttach(container) { // eslint-disable-line no-unused-vars
  285. // Should be defined by the classes that are extending JitsiTrack
  286. }
  287. /**
  288. * Called when the track has been detached from a container.
  289. *
  290. * @param {HTMLElement} container the HTML container which can be 'video' or
  291. * 'audio' element.
  292. * @private
  293. */
  294. _onTrackDetach(container) { // eslint-disable-line no-unused-vars
  295. // Should be defined by the classes that are extending JitsiTrack
  296. }
  297. /**
  298. * Attach time to first media tracker only if there is conference and only
  299. * for the first element.
  300. *
  301. * @param {HTMLElement} container the HTML container which can be 'video' or
  302. * 'audio' element.
  303. * @private
  304. */
  305. _attachTTFMTracker(container) { // eslint-disable-line no-unused-vars
  306. // Should be defined by the classes that are extending JitsiTrack
  307. }
  308. /**
  309. * Removes attached event listeners.
  310. *
  311. * @returns {Promise}
  312. */
  313. dispose() {
  314. this.removeAllListeners();
  315. this.disposed = true;
  316. return Promise.resolve();
  317. }
  318. /**
  319. * Returns true if this is a video track and the source of the video is a
  320. * screen capture as opposed to a camera.
  321. */
  322. isScreenSharing() {
  323. // FIXME: Should be fixed or removed.
  324. }
  325. /**
  326. * Returns id of the track.
  327. * @returns {string|null} id of the track or null if this is fake track.
  328. */
  329. getId() {
  330. if (this.stream) {
  331. return RTCUtils.getStreamID(this.stream);
  332. }
  333. return null;
  334. }
  335. /**
  336. * Checks whether the MediaStream is active/not ended.
  337. * When there is no check for active we don't have information and so
  338. * will return that stream is active (in case of FF).
  339. * @returns {boolean} whether MediaStream is active.
  340. */
  341. isActive() {
  342. if (typeof this.stream.active !== 'undefined') {
  343. return this.stream.active;
  344. }
  345. return true;
  346. }
  347. /**
  348. * Sets the audio level for the stream
  349. * @param {number} audioLevel value between 0 and 1
  350. * @param {TraceablePeerConnection} [tpc] the peerconnection instance which
  351. * is source for the audio level. It can be <tt>undefined</tt> for
  352. * a local track if the audio level was measured outside of the
  353. * peerconnection (see /modules/statistics/LocalStatsCollector.js).
  354. */
  355. setAudioLevel(audioLevel, tpc) {
  356. if (this.audioLevel !== audioLevel) {
  357. this.audioLevel = audioLevel;
  358. this.emit(
  359. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  360. audioLevel,
  361. tpc);
  362. }
  363. }
  364. /**
  365. * Returns the msid of the stream attached to the JitsiTrack object or null
  366. * if no stream is attached.
  367. */
  368. getMSID() {
  369. const streamId = this.getStreamId();
  370. const trackId = this.getTrackId();
  371. return streamId && trackId ? `${streamId} ${trackId}` : null;
  372. }
  373. /**
  374. * Sets new audio output device for track's DOM elements. Video tracks are
  375. * ignored.
  376. * @param {string} audioOutputDeviceId - id of 'audiooutput' device from
  377. * navigator.mediaDevices.enumerateDevices(), '' for default device
  378. * @emits JitsiTrackEvents.TRACK_AUDIO_OUTPUT_CHANGED
  379. * @returns {Promise}
  380. */
  381. setAudioOutput(audioOutputDeviceId) {
  382. if (!RTCUtils.isDeviceChangeAvailable('output')) {
  383. return Promise.reject(
  384. new Error('Audio output device change is not supported'));
  385. }
  386. // All audio communication is done through audio tracks, so ignore
  387. // changing audio output for video tracks at all.
  388. if (this.isVideoTrack()) {
  389. return Promise.resolve();
  390. }
  391. return (
  392. Promise.all(
  393. this.containers.map(
  394. element =>
  395. element.setSinkId(audioOutputDeviceId)
  396. .catch(error => {
  397. logger.warn(
  398. 'Failed to change audio output device on'
  399. + ' element. Default or previously set'
  400. + ' audio output device will be used.',
  401. element,
  402. error);
  403. throw error;
  404. }))
  405. )
  406. .then(() => {
  407. this.emit(
  408. JitsiTrackEvents.TRACK_AUDIO_OUTPUT_CHANGED,
  409. audioOutputDeviceId);
  410. }));
  411. }
  412. }