| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035 |
- //
- // BGTLinkMicPlayController.m
- // BuguLive
- //
- // Created by xfg on 16/12/5.
- // Copyright © 2016年 xfg. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "BGTLinkMicPlayController.h"
- #import "UserView.h"
- #import "VoiceLianmaiUserModel.h"
- #import "VoiceLianmaiView.h"
- @implementation TCLivePushListenerImpl
- {
- }
- - (void)onPushEvent:(int)evtID withParam:(NSDictionary*)param
- {
- if (self.delegate)
- {
- [self.delegate onLivePushEvent:self.pushUrl withEvtID:evtID andParam:param];
- }
- }
- - (void)onNetStatus:(NSDictionary*) param
- {
- if (self.delegate)
- {
- [self.delegate onLivePushNetStatus:self.pushUrl withParam:param];
- }
- }
- @end
- @interface BGTLinkMicPlayController()<ITCLivePushListener, ITCLivePlayListener,UserViewDelegate,TRTCCloudDelegate>
- {
-
- BOOL _isNeedLoading; // 是否需要展示加载指示器
-
- UIView* _smallPlayVideoView; // 小主播预览窗口
- UIView* _fullScreenVideoView; // 大主播全屏窗口
-
- UIView* _loadingBackground; // 加载中的背景
- UIImageView * _loadingImageView; // 加载中的背景图
-
- TCLivePushListenerImpl* _txLivePushListener; // 小主播推流监听
- TXLivePushConfig * _txLivePushConfig; // 小主播推流配置
-
- TCLivePlayListenerImpl* _txLivePlayListener; // 拉流监听
- TXLivePlayConfig * _txLivePlayConfig; // 拉流配置
- NSMutableArray <NSString *> *lianMaiuser;
-
- UserView *userView1;
- UserView *userView2;
- UserView *userView3;
- }
- @property (nonatomic,strong) UIView *userListView;
- @property(nonatomic, assign) BOOL isBeingLinkMic; // 是否正在连麦中
- @end
- @implementation BGTLinkMicPlayController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // _lianmaiOpenSound = YES;
-
- //声网dev
- lianMaiuser = [NSMutableArray array];
- _isBeingLinkMic = false;
- [GlobalVariables sharedInstance].isBeingLinkMic = _isBeingLinkMic;
- _isWaitingResponse = false;
- _isNeedLoading = YES;
-
- _txLivePlayListener = [[TCLivePlayListenerImpl alloc] init];
- _txLivePlayListener.delegate = self;
- _txLivePlayConfig = [[TXLivePlayConfig alloc] init];
-
- _playItemArray = [NSMutableArray array];
- _linkMemeberSet = [NSMutableSet set];
-
- self.userListView.hidden = YES;
- userView1.hidden = YES;
- userView2.hidden = YES;
- userView3.hidden = YES;
-
- userView1.userInteractionEnabled = YES;
- userView2.userInteractionEnabled = YES;
- userView3.userInteractionEnabled = YES;
-
- // [self reloduserListView];
-
- [self addLinkMicPlayItem];
- }
- - (UIView *)userListView {
- if (!_userListView) {
- int userViewWidth = 80;
- int userViewHeight = userViewWidth * 1.5;
-
- _userListView = [[UIView alloc] initWithFrame:CGRectMake(kScreenW - userViewWidth - 10, kScreenH - userViewHeight*3 - 90, 0, 0)];
- _userListView.backgroundColor = kClearColor;
-
- [[GlobalVariables sharedInstance].tliveView addSubview:_userListView];
- userView1 = [UserView getView];
- userView1.delegate = self;
- userView1.frame = CGRectMake(0, 0, userViewWidth, userViewHeight);
- userView1.backgroundColor = kRedColor;
- [_userListView addSubview:userView1];
-
- userView2 = [UserView getView];
- userView2.delegate = self;
- userView2.frame = CGRectMake(0, 0, userViewWidth, userViewHeight);
- userView2.backgroundColor = kYellowColor;
- [_userListView addSubview:userView2];
-
- userView3 = [UserView getView];
- userView3.delegate = self;
- userView3.frame = CGRectMake(0, 0, userViewWidth, userViewHeight);
- [_userListView addSubview:userView3];
- }
- return _userListView;
- }
- //点击视频或者音频代理
- - (void)clickVideoBtn:(UserView *)view
- {
- //如果按钮当前select NO,则要下一步则要关闭视频
- if(view.videoBtn.selected == NO)
- {
- [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:YES];
- }
- else
- {
- [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:NO];
- }
- NSLog(@"点击了视频");
- }
- - (void)clickVoiceBtn:(UserView *)view
- {
- //如果按钮当前select NO,则要下一步则要关闭音频
- if(view.voiceBtn.selected == NO)
- {
- [self.trtcCloud muteLocalAudio:YES];
- }
- else
- {
- [self.trtcCloud muteLocalAudio:NO];
- }
-
- NSLog(@"点击了音频");
- }
- - (void)clickUserView:(UserView *)view
- {
- [userView1 setSelect:NO];
- [userView2 setSelect:NO];
- [userView3 setSelect:NO];
-
-
- // post notification
- // NSDictionary * userInfo = [NSDictionary dictionaryWithObject:view.uid forKey:@"uid"];
- // [[NSNotificationCenter defaultCenter] postNotificationName:@"showGiftView" object:self userInfo:userInfo];
-
-
- [view setSelect:YES];
- }
- #pragma mark - ----------------------- 腾讯云 delegate -----------------------
- - (void)enterRoomWithTXTRTC {
- // 创建 SDK 实例(单例模式)并设置事件监听器
- // Create trtc instance(singleton) and set up event listeners
- _trtcCloud = [TRTCCloud sharedInstance];
- [_trtcCloud addDelegate:self];
-
- // [self.trtcCloud switchRole:TRTCRoleAnchor];
-
-
- [self.trtcCloud startLocalAudio:TRTCAudioQualityMusic];
- // [self.trtcCloud switchRole:TRTCRoleAudience];
-
- TRTCParams *params = [[TRTCParams alloc] init];
- // 以字符串房间号为例
- params.roomId = self.roomIDStr.intValue;
- params.userId = [IMAPlatform sharedInstance].host.userId;
- // 从业务后台获取到的 UserSig
- params.userSig = [IMALoginParam loadFromLocal].userSig;
- // 替换成您的 SDKAppID
- params.sdkAppId = TXSDKAppID;
- // 根据需要指定用户角色
- params.role = TRTCRoleAudience;
-
- if (self.liveInfo.user_id.intValue == [IMAPlatform sharedInstance].host.userId.intValue) {
- params.role = TRTCRoleAnchor;
- }
-
- // 根据需要指定场景
- [self.trtcCloud enterRoom:params appScene:TRTCAppSceneLIVE];
-
- self.trtcCloud = [TRTCCloud sharedInstance];
- // 将 denny 的主路画面切换到一个悬浮的小窗口中(假如该迷你小窗口为 miniFloatingView)
- // [self.trtcCloud updateRemoteView:miniFloatingView streamType:TRTCVideoStreamTypeBig forUser:@"denny"];
- // 将远端用户 denny 的主路画面设置为填充模式,并开启左右镜像模式
- TRTCRenderParams *param = [[TRTCRenderParams alloc] init];
- param.fillMode = TRTCVideoFillMode_Fill;
- param.mirrorType = TRTCVideoMirrorTypeDisable;
- // 播放 denny 的摄像头画面(我们称之为“主路画面”)
- [self.trtcCloud startRemoteView:self.liveInfo.user_id streamType:TRTCVideoStreamTypeBig view:_fullScreenVideoView];
- }
- //我们可以让自己的类继承TRTCCloudDelegate,并重载 onError 函数,最后将 this 指针通过 TRTCCCloud 的 delegate 属性设置给 SDK,就可以在当前类中监听来自 SDK 的回调事件了。
- // 监听 SDK 的事件,并对“摄像头未被授权”等错误进行日志打印
- - (void)onError:(TXLiteAVError)errCode
- errMsg:(nullable NSString *)errMsg
- extInfo:(nullable NSDictionary *)extInfo{
-
- NSLog(@"onError code: %d message: %@", errCode, errMsg);
- if (ERR_CAMERA_NOT_AUTHORIZED == errCode) {
- NSString *errorInfo = @"Current application is not authorized to use the camera:";
- errorInfo = [errorInfo stringByAppendingString: errMsg];
- NSLog(@"%@",errorInfo);
- // [self toastTip:errorInfo];
- }
- }
- - (void)onEnterRoom:(NSInteger)result {
- if (result > 0) {
- // result 代表加入房间所消耗的时间(毫秒)
- // [self toastTip:@"Enter room succeed!"];
- NSLog(@"加入房间成功");
- } else {
- // result 代表进房失败的错误码
- // [self toastTip:@"Enter room failed!"];
- }
- }
- - (void)onRemoteUserEnterRoom:(NSString *)userId {
-
- if(self.isBeingLinkMic) {
-
- //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
- [self.trtcCloud startRemoteView:self.liveInfo.user_id streamType:TRTCVideoStreamTypeBig view:self.videoContrainerViewAgora];
-
- if(userId.intValue != self.liveInfo.user_id.intValue && userId.intValue != [IMAPlatform sharedInstance].host.userId.intValue && ![lianMaiuser containsObject:userId])
- {
- [lianMaiuser addObject:userId];
- // [self reloduserListView];
- }
- }
- else
- {
- if(userId.intValue == self.liveInfo.user_id.intValue)
- {
-
- TRTCRenderParams *params = [[TRTCRenderParams alloc] init];
- // 画面镜像模式
- params.mirrorType = TRTCVideoMirrorTypeDisable;
- // 画面填充模式
- params.fillMode = TRTCVideoFillMode_Fill;
- // 画面旋转角度
- params.rotation = TRTCVideoRotation_0;
- // 设置远端画面的渲染模式
- [self.trtcCloud setRemoteRenderParams:userId streamType:TRTCVideoStreamTypeBig params:params];
-
- //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
- [self.trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeBig view:self.videoContrainerView];
- }
- else
- {
- if([GlobalVariables sharedInstance].isBeingPK)
- {
- //如果正在pk把对方的也加进来
- // AgoraRtcVideoCanvas *videoCanvas = [[AgoraRtcVideoCanvas alloc] init];
- // videoCanvas.uid = uid;
- // videoCanvas.renderMode = AgoraVideoRenderModeHidden;
- // videoCanvas.view = self.rightVideoContrainerView;
- // // 设置远端视图
- // [self.agoraKit setupRemoteVideo:videoCanvas];
-
- TRTCRenderParams *params = [[TRTCRenderParams alloc] init];
- // 画面镜像模式
- params.mirrorType = TRTCVideoMirrorTypeDisable;
- // 画面填充模式
- params.fillMode = TRTCVideoFillMode_Fill;
- // 画面旋转角度
- params.rotation = TRTCVideoRotation_0;
- // 设置远端画面的渲染模式
- [self.trtcCloud setRemoteRenderParams:userId streamType:TRTCVideoStreamTypeBig params:params];
-
- //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
- [self.trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeBig view:self.rightVideoContrainerView];
- }
- }
- }
- //用户加入
- NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
- [parmDict setObject:@"live_multi_room" forKey:@"ctl"];
- [parmDict setObject:@"api_mic_list" forKey:@"act"];
- [parmDict setObject:[BogoNetwork shareInstance].token forKey:@"token"];
- [parmDict setObject:[BogoNetwork shareInstance].uid forKey:@"uid"];
- [parmDict setObject:self.liveInfo.room_id forKey:@"video_id"];
- [[NetHttpsManager manager] POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
- {
- FWWeakify(self)
- if ([responseJson toInt:@"status"] == 1)
- {
- NSMutableArray *model_arr = [NSMutableArray array];
-
- NSArray *arr = [responseJson valueForKey:@"list"];
- if ([arr isKindOfClass:[NSArray class]]) {
- for (NSDictionary *dic in arr) {
- VoiceLianmaiUserModel *model =[VoiceLianmaiUserModel mj_objectWithKeyValues: dic];
- if(model.user_id.intValue == self.liveInfo.user_id.intValue)
- {
- continue;
- }
- [model_arr addObject:model];
- }
- }
- [self adjustPlayItemVoiceUserList:model_arr];
- }
- } FailureBlock:^(NSError *error)
- {
- }];
-
- [self updateLianMaiCoin];
- }
- // 离开房间事件回调
- - (void)onExitRoom:(NSInteger)reason {
- if (reason == 0) {
- NSLog(@"主动调用 exitRoom 退出房间");
- } else if (reason == 1) {
- NSLog(@"被服务器踢出当前房间");
- } else if (reason == 2) {
- NSLog(@"当前房间整个被解散");
- }
-
- //自己在连麦中,退出房间,关闭连麦
- if (self.isLinkingMic) {
- [BGLiveSDKViewModel tLiveStopMick:self.roomIDStr toUserId:[IMAPlatform sharedInstance].host.userId];
- }
- }
- - (void)onUserVoiceVolume:(NSArray<TRTCVolumeInfo *> *)userVolumes totalVolume:(NSInteger)totalVolume {
-
- NSLog(@"totalVolume");
- NSLog(@"%ld",totalVolume);
- /*bug:当a用户在麦上正在说话时,切换到无网状态,此时虽然还显示在麦上(im还没下线),
- 但观众席听不到声音,光圈还在闪烁
- 原因: 这时观众席speakers 里面找不到a,就不会进入for循环,就不会更新a的声音为0
- 解决: 1先找到 麦上的用户 2去判断有无声音
- */
-
- for (TRTCVolumeInfo *info in userVolumes) {
- NSString *uid = @"";
- if (info.userId.intValue == 0) {
- uid = [IMAPlatform sharedInstance].host.userId;
-
- }else{
- uid = [NSString stringWithFormat:@"%@",info.userId];
-
- }
-
- if([[userView1 uid] isEqualToString:uid])
- {
- userView1.totalVolume = info.volume;
- }
-
- if([[userView2 uid] isEqualToString:uid])
- {
- userView2.totalVolume = info.volume;
- }
-
- if([[userView3 uid] isEqualToString:uid])
- {
- userView3.totalVolume = info.volume;
- }
-
-
- //speaker.volume
- }
-
- if(userVolumes.count == 0)
- {
- userView1.totalVolume = 0;
- userView2.totalVolume = 0;
- userView3.totalVolume = 0;
- }
- // [self.uiController.micView setUsers:self.users];
- }
- #pragma mark - ----------------------- 腾讯云 delegate end -----------------------
- - (void)joinChannel2 {
- // 频道内每个用户的 uid 必须是唯一的
- int uid = [IMAPlatform sharedInstance].host.userId.intValue;
- [self.trtcCloud startLocalAudio:TRTCAudioQualityMusic];
- // 主播: TRTCRoleAnchor
- // 观众: TRTCRoleAudience
- // [self.trtcCloud switchRole:TRTCRoleAudience];
-
- TRTCParams *params = [[TRTCParams alloc] init];
- // 以字符串房间号为例
- params.roomId = self.liveInfo.room_id.intValue;
- params.userId = [IMAPlatform sharedInstance].host.userId;
- // 从业务后台获取到的 UserSig
- params.userSig = [IMALoginParam loadFromLocal].userSig;
- // 替换成您的 SDKAppID
- params.sdkAppId = TXSDKAppID;
- // 根据需要指定用户角色
- params.role = TRTCRoleAudience;
- // 根据需要指定场景
- [self.trtcCloud enterRoom:params appScene:TRTCAppSceneLIVE];
- }
- -(void)updateLianMaiCoin
- {
- //用户加入
- NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
- [parmDict setObject:@"live_multi_room" forKey:@"ctl"];
- [parmDict setObject:@"api_mic_list" forKey:@"act"];
- [parmDict setObject:[BogoNetwork shareInstance].token forKey:@"token"];
- [parmDict setObject:[BogoNetwork shareInstance].uid forKey:@"uid"];
- [parmDict setObject:self.liveInfo.room_id forKey:@"video_id"];
- [[NetHttpsManager manager] POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
- {
- FWWeakify(self)
- if ([responseJson toInt:@"status"] == 1)
- {
- NSMutableArray *model_arr = [NSMutableArray array];
-
- NSArray *arr = [responseJson valueForKey:@"list"];
- if ([arr isKindOfClass:[NSArray class]]) {
- for (NSDictionary *dic in arr) {
- VoiceLianmaiUserModel *model =[VoiceLianmaiUserModel mj_objectWithKeyValues: dic];
- if(model.user_id.intValue == self.liveInfo.user_id.intValue)
- {
- continue;
- }
-
- if(model.user_id.intValue == userView1.uid.intValue)
- {
- userView1.numberLab.text = model.coin;
- }
-
- if(model.user_id.intValue == userView2.uid.intValue)
- {
- userView2.numberLab.text = model.coin;
- }
-
- if(model.user_id.intValue == userView2.uid.intValue)
- {
- userView3.numberLab.text = model.coin;
- }
- }
- }
- }
- } FailureBlock:^(NSError *error)
- {
- }];
- }
- - (void)addLinkMicPlayItem
- {
- for (int i = 0; i<2; i++)
- {
- BGTLinkMicPlayItem* playItem = [[BGTLinkMicPlayItem alloc] init];
- playItem.videoView = [[UIView alloc] init];
- [self.view addSubview:playItem.videoView];
-
- playItem.livePlayListener = [[TCLivePlayListenerImpl alloc] init];
- playItem.livePlayListener.delegate = self;
- TXLivePlayConfig * playConfig0 = [[TXLivePlayConfig alloc] init];
- playItem.livePlayer = [[TXLivePlayer alloc] init];
- playItem.livePlayer.delegate = playItem.livePlayListener;
- [playItem.livePlayer setConfig: playConfig0];
- [playItem.livePlayer setRenderMode:RENDER_MODE_FILL_EDGE];
- playItem.pending = false;
- playItem.itemIndex = i;
- [playItem emptyPlayInfo];
-
- [_playItemArray addObject:playItem];
- }
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
-
- if (_smallPlayVideoView == nil)
- {
- _smallPlayVideoView = [[UIView alloc] init];
- [self.view addSubview:_smallPlayVideoView];
- }
-
- if (_fullScreenVideoView == nil)
- {
- _fullScreenVideoView = self.videoContrainerView;
- }
- }
- #pragma mark - ----------------------- 开始、停止连麦 -----------------------
- #pragma mark 开始连麦
- - (void)startVideoLiveLinkMic
- {
- // if (_isBeingLinkMic || _isWaitingResponse)
- // {
- //// [self initializeAgoraEngine];
- //// [self setChannelProfile];
- // [self setClientRole];
- // [self setupLocalVideo];
- // [self reloduserListView];;
- // }
-
- if (_isBeingLinkMic) {
- return;
- }
- _isBeingLinkMic = YES;
-
- [self authorizationCheck];
-
- //结束从CDN拉流
- // [self stopRtmp];
-
-
- // self.videoContrainerViewAgora.hidden = NO;
- //1.加入房
- // [self initializeAgoraEngine];
- // [self setChannelProfile];
- // [self setClientRole];
- // [self setupLocalVideo];
- // [self reloduserListView];
- [self joinChannel2];
- //2.设置自己的预览图
- //3.加载主播视频
- //4.加载其他观众视图
-
- [GlobalVariables sharedInstance].isBeingLinkMic = _isBeingLinkMic;
-
- }
- #pragma mark 停止连麦
- - (void)stopLinkMic
- {
- _isNeedLoading = YES;
- // [self.agoraKit setClientRole:AgoraClientRoleAudience];
- [self.trtcCloud switchRole:(TRTCRoleAudience)];
-
- //自己在连麦中,退出房间,关闭连麦
- if (self.isLinkingMic) {
- self.isLinkingMic = NO;
- [BGLiveSDKViewModel tLiveStopMick:self.roomIDStr toUserId:[IMAPlatform sharedInstance].host.userId];
- }
- if (_isBeingLinkMic)
- {
-
- }
- }
- - (void)startLinkMic:(TLiveMickModel *)mickModel
- {
- if (!_isBeingLinkMic)
- {
- _push_rtmp2 = mickModel.push_rtmp2;
- _play_rtmp_acc = mickModel.play_rtmp_acc;
- [self startVideoLiveLinkMic];
- }
- }
- #pragma mark - //语音连麦窗口
- - (void)adjustPlayItemVoiceUserList:(NSArray <VoiceLianmaiUserModel * > *)userlist
- {
- NSLog(@"刷新语音连麦视图");
- NSLog(@"userlist = %@",userlist);
- self.userListView.hidden = NO;
- userView1.hidden = YES;
- userView2.hidden = YES;
- userView3.hidden = YES;
-
- // NSMutableArray *needAddLianMaiUser = [NSMutableArray array];//需要连麦的列表
- // NSMutableArray *needDelLianMaiUser = [NSMutableArray array];//需要删除的列表
-
- //默认自己没有在连麦中
- self.isLinkingMic = NO;
-
- for (int i=0; i<userlist.count; i++) {
- if(i == 0)
- {
- userView1.hidden = NO;
- userView1.userName.text = userlist[i].nick_name;
- userView1.uid = userlist[i].user_id;
-
- if(userlist[i].user_id.intValue == [IMAPlatform sharedInstance].host.userId.intValue) {
- //是自己,连麦中
- self.isLinkingMic = YES;
-
- // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
- [self.trtcCloud startLocalPreview:YES view:userView1.videoView];
-
- // 主播: TRTCRoleAnchor
- // 观众: TRTCRoleAudience
- [self.trtcCloud switchRole:TRTCRoleAnchor];
-
- if(userView1.videoBtn.selected == YES)
- {
- [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:NO];
- } else {
- // [self.agoraKit muteLocalVideoStream:YES];
- [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:YES];
- }
-
- if(userView1.voiceBtn.selected == YES)
- {
- [self.trtcCloud muteLocalAudio:NO];
- } else {
- // [self.agoraKit muteLocalAudioStream:YES];
- [self.trtcCloud muteLocalAudio:YES];
- }
- } else {
- //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
- [self.trtcCloud startRemoteView:userlist[i].user_id streamType:TRTCVideoStreamTypeBig view:userView1.videoView];
- }
- }
-
- else if(i == 1)
- {
- userView2.hidden = NO;
-
- userView2.userName.text = userlist[i].nick_name;
- userView2.uid = userlist[i].user_id;
- if(userlist[i].user_id.intValue == [IMAPlatform sharedInstance].host.userId.intValue)
- {
- //是自己,连麦中
- self.isLinkingMic = YES;
-
- // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
- [self.trtcCloud startLocalPreview:YES view:userView2.videoView];
-
-
- if(userView2.videoBtn.selected == YES)
- {
- [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:NO];
- }
- else
- {
- // [self.agoraKit muteLocalVideoStream:YES];
- [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:YES];
- }
-
- if(userView2.voiceBtn.selected == YES)
- {
- [self.trtcCloud muteLocalAudio:NO];
- }
- else
- {
- // [self.agoraKit muteLocalAudioStream:YES];
- [self.trtcCloud muteLocalAudio:YES];
- }
- }
- else
- {
-
- // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
- [self.trtcCloud startLocalPreview:YES view:userView2.videoView];
- //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
- [self.trtcCloud startRemoteView:userlist[i].user_id streamType:TRTCVideoStreamTypeBig view:userView2.videoView];
- }
-
-
- }
- else if(i == 2)
- {
- userView3.hidden = NO;
- userView3.userName.text = userlist[i].nick_name;
- userView3.uid = userlist[i].user_id;
- if(userlist[i].user_id.intValue == [IMAPlatform sharedInstance].host.userId.intValue) {
-
- //是自己,连麦中
- self.isLinkingMic = YES;
-
- // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
- [self.trtcCloud startLocalPreview:YES view:userView3.videoView];
-
- if(userView3.videoBtn.selected == YES)
- {
- [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:NO];
- } else {
- // [self.agoraKit muteLocalVideoStream:YES];
- [self.trtcCloud muteLocalVideo:TRTCVideoStreamTypeBig mute:YES];
- }
-
- if(userView3.voiceBtn.selected == YES)
- {
- [self.trtcCloud muteLocalAudio:NO];
- } else {
- // [self.agoraKit muteLocalAudioStream:YES];
- [self.trtcCloud muteLocalAudio:YES];
- }
- } else {
- // isFrontCamera 可指定使用前置/后置摄像头进行视频采集 * @param frontCamera YES:前置摄像头;NO:后置摄像头。
- [self.trtcCloud startLocalPreview:YES view:userView3.videoView];
- //功能描述:订阅远端用户的视频流,并绑定视频渲染控件
- [self.trtcCloud startRemoteView:userlist[i].user_id streamType:TRTCVideoStreamTypeBig view:userView3.videoView];
- }
- }
- }
-
- if (userlist.count <= 0) {
- return;
- }
-
- NSArray *subView = self.userListView.subviews;
-
- NSLog(@"%@ === %@",subView,self.userListView);
-
- // for (int i=0; i<subView.count; i++) {
- // UIView *itemView = subView[i];
- // [itemView mas_remakeConstraints:^(MASConstraintMaker *make) {
- //
- // }];
- // }
-
- NSMutableArray *realViewArr = [NSMutableArray array];
- for (int i=0; i<subView.count; i++) {
- UIView *itemView = subView[i];
- if(itemView.hidden == NO)
- {
- [realViewArr addObject:itemView];
- }
- }
- int userViewWidth = 120;
- int userViewHeight = userViewWidth * 1.6;
- int sp = kRealValue(3);
- [self.userListView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.width.equalTo(@(userViewWidth));
- make.height.equalTo(@((userViewHeight + sp) * realViewArr.count ));
- make.bottom.equalTo([GlobalVariables sharedInstance].tliveView).offset(-80);
- make.right.equalTo([GlobalVariables sharedInstance].tliveView).offset(0);
- // make.height.equalTo(@(realViewArr.count * userViewHeight));
- }];
-
-
- if(realViewArr.count > 1)
- {
- [realViewArr mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.height.equalTo(@(userViewHeight));
- make.width.equalTo(@(userViewWidth));
- }];
- [realViewArr mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:userViewHeight leadSpacing:0 tailSpacing:0];
- }
- else if(realViewArr.count == 1)
- {
- [realViewArr[0] mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.userListView);
- make.height.equalTo(@(userViewHeight));
- make.width.equalTo(@(userViewWidth));
- }];
- }
- }
- #pragma mark 调整连麦窗口
- - (void)adjustPlayItem:(TLiveMickListModel *)mickListModel
- {
-
- }
- - (BGTLinkMicPlayItem*)getPlayItemByStreamUrl:(NSString*)streamUrl
- {
- if (streamUrl)
- {
- for (BGTLinkMicPlayItem* playItem in _playItemArray)
- {
- if ([streamUrl isEqualToString:playItem.playUrl])
- {
- return playItem;
- }
- }
- }
- return nil;
- }
- #pragma mark 请求连麦超时
- - (void)onWaitLinkMicResponseTimeOut
- {
- if (_isWaitingResponse == YES)
- {
- _isWaitingResponse = NO;
- [self toastTip:ASLocalizedString(@"连麦请求超时,主播没有做出回应")];
- }
- }
- #pragma mark 连麦过程出错处理
- - (void)handleLinkMicFailed:(NSString*)message
- {
- [self toastTip:message];
- //结束连麦
- [self stopLinkMic];
-
- [self startRtmp:self.create_type];
- }
- - (BOOL)startRtmp:(NSInteger)create_type
- {
- [super startRtmp:create_type];
-
- if([GlobalVariables sharedInstance].openAgora)
- {
- // [self initializeAgoraEngine];
- [self enterRoomWithTXTRTC];
-
- // [self setClientRole];
- // [self setupLocalVideo];
- // [self joinChannel];
- }
- return YES;
- }
- #pragma mark 权限检查
- - (void)authorizationCheck
- {
- //检查麦克风权限
- AVAuthorizationStatus statusAudio = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
- if (statusAudio == AVAuthorizationStatusDenied)
- {
- [self toastTip:ASLocalizedString(@"获取麦克风权限失败,请前往隐私-麦克风设置里面打开应用权限")];
- return;
- }
-
- //是否有摄像头权限
- AVAuthorizationStatus statusVideo = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
- if (statusVideo == AVAuthorizationStatusDenied)
- {
- [self toastTip:ASLocalizedString(@"获取摄像头权限失败,请前往隐私-相机设置里面打开应用权限")];
- return;
- }
-
- if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
- {
- [self toastTip:ASLocalizedString(@"系统不支持硬编码, 启动连麦失败")];
- return;
- }
- }
- #pragma mark - ----------------------- 代理事件 -----------------------
- - (void)onReceiveMemberJoinNotify:(NSString*)userID withStreamID:(NSString*)streamID
- {
-
- }
- - (void)onReceiveMemberExitNotify:(NSString*)userID
- {
-
- }
- - (void)onLivePushEvent:(NSString*) pushUrl withEvtID:(int)event andParam:(NSDictionary*)param
- {
- NSLog(@"==========PushEvtID2:%d",event);
-
- if (event == PUSH_EVT_PUSH_BEGIN) // 开始推流事件通知
- {
-
- //2.通知主播拉取自己的流
- // [_tcLinkMicMgr sendMemberJoinNotify:_liveInfo.userid withJoinerID:profile.identifier andJoinerPlayUrl:_playUrl];
- }
- else if (event == PUSH_ERR_NET_DISCONNECT)
- { //推流失败事件通知
- [self handleLinkMicFailed:ASLocalizedString(@"推流失败,结束连麦")];
- }
- else if (event == PUSH_WARNING_HW_ACCELERATION_FAIL)
- {
- [self handleLinkMicFailed:ASLocalizedString(@"启动硬编码失败,结束连麦")];
- }
- }
- - (void)onLivePushNetStatus:(NSString*)pushUrl withParam:(NSDictionary*) param
- {
- [super onNetStatus:param];
- }
- - (void)onLivePlayEvent:(NSString*)playUrl withEvtID:(int)event andParam:(NSDictionary*)param
- {
- BGTLinkMicPlayItem *playItem = [self getPlayItemByStreamUrl:playUrl];
-
- if (event == PLAY_EVT_PLAY_BEGIN)
- {
- if (playItem)
- {
- [playItem stopLoading];
- }
- else
- {
- [self stopLoading];
- }
- }
- else if (event == PLAY_EVT_PLAY_END)
- {
- if (playItem)
- {
- [playItem stopLoading];
- }
- }
- else if (event == PLAY_ERR_NET_DISCONNECT)
- {
- if (playItem)
- {
- [playItem stopPlay];
- [playItem stopLoading];
- [playItem emptyPlayInfo];
- }
- }
- else if (event == PLAY_WARNING_HW_ACCELERATION_FAIL)
- {
- if (playItem)
- {
- [playItem stopLoading];
- }
- }
- else if (event == PLAY_ERR_GET_RTMP_ACC_URL_FAIL)
- {
- [playItem reStartPlay];
- }
-
- [super onPlayEvent:event withParam:param];
- }
- - (void)onLivePlayNetStatus:(NSString*)playUrl withParam:(NSDictionary*)param
- {
-
- }
- #pragma mark - ----------------------- 结束视频 -----------------------
- - (void)endVideo
- {
- [self stopLinkMic];
-
- [super stopRtmp];
- }
- #pragma mark - ----------------------- 加载动画 -----------------------
- - (void)startLoading
- {
- if (_loadingBackground)
- {
- _loadingBackground.hidden = NO;
- }
-
- if (_loadingImageView)
- {
- _loadingImageView.hidden = NO;
- [_loadingImageView startAnimating];
- }
- }
- - (void)stopLoading
- {
- if (_loadingBackground)
- {
- _loadingBackground.hidden = YES;
- _loadingBackground = nil;
- }
-
- if (_loadingImageView)
- {
- _loadingImageView.hidden = YES;
- [_loadingImageView stopAnimating];
- _loadingImageView = nil;
- }
- }
- #pragma mark - ----------------------- 进入前后台 -----------------------
- #pragma mark app进入后台
- - (void)onAppDidEnterBackGround
- {
- [super onAppDidEnterBackGround];
-
- if (_isBeingLinkMic)
- {
- [_txLivePush pausePush];
- }
- }
- #pragma mark app将要进入前台
- - (void)onAppWillEnterForeground
- {
- [super onAppWillEnterForeground];
-
- if (_isBeingLinkMic)
- {
- [_txLivePush resumePush];
- }
- }
- @end
|