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. * @param {string} mimeType
  80. * @param {string} codecs
  81. * @return {string}
  82. */
  83. static getExtendedType(stream, mimeType, codecs) {
  84. const components = [mimeType];
  85. const extendedMimeParams = shaka.util.MimeUtils.EXTENDED_MIME_PARAMETERS_;
  86. extendedMimeParams.forEach((mimeKey, streamKey) => {
  87. const value = stream[streamKey];
  88. if (streamKey == 'codecs') {
  89. if (shaka.util.MimeUtils.RAW_FORMATS.includes(stream.mimeType)) {
  90. // Skip codecs for raw formats
  91. } else {
  92. components.push('codecs="' + codecs + '"');
  93. }
  94. } else if (value) {
  95. components.push(mimeKey + '="' + value + '"');
  96. }
  97. });
  98. if (stream.hdr == 'PQ') {
  99. components.push('eotf="smpte2084"');
  100. }
  101. return components.join(';');
  102. }
  103. /**
  104. * Takes a full MIME type (with codecs) or basic MIME type (without codecs)
  105. * and returns a container type string ("mp2t", "mp4", "webm", etc.)
  106. *
  107. * @param {string} mimeType
  108. * @return {string}
  109. */
  110. static getContainerType(mimeType) {
  111. return mimeType.split(';')[0].split('/')[1];
  112. }
  113. /**
  114. * Split a list of codecs encoded in a string into a list of codecs.
  115. * @param {string} codecs
  116. * @return {!Array.<string>}
  117. */
  118. static splitCodecs(codecs) {
  119. return codecs.split(',');
  120. }
  121. /**
  122. * Get the normalized codec from a codec string,
  123. * independently of their container.
  124. *
  125. * @param {string} codecString
  126. * @return {string}
  127. */
  128. static getNormalizedCodec(codecString) {
  129. const parts =
  130. shaka.util.MimeUtils.getCodecParts_(codecString);
  131. const base = parts[0];
  132. const profile = parts[1].toLowerCase();
  133. switch (true) {
  134. case base === 'mp4a' && profile === '69':
  135. case base === 'mp4a' && profile === '6b':
  136. case base === 'mp4a' && profile === '40.34':
  137. return 'mp3';
  138. case base === 'mp4a' && profile === '66':
  139. case base === 'mp4a' && profile === '67':
  140. case base === 'mp4a' && profile === '68':
  141. case base === 'mp4a' && profile === '40.2':
  142. case base === 'mp4a' && profile === '40.02':
  143. case base === 'mp4a' && profile === '40.5':
  144. case base === 'mp4a' && profile === '40.05':
  145. case base === 'mp4a' && profile === '40.29':
  146. case base === 'mp4a' && profile === '40.42': // Extended HE-AAC
  147. return 'aac';
  148. case base === 'mp4a' && profile === 'a5':
  149. return 'ac-3'; // Dolby Digital
  150. case base === 'mp4a' && profile === 'a6':
  151. return 'ec-3'; // Dolby Digital Plus
  152. case base === 'mp4a' && profile === 'b2':
  153. return 'dtsx'; // DTS:X
  154. case base === 'mp4a' && profile === 'a9':
  155. return 'dtsc'; // DTS Digital Surround
  156. case base === 'vp09':
  157. return 'vp9';
  158. case base === 'avc1':
  159. case base === 'avc3':
  160. return 'avc'; // H264
  161. case base === 'hvc1':
  162. case base === 'hev1':
  163. return 'hevc'; // H265
  164. case base === 'vvc1':
  165. case base === 'vvi1':
  166. return 'vvc'; // H266
  167. case base === 'dvh1':
  168. case base === 'dvhe':
  169. return 'dovi-hevc'; // Dolby Vision based in HEVC
  170. case base === 'dvav':
  171. case base === 'dva1':
  172. return 'dovi-avc'; // Dolby Vision based in AVC
  173. case base === 'dav1':
  174. return 'dovi-av1'; // Dolby Vision based in AV1
  175. }
  176. return base;
  177. }
  178. /**
  179. * Get the base codec from a codec string.
  180. *
  181. * @param {string} codecString
  182. * @return {string}
  183. */
  184. static getCodecBase(codecString) {
  185. const codecsBase = [];
  186. for (const codec of codecString.split(',')) {
  187. const parts = shaka.util.MimeUtils.getCodecParts_(codec);
  188. codecsBase.push(parts[0]);
  189. }
  190. return codecsBase.sort().join(',');
  191. }
  192. /**
  193. * Takes a full MIME type (with codecs) or basic MIME type (without codecs)
  194. * and returns a basic MIME type (without codecs or other parameters).
  195. *
  196. * @param {string} mimeType
  197. * @return {string}
  198. */
  199. static getBasicType(mimeType) {
  200. return mimeType.split(';')[0];
  201. }
  202. /**
  203. * Takes a MIME type and returns the codecs parameter, or an empty string if
  204. * there is no codecs parameter.
  205. *
  206. * @param {string} mimeType
  207. * @return {string}
  208. */
  209. static getCodecs(mimeType) {
  210. // Parse the basic MIME type from its parameters.
  211. const pieces = mimeType.split(/ *; */);
  212. pieces.shift(); // Remove basic MIME type from pieces.
  213. const codecs = pieces.find((piece) => piece.startsWith('codecs='));
  214. if (!codecs) {
  215. return '';
  216. }
  217. // The value may be quoted, so remove quotes at the beginning or end.
  218. const value = codecs.split('=')[1].replace(/^"|"$/g, '');
  219. return value;
  220. }
  221. /**
  222. * Checks if the given MIME type is HLS MIME type.
  223. *
  224. * @param {string} mimeType
  225. * @return {boolean}
  226. */
  227. static isHlsType(mimeType) {
  228. return mimeType === 'application/x-mpegurl' ||
  229. mimeType === 'application/vnd.apple.mpegurl';
  230. }
  231. /**
  232. * Get the base and profile of a codec string. Where [0] will be the codec
  233. * base and [1] will be the profile.
  234. * @param {string} codecString
  235. * @return {!Array.<string>}
  236. * @private
  237. */
  238. static getCodecParts_(codecString) {
  239. const parts = codecString.split('.');
  240. const base = parts[0];
  241. parts.shift();
  242. const profile = parts.join('.');
  243. // Make sure that we always return a "base" and "profile".
  244. return [base, profile];
  245. }
  246. };
  247. /**
  248. * A map from Stream object keys to MIME type parameters. These should be
  249. * ignored by platforms that do not recognize them.
  250. *
  251. * This initial set of parameters are all recognized by Chromecast.
  252. *
  253. * @const {!Map.<string, string>}
  254. * @private
  255. */
  256. shaka.util.MimeUtils.EXTENDED_MIME_PARAMETERS_ = new Map()
  257. .set('codecs', 'codecs')
  258. .set('frameRate', 'framerate') // Ours is camelCase, theirs is lowercase.
  259. .set('bandwidth', 'bitrate') // They are in the same units: bits/sec.
  260. .set('width', 'width')
  261. .set('height', 'height')
  262. .set('channelsCount', 'channels');
  263. /**
  264. * A mimetype created for CEA-608 closed captions.
  265. * @const {string}
  266. */
  267. shaka.util.MimeUtils.CEA608_CLOSED_CAPTION_MIMETYPE = 'application/cea-608';
  268. /**
  269. * A mimetype created for CEA-708 closed captions.
  270. * @const {string}
  271. */
  272. shaka.util.MimeUtils.CEA708_CLOSED_CAPTION_MIMETYPE = 'application/cea-708';
  273. /**
  274. * MIME types of raw formats.
  275. *
  276. * @const {!Array.<string>}
  277. */
  278. shaka.util.MimeUtils.RAW_FORMATS = [
  279. 'audio/aac',
  280. 'audio/ac3',
  281. 'audio/ec3',
  282. 'audio/mpeg',
  283. ];