-
Notifications
You must be signed in to change notification settings - Fork 10
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
1 parent
a040527
commit e0ae84c
Showing
15 changed files
with
550 additions
and
4 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,91 @@ | ||
import { | ||
AgoraRTCScreenShareProvider, | ||
LocalAudioTrack, | ||
LocalVideoTrack, | ||
useJoin, | ||
usePublish, | ||
useTrackEvent, | ||
} from "agora-rtc-react"; | ||
import type { ILocalAudioTrack, ILocalTrack, ILocalVideoTrack } from "agora-rtc-sdk-ng"; | ||
import AgoraRTC from "agora-rtc-sdk-ng"; | ||
import { useEffect, useState } from "react"; | ||
|
||
import { appConfig } from "../utils"; | ||
|
||
interface ShareScreenProps { | ||
screenShareOn: boolean; | ||
screenTrack: ILocalVideoTrack | [ILocalVideoTrack, ILocalAudioTrack] | null; | ||
onCloseScreenShare?: () => void; | ||
} | ||
|
||
export const ScreenShare = ({ | ||
screenShareOn, | ||
screenTrack, | ||
onCloseScreenShare, | ||
}: ShareScreenProps) => { | ||
const [client] = useState(() => AgoraRTC.createClient({ mode: "rtc", codec: "vp8" })); | ||
|
||
//screen share | ||
const [screenVideoTrack, setScreenVideoTrack] = useState<ILocalVideoTrack | null>(null); | ||
const [screenAudioTrack, setScreenAudioTrack] = useState<ILocalAudioTrack | null>(null); | ||
|
||
//join room | ||
useJoin( | ||
{ | ||
appid: appConfig.appId, | ||
channel: appConfig.channel, | ||
token: appConfig.token, | ||
uid: appConfig.ShareScreenUID, | ||
}, | ||
screenShareOn, | ||
client, | ||
); | ||
|
||
//get screen share video track and audio track | ||
useEffect(() => { | ||
if (!screenTrack) { | ||
setScreenAudioTrack(null); | ||
setScreenVideoTrack(null); | ||
} else { | ||
if (Array.isArray(screenTrack)) { | ||
setScreenVideoTrack( | ||
screenTrack.filter( | ||
(track: ILocalTrack) => track.trackMediaType === "video", | ||
)[0] as ILocalVideoTrack, | ||
); | ||
setScreenAudioTrack( | ||
screenTrack.filter( | ||
(track: ILocalTrack) => track.trackMediaType === "audio", | ||
)[0] as ILocalAudioTrack, | ||
); | ||
} else { | ||
setScreenVideoTrack(screenTrack); | ||
} | ||
} | ||
}, [screenTrack]); | ||
|
||
//publish screen share | ||
usePublish([screenVideoTrack, screenAudioTrack], screenShareOn, client); | ||
|
||
//screen share closed | ||
useTrackEvent(screenVideoTrack, "track-ended", () => { | ||
console.log("screen sharing ended"); | ||
onCloseScreenShare && onCloseScreenShare(); | ||
}); | ||
|
||
return ( | ||
<AgoraRTCScreenShareProvider client={client}> | ||
{screenShareOn && screenVideoTrack && ( | ||
<LocalVideoTrack | ||
disabled={!screenShareOn} | ||
play={screenShareOn} | ||
style={{ width: "300px", height: "300px" }} | ||
track={screenVideoTrack} | ||
/> | ||
)} | ||
{screenAudioTrack && <LocalAudioTrack disabled={!screenShareOn} track={screenAudioTrack} />} | ||
</AgoraRTCScreenShareProvider> | ||
); | ||
}; | ||
|
||
export default ScreenShare; |
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,94 @@ | ||
import { | ||
useJoin, | ||
useLocalScreenTrack, | ||
useRemoteAudioTracks, | ||
useRemoteUsers, | ||
useRemoteVideoTracks, | ||
} from "agora-rtc-react"; | ||
import { Button } from "antd"; | ||
import { useEffect, useState } from "react"; | ||
|
||
import { Container, MediaControl, Room, ScreenShare } from "../../../components"; | ||
import { RenderRemoteUsers } from "../../../components/RemoteUsers"; | ||
import { appConfig } from "../../../utils"; | ||
|
||
export const ShareScreen = () => { | ||
const [calling, setCalling] = useState(false); | ||
|
||
useJoin( | ||
{ | ||
appid: appConfig.appId, | ||
channel: appConfig.channel, | ||
token: appConfig.token, | ||
}, | ||
calling, | ||
); | ||
|
||
const [micOn, setMic] = useState(false); | ||
const [cameraOn, setCamera] = useState(false); | ||
|
||
//screen share | ||
const [screenShareOn, setScreenShareOn] = useState(false); | ||
//create screen share track | ||
const { screenTrack, error } = useLocalScreenTrack(screenShareOn, {}, "auto"); | ||
//if screen got error, close screen share | ||
useEffect(() => { | ||
setScreenShareOn(false); | ||
}, [error]); | ||
|
||
const remoteUsers = useRemoteUsers(); | ||
const { videoTracks } = useRemoteVideoTracks( | ||
//remember to filter out screen share user to avoid duplicate payment | ||
remoteUsers.filter(user => user.uid !== appConfig.ShareScreenUID), | ||
); | ||
const { audioTracks } = useRemoteAudioTracks( | ||
//remember to filter out screen share user to avoid duplicate payment | ||
remoteUsers.filter(user => user.uid !== appConfig.ShareScreenUID), | ||
); | ||
audioTracks.map(track => track.play()); | ||
|
||
return ( | ||
<Container> | ||
<div className="h-screen p-3"> | ||
{calling && ( | ||
<> | ||
<Button onClick={() => setScreenShareOn(a => !a)} type="primary"> | ||
{screenShareOn ? "stop share screen" : "start share screen"} | ||
</Button> | ||
{screenShareOn && ( | ||
<ScreenShare | ||
onCloseScreenShare={() => { | ||
setScreenShareOn(false); | ||
}} | ||
screenShareOn={screenShareOn} | ||
screenTrack={screenTrack} | ||
/> | ||
)} | ||
<Room | ||
cameraOn={cameraOn} | ||
micOn={micOn} | ||
renderRemoteUsers={() => <RenderRemoteUsers videoTracks={videoTracks} />} | ||
/> | ||
</> | ||
)} | ||
</div> | ||
<MediaControl | ||
calling={calling} | ||
cameraOn={cameraOn} | ||
micOn={micOn} | ||
setCalling={() => { | ||
setCalling(a => !a); | ||
setScreenShareOn(false); | ||
}} | ||
setCamera={() => { | ||
setCamera(a => !a); | ||
}} | ||
setMic={() => { | ||
setMic(a => !a); | ||
}} | ||
/> | ||
</Container> | ||
); | ||
}; | ||
|
||
export default ShareScreen; |
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
93 changes: 93 additions & 0 deletions
93
examples/basic/src/pages/hook/useLocalScreenTrack/index.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,93 @@ | ||
import { | ||
LocalVideoTrack, | ||
useJoin, | ||
useLocalScreenTrack, | ||
usePublish, | ||
useTrackEvent, | ||
} from "agora-rtc-react"; | ||
import type { ILocalAudioTrack, ILocalTrack, ILocalVideoTrack } from "agora-rtc-sdk-ng"; | ||
import { Button, Typography } from "antd"; | ||
import { useEffect, useState } from "react"; | ||
|
||
import { Container } from "../../../components"; | ||
import { appConfig } from "../../../utils"; | ||
const { Title } = Typography; | ||
|
||
export const UseLocalScreenTrack = () => { | ||
//screen share | ||
const [screenShareOn, setScreenShareOn] = useState(false); | ||
const [screenVideoTrack, setScreenVideoTrack] = useState<ILocalVideoTrack | null>(null); | ||
const [screenAudioTrack, setScreenAudioTrack] = useState<ILocalAudioTrack | null>(null); | ||
|
||
//join room | ||
useJoin( | ||
{ | ||
appid: appConfig.appId, | ||
channel: appConfig.channel, | ||
token: appConfig.token, | ||
uid: appConfig.ShareScreenUID, | ||
}, | ||
screenShareOn, | ||
); | ||
|
||
//create screen share track | ||
const { screenTrack, error } = useLocalScreenTrack(screenShareOn, {}, "auto"); | ||
|
||
//get screen share video track and audio track | ||
useEffect(() => { | ||
if (!screenTrack) { | ||
setScreenAudioTrack(null); | ||
setScreenVideoTrack(null); | ||
} else { | ||
if (Array.isArray(screenTrack)) { | ||
setScreenVideoTrack( | ||
screenTrack.filter( | ||
(track: ILocalTrack) => track.trackMediaType === "video", | ||
)[0] as ILocalVideoTrack, | ||
); | ||
setScreenAudioTrack( | ||
screenTrack.filter( | ||
(track: ILocalTrack) => track.trackMediaType === "audio", | ||
)[0] as ILocalAudioTrack, | ||
); | ||
} else { | ||
setScreenVideoTrack(screenTrack); | ||
} | ||
} | ||
}, [screenTrack]); | ||
|
||
//publish screen share | ||
usePublish([screenVideoTrack, screenAudioTrack], screenShareOn); | ||
|
||
//if screen got error, close screen share and leave room | ||
useEffect(() => { | ||
setScreenShareOn(false); | ||
}, [error]); | ||
|
||
//screen share closed | ||
useTrackEvent(screenVideoTrack, "track-ended", () => { | ||
console.log("screen sharing ended"); | ||
setScreenShareOn(false); | ||
}); | ||
|
||
return ( | ||
<Container> | ||
<div className="h-screen p-3"> | ||
<Title>useLocalScreenTrack</Title> | ||
<Button onClick={() => setScreenShareOn(a => !a)} type="primary"> | ||
{screenShareOn ? "stop share screen" : "start share screen"} | ||
</Button> | ||
{screenShareOn && screenVideoTrack && ( | ||
<LocalVideoTrack | ||
disabled={!screenShareOn} | ||
play={screenShareOn} | ||
style={{ width: "600px", height: "400px" }} | ||
track={screenVideoTrack} | ||
/> | ||
)} | ||
</div> | ||
</Container> | ||
); | ||
}; | ||
|
||
export default UseLocalScreenTrack; |
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
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.