您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JitsiTrack.js 12KB

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