-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add host page and update WHIP settings (#12)
- Loading branch information
Showing
12 changed files
with
452 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { Button } from "@/components/ui"; | ||
import { useLocalParticipant } from "@livekit/components-react"; | ||
import { createLocalTracks, Track, type LocalTrack } from "livekit-client"; | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
|
||
interface Props { | ||
slug: string; | ||
} | ||
|
||
export default function HostStudio({ slug }: Props) { | ||
const [videoTrack, setVideoTrack] = useState<LocalTrack>(); | ||
const [audioTrack, setAudioTrack] = useState<LocalTrack>(); | ||
const [isPublishing, setIsPublishing] = useState(false); | ||
const [isUnpublishing, setIsUnpublishing] = useState(false); | ||
const previewVideoEl = useRef<HTMLVideoElement>(null); | ||
|
||
const { localParticipant } = useLocalParticipant(); | ||
|
||
const createTracks = async () => { | ||
const tracks = await createLocalTracks({ audio: true, video: true }); | ||
tracks.forEach((track) => { | ||
switch (track.kind) { | ||
case Track.Kind.Video: { | ||
if (previewVideoEl?.current) { | ||
track.attach(previewVideoEl.current); | ||
} | ||
setVideoTrack(track); | ||
break; | ||
} | ||
case Track.Kind.Audio: { | ||
setAudioTrack(track); | ||
break; | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
void createTracks(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
return () => { | ||
videoTrack?.stop(); | ||
audioTrack?.stop(); | ||
}; | ||
}, [videoTrack, audioTrack]); | ||
|
||
const togglePublishing = useCallback(async () => { | ||
if (isPublishing && localParticipant) { | ||
setIsUnpublishing(true); | ||
|
||
if (videoTrack) { | ||
void localParticipant.unpublishTrack(videoTrack); | ||
} | ||
if (audioTrack) { | ||
void localParticipant.unpublishTrack(audioTrack); | ||
} | ||
|
||
await createTracks(); | ||
|
||
setTimeout(() => { | ||
setIsUnpublishing(false); | ||
}, 2000); | ||
} else if (localParticipant) { | ||
if (videoTrack) { | ||
void localParticipant.publishTrack(videoTrack); | ||
} | ||
if (audioTrack) { | ||
void localParticipant.publishTrack(audioTrack); | ||
} | ||
} | ||
|
||
setIsPublishing((prev) => !prev); | ||
}, [audioTrack, isPublishing, localParticipant, videoTrack]); | ||
|
||
return ( | ||
<div className="flex w-full flex-col gap-4"> | ||
<div className="flex items-center justify-between"> | ||
<div className="flex gap-[5px] text-lg font-bold"> | ||
{isPublishing && !isUnpublishing ? ( | ||
<div className="flex items-center gap-1"> | ||
<span className="relative mr-1 flex h-3 w-3"> | ||
<span className="absolute inline-flex h-full w-full animate-ping rounded-full bg-red-400 opacity-75"></span> | ||
<span className="relative inline-flex h-3 w-3 rounded-full bg-red-500"></span> | ||
</span> | ||
LIVE | ||
</div> | ||
) : ( | ||
"Ready to stream" | ||
)}{" "} | ||
as{" "} | ||
<div className="italic text-purple-500 dark:text-purple-300"> | ||
{slug} | ||
</div> | ||
</div> | ||
<div className="flex gap-2"> | ||
{isPublishing ? ( | ||
<Button | ||
size="sm" | ||
className=" bg-red-500 text-white hover:bg-red-700 dark:bg-red-500 dark:text-white dark:hover:bg-red-600" | ||
onClick={() => void togglePublishing()} | ||
disabled={isUnpublishing} | ||
> | ||
{isUnpublishing ? "Stopping..." : "Stop stream"} | ||
</Button> | ||
) : ( | ||
<Button | ||
size="sm" | ||
className=" bg-blue-500 text-white hover:bg-blue-700 dark:bg-blue-500 dark:text-white dark:hover:bg-blue-600" | ||
onClick={() => void togglePublishing()} | ||
> | ||
Start stream | ||
</Button> | ||
)} | ||
</div> | ||
</div> | ||
<div className="aspect-video rounded-sm border border-neutral-500 bg-neutral-800"> | ||
<video ref={previewVideoEl} width="100%" height="100%" /> | ||
</div> | ||
<div> | ||
<div className="flex items-center gap-2"> | ||
<span className="inline-flex items-center rounded-md bg-purple-200 px-2 py-1 text-xs font-medium uppercase text-purple-600 ring-1 ring-inset ring-purple-600"> | ||
Note | ||
</span> | ||
<p className="text-sm text-neutral-500 dark:text-neutral-400"> | ||
Do not stream to RTMP/WHIP endpoint at the same time. | ||
</p> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import Chat from "@/components/channel/Chat"; | ||
import HostStudio from "@/components/channel/HostStudio"; | ||
import { env } from "@/env.mjs"; | ||
import { api } from "@/lib/api"; | ||
import { LiveKitRoom as RoomProvider } from "@livekit/components-react"; | ||
import jwt, { type JwtPayload } from "jwt-decode"; | ||
import type { GetServerSideProps, InferGetServerSidePropsType } from "next"; | ||
import { useEffect, useState } from "react"; | ||
|
||
interface Props { | ||
slug: string; | ||
} | ||
|
||
export const getServerSideProps: GetServerSideProps<Props> = async ({ | ||
params, | ||
}) => { | ||
return Promise.resolve({ | ||
props: { | ||
slug: params?.slug as string, | ||
}, | ||
}); | ||
}; | ||
|
||
export default function ChannelHostPage({ | ||
slug, | ||
}: InferGetServerSidePropsType<typeof getServerSideProps>) { | ||
const SESSION_STREAMER_TOKEN_KEY = `${slug}-streamer-token`; | ||
|
||
const [streamerToken, setStreamerToken] = useState(""); | ||
const [queryEnabled, setQueryEnabled] = useState(false); | ||
|
||
api.token.getWrite.useQuery( | ||
{ | ||
roomName: slug, | ||
identity: slug, | ||
}, | ||
{ | ||
onSuccess: (data) => { | ||
setStreamerToken(data?.token); | ||
sessionStorage.setItem(SESSION_STREAMER_TOKEN_KEY, data?.token); | ||
}, | ||
enabled: queryEnabled, | ||
refetchOnMount: false, | ||
refetchOnWindowFocus: false, | ||
refetchOnReconnect: false, | ||
retry: false, | ||
} | ||
); | ||
|
||
// NOTE: This is a hack to persist the streamer token in the session storage | ||
// so that the client doesn't have to create a streamer token every time they | ||
// navigate back to the page. | ||
useEffect(() => { | ||
const sessionToken = sessionStorage.getItem(SESSION_STREAMER_TOKEN_KEY); | ||
|
||
if (sessionToken) { | ||
const payload: JwtPayload = jwt(sessionToken); | ||
|
||
if (payload.exp) { | ||
const expiry = new Date(payload.exp * 1000); | ||
if (expiry < new Date()) { | ||
sessionStorage.removeItem(SESSION_STREAMER_TOKEN_KEY); | ||
setQueryEnabled(true); | ||
return; | ||
} | ||
} | ||
|
||
setStreamerToken(sessionToken); | ||
} else { | ||
setQueryEnabled(true); | ||
} | ||
}, [SESSION_STREAMER_TOKEN_KEY]); | ||
|
||
if (streamerToken === "") { | ||
return null; | ||
} | ||
|
||
return ( | ||
<RoomProvider | ||
token={streamerToken} | ||
serverUrl={env.NEXT_PUBLIC_LIVEKIT_WS_URL} | ||
className="flex flex-1 flex-col" | ||
> | ||
<div className="flex h-full flex-1"> | ||
<div className="flex-1 flex-col p-12 dark:border-t-zinc-200 dark:bg-black"> | ||
<HostStudio slug={slug} /> | ||
</div> | ||
<div className="sticky hidden w-80 border-l dark:border-zinc-800 dark:bg-zinc-900 md:block"> | ||
<div className="absolute top-0 bottom-0 right-0 flex h-full w-full flex-col gap-2 p-2"> | ||
<Chat viewerName={slug} /> | ||
</div> | ||
</div> | ||
</div> | ||
</RoomProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
95e054f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
livestream – ./
livestream-livekit.vercel.app
livestream-demo.livekit.io
livestream-git-main-livekit.vercel.app
livestream-one.vercel.app