Use iOS SDK

Updated at:

This topic describes how to use the API of the iOS Software Development Kit (SDK).

Permissions and privacy

  • Add camera permission. You must add the following code to your app's Info.plist file:

    <key>NSCameraUsageDescription</key>
    <string>Camera access needed for video calling</string>
  • Add microphone permission. You must add the following code to your app's Info.plist file:

    <key>NSMicrophoneUsageDescription</key>
    <string>Microphone access needed for video calling</string>
    Important

    The call feature requires microphone permission. Before entering the call interface, check whether your app has microphone permission. If it does, proceed to the call interface. Otherwise, guide the user to grant the permission.

    To check for microphone permission, refer to the AVCaptureDevice class. The following code is an example:

    [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
            if (granted) {
       
            }
        }];

Initialize ARTVCEngine and listen on callback events

  • Initialize the UID and Delegate.

    _artvcEgnine = [[ARTVCEngine alloc] init];
    _artvcEgnine.uid = self.uid;
    _artvcEgnine.delegate = self;
  • Listen on callback events (implement the callback based on requirements, for example, implement error processing).

    #pragma mark - ARTVCEngineDelegate
    -(void)didReceiveRoomInfo:(ARTVCRoomInfomation*)roomInfo{
    }
    -(void)didReceiveLocalFeed:(ARTVCFeed*)localFeed{
      self.feedForPreview = localFeed;
    }
    -(void)didEncounterError:(NSError *)error forFeed:(ARTVCFeed*)feed{
      [self showToastWith:[NSString stringWithFormat:@"%@, Error:%@",feed,error] duration:2.0];
    }
    .....

Set the video encoding resolution

The code for setting the video encoding resolution is as follows:

// Set the video encoding resolution. The default value is ARTVCVideoProfileType_640x360_15Fps.
artvcEgnine.videoProfileType = ARTVCVideoProfileType_640x360_15Fps;

Set the manual/auto stream pulling or ingestion switch

The code for setting the manual/auto stream pulling or ingestion switch is as follows:

_artvcEgnine.autoPublish = YES;
_artvcEgnine.autoSubScribe = YES;

Set up a video call or an audio-only call

  • Audio and video calls

    ARTVCPublishConfig* config = [[ARTVCPublishConfig alloc] init];
    config.videoEnable = YES;// The default value is YES.
    config.audioEnable = YES;// The default value is YES.
    config.videoProfile = _artvcEgnine.videoProfileType;
    _artvcEgnine.autoPublishConfig = config;
    ARTVCSubscribeOptions* options = [[ARTVCSubscribeOptions alloc] init];
    _artvcEgnine.autoSubscribeOptions = options;
  • Audio call

    ARTVCPublishConfig* config = [[ARTVCPublishConfig alloc] init];
    config.videoEnable = NO;
    config.audioEnable = YES;// The default value is YES.
    _artvcEgnine.autoPublishConfig = config;
    ARTVCSubscribeOptions* options = [[ARTVCSubscribeOptions alloc] init];
    options.receiveVideo = NO;
    _artvcEgnine.autoSubscribeOptions = options;

Enable camera preview in an audio and video call

Note

If this is an audio-only call, skip this step.

// By default, the front camera is used. If this parameter is set to YES, the rear camera is used.
[_artvcEgnine startCameraPreviewUsingBackCamera:NO];
  • After you start the camera preview, if a callback for the local feed has not yet been received, a subsequent callback returns an ARTVCFeed object. You can use this object to associate the rendering view that is returned later.

    -(void)didReceiveLocalFeed:(ARTVCFeed*)localFeed forPublishConfig:(ARTVCPublishConfig*)publishConfig{
    self.feedForPreview = localFeed;
    }
  • Callback of the view preview initialization.

    -(void)didVideoRenderViewInitialized:(UIView*)renderView forFeed:(ARTVCFeed*)feed{
    if([feed isEqual:self.feedForPreview]){
    [self showToastWith:@"video preview view created" duration:1.0];
    }else{
    self.feedForRemote = feed;
    };
    // Trigger the UI layout. Add renderView to the view layer.
    }
  • Callback of the first frame rendering preview.

    -(void)didFirstVideoFrameRendered:(UIView*)renderView forFeed:(ARTVCFeed*)feed{
    }

Create or join a room

  1. Create a room as the call initiator.

    ARTVCCreateRoomParams* params = [[ARTVCCreateRoomParams alloc] init];
     params.uid = self.uid;
     params.bizName = DEMO_BIZ;
     params.subBiz = DEMO_SUBBIZ;
     params.signature = DEMO_SIGNATURE;
     [_artvcEgnine createRoom:params];
    • The callback result of the room information is returned if a room is successfully created.

      -(void)didReceiveRoomInfo:(ARTVCRoomInfomation*)roomInfo{
      }
    • An error callback result is returned if a room failed to be created.

      //error.code == ARTVCErrorCodeProtocolErrorCreateRoomFailed
      -(void)didEncounterError:(NSError *)error forFeed:(ARTVCFeed*)feed{
      }
    • The callback result about other people joining the room is returned after other people joined the room.

      -(void)didParticepantsEntered:(NSArray<ARTVCParticipantInfo*>*)participants{
      }
  2. Join the room as the main participant.

    ARTVCJoinRoomParams* params = [[ARTVCJoinRoomParams alloc] init];
     params.uid = self.uid;
     params.bizName = DEMO_BIZ;
     params.subBiz = DEMO_SUBBIZ;
     params.roomId = self.roomId;
     params.signature = DEMO_SIGNATURE;
     params.rtoken = self.rtoken;
     [_artvcEgnine joinRoom:params];
    • The callback result about successfully joining the room and the number of existing members in the room is returned if the participant successfully joined the room.

      -(void)didJoinroomSuccess{
      }
      -(void)didParticepantsEntered:(NSArray<ARTVCParticipantInfo*>*)participants{
      }
    • If you fail to join the channel, an error callback is returned.

      //error.code == ARTVCErrorCodeProtocolErrorJoinRoomFailed
      -(void)didEncounterError:(NSError *)error forFeed:(ARTVCFeed*)feed{
      }
    • The callback result about successfully joining the room is returned if other people join the room later.

      -(void)didParticepantsEntered:(NSArray<ARTVCParticipantInfo*>*)participants{
      }

Start stream ingestion or pulling after creating or joining a room

By default, the automatic stream ingestion and pulling is enabled.

  • During stream ingestion or pulling, callback results about relevant status are returned.

    -(void)didConnectionStatusChangedTo:(ARTVCConnectionStatus)status forFeed:(ARTVCFeed*)feed{
      [self showToastWith:[NSString stringWithFormat:@"connection status:%d\nfeed:%@",status,feed] duration:1.0];
      if((status == ARTVCConnectionStatusClosed)  && [feed.uid isEqualToString:[self uid]]){
          [self.artvcEgnine stopCameraPreview];// For a video call, stop the camera preview.
          [self.artvcEgnine leaveRoom];
      }
    }
  • ARTVCConnectionStatus explanation

    Enumeration

    Value

    Description

    ARTVCConnectionStatusConnecting

    200

    This status is returned first when starting publishing and subscription.

    ARTVCConnectionStatusConnected

    202

    This status is returned when the publishing or subscription is successful.

    ARTVCConnectionStatusDisConnected

    203

    A connection is interrupted suddenly. The underlying media stream is disconnected and a re-connection will be performed automatically. When the callback result is returned to the business, the business will notify the user and will not force the user to leave the room.

    ARTVCConnectionStatusFailed

    204

    The underlying ICE failed and cannot continue to work, which is a final error. When the business receives the callback result, it can stop the camera and force the user to leave the room.

    ARTVCConnectionStatusClosed

    206

    This status is returned each time publishing and subscribing ends. This is the final status. When your service receives this callback, you can stop the camera and process a leaveRoom request.

  • After the stream ingestion is successful, other room members will receive callback results of new feeds.

    -(void)didNewFeedAdded:(ARTVCFeed*)feed{
      [self showToastWith:[NSString stringWithFormat:@"new feed published by others:%@",feed] duration:2.0];
    }
  • In the automatic subscription mode, the system will actively subscribe to this feed.

  • After the subscription is successful, other members in the room will get the following callback result:

    -(void)didSubscriber:(NSString*)subscriber subscribedAFeed:(ARTVCFeed*)feed{
      [self showToastWith:[NSString stringWithFormat:@"subscriber subscribed :%@",feed] duration:2.0];
    }

End a call

  • After an audio and video call ends, stop the camera and leave the room.

    [_artvcEgnine stopCameraPreview];
    [_artvcEgnine leaveRoom];
  • For an audio call, leave the room.

    [_artvcEgnine leaveRoom];
  • After a member leaves a room, other members in the room receive a callback notification.

    -(void)didParticepant:(ARTVCParticipantInfo*)participant leaveRoomWithReason:(ARTVCParticipantLeaveRoomReasonType)reason{
      [self showToastWith:[NSString stringWithFormat:@"participant left:%@ reason:%d",participant,reason] duration:2.0];
    }
  • When you leave a room in the automatic publishing/subscription mode, local streams are automatically unpublished and previous subscribed streams are unsubscribed from.

  • After you unpublish local streams, other members of the room will receive the corresponding callback result.

    -(void)didFeedRemoved:(ARTVCFeed*)feed{
      [self showToastWith:[NSString stringWithFormat:@"feed unpublished by others:%@",feed] duration:2.0];
    }
  • After you cancel your subscription, other members of the room will receive the corresponding callback result.

    -(void)didSubscriber:(NSString*)subscriber unsubscribedAFeed:(ARTVCFeed*)feed{
      [self showToastWith:[NSString stringWithFormat:@"subscriber unsubscribed :%@",feed] duration:2.0];
    }