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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. var logger = require("jitsi-meet-logger").getLogger(__filename);
  2. var RTCBrowserType = require("./RTCBrowserType");
  3. var RTCEvents = require("../../service/RTC/RTCEvents");
  4. var JitsiTrackEvents = require("../../JitsiTrackEvents");
  5. var EventEmitter = require("events");
  6. var RTC = require("./RTCUtils");
  7. /**
  8. * This implements 'onended' callback normally fired by WebRTC after the stream
  9. * is stopped. There is no such behaviour yet in FF, so we have to add it.
  10. * @param jitsiTrack our track object holding the original WebRTC stream object
  11. * to which 'onended' handling will be added.
  12. */
  13. function implementOnEndedHandling(jitsiTrack) {
  14. var stream = jitsiTrack.getOriginalStream();
  15. if(!stream)
  16. return;
  17. var originalStop = stream.stop;
  18. stream.stop = function () {
  19. originalStop.apply(stream);
  20. if (jitsiTrack.isActive()) {
  21. stream.onended();
  22. }
  23. };
  24. }
  25. /**
  26. * Adds onended/oninactive handler to a MediaStream.
  27. * @param mediaStream a MediaStream to attach onended/oninactive handler
  28. * @param handler the handler
  29. */
  30. function addMediaStreamInactiveHandler(mediaStream, handler) {
  31. // Temasys will use onended
  32. if(typeof mediaStream.active !== "undefined")
  33. mediaStream.oninactive = handler;
  34. else
  35. mediaStream.onended = handler;
  36. }
  37. /**
  38. * Represents a single media track (either audio or video).
  39. * @constructor
  40. * @param rtc the rtc instance
  41. * @param stream the stream
  42. * @param streamInactiveHandler the function that will handle
  43. * onended/oninactive events of the stream.
  44. * @param jitsiTrackType optionally a type can be specified.
  45. * This is the case where we are creating a dummy track with no stream
  46. * Currently this happens when a remote side is starting with video muted
  47. */
  48. function JitsiTrack(rtc, stream, streamInactiveHandler, jitsiTrackType)
  49. {
  50. /**
  51. * Array with the HTML elements that are displaying the streams.
  52. * @type {Array}
  53. */
  54. this.containers = [];
  55. this.rtc = rtc;
  56. this.stream = stream;
  57. this.eventEmitter = new EventEmitter();
  58. this.audioLevel = -1;
  59. this.type = jitsiTrackType || ((this.stream.getVideoTracks().length > 0)?
  60. JitsiTrack.VIDEO : JitsiTrack.AUDIO);
  61. if(this.type == JitsiTrack.AUDIO) {
  62. this._getTracks = function () {
  63. return this.stream? this.stream.getAudioTracks() : [];
  64. }.bind(this);
  65. } else {
  66. this._getTracks = function () {
  67. return this.stream? this.stream.getVideoTracks() : [];
  68. }.bind(this);
  69. }
  70. if(stream) {
  71. if (RTCBrowserType.isFirefox()) {
  72. implementOnEndedHandling(this);
  73. }
  74. addMediaStreamInactiveHandler(stream, streamInactiveHandler);
  75. }
  76. }
  77. /**
  78. * JitsiTrack video type.
  79. * @type {string}
  80. */
  81. JitsiTrack.VIDEO = "video";
  82. /**
  83. * JitsiTrack audio type.
  84. * @type {string}
  85. */
  86. JitsiTrack.AUDIO = "audio";
  87. /**
  88. * Returns the type (audio or video) of this track.
  89. */
  90. JitsiTrack.prototype.getType = function() {
  91. return this.type;
  92. };
  93. /**
  94. * Check if this is audiotrack.
  95. */
  96. JitsiTrack.prototype.isAudioTrack = function () {
  97. return this.getType() === JitsiTrack.AUDIO;
  98. };
  99. /**
  100. * Check if this is videotrack.
  101. */
  102. JitsiTrack.prototype.isVideoTrack = function () {
  103. return this.getType() === JitsiTrack.VIDEO;
  104. };
  105. /**
  106. * Returns the RTCMediaStream from the browser (?).
  107. */
  108. JitsiTrack.prototype.getOriginalStream = function() {
  109. return this.stream;
  110. }
  111. /**
  112. * Return meaningful usage label for this track depending on it's media and
  113. * eventual video type.
  114. * @returns {string}
  115. */
  116. JitsiTrack.prototype.getUsageLabel = function () {
  117. if (this.type == JitsiTrack.AUDIO) {
  118. return "mic";
  119. } else {
  120. return this.videoType ? this.videoType : "default";
  121. }
  122. };
  123. /**
  124. * Eventually will trigger RTCEvents.TRACK_ATTACHED event.
  125. * @param container the video/audio container to which this stream is attached
  126. * and for which event will be fired.
  127. * @private
  128. */
  129. JitsiTrack.prototype._maybeFireTrackAttached = function (container) {
  130. if (this.rtc && container) {
  131. this.rtc.eventEmitter.emit(RTCEvents.TRACK_ATTACHED, this, container);
  132. }
  133. };
  134. /**
  135. * Attaches the MediaStream of this track to an HTML container.
  136. * Adds the container to the list of containers that are displaying the track.
  137. * Note that Temasys plugin will replace original audio/video element with
  138. * 'object' when stream is being attached to the container for the first time.
  139. *
  140. * * NOTE * if given container element is not visible when the stream is being
  141. * attached it will be shown back given that Temasys plugin is currently in use.
  142. *
  143. * @param container the HTML container which can be 'video' or 'audio' element.
  144. * It can also be 'object' element if Temasys plugin is in use and this
  145. * method has been called previously on video or audio HTML element.
  146. *
  147. * @returns potentially new instance of container if it was replaced by the
  148. * library. That's the case when Temasys plugin is in use.
  149. */
  150. JitsiTrack.prototype.attach = function (container) {
  151. if(this.stream) {
  152. // The container must be visible in order to play or attach the stream
  153. // when Temasys plugin is in use
  154. var containerSel = $(container);
  155. if (RTCBrowserType.isTemasysPluginUsed() &&
  156. !containerSel.is(':visible')) {
  157. containerSel.show();
  158. }
  159. container
  160. = require("./RTCUtils").attachMediaStream(container, this.stream);
  161. }
  162. this.containers.push(container);
  163. this._maybeFireTrackAttached(container);
  164. return container;
  165. };
  166. /**
  167. * Removes the track from the passed HTML container.
  168. * @param container the HTML container. If <tt>null</tt> all containers are removed.
  169. * A container can be 'video', 'audio' or 'object' HTML element instance
  170. * to which this JitsiTrack is currently attached to.
  171. */
  172. JitsiTrack.prototype.detach = function (container) {
  173. for(var i = 0; i < this.containers.length; i++)
  174. {
  175. if(!container)
  176. {
  177. require("./RTCUtils").setVideoSrc(this.containers[i], null);
  178. }
  179. if(!container || $(this.containers[i]).is($(container)))
  180. {
  181. this.containers.splice(i,1);
  182. }
  183. }
  184. if(container) {
  185. require("./RTCUtils").setVideoSrc(container, null);
  186. }
  187. }
  188. /**
  189. * Dispose sending the media track. And removes it from the HTML.
  190. * NOTE: Works for local tracks only.
  191. */
  192. JitsiTrack.prototype.dispose = function () {
  193. }
  194. /**
  195. * Returns true if this is a video track and the source of the video is a
  196. * screen capture as opposed to a camera.
  197. */
  198. JitsiTrack.prototype.isScreenSharing = function(){
  199. }
  200. /**
  201. * Returns id of the track.
  202. * @returns {string} id of the track or null if this is fake track.
  203. */
  204. JitsiTrack.prototype._getId = function () {
  205. var tracks = this.stream.getTracks();
  206. if(!tracks || tracks.length === 0)
  207. return null;
  208. return tracks[0].id;
  209. };
  210. /**
  211. * Returns id of the track.
  212. * @returns {string} id of the track or null if this is fake track.
  213. */
  214. JitsiTrack.prototype.getId = function () {
  215. if(this.stream)
  216. return RTC.getStreamID(this.stream);
  217. else
  218. return null;
  219. };
  220. /**
  221. * Checks whether the MediaStream is avtive/not ended.
  222. * When there is no check for active we don't have information and so
  223. * will return that stream is active (in case of FF).
  224. * @returns {boolean} whether MediaStream is active.
  225. */
  226. JitsiTrack.prototype.isActive = function () {
  227. if((typeof this.stream.active !== "undefined"))
  228. return this.stream.active;
  229. else
  230. return true;
  231. };
  232. /**
  233. * Attaches a handler for events(For example - "audio level changed".).
  234. * All possible event are defined in JitsiTrackEvents.
  235. * @param eventId the event ID.
  236. * @param handler handler for the event.
  237. */
  238. JitsiTrack.prototype.on = function (eventId, handler) {
  239. if(this.eventEmitter)
  240. this.eventEmitter.on(eventId, handler);
  241. }
  242. /**
  243. * Removes event listener
  244. * @param eventId the event ID.
  245. * @param [handler] optional, the specific handler to unbind
  246. */
  247. JitsiTrack.prototype.off = function (eventId, handler) {
  248. if(this.eventEmitter)
  249. this.eventEmitter.removeListener(eventId, handler);
  250. }
  251. // Common aliases for event emitter
  252. JitsiTrack.prototype.addEventListener = JitsiTrack.prototype.on;
  253. JitsiTrack.prototype.removeEventListener = JitsiTrack.prototype.off;
  254. /**
  255. * Sets the audio level for the stream
  256. * @param audioLevel the new audio level
  257. */
  258. JitsiTrack.prototype.setAudioLevel = function (audioLevel) {
  259. if(this.audioLevel !== audioLevel) {
  260. this.eventEmitter.emit(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  261. audioLevel);
  262. this.audioLevel = audioLevel;
  263. }
  264. }
  265. /**
  266. * Returns the msid of the stream attached to the JitsiTrack object or null if
  267. * no stream is attached.
  268. */
  269. JitsiTrack.prototype.getMSID = function () {
  270. var tracks, track;
  271. return (!this.stream || !this.stream.id || !(tracks = this._getTracks()) ||
  272. !tracks.length || !(track = tracks[0]) || !track.id)?
  273. null : this.stream.id + " " + track.id;
  274. }
  275. module.exports = JitsiTrack;