Files
yinmeng-ios/xplan-ios/Main/RTC/RtcManager.m

174 lines
4.0 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 connectOtherRoom:roomUid userId:uid];
}
///退出跨房通话
- (void)disconnectOtherRoom {
[self.engine disconnectOtherRoom];
}
- (void)destory {
[self.engine destory];
}
- (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