Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

JitsiTrack.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.disposed = false;
  69. if(stream) {
  70. if (RTCBrowserType.isFirefox()) {
  71. implementOnEndedHandling(this);
  72. }
  73. addMediaStreamInactiveHandler(stream, streamInactiveHandler);
  74. }
  75. }
  76. /**
  77. * Returns the type (audio or video) of this track.
  78. */
  79. JitsiTrack.prototype.getType = function() {
  80. return this.type;
  81. };
  82. /**
  83. * Check if this is audiotrack.
  84. */
  85. JitsiTrack.prototype.isAudioTrack = function () {
  86. return this.getType() === MediaType.AUDIO;
  87. };
  88. /**
  89. * Check if this is videotrack.
  90. */
  91. JitsiTrack.prototype.isVideoTrack = function () {
  92. return this.getType() === MediaType.VIDEO;
  93. };
  94. /**
  95. * Returns the WebRTC MediaStream instance.
  96. */
  97. JitsiTrack.prototype.getOriginalStream = function() {
  98. return this.stream;
  99. };
  100. /**
  101. * Returns the ID of the underlying WebRTC Media Stream(if any)
  102. * @returns {String|null}
  103. */
  104. JitsiTrack.prototype.getStreamId = function () {
  105. return this.stream ? this.stream.id : null;
  106. };
  107. /**
  108. * Return the underlying WebRTC MediaStreamTrack
  109. * @returns {MediaStreamTrack}
  110. */
  111. JitsiTrack.prototype.getTrack = function () {
  112. return this.track;
  113. };
  114. /**
  115. * Returns the ID of the underlying WebRTC MediaStreamTrack(if any)
  116. * @returns {String|null}
  117. */
  118. JitsiTrack.prototype.getTrackId = function () {
  119. return this.track ? this.track.id : null;
  120. };
  121. /**
  122. * Return meaningful usage label for this track depending on it's media and
  123. * eventual video type.
  124. * @returns {string}
  125. */
  126. JitsiTrack.prototype.getUsageLabel = function () {
  127. if (this.isAudioTrack()) {
  128. return "mic";
  129. } else {
  130. return this.videoType ? this.videoType : "default";
  131. }
  132. };
  133. /**
  134. * Eventually will trigger RTCEvents.TRACK_ATTACHED event.
  135. * @param container the video/audio container to which this stream is attached
  136. * and for which event will be fired.
  137. * @private
  138. */
  139. JitsiTrack.prototype._maybeFireTrackAttached = function (container) {
  140. if (this.conference && container) {
  141. this.conference._onTrackAttach(this, container);
  142. }
  143. };
  144. /**
  145. * Attaches the MediaStream of this track to an HTML container.
  146. * Adds the container to the list of containers that are displaying the track.
  147. * Note that Temasys plugin will replace original audio/video element with
  148. * 'object' when stream is being attached to the container for the first time.
  149. *
  150. * * NOTE * if given container element is not visible when the stream is being
  151. * attached it will be shown back given that Temasys plugin is currently in use.
  152. *
  153. * @param container the HTML container which can be 'video' or 'audio' element.
  154. * It can also be 'object' element if Temasys plugin is in use and this
  155. * method has been called previously on video or audio HTML element.
  156. *
  157. * @returns potentially new instance of container if it was replaced by the
  158. * library. That's the case when Temasys plugin is in use.
  159. */
  160. JitsiTrack.prototype.attach = function (container) {
  161. if(this.stream) {
  162. container = RTCUtils.attachMediaStream(container, this.stream);
  163. }
  164. this.containers.push(container);
  165. this._maybeFireTrackAttached(container);
  166. return container;
  167. };
  168. /**
  169. * Removes this JitsiTrack from the passed HTML container.
  170. *
  171. * @param container the HTML container to detach from this JitsiTrack. If
  172. * <tt>null</tt> or <tt>undefined</tt>, all containers are removed. A container
  173. * can be a 'video', 'audio' or 'object' HTML element instance to which this
  174. * JitsiTrack is currently attached.
  175. */
  176. JitsiTrack.prototype.detach = function (container) {
  177. for (var cs = this.containers, i = cs.length - 1; i >= 0; --i) {
  178. var c = cs[i];
  179. if (!container) {
  180. RTCUtils.attachMediaStream(c, null);
  181. }
  182. if (!container || c === container) {
  183. cs.splice(i, 1);
  184. }
  185. }
  186. if (container) {
  187. RTCUtils.attachMediaStream(container, null);
  188. }
  189. };
  190. /**
  191. * Removes attached event listeners.
  192. *
  193. * @returns {Promise}
  194. */
  195. JitsiTrack.prototype.dispose = function () {
  196. this.eventEmitter.removeAllListeners();
  197. this.disposed = true;
  198. return Promise.resolve();
  199. };
  200. /**
  201. * Returns true if this is a video track and the source of the video is a
  202. * screen capture as opposed to a camera.
  203. */
  204. JitsiTrack.prototype.isScreenSharing = function() {
  205. };
  206. /**
  207. * FIXME remove hack in SDP.js and this method
  208. * Returns id of the track.
  209. * @returns {string|null} id of the track or null if this is fake track.
  210. */
  211. JitsiTrack.prototype._getId = function () {
  212. return this.getTrackId();
  213. };
  214. /**
  215. * Returns id of the track.
  216. * @returns {string|null} id of the track or null if this is fake track.
  217. */
  218. JitsiTrack.prototype.getId = function () {
  219. if(this.stream)
  220. return RTCUtils.getStreamID(this.stream);
  221. else
  222. return null;
  223. };
  224. /**
  225. * Checks whether the MediaStream is avtive/not ended.
  226. * When there is no check for active we don't have information and so
  227. * will return that stream is active (in case of FF).
  228. * @returns {boolean} whether MediaStream is active.
  229. */
  230. JitsiTrack.prototype.isActive = function () {
  231. if(typeof this.stream.active !== "undefined")
  232. return this.stream.active;
  233. else
  234. return true;
  235. };
  236. /**
  237. * Attaches a handler for events(For example - "audio level changed".).
  238. * All possible event are defined in JitsiTrackEvents.
  239. * @param eventId the event ID.
  240. * @param handler handler for the event.
  241. */
  242. JitsiTrack.prototype.on = function (eventId, handler) {
  243. if(this.eventEmitter)
  244. this.eventEmitter.on(eventId, handler);
  245. };
  246. /**
  247. * Removes event listener
  248. * @param eventId the event ID.
  249. * @param [handler] optional, the specific handler to unbind
  250. */
  251. JitsiTrack.prototype.off = function (eventId, handler) {
  252. if(this.eventEmitter)
  253. this.eventEmitter.removeListener(eventId, handler);
  254. };
  255. // Common aliases for event emitter
  256. JitsiTrack.prototype.addEventListener = JitsiTrack.prototype.on;
  257. JitsiTrack.prototype.removeEventListener = JitsiTrack.prototype.off;
  258. /**
  259. * Sets the audio level for the stream
  260. * @param audioLevel the new audio level
  261. */
  262. JitsiTrack.prototype.setAudioLevel = function (audioLevel) {
  263. if(this.audioLevel !== audioLevel) {
  264. this.eventEmitter.emit(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  265. audioLevel);
  266. this.audioLevel = audioLevel;
  267. }
  268. };
  269. /**
  270. * Returns the msid of the stream attached to the JitsiTrack object or null if
  271. * no stream is attached.
  272. */
  273. JitsiTrack.prototype.getMSID = function () {
  274. var streamId = this.getStreamId();
  275. var trackId = this.getTrackId();
  276. return (streamId && trackId) ? (streamId + " " + trackId) : null;
  277. };
  278. /**
  279. * Sets new audio output device for track's DOM elements. Video tracks are
  280. * ignored.
  281. * @param {string} audioOutputDeviceId - id of 'audiooutput' device from
  282. * navigator.mediaDevices.enumerateDevices(), '' for default device
  283. * @emits JitsiTrackEvents.TRACK_AUDIO_OUTPUT_CHANGED
  284. * @returns {Promise}
  285. */
  286. JitsiTrack.prototype.setAudioOutput = function (audioOutputDeviceId) {
  287. var self = this;
  288. if (!RTCUtils.isDeviceChangeAvailable('output')) {
  289. return Promise.reject(
  290. new Error('Audio output device change is not supported'));
  291. }
  292. // All audio communication is done through audio tracks, so ignore changing
  293. // audio output for video tracks at all.
  294. if (this.isVideoTrack()) {
  295. return Promise.resolve();
  296. }
  297. return Promise.all(this.containers.map(function(element) {
  298. return element.setSinkId(audioOutputDeviceId)
  299. .catch(function (error) {
  300. logger.warn(
  301. 'Failed to change audio output device on element. Default' +
  302. ' or previously set audio output device will be used.',
  303. element, error);
  304. throw error;
  305. });
  306. }))
  307. .then(function () {
  308. self.eventEmitter.emit(JitsiTrackEvents.TRACK_AUDIO_OUTPUT_CHANGED,
  309. audioOutputDeviceId);
  310. });
  311. };
  312. module.exports = JitsiTrack;