diff --git a/package-lock.json b/package-lock.json index b77511d..514c9ca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,12 +10,10 @@ "license": "ISC", "dependencies": { "debug": "^4.3.4", - "events": "^3.3.0", "supports-color": "^9.4.0" }, "devDependencies": { "@types/debug": "^4.1.8", - "@types/events": "^3.0.0", "@types/jest": "^29.5.3", "@typescript-eslint/eslint-plugin": "^6.4.0", "@typescript-eslint/parser": "^6.4.0", @@ -25,7 +23,6 @@ "open-cli": "^7.2.0", "ts-jest": "^29.1.1", "typedoc": "^0.24.8", - "typedoc-plugin-no-inherit": "^1.4.0", "typescript": "~5.1.6" }, "engines": { @@ -1462,12 +1459,6 @@ "@types/ms": "*" } }, - "node_modules/@types/events": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@types/events/-/events-3.0.0.tgz", - "integrity": "sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==", - "dev": true - }, "node_modules/@types/graceful-fs": { "version": "4.1.6", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.6.tgz", @@ -3390,14 +3381,6 @@ "node": ">=0.10.0" } }, - "node_modules/events": { - "version": "3.3.0", - "resolved": "https://registry.npmjs.org/events/-/events-3.3.0.tgz", - "integrity": "sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==", - "engines": { - "node": ">=0.8.x" - } - }, "node_modules/execa": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz", @@ -7248,15 +7231,6 @@ "typescript": "4.6.x || 4.7.x || 4.8.x || 4.9.x || 5.0.x || 5.1.x" } }, - "node_modules/typedoc-plugin-no-inherit": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/typedoc-plugin-no-inherit/-/typedoc-plugin-no-inherit-1.4.0.tgz", - "integrity": "sha512-cAvqQ8X9xh1xztVoDKtF4nYRSBx9XwttN3OBbNNpA0YaJSRM8XvpVVhugq8FoO1HdWjF3aizS0JzdUOMDt0y9g==", - "dev": true, - "peerDependencies": { - "typedoc": ">=0.23.0" - } - }, "node_modules/typedoc/node_modules/brace-expansion": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", @@ -8641,12 +8615,6 @@ "@types/ms": "*" } }, - "@types/events": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@types/events/-/events-3.0.0.tgz", - "integrity": "sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==", - "dev": true - }, "@types/graceful-fs": { "version": "4.1.6", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.6.tgz", @@ -9940,11 +9908,6 @@ "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==", "dev": true }, - "events": { - "version": "3.3.0", - "resolved": "https://registry.npmjs.org/events/-/events-3.3.0.tgz", - "integrity": "sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==" - }, "execa": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz", @@ -12721,13 +12684,6 @@ } } }, - "typedoc-plugin-no-inherit": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/typedoc-plugin-no-inherit/-/typedoc-plugin-no-inherit-1.4.0.tgz", - "integrity": "sha512-cAvqQ8X9xh1xztVoDKtF4nYRSBx9XwttN3OBbNNpA0YaJSRM8XvpVVhugq8FoO1HdWjF3aizS0JzdUOMDt0y9g==", - "dev": true, - "requires": {} - }, "typescript": { "version": "5.1.6", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", diff --git a/package.json b/package.json index a7e955a..9e514cf 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,6 @@ ] }, "coveragePathIgnorePatterns": [ - "src/EnhancedEventEmitter.ts", "src/Logger.ts", "src/utils.ts", "src/tests" @@ -65,12 +64,10 @@ }, "dependencies": { "debug": "^4.3.4", - "events": "^3.3.0", "supports-color": "^9.4.0" }, "devDependencies": { "@types/debug": "^4.1.8", - "@types/events": "^3.0.0", "@types/jest": "^29.5.3", "@typescript-eslint/eslint-plugin": "^6.4.0", "@typescript-eslint/parser": "^6.4.0", @@ -80,7 +77,6 @@ "open-cli": "^7.2.0", "ts-jest": "^29.1.1", "typedoc": "^0.24.8", - "typedoc-plugin-no-inherit": "^1.4.0", "typescript": "~5.1.6" } } diff --git a/src/EnhancedEventEmitter.ts b/src/EnhancedEventEmitter.ts deleted file mode 100644 index 19b4944..0000000 --- a/src/EnhancedEventEmitter.ts +++ /dev/null @@ -1,221 +0,0 @@ -import { EventEmitter } from 'events'; - -type Events = Record; - -/** - * @hidden - * @noInheritDoc - * - * TODO: Problem is that static members/methods of EventEmitter class are shown - * in all RTP and RTCP classes despite parent class Packet uses @noInheritDoc. - * Issue: https://github.com/jonchardy/typedoc-plugin-no-inherit/issues/33 - * So we override all those members with no inline doc to make TypeDoc not - * show them. - */ -export class EnhancedEventEmitter extends EventEmitter -{ - // NOTE: See TODO above. - static on(...args: any[]) - { - // @ts-ignore - return EventEmitter.on(...args); - } - - // NOTE: See TODO above. - static once(...args: any[]) - { - // @ts-ignore - return EventEmitter.once(...args); - } - - // NOTE: See TODO above. - static listenerCount(...args: any[]) - { - // @ts-ignore - return EventEmitter.listenerCount(...args); - } - - // NOTE: See TODO above. - static getMaxListeners(...args: any[]) - { - // @ts-ignore - return EventEmitter.getMaxListeners(...args); - } - - // NOTE: See TODO above. - static setMaxListeners(...args: any[]) - { - // @ts-ignore - return EventEmitter.setMaxListeners(...args); - } - - // NOTE: See TODO above. - static getEventListeners(...args: any[]) - { - // @ts-ignore - return EventEmitter.getEventListeners(...args); - } - - // NOTE: See TODO above. - static get captureRejectionSymbol(): typeof EventEmitter.captureRejectionSymbol - { - return EventEmitter.captureRejectionSymbol; - } - - // NOTE: See TODO above. - static get captureRejections(): typeof EventEmitter.captureRejections - { - return EventEmitter.captureRejections; - } - - // NOTE: See TODO above. - static get defaultMaxListeners(): typeof EventEmitter.defaultMaxListeners - { - return EventEmitter.defaultMaxListeners; - } - - // NOTE: See TODO above. - static get errorMonitor(): typeof EventEmitter.errorMonitor - { - return EventEmitter.errorMonitor; - } - - constructor() - { - super(); - this.setMaxListeners(Infinity); - } - - emit(eventName: K, ...args: E[K]): boolean - { - return super.emit(eventName, ...args); - } - - /** - * Special addition to the EventEmitter API. - * - * @hidden - */ - safeEmit(eventName: K, ...args: E[K]): boolean - { - const numListeners = super.listenerCount(eventName); - - try - { - return super.emit(eventName, ...args); - } - catch (error) - { - return Boolean(numListeners); - } - } - - on( - eventName: K, - listener: (...args: E[K]) => void - ): this - { - super.on(eventName, listener as (...args: any[]) => void); - - return this; - } - - off( - eventName: K, - listener: (...args: E[K]) => void - ): this - { - super.off(eventName, listener as (...args: any[]) => void); - - return this; - } - - addListener( - eventName: K, - listener: (...args: E[K]) => void - ): this - { - super.on(eventName, listener as (...args: any[]) => void); - - return this; - } - - prependListener( - eventName: K, - listener: (...args: E[K]) => void - ): this - { - super.prependListener(eventName, listener as (...args: any[]) => void); - - return this; - } - - once( - eventName: K, - listener: (...args: E[K]) => void - ): this - { - super.once(eventName, listener as (...args: any[]) => void); - - return this; - } - - prependOnceListener( - eventName: K, - listener: (...args: E[K]) => void - ): this - { - super.prependOnceListener(eventName, listener as (...args: any[]) => void); - - return this; - } - - removeListener( - eventName: K, - listener: (...args: E[K]) => void - ): this - { - super.off(eventName, listener as (...args: any[]) => void); - - return this; - } - - removeAllListeners(eventName?: K): this - { - super.removeAllListeners(eventName); - - return this; - } - - listenerCount(eventName: K): number - { - return super.listenerCount(eventName); - } - - listeners(eventName: K): Function[] - { - return super.listeners(eventName); - } - - rawListeners(eventName: K): Function[] - { - return super.rawListeners(eventName); - } - - getMaxListeners(): number - { - return super.getMaxListeners(); - } - - setMaxListeners(n: number): this - { - super.setMaxListeners(n); - - return this; - } - - eventNames(): (string | symbol)[] - { - return super.eventNames(); - } -} diff --git a/src/Packet.ts b/src/Packet.ts index 03985c4..b5f1d79 100644 --- a/src/Packet.ts +++ b/src/Packet.ts @@ -21,9 +21,6 @@ export type PacketDump = SerializableDump & /** * Parent class of all RTP and RTCP packets. - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export abstract class Packet extends Serializable { diff --git a/src/RTCP/ByePacket.ts b/src/RTCP/ByePacket.ts index 2329a9f..188882c 100644 --- a/src/RTCP/ByePacket.ts +++ b/src/RTCP/ByePacket.ts @@ -38,9 +38,6 @@ export type ByePacketDump = RtcpPacketDump & * * @see * - [RFC 3550 section 6.6](https://datatracker.ietf.org/doc/html/rfc3550#section-6.6) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class ByePacket extends RtcpPacket { @@ -158,9 +155,9 @@ export class ByePacket extends RtcpPacket /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); // Position relative to the DataView byte offset. let pos = 0; @@ -219,9 +216,19 @@ export class ByePacket extends RtcpPacket /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): ByePacket + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): ByePacket { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new ByePacket(view); } diff --git a/src/RTCP/CompoundPacket.ts b/src/RTCP/CompoundPacket.ts index d3417cc..f259297 100644 --- a/src/RTCP/CompoundPacket.ts +++ b/src/RTCP/CompoundPacket.ts @@ -35,9 +35,6 @@ export type CompoundPacketDump = PacketDump & * * @see * - [RFC 3550](https://datatracker.ietf.org/doc/html/rfc3550) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class CompoundPacket extends Packet { @@ -249,27 +246,25 @@ export class CompoundPacket extends Packet /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const { buffer, byteOffset, byteLength } = this.getSerializationBuffer(); + const bufferData = this.getSerializationBuffer(buffer, byteOffset); // Create new DataView with new buffer. - const view = new DataView(buffer, byteOffset, byteLength); + const view = new DataView( + bufferData.buffer, + bufferData.byteOffset, + bufferData.byteLength + ); // Position relative to the DataView byte offset. let pos = 0; for (const packet of this.#packets) { - // Serialize the RTCP packet into the current position. - packet.prependOnceListener('will-serialize', (length, cb) => - { - cb(view.buffer, view.byteOffset + pos); + packet.serialize(view.buffer, view.byteOffset + pos); - pos += length; - }); - - packet.serialize(); + pos += packet.getByteLength(); } // Assert that current position is equal than new buffer length. @@ -289,9 +284,19 @@ export class CompoundPacket extends Packet /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): CompoundPacket + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): CompoundPacket { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new CompoundPacket(view); } diff --git a/src/RTCP/ExtendedReports/ExtendedReport.ts b/src/RTCP/ExtendedReports/ExtendedReport.ts index f662f16..13ec7ba 100644 --- a/src/RTCP/ExtendedReports/ExtendedReport.ts +++ b/src/RTCP/ExtendedReports/ExtendedReport.ts @@ -160,9 +160,6 @@ export function reportTypeToString(reportType: ExtendedReportType): string * * @see * - [RFC 3611 section 3](https://datatracker.ietf.org/doc/html/rfc3611#section-3) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export abstract class ExtendedReport extends Serializable { @@ -230,12 +227,16 @@ export abstract class ExtendedReport extends Serializable /** * Serialize base RTCP packet into a new buffer. */ - protected serializeBase(): DataView + protected serializeBase(buffer?: ArrayBuffer, byteOffset?: number): DataView { - const { buffer, byteOffset, byteLength } = this.getSerializationBuffer(); + const bufferData = this.getSerializationBuffer(buffer, byteOffset); // Create new DataView with new buffer. - const view = new DataView(buffer, byteOffset, byteLength); + const view = new DataView( + bufferData.buffer, + bufferData.byteOffset, + bufferData.byteLength + ); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -253,7 +254,7 @@ export abstract class ExtendedReport extends Serializable ); // Update the report length field in the report header. - setExtendedReportLength(view, byteLength); + setExtendedReportLength(view, view.byteLength); return view; } diff --git a/src/RTCP/ExtendedReports/ExtendedReportDLRR.ts b/src/RTCP/ExtendedReports/ExtendedReportDLRR.ts index c8ceb3d..ede2218 100644 --- a/src/RTCP/ExtendedReports/ExtendedReportDLRR.ts +++ b/src/RTCP/ExtendedReports/ExtendedReportDLRR.ts @@ -55,9 +55,6 @@ export type DLRRSubReport = * * @see * - [RFC 3611 section 4.5](https://datatracker.ietf.org/doc/html/rfc3611#section-4.5) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class ExtendedReportDLRR extends ExtendedReport { @@ -145,9 +142,9 @@ export class ExtendedReportDLRR extends ExtendedReport /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); // Position relative to the DataView byte offset. let pos = 0; @@ -187,9 +184,19 @@ export class ExtendedReportDLRR extends ExtendedReport /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): ExtendedReportDLRR + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): ExtendedReportDLRR { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new ExtendedReportDLRR(view); } diff --git a/src/RTCP/ExtendedReports/ExtendedReportDRLE.ts b/src/RTCP/ExtendedReports/ExtendedReportDRLE.ts index 34d9906..c689fc7 100644 --- a/src/RTCP/ExtendedReports/ExtendedReportDRLE.ts +++ b/src/RTCP/ExtendedReports/ExtendedReportDRLE.ts @@ -45,9 +45,6 @@ export type ExtendedReportDRLEDump = ExtendedReportDump & * * @see * - [RFC 3611 section 4.2](https://datatracker.ietf.org/doc/html/rfc3611#section-4.2) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class ExtendedReportDRLE extends ExtendedReport { @@ -143,9 +140,9 @@ export class ExtendedReportDRLE extends ExtendedReport /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -198,9 +195,19 @@ export class ExtendedReportDRLE extends ExtendedReport /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): ExtendedReportDRLE + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): ExtendedReportDRLE { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new ExtendedReportDRLE(view); } diff --git a/src/RTCP/ExtendedReports/ExtendedReportLRLE.ts b/src/RTCP/ExtendedReports/ExtendedReportLRLE.ts index f8f6a9d..46d13fe 100644 --- a/src/RTCP/ExtendedReports/ExtendedReportLRLE.ts +++ b/src/RTCP/ExtendedReports/ExtendedReportLRLE.ts @@ -45,9 +45,6 @@ export type ExtendedReportLRLEDump = ExtendedReportDump & * * @see * - [RFC 3611 section 4.1](https://datatracker.ietf.org/doc/html/rfc3611#section-4.1) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class ExtendedReportLRLE extends ExtendedReport { @@ -141,9 +138,9 @@ export class ExtendedReportLRLE extends ExtendedReport /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -196,9 +193,19 @@ export class ExtendedReportLRLE extends ExtendedReport /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): ExtendedReportLRLE + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): ExtendedReportLRLE { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new ExtendedReportLRLE(view); } diff --git a/src/RTCP/ExtendedReports/ExtendedReportPRT.ts b/src/RTCP/ExtendedReports/ExtendedReportPRT.ts index 426ecf8..dcdeaef 100644 --- a/src/RTCP/ExtendedReports/ExtendedReportPRT.ts +++ b/src/RTCP/ExtendedReports/ExtendedReportPRT.ts @@ -46,9 +46,6 @@ export type ExtendedReportPRTDump = ExtendedReportDump & * * @see * - [RFC 3611 section 4.3](https://datatracker.ietf.org/doc/html/rfc3611#section-4.3) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class ExtendedReportPRT extends ExtendedReport { @@ -135,9 +132,9 @@ export class ExtendedReportPRT extends ExtendedReport /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -187,9 +184,19 @@ export class ExtendedReportPRT extends ExtendedReport /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): ExtendedReportPRT + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): ExtendedReportPRT { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new ExtendedReportPRT(view); } diff --git a/src/RTCP/ExtendedReports/ExtendedReportRRT.ts b/src/RTCP/ExtendedReports/ExtendedReportRRT.ts index 7c7fdf0..d9571f2 100644 --- a/src/RTCP/ExtendedReports/ExtendedReportRRT.ts +++ b/src/RTCP/ExtendedReports/ExtendedReportRRT.ts @@ -34,9 +34,6 @@ export type ExtendedReportRRTDump = ExtendedReportDump & * * @see * - [RFC 3611 section 4.4](https://datatracker.ietf.org/doc/html/rfc3611#section-4.4) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class ExtendedReportRRT extends ExtendedReport { @@ -89,9 +86,9 @@ export class ExtendedReportRRT extends ExtendedReport /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -133,9 +130,19 @@ export class ExtendedReportRRT extends ExtendedReport /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): ExtendedReportRRT + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): ExtendedReportRRT { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new ExtendedReportRRT(view); } diff --git a/src/RTCP/ExtendedReports/ExtendedReportSS.ts b/src/RTCP/ExtendedReports/ExtendedReportSS.ts index aed1dfb..437ea10 100644 --- a/src/RTCP/ExtendedReports/ExtendedReportSS.ts +++ b/src/RTCP/ExtendedReports/ExtendedReportSS.ts @@ -65,9 +65,6 @@ export type ExtendedReportSSDump = ExtendedReportDump & * * @see * - [RFC 3611 section 4.6](https://datatracker.ietf.org/doc/html/rfc3611#section-4.6) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class ExtendedReportSS extends ExtendedReport { @@ -132,9 +129,9 @@ export class ExtendedReportSS extends ExtendedReport /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -176,9 +173,19 @@ export class ExtendedReportSS extends ExtendedReport /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): ExtendedReportSS - { - const view = this.cloneInternal(buffer, byteOffset); + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): ExtendedReportSS + { + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new ExtendedReportSS(view); } diff --git a/src/RTCP/ExtendedReports/ExtendedReportVM.ts b/src/RTCP/ExtendedReports/ExtendedReportVM.ts index e20955b..37adc24 100644 --- a/src/RTCP/ExtendedReports/ExtendedReportVM.ts +++ b/src/RTCP/ExtendedReports/ExtendedReportVM.ts @@ -67,9 +67,6 @@ export type ExtendedReportVMDump = ExtendedReportDump & * * @see * - [RFC 3611 section 4.7](https://datatracker.ietf.org/doc/html/rfc3611#section-4.7) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class ExtendedReportVM extends ExtendedReport { @@ -143,9 +140,9 @@ export class ExtendedReportVM extends ExtendedReport /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -187,9 +184,19 @@ export class ExtendedReportVM extends ExtendedReport /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): ExtendedReportVM - { - const view = this.cloneInternal(buffer, byteOffset); + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): ExtendedReportVM + { + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new ExtendedReportVM(view); } diff --git a/src/RTCP/ExtendedReports/GenericExtendedReport.ts b/src/RTCP/ExtendedReports/GenericExtendedReport.ts index e3363b3..de7d2ff 100644 --- a/src/RTCP/ExtendedReports/GenericExtendedReport.ts +++ b/src/RTCP/ExtendedReports/GenericExtendedReport.ts @@ -28,9 +28,6 @@ export type GenericExtendedReportDump = ExtendedReportDump; * * @see * - [RFC 3611 section 3](https://datatracker.ietf.org/doc/html/rfc3611#section-3) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class GenericExtendedReport extends ExtendedReport { @@ -125,9 +122,9 @@ export class GenericExtendedReport extends ExtendedReport /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -178,9 +175,19 @@ export class GenericExtendedReport extends ExtendedReport /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): GenericExtendedReport + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): GenericExtendedReport { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new GenericExtendedReport(view); } diff --git a/src/RTCP/FeedbackPacket.ts b/src/RTCP/FeedbackPacket.ts index 2cc3308..d630774 100644 --- a/src/RTCP/FeedbackPacket.ts +++ b/src/RTCP/FeedbackPacket.ts @@ -129,9 +129,6 @@ function messageTypeToString( * * @see * - [RFC 4585 section 6.1](https://datatracker.ietf.org/doc/html/rfc4585#section-6.1) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export abstract class FeedbackPacket extends RtcpPacket { @@ -225,9 +222,9 @@ export abstract class FeedbackPacket extends RtcpPacket /** * Serialize base RTCP Feedback packet into a new buffer. */ - protected serializeBase(): DataView + protected serializeBase(buffer?: ArrayBuffer, byteOffset?: number): DataView { - const view = super.serializeBase(); + const view = super.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, diff --git a/src/RTCP/GenericFeedbackPacket.ts b/src/RTCP/GenericFeedbackPacket.ts index 0bf70f4..0e2eb97 100644 --- a/src/RTCP/GenericFeedbackPacket.ts +++ b/src/RTCP/GenericFeedbackPacket.ts @@ -33,9 +33,6 @@ export type GenericFeedbackPacketDump = FeedbackPacketDump & * * @see * - [RFC 4585 section 6.1](https://datatracker.ietf.org/doc/html/rfc4585#section-6.1) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class GenericFeedbackPacket extends FeedbackPacket { @@ -144,9 +141,9 @@ export class GenericFeedbackPacket extends FeedbackPacket /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -200,9 +197,19 @@ export class GenericFeedbackPacket extends FeedbackPacket /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): GenericFeedbackPacket + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): GenericFeedbackPacket { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new GenericFeedbackPacket(view); } diff --git a/src/RTCP/GenericPacket.ts b/src/RTCP/GenericPacket.ts index 2fc2aed..f11541d 100644 --- a/src/RTCP/GenericPacket.ts +++ b/src/RTCP/GenericPacket.ts @@ -31,9 +31,6 @@ export type GenericPacketDump = RtcpPacketDump & * * @see * - [RFC 3550](https://datatracker.ietf.org/doc/html/rfc3550) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class GenericPacket extends RtcpPacket { @@ -132,9 +129,9 @@ export class GenericPacket extends RtcpPacket /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -188,9 +185,19 @@ export class GenericPacket extends RtcpPacket /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): GenericPacket + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): GenericPacket { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new GenericPacket(view); } diff --git a/src/RTCP/NackPacket.ts b/src/RTCP/NackPacket.ts index 33ee943..eff78b8 100644 --- a/src/RTCP/NackPacket.ts +++ b/src/RTCP/NackPacket.ts @@ -35,9 +35,6 @@ export type NackPacketDump = FeedbackPacketDump & * * @see * - [RFC 4585 section 6.2.1](https://datatracker.ietf.org/doc/html/rfc4585#section-6.2.1) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class NackPacket extends FeedbackPacket { @@ -126,9 +123,9 @@ export class NackPacket extends FeedbackPacket /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); // Position relative to the DataView byte offset. let pos = 0; @@ -167,9 +164,19 @@ export class NackPacket extends FeedbackPacket /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): NackPacket + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): NackPacket { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new NackPacket(view); } diff --git a/src/RTCP/PliPacket.ts b/src/RTCP/PliPacket.ts index b38ca09..cc59be9 100644 --- a/src/RTCP/PliPacket.ts +++ b/src/RTCP/PliPacket.ts @@ -28,9 +28,6 @@ export type PliPacketDump = FeedbackPacketDump; * * @see * - [RFC 4585 section 6.3.1](https://datatracker.ietf.org/doc/html/rfc4585#section-6.3.1) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class PliPacket extends FeedbackPacket { @@ -100,9 +97,9 @@ export class PliPacket extends FeedbackPacket /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); // Nothing else to do. @@ -115,9 +112,19 @@ export class PliPacket extends FeedbackPacket /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): PliPacket + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): PliPacket { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new PliPacket(view); } diff --git a/src/RTCP/ReceiverReportPacket.ts b/src/RTCP/ReceiverReportPacket.ts index d95ec0b..21567f6 100644 --- a/src/RTCP/ReceiverReportPacket.ts +++ b/src/RTCP/ReceiverReportPacket.ts @@ -67,9 +67,6 @@ export type ReceptionReportDump = SerializableDump & * * @see * - [RFC 3550 section 6.4.2](https://datatracker.ietf.org/doc/html/rfc3550#section-6.4.2) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class ReceiverReportPacket extends RtcpPacket { @@ -177,9 +174,9 @@ export class ReceiverReportPacket extends RtcpPacket /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -208,21 +205,9 @@ export class ReceiverReportPacket extends RtcpPacket // Write Reception Reports. for (const report of this.#reports) { - // NOTE: ReceptionReport class has fixed length so we don't need to deal - // with calls to serialize() on it. - - const reportView = report.getView(); - - uint8Array.set( - new Uint8Array( - reportView.buffer, - reportView.byteOffset, - RECEPTION_REPORT_LENGTH - ), - pos - ); + report.serialize(view.buffer, view.byteOffset + pos); - pos += RECEPTION_REPORT_LENGTH; + pos += report.getByteLength(); } pos += this.padding; @@ -244,9 +229,19 @@ export class ReceiverReportPacket extends RtcpPacket /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): ReceiverReportPacket + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): ReceiverReportPacket { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new ReceiverReportPacket(view); } @@ -362,9 +357,31 @@ export class ReceptionReport extends Serializable /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - // Nothing to do. + const bufferData = this.getSerializationBuffer(buffer, byteOffset); + + // Create new DataView with new buffer. + const view = new DataView( + bufferData.buffer, + bufferData.byteOffset, + bufferData.byteLength + ); + const uint8Array = new Uint8Array( + view.buffer, + view.byteOffset, + view.byteLength + ); + + // Copy the entire report into the new buffer. + uint8Array.set( + new Uint8Array( + this.view.buffer, + this.view.byteOffset, + RECEPTION_REPORT_LENGTH + ), + 0 + ); this.setSerializationNeeded(false); } @@ -372,9 +389,19 @@ export class ReceptionReport extends Serializable /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): ReceptionReport + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): ReceptionReport { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new ReceptionReport(view); } diff --git a/src/RTCP/RtcpPacket.ts b/src/RTCP/RtcpPacket.ts index f235163..0af0d0e 100644 --- a/src/RTCP/RtcpPacket.ts +++ b/src/RTCP/RtcpPacket.ts @@ -197,9 +197,6 @@ export function packetTypeToString(packetType: RtcpPacketType): string * * @see * - [RFC 3550 section 6.1](https://datatracker.ietf.org/doc/html/rfc3550#section-6.1) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export abstract class RtcpPacket extends Packet { @@ -306,12 +303,16 @@ export abstract class RtcpPacket extends Packet /** * Serialize base RTCP packet into a new buffer. */ - protected serializeBase(): DataView + protected serializeBase(buffer?: ArrayBuffer, byteOffset?: number): DataView { - const { buffer, byteOffset, byteLength } = this.getSerializationBuffer(); + const bufferData = this.getSerializationBuffer(buffer, byteOffset); // Create new DataView with new buffer. - const view = new DataView(buffer, byteOffset, byteLength); + const view = new DataView( + bufferData.buffer, + bufferData.byteOffset, + bufferData.byteLength + ); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -329,7 +330,7 @@ export abstract class RtcpPacket extends Packet ); // Update the length field in the RTCP header. - setRtcpLength(view, byteLength); + setRtcpLength(view, view.byteLength); // Write padding. if (this.padding > 0) @@ -341,7 +342,7 @@ export abstract class RtcpPacket extends Packet ); } - view.setUint8(byteLength - 1, this.padding); + view.setUint8(view.byteLength - 1, this.padding); } return view; diff --git a/src/RTCP/SdesPacket.ts b/src/RTCP/SdesPacket.ts index 566fbf3..ef16bf6 100644 --- a/src/RTCP/SdesPacket.ts +++ b/src/RTCP/SdesPacket.ts @@ -97,9 +97,6 @@ export type SdesChunkDump = SerializableDump & * * @see * - [RFC 3550 section 6.5](https://datatracker.ietf.org/doc/html/rfc3550#section-6.5) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class SdesPacket extends RtcpPacket { @@ -258,9 +255,9 @@ export class SdesPacket extends RtcpPacket /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); // Position relative to the DataView byte offset. let pos = 0; @@ -271,15 +268,9 @@ export class SdesPacket extends RtcpPacket // Write chunks. for (const chunk of this.#chunks) { - // Serialize the chunk into the current position. - chunk.prependOnceListener('will-serialize', (length, cb) => - { - cb(view.buffer, view.byteOffset + pos); + chunk.serialize(view.buffer, view.byteOffset + pos); - pos += length; - }); - - chunk.serialize(); + pos += chunk.getByteLength(); } pos += this.padding; @@ -301,9 +292,19 @@ export class SdesPacket extends RtcpPacket /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): SdesPacket + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): SdesPacket { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new SdesPacket(view); } @@ -351,9 +352,6 @@ export class SdesPacket extends RtcpPacket /** * SDES Chunk. - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class SdesChunk extends Serializable { @@ -487,10 +485,16 @@ export class SdesChunk extends Serializable /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const { buffer, byteOffset, byteLength } = this.getSerializationBuffer(); - const view = new DataView(buffer, byteOffset, byteLength); + const bufferData = this.getSerializationBuffer(buffer, byteOffset); + + // Create new DataView with new buffer. + const view = new DataView( + bufferData.buffer, + bufferData.byteOffset, + bufferData.byteLength + ); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -531,9 +535,19 @@ export class SdesChunk extends Serializable /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): SdesChunk + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): SdesChunk { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new SdesChunk(view); } diff --git a/src/RTCP/SenderReportPacket.ts b/src/RTCP/SenderReportPacket.ts index fb765b8..74588f3 100644 --- a/src/RTCP/SenderReportPacket.ts +++ b/src/RTCP/SenderReportPacket.ts @@ -70,9 +70,6 @@ export type SenderReportPacketDump = RtcpPacketDump & * * @see * - [RFC 3550 section 6.4.1](https://datatracker.ietf.org/doc/html/rfc3550#section-6.4.1) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class SenderReportPacket extends RtcpPacket { @@ -185,9 +182,9 @@ export class SenderReportPacket extends RtcpPacket /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -216,21 +213,9 @@ export class SenderReportPacket extends RtcpPacket // Write Reception Reports. for (const report of this.#reports) { - // NOTE: ReceptionReport class has fixed length so we don't need to deal - // with calls to serialize() on it. - - const reportView = report.getView(); - - uint8Array.set( - new Uint8Array( - reportView.buffer, - reportView.byteOffset, - RECEPTION_REPORT_LENGTH - ), - pos - ); + report.serialize(view.buffer, view.byteOffset + pos); - pos += RECEPTION_REPORT_LENGTH; + pos += report.getByteLength(); } pos += this.padding; @@ -252,11 +237,21 @@ export class SenderReportPacket extends RtcpPacket /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): SenderReportPacket + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): SenderReportPacket { - const destPacketView = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); - return new SenderReportPacket(destPacketView); + return new SenderReportPacket(view); } /** diff --git a/src/RTCP/XrPacket.ts b/src/RTCP/XrPacket.ts index a4331c4..b0a744b 100644 --- a/src/RTCP/XrPacket.ts +++ b/src/RTCP/XrPacket.ts @@ -50,9 +50,6 @@ export type XrPacketDump = RtcpPacketDump & * * @see * - [RFC 3611 section 2](https://datatracker.ietf.org/doc/html/rfc3611#section-2) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class XrPacket extends RtcpPacket { @@ -225,9 +222,9 @@ export class XrPacket extends RtcpPacket /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const view = super.serializeBase(); + const view = this.serializeBase(buffer, byteOffset); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -257,14 +254,9 @@ export class XrPacket extends RtcpPacket for (const report of this.#reports) { // Serialize the report into the current position. - report.prependOnceListener('will-serialize', (length, cb) => - { - cb(view.buffer, view.byteOffset + pos); + report.serialize(view.buffer, view.byteOffset + pos); - pos += length; - }); - - report.serialize(); + pos += report.getByteLength(); } pos += this.padding; @@ -286,9 +278,19 @@ export class XrPacket extends RtcpPacket /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): XrPacket + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): XrPacket { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new XrPacket(view); } diff --git a/src/RtpPacket.ts b/src/RtpPacket.ts index f2a1eb7..04c35da 100644 --- a/src/RtpPacket.ts +++ b/src/RtpPacket.ts @@ -62,9 +62,6 @@ export function isRtp(view: DataView): boolean * @see * - [RFC 3550 section 5.1](https://datatracker.ietf.org/doc/html/rfc3550#section-5.1) * - [RFC 5285 section 4](https://datatracker.ietf.org/doc/html/rfc5285#section-4) - * - * @emits - * - will-serialize: {@link WillSerializeEvent} */ export class RtpPacket extends Packet { @@ -406,12 +403,16 @@ export class RtpPacket extends Packet /** * @inheritDoc */ - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const { buffer, byteOffset, byteLength } = this.getSerializationBuffer(); + const bufferData = this.getSerializationBuffer(buffer, byteOffset); // Create new DataView with new buffer. - const view = new DataView(buffer, byteOffset, byteLength); + const view = new DataView( + bufferData.buffer, + bufferData.byteOffset, + bufferData.byteLength + ); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -646,9 +647,19 @@ export class RtpPacket extends Packet /** * @inheritDoc */ - clone(buffer?: ArrayBuffer, byteOffset?: number): RtpPacket + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): RtpPacket { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new RtpPacket(view); } diff --git a/src/Serializable.ts b/src/Serializable.ts index 3a8889d..2ba6cc9 100644 --- a/src/Serializable.ts +++ b/src/Serializable.ts @@ -1,4 +1,3 @@ -import { EnhancedEventEmitter } from './EnhancedEventEmitter'; import { clone } from './utils'; /** @@ -9,51 +8,10 @@ export type SerializableDump = byteLength: number; }; -/** - * Event emitted when the content is being serialized. The user has a chance to - * pass a buffer, otherwise a new one will be internally allocated. - * - * @param `length: number`: Required buffer length (in bytes). - * @param `callback: (buffer, byteOffset?) => void`: A function that can be - * optionally called to pass a buffer and a byte offset where the content will - * be serialized. - * - * @remarks - * - If `callback` is called, the buffer byte length minus the given byte offset - * must be equal or higher than `length`. - * - * @example - * ```ts - * packet.on('will-serialize', (length, callback) => { - * const buffer = new ArrayBuffer(length); - * const byteOffset = 0; - * - * callback(buffer, byteOffset); - * }); - * ``` - */ -export type WillSerializeEvent = -[ - number, - (buffer: ArrayBuffer, byteOffset?: number) => void -]; - -/** - * Events emitted by all classes inheriting from Serializable class. - */ -type SerializableEvents = -{ - 'will-serialize': WillSerializeEvent; -}; - /** * Class holding a serializable buffer view. - * - * @emits - * - will-serialize: {@link WillSerializeEvent} - * */ -export abstract class Serializable extends EnhancedEventEmitter +export abstract class Serializable { // Buffer view holding the content. // @ts-ignore ('view' has not initializer and is not assigned in constructor). @@ -63,8 +21,6 @@ export abstract class Serializable extends EnhancedEventEmitter - { - buffer = userBuffer; + byteOffset ??= 0; - if (userByteOffset !== undefined) - { - byteOffset = userByteOffset; - } - }); + const byteLength = this.getByteLength(); - // The user called the callback and passed a buffer and optional byteOffset. if (buffer) { if (buffer.byteLength - byteOffset < byteLength) { throw new RangeError( - `given buffer available space (${buffer.byteLength - byteOffset} bytes) is less than content length (${byteLength} bytes)` + `given buffer available space (${buffer.byteLength - byteOffset} bytes) is less than length required for serialization (${byteLength} bytes)` ); } @@ -199,17 +175,23 @@ export abstract class Serializable extends EnhancedEventEmitter const cloningBuffer = new ArrayBuffer(1000); const cloningByteOffset = 123; - chunk.on('will-serialize', (length, cb) => - { - cb(serializationBuffer, serializationByteOffset); - }); - // Clone the chunk instead of just serializing it since clone() will // serialize it anyway. - const clonedChunk = chunk.clone(cloningBuffer, cloningByteOffset); + const clonedChunk = chunk.clone( + cloningBuffer, + cloningByteOffset, + serializationBuffer, + serializationByteOffset + ); expect(chunk.needsSerialization()).toBe(false); expect(chunk.getView().buffer === serializationBuffer).toBe(true); diff --git a/src/tests/RtpPacket.test.ts b/src/tests/RtpPacket.test.ts index 60e9f2f..08f98b7 100644 --- a/src/tests/RtpPacket.test.ts +++ b/src/tests/RtpPacket.test.ts @@ -624,12 +624,7 @@ describe('serialize packet into a given buffer', () => const buffer = new ArrayBuffer(2000); const byteOffset = 135; - packet.on('will-serialize', (length, cb) => - { - cb(buffer, byteOffset); - }); - - packet.serialize(); + packet.serialize(buffer, byteOffset); // Packet and payload views must be the same. expect(areDataViewsEqual(packet.getView(), packetView)).toBe(true); @@ -637,8 +632,6 @@ describe('serialize packet into a given buffer', () => expect(packet.getView().buffer === buffer).toBe(true); expect(packet.getView().byteOffset).toBe(byteOffset); expect(packet.dump()).toEqual(packetDump); - - packet.removeAllListeners('will-serialize'); }); test('serialization fails if given buffer do not have enough space', () => @@ -647,16 +640,9 @@ describe('serialize packet into a given buffer', () => const buffer = new ArrayBuffer(16); const byteOffset = 1; - packet.on('will-serialize', (length, cb) => - { - cb(buffer, byteOffset); - }); - expect( - () => packet.serialize() + () => packet.serialize(buffer, byteOffset) ).toThrow(RangeError); - - packet.removeAllListeners('will-serialize'); }); }); diff --git a/src/tests/Serializable.test.ts b/src/tests/Serializable.test.ts index af47799..512a576 100644 --- a/src/tests/Serializable.test.ts +++ b/src/tests/Serializable.test.ts @@ -13,12 +13,16 @@ export class Foo extends Serializable return this.view.byteLength; } - serialize(): void + serialize(buffer?: ArrayBuffer, byteOffset?: number): void { - const { buffer, byteOffset, byteLength } = this.getSerializationBuffer(); + const bufferData = this.getSerializationBuffer(buffer, byteOffset); // Create new DataView with new buffer. - const view = new DataView(buffer, byteOffset, byteLength); + const view = new DataView( + bufferData.buffer, + bufferData.byteOffset, + bufferData.byteLength + ); const uint8Array = new Uint8Array( view.buffer, view.byteOffset, @@ -41,9 +45,19 @@ export class Foo extends Serializable this.setSerializationNeeded(false); } - clone(buffer?: ArrayBuffer, byteOffset?: number): Foo + clone( + buffer?: ArrayBuffer, + byteOffset?: number, + serializationBuffer?: ArrayBuffer, + serializationByteOffset?: number + ): Foo { - const view = this.cloneInternal(buffer, byteOffset); + const view = this.cloneInternal( + buffer, + byteOffset, + serializationBuffer, + serializationByteOffset + ); return new Foo(view); } @@ -94,12 +108,7 @@ describe('parse Foo 1', () => const buffer = new ArrayBuffer(10); const byteOffset = 0; - foo.once('will-serialize', (length, cb) => - { - cb(buffer, byteOffset); - }); - - foo.serialize(); + foo.serialize(buffer, byteOffset); expect(foo.getByteLength()).toBe(10); expect(foo.needsSerialization()).toBe(false); @@ -109,15 +118,7 @@ describe('parse Foo 1', () => test('serialize() fails if current buffer is given and it collides', () => { - foo.once('will-serialize', (length, cb) => - { - // Given buffer is the object's current one with byte offset 9, which - // is the last byte of the object's DataView, so it will be wronly - // overwritten. - cb(view.buffer, /* byteOffset */ 9); - }); - - foo.serialize(); + foo.serialize(view.buffer, /* byteOffset */ 9); expect(foo.getByteLength()).toBe(10); expect(foo.needsSerialization()).toBe(false); @@ -153,14 +154,14 @@ describe('parse Foo 1', () => // @ts-ignore foo.setSerializationNeeded(true); - foo.once('will-serialize', (length, cb) => - { - cb(view.buffer, /* byteOffset */ 9); - }); - const buffer = new ArrayBuffer(10); const byteOffset = 0; - const clonedFoo = foo.clone(buffer, byteOffset); + const clonedFoo = foo.clone( + buffer, + byteOffset, + /* serializationBuffer */ view.buffer, + /* byteOffset */ 9 + ); expect(clonedFoo.getByteLength()).toBe(10); expect(clonedFoo.needsSerialization()).toBe(false);