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