Source: lib/util/stats.js

  1. /** @license
  2. * Copyright 2016 Google LLC
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. goog.provide('shaka.util.Stats');
  6. goog.require('shaka.util.StateHistory');
  7. goog.require('shaka.util.SwitchHistory');
  8. /**
  9. * This class tracks all the various components (some optional) that are used to
  10. * populate |shaka.extern.Stats| which is passed to the app.
  11. *
  12. * @final
  13. */
  14. shaka.util.Stats = class {
  15. constructor() {
  16. /** @private {number} */
  17. this.width_ = NaN;
  18. /** @private {number} */
  19. this.height_ = NaN;
  20. /** @private {number} */
  21. this.totalDroppedFrames_ = NaN;
  22. /** @private {number} */
  23. this.totalDecodedFrames_ = NaN;
  24. /** @private {number} */
  25. this.totalCorruptedFrames_ = NaN;
  26. /** @private {number} */
  27. this.loadLatencySeconds_ = NaN;
  28. /** @private {number} */
  29. this.manifestTimeSeconds_ = NaN;
  30. /** @private {number} */
  31. this.drmTimeSeconds_ = NaN;
  32. /** @private {number} */
  33. this.licenseTimeSeconds_ = NaN;
  34. /** @private {number} */
  35. this.liveLatencySeconds_ = NaN;
  36. /** @private {number} */
  37. this.maxSegmentDurationSeconds_ = NaN;
  38. /** @private {number} */
  39. this.currentStreamBandwidth_ = NaN;
  40. /** @private {number} */
  41. this.bandwidthEstimate_ = NaN;
  42. /** @private {!shaka.util.StateHistory} */
  43. this.stateHistory_ = new shaka.util.StateHistory();
  44. /** @private {!shaka.util.SwitchHistory} */
  45. this.switchHistory_ = new shaka.util.SwitchHistory();
  46. }
  47. /**
  48. * Update the ratio of dropped frames to total frames. This will replace the
  49. * previous values.
  50. *
  51. * @param {number} dropped
  52. * @param {number} decoded
  53. */
  54. setDroppedFrames(dropped, decoded) {
  55. this.totalDroppedFrames_ = dropped;
  56. this.totalDecodedFrames_ = decoded;
  57. }
  58. /**
  59. * Update corrupted frames. This will replace the previous values.
  60. *
  61. * @param {number} corrupted
  62. */
  63. setCorruptedFrames(corrupted) {
  64. this.totalCorruptedFrames_ = corrupted;
  65. }
  66. /**
  67. * Set the width and height of the video we are currently playing.
  68. *
  69. * @param {number} width
  70. * @param {number} height
  71. */
  72. setResolution(width, height) {
  73. this.width_ = width;
  74. this.height_ = height;
  75. }
  76. /**
  77. * Record the time it took between the user signalling "I want to play this"
  78. * to "I am now seeing this".
  79. *
  80. * @param {number} seconds
  81. */
  82. setLoadLatency(seconds) {
  83. this.loadLatencySeconds_ = seconds;
  84. }
  85. /**
  86. * Record the time it took to download and parse the manifest.
  87. *
  88. * @param {number} seconds
  89. */
  90. setManifestTime(seconds) {
  91. this.manifestTimeSeconds_ = seconds;
  92. }
  93. /**
  94. * Record the time it took to download the first drm key.
  95. *
  96. * @param {number} seconds
  97. */
  98. setDrmTime(seconds) {
  99. this.drmTimeSeconds_ = seconds;
  100. }
  101. /**
  102. * Record the cumulative time spent on license requests during this session.
  103. *
  104. * @param {number} seconds
  105. */
  106. setLicenseTime(seconds) {
  107. this.licenseTimeSeconds_ = seconds;
  108. }
  109. /**
  110. * Record the latency in live streams.
  111. *
  112. * @param {number} seconds
  113. */
  114. setLiveLatency(seconds) {
  115. this.liveLatencySeconds_ = seconds;
  116. }
  117. /**
  118. * Record the presentation's max segment duration.
  119. *
  120. * @param {number} seconds
  121. */
  122. setMaxSegmentDuration(seconds) {
  123. this.maxSegmentDurationSeconds_ = seconds;
  124. }
  125. /**
  126. * @param {number} bandwidth
  127. */
  128. setCurrentStreamBandwidth(bandwidth) {
  129. this.currentStreamBandwidth_ = bandwidth;
  130. }
  131. /**
  132. * @param {number} bandwidth
  133. */
  134. setBandwidthEstimate(bandwidth) {
  135. this.bandwidthEstimate_ = bandwidth;
  136. }
  137. /**
  138. * @return {!shaka.util.StateHistory}
  139. */
  140. getStateHistory() {
  141. return this.stateHistory_;
  142. }
  143. /**
  144. * @return {!shaka.util.SwitchHistory}
  145. */
  146. getSwitchHistory() {
  147. return this.switchHistory_;
  148. }
  149. /**
  150. * Create a stats blob that we can pass up to the app. This blob will not
  151. * reference any internal data.
  152. *
  153. * @return {shaka.extern.Stats}
  154. */
  155. getBlob() {
  156. return {
  157. width: this.width_,
  158. height: this.height_,
  159. streamBandwidth: this.currentStreamBandwidth_,
  160. decodedFrames: this.totalDecodedFrames_,
  161. droppedFrames: this.totalDroppedFrames_,
  162. corruptedFrames: this.totalCorruptedFrames_,
  163. estimatedBandwidth: this.bandwidthEstimate_,
  164. loadLatency: this.loadLatencySeconds_,
  165. manifestTimeSeconds: this.manifestTimeSeconds_,
  166. drmTimeSeconds: this.drmTimeSeconds_,
  167. playTime: this.stateHistory_.getTimeSpentIn('playing'),
  168. pauseTime: this.stateHistory_.getTimeSpentIn('paused'),
  169. bufferingTime: this.stateHistory_.getTimeSpentIn('buffering'),
  170. licenseTime: this.licenseTimeSeconds_,
  171. liveLatency: this.liveLatencySeconds_,
  172. maxSegmentDuration: this.maxSegmentDurationSeconds_,
  173. stateHistory: this.stateHistory_.getCopy(),
  174. switchHistory: this.switchHistory_.getCopy(),
  175. };
  176. }
  177. /**
  178. * Create an empty stats blob. This resembles the stats when we are not
  179. * playing any content.
  180. *
  181. * @return {shaka.extern.Stats}
  182. */
  183. static getEmptyBlob() {
  184. return {
  185. width: NaN,
  186. height: NaN,
  187. streamBandwidth: NaN,
  188. decodedFrames: NaN,
  189. droppedFrames: NaN,
  190. corruptedFrames: NaN,
  191. estimatedBandwidth: NaN,
  192. loadLatency: NaN,
  193. manifestTimeSeconds: NaN,
  194. drmTimeSeconds: NaN,
  195. playTime: NaN,
  196. pauseTime: NaN,
  197. bufferingTime: NaN,
  198. licenseTime: NaN,
  199. liveLatency: NaN,
  200. maxSegmentDuration: NaN,
  201. switchHistory: [],
  202. stateHistory: [],
  203. };
  204. }
  205. };