modified lib-jitsi-meet dev repo
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JitsiTrack.js 13KB

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