Skip to content

Commit 3ba7e09

Browse files
committed
Add SDL_MIXER_HTML5_DISABLE_TYPE_CHECK hint
Fix M4A magic check
1 parent dd62bef commit 3ba7e09

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

music_html5.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,19 @@ static int MusicHTML5_Open(const SDL_AudioSpec *spec)
207207
{ type: "audio/ogg", magic: [0x4f, 0x67, 0x67, 0x53] }, // OggS
208208
{ type: "audio/flac", magic: [0x66, 0x4c, 0x61, 0x43] }, // fLaC
209209
//{ type: "audio/midi", magic: [0x4d, 0x54, 0x68, 0x64] }, // MThd
210-
{ type: "audio/mp3", magic: [0x49, 0x44, 0x33] }, // ID3
210+
{ type: "audio/mpeg", magic: [0x49, 0x44, 0x33] }, // ID3
211211
{ type: "audio/wav", magic: [0x52, 0x49, 0x46, 0x46] },
212-
{ type: "audio/mp4", magic: [0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6F, 0x6D] },
212+
{ type: "audio/mp4", offset: 4, magic: [0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6F, 0x6D] },
213+
{ type: "audio/mp4", offset: 4, magic: [0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x41, 0x20] },
213214
{ type: "audio/x-aiff", magic: [0x46, 0x4F, 0x52, 0x4D] },
214215
{ type: ["audio/webm", "audio/x-matroska"], magic: [0x1A, 0x45, 0xDF, 0xA3] }
215216
];
216217

217218
targets.some((target) => {
218219
const targetMagic = target.magic;
219220
const magicLength = targetMagic.length;
220-
const magic = buf.slice(0, magicLength);
221+
const offset = target.offset || 0;
222+
const magic = buf.slice(offset, offset + magicLength);
221223

222224
let matching = true;
223225
for (let i = 0; i < magicLength; i++) {
@@ -239,8 +241,11 @@ static int MusicHTML5_Open(const SDL_AudioSpec *spec)
239241
});
240242

241243
// MP3 special case
242-
if (!canPlay && magic[0] === 0xFF && (magic[1] & 0xFE) === 0xFA)
243-
canPlay = Module["SDL2Mixer"].canPlayType("audio/mp3");
244+
if (!canPlay) {
245+
const magic = buf.slice(0, 2);
246+
if (magic[0] === 0xFF && (magic[1] & 0xFE) === 0xFA)
247+
canPlay = Module["SDL2Mixer"].canPlayType("audio/mp3");
248+
}
244249

245250
return canPlay;
246251
},
@@ -334,21 +339,23 @@ static void *MusicHTML5_CreateFromRW(SDL_RWops *src, int freesrc)
334339

335340
if (buf && size > 0)
336341
{
342+
SDL_bool force = (SDL_GetHint("SDL_MIXER_HTML5_DISABLE_TYPE_CHECK") != NULL);
337343
id = EM_ASM_INT({
338344
const ptr = $0;
339345
const size = $1;
340346
const context = $2;
347+
const force = $3;
341348

342349
const buf = new Uint8Array(Module.HEAPU8.buffer, ptr, size);
343350

344-
if (!Module["SDL2Mixer"].canPlayMagic(buf))
351+
if (!force && !Module["SDL2Mixer"].canPlayMagic(buf))
345352
return -1;
346353

347354
const url = Module["SDL2Mixer"].createBlob(buf);
348355
const id = Module["SDL2Mixer"].createMusic(url, context);
349356

350357
return id;
351-
}, buf, size, music);
358+
}, buf, size, music, force);
352359
}
353360

354361
if (id == -1)
@@ -376,6 +383,7 @@ static void *MusicHTML5_CreateFromFile(const char *file)
376383
{
377384
MusicHTML5 *music = (MusicHTML5 *)SDL_calloc(1, sizeof *music);;
378385
int id = -1;
386+
SDL_bool force = (SDL_GetHint("SDL_MIXER_HTML5_DISABLE_TYPE_CHECK") != NULL);
379387

380388
if (music == NULL) {
381389
Mix_SetError("Out of memory");
@@ -385,14 +393,15 @@ static void *MusicHTML5_CreateFromFile(const char *file)
385393
id = EM_ASM_INT({
386394
const file = UTF8ToString($0);
387395
const context = $1;
396+
const force = $2;
388397

389398
let url;
390399
try {
391400
// Is path in FS?
392401
const buf = FS.readFile(file);
393402
url = Module["SDL2Mixer"].createBlob(buf);
394403

395-
let canPlay = Module["SDL2Mixer"].canPlayFile(file);
404+
let canPlay = force || Module["SDL2Mixer"].canPlayFile(file);
396405

397406
if (!canPlay)
398407
canPlay = Module["SDL2Mixer"].canPlayMagic(buf);
@@ -405,13 +414,13 @@ static void *MusicHTML5_CreateFromFile(const char *file)
405414
url = file;
406415

407416
// Check audio capability by file extension
408-
if (!Module["SDL2Mixer"].canPlayFile(url))
417+
if (!force && !Module["SDL2Mixer"].canPlayFile(url))
409418
return -1;
410419
}
411420

412421
const id = Module["SDL2Mixer"].createMusic(url, context);
413422
return id;
414-
}, file, music);
423+
}, file, music, force);
415424

416425
if (id == -1) {
417426
SDL_free(music);

0 commit comments

Comments
 (0)