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