import { createBroadcast, createPlayer } from "@daydreamlive/browser";
async function startStreaming(whipUrl: string) {
// Get camera
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
// Show local preview
const previewVideo = document.querySelector("video#preview");
previewVideo.srcObject = stream;
// Start broadcasting
const broadcast = createBroadcast({ whipUrl, stream });
broadcast.on("stateChange", async (state) => {
if (state === "live") {
// Once broadcasting, start the player for AI output
const player = createPlayer(broadcast.whepUrl);
player.on("stateChange", (playerState) => {
console.log("Player:", playerState);
});
await player.connect();
player.attachTo(document.querySelector("video#output"));
}
});
await broadcast.connect();
return { broadcast, player };
}