iOS
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 iOS.
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 a callback. If you store the DingRtcEngine instance in ViewController, declare it as a property. For more information about the 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 stream.
DingRtcVideoCanvas *canvas = [[DingRtcVideoCanvas alloc] init]; canvas.renderMode = DingRtcRenderModeAuto; canvas.view = view; /* The preview window view. This is a UIView object for iOS or an NSView object for macOS. */ canvas.mirrorMode = DingRtcRenderMirrorModeOnlyFrontCameraPreviewEnabled; [self.engine setLocalViewConfig:canvas forTrack:DingRtcVideoTrackCamera]; [self.engine startPreview];NoteDingRtcRenderMode provides four rendering modes:
DingRtcRenderModeAuto (Recommended): Automatically adjusts the rendering mode.
DingRtcRenderModeStretch: Stretches the video to fill the view without maintaining the aspect ratio.
DingRtcRenderModeFill: Scales the video while maintaining the aspect ratio. Black bars may be added to fill the view.
DingRtcRenderModeCrop: Scales and crops the video to fit the view while maintaining the aspect ratio.
You can set the mirror mode for local or remote views. DingRtcRenderMirrorMode provides the following three modes:
DingRtcRenderMirrorModeOnlyFrontCameraPreviewEnabled: Mirrors only the front camera preview.
DingRtcRenderMirrorModeAllEnabled: Mirrors all views.
DingRtcRenderMirrorModeAllDisabled: Disables mirroring for all views.
Optional: You can stop 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 audio streams. [self.engine subscribeAllRemoteVideoStreams:NO];// Do not subscribe to video streams.
You can 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 do not configure the SDK to publish audio and video streams before joining a channel, the local streams are not published automatically. You must call the following methods to manually publish them:
[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 streams after a user joins a channel. If you disabled automatic subscription before joining, call the following methods to manually subscribe:
[self.engine subscribeAllRemoteAudioStreams:YES];// Subscribe to all remote audio streams. [self.engine subscribeAllRemoteVideoStreams:YES];// Subscribe to all remote video streams.After you subscribe to the streams, 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(), ^{ // Handle the UI or logic. For example, render the remote video stream 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]; } }); }Unsubscribing 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 streams of a specific user
After you unsubscribe from all audio and video streams, you can call the following methods to subscribe to the streams of a specific remote user. To unsubscribe from the streams of the remote 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 camera streams of different resolutions
To subscribe to the 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 make real-time audio and video calls with others in a channel. For more information, see Run the iOS demo.