Source: lib/ads/server_side_ad.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ads.ServerSideAd');
  7. /**
  8. * @implements {shaka.extern.IAd}
  9. * @export
  10. */
  11. shaka.ads.ServerSideAd = class {
  12. /**
  13. * @param {google.ima.dai.api.Ad} imaAd
  14. * @param {HTMLMediaElement} video
  15. */
  16. constructor(imaAd, video) {
  17. /** @private {google.ima.dai.api.Ad} */
  18. this.ad_ = imaAd;
  19. /** @private {?google.ima.dai.api.AdProgressData} */
  20. this.adProgressData_ = null;
  21. /** @private {HTMLMediaElement} */
  22. this.video_ = video;
  23. }
  24. /**
  25. * @param {google.ima.dai.api.AdProgressData} data
  26. */
  27. setProgressData(data) {
  28. this.adProgressData_ = data;
  29. }
  30. /**
  31. * @override
  32. * @export
  33. */
  34. getDuration() {
  35. if (!this.adProgressData_) {
  36. // Unknown yet
  37. return -1;
  38. }
  39. return this.adProgressData_.duration;
  40. }
  41. /**
  42. * @override
  43. * @export
  44. */
  45. getMinSuggestedDuration() {
  46. return this.getDuration();
  47. }
  48. /**
  49. * @override
  50. * @export
  51. */
  52. getRemainingTime() {
  53. if (!this.adProgressData_) {
  54. // Unknown yet
  55. return -1;
  56. }
  57. return this.adProgressData_.duration - this.adProgressData_.currentTime;
  58. }
  59. /**
  60. * @override
  61. * @export
  62. */
  63. isPaused() {
  64. return this.video_.paused;
  65. }
  66. /**
  67. * @override
  68. * @export
  69. */
  70. isSkippable() {
  71. return this.ad_.isSkippable();
  72. }
  73. /**
  74. * @override
  75. * @export
  76. */
  77. getTimeUntilSkippable() {
  78. const skipOffset = this.ad_.getSkipTimeOffset();
  79. const canSkipIn = this.getRemainingTime() - skipOffset;
  80. return Math.max(canSkipIn, 0);
  81. }
  82. /**
  83. * @override
  84. * @export
  85. */
  86. canSkipNow() {
  87. return this.getTimeUntilSkippable() == 0;
  88. }
  89. /**
  90. * @override
  91. * @export
  92. */
  93. skip() {
  94. this.video_.currentTime += this.getRemainingTime();
  95. }
  96. /**
  97. * @override
  98. * @export
  99. */
  100. pause() {
  101. return this.video_.pause();
  102. }
  103. /**
  104. * @override
  105. * @export
  106. */
  107. play() {
  108. return this.video_.play();
  109. }
  110. /**
  111. * @override
  112. * @export
  113. */
  114. getVolume() {
  115. return this.video_.volume;
  116. }
  117. /**
  118. * @override
  119. * @export
  120. */
  121. setVolume(volume) {
  122. this.video_.volume = volume;
  123. }
  124. /**
  125. * @override
  126. * @export
  127. */
  128. isMuted() {
  129. return this.video_.muted;
  130. }
  131. /**
  132. * @override
  133. * @export
  134. */
  135. isLinear() {
  136. return true;
  137. }
  138. /**
  139. * @override
  140. * @export
  141. */
  142. resize(width, height) {
  143. // Nothing
  144. }
  145. /**
  146. * @override
  147. * @export
  148. */
  149. setMuted(muted) {
  150. this.video_.muted = muted;
  151. }
  152. /**
  153. * @override
  154. * @export
  155. */
  156. getSequenceLength() {
  157. const podInfo = this.ad_.getAdPodInfo();
  158. if (podInfo == null) {
  159. // No pod, just one ad.
  160. return 1;
  161. }
  162. return podInfo.getTotalAds();
  163. }
  164. /**
  165. * @override
  166. * @export
  167. */
  168. getPositionInSequence() {
  169. const podInfo = this.ad_.getAdPodInfo();
  170. if (podInfo == null) {
  171. // No pod, just one ad.
  172. return 1;
  173. }
  174. return podInfo.getAdPosition();
  175. }
  176. /**
  177. * @override
  178. * @export
  179. */
  180. release() {
  181. this.ad_ = null;
  182. this.adProgressData_ = null;
  183. this.video_ = null;
  184. }
  185. };