Source: lib/ads/interstitial_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.InterstitialAd');
  7. /**
  8. * @implements {shaka.extern.IAd}
  9. * @export
  10. */
  11. shaka.ads.InterstitialAd = class {
  12. /**
  13. * @param {HTMLMediaElement} video
  14. * @param {boolean} isSkippable
  15. * @param {?number} skipOffset
  16. * @param {?number} skipFor
  17. * @param {function()} onSkip
  18. * @param {number} sequenceLength
  19. * @param {number} adPosition
  20. * @param {boolean} isUsingAnotherMediaElement
  21. */
  22. constructor(video, isSkippable, skipOffset, skipFor, onSkip,
  23. sequenceLength, adPosition, isUsingAnotherMediaElement) {
  24. /** @private {HTMLMediaElement} */
  25. this.video_ = video;
  26. /** @private {boolean} */
  27. this.isSkippable_ = isSkippable;
  28. /** @private {?number} */
  29. this.skipOffset_ = isSkippable ? skipOffset || 0 : skipOffset;
  30. /** @private {?number} */
  31. this.skipFor_ = skipFor;
  32. /** @private {function()} */
  33. this.onSkip_ = onSkip;
  34. /** @private {number} */
  35. this.sequenceLength_ = sequenceLength;
  36. /** @private {number} */
  37. this.adPosition_ = adPosition;
  38. /** @private {boolean} */
  39. this.isUsingAnotherMediaElement_ = isUsingAnotherMediaElement;
  40. }
  41. /**
  42. * @override
  43. * @export
  44. */
  45. needsSkipUI() {
  46. return true;
  47. }
  48. /**
  49. * @override
  50. * @export
  51. */
  52. isClientRendering() {
  53. return true;
  54. }
  55. /**
  56. * @override
  57. * @export
  58. */
  59. isUsingAnotherMediaElement() {
  60. return this.isUsingAnotherMediaElement_;
  61. }
  62. /**
  63. * @override
  64. * @export
  65. */
  66. getDuration() {
  67. const duration = this.video_.duration;
  68. if (isNaN(duration)) {
  69. return -1;
  70. }
  71. return duration;
  72. }
  73. /**
  74. * @override
  75. * @export
  76. */
  77. getMinSuggestedDuration() {
  78. return this.getDuration();
  79. }
  80. /**
  81. * @override
  82. * @export
  83. */
  84. getRemainingTime() {
  85. const duration = this.video_.duration;
  86. if (isNaN(duration)) {
  87. return -1;
  88. }
  89. return duration - this.video_.currentTime;
  90. }
  91. /**
  92. * @override
  93. * @export
  94. */
  95. isPaused() {
  96. return this.video_.paused;
  97. }
  98. /**
  99. * @override
  100. * @export
  101. */
  102. isSkippable() {
  103. if (this.isSkippable_ && this.skipFor_ != null) {
  104. const position = this.getDuration() - this.getRemainingTime();
  105. const maxTime = this.skipOffset_ + this.skipFor_;
  106. return position < maxTime;
  107. }
  108. return this.isSkippable_;
  109. }
  110. /**
  111. * @override
  112. * @export
  113. */
  114. getTimeUntilSkippable() {
  115. if (this.isSkippable()) {
  116. const canSkipIn =
  117. this.getRemainingTime() + this.skipOffset_ - this.getDuration();
  118. return Math.max(canSkipIn, 0);
  119. }
  120. return Math.max(this.getRemainingTime(), 0);
  121. }
  122. /**
  123. * @override
  124. * @export
  125. */
  126. canSkipNow() {
  127. return this.isSkippable_ && this.getTimeUntilSkippable() == 0;
  128. }
  129. /**
  130. * @override
  131. * @export
  132. */
  133. skip() {
  134. if (this.canSkipNow()) {
  135. this.onSkip_();
  136. }
  137. }
  138. /**
  139. * @override
  140. * @export
  141. */
  142. pause() {
  143. return this.video_.pause();
  144. }
  145. /**
  146. * @override
  147. * @export
  148. */
  149. play() {
  150. return this.video_.play();
  151. }
  152. /**
  153. * @override
  154. * @export
  155. */
  156. getVolume() {
  157. return this.video_.volume;
  158. }
  159. /**
  160. * @override
  161. * @export
  162. */
  163. setVolume(volume) {
  164. this.video_.volume = volume;
  165. }
  166. /**
  167. * @override
  168. * @export
  169. */
  170. isMuted() {
  171. return this.video_.muted;
  172. }
  173. /**
  174. * @override
  175. * @export
  176. */
  177. isLinear() {
  178. return true;
  179. }
  180. /**
  181. * @override
  182. * @export
  183. */
  184. resize(width, height) {
  185. // Nothing
  186. }
  187. /**
  188. * @override
  189. * @export
  190. */
  191. setMuted(muted) {
  192. this.video_.muted = muted;
  193. }
  194. /**
  195. * @override
  196. * @export
  197. */
  198. getSequenceLength() {
  199. return this.sequenceLength_;
  200. }
  201. /**
  202. * @override
  203. * @export
  204. */
  205. getPositionInSequence() {
  206. return this.adPosition_;
  207. }
  208. /**
  209. * @override
  210. * @export
  211. */
  212. getTitle() {
  213. return '';
  214. }
  215. /**
  216. * @override
  217. * @export
  218. */
  219. getDescription() {
  220. return '';
  221. }
  222. /**
  223. * @override
  224. * @export
  225. */
  226. getVastMediaBitrate() {
  227. return 0;
  228. }
  229. /**
  230. * @override
  231. * @export
  232. */
  233. getVastMediaHeight() {
  234. return 0;
  235. }
  236. /**
  237. * @override
  238. * @export
  239. */
  240. getVastMediaWidth() {
  241. return 0;
  242. }
  243. /**
  244. * @override
  245. * @export
  246. */
  247. getAdId() {
  248. return '';
  249. }
  250. /**
  251. * @override
  252. * @export
  253. */
  254. getCreativeAdId() {
  255. return '';
  256. }
  257. /**
  258. * @override
  259. * @export
  260. */
  261. getAdvertiserName() {
  262. return '';
  263. }
  264. /**
  265. * @override
  266. * @export
  267. */
  268. getMediaUrl() {
  269. return null;
  270. }
  271. /**
  272. * @override
  273. * @export
  274. */
  275. getTimeOffset() {
  276. return 0;
  277. }
  278. /**
  279. * @override
  280. * @export
  281. */
  282. getPodIndex() {
  283. return 0;
  284. }
  285. /**
  286. * @override
  287. * @export
  288. */
  289. release() {
  290. this.video_ = null;
  291. }
  292. };