People engage longer when they see, hear, and interact with each other. The Agora SDK enables you to embed real-time voice and video interaction in any app, on any device, anywhere.
This page shows the minimum code you need to add interactive live streaming standard into your app by using the Agora Video SDK.
The following figure shows the workflow to integrate into your app in order to add Interactive Live Streaming Standard functionality.
As shown in the figure, the workflow for adding Interactive Live Streaming Standard in your project is as follows:
Set the client role and latency level
Each user in an Interactive Live Streaming Standard channel is either a host or an audience member. Hosts publish streams to the channel, and the audience subscribe to the streams. When a user is an audience member, you can control the delay the user receives host streams using ClientRoleOptions
.
Retrieve a token
A token is the credential that authenticates a user when your app client joins a channel. In a test or production environment, your app client retrieves tokens from a server in your security infrastructure.
Join a channel
Call joinChannel
to create and join a channel. App clients that pass the same channel name join the same channel.
Publish and subscribe to audio and video in the channel
After joining a channel, app clients with the role of the host can publish audio and video. For an auidence memeber to send audio and video, call setClientRole
to switch the client role.
For an app client to join a channel, you need the following information:
In this section, we will create a Windows project and integrate the SDK into the project.
Now, let's build a Windows project from scratch. Skip to Integrate the SDK if a Windows project already exists.
Choose one of the following ways to add agora_rtc_sdk
into the project according to the type of your Windows Forms App.
.net core
agora_rtc_sdk
, select the Latest stable SDK, and then click Install. The SDK is now added to your project..net framework
agora_rtc_sdk
, select the Latest stable SDK, and then click Install. The SDK is now added to your project.This section introduces how to use the Agora SDK to start the interactive live streaming. The following figure shows the API call sequence of the live streaming.
Create the user interface (UI) for the one-to-one video call in your project. Skip to Import the SDK if you already have a UI in your project.
If you are implementing an interactive video streaming, we recommend adding the following elements into the UI:
Add the following line to import the Agora SDK:
using agora.rtc;
The C# SDK is developed upon C++. In this step, we create an AgoraRtcEventHandler
class from IAgoraRtcEngineEventHandler
for managing callback events:
class AgoraRtcEventHandler : IAgoraRtcEngineEventHandler
{
public AgoraRtcEventHandler()
{
}
};
Create and initialize the IAgoraRtcEngine
object before calling any other Agora APIs.
Call the CreateAgoraRtcEngine
and Initialize
methods, and pass in the App ID to initialize the IAgoraRtcEngine
object.
You can also listen for callback events, such as when the local user joins or leaves the channel.
// Declare variables
IAgoraRtcEngine rtc_engine_ = null;
IAgoraRtcEngineEventHandler event_handler_ = null;
if (null == rtc_engine_)
{
// Create rtc engine
rtc_engine_ = AgoraRtcEngine.CreateAgoraRtcEngine();
}
RtcEngineContext context = new RtcEngineContext(appId);
// Initialize rtc engine
int ret = rtc_engine_.Initialize(context);
event_handler_ = new AgoraRtcEventHandler();
// Initialize the engine event handler
rtc_engine_.InitEventHandler(event_handler_);
// Enable video
rtc_engine_.EnableVideo();
After initializing the IAgoraRtcEngine
object, call the SetChannelProfile
method to set the channel profile as LIVE_BROADCASTING
.
One IAgoraRtcEngine
object uses one profile only. If you want to switch to another profile, release the current IAgoraRtcEngine
object with the release method and create a new one before calling the SetChannelProfile
method.
rtc_engine_.SetChannelProfile(CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING);
In a live-streaming channel, you need to set the role and level of a user:
You may use the following steps to set the user role and level in your app:
Allow the end user to set the role as host or audience.
Call the SetClientRole
method and set the role
and options
parameters according to the user's choice:
role
as BROADCASTER
, set options
as null
. The latency between two hosts is < 400 ms.role
as AUDIENCE
, set the audienceLatencyLevel
parameter in options
as AUDIENCE_LATENCY_LEVEL_LOW_LATENCY
. The latency from the host's client to the audience's client is 1500 ms - 2000 ms.AgoraAudienceLatencyLevelLowLatency
to AgoraAudienceLatencyLevelUltraLowLatency
, in essence, you switch from Interactive Live Streaming Standard to Interactive Live Streaming Premium and the latency from the host's client to the audience's client changes to 400 ms - 800 ms.AgoraClientRoleAudience
to AgoraClientRoleBroadcaster
, in essence, you switch from Interactive Live Streaming Standard to Interactive Live Streaming Premium and the latency between two hosts is < 400 ms.AgoraAudienceLatencyLevelLowLatency
, the jitterBufferDelay
property in RemoteAudioStats
does not take effect.A LIVE_BROADCASTING
channel has two client roles: BROADCASTER
and AUDIENCE
, and the default role is AUDIENCE
. After setting the channel profile to LIVE_BROADCASTING
, your app may use the following steps to set the client role:
BROADCASTER
or AUDIENCE
.SetClientRole
method and pass in the client role set by the user.Note that in the interactive live streaming, only the host can be heard and seen. If you want to switch the client role after joining the channel, call the SetClientRole
method.
As an audience member:
ClientRoleOptions role_options = new ClientRoleOptions();
role_options.audienceLatencyLevel = AUDIENCE_LATENCY_LEVEL_TYPE.AUDIENCE_LATENCY_LEVEL_LOW_LATENCY;
rtc_engine_.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_AUDIENCE, role_options);
As a host:
rtc_engine_.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
After initializing the IAgoraRtcEngine
object, set the local video view before joining the channel so that hosts can see themselves.
VideoCanvas vs = new VideoCanvas((ulong)localWindowId.Handle, RENDER_MODE_TYPE.RENDER_MODE_FIT);
rtc_engine_.SetupLocalVideo(vs);
After initializing the IAgoraRtcEngine
object and setting the local video view, you can call the joinChannel
method to join a live streaming channel. In this method, set the following parameters:
channelName
: Specify the channel name that you want to join.
token
: Pass a token that identifies the role and privilege of the user. You can set it as one of the following values:
uid
is the same with those you use to generate the token.token
as "".uid
: ID of the local user that is an integer and should be unique. If you set uid
as 0, the SDK assigns a user ID for the local user and returns it in the OnJoinChannelSuccess
callback.
mute
methods accordingly.rtc_engine_.JoinChannel("", "testChannelId","", 0, new ChannelMediaOptions(true, true, true, true));
In a video streaming, you should be able to see all the hosts, regardless of your role. This is achieved by calling the SetupRemoteVideo
method after joining the channel.
Shortly after a remote host joins the channel, the SDK gets the host's ID in the OnUserJoined
callback. Call the SetupRemoteVideo
method in the callback, and pass in the uid
to set the video view of the remote host.
// Call SetupRemoteVideo to set the remote video view
VideoCanvas vc = new VideoCanvas((ulong)remoteWindowId,
RENDER_MODE_TYPE.RENDER_MODE_FIT, "testChannelId", remoteUid);
rtc_engine_.SetupRemoteVideo(vc);
Call the LeaveChannel
method to leave the current channel according to your scenario, for example, when the interactive streaming ends, when you need to close the app, or when your app runs in the background.
rtc_engine_.LeaveChannel();
After building the app, follow the steps to add the necessary libraries to the execution path:
.netcore
64-bit apps, copy all the libraries under netCore/x86_64
..netframework
32-bit apps, copy all the libraries under netFramework/x86
.Run the project on your Windows device. When you set the role as the host and successfully start the interactive video streaming, you can see the video view of yourself in the app. When you set the role as the audience and successfully join the interactive video streaming, you can see the video view of the host in the app.
Agora provides an open-source JoinChannelVideo sample project on GitHub. You can try it out or view the source code.