Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JitsiRemoteTrack.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { createTtfmEvent } from '../../service/statistics/AnalyticsEvents';
  2. import JitsiTrack from './JitsiTrack';
  3. import * as JitsiTrackEvents from '../../JitsiTrackEvents';
  4. import Statistics from '../statistics/statistics';
  5. const logger = require('jitsi-meet-logger').getLogger(__filename);
  6. const RTCEvents = require('../../service/RTC/RTCEvents');
  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. */
  13. export default class JitsiRemoteTrack extends JitsiTrack {
  14. /**
  15. * Creates new JitsiRemoteTrack instance.
  16. * @param {RTC} rtc the RTC service instance.
  17. * @param {JitsiConference} conference the conference to which this track
  18. * belongs to
  19. * @param {string} ownerEndpointId the endpoint ID of the track owner
  20. * @param {MediaStream} stream WebRTC MediaStream, parent of the track
  21. * @param {MediaStreamTrack} track underlying WebRTC MediaStreamTrack for
  22. * the new JitsiRemoteTrack
  23. * @param {MediaType} mediaType the type of the media
  24. * @param {VideoType} videoType the type of the video if applicable
  25. * @param {number} ssrc the SSRC number of the Media Stream
  26. * @param {boolean} muted the initial muted state
  27. * @param {boolean} isP2P indicates whether or not this track belongs to a
  28. * P2P session
  29. * @throws {TypeError} if <tt>ssrc</tt> is not a number.
  30. * @constructor
  31. */
  32. constructor(
  33. rtc,
  34. conference,
  35. ownerEndpointId,
  36. stream,
  37. track,
  38. mediaType,
  39. videoType,
  40. ssrc,
  41. muted,
  42. isP2P) {
  43. super(
  44. conference,
  45. stream,
  46. track,
  47. () => {
  48. // Nothing to do if the track is inactive.
  49. },
  50. mediaType,
  51. videoType);
  52. this.rtc = rtc;
  53. // Prevent from mixing up type of SSRC which should be a number
  54. if (typeof ssrc !== 'number') {
  55. throw new TypeError(`SSRC ${ssrc} is not a number`);
  56. }
  57. this.ssrc = ssrc;
  58. this.ownerEndpointId = ownerEndpointId;
  59. this.muted = muted;
  60. this.isP2P = isP2P;
  61. // we want to mark whether the track has been ever muted
  62. // to detect ttfm events for startmuted conferences, as it can
  63. // significantly increase ttfm values
  64. this.hasBeenMuted = muted;
  65. // Bind 'onmute' and 'onunmute' event handlers
  66. if (this.rtc && this.track) {
  67. this._bindMuteHandlers();
  68. }
  69. }
  70. /* eslint-enable max-params */
  71. /**
  72. * Attaches the track muted handlers.
  73. *
  74. * @returns {void}
  75. */
  76. _bindMuteHandlers() {
  77. this.track.addEventListener('mute', () => this._onTrackMute());
  78. this.track.addEventListener('unmute', () => this._onTrackUnmute());
  79. }
  80. /**
  81. * Callback invoked when the track is muted. Emits an event notifying
  82. * listeners of the mute event.
  83. *
  84. * @private
  85. * @returns {void}
  86. */
  87. _onTrackMute() {
  88. logger.debug(
  89. `"onmute" event(${Date.now()}): `,
  90. this.getParticipantId(), this.getType(), this.getSSRC());
  91. this.rtc.eventEmitter.emit(RTCEvents.REMOTE_TRACK_MUTE, this);
  92. }
  93. /**
  94. * Callback invoked when the track is unmuted. Emits an event notifying
  95. * listeners of the mute event.
  96. *
  97. * @private
  98. * @returns {void}
  99. */
  100. _onTrackUnmute() {
  101. logger.debug(
  102. `"onunmute" event(${Date.now()}): `,
  103. this.getParticipantId(), this.getType(), this.getSSRC());
  104. this.rtc.eventEmitter.emit(RTCEvents.REMOTE_TRACK_UNMUTE, this);
  105. }
  106. /**
  107. * Sets current muted status and fires an events for the change.
  108. * @param value the muted status.
  109. */
  110. setMute(value) {
  111. if (this.muted === value) {
  112. return;
  113. }
  114. if (value) {
  115. this.hasBeenMuted = true;
  116. }
  117. // we can have a fake video stream
  118. if (this.stream) {
  119. this.stream.muted = value;
  120. }
  121. this.muted = value;
  122. this.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED, this);
  123. }
  124. /**
  125. * Returns the current muted status of the track.
  126. * @returns {boolean|*|JitsiRemoteTrack.muted} <tt>true</tt> if the track is
  127. * muted and <tt>false</tt> otherwise.
  128. */
  129. isMuted() {
  130. return this.muted;
  131. }
  132. /**
  133. * Returns the participant id which owns the track.
  134. *
  135. * @returns {string} the id of the participants. It corresponds to the
  136. * Colibri endpoint id/MUC nickname in case of Jitsi-meet.
  137. */
  138. getParticipantId() {
  139. return this.ownerEndpointId;
  140. }
  141. /**
  142. * Return false;
  143. */
  144. isLocal() {
  145. return false;
  146. }
  147. /**
  148. * Returns the synchronization source identifier (SSRC) of this remote
  149. * track.
  150. *
  151. * @returns {number} the SSRC of this remote track.
  152. */
  153. getSSRC() {
  154. return this.ssrc;
  155. }
  156. /**
  157. * Changes the video type of the track.
  158. *
  159. * @param {string} type - The new video type("camera", "desktop").
  160. */
  161. _setVideoType(type) {
  162. if (this.videoType === type) {
  163. return;
  164. }
  165. this.videoType = type;
  166. this.emit(JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED, type);
  167. }
  168. /**
  169. * Handles track play events.
  170. */
  171. _playCallback() {
  172. const type = this.isVideoTrack() ? 'video' : 'audio';
  173. const now = window.performance.now();
  174. console.log(`(TIME) Render ${type}:\t`, now);
  175. this.conference.getConnectionTimes()[`${type}.render`] = now;
  176. // The conference can be started without calling GUM
  177. // FIXME if there would be a module for connection times this kind
  178. // of logic (gumDuration or ttfm) should end up there
  179. const gumStart = window.connectionTimes['obtainPermissions.start'];
  180. const gumEnd = window.connectionTimes['obtainPermissions.end'];
  181. const gumDuration
  182. = !isNaN(gumEnd) && !isNaN(gumStart) ? gumEnd - gumStart : 0;
  183. // Subtract the muc.joined-to-session-initiate duration because jicofo
  184. // waits until there are 2 participants to start Jingle sessions.
  185. const ttfm = now
  186. - (this.conference.getConnectionTimes()['session.initiate']
  187. - this.conference.getConnectionTimes()['muc.joined'])
  188. - gumDuration;
  189. this.conference.getConnectionTimes()[`${type}.ttfm`] = ttfm;
  190. console.log(`(TIME) TTFM ${type}:\t`, ttfm);
  191. Statistics.sendAnalytics(createTtfmEvent(
  192. {
  193. 'media_type': type,
  194. muted: this.hasBeenMuted,
  195. value: ttfm
  196. }));
  197. }
  198. /**
  199. * Attach time to first media tracker only if there is conference and only
  200. * for the first element.
  201. * @param container the HTML container which can be 'video' or 'audio'
  202. * element.
  203. * @private
  204. */
  205. _attachTTFMTracker(container) {
  206. if ((ttfmTrackerAudioAttached && this.isAudioTrack())
  207. || (ttfmTrackerVideoAttached && this.isVideoTrack())) {
  208. return;
  209. }
  210. if (this.isAudioTrack()) {
  211. ttfmTrackerAudioAttached = true;
  212. }
  213. if (this.isVideoTrack()) {
  214. ttfmTrackerVideoAttached = true;
  215. }
  216. container.addEventListener('canplay', this._playCallback.bind(this));
  217. }
  218. /**
  219. * Creates a text representation of this remote track instance.
  220. * @return {string}
  221. */
  222. toString() {
  223. return `RemoteTrack[${
  224. this.ownerEndpointId}, ${
  225. this.getType()}, p2p: ${
  226. this.isP2P}]`;
  227. }
  228. }