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. * @exportDoc
  21. * @summary A set of utility functions for dealing with TimeRanges objects.
  22. */
  23. /**
  24. * Gets the first timestamp in buffer.
  25. *
  26. * @param {TimeRanges} b
  27. * @return {?number} The first buffered timestamp, in seconds, if |buffered|
  28. * is non-empty; otherwise, return null.
  29. */
  30. shaka.media.TimeRangesUtils.bufferStart = function(b) {
  31. if (!b) return null;
  32. return b.length ? b.start(0) : null;
  33. };
  34. /**
  35. * Gets the last timestamp in buffer.
  36. *
  37. * @param {TimeRanges} b
  38. * @return {?number} The last buffered timestamp, in seconds, if |buffered|
  39. * is non-empty; otherwise, return null.
  40. */
  41. shaka.media.TimeRangesUtils.bufferEnd = function(b) {
  42. if (!b) return null;
  43. return b.length ? b.end(b.length - 1) : null;
  44. };
  45. /**
  46. * Computes how far ahead of the given timestamp is buffered.
  47. *
  48. * @param {TimeRanges} buffered
  49. * @param {number} time
  50. * @return {number} The number of seconds buffered, in seconds, ahead of the
  51. * given time.
  52. */
  53. shaka.media.TimeRangesUtils.bufferedAheadOf = function(
  54. buffered, time) {
  55. if (!buffered) return 0;
  56. // NOTE: On IE11, buffered ranges may show appended data before the associated
  57. // append operation is complete.
  58. var fudge = 0.0001; // 0.1ms
  59. // NOTE: The 0.1ms fudge is needed on Safari, where removal up to X may leave
  60. // a range which starts at X + 1us + some small epsilon.
  61. for (var i = 0; i < buffered.length; ++i) {
  62. if (time + fudge >= buffered.start(i) && time < buffered.end(i)) {
  63. return buffered.end(i) - time;
  64. }
  65. }
  66. return 0;
  67. };