modified lib-jitsi-meet dev repo
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JitsiRemoteTrack.js 7.3KB

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