Mac

更新时间:
复制 MD 格式

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

Limits

This topic applies to SDK 2.1 and later. If you need the documentation for SDK 1.17, see Implement basic features for Mac.

Procedure

Note The implementation methods in this topic are for reference only. You must develop your application based on your specific requirements.
  1. Initialize the SDK.

    Create an AliRtcEngine instance and register a callback. If you store the AliRtcEngine instance in ViewController, you must declare a property for it. For more information about the callback interfaces, see Callbacks and listeners.

    @interface ViewController () <AliRtcEngineDelegate>
    @property (nonatomic, strong) AliRtcEngine *engine;
    @end
    self.engine = [AliRtcEngine sharedInstance:self extras:@""]; 
    1. Preview the local video. After you create the AliRtcEngine instance, you can create a canvas layout to preview the local video.
      AliVideoCanvas *canvas = [[AliVideoCanvas alloc] init];
      canvas.renderMode = AliRtcRenderModeAuto;
      canvas.view = view; /* The preview window view. This is a UIView object for iOS or an NSView object for macOS. */
      canvas.mirrorMode = AliRtcRenderMirrorModeOnlyFrontCameraPreviewEnabled;
      [self.engine setLocalViewConfig:canvas forTrack:AliRtcVideoTrackCamera];
      [self.engine startPreview];  
      Note
      • AliRtcRenderMode provides four rendering modes:
        • AliRtcRenderModeAuto (Recommended): Automatic.
        • AliRtcRenderModeStretch: Stretches the video to fill the view without preserving the aspect ratio.
        • AliRtcRenderModeFill: Scales the video while preserving the aspect ratio. Black bars are added to fill the view.
        • AliRtcRenderModeCrop: Scales and crops the video to fit the view while preserving the aspect ratio.
      • You can set the mirror mode for the local or remote view. AliRtcRenderMirrorMode provides three mirror modes:
        • AliRtcRenderMirrorModeOnlyFrontCameraPreviewEnabled: Mirrors only the preview of the front camera.
        • AliRtcRenderMirrorModeAllEnabled: Mirrors all views.
        • AliRtcRenderMirrorModeAllDisabled: Disables mirroring for all views.
    2. Optional: Stop the local preview.
      [self.engine stopPreview];  
    3. Configure publishing and subscription settings.
      • By default, the SDK automatically publishes the audio and video streams after a user joins a channel. To disable automatic publishing, configure the following settings before the user joins the channel:
        [self.engine publishLocalAudioStream:NO];// Do not publish the audio stream by default.
        [self.engine publishLocalVideoStream:NO];// Do not 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, configure the following settings before the user joins the channel:
        [self.engine setDefaultSubscribeAllRemoteAudioStreams:NO];// Do not subscribe to audio streams by default.
        [self.engine setDefaultSubscribeAllRemoteVideoStreams:NO];// Do not subscribe to video streams by default.

  2. Join a channel.
    AliRtcAuthInfo *authinfo = [[AliRtcAuthInfo alloc]init];
    authinfo.channelId   = /* Your channelId */;
    authinfo.appId     = /* Your AppId */;
    authinfo.nonce     = /* Your nonce */;
    authinfo.userId   = /* Your userId */;
    authinfo.token     = /* Your token */;
    authinfo.timestamp = /* Your timestamp */;
    authinfo.gslb      = /* Your GSLB endpoint */;
    [self.engine joinChannel:authinfo name:@"userName" onResult:^(NSInteger errCode,NSString * _Nonnull channel,NSInteger elapsed){
         // Handle the UI after joining the channel.
    }];
    ParameterDescription
    appIdThe application ID. You can create and view the application ID on the Application Management page in the console.
    channelIdThe channel ID. It can be 1 to 64 characters in length and can contain letters, digits, underscores (_), and hyphens (-).
    userIdThe 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.
    nonceA random code. It must start with the `AK-` prefix and can contain letters and digits. The maximum length is 64 bytes. Example: `AK-2b9be4b25c2d38c409c376ffd2372be1`.
    timestampThe timestamp that indicates when the token expires. You can set the validity period to 12 hours, 24 hours, 3 days, or 7 days.
    tokenThe token for channel authentication. It is calculated using the following formula: token = sha256(appId + appKey + channelId + userId + nonce + timestamp).
    gslbThe 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.

  3. Publish or unpublish a local stream.
    • Publish local audio and video streams

      By default, local audio and video streams are automatically published after a user joins a channel. If you disabled this feature, you must call the following methods to manually publish the streams:

      [self.engine publishLocalAudioStream:YES];// Publish the audio stream.
      [self.engine publishLocalVideoStream:YES];// Publish the video stream.
    • Publish a low-bitrate stream

      By default, the SDK does not publish a low-bitrate stream. To push a low-bitrate stream, call the following method. You can call this method before or after a user joins a channel.

      [self.engine publishLocalDualStream:YES];
    • 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.

  4. Subscribe to or unsubscribe from a remote stream.
    • Subscribe to remote audio and video streams

      By default, remote audio and video streams are automatically subscribed to after a user joins a channel. If you disabled this feature, you must call the following methods to manually subscribe to the streams:

      [self.engine setDefaultSubscribeAllRemoteAudioStreams:YES];// Subscribe to all remote audio streams.
      [self.engine setDefaultSubscribeAllRemoteVideoStreams:YES];// Subscribe to all remote video streams.
      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 you subscribe to the remote streams, you can render the remote video in the onRemoteTrackAvailableNotify callback:

      - (void)onRemoteTrackAvailableNotify:(NSString *_Nonnull)uid audioTrack:(AliRtcAudioTrack)audioTrack videoTrack:(AliRtcVideoTrack)videoTrack {
          dispatch_async(dispatch_get_main_queue(), ^{
              // UI or logic processing. For example, the following code shows how to render a remote video stream.
              if(videoTrack == AliRtcVideoTrackCamera) {
              // camera track
              AliVideoCanvas *canvas = [[AliVideoCanvas alloc] init];
              canvas.renderMode = /* renderMode */;
              canvas.view = view;/* The rendering view. */
              [self.engine setRemoteViewConfig:canvas uid:uid forTrack:AliRtcVideoTrackCamera];
            }
          });
      }  
    • Unsubscribe from remote audio and video streams

      To unsubscribe from remote audio and video streams, call the following methods:

      [self.engine setDefaultSubscribeAllRemoteAudioStreams:NO];// Unsubscribe from all remote audio streams.
      [self.engine setDefaultSubscribeAllRemoteVideoStreams:NO];// Unsubscribe from all remote video streams.
      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

      After you unsubscribe from all remote audio and video streams, 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 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:AliRtcVideoTrackCamera sub:YES];// Subscribe to the video stream of a specific user.
    • Subscribe to a low-bitrate stream

      To subscribe to a low-bitrate stream, call the following method:

      [self.engine setRemoteDefaultVideoStreamType:AliRtcVideoStreamTypeLow];
      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 audio and video streams of all current and subsequent users in the channel, call the following methods:

      [self.engine subscribeAllRemoteAudioStreams:NO];
      [self.engine subscribeAllRemoteVideoStreams:NO];
      Note
      • Setting these methods to NO has the highest priority. This setting overrides the setDefaultSubscribeAllRemoteAudioStreams and setDefaultSubscribeAllRemoteVideoStreams methods, even if they are set to YES.
      • If these methods are set to YES, the subscription behavior is determined by the setDefaultSubscribeAllRemoteAudioStreams and setDefaultSubscribeAllRemoteVideoStreams methods.

  5. Leave the channel.
    [self.engine leaveChannel]; 

What to do next

You can 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 Mac.