From 70a30cd7d093f6ee3fdb09adcd6f2f34f2ca25d6 Mon Sep 17 00:00:00 2001 From: zu Date: Thu, 21 Oct 2021 00:50:48 +0800 Subject: [PATCH] =?UTF-8?q?RtcManager=20=E5=A4=96=E9=83=A8=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE=E8=B0=83=E6=95=B4=EF=BC=9B=E6=96=B0=E5=A2=9E=E5=86=85?= =?UTF-8?q?=E9=83=A8=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xplan-ios.xcodeproj/project.pbxproj | 2 ++ xplan-ios/Main/RTC/RtcDelegate.h | 3 +- xplan-ios/Main/RTC/RtcImpl/AgoraRtcImpl.m | 8 ++--- xplan-ios/Main/RTC/RtcImpl/BaseRtcImpl.h | 7 +++-- xplan-ios/Main/RTC/RtcImpl/BaseRtcImpl.m | 29 ++++++++++++++----- xplan-ios/Main/RTC/RtcImpl/RtcImplDelegate.h | 19 ++++++++++++ xplan-ios/Main/RTC/RtcImpl/RtcInterface.h | 4 --- xplan-ios/Main/RTC/RtcManager.h | 15 ++++++---- xplan-ios/Main/RTC/RtcManager.m | 28 +++++++++--------- .../Category/XPRoomViewController+SDKConfig.m | 5 ++-- 10 files changed, 78 insertions(+), 42 deletions(-) create mode 100644 xplan-ios/Main/RTC/RtcImpl/RtcImplDelegate.h diff --git a/xplan-ios.xcodeproj/project.pbxproj b/xplan-ios.xcodeproj/project.pbxproj index 7ea47650..a6915adc 100644 --- a/xplan-ios.xcodeproj/project.pbxproj +++ b/xplan-ios.xcodeproj/project.pbxproj @@ -384,6 +384,7 @@ 7DB00EC07F1D0ADFF900B38D /* Pods-xplan-ios.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-xplan-ios.debug.xcconfig"; path = "Target Support Files/Pods-xplan-ios/Pods-xplan-ios.debug.xcconfig"; sourceTree = ""; }; 9B0E1C5726E77022005D4442 /* BaseNavigationController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BaseNavigationController.h; sourceTree = ""; }; 9B0E1C5826E77022005D4442 /* BaseNavigationController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BaseNavigationController.m; sourceTree = ""; }; + 9BB865B4272076140029CDE0 /* RtcImplDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RtcImplDelegate.h; sourceTree = ""; }; B66633E061B1B34177CD011C /* Pods-xplan-ios.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-xplan-ios.release.xcconfig"; path = "Target Support Files/Pods-xplan-ios/Pods-xplan-ios.release.xcconfig"; sourceTree = ""; }; CACF623970097D653132D69A /* Pods_xplan_ios.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_xplan_ios.framework; sourceTree = BUILT_PRODUCTS_DIR; }; E80487632717DDD9008595F2 /* XPRoomMenuItem.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XPRoomMenuItem.h; sourceTree = ""; }; @@ -710,6 +711,7 @@ 18486233271EB794005FC5DC /* AgoraRtcImpl.h */, 18486234271EB794005FC5DC /* AgoraRtcImpl.m */, E81D587B271FBC3B003063FE /* RtcInterface.h */, + 9BB865B4272076140029CDE0 /* RtcImplDelegate.h */, ); path = RtcImpl; sourceTree = ""; diff --git a/xplan-ios/Main/RTC/RtcDelegate.h b/xplan-ios/Main/RTC/RtcDelegate.h index 51e6c9ac..84f5785d 100644 --- a/xplan-ios/Main/RTC/RtcDelegate.h +++ b/xplan-ios/Main/RTC/RtcDelegate.h @@ -10,10 +10,9 @@ NS_ASSUME_NONNULL_BEGIN @protocol RtcDelegate -@optional ///说话者声音的回调 -- (void)usersSpeaking:(NSMutableArray * _Nullable)speakings; +- (void)usersSpeaking:(NSMutableArray * _Nullable)uids; @end diff --git a/xplan-ios/Main/RTC/RtcImpl/AgoraRtcImpl.m b/xplan-ios/Main/RTC/RtcImpl/AgoraRtcImpl.m index 233f89a9..d7c88195 100644 --- a/xplan-ios/Main/RTC/RtcImpl/AgoraRtcImpl.m +++ b/xplan-ios/Main/RTC/RtcImpl/AgoraRtcImpl.m @@ -18,8 +18,8 @@ @implementation AgoraRtcImpl -- (instancetype)init { - self = [super init]; +- (instancetype)initWithDelegate:(id)delegate { + self = [super initWithDelegate:delegate]; if (self) { _engine = [AgoraRtcEngineKit sharedEngineWithAppId:KeyWithType(KeyType_Agora) delegate:self]; [_engine setChannelProfile:AgoraChannelProfileLiveBroadcasting]; @@ -71,9 +71,7 @@ } } } - if (self.delegate && [self.delegate respondsToSelector:@selector(usersSpeaking:)]) { - [self.delegate usersSpeaking:uids]; - } + [self.delegate usersSpeaking:uids]; } diff --git a/xplan-ios/Main/RTC/RtcImpl/BaseRtcImpl.h b/xplan-ios/Main/RTC/RtcImpl/BaseRtcImpl.h index 51b33309..e5cbe524 100644 --- a/xplan-ios/Main/RTC/RtcImpl/BaseRtcImpl.h +++ b/xplan-ios/Main/RTC/RtcImpl/BaseRtcImpl.h @@ -7,15 +7,16 @@ #import #import "RtcInterface.h" -#import "RtcDelegate.h" +#import "RtcImplDelegate.h" #import "AccountInfoStorage.h" NS_ASSUME_NONNULL_BEGIN @interface BaseRtcImpl : NSObject -///代理 -@property (nonatomic,weak) id delegate; +@property (nonatomic,weak) id delegate; + +- (instancetype)initWithDelegate:(id)delegate; @end diff --git a/xplan-ios/Main/RTC/RtcImpl/BaseRtcImpl.m b/xplan-ios/Main/RTC/RtcImpl/BaseRtcImpl.m index 7478ae5f..d905036b 100644 --- a/xplan-ios/Main/RTC/RtcImpl/BaseRtcImpl.m +++ b/xplan-ios/Main/RTC/RtcImpl/BaseRtcImpl.m @@ -7,15 +7,30 @@ #import "BaseRtcImpl.h" -@interface BaseRtcImpl() - -@end - @implementation BaseRtcImpl -- (BOOL)joinChannel:(NSString *)channelId broadcast:(BOOL)on { - [self broadcast:on]; - return [self joinChannel:channelId]; +- (instancetype)initWithDelegate:(id)delegate{ + self = [super init]; + if (self) { + self.delegate = delegate; + } + return self; +} + +- (void)broadcast:(BOOL)on { + +} + +- (void)destory { + +} + +- (void)exitChannel { + +} + +- (BOOL)joinChannel:(nonnull NSString *)channelId { + return NO; } @end diff --git a/xplan-ios/Main/RTC/RtcImpl/RtcImplDelegate.h b/xplan-ios/Main/RTC/RtcImpl/RtcImplDelegate.h new file mode 100644 index 00000000..c5cdca18 --- /dev/null +++ b/xplan-ios/Main/RTC/RtcImpl/RtcImplDelegate.h @@ -0,0 +1,19 @@ +// +// RtcImplDelegate.h +// xplan-ios +// +// Created by zu on 2021/10/21. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol RtcImplDelegate + +/** 发言用户回调。 */ +- (void)usersSpeaking:(NSMutableArray * _Nullable)uids; + +@end + +NS_ASSUME_NONNULL_END diff --git a/xplan-ios/Main/RTC/RtcImpl/RtcInterface.h b/xplan-ios/Main/RTC/RtcImpl/RtcInterface.h index 293bccc9..4bac1c15 100644 --- a/xplan-ios/Main/RTC/RtcImpl/RtcInterface.h +++ b/xplan-ios/Main/RTC/RtcImpl/RtcInterface.h @@ -18,10 +18,6 @@ NS_ASSUME_NONNULL_BEGIN 上下麦(说话) */ - (void)broadcast:(BOOL)on; -/** - 加入频道同时上麦 - */ -- (BOOL)joinChannel:(NSString *)channelId broadcast:(BOOL)on; /** 退出频道 */ diff --git a/xplan-ios/Main/RTC/RtcManager.h b/xplan-ios/Main/RTC/RtcManager.h index bd16a1a4..d4a3a20f 100644 --- a/xplan-ios/Main/RTC/RtcManager.h +++ b/xplan-ios/Main/RTC/RtcManager.h @@ -16,6 +16,14 @@ typedef enum : NSUInteger { RtcEngineType_AgoraFast,//声网急速 } RtcEngineType; +/** 音频服务管理单例,对所有音频服务 SDK 的封装。 + + **Note:** + + - 外部调用者只需要调用 RtcManager 。 + - 不要侵入 RctImpl 内的逻辑。 + */ + @interface RtcManager : NSObject /** 初始化/重新初始化 RtcManager 实例,设置音频服务类型和 RtcDelegate。 @@ -48,16 +56,13 @@ typedef enum : NSUInteger { - (id)mutableCopy NS_UNAVAILABLE; /** 加入频道(房间)*/ -- (BOOL)joinChannel:(NSString *)channelId; +- (BOOL)enterRoom:(NSString *)roomUid; /** 上下麦(说话)*/ - (void)broadcast:(BOOL)on; -/** 加入频道同时上麦 */ -- (BOOL)joinChannel:(NSString *)channelId broadcast:(BOOL)on; - /** 退出频道 */ -- (void)exitChannel; +- (void)exitRoom; /** 销毁引擎(理论上不需要调用) */ - (void)destory; diff --git a/xplan-ios/Main/RTC/RtcManager.m b/xplan-ios/Main/RTC/RtcManager.m index fd249f5c..df5a5d32 100644 --- a/xplan-ios/Main/RTC/RtcManager.m +++ b/xplan-ios/Main/RTC/RtcManager.m @@ -6,12 +6,13 @@ // #import "RtcManager.h" +#import "RtcImplDelegate.h" #import "AgoraRtcImpl.h" -@interface RtcManager() +@interface RtcManager() @property (nonatomic, strong) id engine; -@property (nonatomic,assign) id engineDelegate; +@property (nonatomic, weak) id engineDelegate; @property (nonatomic, assign) RtcEngineType engineType; @end @@ -34,19 +35,15 @@ return rtcManager; } -- (BOOL)joinChannel:(NSString *)channelId { - return [self.engine joinChannel:channelId]; +- (BOOL)enterRoom:(NSString *)roomUid { + return [self.engine joinChannel:roomUid]; } - (void)broadcast:(BOOL)on { [self.engine broadcast:on]; } -- (BOOL)joinChannel:(NSString *)channelId broadcast:(BOOL)on { - return [self.engine joinChannel:channelId broadcast:on]; -} - -- (void)exitChannel { +- (void)exitRoom { [self.engine exitChannel]; } @@ -54,6 +51,12 @@ [self.engine destory]; } +- (void)usersSpeaking:(NSMutableArray *)uids { + if (self.engineDelegate) { + [self.engineDelegate usersSpeaking:uids]; + } +} + - (void)setEngineType:(RtcEngineType)type { if (_engine && type != _engineType) { [_engine exitChannel]; @@ -72,19 +75,16 @@ switch (_engineType) { case RtcEngineType_Agora: { - _engine = [[AgoraRtcImpl alloc] init]; + _engine = [[AgoraRtcImpl alloc] initWithDelegate:self]; } break; default: { - _engine = [[AgoraRtcImpl alloc] init]; + _engine = [[AgoraRtcImpl alloc] initWithDelegate:self]; } break; } } - if ([_engine isKindOfClass:[BaseRtcImpl class]]) { - ((BaseRtcImpl*)_engine).delegate = self.engineDelegate; - } return _engine; } diff --git a/xplan-ios/Main/Room/View/Category/XPRoomViewController+SDKConfig.m b/xplan-ios/Main/Room/View/Category/XPRoomViewController+SDKConfig.m index d27ba2f1..f0372d6a 100644 --- a/xplan-ios/Main/Room/View/Category/XPRoomViewController+SDKConfig.m +++ b/xplan-ios/Main/Room/View/Category/XPRoomViewController+SDKConfig.m @@ -13,11 +13,12 @@ - (void)configEnterRoomSDK { [RtcManager initEngineWithType:RtcEngineType_Agora delegate:self]; - [[RtcManager instance] joinChannel:[NSString stringWithFormat:@"%ld", (long)self.roomInfo.uid] broadcast:YES]; + [[RtcManager instance] enterRoom:[NSString stringWithFormat:@"%ld", (long)self.roomInfo.uid]]; + [[RtcManager instance] broadcast:YES]; } #pragma mark - RtcDelegate -- (void)usersSpeaking:(NSMutableArray *)speakings { +- (void)usersSpeaking:(NSMutableArray *)uids { }