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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. import * as JitsiTrackEvents from '../../JitsiTrackEvents';
  2. import { VideoType } from '../../service/RTC/VideoType';
  3. import { createTtfmEvent } from '../../service/statistics/AnalyticsEvents';
  4. import TrackStreamingStatusImpl, { TrackStreamingStatus } from '../connectivity/TrackStreamingStatus';
  5. import Statistics from '../statistics/statistics';
  6. import JitsiTrack from './JitsiTrack';
  7. const logger = require('@jitsi/logger').getLogger(__filename);
  8. const RTCEvents = require('../../service/RTC/RTCEvents');
  9. let ttfmTrackerAudioAttached = false;
  10. let ttfmTrackerVideoAttached = false;
  11. /**
  12. * List of container events that we are going to process. _onContainerEventHandler will be added as listener to the
  13. * container for every event in the list.
  14. */
  15. const containerEvents = [ 'abort', 'canplaythrough', 'ended', 'error', 'stalled', 'suspend', 'waiting' ];
  16. /* eslint-disable max-params */
  17. /**
  18. * Represents a single media track (either audio or video).
  19. */
  20. export default class JitsiRemoteTrack extends JitsiTrack {
  21. /**
  22. * Creates new JitsiRemoteTrack instance.
  23. * @param {RTC} rtc the RTC service instance.
  24. * @param {JitsiConference} conference the conference to which this track
  25. * belongs to
  26. * @param {string} ownerEndpointId the endpoint ID of the track owner
  27. * @param {MediaStream} stream WebRTC MediaStream, parent of the track
  28. * @param {MediaStreamTrack} track underlying WebRTC MediaStreamTrack for
  29. * the new JitsiRemoteTrack
  30. * @param {MediaType} mediaType the type of the media
  31. * @param {VideoType} videoType the type of the video if applicable
  32. * @param {number} ssrc the SSRC number of the Media Stream
  33. * @param {boolean} muted the initial muted state
  34. * @param {boolean} isP2P indicates whether or not this track belongs to a
  35. * P2P session
  36. * @param {String} sourceName the source name signaled for the track
  37. * @throws {TypeError} if <tt>ssrc</tt> is not a number.
  38. * @constructor
  39. */
  40. constructor(
  41. rtc,
  42. conference,
  43. ownerEndpointId,
  44. stream,
  45. track,
  46. mediaType,
  47. videoType,
  48. ssrc,
  49. muted,
  50. isP2P,
  51. sourceName) {
  52. super(
  53. conference,
  54. stream,
  55. track,
  56. () => {
  57. // Nothing to do if the track is inactive.
  58. },
  59. mediaType,
  60. videoType);
  61. this.rtc = rtc;
  62. // Prevent from mixing up type of SSRC which should be a number
  63. if (typeof ssrc !== 'number') {
  64. throw new TypeError(`SSRC ${ssrc} is not a number`);
  65. }
  66. this.ssrc = ssrc;
  67. this.ownerEndpointId = ownerEndpointId;
  68. this.muted = muted;
  69. this.isP2P = isP2P;
  70. this._sourceName = sourceName;
  71. this._trackStreamingStatus = null;
  72. this._trackStreamingStatusImpl = null;
  73. /**
  74. * This holds the timestamp indicating when remote video track entered forwarded sources set. Track entering
  75. * forwardedSources will have streaming status restoring and when we start receiving video will become active,
  76. * but if video is not received for certain time {@link DEFAULT_RESTORING_TIMEOUT} that track streaming status
  77. * will become interrupted.
  78. */
  79. this._enteredForwardedSourcesTimestamp = null;
  80. this.addEventListener = this.on = this._addEventListener.bind(this);
  81. this.removeEventListener = this.off = this._removeEventListener.bind(this);
  82. logger.debug(`New remote track created: ${this}`);
  83. // we want to mark whether the track has been ever muted
  84. // to detect ttfm events for startmuted conferences, as it can
  85. // significantly increase ttfm values
  86. this.hasBeenMuted = muted;
  87. // Bind 'onmute' and 'onunmute' event handlers
  88. if (this.rtc && this.track) {
  89. this._bindTrackHandlers();
  90. }
  91. this._containerHandlers = {};
  92. containerEvents.forEach(event => {
  93. this._containerHandlers[event] = this._containerEventHandler.bind(this, event);
  94. });
  95. }
  96. /* eslint-enable max-params */
  97. /**
  98. * Attaches the track handlers.
  99. *
  100. * @returns {void}
  101. */
  102. _bindTrackHandlers() {
  103. this.track.addEventListener('mute', () => this._onTrackMute());
  104. this.track.addEventListener('unmute', () => this._onTrackUnmute());
  105. this.track.addEventListener('ended', () => {
  106. logger.debug(`"onended" event(${Date.now()}): ${this}`);
  107. });
  108. }
  109. /**
  110. * Overrides addEventListener method to init TrackStreamingStatus instance when there are listeners for the
  111. * {@link JitsiTrackEvents.TRACK_STREAMING_STATUS_CHANGED} event.
  112. *
  113. * @param {string} event - event name
  114. * @param {function} handler - event handler
  115. */
  116. _addEventListener(event, handler) {
  117. super.addListener(event, handler);
  118. if (event === JitsiTrackEvents.TRACK_STREAMING_STATUS_CHANGED
  119. && this.listenerCount(JitsiTrackEvents.TRACK_STREAMING_STATUS_CHANGED)
  120. && !this._trackStreamingStatusImpl
  121. ) {
  122. this._initTrackStreamingStatus();
  123. logger.debug(`Initializing track streaming status: ${this._sourceName}`);
  124. }
  125. }
  126. /**
  127. * Overrides removeEventListener method to dispose TrackStreamingStatus instance.
  128. *
  129. * @param {string} event - event name
  130. * @param {function} handler - event handler
  131. */
  132. _removeEventListener(event, handler) {
  133. super.removeListener(event, handler);
  134. if (event === JitsiTrackEvents.TRACK_STREAMING_STATUS_CHANGED
  135. && !this.listenerCount(JitsiTrackEvents.TRACK_STREAMING_STATUS_CHANGED)
  136. ) {
  137. this._disposeTrackStreamingStatus();
  138. logger.debug(`Disposing track streaming status: ${this._sourceName}`);
  139. }
  140. }
  141. /**
  142. * Callback invoked when the track is muted. Emits an event notifying
  143. * listeners of the mute event.
  144. *
  145. * @private
  146. * @returns {void}
  147. */
  148. _onTrackMute() {
  149. logger.debug(`"onmute" event(${Date.now()}): ${this}`);
  150. // Ignore mute events that get fired on desktop tracks because of 0Hz screensharing introduced in Chromium.
  151. // The sender stops sending frames if the content of the captured window doesn't change resulting in the
  152. // receiver showing avatar instead of the shared content.
  153. if (this.videoType === VideoType.DESKTOP) {
  154. logger.debug('Ignoring mute event on desktop tracks.');
  155. return;
  156. }
  157. this.rtc.eventEmitter.emit(RTCEvents.REMOTE_TRACK_MUTE, this);
  158. }
  159. /**
  160. * Callback invoked when the track is unmuted. Emits an event notifying
  161. * listeners of the mute event.
  162. *
  163. * @private
  164. * @returns {void}
  165. */
  166. _onTrackUnmute() {
  167. logger.debug(`"onunmute" event(${Date.now()}): ${this}`);
  168. this.rtc.eventEmitter.emit(RTCEvents.REMOTE_TRACK_UNMUTE, this);
  169. }
  170. /**
  171. * Removes attached event listeners and dispose TrackStreamingStatus .
  172. *
  173. * @returns {Promise}
  174. */
  175. dispose() {
  176. this._disposeTrackStreamingStatus();
  177. return super.dispose();
  178. }
  179. /**
  180. * Sets current muted status and fires an events for the change.
  181. * @param value the muted status.
  182. */
  183. setMute(value) {
  184. if (this.muted === value) {
  185. return;
  186. }
  187. if (value) {
  188. this.hasBeenMuted = true;
  189. }
  190. // we can have a fake video stream
  191. if (this.stream) {
  192. this.stream.muted = value;
  193. }
  194. this.muted = value;
  195. this.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED, this);
  196. }
  197. /**
  198. * Returns the current muted status of the track.
  199. * @returns {boolean|*|JitsiRemoteTrack.muted} <tt>true</tt> if the track is
  200. * muted and <tt>false</tt> otherwise.
  201. */
  202. isMuted() {
  203. return this.muted;
  204. }
  205. /**
  206. * Returns the participant id which owns the track.
  207. *
  208. * @returns {string} the id of the participants. It corresponds to the
  209. * Colibri endpoint id/MUC nickname in case of Jitsi-meet.
  210. */
  211. getParticipantId() {
  212. return this.ownerEndpointId;
  213. }
  214. /**
  215. * Return false;
  216. */
  217. isLocal() {
  218. return false;
  219. }
  220. /**
  221. * Returns the synchronization source identifier (SSRC) of this remote
  222. * track.
  223. *
  224. * @returns {number} the SSRC of this remote track.
  225. */
  226. getSSRC() {
  227. return this.ssrc;
  228. }
  229. /**
  230. * Returns the tracks source name
  231. *
  232. * @returns {string} the track's source name
  233. */
  234. getSourceName() {
  235. return this._sourceName;
  236. }
  237. /**
  238. * Update the properties when the track is remapped to another source.
  239. *
  240. * @param {string} owner The endpoint ID of the new owner.
  241. */
  242. setOwner(owner) {
  243. this.ownerEndpointId = owner;
  244. this.emit(JitsiTrackEvents.TRACK_OWNER_CHANGED, owner);
  245. }
  246. /**
  247. * Sets the name of the source associated with the remtoe track.
  248. *
  249. * @param {string} name - The source name to be associated with the track.
  250. */
  251. setSourceName(name) {
  252. this._sourceName = name;
  253. }
  254. /**
  255. * Changes the video type of the track.
  256. *
  257. * @param {string} type - The new video type("camera", "desktop").
  258. */
  259. _setVideoType(type) {
  260. if (this.videoType === type) {
  261. return;
  262. }
  263. this.videoType = type;
  264. this.emit(JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED, type);
  265. }
  266. /**
  267. * Handles track play events.
  268. */
  269. _playCallback() {
  270. if (!this.conference.room) {
  271. return;
  272. }
  273. const type = this.isVideoTrack() ? 'video' : 'audio';
  274. const now = window.performance.now();
  275. logger.info(`(TIME) Render ${type}:\t`, now);
  276. this.conference.getConnectionTimes()[`${type}.render`] = now;
  277. // The conference can be started without calling GUM
  278. // FIXME if there would be a module for connection times this kind
  279. // of logic (gumDuration or ttfm) should end up there
  280. const gumStart = window.connectionTimes['obtainPermissions.start'];
  281. const gumEnd = window.connectionTimes['obtainPermissions.end'];
  282. const gumDuration
  283. = !isNaN(gumEnd) && !isNaN(gumStart) ? gumEnd - gumStart : 0;
  284. // Subtract the muc.joined-to-session-initiate duration because jicofo
  285. // waits until there are 2 participants to start Jingle sessions.
  286. const ttfm = now
  287. - (this.conference.getConnectionTimes()['session.initiate']
  288. - this.conference.getConnectionTimes()['muc.joined'])
  289. - gumDuration;
  290. this.conference.getConnectionTimes()[`${type}.ttfm`] = ttfm;
  291. logger.info(`(TIME) TTFM ${type}:\t`, ttfm);
  292. Statistics.sendAnalytics(createTtfmEvent(
  293. {
  294. 'media_type': type,
  295. muted: this.hasBeenMuted,
  296. value: ttfm
  297. }));
  298. }
  299. /**
  300. * Attach time to first media tracker only if there is conference and only
  301. * for the first element.
  302. * @param container the HTML container which can be 'video' or 'audio'
  303. * element.
  304. * @private
  305. */
  306. _attachTTFMTracker(container) {
  307. if ((ttfmTrackerAudioAttached && this.isAudioTrack())
  308. || (ttfmTrackerVideoAttached && this.isVideoTrack())) {
  309. return;
  310. }
  311. if (this.isAudioTrack()) {
  312. ttfmTrackerAudioAttached = true;
  313. }
  314. if (this.isVideoTrack()) {
  315. ttfmTrackerVideoAttached = true;
  316. }
  317. container.addEventListener('canplay', this._playCallback.bind(this));
  318. }
  319. /**
  320. * Called when the track has been attached to a new container.
  321. *
  322. * @param {HTMLElement} container the HTML container which can be 'video' or 'audio' element.
  323. * @private
  324. */
  325. _onTrackAttach(container) {
  326. containerEvents.forEach(event => {
  327. container.addEventListener(event, this._containerHandlers[event]);
  328. });
  329. }
  330. /**
  331. * Called when the track has been detached from a container.
  332. *
  333. * @param {HTMLElement} container the HTML container which can be 'video' or 'audio' element.
  334. * @private
  335. */
  336. _onTrackDetach(container) {
  337. containerEvents.forEach(event => {
  338. container.removeEventListener(event, this._containerHandlers[event]);
  339. });
  340. }
  341. /**
  342. * An event handler for events triggered by the attached container.
  343. *
  344. * @param {string} type - The type of the event.
  345. */
  346. _containerEventHandler(type) {
  347. logger.debug(`${type} handler was called for a container with attached ${this}`);
  348. }
  349. /**
  350. * Returns a string with a description of the current status of the track.
  351. *
  352. * @returns {string}
  353. */
  354. _getStatus() {
  355. const { enabled, muted, readyState } = this.track;
  356. return `readyState: ${readyState}, muted: ${muted}, enabled: ${enabled}`;
  357. }
  358. /**
  359. * Initializes trackStreamingStatusImpl.
  360. */
  361. _initTrackStreamingStatus() {
  362. const config = this.conference.options.config;
  363. this._trackStreamingStatus = TrackStreamingStatus.ACTIVE;
  364. this._trackStreamingStatusImpl = new TrackStreamingStatusImpl(
  365. this.rtc,
  366. this.conference,
  367. this,
  368. {
  369. // These options are not public API, leaving it here only as an entry point through config for
  370. // tuning up purposes. Default values should be adjusted as soon as optimal values are discovered.
  371. p2pRtcMuteTimeout: config._p2pConnStatusRtcMuteTimeout,
  372. rtcMuteTimeout: config._peerConnStatusRtcMuteTimeout,
  373. outOfForwardedSourcesTimeout: config._peerConnStatusOutOfLastNTimeout
  374. });
  375. this._trackStreamingStatusImpl.init();
  376. // In some edge cases, both browser 'unmute' and bridge's forwarded sources events are received before a
  377. // LargeVideoUpdate is scheduled for auto-pinning a new screenshare track. If there are no layout changes and
  378. // no further track events are received for the SS track, a black tile will be displayed for screenshare on
  379. // stage. Fire a TRACK_STREAMING_STATUS_CHANGED event if the media is already being received for the remote
  380. // track to prevent this from happening.
  381. !this._trackStreamingStatusImpl.isVideoTrackFrozen()
  382. && this.rtc.eventEmitter.emit(
  383. JitsiTrackEvents.TRACK_STREAMING_STATUS_CHANGED,
  384. this,
  385. this._trackStreamingStatus);
  386. }
  387. /**
  388. * Disposes trackStreamingStatusImpl and clears trackStreamingStatus.
  389. */
  390. _disposeTrackStreamingStatus() {
  391. if (this._trackStreamingStatusImpl) {
  392. this._trackStreamingStatusImpl.dispose();
  393. this._trackStreamingStatusImpl = null;
  394. this._trackStreamingStatus = null;
  395. }
  396. }
  397. /**
  398. * Updates track's streaming status.
  399. *
  400. * @param {string} state the current track streaming state. {@link TrackStreamingStatus}.
  401. */
  402. _setTrackStreamingStatus(status) {
  403. this._trackStreamingStatus = status;
  404. }
  405. /**
  406. * Returns track's streaming status.
  407. *
  408. * @returns {string} the streaming status <tt>TrackStreamingStatus</tt> of the track. Returns null
  409. * if trackStreamingStatusImpl hasn't been initialized.
  410. *
  411. * {@link TrackStreamingStatus}.
  412. */
  413. getTrackStreamingStatus() {
  414. return this._trackStreamingStatus;
  415. }
  416. /**
  417. * Clears the timestamp of when the track entered forwarded sources.
  418. */
  419. _clearEnteredForwardedSourcesTimestamp() {
  420. this._enteredForwardedSourcesTimestamp = null;
  421. }
  422. /**
  423. * Updates the timestamp of when the track entered forwarded sources.
  424. *
  425. * @param {number} timestamp the time in millis
  426. */
  427. _setEnteredForwardedSourcesTimestamp(timestamp) {
  428. this._enteredForwardedSourcesTimestamp = timestamp;
  429. }
  430. /**
  431. * Returns the timestamp of when the track entered forwarded sources.
  432. *
  433. * @returns {number} the time in millis
  434. */
  435. _getEnteredForwardedSourcesTimestamp() {
  436. return this._enteredForwardedSourcesTimestamp;
  437. }
  438. /**
  439. * Creates a text representation of this remote track instance.
  440. * @return {string}
  441. */
  442. toString() {
  443. return `RemoteTrack[userID: ${this.getParticipantId()}, type: ${this.getType()}, ssrc: ${
  444. this.getSSRC()}, p2p: ${this.isP2P}, sourceName: ${this._sourceName}, status: {${this._getStatus()}}]`;
  445. }
  446. }