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

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