Real-Time Communication (RTC) supports features such as SDK initialization, joining channels, local media track publishing, remote media track subscription, and leaving channels. This topic describes the basic features of RTC.
Prerequisites
- You have downloaded and integrated the latest version of the SDK. For more information, see Integrate the SDK for iOS.
- The authentication token for joining the channel is obtained. For more information, see Use tokens for authentication.
Procedure
- Initialize the SDK.
Create an AliRtcEngine instance and register AliRtcEngineDelegate to listen for callbacks. If you maintain an AliRtcEngine instance in ViewController, you must declare the property.
@interface ViewController () <AliRtcEngineDelegate> @property (nonatomic, strong) AliRtcEngine *engine; @endFor more information about callbacks, see Callbacks and listeners.
self.engine = [AliRtcEngine sharedInstance:self extras:@""];Note Multiple instances are not supported.- Preview the local video. After you create an AliRtcEngine instance, create a canvas layout to preview the local video.
AliVideoCanvas *canvas = [[AliVideoCanvas alloc] init]; canvas.renderMode = AliRtcRenderModeAuto; canvas.view = (AliRenderView *)view; /* The preview window view. */ canvas.mirrorMode = AliRtcRenderMirrorModeOnlyFrontCameraPreviewEnabled; [self.engine setLocalViewConfig:canvas forTrack:AliRtcVideoTrackCamera]; [self.engine startPreview];Note- AliRtcRenderMode provides four rendering modes.
- (Recommended) AliRtcRenderModeAuto: The auto mode.
- AliRtcRenderModeStretch: Stretches the video to fill the view without preserving the aspect ratio.
- AliRtcRenderModeFill: Scales the video to fit the view while preserving the aspect ratio. Black bars are added to fill the remaining space.
- AliRtcRenderModeCrop: Scales the video while preserving the aspect ratio and crops the video to fit the view.
- The view must be AliRenderView or its child class.
- You can set the mirror mode for the local or remote view using AliRtcRenderMirrorMode. Three mirror modes are available.
- AliRtcRenderMirrorModeOnlyFrontCameraPreviewEnabled: Only the preview of the front camera is mirrored.
- AliRtcRenderMirrorModeAllEnabled: All views are mirrored.
- AliRtcRenderMirrorModeAllDisabled: No views are mirrored.
You can also stop the local preview.
[self.engine stopPreview]; - AliRtcRenderMode provides four rendering modes.
- Set the publishing and subscription modes.Note By default, streams are automatically published and subscribed to. You can also configure the SDK to manually publish and subscribe to streams.
- Auto-publish mode: If you enable auto-publish mode, the SDK automatically publishes the local audio and video stream after you join a channel. If you disable auto-publish mode, you must call the publish method to publish the stream.
- Auto-subscribe mode: If you enable auto-subscribe mode, the SDK automatically subscribes to the audio and video streams of other users after you join the channel. If you disable auto-subscribe mode, you must call the subscribe method to subscribe to their streams.
/* Set the auto-publish and auto-subscribe modes. This must be configured before you join a channel. autoPublish: Specifies whether to enable auto-publish mode. Valid values: YES and NO. autoSubscribe: Specifies whether to enable auto-subscribe mode. Valid values: YES and NO. */ [self.engine setAutoPublish:YES withAutoSubscribe:YES];
- Preview the local video. After you create an AliRtcEngine instance, create a canvas layout to preview the local video.
- Join a channel.
AliRtcAuthInfo *authinfo = [[AliRtcAuthInfo alloc]init]; authinfo.channel = /* Your channelId. */; authinfo.appid = /* Your Appid. */; authinfo.nonce = /* Your nonce. */; authinfo.user_id = /* Your userId. */; authinfo.token = /* Your token. */; authinfo.timestamp = /* Your timestamp. */; authinfo.gslb = /* Your GSLB endpoint. */; [self.engine joinChannel:authinfo name:/* userName */ onResult:^(NSInteger errCode){ // Handle the UI after joining the channel. }];Parameter Description appId The application ID. You can create and view the application IDs on the Applications page in the RTC console. channelId The channel ID. The ID must be 1 to 64 characters in length and can contain letters, digits, underscores (_), and hyphens (-). userId The user ID. The ID must be 1 to 64 characters in length and can contain letters, digits, underscores (_), and hyphens (-). Note If the user attempts to join a meeting by using two devices at the same time, the first device is dropped from the meeting when the second device joins the meeting.nonce A random string. Nonces start with AK- and consist of letters and digits. Nonces can contain up to 64 characters. Example: AK-2b9be4b25c2d38c409c376ffd2372be1. timestamp The timestamp when the token expires. You can select 12 hours, 24 hours, 3 days, or 7 days. token The authentication token for joining the channel. Calculation formula: Token = sha256(appId + appKey + channelId + userId + nonce + timestamp).gslb The GSLB address. This parameter is an array. We recommend that you send the address ["https://rgslb.rtc.aliyuncs.com"]to the client SDK through the application server instead of including the address in the client code. - Publish or unpublish the local stream.
- Publish the local stream
- In auto-publish mode: After you join a channel, the local stream is automatically published. You do not need to call the publish method.
- In manual publish mode: After you join a channel, you can call the following method to publish the local stream.
To change the stream configuration during the publishing process, you can reset the configuration parameters and then call the publish method again.
// YES indicates that the audio stream can be published. NO indicates that the audio stream cannot be published. [self.engine configLocalAudioPublish:YES]; // YES indicates that the camera stream can be published. NO indicates that the camera stream cannot be published. [self.engine configLocalCameraPublish:YES]; // YES indicates that the secondary video stream can be published. NO indicates that the secondary video stream cannot be published. [self.engine configLocalSimulcast:YES forTrack:AliRtcVideoTrackCamera]; [self.engine publish:^(int err) { }]; - Unpublish the local stream
[self.engine configLocalAudioPublish:NO]; [self.engine configLocalCameraPublish:NO]; [self.engine configLocalSimulcast:NO forTrack:AliRtcVideoTrackCamera]; [self.engine publish:^(int err) { }];
- Publish the local stream
- Subscribe to or unsubscribe from a remote stream.
- Subscribe to a remote stream
- In auto-subscribe mode: After you join a channel, remote streams are automatically subscribed to. You do not need to call the subscribe method.
- In manual subscribe mode: After you join a channel, you can call the following method to subscribe to a remote stream.
To change the configuration or cancel the subscription process, reset the configuration parameters and call the subscribe method.
// YES indicates that the audio stream can be subscribed to. NO indicates that the audio stream cannot be subscribed to. [self.engine configRemoteAudio:/* remoteUserID */ enable:YES]; // YES indicates that the screen stream can be subscribed to. NO indicates that the screen stream cannot be subscribed to. [self.engine configRemoteScreenTrack:/* remoteUserID */ enable:YES]; // The second parameter: YES indicates that the master stream is preferentially pulled. // The third parameter: YES indicates that the camera stream can be subscribed to. NO indicates that the camera stream cannot be subscribed to. [self.engine configRemoteCameraTrack:/* remoteUserID */ preferMaster:YES enable:YES]; [self.engine subscribe:/* remoteUserID */ onResult:^(NSString *uid, AliRtcVideoTrack vt, AliRtcAudioTrack at) { }];In both auto-subscribe and manual subscribe modes, you receive a subscription callback from the delegate after you subscribe to a stream. You can then perform UI operations or logical processing.
- (void)onSubscribeChangedNotify:(NSString *)uid audioTrack:(AliRtcAudioTrack)audioTrack videoTrack:(AliRtcVideoTrack)videoTrack { dispatch_async(dispatch_get_main_queue(), ^{ // Perform UI or logical processing. For example, render the remote video stream as follows. if(videoTrack & AliRtcVideoTrackCamera) { // camera track AliVideoCanvas *canvas = [[AliVideoCanvas alloc] init]; canvas.renderMode = /* renderMode */; canvas.view = (AliRenderView *)view;/* The rendering view. */ [self.engine setRemoteViewConfig:canvas uid:uid forTrack:AliRtcVideoTrackCamera]; } }); }You can use the following delegate callback to listen for changes to a remote user's stream status. For example, in manual mode, when this callback is triggered, you can retrieve the remote user's publishing status and then subscribe to the stream or update the UI.
- (void)onRemoteTrackAvailableNotify:(NSString *)uid audioTrack:(AliRtcAudioTrack)audioTrack videoTrack:(AliRtcVideoTrack)videoTrack { } - Unsubscribe from a remote stream
[self.engine configRemoteAudio:/* remoteUserID */ enable:NO]; [self.engine configRemoteScreenTrack:/* remoteUserID */ enable:NO]; [self.engine configRemoteCameraTrack:/* remoteUserID */ preferMaster:YES enable:NO]; [self.engine subscribe:/* remoteUserID */ onResult:^(NSString *uid, AliRtcVideoTrack vt, AliRtcAudioTrack at) { }];
- Subscribe to a remote stream
- 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 other users in a channel. For more information, see Run the iOS demo.