Source: lib/util/mime_utils.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.util.MimeUtils');
  7. goog.require('shaka.transmuxer.TransmuxerEngine');
  8. goog.require('shaka.util.ManifestParserUtils');
  9. /**
  10. * @summary A set of utility functions for dealing with MIME types.
  11. * @export
  12. */
  13. shaka.util.MimeUtils = class {
  14. /**
  15. * Takes a MIME type and optional codecs string and produces the full MIME
  16. * type. Also remove the codecs for raw formats.
  17. *
  18. * @param {string} mimeType
  19. * @param {string=} codecs
  20. * @return {string}
  21. * @export
  22. */
  23. static getFullType(mimeType, codecs) {
  24. let fullMimeType = mimeType;
  25. if (codecs && !shaka.util.MimeUtils.RAW_FORMATS.includes(mimeType)) {
  26. fullMimeType += '; codecs="' + codecs + '"';
  27. }
  28. return fullMimeType;
  29. }
  30. /**
  31. * Takes a MIME type and optional codecs string and produces the full MIME
  32. * type.
  33. *
  34. * @param {string} mimeType
  35. * @param {string=} codecs
  36. * @return {string}
  37. * @export
  38. */
  39. static getFullTypeWithAllCodecs(mimeType, codecs) {
  40. let fullMimeType = mimeType;
  41. if (codecs) {
  42. fullMimeType += '; codecs="' + codecs + '"';
  43. }
  44. return fullMimeType;
  45. }
  46. /**
  47. * Takes a MIME type and a codecs string and produces the full MIME
  48. * type. If it's a transport stream, convert its codecs to MP4 codecs.
  49. * Otherwise for multiplexed content, convert the video MIME types to
  50. * their audio equivalents if the content type is audio.
  51. *
  52. * @param {string} mimeType
  53. * @param {string} codecs
  54. * @param {string} contentType
  55. * @return {string}
  56. */
  57. static getFullOrConvertedType(mimeType, codecs, contentType) {
  58. const MimeUtils = shaka.util.MimeUtils;
  59. const fullMimeType = MimeUtils.getFullType(mimeType, codecs);
  60. const fullMimeTypeWithAllCodecs = MimeUtils.getFullTypeWithAllCodecs(
  61. mimeType, codecs);
  62. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  63. const TransmuxerEngine = shaka.transmuxer.TransmuxerEngine;
  64. if (TransmuxerEngine.isSupported(fullMimeTypeWithAllCodecs, contentType)) {
  65. return TransmuxerEngine.convertCodecs(
  66. contentType, fullMimeTypeWithAllCodecs);
  67. } else if (mimeType != 'video/mp2t' && contentType == ContentType.AUDIO) {
  68. // video/mp2t is the correct mime type for TS audio, so only replace the
  69. // word "video" with "audio" for non-TS audio content.
  70. return fullMimeType.replace('video', 'audio');
  71. }
  72. return fullMimeType;
  73. }
  74. /**
  75. * Takes a Stream object and produces an extended MIME type with information
  76. * beyond the container and codec type, when available.
  77. *
  78. * @param {shaka.extern.Stream} stream
  79. * @return {string}
  80. */
  81. static getExtendedType(stream) {
  82. const components = [stream.mimeType];
  83. const extendedMimeParams = shaka.util.MimeUtils.EXTENDED_MIME_PARAMETERS_;
  84. extendedMimeParams.forEach((mimeKey, streamKey) => {
  85. const value = stream[streamKey];
  86. if (streamKey == 'codecs' &&
  87. shaka.util.MimeUtils.RAW_FORMATS.includes(stream.mimeType)) {
  88. // Skip codecs for raw formats
  89. } else if (value) {
  90. components.push(mimeKey + '="' + value + '"');
  91. }
  92. });
  93. if (stream.hdr == 'PQ') {
  94. components.push('eotf="smpte2084"');
  95. }
  96. return components.join(';');
  97. }
  98. /**
  99. * Takes a full MIME type (with codecs) or basic MIME type (without codecs)
  100. * and returns a container type string ("mp2t", "mp4", "webm", etc.)
  101. *
  102. * @param {string} mimeType
  103. * @return {string}
  104. */
  105. static getContainerType(mimeType) {
  106. return mimeType.split(';')[0].split('/')[1];
  107. }
  108. /**
  109. * Split a list of codecs encoded in a string into a list of codecs.
  110. * @param {string} codecs
  111. * @return {!Array.<string>}
  112. */
  113. static splitCodecs(codecs) {
  114. return codecs.split(',');
  115. }
  116. /**
  117. * Get the normalized codec from a codec string,
  118. * independently of their container.
  119. *
  120. * @param {string} codecString
  121. * @return {string}
  122. */
  123. static getNormalizedCodec(codecString) {
  124. const parts =
  125. shaka.util.MimeUtils.getCodecParts_(codecString);
  126. const base = parts[0];
  127. const profile = parts[1].toLowerCase();
  128. switch (true) {
  129. case base === 'mp4a' && profile === '69':
  130. case base === 'mp4a' && profile === '6b':
  131. case base === 'mp4a' && profile === '40.34':
  132. return 'mp3';
  133. case base === 'mp4a' && profile === '66':
  134. case base === 'mp4a' && profile === '67':
  135. case base === 'mp4a' && profile === '68':
  136. case base === 'mp4a' && profile === '40.2':
  137. case base === 'mp4a' && profile === '40.02':
  138. case base === 'mp4a' && profile === '40.5':
  139. case base === 'mp4a' && profile === '40.05':
  140. case base === 'mp4a' && profile === '40.29':
  141. case base === 'mp4a' && profile === '40.42': // Extended HE-AAC
  142. return 'aac';
  143. case base === 'mp4a' && profile === 'a5':
  144. return 'ac-3'; // Dolby Digital
  145. case base === 'mp4a' && profile === 'a6':
  146. return 'ec-3'; // Dolby Digital Plus
  147. case base === 'mp4a' && profile === 'b2':
  148. return 'dtsx'; // DTS:X
  149. case base === 'mp4a' && profile === 'a9':
  150. return 'dtsc'; // DTS Digital Surround
  151. case base === 'avc1':
  152. case base === 'avc3':
  153. return 'avc'; // H264
  154. case base === 'hvc1':
  155. case base === 'hev1':
  156. return 'hevc'; // H265
  157. case base === 'dvh1':
  158. case base === 'dvhe':
  159. return 'dovi'; // Dolby Vision
  160. }
  161. return base;
  162. }
  163. /**
  164. * Get the base codec from a codec string.
  165. *
  166. * @param {string} codecString
  167. * @return {string}
  168. */
  169. static getCodecBase(codecString) {
  170. const parts = shaka.util.MimeUtils.getCodecParts_(codecString);
  171. return parts[0];
  172. }
  173. /**
  174. * Takes a full MIME type (with codecs) or basic MIME type (without codecs)
  175. * and returns a basic MIME type (without codecs or other parameters).
  176. *
  177. * @param {string} mimeType
  178. * @return {string}
  179. */
  180. static getBasicType(mimeType) {
  181. return mimeType.split(';')[0];
  182. }
  183. /**
  184. * Takes a MIME type and returns the codecs parameter, or an empty string if
  185. * there is no codecs parameter.
  186. *
  187. * @param {string} mimeType
  188. * @return {string}
  189. */
  190. static getCodecs(mimeType) {
  191. // Parse the basic MIME type from its parameters.
  192. const pieces = mimeType.split(/ *; */);
  193. pieces.shift(); // Remove basic MIME type from pieces.
  194. const codecs = pieces.find((piece) => piece.startsWith('codecs='));
  195. if (!codecs) {
  196. return '';
  197. }
  198. // The value may be quoted, so remove quotes at the beginning or end.
  199. const value = codecs.split('=')[1].replace(/^"|"$/g, '');
  200. return value;
  201. }
  202. /**
  203. * Checks if the given MIME type is HLS MIME type.
  204. *
  205. * @param {string} mimeType
  206. * @return {boolean}
  207. */
  208. static isHlsType(mimeType) {
  209. return mimeType === 'application/x-mpegurl' ||
  210. mimeType === 'application/vnd.apple.mpegurl';
  211. }
  212. /**
  213. * Get the base and profile of a codec string. Where [0] will be the codec
  214. * base and [1] will be the profile.
  215. * @param {string} codecString
  216. * @return {!Array.<string>}
  217. * @private
  218. */
  219. static getCodecParts_(codecString) {
  220. const parts = codecString.split('.');
  221. const base = parts[0];
  222. parts.shift();
  223. const profile = parts.join('.');
  224. // Make sure that we always return a "base" and "profile".
  225. return [base, profile];
  226. }
  227. };
  228. /**
  229. * A map from Stream object keys to MIME type parameters. These should be
  230. * ignored by platforms that do not recognize them.
  231. *
  232. * This initial set of parameters are all recognized by Chromecast.
  233. *
  234. * @const {!Map.<string, string>}
  235. * @private
  236. */
  237. shaka.util.MimeUtils.EXTENDED_MIME_PARAMETERS_ = new Map()
  238. .set('codecs', 'codecs')
  239. .set('frameRate', 'framerate') // Ours is camelCase, theirs is lowercase.
  240. .set('bandwidth', 'bitrate') // They are in the same units: bits/sec.
  241. .set('width', 'width')
  242. .set('height', 'height')
  243. .set('channelsCount', 'channels');
  244. /**
  245. * A mimetype created for CEA-608 closed captions.
  246. * @const {string}
  247. */
  248. shaka.util.MimeUtils.CEA608_CLOSED_CAPTION_MIMETYPE = 'application/cea-608';
  249. /**
  250. * A mimetype created for CEA-708 closed captions.
  251. * @const {string}
  252. */
  253. shaka.util.MimeUtils.CEA708_CLOSED_CAPTION_MIMETYPE = 'application/cea-708';
  254. /**
  255. * MIME types of raw formats.
  256. *
  257. * @const {!Array.<string>}
  258. */
  259. shaka.util.MimeUtils.RAW_FORMATS = [
  260. 'audio/aac',
  261. 'audio/ac3',
  262. 'audio/ec3',
  263. 'audio/mpeg',
  264. ];