Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

JitsiRemoteTrack.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import JitsiTrack from './JitsiTrack';
  2. import * as JitsiTrackEvents from '../../JitsiTrackEvents';
  3. import RTCBrowserType from './RTCBrowserType';
  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. * @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. export default 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. // Use feature detection for finding what event handling function is
  67. // supported. On Internet Explorer, which uses uses temasys/firebreath, the
  68. // track will have attachEvent instead of addEventListener.
  69. //
  70. // FIXME it would be better to use recently added '_setHandler' method, but
  71. // 1. It does not allow to set more than one handler to the event
  72. // 2. It does mix MediaStream('inactive') with MediaStreamTrack events
  73. // 3. Allowing to bind more than one event handler requires too much
  74. // refactoring around camera issues detection.
  75. if (this.track.addEventListener) {
  76. this.track.addEventListener('mute', () => this._onTrackMute());
  77. this.track.addEventListener('unmute', () => this._onTrackUnmute());
  78. } else if (this.track.attachEvent) {
  79. // FIXME Internet Explorer is not emitting out mute/unmute events.
  80. this.track.attachEvent('onmute', () => this._onTrackMute());
  81. this.track.attachEvent('onunmute', () => this._onTrackUnmute());
  82. }
  83. };
  84. /**
  85. * Callback invoked when the track is muted. Emits an event notifying listeners
  86. * of the mute event.
  87. *
  88. * @private
  89. * @returns {void}
  90. */
  91. JitsiRemoteTrack.prototype._onTrackMute = function() {
  92. logger.debug(
  93. `"onmute" event(${Date.now()}): `,
  94. this.getParticipantId(), this.getType(), this.getSSRC());
  95. this.rtc.eventEmitter.emit(RTCEvents.REMOTE_TRACK_MUTE, this);
  96. };
  97. /**
  98. * Callback invoked when the track is unmuted. Emits an event notifying
  99. * listeners of the mute event.
  100. *
  101. * @private
  102. * @returns {void}
  103. */
  104. JitsiRemoteTrack.prototype._onTrackUnmute = function() {
  105. logger.debug(
  106. `"onunmute" event(${Date.now()}): `,
  107. this.getParticipantId(), this.getType(), this.getSSRC());
  108. this.rtc.eventEmitter.emit(RTCEvents.REMOTE_TRACK_UNMUTE, this);
  109. };
  110. /**
  111. * Sets current muted status and fires an events for the change.
  112. * @param value the muted status.
  113. */
  114. JitsiRemoteTrack.prototype.setMute = function(value) {
  115. if (this.muted === value) {
  116. return;
  117. }
  118. if (value) {
  119. this.hasBeenMuted = true;
  120. }
  121. // we can have a fake video stream
  122. if (this.stream) {
  123. this.stream.muted = value;
  124. }
  125. this.muted = value;
  126. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED, this);
  127. };
  128. /**
  129. * Returns the current muted status of the track.
  130. * @returns {boolean|*|JitsiRemoteTrack.muted} <tt>true</tt> if the track is
  131. * muted and <tt>false</tt> otherwise.
  132. */
  133. JitsiRemoteTrack.prototype.isMuted = function() {
  134. return this.muted;
  135. };
  136. /**
  137. * Returns the participant id which owns the track.
  138. * @returns {string} the id of the participants. It corresponds to the Colibri
  139. * endpoint id/MUC nickname in case of Jitsi-meet.
  140. */
  141. JitsiRemoteTrack.prototype.getParticipantId = function() {
  142. return this.ownerEndpointId;
  143. };
  144. /**
  145. * Return false;
  146. */
  147. JitsiRemoteTrack.prototype.isLocal = function() {
  148. return false;
  149. };
  150. /**
  151. * Returns the synchronization source identifier (SSRC) of this remote track.
  152. * @returns {string} the SSRC of this remote track
  153. */
  154. JitsiRemoteTrack.prototype.getSSRC = function() {
  155. return this.ssrc;
  156. };
  157. /**
  158. * Changes the video type of the track
  159. * @param type the new video type("camera", "desktop")
  160. */
  161. JitsiRemoteTrack.prototype._setVideoType = function(type) {
  162. if (this.videoType === type) {
  163. return;
  164. }
  165. this.videoType = type;
  166. this.eventEmitter.emit(JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED, type);
  167. };
  168. JitsiRemoteTrack.prototype._playCallback = function() {
  169. const type = this.isVideoTrack() ? 'video' : 'audio';
  170. const now = window.performance.now();
  171. console.log(`(TIME) Render ${type}:\t`, now);
  172. this.conference.getConnectionTimes()[`${type}.render`] = now;
  173. const ttfm = now
  174. - (this.conference.getConnectionTimes()['session.initiate']
  175. - this.conference.getConnectionTimes()['muc.joined'])
  176. - (window.connectionTimes['obtainPermissions.end']
  177. - window.connectionTimes['obtainPermissions.start']);
  178. this.conference.getConnectionTimes()[`${type}.ttfm`] = ttfm;
  179. console.log(`(TIME) TTFM ${type}:\t`, ttfm);
  180. let eventName = `${type}.ttfm`;
  181. if (this.hasBeenMuted) {
  182. eventName += '.muted';
  183. }
  184. Statistics.analytics.sendEvent(eventName, { value: ttfm });
  185. };
  186. /**
  187. * Attach time to first media tracker only if there is conference and only
  188. * for the first element.
  189. * @param container the HTML container which can be 'video' or 'audio' element.
  190. * It can also be 'object' element if Temasys plugin is in use and this
  191. * method has been called previously on video or audio HTML element.
  192. * @private
  193. */
  194. JitsiRemoteTrack.prototype._attachTTFMTracker = function(container) {
  195. if ((ttfmTrackerAudioAttached && this.isAudioTrack())
  196. || (ttfmTrackerVideoAttached && this.isVideoTrack())) {
  197. return;
  198. }
  199. if (this.isAudioTrack()) {
  200. ttfmTrackerAudioAttached = true;
  201. }
  202. if (this.isVideoTrack()) {
  203. ttfmTrackerVideoAttached = true;
  204. }
  205. if (RTCBrowserType.isTemasysPluginUsed()) {
  206. // XXX Don't require Temasys unless it's to be used because it doesn't
  207. // run on React Native, for example.
  208. const AdapterJS = require('./adapter.screenshare');
  209. // FIXME: this is not working for IE11
  210. AdapterJS.addEvent(container, 'play', this._playCallback.bind(this));
  211. } else {
  212. container.addEventListener('canplay', this._playCallback.bind(this));
  213. }
  214. };
  215. /**
  216. * Creates a text representation of this remote track instance.
  217. * @return {string}
  218. */
  219. JitsiRemoteTrack.prototype.toString = function() {
  220. return `RemoteTrack[${this.ownerEndpointId}, ${this.getType()
  221. }, p2p: ${this.isP2P}]`;
  222. };