Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

JitsiTrack.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. * Eventually will trigger RTCEvents.TRACK_ATTACHED event.
  141. * @param container the video/audio container to which this stream is attached
  142. * and for which event will be fired.
  143. * @private
  144. */
  145. JitsiTrack.prototype._maybeFireTrackAttached = function (container) {
  146. if (this.rtc && container) {
  147. this.rtc.eventEmitter.emit(RTCEvents.TRACK_ATTACHED, this, container);
  148. }
  149. };
  150. /**
  151. * Mutes the track.
  152. */
  153. JitsiTrack.prototype.mute = function () {
  154. this._setMute(true);
  155. }
  156. /**
  157. * Unmutes the stream.
  158. */
  159. JitsiTrack.prototype.unmute = function () {
  160. this._setMute(false);
  161. }
  162. /**
  163. * Attaches the MediaStream of this track to an HTML container.
  164. * Adds the container to the list of containers that are displaying the track.
  165. * Note that Temasys plugin will replace original audio/video element with
  166. * 'object' when stream is being attached to the container for the first time.
  167. *
  168. * * NOTE * if given container element is not visible when the stream is being
  169. * attached it will be shown back given that Temasys plugin is currently in use.
  170. *
  171. * @param container the HTML container which can be 'video' or 'audio' element.
  172. * It can also be 'object' element if Temasys plugin is in use and this
  173. * method has been called previously on video or audio HTML element.
  174. *
  175. * @returns potentially new instance of container if it was replaced by the
  176. * library. That's the case when Temasys plugin is in use.
  177. */
  178. JitsiTrack.prototype.attach = function (container) {
  179. if(this.stream) {
  180. // The container must be visible in order to play or attach the stream
  181. // when Temasys plugin is in use
  182. var containerSel = $(container);
  183. if (RTCBrowserType.isTemasysPluginUsed() &&
  184. !containerSel.is(':visible')) {
  185. containerSel.show();
  186. }
  187. container
  188. = require("./RTCUtils").attachMediaStream(container, this.stream);
  189. }
  190. this.containers.push(container);
  191. this._maybeFireTrackAttached(container);
  192. return container;
  193. };
  194. /**
  195. * Removes the track from the passed HTML container.
  196. * @param container the HTML container. If <tt>null</tt> all containers are removed.
  197. * A container can be 'video', 'audio' or 'object' HTML element instance
  198. * to which this JitsiTrack is currently attached to.
  199. */
  200. JitsiTrack.prototype.detach = function (container) {
  201. for(var i = 0; i < this.containers.length; i++)
  202. {
  203. if(!container)
  204. {
  205. require("./RTCUtils").setVideoSrc(this.containers[i], null);
  206. }
  207. if(!container || $(this.containers[i]).is($(container)))
  208. {
  209. this.containers.splice(i,1);
  210. }
  211. }
  212. if(container) {
  213. require("./RTCUtils").setVideoSrc(container, null);
  214. }
  215. }
  216. /**
  217. * Stops sending the media track. And removes it from the HTML.
  218. * NOTE: Works for local tracks only.
  219. */
  220. JitsiTrack.prototype.stop = function () {
  221. }
  222. /**
  223. * Returns true if this is a video track and the source of the video is a
  224. * screen capture as opposed to a camera.
  225. */
  226. JitsiTrack.prototype.isScreenSharing = function(){
  227. }
  228. /**
  229. * Returns id of the track.
  230. * @returns {string} id of the track or null if this is fake track.
  231. */
  232. JitsiTrack.prototype._getId = function () {
  233. var tracks = this.stream.getTracks();
  234. if(!tracks || tracks.length === 0)
  235. return null;
  236. return tracks[0].id;
  237. };
  238. /**
  239. * Returns id of the track.
  240. * @returns {string} id of the track or null if this is fake track.
  241. */
  242. JitsiTrack.prototype.getId = function () {
  243. if(this.stream)
  244. return RTC.getStreamID(this.stream);
  245. else
  246. return null;
  247. };
  248. /**
  249. * Checks whether the MediaStream is avtive/not ended.
  250. * When there is no check for active we don't have information and so
  251. * will return that stream is active (in case of FF).
  252. * @returns {boolean} whether MediaStream is active.
  253. */
  254. JitsiTrack.prototype.isActive = function () {
  255. if((typeof this.stream.active !== "undefined"))
  256. return this.stream.active;
  257. else
  258. return true;
  259. };
  260. /**
  261. * Attaches a handler for events(For example - "audio level changed".).
  262. * All possible event are defined in JitsiTrackEvents.
  263. * @param eventId the event ID.
  264. * @param handler handler for the event.
  265. */
  266. JitsiTrack.prototype.on = function (eventId, handler) {
  267. if(this.eventEmitter)
  268. this.eventEmitter.on(eventId, handler);
  269. }
  270. /**
  271. * Removes event listener
  272. * @param eventId the event ID.
  273. * @param [handler] optional, the specific handler to unbind
  274. */
  275. JitsiTrack.prototype.off = function (eventId, handler) {
  276. if(this.eventEmitter)
  277. this.eventEmitter.removeListener(eventId, handler);
  278. }
  279. // Common aliases for event emitter
  280. JitsiTrack.prototype.addEventListener = JitsiTrack.prototype.on;
  281. JitsiTrack.prototype.removeEventListener = JitsiTrack.prototype.off;
  282. /**
  283. * Sets the audio level for the stream
  284. * @param audioLevel the new audio level
  285. */
  286. JitsiTrack.prototype.setAudioLevel = function (audioLevel) {
  287. if(this.audioLevel !== audioLevel) {
  288. this.eventEmitter.emit(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  289. audioLevel);
  290. this.audioLevel = audioLevel;
  291. }
  292. }
  293. /**
  294. * Returns the msid of the stream attached to the JitsiTrack object or null if
  295. * no stream is attached.
  296. */
  297. JitsiTrack.prototype.getMSID = function () {
  298. var tracks, track;
  299. return (!this.stream || !this.stream.id || !(tracks = this._getTracks()) ||
  300. !tracks.length || !(track = tracks[0]) || !track.id)?
  301. null : this.stream.id + " " + track.id;
  302. }
  303. module.exports = JitsiTrack;