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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. var RTCBrowserType = require("./RTCBrowserType");
  2. /**
  3. * This implements 'onended' callback normally fired by WebRTC after the stream
  4. * is stopped. There is no such behaviour yet in FF, so we have to add it.
  5. * @param jitsiTrack our track object holding the original WebRTC stream object
  6. * to which 'onended' handling will be added.
  7. */
  8. function implementOnEndedHandling(jitsiTrack) {
  9. var stream = jitsiTrack.getOriginalStream();
  10. var originalStop = stream.stop;
  11. stream.stop = function () {
  12. originalStop.apply(stream);
  13. if (jitsiTrack.isActive()) {
  14. stream.onended();
  15. }
  16. };
  17. }
  18. /**
  19. * Adds onended/oninactive handler to a MediaStream.
  20. * @param mediaStream a MediaStream to attach onended/oninactive handler
  21. * @param handler the handler
  22. */
  23. function addMediaStreamInactiveHandler(mediaStream, handler) {
  24. if(RTCBrowserType.isTemasysPluginUsed()) {
  25. // themasys
  26. mediaStream.attachEvent('ended', function () {
  27. handler(mediaStream);
  28. });
  29. }
  30. else {
  31. if(typeof mediaStream.active !== "undefined")
  32. mediaStream.oninactive = handler;
  33. else
  34. mediaStream.onended = handler;
  35. }
  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. */
  45. function JitsiTrack(rtc, stream, streamInactiveHandler)
  46. {
  47. /**
  48. * Array with the HTML elements that are displaying the streams.
  49. * @type {Array}
  50. */
  51. this.containers = [];
  52. this.rtc = rtc;
  53. this.stream = stream;
  54. this.type = (this.stream.getVideoTracks().length > 0)?
  55. JitsiTrack.VIDEO : JitsiTrack.AUDIO;
  56. if(this.type == "audio") {
  57. this._getTracks = function () {
  58. return this.stream.getAudioTracks();
  59. }.bind(this);
  60. } else {
  61. this._getTracks = function () {
  62. return this.stream.getVideoTracks();
  63. }.bind(this);
  64. }
  65. if (RTCBrowserType.isFirefox() && this.stream) {
  66. implementOnEndedHandling(this);
  67. }
  68. if(stream)
  69. addMediaStreamInactiveHandler(stream, streamInactiveHandler);
  70. }
  71. /**
  72. * JitsiTrack video type.
  73. * @type {string}
  74. */
  75. JitsiTrack.VIDEO = "video";
  76. /**
  77. * JitsiTrack audio type.
  78. * @type {string}
  79. */
  80. JitsiTrack.AUDIO = "audio";
  81. /**
  82. * Returns the type (audio or video) of this track.
  83. */
  84. JitsiTrack.prototype.getType = function() {
  85. return this.type;
  86. };
  87. /**
  88. * Returns the RTCMediaStream from the browser (?).
  89. */
  90. JitsiTrack.prototype.getOriginalStream = function() {
  91. return this.stream;
  92. }
  93. /**
  94. * Mutes the track.
  95. */
  96. JitsiTrack.prototype.mute = function () {
  97. this._setMute(true);
  98. }
  99. /**
  100. * Unmutes the stream.
  101. */
  102. JitsiTrack.prototype.unmute = function () {
  103. this._setMute(false);
  104. }
  105. /**
  106. * Attaches the MediaStream of this track to an HTML container (?).
  107. * Adds the container to the list of containers that are displaying the track.
  108. * @param container the HTML container
  109. */
  110. JitsiTrack.prototype.attach = function (container) {
  111. if(this.stream)
  112. require("./RTCUtils").attachMediaStream(container, this.stream);
  113. this.containers.push(container);
  114. }
  115. /**
  116. * Removes the track from the passed HTML container.
  117. * @param container the HTML container. If <tt>null</tt> all containers are removed.
  118. */
  119. JitsiTrack.prototype.detach = function (container) {
  120. for(var i = 0; i < this.containers.length; i++)
  121. {
  122. if(this.containers[i].is(container))
  123. {
  124. this.containers.splice(i,1);
  125. }
  126. if(!container)
  127. {
  128. this.containers[i].find(">video").remove();
  129. }
  130. }
  131. if(container)
  132. $(container).find(">video").remove();
  133. }
  134. /**
  135. * Stops sending the media track. And removes it from the HTML.
  136. * NOTE: Works for local tracks only.
  137. */
  138. JitsiTrack.prototype.stop = function () {
  139. }
  140. /**
  141. * Returns true if this is a video track and the source of the video is a
  142. * screen capture as opposed to a camera.
  143. */
  144. JitsiTrack.prototype.isScreenSharing = function(){
  145. }
  146. /**
  147. * Returns id of the track.
  148. * @returns {string} id of the track or null if this is fake track.
  149. */
  150. JitsiTrack.prototype.getId = function () {
  151. var tracks = this.stream.getTracks();
  152. if(!tracks || tracks.length === 0)
  153. return null;
  154. return tracks[0].id;
  155. };
  156. /**
  157. * Checks whether the MediaStream is avtive/not ended.
  158. * When there is no check for active we don't have information and so
  159. * will return that stream is active (in case of FF).
  160. * @returns {boolean} whether MediaStream is active.
  161. */
  162. JitsiTrack.prototype.isActive = function () {
  163. if((typeof this.stream.active !== "undefined"))
  164. return this.stream.active;
  165. else
  166. return true;
  167. };
  168. module.exports = JitsiTrack;