You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JitsiTrack.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. * @param container the HTML container
  140. */
  141. JitsiTrack.prototype.attach = function (container) {
  142. if(this.stream)
  143. require("./RTCUtils").attachMediaStream(container, this.stream);
  144. this.containers.push(container);
  145. }
  146. /**
  147. * Removes the track from the passed HTML container.
  148. * @param container the HTML container. If <tt>null</tt> all containers are removed.
  149. */
  150. JitsiTrack.prototype.detach = function (container) {
  151. for(var i = 0; i < this.containers.length; i++)
  152. {
  153. if(this.containers[i].is(container))
  154. {
  155. this.containers.splice(i,1);
  156. }
  157. if(!container)
  158. {
  159. this.containers[i].find(">video").remove();
  160. }
  161. }
  162. if(container)
  163. $(container).find(">video").remove();
  164. }
  165. /**
  166. * Stops sending the media track. And removes it from the HTML.
  167. * NOTE: Works for local tracks only.
  168. */
  169. JitsiTrack.prototype.stop = function () {
  170. }
  171. /**
  172. * Returns true if this is a video track and the source of the video is a
  173. * screen capture as opposed to a camera.
  174. */
  175. JitsiTrack.prototype.isScreenSharing = function(){
  176. }
  177. /**
  178. * Returns id of the track.
  179. * @returns {string} id of the track or null if this is fake track.
  180. */
  181. JitsiTrack.prototype._getId = function () {
  182. var tracks = this.stream.getTracks();
  183. if(!tracks || tracks.length === 0)
  184. return null;
  185. return tracks[0].id;
  186. };
  187. /**
  188. * Returns id of the track.
  189. * @returns {string} id of the track or null if this is fake track.
  190. */
  191. JitsiTrack.prototype.getId = function () {
  192. if(this.stream)
  193. return RTC.getStreamID(this.stream);
  194. else
  195. return null;
  196. };
  197. /**
  198. * Checks whether the MediaStream is avtive/not ended.
  199. * When there is no check for active we don't have information and so
  200. * will return that stream is active (in case of FF).
  201. * @returns {boolean} whether MediaStream is active.
  202. */
  203. JitsiTrack.prototype.isActive = function () {
  204. if((typeof this.stream.active !== "undefined"))
  205. return this.stream.active;
  206. else
  207. return true;
  208. };
  209. /**
  210. * Attaches a handler for events(For example - "audio level changed".).
  211. * All possible event are defined in JitsiTrackEvents.
  212. * @param eventId the event ID.
  213. * @param handler handler for the event.
  214. */
  215. JitsiTrack.prototype.on = function (eventId, handler) {
  216. if(this.eventEmitter)
  217. this.eventEmitter.on(eventId, handler);
  218. }
  219. /**
  220. * Removes event listener
  221. * @param eventId the event ID.
  222. * @param [handler] optional, the specific handler to unbind
  223. */
  224. JitsiTrack.prototype.off = function (eventId, handler) {
  225. if(this.eventEmitter)
  226. this.eventEmitter.removeListener(eventId, handler);
  227. }
  228. // Common aliases for event emitter
  229. JitsiTrack.prototype.addEventListener = JitsiTrack.prototype.on;
  230. JitsiTrack.prototype.removeEventListener = JitsiTrack.prototype.off;
  231. /**
  232. * Sets the audio level for the stream
  233. * @param audioLevel the new audio level
  234. */
  235. JitsiTrack.prototype.setAudioLevel = function (audioLevel) {
  236. if(this.audioLevel !== audioLevel) {
  237. this.eventEmitter.emit(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  238. audioLevel);
  239. this.audioLevel = audioLevel;
  240. }
  241. }
  242. module.exports = JitsiTrack;