-
Notifications
You must be signed in to change notification settings - Fork 8
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1f15060
feat: add container requesting and checking logic
brandonocasey 06c709f
update sinon, add a build for node
brandonocasey b74caa6
watch node tests as well
brandonocasey 7e1bd40
code review comments
brandonocasey b256e65
remove node build
brandonocasey 082bf2c
add raw byte mode
brandonocasey d1f07bb
only remove sinon
brandonocasey 6b9b10b
move fmp4 helper to here
brandonocasey 626e4ec
support unsync ts
brandonocasey c48700f
cr
brandonocasey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,5 +38,4 @@ files.forEach(function(file, i) { | |
}); | ||
|
||
// export the builds to rollup | ||
|
||
export default builds; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
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; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.