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

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