One of the biggest challenges facing any developer is building applications that can scale. With a video-conferencing application using Agora, the main scaling issue is the bandwidth of your local device, especially if there are many incoming video streams. And as the number of participants rises, it becomes increasingly difficult to make sure that your application can keep up.
In this tutorial, you will see how to use Agora to build an application that can scale to up to 17 users by optimizing the bandwidth usage of the incoming video streams.
iOS (in your info.plist): Add both NSCameraUsageDescription and NSMicrophoneUsageDescription to Info.plist along with text descriptions. See here for more information on these, along with code samples of how to request and get authorization states.
All done! Let’s start building the app.
Build
We will stick with a very simple grid view. Our goal is to scale this application as the user joins. To do this, we will optimize our bandwidth usage to ensure all the streams work concurrently without any difficulty.
Building a Home Page
Our home page is responsible for prompting the user for the name of the channel they want to join and requesting camera and microphone permission.
This file contains hidden or 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
To request user permission, we are using a plug-in called permission handler.
Building the Video Call Page
1. Adding all the required variables
This file contains hidden or 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 hidden or 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
In the above code snippet, we set up the Agora engine with dual-stream mode enabled. Dual-stream is an Agora feature that allows a client to publish two streams at the same time. One stream is for a higher resolution and bitrate, and the other stream is for a lower resolution and bitrate. With this dual-stream setup, when a remote client subscribes to your stream, they can switch to a lower stream based on their bandwidth requirements.
3. Initializing the Agora SDK:
This file contains hidden or 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
The _initAgoraRtcEngine function creates an object of the AgoraRtcEngine, which we use to refer all the methods provided in the Agora SDK.
4. Adding Agora event handlers:
This file contains hidden or 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
In the userJoined callback, we are doing the following:
Adding the UID of the remote video to a list of UIDs (_users) that we use as the dataset for all the users’ UIDs present in the channel
Checking if the number of remote users has grown to five to know whether to switch to using the lower stream in dual-stream mode
In the userOffline callback, we are doing the following:
Removing the UID from the _users list
Switching back to the higher stream in the dual-stream mode if the number of remote users shrinks to three
5. Creating the grid view for local and remote user video
This file contains hidden or 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
Here, we simply pass the UID present in the _users list to generate a list of views. These views are then used to generate a grid that scales automatically as the user joins.
Cleaning Up
Finally, it is time to write the dispose method. We clean up all the resources relevant to Agora here:
This file contains hidden or 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
To test the application, you will need five or more users such that when they join the channel the video quality optimizes automatically in order to provide the best video calling experience.
To run the app, enter this command in your terminal:
flutter run
Conclusion
You now have a video chat application that can scale to up to 17 users by optimizing settings for incoming streams.
You can find a complete application using all of the above code on GitHub:
Other Resources
To learn more about the Agora Flutter SDK and other use cases, see the developer guide here.
You can also have a look at the complete documentation for the functions discussed above and many more here. And I invite you to join the Agora Developer Slack Community.
RTE Telehealth 2023
Join us for RTE Telehealth - a virtual webinar where we’ll explore how AI and AR/VR technologies are shaping the future of healthcare delivery.