modified lib-jitsi-meet dev repo
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JitsiTrack.js 9.1KB

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