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].toLowerCase();
  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. case base === 'ac3':
  150. case base === 'ac-3':
  151. return 'ac-3'; // Dolby Digital
  152. case base === 'mp4a' && profile === 'a6':
  153. case base === 'eac3':
  154. case base === 'ec-3':
  155. return 'ec-3'; // Dolby Digital Plus
  156. case base === 'ac-4':
  157. return 'ac-4'; // Dolby AC-4
  158. case base === 'mp4a' && profile === 'b2':
  159. return 'dtsx'; // DTS:X
  160. case base === 'mp4a' && profile === 'a9':
  161. return 'dtsc'; // DTS Digital Surround
  162. case base === 'vp09':
  163. return 'vp9';
  164. case base === 'avc1':
  165. case base === 'avc3':
  166. return 'avc'; // H264
  167. case base === 'hvc1':
  168. case base === 'hev1':
  169. return 'hevc'; // H265
  170. case base === 'vvc1':
  171. case base === 'vvi1':
  172. return 'vvc'; // H266
  173. case base === 'dvh1':
  174. case base === 'dvhe':
  175. if (profile && profile.startsWith('05')) {
  176. return 'dovi-p5'; // Dolby Vision profile 5
  177. }
  178. return 'dovi-hevc'; // Dolby Vision based in HEVC
  179. case base === 'dvav':
  180. case base === 'dva1':
  181. return 'dovi-avc'; // Dolby Vision based in AVC
  182. case base === 'dav1':
  183. return 'dovi-av1'; // Dolby Vision based in AV1
  184. case base === 'dvc1':
  185. case base === 'dvi1':
  186. return 'dovi-vvc'; // Dolby Vision based in VVC
  187. }
  188. return base;
  189. }
  190. /**
  191. * Get the base codec from a codec string.
  192. *
  193. * @param {string} codecString
  194. * @return {string}
  195. */
  196. static getCodecBase(codecString) {
  197. const codecsBase = [];
  198. for (const codec of codecString.split(',')) {
  199. const parts = shaka.util.MimeUtils.getCodecParts_(codec);
  200. codecsBase.push(parts[0]);
  201. }
  202. return codecsBase.sort().join(',');
  203. }
  204. /**
  205. * Takes a full MIME type (with codecs) or basic MIME type (without codecs)
  206. * and returns a basic MIME type (without codecs or other parameters).
  207. *
  208. * @param {string} mimeType
  209. * @return {string}
  210. */
  211. static getBasicType(mimeType) {
  212. return mimeType.split(';')[0];
  213. }
  214. /**
  215. * Takes a MIME type and returns the codecs parameter, or an empty string if
  216. * there is no codecs parameter.
  217. *
  218. * @param {string} mimeType
  219. * @return {string}
  220. */
  221. static getCodecs(mimeType) {
  222. // Parse the basic MIME type from its parameters.
  223. const pieces = mimeType.split(/ *; */);
  224. pieces.shift(); // Remove basic MIME type from pieces.
  225. const codecs = pieces.find((piece) => piece.startsWith('codecs='));
  226. if (!codecs) {
  227. return '';
  228. }
  229. // The value may be quoted, so remove quotes at the beginning or end.
  230. const value = codecs.split('=')[1].replace(/^"|"$/g, '');
  231. return value;
  232. }
  233. /**
  234. * Checks if the given MIME type is HLS MIME type.
  235. *
  236. * @param {string} mimeType
  237. * @return {boolean}
  238. */
  239. static isHlsType(mimeType) {
  240. return mimeType === 'application/x-mpegurl' ||
  241. mimeType === 'application/vnd.apple.mpegurl';
  242. }
  243. /**
  244. * Checks if the given MIME type is DASH MIME type.
  245. *
  246. * @param {string} mimeType
  247. * @return {boolean}
  248. */
  249. static isDashType(mimeType) {
  250. return mimeType === 'application/dash+xml' ||
  251. mimeType === 'video/vnd.mpeg.dash.mpd';
  252. }
  253. /**
  254. * Get the base and profile of a codec string. Where [0] will be the codec
  255. * base and [1] will be the profile.
  256. * @param {string} codecString
  257. * @return {!Array<string>}
  258. * @private
  259. */
  260. static getCodecParts_(codecString) {
  261. const parts = codecString.split('.');
  262. const base = parts[0];
  263. parts.shift();
  264. const profile = parts.join('.');
  265. // Make sure that we always return a "base" and "profile".
  266. return [base, profile];
  267. }
  268. };
  269. /**
  270. * A map from Stream object keys to MIME type parameters. These should be
  271. * ignored by platforms that do not recognize them.
  272. *
  273. * This initial set of parameters are all recognized by Chromecast.
  274. *
  275. * @const {!Map<string, string>}
  276. * @private
  277. */
  278. shaka.util.MimeUtils.EXTENDED_MIME_PARAMETERS_ = new Map()
  279. .set('codecs', 'codecs')
  280. .set('frameRate', 'framerate') // Ours is camelCase, theirs is lowercase.
  281. .set('bandwidth', 'bitrate') // They are in the same units: bits/sec.
  282. .set('width', 'width')
  283. .set('height', 'height')
  284. .set('channelsCount', 'channels');
  285. /**
  286. * A mimetype created for CEA-608 closed captions.
  287. * @const {string}
  288. */
  289. shaka.util.MimeUtils.CEA608_CLOSED_CAPTION_MIMETYPE = 'application/cea-608';
  290. /**
  291. * A mimetype created for CEA-708 closed captions.
  292. * @const {string}
  293. */
  294. shaka.util.MimeUtils.CEA708_CLOSED_CAPTION_MIMETYPE = 'application/cea-708';
  295. /**
  296. * MIME types of raw formats.
  297. *
  298. * @const {!Array<string>}
  299. */
  300. shaka.util.MimeUtils.RAW_FORMATS = [
  301. 'audio/aac',
  302. 'audio/ac3',
  303. 'audio/ec3',
  304. 'audio/mpeg',
  305. ];