Source: lib/util/switch_history.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.SwitchHistory');
  18. /**
  19. * This class is used to track changes in variant and text selections. This
  20. * class will make sure that redundant switches are not recorded in the history.
  21. *
  22. * @final
  23. */
  24. shaka.util.SwitchHistory = class {
  25. constructor() {
  26. /** @private {?shaka.extern.Variant} */
  27. this.currentVariant_ = null;
  28. /** @private {?shaka.extern.Stream} */
  29. this.currentText_ = null;
  30. /** @private {!Array.<shaka.extern.TrackChoice>} */
  31. this.history_ = [];
  32. }
  33. /**
  34. * Update the history to show that we are currently playing |newVariant|. If
  35. * we are already playing |newVariant|, this update will be ignored.
  36. *
  37. * @param {shaka.extern.Variant} newVariant
  38. * @param {boolean} fromAdaptation
  39. */
  40. updateCurrentVariant(newVariant, fromAdaptation) {
  41. if (this.currentVariant_ == newVariant) { return; }
  42. this.currentVariant_ = newVariant;
  43. this.history_.push({
  44. timestamp: this.getNowInSeconds_(),
  45. id: newVariant.id,
  46. type: 'variant',
  47. fromAdaptation: fromAdaptation,
  48. bandwidth: newVariant.bandwidth,
  49. });
  50. }
  51. /**
  52. * Update the history to show that we are currently playing |newText|. If we
  53. * are already playing |newText|, this update will be ignored.
  54. *
  55. * @param {shaka.extern.Stream} newText
  56. * @param {boolean} fromAdaptation
  57. */
  58. updateCurrentText(newText, fromAdaptation) {
  59. if (this.currentText_ == newText) { return; }
  60. this.currentText_ = newText;
  61. this.history_.push({
  62. timestamp: this.getNowInSeconds_(),
  63. id: newText.id,
  64. type: 'text',
  65. fromAdaptation: fromAdaptation,
  66. bandwidth: null,
  67. });
  68. }
  69. /**
  70. * Get a copy of the switch history. This will make sure to expose no internal
  71. * references.
  72. *
  73. * @return {!Array.<shaka.extern.TrackChoice>}
  74. */
  75. getCopy() {
  76. const copy = [];
  77. for (const entry of this.history_) {
  78. copy.push(this.clone_(entry));
  79. }
  80. return copy;
  81. }
  82. /**
  83. * Get the system time in seconds.
  84. *
  85. * @return {number}
  86. * @private
  87. */
  88. getNowInSeconds_() {
  89. return Date.now() / 1000;
  90. }
  91. /**
  92. * @param {shaka.extern.TrackChoice} entry
  93. * @return {shaka.extern.TrackChoice}
  94. * @private
  95. */
  96. clone_(entry) {
  97. return {
  98. timestamp: entry.timestamp,
  99. id: entry.id,
  100. type: entry.type,
  101. fromAdaptation: entry.fromAdaptation,
  102. bandwidth: entry.bandwidth,
  103. };
  104. }
  105. };