Skip to content

Evi/typings #458

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 7 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
8 changes: 4 additions & 4 deletions src/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export class ContainerBox extends Box {
subBoxNames?: ReadonlyArray<string>;

/** @bundle box-write.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.size = 0;
this.writeHeader(stream);
for (let i = 0; i < this.boxes.length; i++) {
Expand All @@ -224,7 +224,7 @@ export class ContainerBox extends Box {
/* adjusting the size, now that all sub-boxes are known */
Log.debug('BoxWriter', 'Adjusting box ' + this.type + ' with new size ' + this.size);
stream.adjustUint32(this.sizePosition, this.size);
}
};

/** @bundle box-print.js */
print(output: Output) {
Expand Down Expand Up @@ -278,9 +278,9 @@ export class SampleGroupEntry {
constructor(public grouping_type: string) {}

/** @bundle writing/samplegroups/samplegroup.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
stream.writeUint8Array(this.data);
}
};

/** @bundle parsing/samplegroups/samplegroup.js */
parse(stream: MultiBufferStream) {
Expand Down
11 changes: 6 additions & 5 deletions src/boxes/avcC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class avcCBox extends Box {
SPS: ParameterSetArray;
nb_PPS_nalus: number;
PPS: ParameterSetArray;
ext: Uint8Array;
ext: Uint8Array | undefined;

parse(stream: DataStream | MP4BoxStream) {
this.configurationVersion = stream.readUint8();
Expand Down Expand Up @@ -53,15 +53,16 @@ export class avcCBox extends Box {
}

/** @bundle writing/avcC.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
console.log(this);
this.size = 7;
for (let i = 0; i < this.SPS.length; i++) {
this.size += 2 + this.SPS[i].length;
}
for (let i = 0; i < this.PPS.length; i++) {
this.size += 2 + this.PPS[i].length;
}
if (this.ext) {
if (this.ext !== undefined) {
this.size += this.ext.length;
}
this.writeHeader(stream);
Expand All @@ -80,8 +81,8 @@ export class avcCBox extends Box {
stream.writeUint16(this.PPS[i].length);
stream.writeUint8Array(this.PPS[i].data);
}
if (this.ext) {
if (this.ext !== undefined) {
stream.writeUint8Array(this.ext);
}
}
};
}
4 changes: 2 additions & 2 deletions src/boxes/co64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class co64Box extends FullBox {
}

/** @bundle writing/co64.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.version = 0;
this.flags = 0;
this.size = 4 + 8 * this.chunk_offsets.length;
Expand All @@ -28,5 +28,5 @@ export class co64Box extends FullBox {
for (let i = 0; i < this.chunk_offsets.length; i++) {
stream.writeUint64(this.chunk_offsets[i]);
}
}
};
}
4 changes: 2 additions & 2 deletions src/boxes/cslg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class cslgBox extends FullBox {
}

/** @bundle writing/cslg.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.version = 0;
if (
this.compositionToDTSShift > INT32_MAX ||
Expand Down Expand Up @@ -60,5 +60,5 @@ export class cslgBox extends FullBox {
stream.writeInt64(this.compositionStartTime);
stream.writeInt64(this.compositionEndTime);
}
}
};
}
4 changes: 2 additions & 2 deletions src/boxes/ctts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class cttsBox extends FullBox {
}

/** @bundle writing/ctts.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.version = 0;
this.flags = 0;
this.size = 4 + 8 * this.sample_counts.length;
Expand All @@ -50,7 +50,7 @@ export class cttsBox extends FullBox {
stream.writeUint32(this.sample_offsets[i]); /* unsigned */
}
}
}
};

/** @bundle box-unpack.js */
unpack(samples: Array<Sample>) {
Expand Down
6 changes: 3 additions & 3 deletions src/boxes/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class moovBox extends ContainerBox {
timescale: number;
mvhd: mvhdBox;
mvhds: Array<mvhdBox>;
mvex: mvexBox;
mvex: mvexBox | undefined;
mvexs: Array<mvexBox>;
iods: iodsBox;
iodss: Array<iodsBox>;
Expand Down Expand Up @@ -243,7 +243,7 @@ export class mvexBox extends ContainerBox {
type = 'mvex' as const;
box_name = 'MovieExtendsBox';
trex: trexBox;
mehd: mehdBox;
mehd: mehdBox | undefined;
mehds: Array<mehdBox>;

trexs: Array<trexBox> = [];
Expand Down Expand Up @@ -348,7 +348,7 @@ export class ipcoBox extends ContainerBox {
export class grplBox extends ContainerBox {
type = 'grpl' as const;
box_name = 'GroupsListBox';
declare boxes: Array<EntityToGroup>;
boxes: Array<EntityToGroup> = [];
}
export class j2kHBox extends ContainerBox {
type = 'j2kH' as const;
Expand Down
4 changes: 2 additions & 2 deletions src/boxes/dref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class drefBox extends FullBox {
}

/** @bundle writing/dref.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.version = 0;
this.flags = 0;
this.size = 4; //
Expand All @@ -38,5 +38,5 @@ export class drefBox extends FullBox {
/* adjusting the size, now that all sub-boxes are known */
Log.debug('BoxWriter', 'Adjusting box ' + this.type + ' with new size ' + this.size);
stream.adjustUint32(this.sizePosition, this.size);
}
};
}
4 changes: 2 additions & 2 deletions src/boxes/elng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export class elngBox extends FullBox {
}

/** @bundle writing/elng.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.version = 0;
this.flags = 0;
this.size = this.extended_language.length;
this.writeHeader(stream);
stream.writeString(this.extended_language);
}
};
}
4 changes: 2 additions & 2 deletions src/boxes/elst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class elstBox extends FullBox {
}

/** @bundle writing/elst.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.version = 0;
this.flags = 0;
this.size = 4 + 12 * this.entries.length;
Expand All @@ -37,5 +37,5 @@ export class elstBox extends FullBox {
stream.writeInt16(entry.media_rate_integer);
stream.writeInt16(entry.media_rate_fraction);
}
}
};
}
4 changes: 2 additions & 2 deletions src/boxes/emsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class emsgBox extends FullBox {
}

/** @bundle writing/emsg.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.version = 0;
this.flags = 0;
this.size =
Expand All @@ -55,5 +55,5 @@ export class emsgBox extends FullBox {
stream.writeUint32(this.event_duration);
stream.writeUint32(this.id);
stream.writeUint8Array(this.message_data);
}
};
}
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
4 changes: 2 additions & 2 deletions src/boxes/ftyp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export class ftypBox extends Box {
}

/** @bundle writing/ftyp.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.size = 8 + 4 * this.compatible_brands.length;
this.writeHeader(stream);
stream.writeString(this.major_brand, null, 4);
stream.writeUint32(this.minor_version);
for (let i = 0; i < this.compatible_brands.length; i++) {
stream.writeString(this.compatible_brands[i], null, 4);
}
}
};
}
4 changes: 2 additions & 2 deletions src/boxes/hdlr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class hdlrBox extends FullBox {
}

/** @bundle writing/hldr.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.size = 5 * 4 + this.name.length + 1;
this.version = 0;
this.flags = 0;
Expand All @@ -34,5 +34,5 @@ export class hdlrBox extends FullBox {
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeCString(this.name);
}
};
}
4 changes: 2 additions & 2 deletions src/boxes/kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export class kindBox extends FullBox {
}

/** @bundle writing/kind.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.version = 0;
this.flags = 0;
this.size = this.schemeURI.length + 1 + (this.value.length + 1);
this.writeHeader(stream);
stream.writeCString(this.schemeURI);
stream.writeCString(this.value);
}
};
}
4 changes: 2 additions & 2 deletions src/boxes/mdhd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class mdhdBox extends FullBox {
}

/** @bundle writing/mdhd.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.size = 4 * 4 + 2 * 2;
this.flags = 0;
this.version = 0;
Expand All @@ -39,5 +39,5 @@ export class mdhdBox extends FullBox {
stream.writeUint32(this.duration);
stream.writeUint16(this.language);
stream.writeUint16(0);
}
};
}
4 changes: 2 additions & 2 deletions src/boxes/mehd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export class mehdBox extends FullBox {
}

/** @bundle writing/mehd.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.version = 0;
this.flags = 0;
this.size = 4;
this.writeHeader(stream);
stream.writeUint32(this.fragment_duration);
}
};
}
4 changes: 2 additions & 2 deletions src/boxes/mfhd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export class mfhdBox extends FullBox {
}

/** @bundle writing/mfhd.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.version = 0;
this.flags = 0;
this.size = 4;
this.writeHeader(stream);
stream.writeUint32(this.sequence_number);
}
};
}
4 changes: 2 additions & 2 deletions src/boxes/mvhd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class mvhdBox extends FullBox {
}

/** @bundle writing/mvhd.js */
write(stream: MultiBufferStream) {
write = (stream: MultiBufferStream) => {
this.version = 0;
this.flags = 0;
this.size = 23 * 4 + 2 * 2;
Expand All @@ -60,7 +60,7 @@ export class mvhdBox extends FullBox {
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeUint32(this.next_track_id);
}
};

/** @bundle box-print.js */
print(output: Output) {
Expand Down
Loading