Source: lib/transmuxer/transmuxer_engine.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.transmuxer.TransmuxerEngine');
  7. goog.require('shaka.util.IDestroyable');
  8. // TODO: revisit this when Closure Compiler supports partially-exported classes.
  9. /**
  10. * @summary Manages transmuxer plugins.
  11. * @implements {shaka.util.IDestroyable}
  12. * @export
  13. */
  14. shaka.transmuxer.TransmuxerEngine = class {
  15. // TODO: revisit this when the compiler supports partially-exported classes.
  16. /**
  17. * @override
  18. * @export
  19. */
  20. destroy() {}
  21. /**
  22. * @param {string} mimeType
  23. * @param {!shaka.extern.TransmuxerPlugin} plugin
  24. * @param {number} priority
  25. * @export
  26. */
  27. static registerTransmuxer(mimeType, plugin, priority) {
  28. const TransmuxerEngine = shaka.transmuxer.TransmuxerEngine;
  29. const normalizedMimetype = TransmuxerEngine.normalizeMimeType_(mimeType);
  30. const key = normalizedMimetype + '-' + priority;
  31. TransmuxerEngine.transmuxerMap_[key] = {
  32. priority: priority,
  33. plugin: plugin,
  34. };
  35. }
  36. /**
  37. * @param {string} mimeType
  38. * @param {number} priority
  39. * @export
  40. */
  41. static unregisterTransmuxer(mimeType, priority) {
  42. const TransmuxerEngine = shaka.transmuxer.TransmuxerEngine;
  43. const normalizedMimetype = TransmuxerEngine.normalizeMimeType_(mimeType);
  44. const key = normalizedMimetype + '-' + priority;
  45. delete TransmuxerEngine.transmuxerMap_[key];
  46. }
  47. /**
  48. * @param {string} mimeType
  49. * @param {string=} contentType
  50. * @return {?shaka.extern.TransmuxerPlugin}
  51. * @export
  52. */
  53. static findTransmuxer(mimeType, contentType) {
  54. const TransmuxerEngine = shaka.transmuxer.TransmuxerEngine;
  55. const normalizedMimetype = TransmuxerEngine.normalizeMimeType_(mimeType);
  56. const priorities = [
  57. TransmuxerEngine.PluginPriority.APPLICATION,
  58. TransmuxerEngine.PluginPriority.PREFERRED,
  59. TransmuxerEngine.PluginPriority.PREFERRED_SECONDARY,
  60. TransmuxerEngine.PluginPriority.FALLBACK,
  61. ];
  62. for (const priority of priorities) {
  63. const key = normalizedMimetype + '-' + priority;
  64. const object = TransmuxerEngine.transmuxerMap_[key];
  65. if (object) {
  66. const transmuxer = object.plugin();
  67. const isSupported = transmuxer.isSupported(mimeType, contentType);
  68. transmuxer.destroy();
  69. if (isSupported) {
  70. return object.plugin;
  71. }
  72. }
  73. }
  74. return null;
  75. }
  76. /**
  77. * @param {string} mimeType
  78. * @return {string}
  79. * @private
  80. */
  81. static normalizeMimeType_(mimeType) {
  82. return mimeType.toLowerCase().split(';')[0];
  83. }
  84. /**
  85. * Check if the mime type and the content type is supported.
  86. * @param {string} mimeType
  87. * @param {string=} contentType
  88. * @return {boolean}
  89. */
  90. static isSupported(mimeType, contentType) {
  91. const TransmuxerEngine = shaka.transmuxer.TransmuxerEngine;
  92. const transmuxerPlugin = TransmuxerEngine.findTransmuxer(mimeType);
  93. if (!transmuxerPlugin) {
  94. return false;
  95. }
  96. return true;
  97. }
  98. /**
  99. * For any stream, convert its codecs to MP4 codecs.
  100. * @param {string} contentType
  101. * @param {string} mimeType
  102. * @return {string}
  103. */
  104. static convertCodecs(contentType, mimeType) {
  105. const TransmuxerEngine = shaka.transmuxer.TransmuxerEngine;
  106. const transmuxerPlugin = TransmuxerEngine.findTransmuxer(mimeType);
  107. if (!transmuxerPlugin) {
  108. return mimeType;
  109. }
  110. const transmuxer = transmuxerPlugin();
  111. const codecs = transmuxer.convertCodecs(contentType, mimeType);
  112. transmuxer.destroy();
  113. return codecs;
  114. }
  115. };
  116. /**
  117. * @typedef {{
  118. * plugin: shaka.extern.TransmuxerPlugin,
  119. * priority: number
  120. * }}
  121. * @property {shaka.extern.TransmuxerPlugin} plugin
  122. * The associated plugin.
  123. * @property {number} priority
  124. * The plugin's priority.
  125. */
  126. shaka.transmuxer.TransmuxerEngine.PluginObject;
  127. /**
  128. * @private {!Object.<string, !shaka.transmuxer.TransmuxerEngine.PluginObject>}
  129. */
  130. shaka.transmuxer.TransmuxerEngine.transmuxerMap_ = {};
  131. /**
  132. * Priority level for transmuxer plugins.
  133. * If multiple plugins are provided for the same mime type, only the
  134. * highest-priority one is used.
  135. *
  136. * @enum {number}
  137. * @export
  138. */
  139. shaka.transmuxer.TransmuxerEngine.PluginPriority = {
  140. 'FALLBACK': 1,
  141. 'PREFERRED_SECONDARY': 2,
  142. 'PREFERRED': 3,
  143. 'APPLICATION': 4,
  144. };