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

233 lines
6.8 KiB
Mathematica
Raw Normal View History

2021-12-06 20:43:58 +08:00
//
// TRTCRtcImpl.m
// xplan-ios
//
// Created by zu on 2021/12/6.
//
#import "TRTCRtcImpl.h"
#import "XPConstant.h"
#import "XPMacro.h"
2021-12-06 20:43:58 +08:00
#import <TXLiteAVSDK_TRTC/TRTCCloud.h>
@interface TRTCRtcImpl()<TRTCCloudDelegate>
@property (strong, nonatomic) TRTCCloud *engine;
2022-05-09 22:18:42 +08:00
///
@property (nonatomic, strong) TXAudioEffectManager *musicManager;
///
@property (nonatomic,copy) void(^MusicCompletion)(NSString *filePath);
2022-05-09 22:18:42 +08:00
///id
@property (nonatomic,assign) int musicId;
2021-12-06 20:43:58 +08:00
@end
@implementation TRTCRtcImpl
- (instancetype)initWithDelegate:(id<RtcImplDelegate>)delegate {
self = [super initWithDelegate:delegate];
if (self) {
_engine = [TRTCCloud sharedInstance];
[_engine enableAudioVolumeEvaluation:900];
2022-05-09 22:18:42 +08:00
[TRTCCloud setConsoleEnabled:NO];
2021-12-06 20:43:58 +08:00
_engine.delegate = self;
}
return self;
}
#pragma mark - RtcInterface impl
- (BOOL)joinChannel:(NSString *)channelId sign:(nonnull NSString *)sign completion:(void (^)(void))completion {
2022-03-08 14:43:55 +08:00
[self.engine enableAudioVolumeEvaluation:900];
2021-12-06 20:43:58 +08:00
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;
2021-12-06 20:43:58 +08:00
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];
if (roomUid.integerValue > INT_MAX) {
[jsonDict setObject:@(uid.integerValue) forKey:@"roomId"];
} else {
[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) {
}
}
///退
- (void)onDisconnectOtherRoom:(TXLiteAVError)errCode errMsg:(NSString *)errMsg {
}
- (void)onUserAudioAvailable:(NSString *)userId available:(BOOL)available {
NSLog(@"成功userid:%@", userId);
if (available) {
}
}
2021-12-06 20:43:58 +08:00
- (BOOL)muteRemote:(BOOL)mute {
[self.engine muteAllRemoteAudio:mute];
return YES;
}
- (BOOL)muteRemote:(BOOL)mute userId:(NSString *)userId {
[self.engine muteRemoteAudio:userId mute:mute];
return YES;
}
2021-12-06 20:43:58 +08:00
- (void)broadcast:(BOOL)on {
[self.engine switchRole:on ? TRTCRoleAnchor : TRTCRoleAudience];
if (on) {
2022-11-18 16:01:42 +08:00
NSString *jsonString = @"{\"api\":\"setAudioQualityEx\",\"params\":{\"sampleRate\":48000,\"channel\":2,\"bitrate\":192,\"encodeMode\":1,\"systemVolumeType\":1}}";
[self.engine callExperimentalAPI:jsonString];
NSString *aecString = @"{\"api\":\"enableAudioAEC\",\"params\":{\"enable\":1,\"level\":100}}";
[self.engine callExperimentalAPI:aecString];
[self.engine startLocalAudio:TRTCAudioQualityMusic];
2021-12-06 20:43:58 +08:00
} 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];
}
2022-05-09 22:18:42 +08:00
///
/// @param filePath
/// @param musicId TRTC
- (BOOL)playBackMusic:(NSString *)filePath musicId:(int)musicId completion:(void (^)(NSString *))completion{
2022-05-09 22:18:42 +08:00
if (filePath) {
self.MusicCompletion = completion;
2022-05-09 22:18:42 +08:00
[self changePlayState:BackMusicPlayState_Stop];//need stop
self.musicId = musicId;
if (self.musicManager == nil) {
self.musicManager = [self.engine getAudioEffectManager];
}
TXAudioMusicParam *param = [[TXAudioMusicParam alloc] init];
param.ID = musicId;
param.path = filePath;
@kWeakify(self);
2022-05-09 22:18:42 +08:00
[self.musicManager startPlayMusic:param onStart:^(NSInteger errCode) {
2022-05-09 22:18:42 +08:00
} onProgress:^(NSInteger progressMs, NSInteger durationMs) {
} onComplete:^(NSInteger errCode) {
@kStrongify(self);
self.MusicCompletion(filePath);
///
[[NSNotificationCenter defaultCenter] postNotificationName:kRoomBackMusicPlayMusicFinishKey object:filePath];
2022-05-09 22:18:42 +08:00
}];
return YES;
} else {
return NO;
}
}
///
/// @param state
- (BOOL)changePlayState:(BackMusicPlayState)state {
BOOL isPlaying = NO;
switch (state) {
case BackMusicPlayState_Stop:
{
[self.musicManager stopPlayMusic:self.musicId];
isPlaying = NO;
}
break;
case BackMusicPlayState_Pause:
{
[self.musicManager pausePlayMusic:self.musicId];
isPlaying = NO;
}
break;
case BackMusicPlayState_Resume:
{
[self.musicManager resumePlayMusic:self.musicId];
isPlaying = YES;
2022-05-09 22:18:42 +08:00
}
default:
break;
}
return isPlaying;
}
- (void)updateUserSound:(int)soundVol {
[self.engine setAudioCaptureVolume:soundVol];
}
- (void)updateMusicSound:(int)soundVol {
[self.musicManager setMusicPublishVolume:self.musicId volume:soundVol * 1.5];
[self.musicManager setMusicPlayoutVolume:self.musicId volume:soundVol * 1.5];
2022-05-09 22:18:42 +08:00
}
2021-12-06 20:43:58 +08:00
#pragma mark - TRTCCloudDelegate
- (void)onUserVoiceVolume:(NSArray<TRTCVolumeInfo *> *)userVolumes totalVolume:(NSInteger)totalVolume {
NSMutableArray *uids = [NSMutableArray array];
for (TRTCVolumeInfo *userInfo in userVolumes) {
NSString *uid = userInfo.userId;
2022-03-08 14:43:55 +08:00
if (userInfo.volume > 2){
2021-12-06 20:43:58 +08:00
if (uid.integerValue == 0) {
[uids addObject:[[AccountInfoStorage instance] getUid]];
}else {
[uids addObject:uid];
}
}
}
[self.delegate usersSpeaking:uids];
}
@end