File size: 5,148 Bytes
87337b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
"use client"
import * as React from "react"
import { cn } from "@/lib/utils"
import { ICameraVideoTrack, ILocalVideoTrack, IMicrophoneAudioTrack } from "agora-rtc-sdk-ng"
import { useAppSelector, useAppDispatch, VOICE_OPTIONS, VideoSourceType, useIsCompactLayout } from "@/common"
import { ITextItem, EMessageType, IChatItem } from "@/types"
import { rtcManager, IUserTracks, IRtcUser } from "@/manager"
import {
setRoomConnected,
addChatItem,
setVoiceType,
setOptions,
} from "@/store/reducers/global"
import AgentVoicePresetSelect from "@/components/Agent/VoicePresetSelect"
import AgentView from "@/components/Agent/View"
import Avatar from "@/components/Agent/AvatarTrulience"
import MicrophoneBlock from "@/components/Agent/Microphone"
import VideoBlock from "@/components/Agent/Camera"
import dynamic from "next/dynamic"
import ChatCard from "@/components/Chat/ChatCard"
let hasInit: boolean = false
export default function RTCCard(props: { className?: string }) {
const { className } = props
const dispatch = useAppDispatch()
const options = useAppSelector((state) => state.global.options)
const trulienceSettings = useAppSelector((state) => state.global.trulienceSettings)
const { userId, channel } = options
const [videoTrack, setVideoTrack] = React.useState<ICameraVideoTrack>()
const [audioTrack, setAudioTrack] = React.useState<IMicrophoneAudioTrack>()
const [screenTrack, setScreenTrack] = React.useState<ILocalVideoTrack>()
const [remoteuser, setRemoteUser] = React.useState<IRtcUser>()
const [videoSourceType, setVideoSourceType] = React.useState<VideoSourceType>(VideoSourceType.CAMERA)
const useTrulienceAvatar = trulienceSettings.enabled
const avatarInLargeWindow = trulienceSettings.avatarDesktopLargeWindow;
const isCompactLayout = useIsCompactLayout();
const DynamicChatCard = dynamic(() => import("@/components/Chat/ChatCard"), {
ssr: false,
});
React.useEffect(() => {
if (!options.channel) {
return
}
if (hasInit) {
return
}
init()
return () => {
if (hasInit) {
destory()
}
}
}, [options.channel])
const init = async () => {
console.log("[rtc] init")
rtcManager.on("localTracksChanged", onLocalTracksChanged)
rtcManager.on("textChanged", onTextChanged)
rtcManager.on("remoteUserChanged", onRemoteUserChanged)
await rtcManager.createCameraTracks()
await rtcManager.createMicrophoneAudioTrack()
await rtcManager.join({
channel,
userId,
})
dispatch(
setOptions({
...options,
appId: rtcManager.appId ?? "",
token: rtcManager.token ?? "",
}),
)
await rtcManager.publish()
dispatch(setRoomConnected(true))
hasInit = true
}
const destory = async () => {
console.log("[rtc] destory")
rtcManager.off("textChanged", onTextChanged)
rtcManager.off("localTracksChanged", onLocalTracksChanged)
rtcManager.off("remoteUserChanged", onRemoteUserChanged)
await rtcManager.destroy()
dispatch(setRoomConnected(false))
hasInit = false
}
const onRemoteUserChanged = (user: IRtcUser) => {
console.log("[rtc] onRemoteUserChanged", user)
if (useTrulienceAvatar) {
// trulience SDK will play audio in synch with mouth
user.audioTrack?.stop();
}
if (user.audioTrack) {
setRemoteUser(user)
}
}
const onLocalTracksChanged = (tracks: IUserTracks) => {
console.log("[rtc] onLocalTracksChanged", tracks)
const { videoTrack, audioTrack, screenTrack } = tracks
setVideoTrack(videoTrack)
setScreenTrack(screenTrack)
if (audioTrack) {
setAudioTrack(audioTrack)
}
}
const onTextChanged = (text: IChatItem) => {
console.log("[rtc] onTextChanged", text)
dispatch(
addChatItem(text),
)
}
const onVoiceChange = (value: any) => {
dispatch(setVoiceType(value))
}
const onVideoSourceTypeChange = async (value: VideoSourceType) => {
await rtcManager.switchVideoSource(value)
setVideoSourceType(value)
}
return (
<div className={cn("flex h-full flex-col min-h-0", className)}>
{/* Scrollable top region (Avatar or ChatCard) */}
<div className="min-h-0 overflow-y-auto z-10">
{useTrulienceAvatar ? (
!avatarInLargeWindow ? (
<div className="h-60 w-full p-1">
<Avatar localAudioTrack={audioTrack} audioTrack={remoteuser?.audioTrack} />
</div>
) : (
!isCompactLayout &&
<ChatCard
className="m-0 w-full h-full rounded-b-lg bg-[#181a1d] md:rounded-lg"
/>
)
) : (
<AgentView audioTrack={remoteuser?.audioTrack} />
)}
</div>
{/* Bottom region for microphone and video blocks */}
<div className="w-full space-y-2 px-2 py-2">
<MicrophoneBlock audioTrack={audioTrack} />
<VideoBlock
cameraTrack={videoTrack}
screenTrack={screenTrack}
videoSourceType={videoSourceType}
onVideoSourceChange={onVideoSourceTypeChange}
/>
</div>
</div>
);
}
|