Files
yinmeng-ios/xplan-ios/Main/RTC/RtcImpl/TRTCRtcImpl.m

140 lines
4.0 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// TRTCRtcImpl.m
// xplan-ios
//
// Created by zu on 2021/12/6.
//
#import "TRTCRtcImpl.h"
#import "XPConstant.h"
#import <TXLiteAVSDK_TRTC/TRTCCloud.h>
@interface TRTCRtcImpl()<TRTCCloudDelegate>
@property (strong, nonatomic) TRTCCloud *engine;
@end
@implementation TRTCRtcImpl
- (instancetype)initWithDelegate:(id<RtcImplDelegate>)delegate {
self = [super initWithDelegate:delegate];
if (self) {
_engine = [TRTCCloud sharedInstance];
[_engine enableAudioVolumeEvaluation:900];
_engine.delegate = self;
}
return self;
}
#pragma mark - RtcInterface impl
- (BOOL)joinChannel:(NSString *)channelId sign:(nonnull NSString *)sign completion:(void (^)(void))completion {
[self.engine enableAudioVolumeEvaluation:900];
TRTCParams *params = [[TRTCParams alloc] init];
UInt32 appId;
sscanf([KeyWithType(KeyType_TRTC) UTF8String], "%u", &appId);
params.sdkAppId = appId;
UInt32 roomId;
sscanf([channelId UTF8String], "%u", &roomId);
params.roomId = roomId;
params.userId = [[AccountInfoStorage instance] getUid];
params.userSig = sign;
params.role = TRTCRoleAudience;
[self.engine enterRoom:params appScene:TRTCAppSceneLIVE];
if (completion) {
completion();
}
return YES;
}
///发起跨房通话
- (void)connectOtherRoom:(NSString *)roomUid userId:(NSString *)uid {
NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];
[jsonDict setObject:@([roomUid intValue]) forKey:@"roomId"];
[jsonDict setObject:uid forKey:@"userId"];
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:nil];
NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[self.engine connectOtherRoom:jsonString];
}
///退出跨房通话
- (void)disconnectOtherRoom {
[self.engine disconnectOtherRoom];
}
///发起跨房通话回调
- (void)onConnectOtherRoom:(NSString *)userId errCode:(TXLiteAVError)errCode errMsg:(NSString *)errMsg {
if (errCode == ERR_NULL) {
// self.delegate
}
}
///退出跨房通话回调
- (void)onDisconnectOtherRoom:(TXLiteAVError)errCode errMsg:(NSString *)errMsg {
}
- (void)onUserAudioAvailable:(NSString *)userId available:(BOOL)available {
NSLog(@"成功userid:%@", userId);
if (available) {
}
}
- (BOOL)muteRemote:(BOOL)mute {
[self.engine muteAllRemoteAudio:mute];
return YES;
}
- (void)broadcast:(BOOL)on {
[self.engine switchRole:on ? TRTCRoleAnchor : TRTCRoleAudience];
if (on) {
[self.engine startLocalAudio:TRTCAudioQualityDefault];
} else {
[self.engine stopLocalAudio];
}
}
- (BOOL)muteLocal:(BOOL)mute {
[self.engine muteLocalAudio:mute];
return YES;
}
- (void)exitChannel:(void (^)(void))completion {
/**
* 1.2 离开房间
*
* 调用 exitRoom() 接口会执行退出房间的相关逻辑,例如释放音视频设备资源和编解码器资源等。
* 待资源释放完毕SDK 会通过 TRTCCloudDelegate 中的 onExitRoom() 回调通知到您。
*
* 如果您要再次调用 enterRoom() 或者切换到其他的音视频 SDK请等待 onExitRoom() 回调到来之后再执行相关操作。
* 否则可能会遇到摄像头或麦克风(例如 iOS 里的 AudioSession被占用等各种异常问题。
*/
[self.engine exitRoom];
if (completion) {
completion();
}
}
- (void)destory {
[TRTCCloud destroySharedIntance];
}
#pragma mark - TRTCCloudDelegate
- (void)onUserVoiceVolume:(NSArray<TRTCVolumeInfo *> *)userVolumes totalVolume:(NSInteger)totalVolume {
NSMutableArray *uids = [NSMutableArray array];
for (TRTCVolumeInfo *userInfo in userVolumes) {
NSString *uid = userInfo.userId;
if (userInfo.volume > 2){
if (uid.integerValue == 0) {
[uids addObject:[[AccountInfoStorage instance] getUid]];
}else {
[uids addObject:uid];
}
}
}
[self.delegate usersSpeaking:uids];
}
@end