The Agora SDK introduced the virtual background extension that lets you apply a color, image or blur to the background of your video in a video call. In this blog post, weāll take a look at how we can use this extension with the Agora Web UIKit.
You can find a code snippet at the end of this post to use the extension without the UIKit.
Prerequisites
- An Agora developer account (Itās free, sign up here!)
- Node.js LTS
- A high-level understanding of React
Setup
You can get the code for the example on GitHub, or you can create your own React project. Open a terminal and execute:
npx create-react-app demo --template typescript
cd demo
Install the Agora React UIKit & the Virtual Background Extension:
npm i agora-react-uikit agora-extension-virtual-background
Note: At the time of writing, the current agora-react-uikit
release is v1.1.0
.
Thatās the setup. You can now executeĀ npm start
Ā to start the web server on localhost.
Building the Live Stream
The UIKit gives you access to a high-level component called <AgoraUIKit>
that can be used to render a full video call or live stream, read more. Weāll use the modular components to compose the video call and add the virtual background feature.
Clear out theĀ App.tsx
Ā file to start fresh:
Weāll create a state variable called videocall. When itās true weāll render the videocall, and when itās false weāll render a button to toggle the application state and start the stream.
To build the videocall, weāll wrap everything with the PropsContext
component to pass in the user props to the UIKit. Weāll add the props to the rtcProps
Ā ā Agora App ID, channel name and token. When the user clicks the end call button, weāll toggle to set the videocall
state to false.
Note:Ā Token authentication is recommended for all RTE apps running in production environments. For more information about token-based authentication in the Agora platform, see this guide:Ā https://docs.agora.io/en/Video/token?platform=All%20Platforms.
Weāll wrap our application with in-built components from the UIKit, these components give us access to UIKit data and handle a lot of the video call logic for us. Weāll use TrackConfigure
: gives us access to the local tracks, RtcConfigure
: handles the call logic, LocalUserContext
: handles the local user state and RtmConfigure
: handles the signalling logic.
Weāll render these components from the UIKit ā RemoteMutePopup
: display the mute request popup, GridVideo
: renders the user videos in a grid and LocalControls
: renders the controls to mute/unmute the local tracks. Weāll add a new VirtualBackground
component that weāll define now!
Using the Agora virtual background extension
To make the virtual background performant, Agora uses a WebAssembly (WASM) module. Weāll need to host the WASM file ā you can host it separately via a CDN or bundle it with your website. Letās create theĀ VirtualBackground
Ā component:
Weāll create a state variableĀ extensionActive
Ā to enable/disable the virtual background. We can get access to theĀ localVideoTrack
Ā from theĀ TracksContext
. Weāll create an instance of theĀ VirtualBackgroundExtension
Ā calledĀ ext
Ā and weāll create a ref to store the background processor.
Inside theĀ useEffect
Ā weāll initialize the extension by calling theĀ registerExtensions
Ā method with our extension instance. Weāll call theĀ createProcessor
Ā method on the extension and assign it to theĀ processor
Ā ref. Weāll then call theĀ init
Ā method on the extension with the path to the WASM file, this can be an absolute or a relative URL.
Weāll create the enableBackground
where we pipe the localVideoTrack
to the processor
which is then piped to trackās processorDestination
. We can use the setOptions
method to define the virtual background, for this example Iām using a pink color, but you can also use a blur effect or an image. Weāll enable the processor and update our state.
For the disableBackground
function, weāll just unpipe the local track and disable the processor before updating the state.
For the UI weāll render a div element that calls the correct method to toggle the effect based on our state.
If youāre not using the UIKit you can use the following snippet:
Conclusion
Thatās how you can use a WebAssembly based Agora Extension for Virtual Backgrounds with the Agora Web UIKit. You can read more about how you can customize the design and functionality of the UIKit (for example, you can use it for live-streaming) inĀ this blog post.
If you have questions while using this project or the Agora Web UIKit, I invite you to join the Agora Developer Slack community, where you can ask them in the #web-help-me channel
. Feel free to open issues for feature requests or to report bugs on the UIKit Repo or the project repo.