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

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