API参考

优质
小牛编辑
117浏览
2023-12-01

目录

  1. 互动直播方法介绍 MRTCInteractiveClient

  2. 代理方法介绍 MRTCInteractiveClientRoomDelegate

  3. 设置 MRTCSetting

  4. 错误信息 MRTCError

互动直播方法介绍 MRTCInteractiveClient

/*
作用:初始化互动直播类
host:主机地址
setting:本地推流设置
renderer:渲染本地推流画面的view
*/
- (instancetype)initWithHost:(NSString *)host setting:(MRTCSetting *)setting renderer:(UIView *)renderer;

/*
作用:加入互动直播房间
channel:频道id
role:加入房间作为的角色,主持人为main,嘉宾为1、2、3...等数字编号
password:房间密码,主持人和嘉宾的密码不同
delegate:MRTCInteractiveClientRoomDelegate,会有加入房间后嘉宾加入,嘉宾离开,错误等回调
*/
- (void)joinWithChannel:(NSString *)channel role:(NSString *)role password:(NSString *)password delegate:(id<MRTCInteractiveClientRoomDelegate>)delegate;

/*
作用:离开房间,销毁资源
*/
- (void)leave;

/*
作用:播放指定角色的流
role:角色名,例如:main、1、2、3等
view:用于渲染该角色流的view
*/
- (void)playClientWithRole:(NSString *)role renderView:(UIView *)view;

/*
作用:停止播放指定角色的流
role:角色名,例如:main、1、2、3等
*/
- (void)stopClientWithRole:(NSString *)role;

/*
作用:开关本地视频推流,关闭后房间其他人将看不到您的画面
enable: YES:开 NO:关
*/
- (void)enableLocalVideo:(BOOL)enable;

/*
作用:开关本地音频推流,关闭后房间其他人将看不到您的声音
作用:开关本地音频推流,
enable: YES:开 NO:关
*/
- (void)enableLocalAudio:(BOOL)enable;

/*
作用:开关远端音频推流,关闭后房间其他人的声音将听不到
作用:开关本地音频推流,
enable: YES:开 NO:关
*/
- (void)enableRemoteAudio:(BOOL)enable;

/*
作用:切换本地摄像头
*/
- (void)switchCamera;

/*
作用获取本地推流实时信息
视频编码器 key:videoCodec
视频实时码率 key:videoBitrate
视频实时丢包率 key:videoPacketsLostRate

音频编码器 key:audioCodec
音频实时码率 key:audioBitrate
音频实时丢包率 key:audioPacketsLostRate
*/
- (void)getHostStats:(void(^)(NSDictionary *stats))completion;

代理方法介绍 MRTCInteractiveClientRoomDelegate

/*
作用:互动直播状态改变时会调用,手动调用leave方法不会触发此回调
client:互动直播实例
state:新状态
*/
- (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client stateChanged:(MRTCInteractiveClientState)state;

/*
作用:当调用加入房间接口之后,如果加入房间成功就会回调此方法
client:互动直播实例
roleArray:已经在房间中的其他角色,您可以在回调中遍历此数组调用播放指定角色接口
*/
- (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client joinSuccess:(NSArray<NSString *> *)roleArray;

/*
作用:当调用加入房间接口之后,如果加入房间失败就会回调此方法
client:互动直播实例
error:错误信息
*/
- (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client joinFailed:(MRTCError *)error;

/*
作用:加入房间成功后,如果有新的角色加入房间将会回调此方法
client:互动直播实例
roleArray:新加入的角色数组,您可以在回调中遍历此数组调用播放指定角色接口
*/
- (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client onJoinRoles:(NSArray<NSString *> *)roleArray;

/*
作用:加入房间成功后,如果有角色离开房间将会回调此方法
client:互动直播实例
roleArray:离开的角色数组,您可以在回调用遍历此数组移除对应的图渲染视
*/
- (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client onLeaveRoles:(NSArray<NSString *> *)roleArray;

/*
作用:加入房间成功后,播放其他角色成功时将会回调此方法
client:互动直播实例
role:播放的角色
*/
- (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client playRoleSuccess:(NSString *)role;

/*
作用:加入房间成功后,播放其他角色失败时将会回调此方法
client:互动直播实例
role:播放的角色
error:错误信息
*/
- (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client playRole:(NSString *)role failure:(MRTCError *)error;

/*
作用:加入房间成功后的错误回调
client:互动直播实例
error:错误信息
*/
- (void)MRTCInteractiveClient:(MRTCInteractiveClient *)client onError:(MRTCError *)error;

设置 MRTCSetting

您可以使用MRTCSetting配置推流参数,每一个参数都有默认值,也可以从MRTCSetting中获取机器支持的配置。

  • 默认配置
    在需要使用推流器的ViewController中引用头文件#import <MRTC/MRTCSetting.h>,示例代码如下:

      MRTCSetting *setting = [[MRTCSetting alloc]init];
    
  • 自定义配置

    • 获取支持的编解码器

        NSArray<MRTCCodec *> *supportCodecs = [MRTCSetting supportVideoCodec];
      
    • 获取支持的分辨率(注:分辨率格式为 宽x高)

        NSArray<NSString *> *supportResolutions = [MRTCSetting supportVideoResolutions];
      
    • 获取支持的fps范围

        MRTCFpsRange *fpsRange = [MRTCSetting supportFPSRange];
        NSUInteger min = fpsRange.minFps;//支持的最小fps
        NSUInteger max = fpsRange.maxFps;//支持的最大fps
      
    • 构造自定义配置

        MRTCSetting *customSetting = [[MRTCSetting alloc]initWithVideoCodec:supportCodecs.firstObject videoResolution:@"1280x720" maxBitRate:1800 fps:25 useFrontCamera:YES];
      

设置为自定配置时,您需要自己定义偏好编解码器、视频分辨率、最大码率、帧率、前后摄像头。

自定义配置建议参数:

分辨率帧率码率
360p25350
480p25600
540p251000
720p251500
360p30400
480p30750
540p301200
720p301800

注:码率估算公式:(0.0000782*视频宽*视频高*帧率)

错误信息 MRTCError

错误码错误含义
1600Create new client error
1601Client Not exist
1602Stream already exist
1603Stream not exist
1604Stream server unavailable
1605Get Mrtc server error
1606Play failed
1607Set play answer failed
1608Create stream error
1609Remote create offer error
1610Set configuration error
1611Json error
1612Send message error
1613Close PeerConnection error
1614Set candidate error
1615Keepalive Timeout
1616Stream have been closed
1617Websocket have been closed by server
2000AudioSession Config failed
2001Priview is nil
2002URL wrong
2003Signaling json init error
2004Signaling json parse error
2005Ice connect failed
2006Wrong video resolution setting
2007No valid formats for device
2008Capture error
2009Local create offer error
2010Set local description error
2011Local create answer error
2012Set remote description error
2013Socket connect faild
2014MRTC pusher socket disconnect
2015MRTC puller socket disconnect
2016MRTC interactive socket disconnect
2017Role out of bounds
2018Wrong video resolution format