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

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