Live video on the AT Protocol
1import { useState } from "react";
2import { useLivestreamStore } from "../livestream-store";
3import { usePlayerStore } from "../player-store";
4import { useCreateStreamRecord, useEndLivestream } from "../streamplace-store";
5
6export function useLivestreamInfo(url?: string) {
7 const ingest = usePlayerStore((x) => x.ingestConnectionState);
8 const profile = useLivestreamStore((x) => x.profile);
9 const endLivestream = useEndLivestream();
10 const setLocalLivestreamURI = useLivestreamStore(
11 (x) => x.setLocalLivestreamURI,
12 );
13 const createStreamRecord = useCreateStreamRecord();
14
15 const [title, setTitle] = useState<string>("");
16 const [showCountdown, setShowCountdown] = useState(false);
17 const [recordSubmitted, setRecordSubmitted] = useState(false);
18
19 const handleSubmit = async () => {
20 try {
21 if (title !== "") {
22 setRecordSubmitted(true);
23 // Create the livestream record with title and custom url if available
24 const { uri } = await createStreamRecord({
25 title,
26 canonicalUrl: url || undefined,
27 });
28 setLocalLivestreamURI(uri);
29 }
30 } catch (error) {
31 console.error("Error creating livestream:", error);
32 throw new Error("Failed to create livestream record");
33 } finally {
34 setRecordSubmitted(false);
35 }
36 };
37
38 const toggleGoLive = (
39 keyboardHeight?: number,
40 closeKeyboard?: () => void,
41 ) => {
42 // Optionally close keyboard if provided
43 if (closeKeyboard) closeKeyboard();
44 setShowCountdown(true);
45 // wait ~3 seconds before announcing
46 setTimeout(() => {
47 handleSubmit();
48 }, 3000);
49 };
50
51 // Stop the current broadcast
52 const toggleStopStream = () => {
53 console.log("Stopping stream...");
54 endLivestream();
55 };
56
57 return {
58 ingest,
59 profile,
60 title,
61 setTitle,
62 showCountdown,
63 setShowCountdown,
64 recordSubmitted,
65 setRecordSubmitted,
66 handleSubmit,
67 toggleGoLive,
68 toggleStopStream,
69 };
70}