-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathinput.ts
40 lines (37 loc) · 1.54 KB
/
input.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { Input as CoreInput } from '@swmansion/smelter-core';
import type { WorkerMessage } from '../workerApi';
import { assert, downloadToArrayBuffer } from '../utils';
import { handleRegisterCameraInput } from './input/camera';
import { handleRegisterScreenCaptureInput } from './input/screenCapture';
import { handleRegisterStreamInput } from './input/stream';
import { handleRegisterMp4Input } from './input/mp4';
import type { InstanceContext } from './instance';
export interface Input {
terminate(): Promise<void>;
}
export type RegisterInputResult = {
input: Input;
workerMessage: [WorkerMessage, Transferable[]];
};
export async function handleRegisterInputRequest(
ctx: InstanceContext,
inputId: string,
body: CoreInput.RegisterInputRequest
): Promise<RegisterInputResult> {
if (body.type === 'mp4') {
assert(body.url, 'mp4 URL is required');
const arrayBuffer = await downloadToArrayBuffer(body.url);
return await handleRegisterMp4Input(ctx, inputId, arrayBuffer);
} else if (body.type === 'mp4_blob') {
const arrayBuffer = await (body.blob as Blob).arrayBuffer();
return await handleRegisterMp4Input(ctx, inputId, arrayBuffer);
} else if (body.type === 'camera') {
return await handleRegisterCameraInput(ctx, inputId);
} else if (body.type === 'screen_capture') {
return await handleRegisterScreenCaptureInput(ctx, inputId);
} else if (body.type === 'stream') {
return await handleRegisterStreamInput(ctx, inputId, body.stream);
} else {
throw new Error(`Unknown input type ${body.type}`);
}
}