Source: lib/lcevc/lcevc_dec.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.lcevc.Dec');
  7. goog.require('shaka.log');
  8. goog.require('shaka.Deprecate');
  9. goog.require('shaka.util.IReleasable');
  10. /**
  11. * @summary
  12. * lcevcDec - (MPEG-5 Part 2 LCEVC - Decoder) provides
  13. * all the operations related to the enhancement and rendering
  14. * of LCEVC enabled streams and on to a canvas.
  15. * @implements {shaka.util.IReleasable}
  16. * @export
  17. */
  18. shaka.lcevc.Dec = class {
  19. /**
  20. * @param {HTMLVideoElement} media The video element that will be attached to
  21. * LCEVC Decoder for input.
  22. * @param {HTMLCanvasElement} canvas The canvas element that will be attached
  23. * to LCEVC Decoder to render the enhanced frames.
  24. * @param {shaka.extern.LcevcConfiguration} lcevcConfig LCEVC configuration
  25. * object to initialize the LCEVC Decoder.
  26. */
  27. constructor(media, canvas, lcevcConfig) {
  28. /**
  29. * LCEVC Decoder library based on availability, to check if either
  30. * lcevc_dil or lcevc_dec is present.
  31. * @private {LCEVCmodule}
  32. */
  33. this.lcevcLib_;
  34. /** @private {?LCEVCdec.LCEVCdec} */
  35. this.dec_ = null;
  36. /** @private {number} */
  37. this.variantId_ = -1;
  38. /** @private {HTMLVideoElement} */
  39. this.media_ = media;
  40. /** @private {HTMLCanvasElement} */
  41. this.canvas_ = canvas;
  42. /** @private {shaka.extern.LcevcConfiguration} */
  43. this.decConfig_ = lcevcConfig;
  44. /** @private {boolean} */
  45. this.toBeDeprecated_ = false;
  46. this.create_();
  47. }
  48. /**
  49. * Append data to the LCEVC Dec.
  50. * @param {BufferSource} data
  51. * @param {number} timestampOffset
  52. */
  53. appendBuffer(data, timestampOffset) {
  54. if (this.dec_) {
  55. // Timestamp offset describes how much offset should be applied to the
  56. // LCEVC enhancement to match the decoded video frame, as such it needs
  57. // to be negated.
  58. this.dec_.appendBuffer(data, 'video', this.variantId_, -timestampOffset);
  59. }
  60. }
  61. /**
  62. * Hide the canvas specifically in the case of a DRM Content
  63. */
  64. hideCanvas() {
  65. if (this.dec_) {
  66. this.canvas_.classList.add('shaka-hidden');
  67. }
  68. }
  69. /**
  70. * Create LCEVC Decoder.
  71. * @private
  72. */
  73. create_() {
  74. if (this.isSupported_() && !this.dec_) {
  75. if (this.lcevcLib_.SupportObject.webGLSupport(this.canvas_)) {
  76. // Make sure the canvas is not hidden from a previous playback session.
  77. this.canvas_.classList.remove('shaka-hidden');
  78. // Initiate LCEVC Dec Library based on the type of module available.
  79. if (this.toBeDeprecated_) {
  80. this.dec_ = new this.lcevcLib_.LcevcDil(
  81. this.media_,
  82. this.canvas_,
  83. this.decConfig_);
  84. } else {
  85. this.dec_ = new this.lcevcLib_.LCEVCdec(
  86. this.media_,
  87. this.canvas_,
  88. this.decConfig_);
  89. }
  90. }
  91. }
  92. }
  93. /**
  94. * Close LCEVC Decoder.
  95. * @override
  96. * @export
  97. */
  98. release() {
  99. if (this.dec_) {
  100. this.dec_.close();
  101. this.dec_ = null;
  102. }
  103. }
  104. /**
  105. * Check if the LCEVC Decoder lib is present and is supported by the browser.
  106. * @return {boolean}
  107. * @private
  108. */
  109. isSupported_() {
  110. if (typeof libDPIModule === 'undefined') {
  111. shaka.log.alwaysWarn(
  112. 'Could not find LCEVC Library dependencies on this page');
  113. }
  114. // This is a check to make the LCEVC Dec Integration is backwards compatible
  115. // with previous Integration. This logic checks for both implementations
  116. // and uses the one available.
  117. if (typeof LCEVCdec !== 'undefined') {
  118. this.lcevcLib_ = LCEVCdec;
  119. } else {
  120. if (typeof LcevcDil !== 'undefined') {
  121. this.lcevcLib_ = LcevcDil;
  122. this.toBeDeprecated_ = true;
  123. shaka.Deprecate.deprecateFeature(5,
  124. 'LcevcDil',
  125. 'lcevc_dil.js is deprecated, please use lcevc_dec.js instead');
  126. } else {
  127. shaka.log.alwaysWarn('Could not find LCEVC Library on this page');
  128. return false;
  129. }
  130. }
  131. // Making Sure if there is a valid LCEVC Dec Library exists.
  132. if (typeof this.lcevcLib_.SupportObject === 'undefined') {
  133. shaka.log.alwaysWarn('Could not find LCEVC Library on this page');
  134. return false;
  135. } else {
  136. if (!this.lcevcLib_.SupportObject.SupportStatus) {
  137. shaka.log.alwaysWarn(this.lcevcLib_.SupportObject.SupportError);
  138. }
  139. }
  140. return typeof this.lcevcLib_ !== 'undefined' &&
  141. typeof libDPIModule !== 'undefined' &&
  142. this.canvas_ instanceof HTMLCanvasElement &&
  143. this.lcevcLib_.SupportObject.SupportStatus;
  144. }
  145. /**
  146. * Update current active variant
  147. * @param {shaka.extern.Track} track
  148. */
  149. updateVariant(track, manifestType) {
  150. let containerFormat = shaka.lcevc.Dec.ContainerFormat.MPEG2_TS;
  151. let streamingFormat = shaka.lcevc.Dec.StreamingFormat.OTHER;
  152. switch (track.mimeType) {
  153. case 'video/webm': {
  154. containerFormat = shaka.lcevc.Dec.ContainerFormat.WEBM;
  155. break;
  156. }
  157. case 'video/mp4': {
  158. containerFormat = shaka.lcevc.Dec.ContainerFormat.MP4;
  159. break;
  160. }
  161. }
  162. switch (manifestType) {
  163. case 'DASH': {
  164. streamingFormat = shaka.lcevc.Dec.StreamingFormat.DASH;
  165. break;
  166. }
  167. case 'HLS': {
  168. streamingFormat = shaka.lcevc.Dec.StreamingFormat.HLS;
  169. break;
  170. }
  171. }
  172. if (this.dec_) {
  173. this.variantId_ = track.id;
  174. this.dec_.setLevelSwitching(track.id, true);
  175. this.dec_.setContainerFormat(containerFormat);
  176. // This functionality is only available on the LCEVC Dec Library.
  177. if (!this.toBeDeprecated_) {
  178. this.dec_.setStreamingFormat(streamingFormat);
  179. }
  180. }
  181. }
  182. };
  183. /**
  184. * Container Formats.
  185. * @const @enum {number}
  186. */
  187. shaka.lcevc.Dec.ContainerFormat = {
  188. MPEG2_TS: 0,
  189. WEBM: 1,
  190. MP4: 2,
  191. };
  192. /**
  193. * Streaming Formats.
  194. * @const @enum {number}
  195. */
  196. shaka.lcevc.Dec.StreamingFormat = {
  197. HLS: 0,
  198. DASH: 1,
  199. OTHER: -1,
  200. };