This article introduces how to quickly implement interactive live broadcast on Windows platform through Agora SDK. The difference between an interactive live broadcast and a real-time call is that the users of the live channel have different roles. You can set the role as a host or a viewer, where the host can receive and send streams, and the viewers can only receive streams.

Demo experience

Agora provides an open source real-time audio and video call sample project OpenLive-Windows on GitHub. Before implementing related functions, you can download and view the source code: https://github.com/AgoraIO/Basic-Video-Broadcasting/tree/master/OpenLive-Windows-MFC

Preconditions

  • Microsoft Visual Studio 2017 or above
  • Windows devices that support Windows 7 or above
  • Valid Agora account (free registration)

If a firewall is deployed in your network environment, please open the relevant ports according to "Applying Enterprise Firewall Restrictions" in the SoundNet Documentation Center.

Set up the development environment

This section describes how to create a project and integrate the Agora SDK into your project.

Create a Windows project

Follow the steps below to create a Windows project. If you already have a Windows project, check the integrated SDK directly.

1. Open Microsoft Visual Studio and click New Project.

2. Enter the new project window, select the project type as MFC application, enter the project name, select the project storage path, and click OK.

3. Go to the MFC application window, select the application type as dialog-based, and click Finish.

Integrated SDK

Follow the steps below to integrate the Agora SDK into your project.

1. Configure the project file

  • According to the application scenario, download the latest SDK from the SDK, unzip it and open it.
  • Open the downloaded SDK file and copy the sdk folder in it to your project folder .

2. Configure project properties

In the Solution Explorer window, right-click the project name and click Properties to configure the following, and click OK when the configuration is complete.

  • Go to the C/C++ > General > Additional Include Directories menu, click Edit, and select the decompressed libs/include in the pop-up window.
  • Go to Linker > General > Additional Libraries Directories menu, click Edit, and unpack libs/x86 or libs/x86_64 in the popup window.
  • Go to the Linker > Input > Additional Dependencies menu, click Edit, and unpack the agora_rtc_sdk.lib in the popup.

Realize live audio and video

This section describes how to implement live audio and video. The API usage sequence of live video is shown in the following figure:
image

1. Create the user interface

In order to experience audio and video calls intuitively, a user interface (UI) needs to be created according to the application scenario. If there is a user interface in the project, directly view the initialization of IRtcEngine.

If you want to implement a live video, it is recommended to add the following controls to the UI:

  • Anchor video window
  • leave channel button

When you use the UI design in the sample project, you will see the following interface:

image

2. Initialize IRtcEngine

Before calling other Agora APIs, an IRtcEngine object needs to be created and initialized.

You need to fill in your project's App ID in this step. Please refer to the following steps to create an Agora project on the console and obtain the App ID.

1. Log in to the console and click the project management icon in the left navigation bar img .

2. Click Create, follow the on-screen prompts to set the project name, select an authentication mechanism, and then click Submit.

3. On the project management page, you can get the App ID of the project.

在这里插入图片描述
image1019×537 52.3KB

Call createAgoraRtcEngine and initialize methods, and pass in the obtained App ID to initialize IRtcEngine .
You can also implement other functions during initialization according to your needs. Such as callbacks for registered users to join and leave channels (declared in AgoraObject.h).

 // 在主播调用 joinChannel 方法后,此回调会报告加入频道的主播信息。
    BOOL JoinChannel(LPCTSTR lpChannelName, UINT nUID = 0, LPCSTR lpDynamicKey = NULL);

    // 在主播调用 leaveChannel 方法后,此回调会报告离开频道的主播信息。
    BOOL LeaveCahnnel();
  //静音本地音频
BOOL MuteLocalAudio(BOOL bMuted = TRUE);
    BOOL IsLocalAudioMuted();
  //静音所有远端音频
    BOOL MuteAllRemoteAudio(BOOL bMuted = TRUE);
    BOOL IsAllRemoteAudioMuted();
  //静音本地视频
    BOOL MuteLocalVideo(BOOL bMuted = TRUE);
    BOOL IsLocalVideoMuted();
  //静音所有远端视频
    BOOL MuteAllRemoteVideo(BOOL bMuted = TRUE);
    BOOL IsAllRemoteVideoMuted();
    BOOL IsAllRemoteVideoMuted();

3. Set channel mode

After initialization, call the setChannelProfile method to set the channel mode to live.

An IRtcEngine can only use one channel mode. If you want to switch to other modes, you need to call the release method to release the current IRtcEngine instance, then call the createAgoraRtcEngine and setChannelProfile initialize method to create a new instance, and then call the- method setChannelProfile Set new channel mode.

 BOOL SetChannelProfile(BOOL bBroadcastMode);
    BOOL IsBroadcastMode();

4. Set user roles

There are two user roles for live channels: host and viewer, and the default role is viewer. After setting the channel mode to live, you can set the user role in the app by referring to the following steps:

  • Let users choose whether their role is a streamer or a viewer
  • Call the setClientRole method, and then use the role selected by the user to pass parameters

Note that users in the live channel can only see the anchor's screen and hear the anchor's voice. After joining the channel, if you want to switch user roles, you can also call setClientRole method.

 //在AgoraObject.h中声明
    BOOL SetClientRole(CLIENT_ROLE_TYPE role, LPCSTR lpPermissionKey = NULL);
    int  GetClientRole() { return m_nRoleType; };
//在AgoraObject.cpp实现
BOOL CAgoraObject::SetClientRole(CLIENT_ROLE_TYPE role, LPCSTR lpPermissionKey)
{
    // 设置用户角色。
    int nRet = m_lpAgoraEngine->setClientRole(role);
    m_nRoleType = role;
    return nRet == 0 ? TRUE : FALSE;
}
/ 创建选择用户角色的对话框。
void CEnterChannelDlg::OnCbnSelchangeCmbRole()
{
    int nSel = m_ctrRole.GetCurSel();
    if (nSel == 0)
        CAgoraObject::GetAgoraObject()->SetClientRole(CLIENT_ROLE_BROADCASTER);
    else
        CAgoraObject::GetAgoraObject()->SetClientRole(CLIENT_ROLE_AUDIENCE);
}

5. Set the local view

If you want to implement a voice live broadcast, you can directly view the join channel.

After successfully initializing the IRtcEngine object, you need to set the local view before joining the channel, so that the host can see the local image in the live broadcast. Follow the steps below to set up the local view (OpenLiveDig.cpp):

  • Call enableVideo method to enable the video module.
  • Call setupLocalVideo method to set the local view.
 // 启用视频模块。
CAgoraObject::GetAgoraObject()->EnableVideo(TRUE);

// 设置本地视图。
VideoCanvas vc;

    vc.uid = 0;
    vc.view = m_dlgVideo.GetLocalVideoWnd();
    vc.renderMode = RENDER_MODE_TYPE::RENDER_MODE_FIT;

    //cancel setVideoProfile bitrate since version 2.1.0
    int nVideoSolution = m_dlgSetup.GetVideoSolution();
//
    VideoEncoderConfiguration config;
    config.bitrate = m_dlgSetup.GetBirate();
    config.frameRate = (FRAME_RATE)m_dlgSetup.GetFPS();
    SIZE resolution = m_dlgSetup.GetVideoResolution();
    config.dimensions.width = resolution.cx;
    config.dimensions.height = resolution.cy;
    lpRtcEngine->setVideoEncoderConfiguration(config);
//
    m_dlgVideo.SetWindowText(strChannelName);
    lpRtcEngine->setupLocalVideo(vc);
    lpRtcEngine->startPreview();

6. Join a channel

After setting the role and local view (live video scene), you can call the joinChannel method to join the channel. You need to pass the following parameters to this method:

  • channelName : Pass in the channel ID that can identify the channel. Users who enter the same channel ID will enter the same channel.
  • token: Pass in a Token that identifies the user's roles and permissions. Can be set to one of the following values: If the project has enabled App Certificate, please use Token.

    • NULL
    • Temporary Token. The temporary token service is valid for 24 hours. You can generate a temporary token in the console. For details, see Obtaining a Temporary Token.
    • Token generated on your server side. In scenarios with high security requirements, we recommend that you use Tokens generated in this way. For details, see Generating Tokens.
  • uid : ID of the local user. The data type is integer, and each user's uid must be unique within the channel. If uid is set to 0, the SDK will automatically assign a uid and report it in the onJoinChannelSuccess callback.

For more parameter setting precautions, please find and refer to the parameter description in the joinChannel interface in the SoundNet Documentation Center.

 // 加入频道。
std::string token = lpAgoraObject->GetToken();
lpAgoraObject->JoinChannel(strChannelName, 0, token.length() > 0 ? token.c_str() : NULL);

7. Set the remote view

In a live video, whether you are the host or the viewer, you should see all the hosts on the channel. After joining the channel, you can set the view of the remote host by calling the method setupRemoteVideo .

After the remote host successfully joins the channel, the SDK will trigger the onFirstRemoteVideoDecoded callback, which will contain the host's uid information. Call the setupRemoteVideo method in the callback, pass in the obtained uid , and set the view of the anchor (AGEngineEventHandler.h).

 // 在引擎收到第一帧远端视频流并解码成功时,会触发此回调(AGEngineEventHandler.cpp)。
void CAGEngineEventHandler::onFirstRemoteVideoDecoded(uid_t uid, int width, int height, int elapsed)
{
    LPAGE_FIRST_REMOTE_VIDEO_DECODED lpData = new AGE_FIRST_REMOTE_VIDEO_DECODED;
    lpData->uid = uid;
    lpData->width = width;
    lpData->height = height;
    lpData->elapsed = elapsed;
    if(m_hMainWnd != NULL)
        ::PostMessage(m_hMainWnd, WM_MSGID(EID_FIRST_REMOTE_VIDEO_DECODED), (WPARAM)lpData, 0);
}

8. Leave the channel

According to the needs of the scene, such as ending the call, closing the App or switching the App to the background, call leaveChannel to leave the current call channel.

 BOOL CAgoraObject::LeaveCahnnel()
{
    m_lpAgoraEngine->stopPreview();
    int nRet = m_lpAgoraEngine->leaveChannel();

    m_nSelfUID = 0;
    bJoinedChannel = false;
    return nRet == 0 ? TRUE : FALSE;
}
//关闭引擎
void CAgoraObject::CloseAgoraObject()
{
    if(m_lpAgoraEngine != NULL)
        m_lpAgoraEngine->release();
     
    if(m_lpAgoraObject != NULL)
        delete m_lpAgoraObject;

    m_lpAgoraEngine = NULL;
    m_lpAgoraObject = NULL;
}

sample code

You can view the complete source code and code logic in the OpenLive-Windows sample code. This article uses the C++ MFC project.

github: https://github.com/AgoraIO/Basic-Video-Broadcasting/tree/master/OpenLive-Windows-MFC

run the project

Run the project on a Windows device. When the live video broadcast is successfully started, the host can see his own screen; the audience can see the host's screen (the current configuration is Debug mode, x64 system).

//Open the interface

image
//test camera

image

//Start live broadcast, thick code

在这里插入图片描述
image938×715 109KB


RTE开发者社区
663 声望975 粉丝

RTE 开发者社区是聚焦实时互动领域的中立开发者社区。不止于纯粹的技术交流,我们相信开发者具备更加丰盈的个体价值。行业发展变革、开发者职涯发展、技术创业创新资源,我们将陪跑开发者,共享、共建、共成长。