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.

JitsiRemoteTrack.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* global */
  2. var JitsiTrack = require("./JitsiTrack");
  3. import * as JitsiTrackEvents from "../../JitsiTrackEvents";
  4. var logger = require("jitsi-meet-logger").getLogger(__filename);
  5. var RTCBrowserType = require("./RTCBrowserType");
  6. var RTCEvents = require("../../service/RTC/RTCEvents");
  7. var Statistics = require("../statistics/statistics");
  8. var ttfmTrackerAudioAttached = false;
  9. var ttfmTrackerVideoAttached = false;
  10. /**
  11. * Represents a single media track (either audio or video).
  12. * @param {RTC} rtc the RTC service instance.
  13. * @param {JitsiConference} conference the conference to which this track
  14. * belongs to
  15. * @param {string} ownerEndpointId the endpoint ID of the track owner
  16. * @param {MediaStream} stream WebRTC MediaStream, parent of the track
  17. * @param {MediaStreamTrack} track underlying WebRTC MediaStreamTrack for
  18. * the new JitsiRemoteTrack
  19. * @param {MediaType} mediaType the type of the media
  20. * @param {VideoType} videoType the type of the video if applicable
  21. * @param {string} ssrc the SSRC number of the Media Stream
  22. * @param {boolean} muted the initial muted state
  23. * @constructor
  24. */
  25. function JitsiRemoteTrack(rtc, conference, ownerEndpointId, stream, track,
  26. mediaType, videoType, ssrc, muted) {
  27. JitsiTrack.call(
  28. this, conference, stream, track, function () {}, mediaType, videoType, ssrc);
  29. this.rtc = rtc;
  30. this.ownerEndpointId = ownerEndpointId;
  31. this.muted = muted;
  32. // we want to mark whether the track has been ever muted
  33. // to detect ttfm events for startmuted conferences, as it can significantly
  34. // increase ttfm values
  35. this.hasBeenMuted = muted;
  36. // Bind 'onmute' and 'onunmute' event handlers
  37. if (this.rtc && this.track) {
  38. this._bindMuteHandlers();
  39. }
  40. }
  41. JitsiRemoteTrack.prototype = Object.create(JitsiTrack.prototype);
  42. JitsiRemoteTrack.prototype.constructor = JitsiRemoteTrack;
  43. JitsiRemoteTrack.prototype._bindMuteHandlers = function() {
  44. // Bind 'onmute'
  45. // FIXME it would be better to use recently added '_setHandler' method, but
  46. // 1. It does not allow to set more than one handler to the event
  47. // 2. It does mix MediaStream('inactive') with MediaStreamTrack events
  48. // 3. Allowing to bind more than one event handler requires too much
  49. // refactoring around camera issues detection.
  50. this.track.addEventListener('mute', function () {
  51. logger.debug(
  52. '"onmute" event(' + Date.now() + '): ',
  53. this.getParticipantId(), this.getType(), this.getSSRC());
  54. this.rtc.eventEmitter.emit(RTCEvents.REMOTE_TRACK_MUTE, this);
  55. }.bind(this));
  56. // Bind 'onunmute'
  57. this.track.addEventListener('unmute', function () {
  58. logger.debug(
  59. '"onunmute" event(' + Date.now() + '): ',
  60. this.getParticipantId(), this.getType(), this.getSSRC());
  61. this.rtc.eventEmitter.emit(RTCEvents.REMOTE_TRACK_UNMUTE, this);
  62. }.bind(this));
  63. };
  64. /**
  65. * Sets current muted status and fires an events for the change.
  66. * @param value the muted status.
  67. */
  68. JitsiRemoteTrack.prototype.setMute = function (value) {
  69. if(this.muted === value) {
  70. return;
  71. }
  72. if(value) {
  73. this.hasBeenMuted = true;
  74. }
  75. // we can have a fake video stream
  76. if(this.stream) {
  77. this.stream.muted = value;
  78. }
  79. this.muted = value;
  80. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED, this);
  81. };
  82. /**
  83. * Returns the current muted status of the track.
  84. * @returns {boolean|*|JitsiRemoteTrack.muted} <tt>true</tt> if the track is
  85. * muted and <tt>false</tt> otherwise.
  86. */
  87. JitsiRemoteTrack.prototype.isMuted = function () {
  88. return this.muted;
  89. };
  90. /**
  91. * Returns the participant id which owns the track.
  92. * @returns {string} the id of the participants. It corresponds to the Colibri
  93. * endpoint id/MUC nickname in case of Jitsi-meet.
  94. */
  95. JitsiRemoteTrack.prototype.getParticipantId = function() {
  96. return this.ownerEndpointId;
  97. };
  98. /**
  99. * Return false;
  100. */
  101. JitsiRemoteTrack.prototype.isLocal = function () {
  102. return false;
  103. };
  104. /**
  105. * Returns the synchronization source identifier (SSRC) of this remote track.
  106. * @returns {string} the SSRC of this remote track
  107. */
  108. JitsiRemoteTrack.prototype.getSSRC = function () {
  109. return this.ssrc;
  110. };
  111. /**
  112. * Changes the video type of the track
  113. * @param type the new video type("camera", "desktop")
  114. */
  115. JitsiRemoteTrack.prototype._setVideoType = function (type) {
  116. if(this.videoType === type) {
  117. return;
  118. }
  119. this.videoType = type;
  120. this.eventEmitter.emit(JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED, type);
  121. };
  122. JitsiRemoteTrack.prototype._playCallback = function () {
  123. var type = this.isVideoTrack() ? 'video' : 'audio';
  124. var now = window.performance.now();
  125. console.log("(TIME) Render " + type + ":\t", now);
  126. this.conference.getConnectionTimes()[type + ".render"] = now;
  127. var ttfm = now
  128. - (this.conference.getConnectionTimes()["session.initiate"]
  129. - this.conference.getConnectionTimes()["muc.joined"])
  130. - (window.connectionTimes["obtainPermissions.end"]
  131. - window.connectionTimes["obtainPermissions.start"]);
  132. this.conference.getConnectionTimes()[type + ".ttfm"] = ttfm;
  133. console.log("(TIME) TTFM " + type + ":\t", ttfm);
  134. var eventName = type +'.ttfm';
  135. if(this.hasBeenMuted) {
  136. eventName += '.muted';
  137. }
  138. Statistics.analytics.sendEvent(eventName, {value: ttfm});
  139. };
  140. /**
  141. * Attach time to first media tracker only if there is conference and only
  142. * for the first element.
  143. * @param container the HTML container which can be 'video' or 'audio' element.
  144. * It can also be 'object' element if Temasys plugin is in use and this
  145. * method has been called previously on video or audio HTML element.
  146. * @private
  147. */
  148. JitsiRemoteTrack.prototype._attachTTFMTracker = function (container) {
  149. if((ttfmTrackerAudioAttached && this.isAudioTrack())
  150. || (ttfmTrackerVideoAttached && this.isVideoTrack())) {
  151. return;
  152. }
  153. if (this.isAudioTrack()) {
  154. ttfmTrackerAudioAttached = true;
  155. }
  156. if (this.isVideoTrack()) {
  157. ttfmTrackerVideoAttached = true;
  158. }
  159. if (RTCBrowserType.isTemasysPluginUsed()) {
  160. // XXX Don't require Temasys unless it's to be used because it doesn't
  161. // run on React Native, for example.
  162. const AdapterJS = require("./adapter.screenshare");
  163. // FIXME: this is not working for IE11
  164. AdapterJS.addEvent(container, 'play', this._playCallback.bind(this));
  165. } else {
  166. container.addEventListener("canplay", this._playCallback.bind(this));
  167. }
  168. };
  169. module.exports = JitsiRemoteTrack;