Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JitsiRemoteTrack.js 9.5KB

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