Source: lib/util/periods.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.util.Periods');
  18. /**
  19. * This is a collection of period-focused utility methods.
  20. *
  21. * @final
  22. */
  23. shaka.util.Periods = class {
  24. /**
  25. * Get all the variants across all periods.
  26. *
  27. * @param {!Iterable.<shaka.extern.Period>} periods
  28. * @return {!Array.<shaka.extern.Variant>}
  29. */
  30. static getAllVariantsFrom(periods) {
  31. const found = [];
  32. for (const period of periods) {
  33. for (const variant of period.variants) {
  34. found.push(variant);
  35. }
  36. }
  37. return found;
  38. }
  39. /**
  40. * Find our best guess at which period contains the given time. If
  41. * |timeInSeconds| starts before the first period, then |null| will be
  42. * returned.
  43. *
  44. * @param {!Iterable.<shaka.extern.Period>} periods
  45. * @param {number} timeInSeconds
  46. * @return {?shaka.extern.Period}
  47. */
  48. static findPeriodForTime(periods, timeInSeconds) {
  49. let bestGuess = null;
  50. // Go period-by-period and see if the period started before our current
  51. // time. If so, we could be in that period. Since periods are supposed to be
  52. // in order by start time, we can allow later periods to override our best
  53. // guess.
  54. for (const period of periods) {
  55. if (timeInSeconds >= period.startTime) {
  56. bestGuess = period;
  57. }
  58. }
  59. return bestGuess;
  60. }
  61. };