Source: lib/util/switch_history.js

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