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