diff --git a/README.md b/README.md index 752d504..a453b46 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,11 @@ --mdat [audio data] ``` -## Methods +## API + +### `MSEAudioWrapper` + +_Default export_ `const wrapper = new MSEAudioWrapper("audio/mpeg", {minFramesPerSegment: 2, minBytesPerSegment: 576, preferredContainer: "webm"});` * `constructor` @@ -133,6 +137,20 @@ * OPUS (ISOBMFF) - `audio/mp4;codecs="opus"` * OPUS (WEBM) - `audio/webm;codecs="opus"` * Vorbis - `audio/webm;codecs="vorbis"` + +### `getWrappedMimeType(codec, container = WEBM)` + +_Named export_ + +Returns the mime-type that MSEAudioWrapper will produce given a passed in codec and container type. + +* `codec` *required* + * Codec of the audio data to wrap + * Accepts `aac`, `flac`, `mpeg`, `opus`, `vorbis` +* `container` *optional* + * Accepts `"webm"`, `"fmp4"` + * Defaults to `"webm"` + --- diff --git a/package-lock.json b/package-lock.json index 3321af8..d55aa38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,18 @@ { "name": "mse-audio-wrapper", - "version": "1.4.14", + "version": "1.4.15", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "mse-audio-wrapper", - "version": "1.4.14", + "version": "1.4.15", "license": "LGPL-3.0-or-later", "dependencies": { - "codec-parser": "2.4.3" + "codec-parser": "2.5.0" }, "devDependencies": { - "prettier": "^3.0.1" + "prettier": "^3.3.3" }, "funding": { "type": "individual", @@ -20,14 +20,14 @@ } }, "node_modules/codec-parser": { - "version": "2.4.3", - "resolved": "https://registry.npmjs.org/codec-parser/-/codec-parser-2.4.3.tgz", - "integrity": "sha512-3dAvFtdpxn4YLstqsB2ZiJXXNg7n1j7R5ONeDuk+2kBkb39PwrCRytOFHlSWA8q5jCjW3PumeMv9q37bFHsijg==" + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/codec-parser/-/codec-parser-2.5.0.tgz", + "integrity": "sha512-Ru9t80fV8B0ZiixQl8xhMTLru+dzuis/KQld32/x5T/+3LwZb0/YvQdSKytX9JqCnRdiupvAvyYJINKrXieziQ==" }, "node_modules/prettier": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.1.tgz", - "integrity": "sha512-fcOWSnnpCrovBsmFZIGIy9UqK2FaI7Hqax+DIO0A9UxeVoY4iweyaFjS5TavZN97Hfehph0nhsZnjlVKzEQSrQ==", + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.3.3.tgz", + "integrity": "sha512-i2tDNA0O5IrMO757lfrdQZCc2jPNDVntV0m/+4whiDfWaTKfMNgR7Qz0NAeGz/nRqF4m5/6CLzbP4/liHt12Ew==", "dev": true, "bin": { "prettier": "bin/prettier.cjs" diff --git a/package.json b/package.json index c583919..19fe9ab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mse-audio-wrapper", - "version": "1.4.14", + "version": "1.4.15", "description": "Library to enable Media Source Extensions API playback for unsupported audio containers and raw codecs", "main": "src/MSEAudioWrapper.js", "type": "module", @@ -44,9 +44,9 @@ "url": "https://github.com/sponsors/eshaz" }, "dependencies": { - "codec-parser": "2.4.3" + "codec-parser": "2.5.0" }, "devDependencies": { - "prettier": "^3.0.1" + "prettier": "^3.3.3" } } diff --git a/src/MSEAudioWrapper.js b/src/MSEAudioWrapper.js index a038d28..f0b3534 100644 --- a/src/MSEAudioWrapper.js +++ b/src/MSEAudioWrapper.js @@ -34,6 +34,23 @@ import WEBMContainer from "./containers/webm/WEBMContainer.js"; const noOp = () => {}; +export const getWrappedMimeType = (codec, container = WEBM) => { + switch (codec) { + case "mpeg": + return `${AUDIO_MP4}"${MP3}"`; + case "aac": + return `${AUDIO_MP4}"${MP4A_40_2}"`; + case "flac": + return `${AUDIO_MP4}"${FLAC}"`; + case "vorbis": + return `${AUDIO_WEBM}"${VORBIS}"`; + case "opus": + return container === WEBM + ? `${AUDIO_WEBM}"${OPUS}"` + : `${AUDIO_MP4}"${OPUS}"`; + } +}; + export default class MSEAudioWrapper { /** * @description Wraps audio data into media source API compatible containers @@ -166,29 +183,23 @@ export default class MSEAudioWrapper { * @private */ _getContainer(codec) { + this._mimeType = getWrappedMimeType(codec, this.PREFERRED_CONTAINER); + switch (codec) { case "mpeg": - this._mimeType = `${AUDIO_MP4}"${MP3}"`; return new ISOBMFFContainer(MP3); case "aac": - this._mimeType = `${AUDIO_MP4}"${MP4A_40_2}"`; return new ISOBMFFContainer(MP4A_40_2); case "flac": - this._mimeType = `${AUDIO_MP4}"${FLAC}"`; return new ISOBMFFContainer(FLAC); case "vorbis": - this._mimeType = `${AUDIO_WEBM}"${VORBIS}"`; - this.MAX_SAMPLES_PER_SEGMENT = 32767; return new WEBMContainer(VORBIS); case "opus": if (this.PREFERRED_CONTAINER === WEBM) { - this._mimeType = `${AUDIO_WEBM}"${OPUS}"`; - this.MAX_SAMPLES_PER_SEGMENT = 32767; return new WEBMContainer(OPUS); } - this._mimeType = `${AUDIO_MP4}"${OPUS}"`; return new ISOBMFFContainer(OPUS); } }