Skip to content

Instantly share code, notes, and snippets.

@thegiantbeast
Created November 7, 2022 14:34
Show Gist options
  • Select an option

  • Save thegiantbeast/a15591ecbda8a4cee9b8bd1acef83cb0 to your computer and use it in GitHub Desktop.

Select an option

Save thegiantbeast/a15591ecbda8a4cee9b8bd1acef83cb0 to your computer and use it in GitHub Desktop.
export default async (
roomId: string,
{ appId, token },
{ width, height, frameRate }
) => {
const { default: AgoraRTC } = await import("agora-rtc-sdk-ng");
// required agora options
const options = {
appId,
token,
uid: "game-agent",
};
AgoraRTC.enableLogUpload();
const client = AgoraRTC.createClient({ mode: "rtc", codec: "vp8" });
const gameStream = await navigator.mediaDevices.getDisplayMedia({
preferCurrentTab: true,
audio: true,
video: {
width,
height,
aspectRatio: width / height,
frameRate,
},
});
const videoSource = AgoraRTC.createCustomVideoTrack({
mediaStreamTrack: gameStream.getVideoTracks()[0],
});
const audioSource = AgoraRTC.createCustomAudioTrack({
mediaStreamTrack: gameStream.getAudioTracks()[0],
encoderConfig: "high_quality_stereo",
});
await client.join(options.appId, roomId, options.token ?? null, options.uid);
await client.publish([videoSource, audioSource]);
};
import React, { useCallback, useEffect, useRef } from "react";
import AgoraRTC, {
ClientRole,
IAgoraRTCClient,
IAgoraRTCRemoteUser,
} from "agora-rtc-sdk-ng";
type MediaType = "audio" | "video";
interface AgoraProps {
roomId: string;
}
// required agora options
const options = {
appId: "<app-id>",
token: null,
uid: null,
role: "audience" as ClientRole,
// host or audience
audienceLatency: 2,
};
const Agora: React.VFC<AgoraProps> = ({
roomId
}) => {
const videoRef = useRef<HTMLVideoElement>(null);
const subscribe = useCallback(
async (
client: IAgoraRTCClient,
user: IAgoraRTCRemoteUser,
mediaType: MediaType,
) => {
await client.subscribe(user, mediaType);
if (mediaType === "video" && user.videoTrack && videoRef.current) {
const msVideoTrack = user.videoTrack.getMediaStreamTrack();
const ms = new MediaStream([msVideoTrack]);
if (user.hasAudio && user.audioTrack) {
const msAudioTrack = user.audioTrack.getMediaStreamTrack();
ms.addTrack(msAudioTrack);
}
videoRef.current.srcObject = ms;
videoRef.current.play();
}
},
[],
);
const initStream = useCallback(async () => {
AgoraRTC.enableLogUpload();
const client = AgoraRTC.createClient({
mode: "live",
codec: "vp8",
});
await client.join(options.appId, roomId, options.token, options.uid);
await client.setClientRole(options.role, {
level: options.audienceLatency,
});
client.on("user-published", (...args) => {
subscribe.apply(null, [client, ...args]);
});
client.on("user-unpublished", () => {
console.log('"user-unpublished" event triggered - stream stopped.');
});
}, [roomId, subscribe]);
useEffect(() => {
initStream();
}, [initStream]);
return (
<video
ref={videoRef}
style={{
pointerEvents: "none",
userSelect: "none",
objectFit: "fill",
transform: "scaleX(1.08)",
}}
controls={false}
playsInline
muted
autoPlay={true}
disablePictureInPicture
preload="auto"
/>
);
};
export default Agora;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment