Preload channels
In live streaming applications, a key user experience performance metric is how quickly a viewer can join or switch channels and how fast the remote video stream is rendered on their device. This document explores methods and best practices for reducing first-frame rendering time in apps using Video SDK and provides sample code for desktop and mobile devices.
Prerequisites
To test the code used in this page you need to have:
- An Agora account and project.
- A computer with Internet access. Ensure that no firewall is blocking your network communication.
- Implemented the SDK quickstart.
Project setup
Agora provides an open source VideoLoaderAPI sample project on GitHub that includes APIs for fast channel loading and switching. To integrate the APIs into your project, copy the following files from the videoloaderapi directory to your project:
To facilitate future code upgrades, do not modify the names and paths of these files.
Implementation
Follow these steps to implement fast channel joining and switching:
Prepare to use the VideoLoader API
Take the following steps before using the fast joining and fast switching features:
-
Initialize an
RtcEngineinstance.Call the
createmethod to create and initialize anRtcEngineinstance: -
Use a wildcard token.
To speed up the process of users joining a channel, use a wildcard token. Generate the token on your server and pass it to the client for authentication.
NoteUsing wildcard tokens carries security risks, such as room bombing. Evaluate whether wildcard tokens are appropriate for your use case before implementation.
Fast channel joining
This section explains how to create a seamless, instant-opening experience in live broadcast scenarios.
-
Add a UI element for the live broadcast room list.
Implement a UI element to display a list of live broadcast rooms. The following example uses a
RecyclerView: -
Listen for scrolling events.
Create an
OnRoomListScrollEventHandlerinstance and register it as a proxy for live room list scroll events in the UI. To create theOnRoomListScrollEventHandlerinstance, pass the following parameters to its constructor:mRtcEngine: A previously initializedRtcEngineinstance.localUid: The uid of the local user.
The
OnRoomListScrollEventHandlerlistens for scroll events in the live broadcast room list and applies best practices encapsulated within the class. It preloads channels for live broadcast rooms that appear on the screen (preloadChannel). -
Listen for touch events.
Create an
OnLiveRoomItemTouchEventHandlerinstance and register it as a proxy for touch events in a single live room UI. To create theOnLiveRoomItemTouchEventHandlerinstance, pass the following parameters to its constructor:mRtcEngine: A previously initializedRtcEngineinstance.roomInfo: AVideoLoader.RoomInfoinstance.
The
OnLiveRoomItemTouchEventHandlerinstance listens for touch events in a live broadcast room and implements the best practices encapsulated within the class. When a user taps a live room, they enter it automatically. You do not need to explicitly calljoinChannelor similar methods in the business layer.The
OnLiveRoomItemTouchEventHandlerinstance automatically adds the host screen to the container and renders it when it receives theonRequireRenderVideoevent. To ensure smooth rendering, create a container for the host screen in advance and return it to the handler when the event occurs.
Fast channel switching
This section explains how to enable viewers to switch live broadcast channels quickly.
-
Implement a UI component for sliding between live broadcast rooms.
The following example uses a
ViewPager2element: -
Listen for live room switching events.
Create an
OnPageScrollEventHandlerinstance and register it as the slide event proxy for theViewPager2live broadcast room. To create theOnPageScrollEventHandlerinstance, pass the following parameters to the constructor:mRtcEngine: A previously initializedRtcEngineinstance.localUid: Theuidof the local user.needPreJoin: Determines whether to prejoin the adjacent live broadcast rooms (above and below the current one). Setting this totrueimproves instant switching but increases resource consumption.sliceMode: Toggles the timing of outputting pictures in the live broadcast room.
The
OnPageScrollEventHandlerinstance implements best practices for managing live broadcast rooms. It listens for theViewPager2slide event of the live broadcast room and optimally switches audio/video subscription behavior between rooms. It triggers the following events at the correspondingposition:onPageStartLoading: Just started loading/displaying the live broadcast room.onPageLoaded: Live room loading/display completedonPageLeft: Left the live broadcast roomonRequireRenderVideo: The best time to render the host view of the corresponding live broadcast room. At this time, pass in the container for the host screen of the corresponding live broadcast room. TheOnPageScrollEventHandlerinstance adds and renders the host screen on the container.
Call
updateRoomListwhen the Activity is created to pass the initial live broadcast room list to theonPageScrollEventHandlerinstance. Additionally, callonRoomCreatedwithin thecreateFragmentevent ofFragmentStateAdapterto notifyonPageScrollEventHandlerthat the corresponding live room has been created. Add the following code insideoverride fun onCreate(savedInstanceState: Bundle?) {}:
Release resources
After using the VideoLoaderAPI, you do not need to call leaveChannel in the business layer to leave the live broadcast. To release resources, refer to the following sample code:
This feature guide is not available yet.