Windows
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 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.
Limits
This topic applies only to SDK versions 2.1 and later. For the documentation corresponding to SDK version 1.17, see Implement basic features on Windows.
Procedure
- Initialize the SDK.
Create an AliEngine instance and register callbacks. For more information about the callback interfaces, see AliEngineEventListener.
mpEngine = AliEngine::Create(pConfig); // pConfig specifies the SDK initialization configurations. For the current version, use an empty string. mpEngine->SetEngineEventListener(pListener); // pListener is the AliRtcEventListener object that you implement to receive SDK callbacks.- Preview the local video. After you create the AliEngine instance, you can create a canvas layout to preview the local video.
AliEngineVideoCanvas canvas; canvas.displayView = (void*)GetSafeHwnd(); canvas.renderMode = AliEngineRenderMode::AliEngineRenderModeAuto; mpEngine->SetLocalViewConfig(canvas, AliEngineVideoTrackCamera); mpEngine->StartPreview();Note- AliEngineRenderMode provides five rendering modes:
- AliEngineRenderModeAuto (Recommended): Automatically adjusts the rendering mode.
- AliEngineRenderModeStretch: Stretches the video to fill the view without preserving the aspect ratio.
- AliEngineRenderModeFill: Scales the video while preserving the aspect ratio. Black bars are added to fill the view.
- AliEngineRenderModeCrop: Scales the video while preserving the aspect ratio and crops it to fit the view.
- AliEngineRenderModeScroll: Scrolls the view to show more content.
- You can set the mirror mode for local or remote views using AliEngineRenderMirrorMode. Three mirror modes are available:
- AliEngineRenderMirrorModeOnlyFrontMirror: Mirrors only the preview of the front camera.
- AliEngineRenderMirrorModeAllMirror: Mirrors all views.
- AliEngineRenderMirrorModeAllNoMirror: Does not mirror any views.
- AliEngineRenderMode provides five rendering modes:
- Optional:Stop the local preview.
engine.stopPreview(); - Configure publishing and subscription settings.
- By default, the SDK automatically publishes audio and video streams after a user joins a channel. If you do not want to automatically publish audio and video streams, call the following methods before the user joins the channel:
mpEngine->PublishLocalAudioStream(false); mpEngine->PublishLocalVideoStream(false); - By default, the SDK automatically subscribes to remote audio and video streams after a user joins a channel. If you do not want to automatically subscribe to audio and video streams, call the following methods before the user joins the channel:
mpEngine->SetDefaultSubscribeAllRemoteAudioStreams(false); mpEngine->SetDefaultSubscribeAllRemoteVideoStreams(false);
- By default, the SDK automatically publishes audio and video streams after a user joins a channel. If you do not want to automatically publish audio and video streams, call the following methods before the user joins the channel:
- Preview the local video. After you create the AliEngine instance, you can create a canvas layout to preview the local video.
- Join a channel.
AliEngineAuthInfo authinfo; authinfo.channelId = /* The channel ID. */; authinfo.appId = /* The application ID. */; authinfo.token = /* The token for channel authentication. */; authinfo.nonce = /* The nonce. */; authinfo.userId = /* The user ID. */; authinfo.timestamp = /* The timestamp. */; authinfo.gslbCount = 1; /* The number of GSLB endpoints. */; authinfo.gslb = new char*[authinfo.gslbCount]; /* The array of GSLB endpoints. */; for (int i = 0; i < authinfo.gslbCount; i++) { authinfo.gslb[i] = "https://rgslb.rtc.aliyuncs.com"; } authinfo.agentCount = 1;/* The number of AGENT endpoints. */; authinfo.agent = new char*[authinfo.agentCount];/* The array of AGENT endpoints. */; for (int i = 0; i < authinfo.agentCount; i++) { authinfo.agent[i] = "example.com"; } m_pEngine->joinChannel(authinfo,userName /* The display name. */);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 local streams.
- Publish local audio and video streams
By default, the SDK automatically publishes local audio and video streams after you join a channel. If you disabled automatic publishing before joining the channel, you must call the following methods to manually publish the streams:
mpEngine->PublishLocalAudioStream(true); mpEngine->PublishLocalVideoStream(true); - Publish a low-resolution stream
By default, the SDK does not publish a low-resolution stream. To push a low-resolution stream, call the following method. You can call this method before or after joining a channel.
mpEngine->PublishLocalDualStream(true); - Unpublish local audio and video streams
To unpublish local audio and video streams, call the following methods:
mpEngine->PublishLocalAudioStream(false); mpEngine->PublishLocalVideoStream(false);
- Publish local audio and video streams
- Subscribe to or unsubscribe from remote streams.
- Subscribe to remote audio and video streams
By default, the SDK automatically subscribes to remote audio and video streams after you join a channel. If you disabled automatic subscription before joining the channel, you must call the following methods to manually subscribe to the streams:
mpEngine->SetDefaultSubscribeAllRemoteAudioStreams(true); mpEngine->SetDefaultSubscribeAllRemoteVideoStreams(true);Note These methods apply only to remote users who join the channel after the methods are called. They do not affect remote users who are already in the channel.After the subscription is successful, you can render the remote video in the OnRemoteTrackAvailableNotify callback:
AliEngineVideoCanvas canvas; canvas.displayView = (void*)view->GetSafeHwnd(); canvas.mirrorMode = view->mCameraMirror ? AliEngineRenderMirrorModeAllMirror : AliEngineRenderMirrorModeAllNoMirror; mpEngine->SetRemoteViewConfig(canvas, uid.c_str(), AliEngineVideoTrackCamera); - Unsubscribe from remote audio and video streams
To unsubscribe from remote audio and video streams, call the following methods:
mpEngine->SetDefaultSubscribeAllRemoteAudioStreams(false); mpEngine->SetDefaultSubscribeAllRemoteVideoStreams(false);Note These methods apply only to remote users who join the channel after the methods are called. They do not affect remote users who are already in the channel. - Subscribe to the audio and video streams of a specific user
You can call the following methods to subscribe to the audio and video streams of a specific remote user. To unsubscribe from the streams of this remote user, set the `sub` parameter to `false`.
mpEngine->SubscribeRemoteAudioStream(uid.c_str(), true); mpEngine->SubscribeRemoteVideoStream(uid.c_str(), AliEngineVideoTrackCamera, true); - Subscribe to a low-resolution stream
To subscribe to a low-resolution stream, call the following method:
mpEngine->SetRemoteDefaultVideoStreamType(AliEngineVideoStreamTypeLow);Note These methods apply only to remote users who join the channel after the methods are called. They do not affect remote users who are already in the channel. - Unsubscribe from the audio and video streams of all remote users
To unsubscribe from the streams of all current and subsequent users in the channel, call the following methods:
mpEngine->SubscribeAllRemoteVideoStreams(false); mpEngine->SubscribeAllRemoteAudioStreams(false);Note- If these methods are set to `false`, they have the highest priority. The `SetDefaultSubscribeAllRemoteAudioStreams` and `SetDefaultSubscribeAllRemoteVideoStreams` methods do not take effect, even if they are set to `true`.
- If these methods are set to `true`, the subscription is determined by the settings of `SetDefaultSubscribeAllRemoteAudioStreams` and `SetDefaultSubscribeAllRemoteVideoStreams`.
- Subscribe to remote audio and video streams
- Leave the channel.
m_pEngine->LeaveChannel();
What to do next
Download the sample code and run the demo to start a real-time audio and video call with other users in a channel. For more information, see Run the demo for Windows.