Mac
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 Mac.
You have obtained the token required for channel authentication. For more information, see Use a token for authentication.
Procedure
Initialize the SDK.
Create a DingRtcEngine instance and register callbacks. If you store the DingRtcEngine instance in a ViewController, declare it as a property. For more information about callback interfaces, see Callbacks and listeners.
#import <DingRTC/DingRtcEngine.h> @interface ViewController () <DingRtcEngineDelegate> @property (nonatomic, strong) DingRtcEngine *engine; @endself.engine = [DingRtcEngine sharedInstance:self extras:@""];Preview the local video. After you create the DingRtcEngine instance, you can create a canvas layout to preview the local video.
DingRtcVideoCanvas *canvas = [[DingRtcVideoCanvas alloc] init]; canvas.renderMode = DingRtcRenderModeAuto; canvas.view = view; /* The preview window view. A UIView object for iOS or an NSView object for Mac. */ canvas.mirrorMode = DingRtcRenderMirrorModeOnlyFrontCameraPreviewEnabled; [self.engine setLocalViewConfig:canvas forTrack:DingRtcVideoTrackCamera]; [self.engine startPreview];NoteDingRtcRenderMode provides four rendering modes:
DingRtcRenderModeAuto (Recommended): Auto.
DingRtcRenderModeStretch: Stretches the video to fill the view without maintaining the aspect ratio.
DingRtcRenderModeFill: Scales the video while maintaining the aspect ratio and fills the remaining space with black bars.
DingRtcRenderModeCrop: Scales the video while maintaining the aspect ratio and crops it to fit the view.
You can set the mirror mode for the local or remote view. DingRtcRenderMirrorMode provides three mirror modes:
DingRtcRenderMirrorModeOnlyFrontCameraPreviewEnabled: Mirrors the preview of the front camera only. Other views are not mirrored.
DingRtcRenderMirrorModeAllEnabled: Mirrors all views.
DingRtcRenderMirrorModeAllDisabled: Does not mirror any views.
Optional: Cancel the local preview.
[self.engine stopPreview];Configure publishing and subscribing.
By default, the SDK does not automatically publish audio and video streams after a user joins a channel. To enable automatic publishing, call the following methods before the user joins the channel:
[self.engine publishLocalAudioStream:YES];// Publish the audio stream by default. [self.engine publishLocalVideoStream:YES];// Publish the video stream by default.By default, the SDK automatically subscribes to remote audio and video streams after a user joins a channel. To disable automatic subscription, call the following methods before the user joins the channel:
[self.engine subscribeAllRemoteAudioStreams:NO];// Do not subscribe to the audio stream. [self.engine subscribeAllRemoteVideoStreams:NO];// Do not subscribe to the video stream.
Join a channel.
DingRtcAuthInfo *authinfo = [[DingRtcAuthInfo alloc]init]; authinfo.channelId = /* Your channel ID */; authinfo.appId = /* Your App ID */; authinfo.userId = /* Your user ID */; authinfo.token = /* Your token */; authinfo.gslbServer = /* Your GSLB address */; [self.engine joinChannel:authinfo name:@"userName" onResult:^(NSInteger errCode,NSString * _Nonnull channel,NSInteger elapsed){ // Handle the UI after joining the channel. }];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 must be 1 to 64 characters in length and can contain uppercase letters, lowercase letters, digits, underscores (_), and hyphens (-).
userId
The user ID. It must be 1 to 64 characters in length and can contain uppercase letters, lowercase letters, digits, underscores (_), and hyphens (-).
NoteIf a user logs on from another client with the same user ID, the client that joined the channel first is removed from the channel.
token
The token for channel authentication.
gslbServer
The service endpoint. This parameter can be empty. The default value is
"https://gslb.dingrtc.com". We recommend that you obtain the endpoint from your business server and pass it to the client SDK instead of hardcoding it in the client.Important: We strongly recommend that you do not pass an empty value. Use the GSLB address returned from your business server (AppServer).
Publish or unpublish a local stream.
Publish local audio and video streams
If you did not configure the SDK to automatically publish streams before joining the channel, the local streams are not published. You can call the following methods to publish them manually:
[self.engine publishLocalAudioStream:YES];// Publish the audio stream. [self.engine publishLocalVideoStream:YES];// Publish the video stream.Unpublish local audio and video streams
To unpublish the local audio and video streams, call the following methods:
[self.engine publishLocalAudioStream:NO];// Unpublish the audio stream. [self.engine publishLocalVideoStream:NO];// Unpublish the video stream.
Subscribe to or unsubscribe from a remote stream.
Subscribe to remote audio and video streams
By default, the SDK automatically subscribes to remote audio and video streams. If you disabled automatic subscription, you can call the following methods to subscribe manually:
[self.engine subscribeAllRemoteAudioStreams:YES];// Subscribe to all remote audio streams. [self.engine subscribeAllRemoteVideoStreams:YES];// Subscribe to all remote video streams.After you subscribe, you can render the remote video in the onRemoteTrackAvailableNotify callback:
- (void)onRemoteTrackAvailableNotify:(NSString *_Nonnull)uid audioTrack:(DingRtcAudioTrack)audioTrack videoTrack:(DingRtcVideoTrack)videoTrack { dispatch_async(dispatch_get_main_queue(), ^{ // UI or logic processing. For example, the operation to render the remote video stream is as follows. if(videoTrack == DingRtcVideoTrackCamera) { // camera track DingRtcVideoCanvas *canvas = [[DingRtcVideoCanvas alloc] init]; canvas.renderMode = /* renderMode */; canvas.view = view;/* The rendering view */ [self.engine setRemoteViewConfig:canvas uid:uid forTrack:DingRtcVideoTrackCamera]; } }); }Unsubscribe from remote audio and video streams
To unsubscribe from remote audio and video streams, call the following methods:
[self.engine subscribeAllRemoteAudioStreams:NO];// Unsubscribe from all remote audio streams. [self.engine subscribeAllRemoteVideoStreams:NO];// Unsubscribe from all remote video streams.Subscribe to the audio and video streams of a specific user
After unsubscribing from all streams, you can call the following methods to subscribe to the streams of a specific remote user. To unsubscribe from the streams of this user, set the sub parameter to NO.
[self.engine subscribeRemoteAudioStream:uid sub:YES];// Subscribe to the audio stream of a specific user. [self.engine subscribeRemoteVideoStream:uid track:DingRtcVideoTrackCamera sub:YES];// Subscribe to the video stream of a specific user.Subscribe to a camera stream of a different resolution
To subscribe to a low-definition camera stream, call the following method:
[self.engine setRemoteDefaultVideoStreamType:DingRtcVideoStreamTypeLD];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 future users in the channel, call the following methods:
[self.engine subscribeAllRemoteAudioStreams:NO]; [self.engine subscribeAllRemoteVideoStreams:NO];
Leave the channel.
[self.engine leaveChannel];
What to do next
You can download the sample code and run the demo to quickly start a real-time audio and video call in a channel. For more information, see Run the Mac demo.