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

feat: add container requesting and checking logic #12

Merged
merged 10 commits into from
Apr 27, 2020
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
111 changes: 0 additions & 111 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"version": "is-prerelease || npm run update-changelog && git add CHANGELOG.md",
"watch": "npm-run-all -p watch:*",
"watch:js": "npm run build:js -- -w",
"watch:node": "npm run test:node -- --watch",
"prepublishOnly": "npm-run-all build-prod && vjsverify --verbose --skip-es-check"
},
"engines": {
Expand Down Expand Up @@ -79,7 +80,6 @@
"@videojs/generator-helpers": "~1.2.0",
"karma": "^4.0.0",
"rollup": "^1.12.0",
"sinon": "^7.2.2",
"videojs-generate-karma-config": "~5.3.0",
"videojs-generate-rollup-config": "~5.0.0",
"videojs-generator-verify": "~1.2.0",
Expand Down
1 change: 0 additions & 1 deletion scripts/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ files.forEach(function(file, i) {
});

// export the builds to rollup

export default builds;
46 changes: 46 additions & 0 deletions src/byte-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export const isTypedArray = (obj) => ArrayBuffer.isView(obj);
export const toUint8 = (bytes) => (bytes instanceof Uint8Array) ?
bytes :
new Uint8Array(
bytes && bytes.buffer || bytes,
bytes && bytes.byteOffset || 0,
bytes && bytes.byteLength || 0
);

export const bytesToString = (bytes) => {
if (!bytes) {
return '';
}

bytes = Array.prototype.slice.call(bytes);

const string = String.fromCharCode.apply(null, toUint8(bytes));

try {
return decodeURIComponent(escape(string));
} catch (e) {
// if decodeURIComponent/escape fails, we are dealing with partial
// or full non string data. Just return the potentially garbled string.
}

return string;
};

export const stringToBytes = (string, stringIsBytes = false) => {
const bytes = [];

if (typeof string !== 'string' && string && typeof string.toString === 'function') {
string = string.toString();
}

if (typeof string !== 'string') {
return bytes;
}

// If the string already is bytes, we don't have to do this
if (!stringIsBytes) {
string = unescape(encodeURIComponent(string));
}

return string.split('').map((s) => s.charCodeAt(0) & 0xFF);
};
141 changes: 141 additions & 0 deletions src/containers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import {bytesToString, toUint8} from './byte-helpers.js';

export const getId3Offset = function(bytes) {
bytes = toUint8(bytes);

if (bytes.length < 10 || bytesToString(bytes.subarray(0, 3)) !== 'ID3') {
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is assuming that bytes always contains valid ID3 data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it doesn't then we likely won't be able to determine the type.

}
const returnSize = (bytes[6] << 21) |
(bytes[7] << 14) |
(bytes[8] << 7) |
(bytes[9]);
const flags = bytes[5];
const footerPresent = (flags & 16) >> 4;

if (footerPresent) {
return returnSize + 20;
}
return returnSize + 10;
};

export const isLikely = {
aac(bytes) {
const offset = getId3Offset(bytes);

return bytes.length >= offset + 2 &&
(bytes[offset] & 0xFF) === 0xFF &&
(bytes[offset + 1] & 0xE0) === 0xE0 &&
(bytes[offset + 1] & 0x16) === 0x10;
},

mp3(bytes) {
gkatsev marked this conversation as resolved.
Show resolved Hide resolved
const offset = getId3Offset(bytes);

return bytes.length >= offset + 2 &&
(bytes[offset] & 0xFF) === 0xFF &&
(bytes[offset + 1] & 0xE0) === 0xE0 &&
(bytes[offset + 1] & 0x06) === 0x02;
},

webm(bytes) {
return bytes.length >= 4 &&
(bytes[0] & 0xFF) === 0x1A &&
(bytes[1] & 0xFF) === 0x45 &&
(bytes[2] & 0xFF) === 0xDF &&
(bytes[3] & 0xFF) === 0xA3;
},

mp4(bytes) {
return bytes.length >= 8 &&
(/^(f|s)typ$/).test(bytesToString(bytes.subarray(4, 8))) &&
// not 3gp data
!(/^ftyp3g$/).test(bytesToString(bytes.subarray(4, 10)));
},

'3gp'(bytes) {
return bytes.length >= 10 &&
(/^ftyp3g$/).test(bytesToString(bytes.subarray(4, 10)));
},

ts(bytes) {
if (bytes.length < 189 && bytes.length >= 1) {
return bytes[0] === 0x47;
}

let i = 0;

// check the first 376 bytes for two matching sync bytes
while (i + 188 < bytes.length && i < 188) {
if (bytes[i] === 0x47 && bytes[i + 188] === 0x47) {
return true;
}
i += 1;
}

return false;
},
flac(bytes) {
return bytes.length >= 4 &&
(/^fLaC$/).test(bytesToString(bytes.subarray(0, 4)));
},
ogg(bytes) {
return bytes.length >= 4 &&
(/^OggS$/).test(bytesToString(bytes.subarray(0, 4)));
}
};

// get all the isLikely functions
// but make sure 'ts' is at the bottom
// as it is the least specific
const isLikelyTypes = Object.keys(isLikely)
// remove ts
.filter((t) => t !== 'ts')
// add it back to the bottom
.concat('ts');

// make sure we are dealing with uint8 data.
isLikelyTypes.forEach(function(type) {
const isLikelyFn = isLikely[type];

isLikely[type] = (bytes) => isLikelyFn(toUint8(bytes));
});

// A useful list of file signatures can be found here
// https://en.wikipedia.org/wiki/List_of_file_signatures
export const detectContainerForBytes = (bytes) => {
bytes = toUint8(bytes);

for (let i = 0; i < isLikelyTypes.length; i++) {
const type = isLikelyTypes[i];

if (isLikely[type](bytes)) {
return type;
}
}

return '';
};

// fmp4 is not a container
export const isLikelyFmp4MediaSegment = (bytes) => {
bytes = toUint8(bytes);
let i = 0;

while (i < bytes.length) {
const size = (bytes[i] << 24 | bytes[i + 1] << 16 | bytes[i + 2] << 8 | bytes[i + 3]) >>> 0;
const type = bytesToString(bytes.subarray(i + 4, i + 8));

if (type === 'moof') {
return true;
}

if (size === 0 || (size + i) > bytes.length) {
i = bytes.length;
} else {
i += size;
}
}

return false;
};
Loading