-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodec-detect.js
324 lines (274 loc) · 7.72 KB
/
codec-detect.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
var CodecDetect = CodecDetect || (function CodecDetectIIFE() {
// constants
var Support = {
PENDING: 'pending',
PROBABLY: 'probably',
MAYBE: 'maybe'
};
var Method = {
OPTIMISTIC: 'optimistic',
PESSIMISTIC: 'pessimistic',
EXACT: 'exact'
};
// vars
var _video;
var _videoSupport = {
mpeg4: Support.PENDING,
h264: Support.PENDING,
ogg: Support.PENDING,
webm: Support.PENDING
};
var _audio;
var _audioSupport = {
mp3: Support.PENDING,
aac: Support.PENDING,
oggVorbis: Support.PENDING,
oggOpus: Support.PENDING,
webm: Support.PENDING,
wave: Support.PENDING
};
// public api
var cd = {
// constants
VERSION: '1.1.3',
// properties
method: Method.EXACT,
// video methods
getVideoSupport: getVideoSupport,
isMPEG4VideoSupported: isMPEG4VideoSupported,
isH264VideoSupported: isH264VideoSupported,
isOGGVideoSupported: isOGGVideoSupported,
isWEBMVideoSupported: isWEBMVideoSupported,
// audio methods
getAudioSupport: getAudioSupport,
isMP3AudioSupported: isMP3AudioSupported,
isAACAudioSupported: isAACAudioSupported,
isOGGVorbisAudioSupported: isOGGVorbisAudioSupported,
isOGGOpusAudioSupported: isOGGOpusAudioSupported,
isWEBMAudioSupported: isWEBMAudioSupported,
isWaveAudioSupported: isWaveAudioSupported
};
// auto initialization
init();
// return public api
return cd;
// private methods
function init() {
detectVideoSupport();
detectAudioSupport();
}
function getVideoSupport() {
return _videoSupport;
}
function isMPEG4VideoSupported() {
var isSupported;
switch (CodecDetect.method) {
case Method.OPTIMISTIC:
isSupported = _videoSupport.mpeg4 !== false;
break;
case Method.PESSIMISTIC:
isSupported = _videoSupport.mpeg4 === true;
break;
default:
isSupported = _videoSupport.mpeg4;
break;
}
return isSupported;
}
function isH264VideoSupported() {
var isSupported;
switch (CodecDetect.method) {
case Method.OPTIMISTIC:
isSupported = _videoSupport.h264 !== false;
break;
case Method.PESSIMISTIC:
isSupported = _videoSupport.h264 === true;
break;
default:
isSupported = _videoSupport.h264;
break;
}
return isSupported;
}
function isOGGVideoSupported() {
var isSupported;
switch (CodecDetect.method) {
case Method.OPTIMISTIC:
isSupported = _videoSupport.ogg !== false;
break;
case Method.PESSIMISTIC:
isSupported = _videoSupport.ogg === true;
break;
default:
isSupported = _videoSupport.ogg;
break;
}
return isSupported;
}
function isWEBMVideoSupported() {
var isSupported;
switch (CodecDetect.method) {
case Method.OPTIMISTIC:
isSupported = _videoSupport.webm !== false;
break;
case Method.PESSIMISTIC:
isSupported = _videoSupport.webm === true;
break;
default:
isSupported = _videoSupport.webm;
break;
}
return isSupported;
}
function detectVideoSupport() {
// create a video element to use for testing
_video = document.createElement('video');
// get supported video codecs
if (_video) {
_videoSupport.mpeg4 = _video.canPlayType('video/mp4; codecs="mp4v.20.8"');
_videoSupport.h264 = _video.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
_videoSupport.ogg = _video.canPlayType('video/ogg; codecs="theora"');
_videoSupport.webm = _video.canPlayType('video/webm; codecs="vp8, vorbis"');
convertEmptyStringsToFalse(_videoSupport);
// video dom element couldn't be created, video is not supported
} else {
_videoSupport.mpeg4 = false;
_videoSupport.h264 = false;
_videoSupport.ogg = false;
_videoSupport.webm = false;
}
// memory management
_video = null;
}
function getAudioSupport() {
return _audioSupport;
}
function isMP3AudioSupported() {
var isSupported;
switch (CodecDetect.method) {
case Method.OPTIMISTIC:
isSupported = _audioSupport.mp3 !== false;
break;
case Method.PESSIMISTIC:
isSupported = _audioSupport.mp3 === true;
break;
case Method.EXACT:
isSupported = _audioSupport.mp3;
break;
default:
isSupported = _audioSupport.mp3;
break;
}
return isSupported;
}
function isAACAudioSupported() {
var isSupported;
switch (CodecDetect.method) {
case Method.OPTIMISTIC:
isSupported = _audioSupport.aac !== false;
break;
case Method.PESSIMISTIC:
isSupported = _audioSupport.aac === true;
break;
default:
isSupported = _audioSupport.aac;
break;
}
return isSupported;
}
function isOGGVorbisAudioSupported() {
var isSupported;
switch (CodecDetect.method) {
case Method.OPTIMISTIC:
isSupported = _audioSupport.oggVorbis !== false;
break;
case Method.PESSIMISTIC:
isSupported = _audioSupport.oggVorbis === true;
break;
default:
isSupported = _audioSupport.oggVorbis;
break;
}
return isSupported;
}
function isOGGOpusAudioSupported() {
var isSupported;
switch (CodecDetect.method) {
case Method.OPTIMISTIC:
isSupported = _audioSupport.oggOpus !== false;
break;
case Method.PESSIMISTIC:
isSupported = _audioSupport.oggOpus === true;
break;
default:
isSupported = _audioSupport.oggOpus;
break;
}
return isSupported;
}
function isWEBMAudioSupported() {
var isSupported;
switch (CodecDetect.method) {
case Method.OPTIMISTIC:
isSupported = _audioSupport.webm !== false;
break;
case Method.PESSIMISTIC:
isSupported = _audioSupport.webm === true;
break;
default:
isSupported = _audioSupport.webm;
break;
}
return isSupported;
}
function isWaveAudioSupported() {
var isSupported;
switch (CodecDetect.method) {
case Method.OPTIMISTIC:
isSupported = _audioSupport.wave !== false;
break;
case Method.PESSIMISTIC:
isSupported = _audioSupport.wave === true;
break;
default:
isSupported = _audioSupport.wave;
break;
}
return isSupported;
}
function detectAudioSupport() {
// create a audio element to use for testing
_audio = document.createElement('audio');
// get supported audio codecs
if (_audio) {
_audioSupport.mp3 = _audio.canPlayType('audio/mpeg');
_audioSupport.aac = _audio.canPlayType('audio/mp4; codecs="mp4a.40.2"');
_audioSupport.oggVorbis = _audio.canPlayType('audio/ogg; codecs="vorbis"');
_audioSupport.oggOpus = _audio.canPlayType('audio/ogg; codecs="opus"');
_audioSupport.webm = _audio.canPlayType('audio/webm; codecs="vorbis"');
_audioSupport.wave = _audio.canPlayType('audio/wav; codecs="1"');
convertEmptyStringsToFalse(_audioSupport);
// audio dom element couldn't be created, audio is not supported
} else {
_audioSupport.mp3 = false;
_audioSupport.aac = false;
_audioSupport.oggVorbis = false;
_audioSupport.oggOpus = false;
_audioSupport.webm = false;
_audioSupport.wave = false;
}
// memory management
_audio = null;
}
// utils
function convertEmptyStringsToFalse(support) {
// iterate through codecs, set as not supported if is empty string
_.forEach(support, setAsFalseIfEmpty);
// iterator method
function setAsFalseIfEmpty(n, key) {
if (_.isEmpty(n)) {
support[key] = false;
}
}
}
})();