Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wkozyra95 committed Jan 9, 2025
1 parent f24bf03 commit 61c94ef
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 240 deletions.
3 changes: 2 additions & 1 deletion ts/@live-compositor/web-wasm/src/compositor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { loadWasmModule, Renderer } from '@live-compositor/browser-render';
import { LiveCompositor as CoreLiveCompositor } from '@live-compositor/core';
import WasmInstance from './manager/wasmInstance';
import WasmInstance from './wasmInstance';
import type { RegisterOutput } from './output/registerOutput';
import { intoRegisterOutput } from './output/registerOutput';
import type { RegisterInput } from './input/registerInput';
Expand Down Expand Up @@ -54,6 +54,7 @@ export default class LiveCompositor {
this.instance = new WasmInstance({
renderer: this.renderer!,
framerate: this.options.framerate ?? { num: 30, den: 1 },
logger: this.logger.child({ element: 'wasm-instance' }),
});
this.coreCompositor = new CoreLiveCompositor(this.instance, this.logger);

Expand Down
4 changes: 2 additions & 2 deletions ts/@live-compositor/web-wasm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import WasmInstance from './manager/wasmInstance';
import Pipeline from './pipeline/wasmInstance';
import LiveCompositor, { setWasmBundleUrl } from './compositor';

export { WasmInstance, LiveCompositor, setWasmBundleUrl };
export { Pipeline as WasmInstance, LiveCompositor, setWasmBundleUrl };
69 changes: 48 additions & 21 deletions ts/@live-compositor/web-wasm/src/input/input.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
import type { InputId } from '@live-compositor/browser-render';
import type { Frame, InputId } from '@live-compositor/browser-render';
import { CompositorEventType, type EventSender } from '../eventSender';
import type { FrameRef } from './frame';
import { assert } from '../utils';
import type InputFrameProducer from './inputFrameProducer';
import type { RegisterInputRequest } from '@live-compositor/core';
import DecodingFrameProducer from './producer/decodingFrameProducer';
import MP4Source from './mp4/source';
import type { PipelineCtx } from '../pipeline/wasmInstance';
import MediaStreamFrameProducer from './producer/mediaStreamFrameProducer';
import { initCameraMediaStream } from './producer/mediaStreamInit';

export type InputState = 'waiting_for_start' | 'buffering' | 'playing' | 'finished';

export class Input {
export interface Input {
start(): void;
getFrame(currentQueuePts: number): Promise<Frame | undefined>;
}

export async function createInputFromRequest(
inputId: string,
request: RegisterInputRequest,
pipelineCtx: PipelineCtx
): Promise<Input> {
if (request.type === 'mp4') {
const frameProducer = new DecodingFrameProducer(new MP4Source(request.url!));
await frameProducer.init();
return new QueuedInput(inputId, frameProducer, pipelineCtx);
} else if (request.type === 'camera') {
const frameProducer = new MediaStreamFrameProducer(initCameraMediaStream);
await frameProducer.init();
} else {
throw new Error(`Unknown input type ${(request as any).type}`);
}
}

export class QueuedInput implements Input {
private id: InputId;
private state: InputState;
private frameProducer: InputFrameProducer;
Expand All @@ -16,11 +43,11 @@ export class Input {
*/
private startPtsMs?: number;

public constructor(id: InputId, frameProducer: InputFrameProducer, eventSender: EventSender) {
public constructor(id: InputId, frameProducer: InputFrameProducer, ctx: PipelineCtx) {
this.id = id;
this.state = 'waiting_for_start';
this.frameProducer = frameProducer;
this.eventSender = eventSender;
this.eventSender = ctx.eventSender;

this.frameProducer.registerCallbacks({
onReady: () => {
Expand All @@ -47,10 +74,25 @@ export class Input {
});
}

/**
* Retrieves reference of a frame closest to the provided `currentQueuePts`.
*/
public async getFrame(currentQueuePts: number): Promise<Frame | undefined> {
if (this.state !== 'playing') {
return;
}
if (this.startPtsMs === undefined) {
this.startPtsMs = currentQueuePts;
}

const framePts = this.queuePtsToInputPts(currentQueuePts);
return await this.frameProducer.getFrameRef(framePts)?.getFrame();
}

/**
* Called on every queue tick. Produces frames for given `currentQueuePts` & handles EOS.
*/
public async onQueueTick(currentQueuePts: number): Promise<void> {
private async onQueueTick(currentQueuePts: number): Promise<void> {
let targetPts: number | undefined;
if (this.startPtsMs !== undefined) {
targetPts = this.queuePtsToInputPts(currentQueuePts);
Expand All @@ -70,21 +112,6 @@ export class Input {
}
}

/**
* Retrieves reference of a frame closest to the provided `currentQueuePts`.
*/
public getFrameRef(currentQueuePts: number): FrameRef | undefined {
if (this.state !== 'playing') {
return;
}
if (this.startPtsMs === undefined) {
this.startPtsMs = currentQueuePts;
}

const framePts = this.queuePtsToInputPts(currentQueuePts);
return this.frameProducer.getFrameRef(framePts);
}

private queuePtsToInputPts(queuePts: number): number {
assert(this.startPtsMs !== undefined);
return queuePts - this.startPtsMs;
Expand Down
15 changes: 0 additions & 15 deletions ts/@live-compositor/web-wasm/src/input/inputFrameProducer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type { RegisterInputRequest } from '@live-compositor/core';
import type { FrameRef } from './frame';
import DecodingFrameProducer from './producer/decodingFrameProducer';
import MediaStreamFrameProducer from './producer/mediaStreamFrameProducer';
import MP4Source from './mp4/source';
import { initCameraMediaStream } from './producer/mediaStreamInit';

export type InputFrameProducerCallbacks = {
onReady(): void;
Expand All @@ -28,13 +23,3 @@ export default interface InputFrameProducer {
isFinished(): boolean;
close(): void;
}

export function producerFromRequest(request: RegisterInputRequest): InputFrameProducer {
if (request.type === 'mp4') {
return new DecodingFrameProducer(new MP4Source(request.url!));
} else if (request.type === 'camera') {
return new MediaStreamFrameProducer(initCameraMediaStream);
} else {
throw new Error(`Unknown input type ${(request as any).type}`);
}
}
159 changes: 0 additions & 159 deletions ts/@live-compositor/web-wasm/src/manager/wasmInstance.ts

This file was deleted.

Loading

0 comments on commit 61c94ef

Please sign in to comment.