-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
287 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { LocalUser, useIsConnected, useRTCClient } from "agora-rtc-react"; | ||
import { useAppStore } from "./stores"; | ||
import { User } from "./User"; | ||
|
||
export function CurrentUser() { | ||
const localTracks = useAppStore(state => state.localTracks); | ||
const hostRoom = useAppStore(state => state.hostRoom); | ||
const isConnected = useIsConnected(); | ||
const client = useRTCClient(); | ||
|
||
if ( | ||
!isConnected || | ||
!localTracks || | ||
!client.uid || | ||
!hostRoom || | ||
hostRoom.client.channelName !== client.channelName | ||
) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<User uid={client.uid}> | ||
<LocalUser cameraOn micOn audioTrack={localTracks[0]} videoTrack={localTracks[1]} /> | ||
</User> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,39 +1,5 @@ | ||
.container { | ||
display: flex; | ||
flex-wrap: nowrap; | ||
} | ||
|
||
.user { | ||
position: relative; | ||
width: 54px; | ||
height: 40px; | ||
overflow: hidden; | ||
border-radius: 5px; | ||
} | ||
|
||
.mask { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
background: linear-gradient( | ||
rgba(0, 0, 0, 0.1) 60%, | ||
rgba(0, 0, 0, 0.3) 80%, | ||
rgba(0, 0, 0, 0.8) 100% | ||
); | ||
} | ||
|
||
.label { | ||
position: absolute; | ||
z-index: 1; | ||
left: 2px; | ||
bottom: -2px; | ||
width: 100%; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
white-space: nowrap; | ||
word-break: keep-all; | ||
font-size: 12px; | ||
color: #fff; | ||
gap: 10px; | ||
} |
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,34 @@ | ||
.user { | ||
position: relative; | ||
width: 54px; | ||
height: 40px; | ||
overflow: hidden; | ||
border-radius: 5px; | ||
} | ||
|
||
.mask { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
background: linear-gradient( | ||
rgba(0, 0, 0, 0.1) 60%, | ||
rgba(0, 0, 0, 0.3) 80%, | ||
rgba(0, 0, 0, 0.8) 100% | ||
); | ||
} | ||
|
||
.label { | ||
position: absolute; | ||
z-index: 1; | ||
left: 2px; | ||
bottom: -2px; | ||
width: 100%; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
white-space: nowrap; | ||
word-break: keep-all; | ||
font-size: 12px; | ||
color: #fff; | ||
} |
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,23 @@ | ||
import styles from "./User.module.css"; | ||
|
||
import type { UID } from "agora-rtc-sdk-ng"; | ||
import type { PropsWithChildren } from "react"; | ||
|
||
import { useMemo } from "react"; | ||
import { fakeName } from "./utils"; | ||
|
||
export interface UserProps extends PropsWithChildren { | ||
uid: UID; | ||
} | ||
|
||
export function User({ uid, children }: UserProps) { | ||
const name = useMemo(() => fakeName(uid), [uid]); | ||
|
||
return ( | ||
<div className={styles.user}> | ||
{children} | ||
<div className={styles.mask}></div> | ||
<label className={styles.label}>{name}</label> | ||
</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
81 changes: 81 additions & 0 deletions
81
packages/agora-rtc-react/src/components/LocalMicrophoneAndCameraUser.stories.tsx
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,81 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import type { LocalMicrophoneAndCameraUserProps } from "./LocalMicrophoneAndCameraUser"; | ||
|
||
import { action } from "@storybook/addon-actions"; | ||
import { FakeRTCClient, FakeCameraVideoTrack, FakeMicrophoneAudioTrack } from "fake-agora-rtc"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { AgoraRTCProvider } from "../hooks"; | ||
import { LocalMicrophoneAndCameraUser } from "./LocalMicrophoneAndCameraUser"; | ||
|
||
const meta: Meta<LocalMicrophoneAndCameraUserProps> = { | ||
title: "User/LocalMicrophoneAndCameraUser", | ||
component: LocalMicrophoneAndCameraUser, | ||
tags: ["autodocs"], | ||
parameters: { | ||
backgrounds: { default: "light" }, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
export interface OverviewProps { | ||
micOn: boolean; | ||
cameraOn: boolean; | ||
} | ||
|
||
type OverviewArgs = OverviewProps & Omit<LocalMicrophoneAndCameraUserProps, keyof OverviewProps>; | ||
|
||
export const Overview: StoryObj<OverviewArgs> = { | ||
args: { | ||
micOn: false, | ||
cameraOn: false, | ||
playVideo: false, | ||
playAudio: false, | ||
cover: "http://placekitten.com/200/200", | ||
style: { | ||
width: 288, | ||
height: 216, | ||
}, | ||
}, | ||
render: function RenderLocalUser({ micOn, cameraOn, ...args }: OverviewArgs) { | ||
const [client] = useState(() => | ||
FakeRTCClient.create({ | ||
publish: async () => { | ||
action("IAgoraRTCClient.publish()")(); | ||
}, | ||
}), | ||
); | ||
|
||
const audioTrack = useMemo(() => { | ||
return micOn ? FakeMicrophoneAudioTrack.create() : null; | ||
}, [micOn]); | ||
|
||
const videoTrack = useMemo(() => { | ||
return cameraOn ? FakeCameraVideoTrack.create() : null; | ||
}, [cameraOn]); | ||
|
||
useEffect(() => { | ||
if (client && audioTrack) { | ||
client.publish(audioTrack); | ||
} | ||
}, [client, audioTrack]); | ||
|
||
useEffect(() => { | ||
if (client && videoTrack) { | ||
client.publish(videoTrack); | ||
} | ||
}, [client, videoTrack]); | ||
|
||
return ( | ||
<AgoraRTCProvider client={client}> | ||
<LocalMicrophoneAndCameraUser | ||
audioTrack={audioTrack} | ||
videoTrack={videoTrack} | ||
micOn={micOn} | ||
cameraOn={cameraOn} | ||
{...args} | ||
/> | ||
</AgoraRTCProvider> | ||
); | ||
}, | ||
}; |
96 changes: 96 additions & 0 deletions
96
packages/agora-rtc-react/src/components/LocalMicrophoneAndCameraUser.tsx
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 type { ICameraVideoTrack, IMicrophoneAudioTrack } from "agora-rtc-sdk-ng"; | ||
import type { HTMLProps, ReactNode } from "react"; | ||
import type { MaybePromiseOrNull } from "../utils"; | ||
|
||
import { CameraVideoTrack } from "./CameraVideoTrack"; | ||
import { MicrophoneAudioTrack } from "./MicrophoneAudioTrack"; | ||
import { UserCover } from "./UserCover"; | ||
import { FloatBoxStyle, useMergedStyle, VideoTrackWrapperStyle } from "./styles"; | ||
|
||
export interface LocalMicrophoneAndCameraUserProps extends HTMLProps<HTMLDivElement> { | ||
/** | ||
* Whether to turn on the local user's microphone. Default false. | ||
*/ | ||
readonly micOn?: boolean; | ||
/** | ||
* Whether to turn on the local user's camera. Default false. | ||
*/ | ||
readonly cameraOn?: boolean; | ||
/** | ||
* A microphone audio track which can be created by `createMicrophoneAudioTrack()`. | ||
*/ | ||
readonly audioTrack?: MaybePromiseOrNull<IMicrophoneAudioTrack>; | ||
/** | ||
* A camera video track which can be created by `createCameraVideoTrack()`. | ||
*/ | ||
readonly videoTrack?: MaybePromiseOrNull<ICameraVideoTrack>; | ||
/** | ||
* Whether to play the local user's audio track. Default follows `micOn`. | ||
*/ | ||
readonly playAudio?: boolean; | ||
/** | ||
* Whether to play the local user's video track. Default follows `cameraOn`. | ||
*/ | ||
readonly playVideo?: boolean; | ||
/** | ||
* Device ID, which can be retrieved by calling `getDevices()`. | ||
*/ | ||
readonly micDeviceId?: string; | ||
/** | ||
* Device ID, which can be retrieved by calling `getDevices()`. | ||
*/ | ||
readonly cameraDeviceId?: string; | ||
/** | ||
* The volume. The value ranges from 0 (mute) to 1000 (maximum). A value of 100 is the current volume. | ||
*/ | ||
readonly volume?: number; | ||
/** | ||
* Render cover image if playVideo is off. | ||
*/ | ||
readonly cover?: string; | ||
/** | ||
* Children is rendered on top of the video canvas. | ||
*/ | ||
readonly children?: ReactNode; | ||
} | ||
|
||
/** | ||
* Play/Stop local user camera and microphone track. | ||
*/ | ||
export function LocalMicrophoneAndCameraUser({ | ||
micOn, | ||
cameraOn, | ||
audioTrack, | ||
videoTrack, | ||
playAudio, | ||
playVideo, | ||
micDeviceId, | ||
cameraDeviceId, | ||
volume, | ||
cover, | ||
children, | ||
style, | ||
...props | ||
}: LocalMicrophoneAndCameraUserProps) { | ||
const mergedStyle = useMergedStyle(VideoTrackWrapperStyle, style); | ||
playVideo = playVideo ?? !!cameraOn; | ||
playAudio = playAudio ?? !!micOn; | ||
return ( | ||
<div {...props} style={mergedStyle}> | ||
<CameraVideoTrack | ||
track={videoTrack} | ||
play={playVideo} | ||
deviceId={cameraDeviceId} | ||
disabled={!cameraOn} | ||
/> | ||
<MicrophoneAudioTrack | ||
track={audioTrack} | ||
play={playAudio} | ||
deviceId={micDeviceId} | ||
disabled={!micOn} | ||
/> | ||
{cover && !cameraOn && <UserCover cover={cover} />} | ||
<div style={FloatBoxStyle}>{children}</div> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.