Files
yinmeng-ios/xplan-ios/Main/RTC/RtcManager.m
2022-12-23 10:49:26 +08:00

204 lines
4.8 KiB
Objective-C

//
// RTCManager.m
// xplan-ios
//
// Created by zu on 2021/10/19.
//
#import "RtcManager.h"
#import "RtcImplDelegate.h"
#import "AgoraRtcImpl.h"
#import "ZegoRtcImpl.h"
#import "TRTCRtcImpl.h"
@interface RtcManager()<RtcImplDelegate>
@property (nonatomic, strong) id<RtcInterface> engine;
@property (nonatomic, weak) id<RtcDelegate> engineDelegate;
@property (nonatomic, assign) RtcEngineType engineType;
/**
* 当前 Rtc 所在的房间 roomUid 。
*/
@property(nonatomic, strong) NSString * enterdRoomUid;
@end
@implementation RtcManager
+ (instancetype)instance {
static dispatch_once_t onceToken;
static RtcManager *instance = nil;
dispatch_once(&onceToken,^{
instance = [[self alloc] init];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
_localMuted = NO;
_remoteMuted = NO;
_engineType = RtcEngineType_Agora;
}
return self;
}
+ (instancetype)initEngineWithType:(RtcEngineType)type delegate:(id<RtcDelegate> _Nullable)delegate {
RtcManager* rtcManager = [self instance];
[rtcManager setEngineType:type];
[rtcManager setEngineDelegate:delegate];
return rtcManager;
}
- (BOOL)enterRoom:(NSString *)roomUid {
if (self.enterdRoomUid && [self.enterdRoomUid isEqualToString:roomUid]) {
return YES;
}
return [self.engine joinChannel:roomUid completion:^{
[self muteRemote:NO];
[self muteLocal:NO];
self.enterdRoomUid = roomUid;
}];
}
- (BOOL)enterRoom:(NSString *)roomUid trtcSign:(nonnull NSString *)sign {
if (self.enterdRoomUid && [self.enterdRoomUid isEqualToString:roomUid]) {
return YES;
}
return [self.engine joinChannel:roomUid sign:sign completion:^{
[self muteRemote:NO];
[self muteLocal:NO];
self.enterdRoomUid = roomUid;
}];
}
- (BOOL)muteRemote:(BOOL)mute {
return [self.engine muteRemote:mute];
}
- (void)broadcast:(BOOL)on {
[self.engine broadcast:on];
if (on) {
[self.engine muteLocal:self.isLocalMuted];
}
}
- (BOOL)muteLocal:(BOOL)mute {
return [self.engine muteLocal:mute];
}
- (void)exitRoom {
[self.engine exitChannel:^{
[self muteRemote:NO];
[self muteLocal:NO];
self.enterdRoomUid = nil;
}];
}
///发起跨房通话
- (void)connectOtherRoom:(NSString *)roomUid userId:(NSString *)uid {
[self.engine broadcast:YES];
[self.engine connectOtherRoom:roomUid userId:uid];
}
///退出跨房通话
- (void)disconnectOtherRoom {
[self.engine disconnectOtherRoom];
}
/// 静音某个人
/// @param userId 用户id
- (void)muteOne:(BOOL)mute userId:(NSString *)userId {
[self.engine muteRemote:mute userId:userId];
}
- (void)destory {
[self.engine destory];
}
/// 播放背景音乐
/// @param filePath 音频文件的地址
/// @param musicId TRTC自己要的
- (BOOL)playBackMusic:(NSString *)filePath musicId:(int)musicId completion:(void (^)(NSString *))completion{
return [self.engine playBackMusic:filePath musicId:musicId completion:completion];
}
/// 改变播放器的状态
/// @param state 播放状态
- (BOOL)changePlayState:(BackMusicPlayState)state {
return [self.engine changePlayState:state];
}
///背景音乐的音量大小
- (void)updateMusicSound:(int)soundVol {
[self.engine updateMusicSound:soundVol];
}
///背景人声的音量大小
- (void)updateUserSound:(int)soundVol {
[self.engine updateUserSound:soundVol];
}
- (void)usersSpeaking:(NSMutableArray *)uids {
if (self.engineDelegate) {
[self.engineDelegate usersSpeaking:uids];
}
}
- (void)setRemoteMuted:(BOOL)remoteMuted {
if ([self.engine muteRemote:remoteMuted]) {
_remoteMuted = remoteMuted;
}
}
- (void)setLocalMuted:(BOOL)localMuted {
if ([self.engine muteLocal:localMuted]) {
_localMuted = localMuted;
}
}
- (void)setEngineType:(RtcEngineType)type {
if (_engine && type != _engineType) {
[_engine exitChannel:nil];
[_engine destory];
_engine = nil;
_enterdRoomUid = nil;
}
_engineType = type;
}
- (void)setEngineDelegate:(id<RtcDelegate>)delegate {
_engineDelegate = delegate;
}
- (id<RtcInterface>)engine {
if (!_engine) {
switch (_engineType) {
case RtcEngineType_Agora:
{
_engine = [[AgoraRtcImpl alloc] initWithDelegate:self];
}
break;
case RtcEngineType_Zego:
{
_engine = [[ZegoRtcImpl alloc] initWithDelegate:self];
}
break;
case RtcEngineType_TRTC:
{
_engine = [[TRTCRtcImpl alloc] initWithDelegate:self];
}
break;
default:
{
_engine = [[AgoraRtcImpl alloc] initWithDelegate:self];
}
break;
}
}
return _engine;
}
@end