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

FLVDemuxer: FLV use big-endian #415

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 11 additions & 15 deletions src/demux/amf-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ import Log from '../utils/logger.js';
import decodeUTF8 from '../utils/utf8-conv.js';
import {IllegalStateException} from '../utils/exception.js';

let le = (function () {
let buf = new ArrayBuffer(2);
(new DataView(buf)).setInt16(0, 256, true); // little-endian write
return (new Int16Array(buf))[0] === 256; // platform-spec read, if equal then LE
})();
let le = false; // amf use big-endian

class AMF {

Expand Down Expand Up @@ -70,7 +66,7 @@ class AMF {
throw new IllegalStateException('Data not enough when parse String');
}
let v = new DataView(arrayBuffer, dataOffset, dataSize);
let length = v.getUint16(0, !le);
let length = v.getUint16(0, le);

let str;
if (length > 0) {
Expand All @@ -90,7 +86,7 @@ class AMF {
throw new IllegalStateException('Data not enough when parse LongString');
}
let v = new DataView(arrayBuffer, dataOffset, dataSize);
let length = v.getUint32(0, !le);
let length = v.getUint32(0, le);

let str;
if (length > 0) {
Expand All @@ -110,8 +106,8 @@ class AMF {
throw new IllegalStateException('Data size invalid when parse Date');
}
let v = new DataView(arrayBuffer, dataOffset, dataSize);
let timestamp = v.getFloat64(0, !le);
let localTimeOffset = v.getInt16(8, !le);
let timestamp = v.getFloat64(0, le);
let localTimeOffset = v.getInt16(8, le);
timestamp += localTimeOffset * 60 * 1000; // get UTC time

return {
Expand All @@ -135,7 +131,7 @@ class AMF {
try {
switch (type) {
case 0: // Number(Double) type
value = v.getFloat64(1, !le);
value = v.getFloat64(1, le);
offset += 8;
break;
case 1: { // Boolean type
Expand All @@ -153,7 +149,7 @@ class AMF {
case 3: { // Object(s) type
value = {};
let terminal = 0; // workaround for malformed Objects which has missing ScriptDataObjectEnd
if ((v.getUint32(dataSize - 4, !le) & 0x00FFFFFF) === 9) {
if ((v.getUint32(dataSize - 4, le) & 0x00FFFFFF) === 9) {
terminal = 3;
}
while (offset < dataSize - 4) { // 4 === type(UI8) + ScriptDataObjectEnd(UI24)
Expand All @@ -164,7 +160,7 @@ class AMF {
offset += amfobj.size;
}
if (offset <= dataSize - 3) {
let marker = v.getUint32(offset - 1, !le) & 0x00FFFFFF;
let marker = v.getUint32(offset - 1, le) & 0x00FFFFFF;
if (marker === 9) {
offset += 3;
}
Expand All @@ -175,7 +171,7 @@ class AMF {
value = {};
offset += 4; // ECMAArrayLength(UI32)
let terminal = 0; // workaround for malformed MixedArrays which has missing ScriptDataObjectEnd
if ((v.getUint32(dataSize - 4, !le) & 0x00FFFFFF) === 9) {
if ((v.getUint32(dataSize - 4, le) & 0x00FFFFFF) === 9) {
terminal = 3;
}
while (offset < dataSize - 8) { // 8 === type(UI8) + ECMAArrayLength(UI32) + ScriptDataVariableEnd(UI24)
Expand All @@ -186,7 +182,7 @@ class AMF {
offset += amfvar.size;
}
if (offset <= dataSize - 3) {
let marker = v.getUint32(offset - 1, !le) & 0x00FFFFFF;
let marker = v.getUint32(offset - 1, le) & 0x00FFFFFF;
if (marker === 9) {
offset += 3;
}
Expand All @@ -201,7 +197,7 @@ class AMF {
case 10: { // Strict array type
// ScriptDataValue[n]. NOTE: according to video_file_format_spec_v10_1.pdf
value = [];
let strictArrayLength = v.getUint32(1, !le);
let strictArrayLength = v.getUint32(1, le);
offset += 4;
for (let i = 0; i < strictArrayLength; i++) {
let val = AMF.parseValue(arrayBuffer, dataOffset + offset, dataSize - offset);
Expand Down
22 changes: 9 additions & 13 deletions src/demux/flv-demuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ class FLVDemuxer {
this._videoTrack = {type: 'video', id: 1, sequenceNumber: 0, samples: [], length: 0};
this._audioTrack = {type: 'audio', id: 2, sequenceNumber: 0, samples: [], length: 0};

this._littleEndian = (function () {
let buf = new ArrayBuffer(2);
(new DataView(buf)).setInt16(0, 256, true); // little-endian write
return (new Int16Array(buf))[0] === 256; // platform-spec read, if equal then LE
})();
this._littleEndian = false; // flv use big-endian
}

destroy() {
Expand Down Expand Up @@ -289,7 +285,7 @@ class FLVDemuxer {
}

let v = new DataView(chunk, offset);
let prevTagSize0 = v.getUint32(0, !le);
let prevTagSize0 = v.getUint32(0, le);
if (prevTagSize0 !== 0) {
Log.w(this.TAG, 'PrevTagSize0 !== 0 !!!');
}
Expand All @@ -307,7 +303,7 @@ class FLVDemuxer {
}

let tagType = v.getUint8(0);
let dataSize = v.getUint32(0, !le) & 0x00FFFFFF;
let dataSize = v.getUint32(0, le) & 0x00FFFFFF;

if (offset + 11 + dataSize + 4 > chunk.byteLength) {
// data not enough for parsing actual data body
Expand All @@ -328,7 +324,7 @@ class FLVDemuxer {

let timestamp = ts0 | (ts1 << 8) | (ts2 << 16) | (ts3 << 24);

let streamId = v.getUint32(7, !le) & 0x00FFFFFF;
let streamId = v.getUint32(7, le) & 0x00FFFFFF;
if (streamId !== 0) {
Log.w(this.TAG, 'Meet tag which has StreamID != 0!');
}
Expand All @@ -347,7 +343,7 @@ class FLVDemuxer {
break;
}

let prevTagSize = v.getUint32(11 + dataSize, !le);
let prevTagSize = v.getUint32(11 + dataSize, le);
if (prevTagSize !== 11 + dataSize) {
Log.w(this.TAG, `Invalid PrevTagSize ${prevTagSize}`);
}
Expand Down Expand Up @@ -855,7 +851,7 @@ class FLVDemuxer {
let v = new DataView(arrayBuffer, dataOffset, dataSize);

let packetType = v.getUint8(0);
let cts_unsigned = v.getUint32(0, !le) & 0x00FFFFFF;
let cts_unsigned = v.getUint32(0, le) & 0x00FFFFFF;
let cts = (cts_unsigned << 8) >> 8; // convert to 24-bit signed int

if (packetType === 0) { // AVCDecoderConfigurationRecord
Expand Down Expand Up @@ -925,7 +921,7 @@ class FLVDemuxer {
let offset = 6;

for (let i = 0; i < spsCount; i++) {
let len = v.getUint16(offset, !le); // sequenceParameterSetLength
let len = v.getUint16(offset, le); // sequenceParameterSetLength
offset += 2;

if (len === 0) {
Expand Down Expand Up @@ -1010,7 +1006,7 @@ class FLVDemuxer {
offset++;

for (let i = 0; i < ppsCount; i++) {
let len = v.getUint16(offset, !le); // pictureParameterSetLength
let len = v.getUint16(offset, le); // pictureParameterSetLength
offset += 2;

if (len === 0) {
Expand Down Expand Up @@ -1055,7 +1051,7 @@ class FLVDemuxer {
break; // data not enough for next Nalu
}
// Nalu with length-header (AVC1)
let naluSize = v.getUint32(offset, !le); // Big-Endian read
let naluSize = v.getUint32(offset, le); // Big-Endian read
if (lengthSize === 3) {
naluSize >>>= 8;
}
Expand Down