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

JitsiTrack.js 8.9KB

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