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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 {number} 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. * @throws {TypeError} if <tt>ssrc</tt> is not a number.
  26. * @constructor
  27. */
  28. export default function JitsiRemoteTrack(
  29. rtc,
  30. conference,
  31. ownerEndpointId,
  32. stream,
  33. track,
  34. mediaType,
  35. videoType,
  36. ssrc,
  37. muted,
  38. isP2P) {
  39. JitsiTrack.call(
  40. this,
  41. conference,
  42. stream,
  43. track,
  44. () => {
  45. // Nothing to do if the track is inactive.
  46. },
  47. mediaType,
  48. videoType);
  49. this.rtc = rtc;
  50. // Prevent from mixing up type of SSRC which should be a number
  51. if (typeof ssrc !== 'number') {
  52. throw new TypeError(`SSRC ${ssrc} is not a number`);
  53. }
  54. this.ssrc = ssrc;
  55. this.ownerEndpointId = ownerEndpointId;
  56. this.muted = muted;
  57. this.isP2P = isP2P;
  58. // we want to mark whether the track has been ever muted
  59. // to detect ttfm events for startmuted conferences, as it can significantly
  60. // increase ttfm values
  61. this.hasBeenMuted = muted;
  62. // Bind 'onmute' and 'onunmute' event handlers
  63. if (this.rtc && this.track) {
  64. this._bindMuteHandlers();
  65. }
  66. }
  67. /* eslint-enable max-params */
  68. JitsiRemoteTrack.prototype = Object.create(JitsiTrack.prototype);
  69. JitsiRemoteTrack.prototype.constructor = JitsiRemoteTrack;
  70. JitsiRemoteTrack.prototype._bindMuteHandlers = function() {
  71. // Use feature detection for finding what event handling function is
  72. // supported. On Internet Explorer, which uses uses temasys/firebreath, the
  73. // track will have attachEvent instead of addEventListener.
  74. //
  75. // FIXME it would be better to use recently added '_setHandler' method, but
  76. // 1. It does not allow to set more than one handler to the event
  77. // 2. It does mix MediaStream('inactive') with MediaStreamTrack events
  78. // 3. Allowing to bind more than one event handler requires too much
  79. // refactoring around camera issues detection.
  80. if (this.track.addEventListener) {
  81. this.track.addEventListener('mute', () => this._onTrackMute());
  82. this.track.addEventListener('unmute', () => this._onTrackUnmute());
  83. } else if (this.track.attachEvent) {
  84. // FIXME Internet Explorer is not emitting out mute/unmute events.
  85. this.track.attachEvent('onmute', () => this._onTrackMute());
  86. this.track.attachEvent('onunmute', () => this._onTrackUnmute());
  87. }
  88. };
  89. /**
  90. * Callback invoked when the track is muted. Emits an event notifying listeners
  91. * of the mute event.
  92. *
  93. * @private
  94. * @returns {void}
  95. */
  96. JitsiRemoteTrack.prototype._onTrackMute = function() {
  97. logger.debug(
  98. `"onmute" event(${Date.now()}): `,
  99. this.getParticipantId(), this.getType(), this.getSSRC());
  100. this.rtc.eventEmitter.emit(RTCEvents.REMOTE_TRACK_MUTE, this);
  101. };
  102. /**
  103. * Callback invoked when the track is unmuted. Emits an event notifying
  104. * listeners of the mute event.
  105. *
  106. * @private
  107. * @returns {void}
  108. */
  109. JitsiRemoteTrack.prototype._onTrackUnmute = function() {
  110. logger.debug(
  111. `"onunmute" event(${Date.now()}): `,
  112. this.getParticipantId(), this.getType(), this.getSSRC());
  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. JitsiRemoteTrack.prototype.setMute = function(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.eventEmitter.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. JitsiRemoteTrack.prototype.isMuted = function() {
  139. return this.muted;
  140. };
  141. /**
  142. * Returns the participant id which owns the track.
  143. * @returns {string} the id of the participants. It corresponds to the Colibri
  144. * endpoint id/MUC nickname in case of Jitsi-meet.
  145. */
  146. JitsiRemoteTrack.prototype.getParticipantId = function() {
  147. return this.ownerEndpointId;
  148. };
  149. /**
  150. * Return false;
  151. */
  152. JitsiRemoteTrack.prototype.isLocal = function() {
  153. return false;
  154. };
  155. /**
  156. * Returns the synchronization source identifier (SSRC) of this remote track.
  157. * @returns {number} the SSRC of this remote track
  158. */
  159. JitsiRemoteTrack.prototype.getSSRC = function() {
  160. return this.ssrc;
  161. };
  162. /**
  163. * Changes the video type of the track
  164. * @param type the new video type("camera", "desktop")
  165. */
  166. JitsiRemoteTrack.prototype._setVideoType = function(type) {
  167. if (this.videoType === type) {
  168. return;
  169. }
  170. this.videoType = type;
  171. this.eventEmitter.emit(JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED, type);
  172. };
  173. JitsiRemoteTrack.prototype._playCallback = function() {
  174. const type = this.isVideoTrack() ? 'video' : 'audio';
  175. const now = window.performance.now();
  176. console.log(`(TIME) Render ${type}:\t`, now);
  177. this.conference.getConnectionTimes()[`${type}.render`] = now;
  178. const ttfm = now
  179. - (this.conference.getConnectionTimes()['session.initiate']
  180. - this.conference.getConnectionTimes()['muc.joined'])
  181. - (window.connectionTimes['obtainPermissions.end']
  182. - window.connectionTimes['obtainPermissions.start']);
  183. this.conference.getConnectionTimes()[`${type}.ttfm`] = ttfm;
  184. console.log(`(TIME) TTFM ${type}:\t`, ttfm);
  185. let eventName = `${type}.ttfm`;
  186. if (this.hasBeenMuted) {
  187. eventName += '.muted';
  188. }
  189. Statistics.analytics.sendEvent(eventName, { value: ttfm });
  190. };
  191. /**
  192. * Attach time to first media tracker only if there is conference and only
  193. * for the first element.
  194. * @param container the HTML container which can be 'video' or 'audio' element.
  195. * It can also be 'object' element if Temasys plugin is in use and this
  196. * method has been called previously on video or audio HTML element.
  197. * @private
  198. */
  199. JitsiRemoteTrack.prototype._attachTTFMTracker = function(container) {
  200. if ((ttfmTrackerAudioAttached && this.isAudioTrack())
  201. || (ttfmTrackerVideoAttached && this.isVideoTrack())) {
  202. return;
  203. }
  204. if (this.isAudioTrack()) {
  205. ttfmTrackerAudioAttached = true;
  206. }
  207. if (this.isVideoTrack()) {
  208. ttfmTrackerVideoAttached = true;
  209. }
  210. if (RTCBrowserType.isTemasysPluginUsed()) {
  211. // XXX Don't require Temasys unless it's to be used because it doesn't
  212. // run on React Native, for example.
  213. const AdapterJS = require('./adapter.screenshare');
  214. // FIXME: this is not working for IE11
  215. AdapterJS.addEvent(container, 'play', this._playCallback.bind(this));
  216. } else {
  217. container.addEventListener('canplay', this._playCallback.bind(this));
  218. }
  219. };
  220. /**
  221. * Creates a text representation of this remote track instance.
  222. * @return {string}
  223. */
  224. JitsiRemoteTrack.prototype.toString = function() {
  225. return `RemoteTrack[${this.ownerEndpointId}, ${this.getType()
  226. }, p2p: ${this.isP2P}]`;
  227. };