Skip to content

fix: mark fileStart mandatory on MP4BoxBuffer #466

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

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 5 additions & 7 deletions src/DataStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class DataStream {

_buffer?: MP4BoxBuffer;
_byteOffset?: number;
_dataView?: DataView<ArrayBuffer>;
_dataView?: DataView<MP4BoxBuffer>;

endianness: boolean;
position: number;
Expand All @@ -50,14 +50,12 @@ export class DataStream {
* @param endianness DataStream.BIG_ENDIAN or DataStream.LITTLE_ENDIAN (the default).
*/
constructor(
arrayBuffer?: ArrayBuffer | DataView<ArrayBuffer> | number,
arrayBuffer?: DataView<MP4BoxBuffer> | number,
byteOffset?: number,
endianness?: boolean | null,
) {
this._byteOffset = byteOffset || 0;
if (arrayBuffer instanceof ArrayBuffer) {
this.buffer = arrayBuffer;
} else if (arrayBuffer instanceof DataView) {
if (arrayBuffer instanceof DataView) {
this.dataView = arrayBuffer;
if (byteOffset) this._byteOffset += byteOffset;
} else {
Expand Down Expand Up @@ -181,7 +179,7 @@ export class DataStream {
get dataView() {
return this._dataView;
}
set dataView(value: DataView<ArrayBuffer>) {
set dataView(value: DataView<MP4BoxBuffer>) {
this._byteOffset = value.byteOffset;
this._buffer = value.buffer;
this._dataView = new DataView(this._buffer, this._byteOffset);
Expand Down Expand Up @@ -346,7 +344,7 @@ export class DataStream {
* @param e Endianness of the data to read.
* @return The read Uint8Array.
*/
readUint8Array(length: number | null) {
readUint8Array(length: number | null): Uint8Array {
length = length === null ? this.byteLength - this.position : length;
const arr = new Uint8Array(length);
DataStream.memcpy(
Expand Down
10 changes: 9 additions & 1 deletion src/boxes/esds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FullBox } from '#/box';
import type { MultiBufferStream } from '#/buffer';
import { DataStream } from '#/DataStream';
import type { ES_Descriptor } from '#/descriptor';
import type { MP4BoxBuffer } from '#/mp4boxbuffer';
import { DescriptorRegistry } from '#/registry';

export class esdsBox extends FullBox {
Expand All @@ -16,8 +17,15 @@ export class esdsBox extends FullBox {
// NOTE: This used to be `typeof MPEG4DescriptorParser !== 'undefined'`
if ('MPEG4DescriptorParser' in DescriptorRegistry) {
const esd_parser = new DescriptorRegistry.MPEG4DescriptorParser();
const arrayBuffer = new ArrayBuffer(esd_data.length);
const uint8Array = new Uint8Array(arrayBuffer);
uint8Array.set(esd_data);

const mp4Buffer: MP4BoxBuffer = Object.assign(arrayBuffer, { fileStart: 0 });
const mp4Dataview = new DataView(mp4Buffer, 0, arrayBuffer.byteLength);

this.esd = esd_parser.parseOneDescriptor(
new DataStream(esd_data.buffer, 0, DataStream.BIG_ENDIAN),
new DataStream(mp4Dataview, 0, DataStream.BIG_ENDIAN),
) as ES_Descriptor;
}
}
Expand Down
21 changes: 13 additions & 8 deletions src/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ import type { MP4BoxBuffer } from './mp4boxbuffer';
* @param buffer2
* @return the concatenation of buffer1 and buffer2 in that order
*/
function concatBuffers(buffer1: ArrayBuffer, buffer2: ArrayBuffer) {
function concatBuffers(buffer1: MP4BoxBuffer, buffer2: MP4BoxBuffer): MP4BoxBuffer {
Log.debug(
'ArrayBuffer',
'Trying to create a new buffer of size: ' + (buffer1.byteLength + buffer2.byteLength),
);
const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer as MP4BoxBuffer;

const arrayBuffer = tmp.buffer;

return Object.assign(arrayBuffer, {
fileStart: buffer1.fileStart,
usedBytes: buffer1.usedBytes + buffer2.usedBytes,
});
}

/**
Expand All @@ -31,7 +37,7 @@ export class MultiBufferStream extends DataStream {
bufferIndex: number;

constructor(buffer?: MP4BoxBuffer) {
super(new ArrayBuffer(), 0, DataStream.BIG_ENDIAN);
super(undefined, 0, DataStream.BIG_ENDIAN);
// List of ArrayBuffers, with a fileStart property, sorted in fileStart order and non-overlapping
this.buffers = [];
this.bufferIndex = -1;
Expand Down Expand Up @@ -73,14 +79,13 @@ export class MultiBufferStream extends DataStream {
* @param {ArrayBuffer} buffer
* @param {Number} offset the start of new buffer
* @param {Number} newLength the length of the new buffer
* @return {ArrayBuffer} the new buffer
* @return {MP4BoxBuffer} the new buffer
*/
reduceBuffer(buffer: MP4BoxBuffer, offset: number, newLength: number) {
reduceBuffer(buffer: MP4BoxBuffer, offset: number, newLength: number): MP4BoxBuffer {
const smallB = new Uint8Array(newLength);
smallB.set(new Uint8Array(buffer, offset, newLength));
(smallB.buffer as MP4BoxBuffer).fileStart = buffer.fileStart + offset;
(smallB.buffer as MP4BoxBuffer).usedBytes = 0;
return smallB.buffer as MP4BoxBuffer;

return Object.assign(smallB.buffer, { fileStart: buffer.fileStart + offset, usedBytes: 0 });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/mp4boxbuffer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class MP4BoxBuffer extends ArrayBuffer {
fileStart?: number;
fileStart: number;
usedBytes?: number;
}