BGTLinkMicPlayController.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. NSLog(@"onError code: %d message: %@", errCode, errMsg);
  177. if (ERR_CAMERA_NOT_AUTHORIZED == errCode) {
  178. NSString *errorInfo = @"Current application is not authorized to use the camera:";
  179. errorInfo = [errorInfo stringByAppendingString: errMsg];
  180. NSLog(@"%@",errorInfo);
  181. // [self toastTip:errorInfo];
  182. }
  183. }
  184. - (void)onEnterRoom:(NSInteger)result {
  185. if (result > 0) {
  186. // result 代表加入房间所消耗的时间(毫秒)
  187. // [self toastTip:@"Enter room succeed!"];
  188. NSLog(@"加入房间成功");
  189. } else {
  190. // result 代表进房失败的错误码
  191. // [self toastTip:@"Enter room failed!"];
  192. }
  193. }
  194. - (void)onRemoteUserEnterRoom:(NSString *)userId {
  195. if(self.isBeingLinkMic) {
  196. //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
  197. [self.trtcCloud startRemoteView:self.liveInfo.user_id streamType:TRTCVideoStreamTypeBig view:self.videoContrainerViewAgora];
  198. if(userId.intValue != self.liveInfo.user_id.intValue && userId.intValue != [IMAPlatform sharedInstance].host.userId.intValue && ![lianMaiuser containsObject:userId])
  199. {
  200. [lianMaiuser addObject:userId];
  201. // [self reloduserListView];
  202. }
  203. }
  204. else
  205. {
  206. if(userId.intValue == self.liveInfo.user_id.intValue)
  207. {
  208. TRTCRenderParams *params = [[TRTCRenderParams alloc] init];
  209. // 画面镜像模式
  210. params.mirrorType = TRTCVideoMirrorTypeDisable;
  211. // 画面填充模式
  212. params.fillMode = TRTCVideoFillMode_Fill;
  213. // 画面旋转角度
  214. params.rotation = TRTCVideoRotation_0;
  215. // 设置远端画面的渲染模式
  216. [self.trtcCloud setRemoteRenderParams:userId streamType:TRTCVideoStreamTypeBig params:params];
  217. //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
  218. [self.trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeBig view:self.videoContrainerView];
  219. }
  220. else
  221. {
  222. if([GlobalVariables sharedInstance].isBeingPK)
  223. {
  224. //如果正在pk把对方的也加进来
  225. // AgoraRtcVideoCanvas *videoCanvas = [[AgoraRtcVideoCanvas alloc] init];
  226. // videoCanvas.uid = uid;
  227. // videoCanvas.renderMode = AgoraVideoRenderModeHidden;
  228. // videoCanvas.view = self.rightVideoContrainerView;
  229. // // 设置远端视图
  230. // [self.agoraKit setupRemoteVideo:videoCanvas];
  231. TRTCRenderParams *params = [[TRTCRenderParams alloc] init];
  232. // 画面镜像模式
  233. params.mirrorType = TRTCVideoMirrorTypeDisable;
  234. // 画面填充模式
  235. params.fillMode = TRTCVideoFillMode_Fill;
  236. // 画面旋转角度
  237. params.rotation = TRTCVideoRotation_0;
  238. // 设置远端画面的渲染模式
  239. [self.trtcCloud setRemoteRenderParams:userId streamType:TRTCVideoStreamTypeBig params:params];
  240. //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
  241. [self.trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeBig view:self.rightVideoContrainerView];
  242. }
  243. }
  244. }
  245. //用户加入
  246. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  247. [parmDict setObject:@"live_multi_room" forKey:@"ctl"];
  248. [parmDict setObject:@"api_mic_list" forKey:@"act"];
  249. [parmDict setObject:[BogoNetwork shareInstance].token forKey:@"token"];
  250. [parmDict setObject:[BogoNetwork shareInstance].uid forKey:@"uid"];
  251. [parmDict setObject:self.liveInfo.room_id forKey:@"video_id"];
  252. [[NetHttpsManager manager] POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  253. {
  254. FWWeakify(self)
  255. if ([responseJson toInt:@"status"] == 1)
  256. {
  257. NSMutableArray *model_arr = [NSMutableArray array];
  258. NSArray *arr = [responseJson valueForKey:@"list"];
  259. if ([arr isKindOfClass:[NSArray class]]) {
  260. for (NSDictionary *dic in arr) {
  261. VoiceLianmaiUserModel *model =[VoiceLianmaiUserModel mj_objectWithKeyValues: dic];
  262. if(model.user_id.intValue == self.liveInfo.user_id.intValue)
  263. {
  264. continue;
  265. }
  266. [model_arr addObject:model];
  267. }
  268. }
  269. [self adjustPlayItemVoiceUserList:model_arr];
  270. }
  271. } FailureBlock:^(NSError *error)
  272. {
  273. }];
  274. [self updateLianMaiCoin];
  275. }
  276. // 离开房间事件回调
  277. - (void)onExitRoom:(NSInteger)reason {
  278. if (reason == 0) {
  279. NSLog(@"主动调用 exitRoom 退出房间");
  280. } else if (reason == 1) {
  281. NSLog(@"被服务器踢出当前房间");
  282. } else if (reason == 2) {
  283. NSLog(@"当前房间整个被解散");
  284. }
  285. //自己在连麦中,退出房间,关闭连麦
  286. if (self.isLinkingMic) {
  287. [BGLiveSDKViewModel tLiveStopMick:self.roomIDStr toUserId:[IMAPlatform sharedInstance].host.userId];
  288. }
  289. }
  290. - (void)onUserVoiceVolume:(NSArray<TRTCVolumeInfo *> *)userVolumes totalVolume:(NSInteger)totalVolume {
  291. NSLog(@"totalVolume");
  292. NSLog(@"%ld",totalVolume);
  293. /*bug:当a用户在麦上正在说话时,切换到无网状态,此时虽然还显示在麦上(im还没下线),
  294. 但观众席听不到声音,光圈还在闪烁
  295. 原因: 这时观众席speakers 里面找不到a,就不会进入for循环,就不会更新a的声音为0
  296. 解决: 1先找到 麦上的用户 2去判断有无声音
  297. */
  298. for (TRTCVolumeInfo *info in userVolumes) {
  299. NSString *uid = @"";
  300. if (info.userId.intValue == 0) {
  301. uid = [IMAPlatform sharedInstance].host.userId;
  302. }else{
  303. uid = [NSString stringWithFormat:@"%@",info.userId];
  304. }
  305. if([[userView1 uid] isEqualToString:uid])
  306. {
  307. userView1.totalVolume = info.volume;
  308. }
  309. if([[userView2 uid] isEqualToString:uid])
  310. {
  311. userView2.totalVolume = info.volume;
  312. }
  313. if([[userView3 uid] isEqualToString:uid])
  314. {
  315. userView3.totalVolume = info.volume;
  316. }
  317. //speaker.volume
  318. }
  319. if(userVolumes.count == 0)
  320. {
  321. userView1.totalVolume = 0;
  322. userView2.totalVolume = 0;
  323. userView3.totalVolume = 0;
  324. }
  325. // [self.uiController.micView setUsers:self.users];
  326. }
  327. #pragma mark - ----------------------- 腾讯云 delegate end -----------------------
  328. - (void)joinChannel2 {
  329. // 频道内每个用户的 uid 必须是唯一的
  330. int uid = [IMAPlatform sharedInstance].host.userId.intValue;
  331. [self.trtcCloud startLocalAudio:TRTCAudioQualityMusic];
  332. // 主播: TRTCRoleAnchor
  333. // 观众: TRTCRoleAudience
  334. // [self.trtcCloud switchRole:TRTCRoleAudience];
  335. TRTCParams *params = [[TRTCParams alloc] init];
  336. // 以字符串房间号为例
  337. params.roomId = self.liveInfo.room_id.intValue;
  338. params.userId = [IMAPlatform sharedInstance].host.userId;
  339. // 从业务后台获取到的 UserSig
  340. params.userSig = [IMALoginParam loadFromLocal].userSig;
  341. // 替换成您的 SDKAppID
  342. params.sdkAppId = TXSDKAppID;
  343. // 根据需要指定用户角色
  344. params.role = TRTCRoleAudience;
  345. // 根据需要指定场景
  346. [self.trtcCloud enterRoom:params appScene:TRTCAppSceneLIVE];
  347. }
  348. -(void)updateLianMaiCoin
  349. {
  350. //用户加入
  351. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  352. [parmDict setObject:@"live_multi_room" forKey:@"ctl"];
  353. [parmDict setObject:@"api_mic_list" forKey:@"act"];
  354. [parmDict setObject:[BogoNetwork shareInstance].token forKey:@"token"];
  355. [parmDict setObject:[BogoNetwork shareInstance].uid forKey:@"uid"];
  356. [parmDict setObject:self.liveInfo.room_id forKey:@"video_id"];
  357. [[NetHttpsManager manager] POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  358. {
  359. FWWeakify(self)
  360. if ([responseJson toInt:@"status"] == 1)
  361. {
  362. NSMutableArray *model_arr = [NSMutableArray array];
  363. NSArray *arr = [responseJson valueForKey:@"list"];
  364. if ([arr isKindOfClass:[NSArray class]]) {
  365. for (NSDictionary *dic in arr) {
  366. VoiceLianmaiUserModel *model =[VoiceLianmaiUserModel mj_objectWithKeyValues: dic];
  367. if(model.user_id.intValue == self.liveInfo.user_id.intValue)
  368. {
  369. continue;
  370. }
  371. if(model.user_id.intValue == userView1.uid.intValue)
  372. {
  373. userView1.numberLab.text = model.coin;
  374. }
  375. if(model.user_id.intValue == userView2.uid.intValue)
  376. {
  377. userView2.numberLab.text = model.coin;
  378. }
  379. if(model.user_id.intValue == userView2.uid.intValue)
  380. {
  381. userView3.numberLab.text = model.coin;
  382. }
  383. }
  384. }
  385. }
  386. } FailureBlock:^(NSError *error)
  387. {
  388. }];
  389. }
  390. - (void)addLinkMicPlayItem
  391. {
  392. for (int i = 0; i<2; i++)
  393. {
  394. BGTLinkMicPlayItem* playItem = [[BGTLinkMicPlayItem alloc] init];
  395. playItem.videoView = [[UIView alloc] init];
  396. [self.view addSubview:playItem.videoView];
  397. playItem.livePlayListener = [[TCLivePlayListenerImpl alloc] init];
  398. playItem.livePlayListener.delegate = self;
  399. TXLivePlayConfig * playConfig0 = [[TXLivePlayConfig alloc] init];
  400. playItem.livePlayer = [[TXLivePlayer alloc] init];
  401. playItem.livePlayer.delegate = playItem.livePlayListener;
  402. [playItem.livePlayer setConfig: playConfig0];
  403. [playItem.livePlayer setRenderMode:RENDER_MODE_FILL_EDGE];
  404. playItem.pending = false;
  405. playItem.itemIndex = i;
  406. [playItem emptyPlayInfo];
  407. [_playItemArray addObject:playItem];
  408. }
  409. }
  410. - (void)viewWillAppear:(BOOL)animated
  411. {
  412. [super viewWillAppear:animated];
  413. if (_smallPlayVideoView == nil)
  414. {
  415. _smallPlayVideoView = [[UIView alloc] init];
  416. [self.view addSubview:_smallPlayVideoView];
  417. }
  418. if (_fullScreenVideoView == nil)
  419. {
  420. _fullScreenVideoView = self.videoContrainerView;
  421. }
  422. }
  423. #pragma mark - ----------------------- 开始、停止连麦 -----------------------
  424. #pragma mark 开始连麦
  425. - (void)startVideoLiveLinkMic
  426. {
  427. // if (_isBeingLinkMic || _isWaitingResponse)
  428. // {
  429. //// [self initializeAgoraEngine];
  430. //// [self setChannelProfile];
  431. // [self setClientRole];
  432. // [self setupLocalVideo];
  433. // [self reloduserListView];;
  434. // }
  435. if (_isBeingLinkMic) {
  436. return;
  437. }
  438. _isBeingLinkMic = YES;
  439. [self authorizationCheck];
  440. //结束从CDN拉流
  441. // [self stopRtmp];
  442. // self.videoContrainerViewAgora.hidden = NO;
  443. //1.加入房
  444. // [self initializeAgoraEngine];
  445. // [self setChannelProfile];
  446. // [self setClientRole];
  447. // [self setupLocalVideo];
  448. // [self reloduserListView];
  449. [self joinChannel2];
  450. //2.设置自己的预览图
  451. //3.加载主播视频
  452. //4.加载其他观众视图
  453. [GlobalVariables sharedInstance].isBeingLinkMic = _isBeingLinkMic;
  454. }
  455. #pragma mark 停止连麦
  456. - (void)stopLinkMic
  457. {
  458. _isNeedLoading = YES;
  459. // [self.agoraKit setClientRole:AgoraClientRoleAudience];
  460. [self.trtcCloud switchRole:(TRTCRoleAudience)];
  461. //自己在连麦中,退出房间,关闭连麦
  462. if (self.isLinkingMic) {
  463. self.isLinkingMic = NO;
  464. [BGLiveSDKViewModel tLiveStopMick:self.roomIDStr toUserId:[IMAPlatform sharedInstance].host.userId];
  465. }
  466. if (_isBeingLinkMic)
  467. {
  468. }
  469. }
  470. - (void)startLinkMic:(TLiveMickModel *)mickModel
  471. {
  472. if (!_isBeingLinkMic)
  473. {
  474. _push_rtmp2 = mickModel.push_rtmp2;
  475. _play_rtmp_acc = mickModel.play_rtmp_acc;
  476. [self startVideoLiveLinkMic];
  477. }
  478. }
  479. #pragma mark - //语音连麦窗口
  480. - (void)adjustPlayItemVoiceUserList:(NSArray <VoiceLianmaiUserModel * > *)userlist
  481. {
  482. NSLog(@"刷新语音连麦视图");
  483. NSLog(@"userlist = %@",userlist);
  484. self.userListView.hidden = NO;
  485. userView1.hidden = YES;
  486. userView2.hidden = YES;
  487. userView3.hidden = YES;
  488. // NSMutableArray *needAddLianMaiUser = [NSMutableArray array];//需要连麦的列表
  489. // NSMutableArray *needDelLianMaiUser = [NSMutableArray array];//需要删除的列表
  490. //默认自己没有在连麦中
  491. self.isLinkingMic = NO;
  492. for (int i=0; i<userlist.count; i++) {
  493. if(i == 0)
  494. {
  495. userView1.hidden = NO;
  496. userView1.userName.text = userlist[i].nick_name;
  497. userView1.uid = userlist[i].user_id;
  498. if(userlist[i].user_id.intValue == [IMAPlatform sharedInstance].host.userId.intValue) {
  499. //是自己,连麦中
  500. self.isLinkingMic = YES;
  501. // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
  502. [self.trtcCloud startLocalPreview:YES view:userView1.videoView];
  503. // 主播: TRTCRoleAnchor
  504. // 观众: TRTCRoleAudience
  505. [self.trtcCloud switchRole:TRTCRoleAnchor];
  506. if(userView1.videoBtn.selected == YES)
  507. {
  508. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:NO];
  509. } else {
  510. // [self.agoraKit muteLocalVideoStream:YES];
  511. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:YES];
  512. }
  513. if(userView1.voiceBtn.selected == YES)
  514. {
  515. [self.trtcCloud muteLocalAudio:NO];
  516. } else {
  517. // [self.agoraKit muteLocalAudioStream:YES];
  518. [self.trtcCloud muteLocalAudio:YES];
  519. }
  520. } else {
  521. //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
  522. [self.trtcCloud startRemoteView:userlist[i].user_id streamType:TRTCVideoStreamTypeBig view:userView1.videoView];
  523. }
  524. }
  525. else if(i == 1)
  526. {
  527. userView2.hidden = NO;
  528. userView2.userName.text = userlist[i].nick_name;
  529. userView2.uid = userlist[i].user_id;
  530. if(userlist[i].user_id.intValue == [IMAPlatform sharedInstance].host.userId.intValue)
  531. {
  532. //是自己,连麦中
  533. self.isLinkingMic = YES;
  534. // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
  535. [self.trtcCloud startLocalPreview:YES view:userView2.videoView];
  536. if(userView2.videoBtn.selected == YES)
  537. {
  538. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:NO];
  539. }
  540. else
  541. {
  542. // [self.agoraKit muteLocalVideoStream:YES];
  543. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:YES];
  544. }
  545. if(userView2.voiceBtn.selected == YES)
  546. {
  547. [self.trtcCloud muteLocalAudio:NO];
  548. }
  549. else
  550. {
  551. // [self.agoraKit muteLocalAudioStream:YES];
  552. [self.trtcCloud muteLocalAudio:YES];
  553. }
  554. }
  555. else
  556. {
  557. // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
  558. [self.trtcCloud startLocalPreview:YES view:userView2.videoView];
  559. //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
  560. [self.trtcCloud startRemoteView:userlist[i].user_id streamType:TRTCVideoStreamTypeBig view:userView2.videoView];
  561. }
  562. }
  563. else if(i == 2)
  564. {
  565. userView3.hidden = NO;
  566. userView3.userName.text = userlist[i].nick_name;
  567. userView3.uid = userlist[i].user_id;
  568. if(userlist[i].user_id.intValue == [IMAPlatform sharedInstance].host.userId.intValue) {
  569. //是自己,连麦中
  570. self.isLinkingMic = YES;
  571. // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
  572. [self.trtcCloud startLocalPreview:YES view:userView3.videoView];
  573. if(userView3.videoBtn.selected == YES)
  574. {
  575. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:NO];
  576. } else {
  577. // [self.agoraKit muteLocalVideoStream:YES];
  578. [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:YES];
  579. }
  580. if(userView3.voiceBtn.selected == YES)
  581. {
  582. [self.trtcCloud muteLocalAudio:NO];
  583. } else {
  584. // [self.agoraKit muteLocalAudioStream:YES];
  585. [self.trtcCloud muteLocalAudio:YES];
  586. }
  587. } else {
  588. // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
  589. [self.trtcCloud startLocalPreview:YES view:userView3.videoView];
  590. //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
  591. [self.trtcCloud startRemoteView:userlist[i].user_id streamType:TRTCVideoStreamTypeBig view:userView3.videoView];
  592. }
  593. }
  594. }
  595. if (userlist.count <= 0) {
  596. return;
  597. }
  598. NSArray *subView = self.userListView.subviews;
  599. NSLog(@"%@ === %@",subView,self.userListView);
  600. // for (int i=0; i<subView.count; i++) {
  601. // UIView *itemView = subView[i];
  602. // [itemView mas_remakeConstraints:^(MASConstraintMaker *make) {
  603. //
  604. // }];
  605. // }
  606. NSMutableArray *realViewArr = [NSMutableArray array];
  607. for (int i=0; i<subView.count; i++) {
  608. UIView *itemView = subView[i];
  609. if(itemView.hidden == NO)
  610. {
  611. [realViewArr addObject:itemView];
  612. }
  613. }
  614. int userViewWidth = 120;
  615. int userViewHeight = userViewWidth * 1.6;
  616. int sp = kRealValue(3);
  617. [self.userListView mas_remakeConstraints:^(MASConstraintMaker *make) {
  618. make.width.equalTo(@(userViewWidth));
  619. make.height.equalTo(@((userViewHeight + sp) * realViewArr.count ));
  620. make.bottom.equalTo([GlobalVariables sharedInstance].tliveView).offset(-80);
  621. make.right.equalTo([GlobalVariables sharedInstance].tliveView).offset(0);
  622. // make.height.equalTo(@(realViewArr.count * userViewHeight));
  623. }];
  624. if(realViewArr.count > 1)
  625. {
  626. [realViewArr mas_remakeConstraints:^(MASConstraintMaker *make) {
  627. make.height.equalTo(@(userViewHeight));
  628. make.width.equalTo(@(userViewWidth));
  629. }];
  630. [realViewArr mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:userViewHeight leadSpacing:0 tailSpacing:0];
  631. }
  632. else if(realViewArr.count == 1)
  633. {
  634. [realViewArr[0] mas_remakeConstraints:^(MASConstraintMaker *make) {
  635. make.bottom.equalTo(self.userListView);
  636. make.height.equalTo(@(userViewHeight));
  637. make.width.equalTo(@(userViewWidth));
  638. }];
  639. }
  640. }
  641. #pragma mark 调整连麦窗口
  642. - (void)adjustPlayItem:(TLiveMickListModel *)mickListModel
  643. {
  644. }
  645. - (BGTLinkMicPlayItem*)getPlayItemByStreamUrl:(NSString*)streamUrl
  646. {
  647. if (streamUrl)
  648. {
  649. for (BGTLinkMicPlayItem* playItem in _playItemArray)
  650. {
  651. if ([streamUrl isEqualToString:playItem.playUrl])
  652. {
  653. return playItem;
  654. }
  655. }
  656. }
  657. return nil;
  658. }
  659. #pragma mark 请求连麦超时
  660. - (void)onWaitLinkMicResponseTimeOut
  661. {
  662. if (_isWaitingResponse == YES)
  663. {
  664. _isWaitingResponse = NO;
  665. [self toastTip:ASLocalizedString(@"连麦请求超时,主播没有做出回应")];
  666. }
  667. }
  668. #pragma mark 连麦过程出错处理
  669. - (void)handleLinkMicFailed:(NSString*)message
  670. {
  671. [self toastTip:message];
  672. //结束连麦
  673. [self stopLinkMic];
  674. [self startRtmp:self.create_type];
  675. }
  676. - (BOOL)startRtmp:(NSInteger)create_type
  677. {
  678. [super startRtmp:create_type];
  679. if([GlobalVariables sharedInstance].openAgora)
  680. {
  681. // [self initializeAgoraEngine];
  682. [self enterRoomWithTXTRTC];
  683. // [self setClientRole];
  684. // [self setupLocalVideo];
  685. // [self joinChannel];
  686. }
  687. return YES;
  688. }
  689. #pragma mark 权限检查
  690. - (void)authorizationCheck
  691. {
  692. //检查麦克风权限
  693. AVAuthorizationStatus statusAudio = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
  694. if (statusAudio == AVAuthorizationStatusDenied)
  695. {
  696. [self toastTip:ASLocalizedString(@"获取麦克风权限失败,请前往隐私-麦克风设置里面打开应用权限")];
  697. return;
  698. }
  699. //是否有摄像头权限
  700. AVAuthorizationStatus statusVideo = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  701. if (statusVideo == AVAuthorizationStatusDenied)
  702. {
  703. [self toastTip:ASLocalizedString(@"获取摄像头权限失败,请前往隐私-相机设置里面打开应用权限")];
  704. return;
  705. }
  706. if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
  707. {
  708. [self toastTip:ASLocalizedString(@"系统不支持硬编码, 启动连麦失败")];
  709. return;
  710. }
  711. }
  712. #pragma mark - ----------------------- 代理事件 -----------------------
  713. - (void)onReceiveMemberJoinNotify:(NSString*)userID withStreamID:(NSString*)streamID
  714. {
  715. }
  716. - (void)onReceiveMemberExitNotify:(NSString*)userID
  717. {
  718. }
  719. - (void)onLivePushEvent:(NSString*) pushUrl withEvtID:(int)event andParam:(NSDictionary*)param
  720. {
  721. NSLog(@"==========PushEvtID2:%d",event);
  722. if (event == PUSH_EVT_PUSH_BEGIN) // 开始推流事件通知
  723. {
  724. //2.通知主播拉取自己的流
  725. // [_tcLinkMicMgr sendMemberJoinNotify:_liveInfo.userid withJoinerID:profile.identifier andJoinerPlayUrl:_playUrl];
  726. }
  727. else if (event == PUSH_ERR_NET_DISCONNECT)
  728. { //推流失败事件通知
  729. [self handleLinkMicFailed:ASLocalizedString(@"推流失败,结束连麦")];
  730. }
  731. else if (event == PUSH_WARNING_HW_ACCELERATION_FAIL)
  732. {
  733. [self handleLinkMicFailed:ASLocalizedString(@"启动硬编码失败,结束连麦")];
  734. }
  735. }
  736. - (void)onLivePushNetStatus:(NSString*)pushUrl withParam:(NSDictionary*) param
  737. {
  738. [super onNetStatus:param];
  739. }
  740. - (void)onLivePlayEvent:(NSString*)playUrl withEvtID:(int)event andParam:(NSDictionary*)param
  741. {
  742. BGTLinkMicPlayItem *playItem = [self getPlayItemByStreamUrl:playUrl];
  743. if (event == PLAY_EVT_PLAY_BEGIN)
  744. {
  745. if (playItem)
  746. {
  747. [playItem stopLoading];
  748. }
  749. else
  750. {
  751. [self stopLoading];
  752. }
  753. }
  754. else if (event == PLAY_EVT_PLAY_END)
  755. {
  756. if (playItem)
  757. {
  758. [playItem stopLoading];
  759. }
  760. }
  761. else if (event == PLAY_ERR_NET_DISCONNECT)
  762. {
  763. if (playItem)
  764. {
  765. [playItem stopPlay];
  766. [playItem stopLoading];
  767. [playItem emptyPlayInfo];
  768. }
  769. }
  770. else if (event == PLAY_WARNING_HW_ACCELERATION_FAIL)
  771. {
  772. if (playItem)
  773. {
  774. [playItem stopLoading];
  775. }
  776. }
  777. else if (event == PLAY_ERR_GET_RTMP_ACC_URL_FAIL)
  778. {
  779. [playItem reStartPlay];
  780. }
  781. [super onPlayEvent:event withParam:param];
  782. }
  783. - (void)onLivePlayNetStatus:(NSString*)playUrl withParam:(NSDictionary*)param
  784. {
  785. }
  786. #pragma mark - ----------------------- 结束视频 -----------------------
  787. - (void)endVideo
  788. {
  789. [self stopLinkMic];
  790. [super stopRtmp];
  791. }
  792. #pragma mark - ----------------------- 加载动画 -----------------------
  793. - (void)startLoading
  794. {
  795. if (_loadingBackground)
  796. {
  797. _loadingBackground.hidden = NO;
  798. }
  799. if (_loadingImageView)
  800. {
  801. _loadingImageView.hidden = NO;
  802. [_loadingImageView startAnimating];
  803. }
  804. }
  805. - (void)stopLoading
  806. {
  807. if (_loadingBackground)
  808. {
  809. _loadingBackground.hidden = YES;
  810. _loadingBackground = nil;
  811. }
  812. if (_loadingImageView)
  813. {
  814. _loadingImageView.hidden = YES;
  815. [_loadingImageView stopAnimating];
  816. _loadingImageView = nil;
  817. }
  818. }
  819. #pragma mark - ----------------------- 进入前后台 -----------------------
  820. #pragma mark app进入后台
  821. - (void)onAppDidEnterBackGround
  822. {
  823. [super onAppDidEnterBackGround];
  824. if (_isBeingLinkMic)
  825. {
  826. [_txLivePush pausePush];
  827. }
  828. }
  829. #pragma mark app将要进入前台
  830. - (void)onAppWillEnterForeground
  831. {
  832. [super onAppWillEnterForeground];
  833. if (_isBeingLinkMic)
  834. {
  835. [_txLivePush resumePush];
  836. }
  837. }
  838. @end