Preconditions

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

Note: 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.

Create a new MFC project

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.

Beginners directly download the github address project behind the article

Integrated SDK

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

1. Configure the project file

  • According to the application scenario, obtain the latest SDK from the official website, unzip and open it.
  • Copy the sdk folder in the download package to your project folder.
  • Currently version 3.6.2, the directory structure is libs/include, libs/x86, libs/x86_64

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.

Implement audio and video calls

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

1. Create the user interface

Create a user interface for audio and video calls for your project as required by the scenario. If there is a user interface, you can directly initialize the IRtcEngine.

If you want to implement a video call, we recommend that you add the following UI elements:

  • local video window
  • Remote video window
  • end call button

When you use the interface in the GitHub project linked in this article, you will see the following interface:
agor

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 ( console.agora.io ) 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.

Call createAgoraRtcEngine and initialize methods and pass in the obtained App ID to initialize IRtcEngine .

You can also register the callback events you want to monitor during initialization according to the needs of the scene, such as the local user joining the channel, and decoding the first frame of the remote user video.

 /** 
    create or return existing AgoraObject 
*/
CAgoraObject *CAgoraObject::GetAgoraObject(LPCTSTR lpAppId)
{
    if (m_lpAgoraObject == NULL)
        m_lpAgoraObject = new CAgoraObject();
    if (m_lpAgoraEngine == NULL)
        m_lpAgoraEngine = (IRtcEngine *)createAgoraRtcEngine();
    if (lpAppId == NULL)
        return m_lpAgoraObject;
    RtcEngineContext ctx;
    ctx.eventHandler = &m_EngineEventHandler;
#ifdef UNICODE
    char szAppId[128];
    ::WideCharToMultiByte(CP_ACP, 0, lpAppId, -1, szAppId, 128, NULL, NULL);
    ctx.appId = szAppId;
#else
    ctx.appId = lpAppId;
#endif
    m_lpAgoraEngine->initialize(ctx);
    return m_lpAgoraObject;
}
// 继承 IRtcEngineEventHandler 类中的回调与事件。
class CAGEngineEventHandler :
    public IRtcEngineEventHandler
{
public:
    CAGEngineEventHandler(void);
    ~CAGEngineEventHandler(void);

    void SetMsgReceiver(HWND hWnd = NULL);
    HWND GetMsgReceiver() {return m_hMainWnd;};
    // 本地用户成功加入频道时,会触发该回调。
    virtual void onJoinChannelSuccess(const char* channel, uid_t uid, int elapsed);
    // 本地重新加入频道时,会触发该回调。
    virtual void onRejoinChannelSuccess(const char* channel, uid_t uid, int elapsed);
    virtual void onWarning(int warn, const char* msg);
    virtual void onError(int err, const char* msg);
    virtual void onAudioQuality(uid_t uid, int quality, unsigned short delay, unsigned short lost);
    virtual void onAudioVolumeIndication(const AudioVolumeInfo* speakers, unsigned int speakerNumber, int totalVolume);
    // 本地用户成功离开频道时,会触发该回调。
    virtual void onLeaveChannel(const RtcStats& stat);
//以下还有多个扩展方法
/*
....
/*
private:
    HWND        m_hMainWnd;

};

3. Set the local view

If you want to implement a voice call, you can skip this step and see the next step "Join a Channel".

After successfully initializing the IRtcEngine object, you need to set up the local view before joining the channel so that you can see the local image during the call. The local camera is turned on by default, refer to the following steps to set the local view:

  • Call enableVideo method to enable the video module.
  • Call setupLocalVideo method to set the local view.
 // 启用视频模块。m_lpAgoraObject->GetEngine()->enableVideo();// 设置本地视图。
//在文件AgoraTutorialDlg.cpp中
 BOOL CAgoraTutorialDlg::OnInitDialog(){
VideoCanvas vc;

    vc.uid = 0;
    vc.view = m_wndLocal.GetSafeHwnd();
    vc.renderMode = RENDER_MODE_FIT;

    m_lpAgoraObject->GetEngine()->setupLocalVideo(vc);
//...

}

4. Join the channel

After completing the initialization and setting up the local view (video call scenario), 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:

Note: 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, please search "Get Temporary Token" in the documentation center.
  • Token generated on your server side. In scenarios with high security requirements, we recommend that you use Tokens generated in this way.
  • uid : ID of the local user. The data type is integer, and each user in the channel uid must be unique. 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 refer to the parameter description in the SoundNet Documentation Center joinChannel interface.

 // 加入频道。在文件"AgoraObject.cpp"中
BOOL CAgoraObject::JoinChannel(LPCTSTR lpChannelName, UINT nUID,LPCTSTR lpToken)
{
    int nRet = 0;
#ifdef UNICODE
    CHAR szChannelName[128];
    ::WideCharToMultiByte(CP_UTF8, 0, lpChannelName, -1, szChannelName, 128, NULL, NULL);
    char szToken[128];
    ::WideCharToMultiByte(CP_UTF8, 0, lpToken, -1, szToken, 128, NULL, NULL);

    if(0 == _tcslen(lpToken))
        nRet = m_lpAgoraEngine->joinChannel(NULL, szChannelName, NULL, nUID); 
    else
        nRet = m_lpAgoraEngine->joinChannel(szToken, szChannelName, NULL, nUID);
#else
    if(0 == _tcslen(lpToken))
        nRet = m_lpAgoraEngine->joinChannel(NULL, lpChannelName, NULL, nUID);
    else
        nRet = m_lpAgoraEngine->joinChannel(lpToken, lpChannelName, NULL, nUID);
#endif
    if (nRet == 0)
        m_strChannelName = lpChannelName;
    return nRet == 0 ? TRUE : FALSE;
}

5. Set the remote view

During a video call, usually you also need to see other users. After joining the channel, you can set the remote user's view by calling the setupRemoteVideo method.

After the remote user successfully joins the channel, the SDK will trigger the onFirstRemoteVideoDecoded callback, which will contain the uid information of the remote user. In this callback, call the setupRemoteVideo method, and pass in the obtained uid to set the view of the remote user.

 // SDK 接收到第一帧远端视频并成功解码时,会触发该回调。// 在该回调中调用 setupRemoteVideo 方法设置远端视图。存在于文件“AgoraTutorialDlg.cpp”中
LRESULT CAgoraTutorialDlg::OnFirstRemoteVideoDecoded(WPARAM wParam, LPARAM lParam)
{
    LPAGE_FIRST_REMOTE_VIDEO_DECODED lpData = (LPAGE_FIRST_REMOTE_VIDEO_DECODED)wParam;
    VideoCanvas vc;
    vc.renderMode = RENDER_MODE_FIT;
    vc.uid = lpData->uid;
    vc.view = m_wndRemote.GetSafeHwnd();
    m_lpAgoraObject->GetEngine()->setupRemoteVideo(vc);//设置远端视图
    delete lpData;
    return 0;
}

6. 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.

 /**
    Leave the channel
*/
BOOL CAgoraObject::LeaveChannel()
{
    m_lpAgoraEngine->stopPreview();
    int nRet = m_lpAgoraEngine->leaveChannel();

    return nRet == 0 ? TRUE : FALSE;
}

sample code

You can view the complete source code and code logic in the AgoraTutorialDlg.cpp file of the Agora-Windows-Tutorial-1to1 sample project.


RTE开发者社区
658 声望971 粉丝

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