Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detect mp4 starting with moof/moov box as mp4 #29

Merged
merged 5 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 30 additions & 44 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,81 +8,67 @@ jobs:
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
should-skip-job: ${{ steps.skip-check.outputs.should_skip }}
should-skip-job: ${{steps.skip-check.outputs.should_skip}}
steps:
- id: skip-check
uses: fkirc/skip-duplicate-actions@v2.1.0
with:
github_token: ${{ github.token }}
github_token: ${{github.token}}

ci:
needs: should-skip
if: ${{ needs.should-skip.outputs.should-skip-job != 'true'}}
env:
BROWSER_STACK_USERNAME: ${{ secrets.BROWSER_STACK_USERNAME }}
BROWSER_STACK_ACCESS_KEY: ${{ secrets.BROWSER_STACK_ACCESS_KEY }}
runs-on: ${{ matrix.os }}
if: ${{needs.should-skip.outputs.should-skip-job != 'true' || github.ref == 'refs/heads/main'}}
strategy:
# TODO: test IE 11, Legacy Edge, and New Edge on windows-latest
# test Safari on macos-latest
fail-fast: false
matrix:
os: [ubuntu-latest]

test-type: ['unit', 'coverage']
env:
BROWSER_STACK_USERNAME: ${{secrets.BROWSER_STACK_USERNAME}}
BROWSER_STACK_ACCESS_KEY: ${{secrets.BROWSER_STACK_ACCESS_KEY}}
CI_TEST_TYPE: ${{matrix.test-type}}
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- name: Cache dependencies
uses: actions/cache@v2
with:
path: |
~/.npm
**/node_modules
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
${{ runner.os }}-
- name: checkout code
uses: actions/checkout@v2

- name: Read .nvmrc
- name: read node version from .nvmrc
run: echo ::set-output name=NVMRC::$(cat .nvmrc)
shell: bash
id: nvm

- name: update apt cache on linux w/o browserstack
run: sudo apt-get update
if: ${{ startsWith(matrix.os, 'ubuntu') && !env.BROWSER_STACK_USERNAME }}

- name: Install ffmpeg/pulseaudio for firefox on linux w/o browserstack
- name: install ffmpeg/pulseaudio for firefox on linux w/o browserstack
run: sudo apt-get install ffmpeg pulseaudio
if: ${{ startsWith(matrix.os, 'ubuntu') && !env.BROWSER_STACK_USERNAME }}

- name: start pulseaudio for firefox on linux w/o browserstack
run: pulseaudio -D
if: ${{ startsWith(matrix.os, 'ubuntu') && !env.BROWSER_STACK_USERNAME }}

- name: Setup node
uses: actions/setup-node@v1
- name: setup node
uses: actions/setup-node@v2
with:
node-version: '${{ steps.nvm.outputs.NVMRC }}'
node-version: '${{steps.nvm.outputs.NVMRC}}'
cache: npm

# turn off the default setup-node problem watchers...
- run: echo "::remove-matcher owner=eslint-compact::"
- run: echo "::remove-matcher owner=eslint-stylish::"
- run: echo "::remove-matcher owner=tsc::"

- run: npm i --prefer-offline --no-audit

# run safari on macos
- name: Run Mac test
run: npm run test -- --browsers Safari
if: ${{ startsWith(matrix.os, 'macos') }}
- name: npm install
run: npm i --prefer-offline --no-audit

# only run ie 11 on windows
- name: Run Windows test
run: npm run test -- --browsers IE
if: ${{ startsWith(matrix.os, 'windows') }}

# run chrome/firefox or browserstack in linux
- name: Run linux test
- name: run npm test
uses: GabrielBB/xvfb-action@v1
with:
run: npm test
if: ${{ startsWith(matrix.os, 'ubuntu') }}
run: npm run test

- name: coverage
uses: codecov/codecov-action@v1
with:
token: ${{secrets.CODECOV_TOKEN}}
files: './test/dist/coverage/coverage-final.json'
fail_ci_if_error: true
if: ${{startsWith(env.CI_TEST_TYPE, 'coverage')}}
27 changes: 22 additions & 5 deletions src/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ const CONSTANTS = {
// "styp" string literal in hex
'fmp4': toUint8([0x73, 0x74, 0x79, 0x70]),

// "ftyp" string literal in hex
'mov': toUint8([0x66, 0x74, 0x79, 0x70, 0x71, 0x74])
// "ftypqt" string literal in hex
'mov': toUint8([0x66, 0x74, 0x79, 0x70, 0x71, 0x74]),

// moov string literal in hex
'moov': toUint8([0x6D, 0x6F, 0x6F, 0x76]),

// moof string literal in hex
'moof': toUint8([0x6D, 0x6F, 0x6F, 0x66])
};

const _isLikely = {
Expand Down Expand Up @@ -71,9 +77,20 @@ const _isLikely = {
},

mp4(bytes) {
return !_isLikely['3gp'](bytes) && !_isLikely.mov(bytes) &&
(bytesMatch(bytes, CONSTANTS.mp4, {offset: 4}) ||
bytesMatch(bytes, CONSTANTS.fmp4, {offset: 4}));
// if this file is another base media file format, it is not mp4
if (_isLikely['3gp'](bytes) || _isLikely.mov(bytes)) {
return false;
}

// if this file starts with a ftyp or styp box its mp4
if (bytesMatch(bytes, CONSTANTS.mp4, {offset: 4}) || bytesMatch(bytes, CONSTANTS.fmp4, {offset: 4})) {
return true;
}

// if this file starts with a moof/moov box its mp4
if (bytesMatch(bytes, CONSTANTS.moof, {offset: 4}) || bytesMatch(bytes, CONSTANTS.moov, {offset: 4})) {
return true;
}
},
mov(bytes) {
return bytesMatch(bytes, CONSTANTS.mov, {offset: 4});
Expand Down
12 changes: 10 additions & 2 deletions test/container.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ h265shortnal.splice(0, 1);
// remove 0x00 from the back
h265shortnal.splice(h265shortnal.length - 2, 1);

const mp4Variants = {
'start with moov': concatTypedArrays(filler(4), [0x6D, 0x6F, 0x6F, 0x76]),
'start with moof': concatTypedArrays(filler(4), [0x6D, 0x6F, 0x6F, 0x66]),
'start with styp': concatTypedArrays(filler(4), [0x73, 0x74, 0x79, 0x70])
};

QUnit.module('detectContainerForBytes');

QUnit.test('should identify known types', function(assert) {
Expand All @@ -96,9 +102,11 @@ QUnit.test('should identify known types', function(assert) {
assert.equal(detectContainerForBytes(data), key, `found ${key} with Uint8Array`);
});

const mp4Bytes = concatTypedArrays([0x00, 0x00, 0x00, 0x00], stringToBytes('styp'));
Object.keys(mp4Variants).forEach(function(name) {
const bytes = mp4Variants[name];

assert.equal(detectContainerForBytes(mp4Bytes), 'mp4', 'styp mp4 detected as mp4');
assert.equal(detectContainerForBytes(bytes), 'mp4', `${name} detected as mp4`);
});

// mp3/aac/flac/ac3 audio can have id3 data before the
// signature for the file, so we need to handle that.
Expand Down