Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Nov 18, 2023
1 parent 71c1381 commit 2678af3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
21 changes: 11 additions & 10 deletions packages/rtp/src/container/mp4/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ type EncodedChunk = EncodedAudioChunk | EncodedVideoChunk;

export type DataType = "init" | "delta" | "key";

export interface Mp4Data {
type: DataType;
timestamp: number;
duration: number;
data: Uint8Array;
kind: "audio" | "video";
}

export class Mp4Container {
#mp4: MP4.ISOFile;
#audioFrame?: EncodedAudioChunk | EncodedVideoChunk;
Expand All @@ -15,16 +23,7 @@ export class Mp4Container {
videoTrack?: number;
#audioSegment = 0;
#videoSegment = 0;
onData = new Event<
[
{
type: DataType;
timestamp: number;
duration: number;
data: Uint8Array;
}
]
>();
onData = new Event<[Mp4Data]>();

constructor(
private props: {
Expand Down Expand Up @@ -128,6 +127,7 @@ export class Mp4Container {
timestamp: 0,
duration: 0,
data,
kind: frame.track,
} as const;
this.onData.execute(res);
}
Expand Down Expand Up @@ -237,6 +237,7 @@ export class Mp4Container {
timestamp: frame.timestamp,
duration: frame.duration ?? 0,
data,
kind: frame.track,
};
this.onData.execute(res);
}
Expand Down
14 changes: 9 additions & 5 deletions packages/rtp/src/container/mp4/exp-golomb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@
// Exponential-Golomb buffer decoder
class ExpGolomb {
TAG = "ExpGolomb";
_buffer = this.uint8array;
_buffer: Uint8Array;
_buffer_index = 0;
_total_bytes = this.uint8array.byteLength;
_total_bits = this.uint8array.byteLength * 8;
_total_bytes: number;
_total_bits: number;
_current_word = 0;
_current_word_bits_left = 0;

constructor(private uint8array) {}
constructor(private uint8array: Uint8Array) {
this._buffer = uint8array;
this._total_bytes = uint8array.byteLength;
this._total_bits = uint8array.byteLength * 8;
}

destroy() {
this._buffer = null;
this._buffer = null as any;
}

_fillCurrentWord() {
Expand Down
14 changes: 8 additions & 6 deletions packages/rtp/src/processor/mp4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Mp4Output {
duration: number;
data: Uint8Array;
eol?: boolean;
kind: "audio" | "video";
}

export interface MP4Option {
Expand All @@ -37,12 +38,7 @@ export interface MP4Option {

export class MP4Base implements AVProcessor<Mp4Input> {
private internalStats = {};
private container = new Mp4Container({
track: {
audio: !!this.tracks.find((t) => t.kind === "audio"),
video: !!this.tracks.find((t) => t.kind === "video"),
},
});
private container: Mp4Container;
stopped = false;
onStopped = new Event();

Expand All @@ -51,6 +47,12 @@ export class MP4Base implements AVProcessor<Mp4Input> {
private output: (output: Mp4Output) => void,
private options: MP4Option = {}
) {
this.container = new Mp4Container({
track: {
audio: !!this.tracks.find((t) => t.kind === "audio"),
video: !!this.tracks.find((t) => t.kind === "video"),
},
});
this.container.onData.subscribe((data) => {
this.output(data);
});
Expand Down
9 changes: 6 additions & 3 deletions packages/rtp/src/processor/mute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export class MuteHandlerBase implements Processor<MuteInput, MuteOutput> {
private lastCommittedTime = 0;
private lastExecutionTime = 0;
/**ms */
private interval = this.props.interval;
private bufferDuration: number = this.interval / 2;
private bufferLength = this.props.bufferLength * 2;
private interval: number;
private bufferDuration: number;
private bufferLength: number;
/**ms */
private lastFrameReceivedAt = 0;

Expand All @@ -38,6 +38,9 @@ export class MuteHandlerBase implements Processor<MuteInput, MuteOutput> {
bufferLength: number;
}
) {
this.interval = props.interval;
this.bufferDuration = this.interval / 2;
this.bufferLength = this.props.bufferLength * 2;
this.buffer = [...new Array(this.bufferLength)].map(() => []);
}

Expand Down

0 comments on commit 2678af3

Please sign in to comment.