Source: lib/media/time_ranges_utils.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.media.TimeRangesUtils');
  18. /**
  19. * @namespace shaka.media.TimeRangesUtils
  20. * @summary A set of utility functions for dealing with TimeRanges objects.
  21. */
  22. /**
  23. * Gets the first timestamp in the buffer.
  24. *
  25. * @param {TimeRanges} b
  26. * @return {?number} The first buffered timestamp, in seconds, if |buffered|
  27. * is non-empty; otherwise, return null.
  28. */
  29. shaka.media.TimeRangesUtils.bufferStart = function(b) {
  30. if (!b) return null;
  31. // Workaround Safari bug: https://goo.gl/EDRCoZ
  32. if (b.length == 1 && b.end(0) - b.start(0) < 1e-6) return null;
  33. // Workaround Edge bug: https://goo.gl/BtxKgb
  34. if (b.length == 1 && b.start(0) < 0) return 0;
  35. return b.length ? b.start(0) : null;
  36. };
  37. /**
  38. * Gets the last timestamp in the buffer.
  39. *
  40. * @param {TimeRanges} b
  41. * @return {?number} The last buffered timestamp, in seconds, if |buffered|
  42. * is non-empty; otherwise, return null.
  43. */
  44. shaka.media.TimeRangesUtils.bufferEnd = function(b) {
  45. if (!b) return null;
  46. // Workaround Safari bug: https://goo.gl/EDRCoZ
  47. if (b.length == 1 && b.end(0) - b.start(0) < 1e-6) return null;
  48. return b.length ? b.end(b.length - 1) : null;
  49. };
  50. /**
  51. * Determines if the given time is inside a buffered range. This includes gaps,
  52. * meaning that if the playhead is in a gap, it is considered buffered. If there
  53. * is a small gap between the playhead and buffer start, consider it as
  54. * buffered.
  55. *
  56. * @param {TimeRanges} b
  57. * @param {number} time Playhead time
  58. * @param {number=} smallGapLimit Set in configuration
  59. * @return {boolean}
  60. */
  61. shaka.media.TimeRangesUtils.isBuffered = function(b, time, smallGapLimit = 0) {
  62. if (!b || !b.length) return false;
  63. // Workaround Safari bug: https://goo.gl/EDRCoZ
  64. if (b.length == 1 && b.end(0) - b.start(0) < 1e-6) return false;
  65. if (time > b.end(b.length - 1)) {
  66. return false;
  67. }
  68. // Push the time forward by the gap limit so that it is more likely to be in
  69. // the range.
  70. return (time + smallGapLimit >= b.start(0));
  71. };
  72. /**
  73. * Computes how far ahead of the given timestamp is buffered. To provide smooth
  74. * playback while jumping gaps, we don't include the gaps when calculating this.
  75. * This only includes the amount of content that is buffered.
  76. *
  77. * @param {TimeRanges} b
  78. * @param {number} time
  79. * @return {number} The number of seconds buffered, in seconds, ahead of the
  80. * given time.
  81. */
  82. shaka.media.TimeRangesUtils.bufferedAheadOf = function(b, time) {
  83. if (!b || !b.length) return 0;
  84. // Workaround Safari bug: https://goo.gl/EDRCoZ
  85. if (b.length == 1 && b.end(0) - b.start(0) < 1e-6) return 0;
  86. // NOTE: On IE11, buffered ranges may show appended data before the associated
  87. // append operation is complete.
  88. // We calculate the buffered amount by ONLY accounting for the content
  89. // buffered (i.e. we ignore the times of the gaps). We also buffer through
  90. // all gaps.
  91. // Therefore, we start at the end and add up all buffers until |time|.
  92. let result = 0;
  93. for (let i = b.length - 1; i >= 0 && b.end(i) > time; --i) {
  94. result += b.end(i) - Math.max(b.start(i), time);
  95. }
  96. return result;
  97. };
  98. /**
  99. * Determines if the given time is inside a gap between buffered ranges. If it
  100. * is, this returns the index of the buffer that is *ahead* of the gap.
  101. *
  102. * @param {TimeRanges} b
  103. * @param {number} time
  104. * @return {?number} The index of the buffer after the gap, or null if not in a
  105. * gap.
  106. */
  107. shaka.media.TimeRangesUtils.getGapIndex = function(b, time) {
  108. if (!b || !b.length) return null;
  109. // Workaround Safari bug: https://goo.gl/EDRCoZ
  110. if (b.length == 1 && b.end(0) - b.start(0) < 1e-6) return null;
  111. // IE/Edge stops 0.5 seconds before a gap, so it needs a much larger
  112. // threshold, but we don't want to punish other browsers that stop closer.
  113. // See: https://goo.gl/cuAcYd
  114. let threshold = 0.1;
  115. if (/(Edge\/|Trident\/|Tizen|CrKey)/.test(navigator.userAgent)) {
  116. threshold = 0.5;
  117. }
  118. for (let i = 0; i < b.length; i++) {
  119. if (b.start(i) > time && (i == 0 || b.end(i - 1) - time <= threshold)) {
  120. return i;
  121. }
  122. }
  123. return null;
  124. };
  125. /**
  126. * @param {TimeRanges} b
  127. * @return {!Array.<shakaExtern.BufferedRange>}
  128. */
  129. shaka.media.TimeRangesUtils.getBufferedInfo = function(b) {
  130. if (!b) return [];
  131. let ret = [];
  132. for (let i = 0; i < b.length; i++) {
  133. ret.push({start: b.start(i), end: b.end(i)});
  134. }
  135. return ret;
  136. };