-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealtime-video.ts
More file actions
33 lines (27 loc) · 1 KB
/
Copy pathrealtime-video.ts
File metadata and controls
33 lines (27 loc) · 1 KB
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
import { createRealtimeClient } from "@beatapi/realtime";
type SessionResponse = {
data: {
client_secret: string;
};
};
const response = await fetch("/api/realtime/session", { method: "POST" });
if (!response.ok) throw new Error("Unable to create a BeatAPI Realtime Session.");
const session = (await response.json()) as SessionResponse;
const input = await navigator.mediaDevices.getUserMedia({ video: true });
const output = document.querySelector<HTMLVideoElement>("#realtime-output");
if (!output) throw new Error("Missing #realtime-output video element.");
const client = createRealtimeClient({
clientSecret: session.data.client_secret,
});
const connection = await client.connect({
input,
output,
initial: { prompt: "Transform the scene while preserving motion." },
});
await connection.set({
prompt: "Keep the movement and change the scene to watercolor.",
});
window.addEventListener("pagehide", () => {
void connection.disconnect();
input.getTracks().forEach((track) => track.stop());
});