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

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