更新时间:2024-07-02 GMT+08:00
分享

SDK使用

  1. 创建引擎并初始化。

    域名不需要设置,由SDK自动获取。appId获取方法请参见创建应用
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    _rtcEngine = [HWRtcEngine sharedEngine];
    HWRtcEngineConfig * cfg = [[HWRtcEngineConfig alloc]init];
    cfg.appId = appid;// appId需在控制台中创建应用后获取
    cfg.domain = domain;// 该字段已废弃,不需要再传值
    cfg.countryCode = rtcCountryCode;// 可以根据Grs国家码对照表传值,建议传"CN"
    cfg.enableHaTrace = YES;
    cfg.logLevel = HWRtcLogLevelDebug;//输出DEBUG级别日志
    cfg.logPath = [NSString stringWithFormat:@"%@/",logFilePath];//日志存储路径
    cfg.enableLog = YES;//开启日志
    cfg.logSize = 10*1024;
    cfg.muteAudioRoute = NO;//远端音频路由
    [_rtcEngine initWithConfig:cfg];
    

  2. 设置本地窗口。

    1
    2
    3
    4
    5
    HWRtcVideoCanvas *canvas = [[HWRtcVideoCanvas alloc] init];    
    canvas.view = [[UIView alloc] initWithFrame:CGRectMacke(0, 0, 90, 160)];//iOS
    //canvas.view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 90, 160)];//macOS
    canvas.uid  = @"HW";    
    [_rtcEngine setupLocalVideo:canvas viewMode:HWRtcVideoDisplayModeFit];
    

  3. 加入房间。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    HWRtcJoinParam *joinParam = [[HWRtcJoinParam alloc]init];
    NSString *authorization = @"";// 鉴权信息
    joinParam.role  = HWRtcRoleJoiner;
    joinParam.userId = @"HW"; // userId用于标识同一房间的不同用户
    joinParam.userName = @"HW";// 用户昵称,如无特殊需求,保持和userId一致即可
    joinParam.scenario = 1;
    joinParam.authorization = authorization;
    joinParam.ctime = time;
    joinParam.roomId = roomid;
    joinParam.autoSubscribeAudio = YES;//是否主动订阅音频
    joinParam.autoSubscribeVideo  = NO ;//默认-关闭
    BOOL result = [self.rtcEngine joinRoom:joinParam ];
    

    joinParam:入会参数,包含用户ID、用户名、房间号、认证信息、ctime、是否自动订阅音频和视频、SFU类型、场景和用户角色,具体请参见HWRtcJoinParam

  4. 监听远端用户加入房间,并设置远端窗口。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    -(void)onRemoteUserOnline:(NSString*)roomId userId:(NSString*)userId userName:(NSString*)userName{
        if([userId isEqualToString:localUid]){
            return;
        }
             dispatch_async(dispatch_get_main_queue(),
                              {         
                 UIView *videoView = [[UIVIew alloc] initWithFrame:CGRectMake(0, 0, 90, 160)];//iOS
                 //NSView *videoView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 90, 160)];//macOS
                 [self.view addSubview:videoView];
                  HWRtcVideoCanvas *canvas = [[HWRtcVideoCanvas alloc] init];    
                  canvas.uid  = userId;  
                  canvas.view = videoView;
                  int result = [self.rtcEngine startRemoteStreamView:canvas  treamType:self.streamType];
    
                if (result == 0) {
                     [self.rtcEngine updateRemoteRenderMode:userId displayMode:HWRtcVideoDisplayModeFit mirrorMode:HWRtcVideoMirrorTypeDisable ];
                 }
                 [self.viewsArray addObject:canvas];
    
            });
        }
    

  5. 监听远端用户离开房间,并删除远端窗口。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    - (void)onRemoteUserOffline:(NSString *)roomId userId:(NSString *)userId reason:(NSInteger)reason
    {
        dispatch_async(dispatch_get_main_queue(),
        ^{
            for (HWRtcVideoCanvas * canvas in self.viewsArray) {
                if ([userId isEqualToString:canvas.uid]) {
                 [self.viewsArray removeObject:canvas];
                }
            }
        });
    }
    

  6. 离开房间。

    [_rtcEngine leaveRoom];

相关文档