metrics.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* global $, events, eventLeftMargin, canvasBufferTimeRangeUpdate, canvasBufferWindowUpdate, canvasBitrateEventUpdate,
  2. canvasVideoEventUpdate, canvasLoadEventUpdate */
  3. /* eslint no-var: 0 */
  4. var windowDuration = 20000;
  5. var windowSliding = true;
  6. var windowStart = 0;
  7. var windowEnd = 10000;
  8. var windowFocus;
  9. var metricsDisplayed = false;
  10. var timeRangeMouseDown = false;
  11. $('#windowStart').val(windowStart);
  12. $('#windowEnd').val(windowEnd);
  13. function showMetrics() {
  14. metricsDisplayed = true;
  15. var width = window.innerWidth - 30;
  16. $('#bufferWindow_c')[0].width =
  17. $('#bitrateTimerange_c')[0].width =
  18. $('#bufferTimerange_c')[0].width =
  19. $('#videoEvent_c')[0].width =
  20. $('#metricsButton')[0].width =
  21. $('#loadEvent_c')[0].width =
  22. width;
  23. $('#bufferWindow_c').show();
  24. $('#bitrateTimerange_c').show();
  25. $('#bufferTimerange_c').show();
  26. $('#videoEvent_c').show();
  27. $('#metricsButton').show();
  28. $('#loadEvent_c').show();
  29. }
  30. function hideMetrics() {
  31. metricsDisplayed = false;
  32. $('#bufferWindow_c').hide();
  33. $('#bitrateTimerange_c').hide();
  34. $('#bufferTimerange_c').hide();
  35. $('#videoEvent_c').hide();
  36. $('#metricsButton').hide();
  37. $('#loadEvent_c').hide();
  38. }
  39. function timeRangeSetSliding(duration) {
  40. windowDuration = duration;
  41. windowSliding = true;
  42. refreshCanvas();
  43. }
  44. function timeRangeCanvasonMouseDown(evt) {
  45. var canvas = evt.currentTarget;
  46. var bRect = canvas.getBoundingClientRect();
  47. var mouseX = Math.round(
  48. (evt.clientX - bRect.left) * (canvas.width / bRect.width)
  49. );
  50. windowStart = Math.max(
  51. 0,
  52. Math.round(
  53. ((mouseX - eventLeftMargin) * getWindowTimeRange().now) /
  54. (canvas.width - eventLeftMargin)
  55. )
  56. );
  57. windowEnd = windowStart + 500;
  58. timeRangeMouseDown = true;
  59. windowSliding = false;
  60. // console.log('windowStart/windowEnd:' + '/' + windowStart + '/' + windowEnd);
  61. $('#windowStart').val(windowStart);
  62. $('#windowEnd').val(windowEnd);
  63. refreshCanvas();
  64. }
  65. function timeRangeCanvasonMouseMove(evt) {
  66. if (timeRangeMouseDown) {
  67. var canvas = evt.currentTarget;
  68. var bRect = canvas.getBoundingClientRect();
  69. var mouseX = Math.round(
  70. (evt.clientX - bRect.left) * (canvas.width / bRect.width)
  71. );
  72. var pos = Math.max(
  73. 0,
  74. Math.round(
  75. ((mouseX - eventLeftMargin) * getWindowTimeRange().now) /
  76. (canvas.width - eventLeftMargin)
  77. )
  78. );
  79. if (pos < windowStart) {
  80. windowStart = pos;
  81. } else {
  82. windowEnd = pos;
  83. }
  84. if (windowStart === windowEnd) {
  85. // to avoid division by zero ...
  86. windowEnd += 50;
  87. }
  88. // console.log('windowStart/windowEnd:' + '/' + windowStart + '/' + windowEnd);
  89. $('#windowStart').val(windowStart);
  90. $('#windowEnd').val(windowEnd);
  91. refreshCanvas();
  92. }
  93. }
  94. function timeRangeCanvasonMouseUp(evt) {
  95. timeRangeMouseDown = false;
  96. }
  97. function timeRangeCanvasonMouseOut(evt) {
  98. timeRangeMouseDown = false;
  99. }
  100. function windowCanvasonMouseMove(evt) {
  101. var canvas = evt.currentTarget;
  102. var bRect = canvas.getBoundingClientRect();
  103. var mouseX = Math.round(
  104. (evt.clientX - bRect.left) * (canvas.width / bRect.width)
  105. );
  106. var timeRange = getWindowTimeRange();
  107. windowFocus =
  108. timeRange.min +
  109. Math.max(
  110. 0,
  111. Math.round(
  112. ((mouseX - eventLeftMargin) * (timeRange.max - timeRange.min)) /
  113. (canvas.width - eventLeftMargin)
  114. )
  115. );
  116. // console.log(windowFocus);
  117. refreshCanvas();
  118. }
  119. function refreshCanvas() {
  120. if (metricsDisplayed) {
  121. try {
  122. var windowTime = getWindowTimeRange();
  123. canvasBufferTimeRangeUpdate(
  124. $('#bufferTimerange_c')[0],
  125. 0,
  126. windowTime.now,
  127. windowTime.min,
  128. windowTime.max,
  129. events.buffer
  130. );
  131. if (windowTime.min !== 0 || windowTime.max !== windowTime.now) {
  132. $('#bufferWindow_c').show();
  133. canvasBufferWindowUpdate(
  134. $('#bufferWindow_c')[0],
  135. windowTime.min,
  136. windowTime.max,
  137. windowTime.focus,
  138. events.buffer
  139. );
  140. } else {
  141. $('#bufferWindow_c').hide();
  142. }
  143. canvasBitrateEventUpdate(
  144. $('#bitrateTimerange_c')[0],
  145. 0,
  146. windowTime.now,
  147. windowTime.min,
  148. windowTime.max,
  149. events.level,
  150. events.bitrate
  151. );
  152. canvasVideoEventUpdate(
  153. $('#videoEvent_c')[0],
  154. windowTime.min,
  155. windowTime.max,
  156. events.video
  157. );
  158. canvasLoadEventUpdate(
  159. $('#loadEvent_c')[0],
  160. windowTime.min,
  161. windowTime.max,
  162. events.load
  163. );
  164. } catch (err) {
  165. console.log('refreshCanvas error:' + err.message);
  166. }
  167. }
  168. }
  169. function getWindowTimeRange() {
  170. var tnow;
  171. var minTime;
  172. var maxTime;
  173. if (events.buffer.length) {
  174. tnow = events.buffer[events.buffer.length - 1].time;
  175. } else {
  176. tnow = 0;
  177. }
  178. if (windowSliding) {
  179. // let's show the requested window
  180. if (windowDuration) {
  181. minTime = Math.max(0, tnow - windowDuration);
  182. maxTime = Math.min(minTime + windowDuration, tnow);
  183. } else {
  184. minTime = 0;
  185. maxTime = tnow;
  186. }
  187. } else {
  188. minTime = windowStart;
  189. maxTime = windowEnd;
  190. }
  191. if (
  192. windowFocus === undefined ||
  193. windowFocus < minTime ||
  194. windowFocus > maxTime
  195. ) {
  196. windowFocus = minTime;
  197. }
  198. return { min: minTime, max: maxTime, now: tnow, focus: windowFocus };
  199. }
  200. function timeRangeZoomIn() {
  201. if (windowSliding) {
  202. windowDuration /= 2;
  203. } else {
  204. var duration = windowEnd - windowStart;
  205. windowStart += duration / 4;
  206. windowEnd -= duration / 4;
  207. if (windowStart === windowEnd) {
  208. windowEnd += 50;
  209. }
  210. }
  211. $('#windowStart').val(windowStart);
  212. $('#windowEnd').val(windowEnd);
  213. refreshCanvas();
  214. }
  215. function timeRangeZoomOut() {
  216. if (windowSliding) {
  217. windowDuration *= 2;
  218. } else {
  219. var duration = windowEnd - windowStart;
  220. windowStart -= duration / 2;
  221. windowEnd += duration / 2;
  222. windowStart = Math.max(0, windowStart);
  223. windowEnd = Math.min(
  224. events.buffer[events.buffer.length - 1].time,
  225. windowEnd
  226. );
  227. }
  228. $('#windowStart').val(windowStart);
  229. $('#windowEnd').val(windowEnd);
  230. refreshCanvas();
  231. }
  232. function timeRangeSlideLeft() {
  233. var duration = windowEnd - windowStart;
  234. windowStart -= duration / 4;
  235. windowEnd -= duration / 4;
  236. windowStart = Math.max(0, windowStart);
  237. windowEnd = Math.min(events.buffer[events.buffer.length - 1].time, windowEnd);
  238. $('#windowStart').val(windowStart);
  239. $('#windowEnd').val(windowEnd);
  240. refreshCanvas();
  241. }
  242. function timeRangeSlideRight() {
  243. var duration = windowEnd - windowStart;
  244. windowStart += duration / 4;
  245. windowEnd += duration / 4;
  246. windowStart = Math.max(0, windowStart);
  247. windowEnd = Math.min(events.buffer[events.buffer.length - 1].time, windowEnd);
  248. $('#windowStart').val(windowStart);
  249. $('#windowEnd').val(windowEnd);
  250. refreshCanvas();
  251. }