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

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