The basic features of ApsaraVideo Real-time Communication (RTC) include initializing the software development kit (SDK), joining a channel, publishing local streams, subscribing to remote streams, and leaving a channel. This topic describes how to implement these basic features.
Prerequisites
- You have downloaded and integrated the latest version of the software development kit (SDK). For more information, see Integrate the SDK for Windows.
- Obtain a token for channel authentication. For more information, see Use a token for authentication.
Procedure
- Initialize the SDK.
- Create an AliRTCEngine instance in the CRtcSampleDlg class and register AliRtcEventListener to listen for callbacks. For more information, see Callbacks and listeners.
m_pEngine = AliRtcEngine::sharedInstance(AliRtcEventListener* pListener, char* pConfig);// pListener is the AliRtcEventListener object that you implement to receive callbacks from the SDK. pConfig is the SDK initialization configuration. For the current version, use an empty string.Note Multiple instances are not supported. - Create a canvas layout to preview the local video.
- Start the local preview.
// Get the preview window. AliVideoCanvas canvas; canvas.renderMode = AliRtcRenderModeAuto; canvas.hWnd = /*Preview window handle*/; // Set the preview window. m_pEngine->setLocalViewConfig(canvas, AliRtcVideoTrackCamera); m_pEngine->startPreview(); canvas.flip = true;// true indicates that the image is mirrored. false indicates that the image is normal.Note AliRtcRenderMode provides four rendering modes.- (Recommended) AliRtcRenderModeAuto: The automatic mode.
- AliRtcRenderModeStretch: Stretches the video to fill the view without preserving the aspect ratio.
- AliRtcRenderModeFill: Scales the video while preserving the aspect ratio and adds black bars to fill the view.
- AliRtcRenderModeCrop: Scales and crops the video to fit the view while preserving the aspect ratio.
- Stop the local preview.
m_pEngine->stopPreview();
- Start the local preview.
- Configure publishing and subscribing.Note By default, streams are automatically published and subscribed. You can also manually publish and subscribe to streams.
- Automatic publishing mode: If you enable this mode, the SDK automatically publishes audio and video streams after you join a channel. If you disable this mode, you must call the publish method to publish the streams.
- Automatic subscribing mode: If you enable this mode, the SDK automatically subscribes to the audio and video streams of other users in the channel after you join. If you disable this mode, you must call the subscribe method to subscribe to their streams.
/* Configure automatic publishing and subscribing. This can only be configured before you call joinChannel. autoPub: true indicates automatic publishing. false indicates manual publishing. autoSub: true indicates automatic subscribing. false indicates manual subscribing. */ m_pEngine->setAutoPublishSubscribe(bool autoPub, bool autoSub);
- Create an AliRTCEngine instance in the CRtcSampleDlg class and register AliRtcEventListener to listen for callbacks. For more information, see Callbacks and listeners.
- Join a channel.
AliRtcAuthInfo authinfo; authinfo.channel = /* Channel ID */; authinfo.appid = /* Application ID */; authinfo.token = /* Channel authentication token */; authinfo.nonce = /* Nonce */; authinfo.user_id = /* User ID */; authinfo.timestamp = /* Timestamp */; authinfo.gslb = /* GSLB endpoint */; ... // joinChannel is an asynchronous method. Set a callback function. auto onJoinResult = [](void *opaque, int errCode) { if (errCode == 0) { // Joined the channel. } else { // Failed to join the channel. } }; m_pEngine->joinChannel(authinfo, /* Display name */, onJoinResult, /*UserData, which is passed to the onJoinResult callback function*/);Parameter Description appId The application ID. You can create and view the application ID on the Application Management page in the console. channelId The channel ID. It can be 1 to 64 characters in length and can contain letters, digits, underscores (_), and hyphens (-). userId The user ID. It can be 1 to 64 characters in length and can contain letters, digits, underscores (_), and hyphens (-). Note If a user joins a channel with a user ID that is already used in the channel, the original user with that ID is removed from the channel.nonce A random code. It must start with the `AK-` prefix and can contain letters and digits. The maximum length is 64 bytes. Example: `AK-2b9be4b25c2d38c409c376ffd2372be1`. timestamp The timestamp that indicates when the token expires. You can set the validity period to 12 hours, 24 hours, 3 days, or 7 days. token The token for channel authentication. It is calculated using the following formula: token = sha256(appId + appKey + channelId + userId + nonce + timestamp).gslb The service endpoint. This parameter is an array. Use ["https://rgslb.rtc.aliyuncs.com"]. We recommend that you obtain the endpoint from your business server and pass it to the client SDK. Do not hardcode the endpoint in your client code. - Publish or unpublish a local stream.
- Publish a local stream
- In automatic publishing mode: The SDK automatically publishes the local stream after you join a channel. You do not need to call the publish method again.
- In manual publishing mode: After you join a channel, you can call the following method to publish the local stream.
To change the configuration or stop publishing, reset the configuration parameters and then call the publish method.
// Configure local stream publishing. // true: Allows publishing the screen sharing stream. false: Disallows publishing the screen sharing stream. m_pEngine->configLocalScreenPublish(true); // true: Allows publishing the audio stream. false: Disallows publishing the audio stream. m_pEngine->configLocalAudioPublish(true); // true: Allows publishing the camera stream. false: Disallows publishing the camera stream. m_pEngine->configLocalCameraPublish(true); // true: Allows publishing the secondary video stream. false: Disallows publishing the secondary video stream. // Sub-stream m_pEngine->configLocalSimulcast(true, AliRtcVideoTrack::AliRtcVideoTrackCamera); // Screen sharing streams do not support sub-streams. Therefore, the second parameter can only be AliRtcVideoTrack::AliRtcVideoTrackCamera. // Callback for when publishing is finished. auto onPubResult = [](void *opaque, int errCode) { if (errCode == 0) { // Published successfully. } else { // Failed to publish. } }; m_pEngine->publish(onPubResult, /*UserData, which is passed to the onPubResult callback function*/); - Unpublish a local stream
m_pEngine->configLocalScreenPublish(false); m_pEngine->configLocalAudioPublish(false); m_pEngine->configLocalCameraPublish(false); m_pEngine->configLocalSimulcast(false, AliRtcVideoTrack::AliRtcVideoTrackCamera); // Returns success or failure when unpublishing is complete. auto onPubResult = [](void *opaque, int errCode) { if (errCode == 0) { // Unpublished successfully. } else { // Failed to unpublish. } }; m_pEngine->publish(onPubResult, /*UserData, which is passed to the onPubResult callback function*/);
- Publish a local stream
- Subscribe to or unsubscribe from a remote stream.
- Subscribe to a remote stream
- In automatic subscribing mode: The SDK automatically subscribes to remote streams after you join a channel. You do not need to call the subscribe method again.
- In manual subscribing mode: After you join a channel, you can call the following method to subscribe to the remote stream.
To change the configuration or stop subscribing, reset the configuration parameters and then call the subscribe method.
// true: Allows subscribing to the audio stream. false: Disallows subscribing to the audio stream. m_pEngine->configRemoteAudio(/* remoteUserID */, true); // true: Allows subscribing to the screen sharing stream. false: Disallows subscribing to the screen sharing stream. m_pEngine->configRemoteScreenTrack(/* remoteUserID */, true); // The second parameter is preferMaster. true: Prioritizes subscribing to the main stream. false: Prioritizes subscribing to the secondary stream. // The third parameter is enable. true: Allows subscribing to the camera stream. false: Disallows subscribing to the camera stream. m_pEngine->configRemoteCameraTrack(/* remoteUserID */, true, true); // Callback for when subscribing is finished. auto onSubResult = [](void *opaque, const AliRtc::String &uid, AliRtcVideoTrack vt, AliRtcAudioTrack at) { }; m_pEngine->subscribe(/* remoteUserID */, onSubResult, /*UserData*/);In both automatic and manual modes, after you subscribe to a stream, you can use the subscription callback to perform UI operations or handle logic.
auto onSubResult = [](void *opaque, const AliRtc::String &uid, AliRtcVideoTrack vt, AliRtcAudioTrack at) { ... // Set the window for the video stream. if (vt == AliRtcVideoTrack::AliRtcVideoTrackCamera || vt == AliRtcVideoTrack::AliRtcVideoTrackBoth) { /*Set the remote video preview*/ }You can also use the onRemoteTrackAvailableNotify callback to receive notifications about changes in the stream status of remote users. For example, in manual mode, you can use this callback to check the publishing status of a remote user and then subscribe to the stream or update the UI.
void onRemoteTrackAvailableNotify(const AliRtc::String & uid, AliRtcAudioTrack audioTrack, AliRtcVideoTrack videoTrack) { } - Unsubscribe from a remote stream
m_pEngine->configRemoteAudio(/* remoteUserID */, false); m_pEngine->configRemoteScreenTrack(/* remoteUserID */, false); m_pEngine->configRemoteCameraTrack(/* remoteUserID */, true, false); auto onSubResult = [](void *opaque, const AliRtc::String &uid, AliRtcVideoTrack vt, AliRtcAudioTrack at) { }; m_pEngine->subscribe(/* remoteUserID */, onSubResult, /*UserData*/);
- Subscribe to a remote stream
- Leave the channel.
m_pEngine->leaveChannel();
What to do next
You can download the sample code and run the demo to start a real-time audio and video call. For more information, see Run the Windows demo.