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

JitsiRemoteTrack.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* global Strophe */
  2. var JitsiTrack = require("./JitsiTrack");
  3. import * as JitsiTrackEvents from "../../JitsiTrackEvents";
  4. var logger = require("jitsi-meet-logger").getLogger(__filename);
  5. var RTCBrowserType = require("./RTCBrowserType");
  6. var RTCEvents = require("../../service/RTC/RTCEvents");
  7. var Statistics = require("../statistics/statistics");
  8. var AdapterJS = require("./adapter.screenshare");
  9. var ttfmTrackerAudioAttached = false;
  10. var ttfmTrackerVideoAttached = false;
  11. /**
  12. * Represents a single media track (either audio or video).
  13. * @param rtc {RTC} the RTC service instance.
  14. * @param ownerJid the MUC JID of the track owner
  15. * @param stream WebRTC MediaStream, parent of the track
  16. * @param track underlying WebRTC MediaStreamTrack for new JitsiRemoteTrack
  17. * @param mediaType the MediaType of the JitsiRemoteTrack
  18. * @param videoType the VideoType of the JitsiRemoteTrack
  19. * @param ssrc the SSRC number of the Media Stream
  20. * @param muted intial muted state of the JitsiRemoteTrack
  21. * @constructor
  22. */
  23. function JitsiRemoteTrack(rtc, conference, ownerJid, stream, track, mediaType, videoType,
  24. ssrc, muted) {
  25. JitsiTrack.call(
  26. this, conference, stream, track, function () {}, mediaType, videoType, ssrc);
  27. this.rtc = rtc;
  28. this.peerjid = ownerJid;
  29. this.muted = muted;
  30. // we want to mark whether the track has been ever muted
  31. // to detect ttfm events for startmuted conferences, as it can significantly
  32. // increase ttfm values
  33. this.hasBeenMuted = muted;
  34. // Bind 'onmute' and 'onunmute' event handlers
  35. if (this.rtc && this.track)
  36. this._bindMuteHandlers();
  37. }
  38. JitsiRemoteTrack.prototype = Object.create(JitsiTrack.prototype);
  39. JitsiRemoteTrack.prototype.constructor = JitsiRemoteTrack;
  40. JitsiRemoteTrack.prototype._bindMuteHandlers = function() {
  41. // Bind 'onmute'
  42. // FIXME it would be better to use recently added '_setHandler' method, but
  43. // 1. It does not allow to set more than one handler to the event
  44. // 2. It does mix MediaStream('inactive') with MediaStreamTrack events
  45. // 3. Allowing to bind more than one event handler requires too much
  46. // refactoring around camera issues detection.
  47. this.track.addEventListener('mute', function () {
  48. logger.debug(
  49. '"onmute" event(' + Date.now() + '): ',
  50. this.getParticipantId(), this.getType(), this.getSSRC());
  51. this.rtc.eventEmitter.emit(RTCEvents.REMOTE_TRACK_MUTE, this);
  52. }.bind(this));
  53. // Bind 'onunmute'
  54. this.track.addEventListener('unmute', function () {
  55. logger.debug(
  56. '"onunmute" event(' + Date.now() + '): ',
  57. this.getParticipantId(), this.getType(), this.getSSRC());
  58. this.rtc.eventEmitter.emit(RTCEvents.REMOTE_TRACK_UNMUTE, this);
  59. }.bind(this));
  60. };
  61. /**
  62. * Sets current muted status and fires an events for the change.
  63. * @param value the muted status.
  64. */
  65. JitsiRemoteTrack.prototype.setMute = function (value) {
  66. if(this.muted === value)
  67. return;
  68. if(value)
  69. this.hasBeenMuted = true;
  70. // we can have a fake video stream
  71. if(this.stream)
  72. this.stream.muted = value;
  73. this.muted = value;
  74. this.eventEmitter.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED, this);
  75. };
  76. /**
  77. * Returns the current muted status of the track.
  78. * @returns {boolean|*|JitsiRemoteTrack.muted} <tt>true</tt> if the track is
  79. * muted and <tt>false</tt> otherwise.
  80. */
  81. JitsiRemoteTrack.prototype.isMuted = function () {
  82. return this.muted;
  83. };
  84. /**
  85. * Returns the participant id which owns the track.
  86. * @returns {string} the id of the participants.
  87. */
  88. JitsiRemoteTrack.prototype.getParticipantId = function() {
  89. return Strophe.getResourceFromJid(this.peerjid);
  90. };
  91. /**
  92. * Return false;
  93. */
  94. JitsiRemoteTrack.prototype.isLocal = function () {
  95. return false;
  96. };
  97. /**
  98. * Returns the synchronization source identifier (SSRC) of this remote track.
  99. * @returns {string} the SSRC of this remote track
  100. */
  101. JitsiRemoteTrack.prototype.getSSRC = function () {
  102. return this.ssrc;
  103. };
  104. /**
  105. * Changes the video type of the track
  106. * @param type the new video type("camera", "desktop")
  107. */
  108. JitsiRemoteTrack.prototype._setVideoType = function (type) {
  109. if(this.videoType === type)
  110. return;
  111. this.videoType = type;
  112. this.eventEmitter.emit(JitsiTrackEvents.TRACK_VIDEOTYPE_CHANGED, type);
  113. };
  114. JitsiRemoteTrack.prototype._playCallback = function () {
  115. var type = (this.isVideoTrack() ? 'video' : 'audio');
  116. var now = window.performance.now();
  117. console.log("(TIME) Render " + type + ":\t", now);
  118. this.conference.getConnectionTimes()[type + ".render"] = now;
  119. var ttfm = now
  120. - (this.conference.getConnectionTimes()["session.initiate"]
  121. - this.conference.getConnectionTimes()["muc.joined"])
  122. - (window.connectionTimes["obtainPermissions.end"]
  123. - window.connectionTimes["obtainPermissions.start"]);
  124. this.conference.getConnectionTimes()[type + ".ttfm"] = ttfm;
  125. console.log("(TIME) TTFM " + type + ":\t", ttfm);
  126. var eventName = type +'.ttfm';
  127. if(this.hasBeenMuted)
  128. eventName += '.muted';
  129. Statistics.analytics.sendEvent(eventName, ttfm);
  130. };
  131. /**
  132. * Attach time to first media tracker only if there is conference and only
  133. * for the first element.
  134. * @param container the HTML container which can be 'video' or 'audio' element.
  135. * It can also be 'object' element if Temasys plugin is in use and this
  136. * method has been called previously on video or audio HTML element.
  137. * @private
  138. */
  139. JitsiRemoteTrack.prototype._attachTTFMTracker = function (container) {
  140. if((ttfmTrackerAudioAttached && this.isAudioTrack())
  141. || (ttfmTrackerVideoAttached && this.isVideoTrack()))
  142. return;
  143. if (this.isAudioTrack())
  144. ttfmTrackerAudioAttached = true;
  145. if (this.isVideoTrack())
  146. ttfmTrackerVideoAttached = true;
  147. if (RTCBrowserType.isTemasysPluginUsed()) {
  148. // FIXME: this is not working for IE11
  149. AdapterJS.addEvent(container, 'play', this._playCallback.bind(this));
  150. }
  151. else {
  152. container.addEventListener("canplay", this._playCallback.bind(this));
  153. }
  154. };
  155. module.exports = JitsiRemoteTrack;