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
RtcEngine
instance.Call the
create
method to create and initialize anRtcEngine
instance: -
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
OnRoomListScrollEventHandler
instance and register it as a proxy for live room list scroll events in the UI. To create theOnRoomListScrollEventHandler
instance, pass the following parameters to its constructor:mRtcEngine
: A previously initializedRtcEngine
instance.localUid
: The uid of the local user.
The
OnRoomListScrollEventHandler
listens 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
OnLiveRoomItemTouchEventHandler
instance and register it as a proxy for touch events in a single live room UI. To create theOnLiveRoomItemTouchEventHandler
instance, pass the following parameters to its constructor:mRtcEngine
: A previously initializedRtcEngine
instance.roomInfo
: AVideoLoader.RoomInfo
instance.
The
OnLiveRoomItemTouchEventHandler
instance 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 calljoinChannel
or similar methods in the business layer.The
OnLiveRoomItemTouchEventHandler
instance automatically adds the host screen to the container and renders it when it receives theonRequireRenderVideo
event. 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
ViewPager2
element: -
Listen for live room switching events.
Create an
OnPageScrollEventHandler
instance and register it as the slide event proxy for theViewPager2
live broadcast room. To create theOnPageScrollEventHandler
instance, pass the following parameters to the constructor:mRtcEngine
: A previously initializedRtcEngine
instance.localUid
: Theuid
of the local user.needPreJoin
: Determines whether to prejoin the adjacent live broadcast rooms (above and below the current one). Setting this totrue
improves instant switching but increases resource consumption.sliceMode
: Toggles the timing of outputting pictures in the live broadcast room.
The
OnPageScrollEventHandler
instance implements best practices for managing live broadcast rooms. It listens for theViewPager2
slide 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. TheOnPageScrollEventHandler
instance adds and renders the host screen on the container.
Call
updateRoomList
when the Activity is created to pass the initial live broadcast room list to theonPageScrollEventHandler
instance. Additionally, callonRoomCreated
within thecreateFragment
event ofFragmentStateAdapter
to notifyonPageScrollEventHandler
that 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: