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.2KB

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