您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JitsiTrack.js 8.4KB

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