char/live.umm.gay

git clone https://git.t4t.associates/char/live.umm.gay

videogame hackerStart video mutedbe8b658

main
1.7 KiB75 linesraw
1import Head from "next/head";
2import { useRouter } from "next/router"
3import React from "react"
4
5const WHEP_API_LOCATION = "/api/wish-server/whep"
6
7const Video = ({user}: {user: string}) => {
8  const videoRef = React.createRef<HTMLVideoElement>();
9  React.useEffect(() => {
10    const peerConnection = new RTCPeerConnection()
11
12    peerConnection.ontrack = function (event) {
13      videoRef.current!.srcObject = event.streams[0]
14    }
15  
16    peerConnection.addTransceiver('audio')
17    peerConnection.addTransceiver('video')
18
19
20    peerConnection.createOffer().then(async offer => {
21      peerConnection.setLocalDescription(offer)
22
23      const response = await fetch(WHEP_API_LOCATION, {
24        method: 'POST',
25        body: offer.sdp,
26        headers: {
27          Authorization: `Bearer ${user}`,
28          'Content-Type': 'application/sdp'
29        }
30      })
31
32      if (response.status >= 200 && response.status < 300) {
33        const sdp = await response.text();
34        peerConnection.setRemoteDescription({
35          sdp,
36          type: 'answer'
37        })
38      }
39    })
40
41    return function cleanup() {
42      peerConnection.close()
43    }
44  }, [videoRef, user])
45
46  return <video ref={videoRef} autoPlay muted controls playsInline />
47}
48
49export default () => {
50  const router = useRouter()
51  const { user } = router.query;
52  const Player = ({ user }: { user: any }) => {
53    if (typeof user !== "string") {
54      return <>
55        <strong>The requested user was not found.</strong>
56      </>
57    }
58
59    return <>
60      <Video user={user}></Video>
61    </>
62  }
63
64  return <>
65    <Head>
66      <title>{`${user} | live.umm.gay`}</title>
67    </Head>
68
69    <main>
70      <h1>watching: {user}</h1>
71      <Player user={user}/>
72      {/* TODO: Chat? */}
73    </main>
74  </>
75}