human: 如何处理七牛云的实时音视频QNRTCClient

gpt: [实时音视频](https://developer.qiniu.com/rtc) > API 文档 > iOS >QNRTCClient

#  QNRTCClient

最近更新时间: 2023-11-20 11:12:45

RTC 核心类，包含加入房间，离开房间，订阅、发布 Track，获取统计信息等功能

Index

Core Properties

  * delegate
  * connectionState
  * autoSubscribe
  * remoteUserList
  * publishedTracks

Core Methods

  * setClientRole
  * join[1/2]
  * join[2/2]
  * leave
  * publish[1/2]
  * publish[2/2]
  * unpublish
  * subscribe
  * unsubscribe
  * startLiveStreamingWithDirect
  * startLiveStreamingWithTranscoding
  * stopLiveStreamingWithDirect
  * stopLiveStreamingWithTranscoding
  * setTranscodingLiveStreamingID
  * removeTranscodingLiveStreamingID
  * startRoomMediaRelay
  * updateRoomMediaRelay
  * stopRoomMediaRelay
  * sendMessage
  * getRemoteUser
  * getUserNetworkQuality
  * getRemoteVideoTrackStats
  * getRemoteAudioTrackStats
  * getLocalVideoTrackStats
  * getLocalAudioTrackStats
  * enableAudioVolumeIndication

Core Properties

delegate

  * @property (nonatomic, weak) id<[QNRTCClientDelegate](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9248/QNRTCClientDelegate)> delegate;
  * RTC Client 的状态回调代理

connectionState

  * @property (nonatomic, assign, readonly) [QNConnectionState](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9254) connectionState;
  * RTC client 的房间状态

autoSubscribe

  * @property (nonatomic, assign) BOOL autoSubscribe;
  * 是否自动订阅远端的流，默认为 YES

当开启此选项后，房间内有任意用户新发布 Track 的时候，会自动订阅此路 Track，订阅成功后，会触发
[QNRTCClientDelegate.didSubscribedRemoteVideoTracks](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/9248) 回调

remoteUserList

  * @property (nonatomic, strong, readonly) NSArray<[QNRemoteUser](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8835/QNRemoteUser-iOS) *> *remoteUserList;
  * 远端用户列表

publishedTracks

  * @property (nonatomic, strong, readonly) NSArray<[QNLocalTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8847/QNLocalTrack-iOS) *> *publishedTracks;
  * 已发布 Track 列表

Core Methods

setClientRole

  * \- (void)setClientRole:([QNClientRole](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9254))role completeCallback:(nullable [QNClientRoleResultCallback](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9254))callback;
  * 设置用户角色

> 该方法在加入频道前后均可调用，该方法仅适用于直播场景。

Parameters

    * role: [QNClientRole](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9254)

用户角色

join[1/2]

  * \- (void)join:(NSString *)token;
  * 加入房间

接口调用成功后，将会触发
[QNRTCClientDelegate.didConnectionStateChanged](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/9248) 回调

> 请确保在 QNConnectionStateDisconnected 状态下调用该接口加入房间，否则接口调用无效

Parameters

    * token: NSString 

房间 Token

join[2/2]

  * \- (void)join:(NSString *)token userData:(NSString *)userData;
  * 加入房间

接口调用成功后，将会触发
[QNRTCClientDelegate.didConnectionStateChanged](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/9248) 回调

> 请确保在 QNConnectionStateDisconnected 状态下调用该接口加入房间，否则接口调用无效

Parameters

    * token: NSString 

房间 Token

    * userData: NSString 

用户信息

leave

  * \- (void)leave;
  * 离开房间

成功离开房间后，会触发
[QNRTCClientDelegate.didConnectionStateChanged](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/9248) 回调

publish[1/2]

  * -(void)publish:(NSArray<[QNLocalTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8847/QNLocalTrack-iOS) *> *)tracks;
  * 发布本地 Track

Parameters

    * tracks: NSArray<[QNLocalTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8847/QNLocalTrack-iOS)>

要发布的本地 Track

publish[2/2]

  * -(void)publish:(NSArray<[QNLocalTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8847/QNLocalTrack-iOS) *> *)tracks completeCallback:([QNPublishResultCallback](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9254))callback;
  * 发布本地 Track 列表，若想获取发布结果，可实现 [QNPublishResultCallback](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9254)

Parameters

    * tracks: NSArray<[QNLocalTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8847/QNLocalTrack-iOS)>

要发布的本地 Track 列表

    * callback: [QNPublishResultCallback](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9254)

发布本地 Track 列表是否成功的回调

unpublish

  * \- (void)unpublish:(NSArray<[QNLocalTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8847/QNLocalTrack-iOS) *> *)tracks;
  * 取消发布本地 Track

Parameters

    * tracks: NSArray<[QNLocalTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8847/QNLocalTrack-iOS)>

要取消发布的本地 Track

subscribe

  * \- (void)subscribe:(NSArray<[QNRemoteTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8857/QNRemoteTrack-iOS) *> *)tracks;
  * 订阅远端 Track，订阅成功后，会触发 [QNRTCClientDelegate.didSubscribedRemoteVideoTracks](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9248) 回调

Parameters

    * tracks: NSArray<[QNRemoteTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8857/QNRemoteTrack-iOS)>

要订阅的远端 Track

unsubscribe

  * \- (void)unsubscribe:(NSArray<[QNRemoteTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8857/QNRemoteTrack-iOS) *> *)tracks;
  * 取消订阅远端 Track

Parameters

    * tracks: NSArray<[QNRemoteTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8857/QNRemoteTrack-iOS)>

要取消订阅的远端 Track

startLiveStreamingWithDirect

  * \- (void)startLiveStreamingWithDirect:([QNDirectLiveStreamingConfig](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8861/QNDirectLiveStreamingConfig-iOS) *)config;
  * 开始单路 CDN 转推

转推成功会触发
[QNRTCClientDelegate.didStartLiveStreaming](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/9248) 回调

Parameters

    * config: [QNDirectLiveStreamingConfig](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8861/QNDirectLiveStreamingConfig-iOS)

单路转推配置

startLiveStreamingWithTranscoding

  * \- (void)startLiveStreamingWithTranscoding:([QNTranscodingLiveStreamingConfig](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8862/QNTranscodingLiveStreamingConfig-iOS) *)config;
  * 开始合流转码 CDN 转推

转推成功会触发
[QNRTCClientDelegate.didStartLiveStreaming](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/9248) 回调

Parameters

    * config: [QNTranscodingLiveStreamingConfig](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8862/QNTranscodingLiveStreamingConfig-iOS)

合流转推配置

stopLiveStreamingWithDirect

  * \- (void)stopLiveStreamingWithDirect:([QNDirectLiveStreamingConfig](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8861/QNDirectLiveStreamingConfig-iOS) *)config;
  * 停止单路 CDN 转推

停止成功会触发
[QNRTCClientDelegate.didStopLiveStreaming](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/9248) 回调

Parameters

    * config: [QNDirectLiveStreamingConfig](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8861/QNDirectLiveStreamingConfig-iOS)

单路 CDN 转推配置

stopLiveStreamingWithTranscoding

  * \- (void)stopLiveStreamingWithTranscoding:([QNTranscodingLiveStreamingConfig](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8862/QNTranscodingLiveStreamingConfig-iOS) *)config;
  * 停止合流转码 CDN 转推

停止成功会触发
[QNRTCClientDelegate.didStopLiveStreaming](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/9248) 回调

Parameters

    * config: [QNTranscodingLiveStreamingConfig](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8862/QNTranscodingLiveStreamingConfig-iOS)

合流转推配置

setTranscodingLiveStreamingID

  * \- (void)setTranscodingLiveStreamingID:(NSString *)streamID withTracks:(NSArray <[QNTranscodingLiveStreamingTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8863/QNTranscodingLiveStreamingTrack-iOS) *> *)tracks;
  * 新增、更新合流转推布局配置

更新成功会触发
[QNRTCClientDelegate.didTranscodingTracksUpdated](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/9248) 回调

> 新增 Track 布局以及对已有的 Track 的合流布局进行更新均可通过该接口实现，只需修改 transcodingTracks 对应的 Track
> 内容即可。

Parameters

    * streamID: NSString 

合流任务 streamID

    * transcodingTracks: NSArray <[QNTranscodingLiveStreamingTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8863/QNTranscodingLiveStreamingTrack-iOS)>

待新增、更新的合流转推布局配置

removeTranscodingLiveStreamingID

  * \- (void)removeTranscodingLiveStreamingID:(NSString *)streamID withTracks:(NSArray <[QNTranscodingLiveStreamingTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8863/QNTranscodingLiveStreamingTrack-iOS) *> *)tracks;
  * 移除合流转推布局配置

Parameters

    * streamID: NSString 

合流任务 streamID

    * transcodingTracks: NSArray <[QNTranscodingLiveStreamingTrack](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8863/QNTranscodingLiveStreamingTrack-iOS)>

待移除合流转推布局配置

startRoomMediaRelay

  * \- (void)startRoomMediaRelay:([QNRoomMediaRelayConfiguration](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/10143/QNRoomMediaRelayConfiguration-iOS) *_Nonnull)config completeCallback:(nullable [QNMediaRelayResultCallback](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9254))callback;
  * 开启跨房媒体转发，如果已经开启，则调用失败

> 当所有目标房间跨房媒体转发都失败，则本次跨房媒体转发请求视为失败，使用跨房媒体转发功能需要再次调用此方法
>
> 当有任意一个目标或多个目标房间媒体转发成功，则本次跨房媒体转发请求视为成功，具体每个房间的状态参考回调结果。

> 该方法仅适用直播类型房间中角色类型为主播的用户

Parameters

    * config: [QNRoomMediaRelayConfiguration](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/10143/QNRoomMediaRelayConfiguration-iOS)

跨房间媒体转发参数配置

updateRoomMediaRelay

  * \- (void)updateRoomMediaRelay:([QNRoomMediaRelayConfiguration](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/10143/QNRoomMediaRelayConfiguration-iOS) *_Nonnull)config completeCallback:(nullable [QNMediaRelayResultCallback](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9254))callback;
  * 更新跨房媒体转发

> 成功开启跨房媒体转发后，如果你希望将流转发到多个目标房间，或退出当前正在转发的房间，可以调用该方法
>
> 此 API 为全量更新，正在跨房媒体转发中却未被包含在参数 configuration 中的房间，将停止媒体转发。

> 调用此 API 前必须确保已经成功开启跨房媒体转发，否则将调用失败；该方法仅适用直播类型房间中角色类型为主播的用户。

Parameters

    * config: [QNRoomMediaRelayConfiguration](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/10143/QNRoomMediaRelayConfiguration-iOS)

跨房媒体转发参数配置

stopRoomMediaRelay

  * \- (void)stopRoomMediaRelay:(nullable [QNMediaRelayResultCallback](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9254))callback;
  * 停止跨房媒体转发，如果未开启，则调用失败。

> 一旦停止，会停止在所有目标房间中的媒体转发；该方法仅适用直播类型房间中角色类型为主播的用户。

sendMessage

  * \- (void)sendMessage:(NSString *)messsage toUsers:(nullable NSArray<NSString *> *)users messageId:(nullable NSString *)messageId;
  * 发送自定义消息到指定用户群，发送成功后，会触发远端用户的 [QNRTCClientDelegate.didReceiveMessage](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/9248) 回调接口

Parameters

    * message: NSString 

自定义消息 内容

    * users: NSArray<NSString>

用户 ID 列表

    * messageId: NSString 

自定义消息 ID

getRemoteUser

  * \- ([QNRemoteUser](https://developer.qiniu.com/rtc/8836/QNRTCClient-iOS/8835/QNRemoteUser-iOS) *)getRemoteUser:(NSString *)userID;
  * 获取对应 ID 的远端用户

Parameters

    * userID: NSString 

远端用户 ID

Returns [QNRemoteUser](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/8835/QNRemoteUser-iOS)

getUserNetworkQuality

  * \- (NSDictionary *)getUserNetworkQuality;
  * 获取当前订阅的远端用户网络质量

Returns NSDictionary<NSString,
[QNNetworkQuality](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/8869/QNNetworkQuality-iOS)>

getRemoteVideoTrackStats

  * \- (NSDictionary *)getRemoteVideoTrackStats;
  * 获取已订阅的远端视频轨道统计信息

Returns NSDictionary<NSString,
[QNRemoteVideoTrackStats](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/8867/QNRemoteVideoTrackStats-iOS)>

getRemoteAudioTrackStats

  * \- (NSDictionary *)getRemoteAudioTrackStats;
  * 获取已订阅的远端音频轨道统计信息

Returns NSDictionary<NSString,
[QNRemoteAudioTrackStats](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/8868/QNRemoteAudioTrackStats-iOS)>

getLocalVideoTrackStats

  * \- (NSDictionary *)getLocalVideoTrackStats;
  * 获取已发布的近端视频轨道统计信息

Returns NSDictionary<NSString,
<[QNLocalVideoTrackStats](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/8870/QNLocalVideoTrackStats-iOS)>>

getLocalAudioTrackStats

  * \- (NSDictionary *)getLocalAudioTrackStats;
  * 获取已发布的近端音频轨道统计信息

Returns NSDictionary<NSString,
[QNLocalAudioTrackStats](https://developer.qiniu.com/rtc/8836/QNRTCClient-
iOS/8871/QNLocalAudioTrackStats-iOS)>

enableAudioVolumeIndication

  * \- (void)enableAudioVolumeIndication:(int)interval;
  * 开启语音音量监测

> 指定音量提示的时间间隔，≤ 0: 禁用音量提示功能，> 0: 音量提示的间隔，单位为毫秒，最小取值为 100。

Parameters

    * interval: int 

音量提示的时间间隔

以上内容是否对您有帮助？