BGTLinkMicPlayController.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. //
  2. // BGTLinkMicPlayController.m
  3. // BuguLive
  4. //
  5. // Created by xfg on 16/12/5.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "BGTLinkMicPlayController.h"
  10. #import "UserView.h"
  11. #import "VoiceLianmaiUserModel.h"
  12. #import "VoiceLianmaiView.h"
  13. @implementation TCLivePushListenerImpl
  14. {
  15. }
  16. - (void)onPushEvent:(int)evtID withParam:(NSDictionary*)param
  17. {
  18. if (self.delegate)
  19. {
  20. [self.delegate onLivePushEvent:self.pushUrl withEvtID:evtID andParam:param];
  21. }
  22. }
  23. - (void)onNetStatus:(NSDictionary*) param
  24. {
  25. if (self.delegate)
  26. {
  27. [self.delegate onLivePushNetStatus:self.pushUrl withParam:param];
  28. }
  29. }
  30. @end
  31. @interface BGTLinkMicPlayController()<ITCLivePushListener, ITCLivePlayListener,UserViewDelegate,TRTCCloudDelegate>
  32. {
  33. BOOL _isNeedLoading; // 是否需要展示加载指示器
  34. UIView* _smallPlayVideoView; // 小主播预览窗口
  35. UIView* _fullScreenVideoView; // 大主播全屏窗口
  36. UIView* _loadingBackground; // 加载中的背景
  37. UIImageView * _loadingImageView; // 加载中的背景图
  38. TCLivePushListenerImpl* _txLivePushListener; // 小主播推流监听
  39. TXLivePushConfig * _txLivePushConfig; // 小主播推流配置
  40. TCLivePlayListenerImpl* _txLivePlayListener; // 拉流监听
  41. TXLivePlayConfig * _txLivePlayConfig; // 拉流配置
  42. NSMutableArray <NSString *> *lianMaiuser;
  43. UserView *userView1;
  44. UserView *userView2;
  45. UserView *userView3;
  46. }
  47. @property (nonatomic,strong) UIView *userListView;
  48. @property(nonatomic, assign) BOOL isBeingLinkMic; // 是否正在连麦中
  49. @end
  50. @implementation BGTLinkMicPlayController
  51. - (void)viewDidLoad
  52. {
  53. [super viewDidLoad];
  54. // _lianmaiOpenSound = YES;
  55. //声网dev
  56. lianMaiuser = [NSMutableArray array];
  57. _isBeingLinkMic = false;
  58. [GlobalVariables sharedInstance].isBeingLinkMic = _isBeingLinkMic;
  59. _isWaitingResponse = false;
  60. _isNeedLoading = YES;
  61. _txLivePlayListener = [[TCLivePlayListenerImpl alloc] init];
  62. _txLivePlayListener.delegate = self;
  63. _txLivePlayConfig = [[TXLivePlayConfig alloc] init];
  64. _playItemArray = [NSMutableArray array];
  65. _linkMemeberSet = [NSMutableSet set];
  66. self.userListView.hidden = YES;
  67. userView1.hidden = YES;
  68. userView2.hidden = YES;
  69. userView3.hidden = YES;
  70. userView1.userInteractionEnabled = YES;
  71. userView2.userInteractionEnabled = YES;
  72. userView3.userInteractionEnabled = YES;
  73. // [self reloduserListView];
  74. [self addLinkMicPlayItem];
  75. }
  76. - (UIView *)userListView {
  77. if (!_userListView) {
  78. int userViewWidth = 80;
  79. int userViewHeight = userViewWidth * 1.5;
  80. _userListView = [[UIView alloc] initWithFrame:CGRectMake(kScreenW - userViewWidth - 10, kScreenH - userViewHeight*3 - 90, 0, 0)];
  81. _userListView.backgroundColor = kClearColor;
  82. [[GlobalVariables sharedInstance].tliveView addSubview:_userListView];
  83. userView1 = [UserView getView];
  84. userView1.delegate = self;
  85. userView1.frame = CGRectMake(0, 0, userViewWidth, userViewHeight);
  86. userView1.backgroundColor = kRedColor;
  87. [_userListView addSubview:userView1];
  88. userView2 = [UserView getView];
  89. userView2.delegate = self;
  90. userView2.frame = CGRectMake(0, 0, userViewWidth, userViewHeight);
  91. userView2.backgroundColor = kYellowColor;
  92. [_userListView addSubview:userView2];
  93. userView3 = [UserView getView];
  94. userView3.delegate = self;
  95. userView3.frame = CGRectMake(0, 0, userViewWidth, userViewHeight);
  96. [_userListView addSubview:userView3];
  97. }
  98. return _userListView;
  99. }
  100. //点击视频或者音频代理
  101. - (void)clickVideoBtn:(UserView *)view
  102. {
  103. //如果按钮当前select NO,则要下一步则要关闭视频
  104. if(view.videoBtn.selected == NO)
  105. {
  106. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:YES];
  107. }
  108. else
  109. {
  110. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:NO];
  111. }
  112. NSLog(@"点击了视频");
  113. }
  114. - (void)clickVoiceBtn:(UserView *)view
  115. {
  116. //如果按钮当前select NO,则要下一步则要关闭音频
  117. if(view.voiceBtn.selected == NO)
  118. {
  119. [self.trtcCloud muteLocalAudio:YES];
  120. }
  121. else
  122. {
  123. [self.trtcCloud muteLocalAudio:NO];
  124. }
  125. NSLog(@"点击了音频");
  126. }
  127. - (void)clickUserView:(UserView *)view
  128. {
  129. [userView1 setSelect:NO];
  130. [userView2 setSelect:NO];
  131. [userView3 setSelect:NO];
  132. // post notification
  133. // NSDictionary * userInfo = [NSDictionary dictionaryWithObject:view.uid forKey:@"uid"];
  134. // [[NSNotificationCenter defaultCenter] postNotificationName:@"showGiftView" object:self userInfo:userInfo];
  135. [view setSelect:YES];
  136. }
  137. #pragma mark - ----------------------- 腾讯云 delegate -----------------------
  138. - (void)enterRoomWithTXTRTC {
  139. // 创建 SDK 实例(单例模式)并设置事件监听器
  140. // Create trtc instance(singleton) and set up event listeners
  141. _trtcCloud = [TRTCCloud sharedInstance];
  142. [_trtcCloud addDelegate:self];
  143. // [self.trtcCloud switchRole:TRTCRoleAnchor];
  144. [self.trtcCloud startLocalAudio:TRTCAudioQualityMusic];
  145. // [self.trtcCloud switchRole:TRTCRoleAudience];
  146. TRTCParams *params = [[TRTCParams alloc] init];
  147. // 以字符串房间号为例
  148. params.roomId = self.roomIDStr.intValue;
  149. params.userId = [IMAPlatform sharedInstance].host.userId;
  150. // 从业务后台获取到的 UserSig
  151. params.userSig = [IMALoginParam loadFromLocal].userSig;
  152. // 替换成您的 SDKAppID
  153. params.sdkAppId = TXSDKAppID;
  154. // 根据需要指定用户角色
  155. params.role = TRTCRoleAudience;
  156. if (self.liveInfo.user_id.intValue == [IMAPlatform sharedInstance].host.userId.intValue) {
  157. params.role = TRTCRoleAnchor;
  158. }
  159. // 根据需要指定场景
  160. [self.trtcCloud enterRoom:params appScene:TRTCAppSceneLIVE];
  161. self.trtcCloud = [TRTCCloud sharedInstance];
  162. // 将 denny 的主路画面切换到一个悬浮的小窗口中(假如该迷你小窗口为 miniFloatingView)
  163. // [self.trtcCloud updateRemoteView:miniFloatingView streamType:TRTCVideoStreamTypeBig forUser:@"denny"];
  164. // 将远端用户 denny 的主路画面设置为填充模式,并开启左右镜像模式
  165. TRTCRenderParams *param = [[TRTCRenderParams alloc] init];
  166. param.fillMode = TRTCVideoFillMode_Fill;
  167. param.mirrorType = TRTCVideoMirrorTypeDisable;
  168. // 播放 denny 的摄像头画面(我们称之为“主路画面”)
  169. [self.trtcCloud startRemoteView:self.liveInfo.user_id streamType:TRTCVideoStreamTypeBig view:_fullScreenVideoView];
  170. }
  171. //我们可以让自己的类继承TRTCCloudDelegate,并重载 onError 函数,最后将 this 指针通过 TRTCCCloud 的 delegate 属性设置给 SDK,就可以在当前类中监听来自 SDK 的回调事件了。
  172. // 监听 SDK 的事件,并对“摄像头未被授权”等错误进行日志打印
  173. - (void)onError:(TXLiteAVError)errCode
  174. errMsg:(nullable NSString *)errMsg
  175. extInfo:(nullable NSDictionary *)extInfo{
  176. if (ERR_CAMERA_NOT_AUTHORIZED == errCode) {
  177. NSString *errorInfo = @"Current application is not authorized to use the camera:";
  178. errorInfo = [errorInfo stringByAppendingString: errMsg];
  179. NSLog(@"%@",errorInfo);
  180. // [self toastTip:errorInfo];
  181. }
  182. }
  183. - (void)onEnterRoom:(NSInteger)result {
  184. if (result > 0) {
  185. // result 代表加入房间所消耗的时间(毫秒)
  186. // [self toastTip:@"Enter room succeed!"];
  187. NSLog(@"加入房间成功");
  188. } else {
  189. // result 代表进房失败的错误码
  190. // [self toastTip:@"Enter room failed!"];
  191. }
  192. }
  193. - (void)onRemoteUserEnterRoom:(NSString *)userId {
  194. if(self.isBeingLinkMic) {
  195. //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
  196. [self.trtcCloud startRemoteView:self.liveInfo.user_id streamType:TRTCVideoStreamTypeBig view:self.videoContrainerViewAgora];
  197. if(userId.intValue != self.liveInfo.user_id.intValue && userId.intValue != [IMAPlatform sharedInstance].host.userId.intValue && ![lianMaiuser containsObject:userId])
  198. {
  199. [lianMaiuser addObject:userId];
  200. // [self reloduserListView];
  201. }
  202. }
  203. else
  204. {
  205. if(userId.intValue == self.liveInfo.user_id.intValue)
  206. {
  207. TRTCRenderParams *params = [[TRTCRenderParams alloc] init];
  208. // 画面镜像模式
  209. params.mirrorType = TRTCVideoMirrorTypeDisable;
  210. // 画面填充模式
  211. params.fillMode = TRTCVideoFillMode_Fill;
  212. // 画面旋转角度
  213. params.rotation = TRTCVideoRotation_0;
  214. // 设置远端画面的渲染模式
  215. [self.trtcCloud setRemoteRenderParams:userId streamType:TRTCVideoStreamTypeBig params:params];
  216. //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
  217. [self.trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeBig view:self.videoContrainerView];
  218. }
  219. else
  220. {
  221. if([GlobalVariables sharedInstance].isBeingPK)
  222. {
  223. //如果正在pk把对方的也加进来
  224. // AgoraRtcVideoCanvas *videoCanvas = [[AgoraRtcVideoCanvas alloc] init];
  225. // videoCanvas.uid = uid;
  226. // videoCanvas.renderMode = AgoraVideoRenderModeHidden;
  227. // videoCanvas.view = self.rightVideoContrainerView;
  228. // // 设置远端视图
  229. // [self.agoraKit setupRemoteVideo:videoCanvas];
  230. TRTCRenderParams *params = [[TRTCRenderParams alloc] init];
  231. // 画面镜像模式
  232. params.mirrorType = TRTCVideoMirrorTypeDisable;
  233. // 画面填充模式
  234. params.fillMode = TRTCVideoFillMode_Fill;
  235. // 画面旋转角度
  236. params.rotation = TRTCVideoRotation_0;
  237. // 设置远端画面的渲染模式
  238. [self.trtcCloud setRemoteRenderParams:userId streamType:TRTCVideoStreamTypeBig params:params];
  239. //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
  240. [self.trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeBig view:self.rightVideoContrainerView];
  241. }
  242. }
  243. }
  244. //用户加入
  245. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  246. [parmDict setObject:@"live_multi_room" forKey:@"ctl"];
  247. [parmDict setObject:@"api_mic_list" forKey:@"act"];
  248. [parmDict setObject:[BogoNetwork shareInstance].token forKey:@"token"];
  249. [parmDict setObject:[BogoNetwork shareInstance].uid forKey:@"uid"];
  250. [parmDict setObject:self.liveInfo.room_id forKey:@"video_id"];
  251. [[NetHttpsManager manager] POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  252. {
  253. FWWeakify(self)
  254. if ([responseJson toInt:@"status"] == 1)
  255. {
  256. NSMutableArray *model_arr = [NSMutableArray array];
  257. NSArray *arr = [responseJson valueForKey:@"list"];
  258. if ([arr isKindOfClass:[NSArray class]]) {
  259. for (NSDictionary *dic in arr) {
  260. VoiceLianmaiUserModel *model =[VoiceLianmaiUserModel mj_objectWithKeyValues: dic];
  261. if(model.user_id.intValue == self.liveInfo.user_id.intValue)
  262. {
  263. continue;
  264. }
  265. [model_arr addObject:model];
  266. }
  267. }
  268. [self adjustPlayItemVoiceUserList:model_arr];
  269. }
  270. } FailureBlock:^(NSError *error)
  271. {
  272. }];
  273. [self updateLianMaiCoin];
  274. }
  275. // 离开房间事件回调
  276. - (void)onExitRoom:(NSInteger)reason {
  277. if (reason == 0) {
  278. NSLog(@"主动调用 exitRoom 退出房间");
  279. } else if (reason == 1) {
  280. NSLog(@"被服务器踢出当前房间");
  281. } else if (reason == 2) {
  282. NSLog(@"当前房间整个被解散");
  283. }
  284. //自己在连麦中,退出房间,关闭连麦
  285. if (self.isLinkingMic) {
  286. [BGLiveSDKViewModel tLiveStopMick:self.roomIDStr toUserId:[IMAPlatform sharedInstance].host.userId];
  287. }
  288. }
  289. - (void)onUserVoiceVolume:(NSArray<TRTCVolumeInfo *> *)userVolumes totalVolume:(NSInteger)totalVolume {
  290. NSLog(@"totalVolume");
  291. NSLog(@"%ld",totalVolume);
  292. /*bug:当a用户在麦上正在说话时,切换到无网状态,此时虽然还显示在麦上(im还没下线),
  293. 但观众席听不到声音,光圈还在闪烁
  294. 原因: 这时观众席speakers 里面找不到a,就不会进入for循环,就不会更新a的声音为0
  295. 解决: 1先找到 麦上的用户 2去判断有无声音
  296. */
  297. for (TRTCVolumeInfo *info in userVolumes) {
  298. NSString *uid = @"";
  299. if (info.userId.intValue == 0) {
  300. uid = [IMAPlatform sharedInstance].host.userId;
  301. }else{
  302. uid = [NSString stringWithFormat:@"%@",info.userId];
  303. }
  304. if([[userView1 uid] isEqualToString:uid])
  305. {
  306. userView1.totalVolume = info.volume;
  307. }
  308. if([[userView2 uid] isEqualToString:uid])
  309. {
  310. userView2.totalVolume = info.volume;
  311. }
  312. if([[userView3 uid] isEqualToString:uid])
  313. {
  314. userView3.totalVolume = info.volume;
  315. }
  316. //speaker.volume
  317. }
  318. if(userVolumes.count == 0)
  319. {
  320. userView1.totalVolume = 0;
  321. userView2.totalVolume = 0;
  322. userView3.totalVolume = 0;
  323. }
  324. // [self.uiController.micView setUsers:self.users];
  325. }
  326. #pragma mark - ----------------------- 腾讯云 delegate end -----------------------
  327. - (void)joinChannel2 {
  328. // 频道内每个用户的 uid 必须是唯一的
  329. int uid = [IMAPlatform sharedInstance].host.userId.intValue;
  330. [self.trtcCloud startLocalAudio:TRTCAudioQualityMusic];
  331. // 主播: TRTCRoleAnchor
  332. // 观众: TRTCRoleAudience
  333. // [self.trtcCloud switchRole:TRTCRoleAudience];
  334. TRTCParams *params = [[TRTCParams alloc] init];
  335. // 以字符串房间号为例
  336. params.roomId = self.liveInfo.room_id.intValue;
  337. params.userId = [IMAPlatform sharedInstance].host.userId;
  338. // 从业务后台获取到的 UserSig
  339. params.userSig = [IMALoginParam loadFromLocal].userSig;
  340. // 替换成您的 SDKAppID
  341. params.sdkAppId = TXSDKAppID;
  342. // 根据需要指定用户角色
  343. params.role = TRTCRoleAudience;
  344. // 根据需要指定场景
  345. [self.trtcCloud enterRoom:params appScene:TRTCAppSceneLIVE];
  346. }
  347. -(void)updateLianMaiCoin
  348. {
  349. //用户加入
  350. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  351. [parmDict setObject:@"live_multi_room" forKey:@"ctl"];
  352. [parmDict setObject:@"api_mic_list" forKey:@"act"];
  353. [parmDict setObject:[BogoNetwork shareInstance].token forKey:@"token"];
  354. [parmDict setObject:[BogoNetwork shareInstance].uid forKey:@"uid"];
  355. [parmDict setObject:self.liveInfo.room_id forKey:@"video_id"];
  356. [[NetHttpsManager manager] POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  357. {
  358. FWWeakify(self)
  359. if ([responseJson toInt:@"status"] == 1)
  360. {
  361. NSMutableArray *model_arr = [NSMutableArray array];
  362. NSArray *arr = [responseJson valueForKey:@"list"];
  363. if ([arr isKindOfClass:[NSArray class]]) {
  364. for (NSDictionary *dic in arr) {
  365. VoiceLianmaiUserModel *model =[VoiceLianmaiUserModel mj_objectWithKeyValues: dic];
  366. if(model.user_id.intValue == self.liveInfo.user_id.intValue)
  367. {
  368. continue;
  369. }
  370. if(model.user_id.intValue == userView1.uid.intValue)
  371. {
  372. userView1.numberLab.text = model.coin;
  373. }
  374. if(model.user_id.intValue == userView2.uid.intValue)
  375. {
  376. userView2.numberLab.text = model.coin;
  377. }
  378. if(model.user_id.intValue == userView2.uid.intValue)
  379. {
  380. userView3.numberLab.text = model.coin;
  381. }
  382. }
  383. }
  384. }
  385. } FailureBlock:^(NSError *error)
  386. {
  387. }];
  388. }
  389. - (void)addLinkMicPlayItem
  390. {
  391. for (int i = 0; i<2; i++)
  392. {
  393. BGTLinkMicPlayItem* playItem = [[BGTLinkMicPlayItem alloc] init];
  394. playItem.videoView = [[UIView alloc] init];
  395. [self.view addSubview:playItem.videoView];
  396. playItem.livePlayListener = [[TCLivePlayListenerImpl alloc] init];
  397. playItem.livePlayListener.delegate = self;
  398. TXLivePlayConfig * playConfig0 = [[TXLivePlayConfig alloc] init];
  399. playItem.livePlayer = [[TXLivePlayer alloc] init];
  400. playItem.livePlayer.delegate = playItem.livePlayListener;
  401. [playItem.livePlayer setConfig: playConfig0];
  402. [playItem.livePlayer setRenderMode:RENDER_MODE_FILL_EDGE];
  403. playItem.pending = false;
  404. playItem.itemIndex = i;
  405. [playItem emptyPlayInfo];
  406. [_playItemArray addObject:playItem];
  407. }
  408. }
  409. - (void)viewWillAppear:(BOOL)animated
  410. {
  411. [super viewWillAppear:animated];
  412. if (_smallPlayVideoView == nil)
  413. {
  414. _smallPlayVideoView = [[UIView alloc] init];
  415. [self.view addSubview:_smallPlayVideoView];
  416. }
  417. if (_fullScreenVideoView == nil)
  418. {
  419. _fullScreenVideoView = self.videoContrainerView;
  420. }
  421. }
  422. #pragma mark - ----------------------- 开始、停止连麦 -----------------------
  423. #pragma mark 开始连麦
  424. - (void)startVideoLiveLinkMic
  425. {
  426. // if (_isBeingLinkMic || _isWaitingResponse)
  427. // {
  428. //// [self initializeAgoraEngine];
  429. //// [self setChannelProfile];
  430. // [self setClientRole];
  431. // [self setupLocalVideo];
  432. // [self reloduserListView];;
  433. // }
  434. if (_isBeingLinkMic) {
  435. return;
  436. }
  437. _isBeingLinkMic = YES;
  438. [self authorizationCheck];
  439. //结束从CDN拉流
  440. // [self stopRtmp];
  441. // self.videoContrainerViewAgora.hidden = NO;
  442. //1.加入房
  443. // [self initializeAgoraEngine];
  444. // [self setChannelProfile];
  445. // [self setClientRole];
  446. // [self setupLocalVideo];
  447. // [self reloduserListView];
  448. [self joinChannel2];
  449. //2.设置自己的预览图
  450. //3.加载主播视频
  451. //4.加载其他观众视图
  452. [GlobalVariables sharedInstance].isBeingLinkMic = _isBeingLinkMic;
  453. }
  454. #pragma mark 停止连麦
  455. - (void)stopLinkMic
  456. {
  457. _isNeedLoading = YES;
  458. // [self.agoraKit setClientRole:AgoraClientRoleAudience];
  459. [self.trtcCloud switchRole:(TRTCRoleAudience)];
  460. //自己在连麦中,退出房间,关闭连麦
  461. if (self.isLinkingMic) {
  462. self.isLinkingMic = NO;
  463. [BGLiveSDKViewModel tLiveStopMick:self.roomIDStr toUserId:[IMAPlatform sharedInstance].host.userId];
  464. }
  465. if (_isBeingLinkMic)
  466. {
  467. }
  468. }
  469. - (void)startLinkMic:(TLiveMickModel *)mickModel
  470. {
  471. if (!_isBeingLinkMic)
  472. {
  473. _push_rtmp2 = mickModel.push_rtmp2;
  474. _play_rtmp_acc = mickModel.play_rtmp_acc;
  475. [self startVideoLiveLinkMic];
  476. }
  477. }
  478. #pragma mark - //语音连麦窗口
  479. - (void)adjustPlayItemVoiceUserList:(NSArray <VoiceLianmaiUserModel * > *)userlist
  480. {
  481. NSLog(@"刷新语音连麦视图");
  482. NSLog(@"userlist = %@",userlist);
  483. self.userListView.hidden = NO;
  484. userView1.hidden = YES;
  485. userView2.hidden = YES;
  486. userView3.hidden = YES;
  487. // NSMutableArray *needAddLianMaiUser = [NSMutableArray array];//需要连麦的列表
  488. // NSMutableArray *needDelLianMaiUser = [NSMutableArray array];//需要删除的列表
  489. //默认自己没有在连麦中
  490. self.isLinkingMic = NO;
  491. for (int i=0; i<userlist.count; i++) {
  492. if(i == 0)
  493. {
  494. userView1.hidden = NO;
  495. userView1.userName.text = userlist[i].nick_name;
  496. userView1.uid = userlist[i].user_id;
  497. if(userlist[i].user_id.intValue == [IMAPlatform sharedInstance].host.userId.intValue) {
  498. //是自己,连麦中
  499. self.isLinkingMic = YES;
  500. // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
  501. [self.trtcCloud startLocalPreview:YES view:userView1.videoView];
  502. // 主播: TRTCRoleAnchor
  503. // 观众: TRTCRoleAudience
  504. [self.trtcCloud switchRole:TRTCRoleAnchor];
  505. if(userView1.videoBtn.selected == YES)
  506. {
  507. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:NO];
  508. } else {
  509. // [self.agoraKit muteLocalVideoStream:YES];
  510. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:YES];
  511. }
  512. if(userView1.voiceBtn.selected == YES)
  513. {
  514. [self.trtcCloud muteLocalAudio:NO];
  515. } else {
  516. // [self.agoraKit muteLocalAudioStream:YES];
  517. [self.trtcCloud muteLocalAudio:YES];
  518. }
  519. } else {
  520. //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
  521. [self.trtcCloud startRemoteView:userlist[i].user_id streamType:TRTCVideoStreamTypeBig view:userView1.videoView];
  522. }
  523. }
  524. else if(i == 1)
  525. {
  526. userView2.hidden = NO;
  527. userView2.userName.text = userlist[i].nick_name;
  528. userView2.uid = userlist[i].user_id;
  529. if(userlist[i].user_id.intValue == [IMAPlatform sharedInstance].host.userId.intValue)
  530. {
  531. //是自己,连麦中
  532. self.isLinkingMic = YES;
  533. // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
  534. [self.trtcCloud startLocalPreview:YES view:userView2.videoView];
  535. if(userView2.videoBtn.selected == YES)
  536. {
  537. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:NO];
  538. }
  539. else
  540. {
  541. // [self.agoraKit muteLocalVideoStream:YES];
  542. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:YES];
  543. }
  544. if(userView2.voiceBtn.selected == YES)
  545. {
  546. [self.trtcCloud muteLocalAudio:NO];
  547. }
  548. else
  549. {
  550. // [self.agoraKit muteLocalAudioStream:YES];
  551. [self.trtcCloud muteLocalAudio:YES];
  552. }
  553. }
  554. else
  555. {
  556. // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
  557. [self.trtcCloud startLocalPreview:YES view:userView2.videoView];
  558. //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
  559. [self.trtcCloud startRemoteView:userlist[i].user_id streamType:TRTCVideoStreamTypeBig view:userView2.videoView];
  560. }
  561. }
  562. else if(i == 2)
  563. {
  564. userView3.hidden = NO;
  565. userView3.userName.text = userlist[i].nick_name;
  566. userView3.uid = userlist[i].user_id;
  567. if(userlist[i].user_id.intValue == [IMAPlatform sharedInstance].host.userId.intValue) {
  568. //是自己,连麦中
  569. self.isLinkingMic = YES;
  570. // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
  571. [self.trtcCloud startLocalPreview:YES view:userView3.videoView];
  572. if(userView3.videoBtn.selected == YES)
  573. {
  574. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:NO];
  575. } else {
  576. // [self.agoraKit muteLocalVideoStream:YES];
  577. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:YES];
  578. }
  579. if(userView3.voiceBtn.selected == YES)
  580. {
  581. [self.trtcCloud muteLocalAudio:NO];
  582. } else {
  583. // [self.agoraKit muteLocalAudioStream:YES];
  584. [self.trtcCloud muteLocalAudio:YES];
  585. }
  586. } else {
  587. // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
  588. [self.trtcCloud startLocalPreview:YES view:userView3.videoView];
  589. //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
  590. [self.trtcCloud startRemoteView:userlist[i].user_id streamType:TRTCVideoStreamTypeBig view:userView3.videoView];
  591. }
  592. }
  593. }
  594. if (userlist.count <= 0) {
  595. return;
  596. }
  597. NSArray *subView = self.userListView.subviews;
  598. NSLog(@"%@ === %@",subView,self.userListView);
  599. // for (int i=0; i<subView.count; i++) {
  600. // UIView *itemView = subView[i];
  601. // [itemView mas_remakeConstraints:^(MASConstraintMaker *make) {
  602. //
  603. // }];
  604. // }
  605. NSMutableArray *realViewArr = [NSMutableArray array];
  606. for (int i=0; i<subView.count; i++) {
  607. UIView *itemView = subView[i];
  608. if(itemView.hidden == NO)
  609. {
  610. [realViewArr addObject:itemView];
  611. }
  612. }
  613. int userViewWidth = 120;
  614. int userViewHeight = userViewWidth * 1.6;
  615. int sp = kRealValue(3);
  616. [self.userListView mas_remakeConstraints:^(MASConstraintMaker *make) {
  617. make.width.equalTo(@(userViewWidth));
  618. make.height.equalTo(@((userViewHeight + sp) * realViewArr.count ));
  619. make.bottom.equalTo([GlobalVariables sharedInstance].tliveView).offset(-80);
  620. make.right.equalTo([GlobalVariables sharedInstance].tliveView).offset(0);
  621. // make.height.equalTo(@(realViewArr.count * userViewHeight));
  622. }];
  623. if(realViewArr.count > 1)
  624. {
  625. [realViewArr mas_remakeConstraints:^(MASConstraintMaker *make) {
  626. make.height.equalTo(@(userViewHeight));
  627. make.width.equalTo(@(userViewWidth));
  628. }];
  629. [realViewArr mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:userViewHeight leadSpacing:0 tailSpacing:0];
  630. }
  631. else if(realViewArr.count == 1)
  632. {
  633. [realViewArr[0] mas_remakeConstraints:^(MASConstraintMaker *make) {
  634. make.bottom.equalTo(self.userListView);
  635. make.height.equalTo(@(userViewHeight));
  636. make.width.equalTo(@(userViewWidth));
  637. }];
  638. }
  639. }
  640. #pragma mark 调整连麦窗口
  641. - (void)adjustPlayItem:(TLiveMickListModel *)mickListModel
  642. {
  643. }
  644. - (BGTLinkMicPlayItem*)getPlayItemByStreamUrl:(NSString*)streamUrl
  645. {
  646. if (streamUrl)
  647. {
  648. for (BGTLinkMicPlayItem* playItem in _playItemArray)
  649. {
  650. if ([streamUrl isEqualToString:playItem.playUrl])
  651. {
  652. return playItem;
  653. }
  654. }
  655. }
  656. return nil;
  657. }
  658. #pragma mark 请求连麦超时
  659. - (void)onWaitLinkMicResponseTimeOut
  660. {
  661. if (_isWaitingResponse == YES)
  662. {
  663. _isWaitingResponse = NO;
  664. [self toastTip:ASLocalizedString(@"连麦请求超时,主播没有做出回应")];
  665. }
  666. }
  667. #pragma mark 连麦过程出错处理
  668. - (void)handleLinkMicFailed:(NSString*)message
  669. {
  670. [self toastTip:message];
  671. //结束连麦
  672. [self stopLinkMic];
  673. [self startRtmp:self.create_type];
  674. }
  675. - (BOOL)startRtmp:(NSInteger)create_type
  676. {
  677. [super startRtmp:create_type];
  678. if([GlobalVariables sharedInstance].openAgora)
  679. {
  680. // [self initializeAgoraEngine];
  681. [self enterRoomWithTXTRTC];
  682. // [self setClientRole];
  683. // [self setupLocalVideo];
  684. // [self joinChannel];
  685. }
  686. return YES;
  687. }
  688. #pragma mark 权限检查
  689. - (void)authorizationCheck
  690. {
  691. //检查麦克风权限
  692. AVAuthorizationStatus statusAudio = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
  693. if (statusAudio == AVAuthorizationStatusDenied)
  694. {
  695. [self toastTip:ASLocalizedString(@"获取麦克风权限失败,请前往隐私-麦克风设置里面打开应用权限")];
  696. return;
  697. }
  698. //是否有摄像头权限
  699. AVAuthorizationStatus statusVideo = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  700. if (statusVideo == AVAuthorizationStatusDenied)
  701. {
  702. [self toastTip:ASLocalizedString(@"获取摄像头权限失败,请前往隐私-相机设置里面打开应用权限")];
  703. return;
  704. }
  705. if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
  706. {
  707. [self toastTip:ASLocalizedString(@"系统不支持硬编码, 启动连麦失败")];
  708. return;
  709. }
  710. }
  711. #pragma mark - ----------------------- 代理事件 -----------------------
  712. - (void)onReceiveMemberJoinNotify:(NSString*)userID withStreamID:(NSString*)streamID
  713. {
  714. }
  715. - (void)onReceiveMemberExitNotify:(NSString*)userID
  716. {
  717. }
  718. - (void)onLivePushEvent:(NSString*) pushUrl withEvtID:(int)event andParam:(NSDictionary*)param
  719. {
  720. NSLog(@"==========PushEvtID2:%d",event);
  721. if (event == PUSH_EVT_PUSH_BEGIN) // 开始推流事件通知
  722. {
  723. //2.通知主播拉取自己的流
  724. // [_tcLinkMicMgr sendMemberJoinNotify:_liveInfo.userid withJoinerID:profile.identifier andJoinerPlayUrl:_playUrl];
  725. }
  726. else if (event == PUSH_ERR_NET_DISCONNECT)
  727. { //推流失败事件通知
  728. [self handleLinkMicFailed:ASLocalizedString(@"推流失败,结束连麦")];
  729. }
  730. else if (event == PUSH_WARNING_HW_ACCELERATION_FAIL)
  731. {
  732. [self handleLinkMicFailed:ASLocalizedString(@"启动硬编码失败,结束连麦")];
  733. }
  734. }
  735. - (void)onLivePushNetStatus:(NSString*)pushUrl withParam:(NSDictionary*) param
  736. {
  737. [super onNetStatus:param];
  738. }
  739. - (void)onLivePlayEvent:(NSString*)playUrl withEvtID:(int)event andParam:(NSDictionary*)param
  740. {
  741. BGTLinkMicPlayItem *playItem = [self getPlayItemByStreamUrl:playUrl];
  742. if (event == PLAY_EVT_PLAY_BEGIN)
  743. {
  744. if (playItem)
  745. {
  746. [playItem stopLoading];
  747. }
  748. else
  749. {
  750. [self stopLoading];
  751. }
  752. }
  753. else if (event == PLAY_EVT_PLAY_END)
  754. {
  755. if (playItem)
  756. {
  757. [playItem stopLoading];
  758. }
  759. }
  760. else if (event == PLAY_ERR_NET_DISCONNECT)
  761. {
  762. if (playItem)
  763. {
  764. [playItem stopPlay];
  765. [playItem stopLoading];
  766. [playItem emptyPlayInfo];
  767. }
  768. }
  769. else if (event == PLAY_WARNING_HW_ACCELERATION_FAIL)
  770. {
  771. if (playItem)
  772. {
  773. [playItem stopLoading];
  774. }
  775. }
  776. else if (event == PLAY_ERR_GET_RTMP_ACC_URL_FAIL)
  777. {
  778. [playItem reStartPlay];
  779. }
  780. [super onPlayEvent:event withParam:param];
  781. }
  782. - (void)onLivePlayNetStatus:(NSString*)playUrl withParam:(NSDictionary*)param
  783. {
  784. }
  785. #pragma mark - ----------------------- 结束视频 -----------------------
  786. - (void)endVideo
  787. {
  788. [self stopLinkMic];
  789. [super stopRtmp];
  790. }
  791. #pragma mark - ----------------------- 加载动画 -----------------------
  792. - (void)startLoading
  793. {
  794. if (_loadingBackground)
  795. {
  796. _loadingBackground.hidden = NO;
  797. }
  798. if (_loadingImageView)
  799. {
  800. _loadingImageView.hidden = NO;
  801. [_loadingImageView startAnimating];
  802. }
  803. }
  804. - (void)stopLoading
  805. {
  806. if (_loadingBackground)
  807. {
  808. _loadingBackground.hidden = YES;
  809. _loadingBackground = nil;
  810. }
  811. if (_loadingImageView)
  812. {
  813. _loadingImageView.hidden = YES;
  814. [_loadingImageView stopAnimating];
  815. _loadingImageView = nil;
  816. }
  817. }
  818. #pragma mark - ----------------------- 进入前后台 -----------------------
  819. #pragma mark app进入后台
  820. - (void)onAppDidEnterBackGround
  821. {
  822. [super onAppDidEnterBackGround];
  823. if (_isBeingLinkMic)
  824. {
  825. [_txLivePush pausePush];
  826. }
  827. }
  828. #pragma mark app将要进入前台
  829. - (void)onAppWillEnterForeground
  830. {
  831. [super onAppWillEnterForeground];
  832. if (_isBeingLinkMic)
  833. {
  834. [_txLivePush resumePush];
  835. }
  836. }
  837. @end