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

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