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