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

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