YBLiveVC.m 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. //
  2. // YBLiveVC.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2019/11/29.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "YBLiveVC.h"
  9. #import <AFNetworkReachabilityManager.h>
  10. #import <AFNetworking/AFNetworking.h>
  11. #import <CoreTelephony/CTCallCenter.h>
  12. #import <CoreTelephony/CTCall.h>
  13. #import <TXLiteAVSDK_Professional/TXLivePush.h>
  14. #import <TXLiteAVSDK_Professional/TXLiveBase.h>
  15. #import "YBLivePreview.h"
  16. #import "YBLiveCtrlView.h"
  17. #import "YBShareView.h"
  18. #import "Utils.h"
  19. #import "YBSocketLive.h"
  20. #import "YBLiveEndView.h"
  21. #import "UserBulletWindow.h"
  22. #import "YBCenterVC.h"
  23. #import "adminLists.h"
  24. #import "huanxinsixinview.h"
  25. #import "JCHATConversationViewController.h"
  26. #import "MsgSysVC.h"
  27. #import "V8HorizontalPickerView.h"
  28. #import "MessageListModel.h"
  29. #import "TXBaseBeautyView.h"
  30. /******************** 美狐开始 ******************/
  31. #import "MHMeiyanMenusView.h"
  32. #import <MHBeautySDK/MHBeautyManager.h>
  33. #import <MHBeautySDK/MHSDK.h>
  34. #import "MHBeautyParams.h"
  35. /******************** 美狐结束 ******************/
  36. #import "YBImRoomSmallView.h"
  37. #import "YBLiveRTCManager.h"
  38. #import "UpHotView.h"
  39. #import "UpHotOrderView.h"
  40. @interface YBLiveVC ()<TXVideoCustomProcessDelegate,TXLivePushListener,UserBulletWindowDelegate,adminDelegate,YBLiveRTCDelegate,MHMeiyanMenusViewDelegate,V2TIMConversationListener>{
  41. BOOL isclosenetwork; //判断断网回后台
  42. NSTimer *backGroundTimer; //检测后台时间(超过60秒执行断流操作)
  43. int backTime; //返回后台时间60s
  44. int _canBackTime; //正常60s---锁屏30s
  45. NSString *selectShareName; //选择分享的名称
  46. NSString *liveClassStr; //直播分类
  47. NSString *_hostUrl; //推流地址
  48. NSString *_hostStream; //流
  49. int _userlistRefreshTime; //用户列表刷新时间
  50. NSTimer *_listTimer; //用户列表计时器
  51. NSTimer *_liveTimer; //直播时长
  52. NSTimer *_heartTimer; //点亮
  53. UserBulletWindow *buttleView; //用户信息卡
  54. huanxinsixinview *huanxinviews; //直播间私信
  55. JCHATConversationViewController *chatsmall;
  56. MsgSysVC *sysView;
  57. int unRead;
  58. BOOL _isPreView;
  59. BOOL isLoadWebSprout;
  60. NSString *_liveTitle;
  61. BOOL _needScale;
  62. YBImRoomSmallView *_imListView;
  63. BOOL _isFrontCamera;
  64. NSString *uphotOrderId;
  65. }
  66. @property(nonatomic,strong)UIImageView *pkBgIV;
  67. @property (nonatomic,strong)CTCallCenter *callCenter;
  68. //@property(nonatomic,strong)TXLivePushConfig *txLivePushonfig;
  69. //@property(nonatomic,strong)TXLivePush *txLivePublisher;
  70. @property(nonatomic,strong)UIView *txPushPreview; //推流器预览视图
  71. @property(nonatomic,strong)YBLivePreview *livePreview; //开播前预览界面
  72. @property(nonatomic,strong)YBLiveCtrlView *liveCtrlView; //控制层
  73. @property(nonatomic,strong)YBLiveEndView *liveEndView; //直播结束页面
  74. @property(nonatomic,strong)NSDictionary *roomDic;
  75. @property (nonatomic, strong) UpHotView *hotView;
  76. /******************** 美狐开始 ******************/
  77. @property(nonatomic,strong)UIButton *mhShadowBtn;
  78. @property(nonatomic,strong)MHMeiyanMenusView *menusView;
  79. @property(nonatomic,strong)MHBeautyManager *beautyManager;
  80. /******************** 美狐结束 ******************/
  81. @property(nonatomic,strong)UpHotOrderView *uphotOrderView;
  82. @end
  83. @implementation YBLiveVC
  84. - (void)viewWillAppear:(BOOL)animated {
  85. [super viewWillAppear:animated];
  86. [IQKeyboardManager sharedManager].enable = NO;
  87. isLoadWebSprout = NO;
  88. [self requestJmsg];
  89. }
  90. - (void)viewWillDisappear:(BOOL)animated {
  91. [super viewWillDisappear:animated];
  92. [IQKeyboardManager sharedManager].enable = YES;
  93. }
  94. - (void)viewDidLoad {
  95. [super viewDidLoad];
  96. _canBackTime = 60;
  97. uphotOrderId = @"";
  98. /*
  99. if (@available(iOS 13.0,*)) {
  100. _canBackTime = 28;
  101. }
  102. */
  103. _liveTitle = @"";
  104. liveClassStr = @"-99999999";
  105. _isFrontCamera = YES;
  106. self.naviView.hidden = YES;
  107. self.navigationController.interactivePopGestureRecognizer.enabled = NO;
  108. self.navigationController.interactivePopGestureRecognizer.delegate =nil;
  109. _isPreView = YES;
  110. [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
  111. //弹出相机权限
  112. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  113. }];
  114. //弹出麦克风权限
  115. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
  116. }];
  117. selectShareName = @"";
  118. [self addNotification];
  119. [self netMonitoring];
  120. //创建推流器
  121. [self initTxPush];
  122. [self.view addSubview:self.livePreview];
  123. [self.view addSubview:self.liveCtrlView];
  124. NSInteger deviceType = [PublicObj getDeviceType];
  125. //iPhone6s
  126. if (deviceType >= 8){
  127. _needScale = NO;
  128. }else{
  129. _needScale = YES;
  130. }
  131. if([_has_hotliveorder isEqual:@"1"]){
  132. YBWeakSelf;
  133. _uphotOrderView =[[UpHotOrderView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)andInfoDic:_hotliveorderDic];
  134. _uphotOrderView.btnEvent = ^(NSString *type) {
  135. if([type isEqual:YZMsg(@"使用")]){
  136. uphotOrderId =minstr([weakSelf.hotliveorderDic valueForKey:@"id"]) ;
  137. }
  138. [weakSelf.uphotOrderView removeFromSuperview];
  139. weakSelf.uphotOrderView = nil;
  140. };
  141. [self.view addSubview:_uphotOrderView];
  142. }
  143. }
  144. -(void)initTxPush{
  145. [self.view addSubview:self.pkBgIV];
  146. _txPushPreview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
  147. [self.view addSubview:self.txPushPreview];
  148. [[YBLiveRTCManager shareInstance]initWithLiveMode:V2TXLiveMode_RTC andPushData:_pushSettingDic];
  149. [[YBLiveRTCManager shareInstance]setPushView:_txPushPreview];
  150. [YBLiveRTCManager shareInstance].delegate = self;
  151. if ([PublicObj isBaseBeauty]) {
  152. //腾讯美颜
  153. [[YBLiveRTCManager shareInstance] setBeautyLevel:[TXBaseBeautyView getBaseBeautyValue] WhitenessLevel:[TXBaseBeautyView getBaseWhiteValue]];
  154. }else{
  155. //其他美颜
  156. // [self txBaseStyle:0 level:0 white:0 ruddiness:0];
  157. // [_txLivePublisher setMirror:YES];
  158. // [[MHSDK shareInstance]initWithAppID:@"33893d3ce4ad50e38efa067755386b24" key:@"061e4b4758d2f8a3afd0a095f543f091"];
  159. [[MHSDK shareInstance] initWithAppID:[common getTISDKAppid] key:[common getTISDKKey]];
  160. }
  161. }
  162. #pragma mark -美狐回调
  163. -(void)MHBeautyBlock:(V2TXLiveVideoFrame *)srcFrame dstFrame:(V2TXLiveVideoFrame *)dstFrame
  164. {
  165. dstFrame.textureId= [self.beautyManager getTextureProcessWithTexture:srcFrame.textureId width:(GLint)srcFrame.width height:(GLint)srcFrame.height mirror:YES];
  166. dispatch_async(dispatch_get_main_queue(), ^{
  167. if (self.menusView) {
  168. if (!isLoadWebSprout) {
  169. isLoadWebSprout = YES;
  170. [self.menusView setupDefaultBeautyAndFaceValue];
  171. }
  172. }
  173. });
  174. }
  175. -(void)destoryMHBeauty{
  176. [self destroyMhSource];
  177. }
  178. #pragma mark - 美颜开始
  179. -(void)showBeautyUI {
  180. if (_livePreview && _isPreView) {
  181. _livePreview.hidden = YES;
  182. }
  183. if ([PublicObj isBaseBeauty]) {
  184. YBWeakSelf;
  185. [TXBaseBeautyView showBaseBeauty:^(NSString *eventStr, float value, NSString *filterName) {
  186. [weakSelf txBaseBeauty:eventStr value:value filter:filterName];
  187. }];
  188. }else{
  189. //[self.tiUIView createTiUIView:NO];
  190. self.mhShadowBtn.hidden = NO;
  191. [self.menusView showMenuView:YES];
  192. }
  193. }
  194. //基础美颜
  195. -(void)txBaseBeauty:(NSString *)eventStr value:(float)value filter:(NSString *)filterName {
  196. if ([eventStr isEqual:@"基础美颜-关闭"]) {
  197. [self onTiTapEvent];
  198. }
  199. if ([eventStr isEqual:@"基础美颜-美颜"]) {
  200. [self txBaseStyle:0 level:value white:[TXBaseBeautyView getBaseWhiteValue] ruddiness:0];
  201. }
  202. if ([eventStr isEqual:@"基础美颜-美白"]) {
  203. [self txBaseStyle:0 level:[TXBaseBeautyView getBaseBeautyValue] white:value ruddiness:0];
  204. }
  205. if ([eventStr isEqual:@"基础美颜-滤镜"]) {
  206. if (![PublicObj checkNull:filterName]) {
  207. [[YBLiveRTCManager shareInstance]setYBFilter:[UIImage imageWithContentsOfFile:filterName]];
  208. }else {
  209. [[YBLiveRTCManager shareInstance]setYBFilter:nil];
  210. }
  211. }
  212. }
  213. -(void)txBaseStyle:(int)beautyStyle level:(CGFloat)beautyLevel white:(CGFloat)whiteness ruddiness:(CGFloat)ruddiness {
  214. [[YBLiveRTCManager shareInstance] setYBRuddyLevel:ruddiness];
  215. [[YBLiveRTCManager shareInstance]setBeautyLevel:beautyLevel WhitenessLevel:whiteness];
  216. // [[_txLivePublisher getBeautyManager] setBeautyStyle:beautyStyle];
  217. // [[_txLivePublisher getBeautyManager] setBeautyLevel:beautyLevel];
  218. // [[_txLivePublisher getBeautyManager] setWhitenessLevel:whiteness];
  219. // [[_txLivePublisher getBeautyManager] setRuddyLevel:ruddiness];
  220. }
  221. - (void)dealloc{
  222. //[self destroyTiSdk];
  223. // [self destroyMhSource];
  224. }
  225. -(void)destroyMhSource {
  226. if (self.beautyManager) {
  227. [self.beautyManager destroy];
  228. self.beautyManager = nil;
  229. }
  230. }
  231. - (void)onTiTapEvent {
  232. if (_livePreview && _isPreView) {
  233. _livePreview.hidden = NO;
  234. }
  235. }
  236. //美狐
  237. - (MHBeautyManager *)beautyManager {
  238. if (!_beautyManager) {
  239. _beautyManager = [[MHBeautyManager alloc] init];
  240. }
  241. return _beautyManager;
  242. }
  243. - (UIButton *)mhShadowBtn {
  244. if (!_mhShadowBtn) {
  245. _mhShadowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  246. _mhShadowBtn.frame = CGRectMake(0, 0, _window_width, _window_height);
  247. [_mhShadowBtn addTarget:self action:@selector(dismissMHUI) forControlEvents:UIControlEventTouchUpInside];
  248. [self.view addSubview:_mhShadowBtn];
  249. }
  250. return _mhShadowBtn;
  251. }
  252. -(void)dismissMHUI {
  253. if (self.menusView.isShow) {
  254. self.mhShadowBtn.hidden = YES;
  255. //更新美颜值
  256. [sproutCommon updataMHValueToService];
  257. [self.menusView showMenuView:NO];
  258. [self onTiTapEvent];
  259. }else {
  260. [self.menusView showMenuView:YES];
  261. }
  262. }
  263. - (MHMeiyanMenusView *)menusView {
  264. if (!_menusView) {
  265. // _menusView = [[MHMeiyanMenusView alloc]initWithFrame:CGRectMake(0, window_height - MHMeiyanMenuHeight - BottomIndicatorHeight, window_width,MHMeiyanMenuHeight) superView:self.view delegate:self beautyManager:self.beautyManager isTXSDK:YES];
  266. _menusView = [[MHMeiyanMenusView alloc] initWithFrame:CGRectMake(0, window_height - MHMeiyanMenuHeight - BottomIndicatorHeight, window_width, MHMeiyanMenuHeight) superView:self.view beautyManager:self.beautyManager];
  267. }
  268. return _menusView;
  269. }
  270. - (void)beautyEffectWithLevel:(NSInteger)beauty whitenessLevel:(NSInteger)white ruddinessLevel:(NSInteger)ruddiness {
  271. [self txBaseStyle:0 level:beauty white:white ruddiness:ruddiness];
  272. }
  273. #pragma mark - 美颜结束
  274. #pragma mark - 直播
  275. -(void)rotateCamera{
  276. dispatch_async(dispatch_get_main_queue(), ^{
  277. // [_txLivePublisher switchCamera];
  278. // [_txLivePublisher setMirror:_txLivePublisher.frontCamera];
  279. _isFrontCamera = !_isFrontCamera;
  280. [[YBLiveRTCManager shareInstance]changeCamera:_isFrontCamera];
  281. });
  282. }
  283. - (void)txStopRtmp {
  284. [[YBLiveRTCManager shareInstance]stopPush];
  285. [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
  286. }
  287. -(void)hostStopRoom {
  288. if (isclosenetwork) {
  289. //断网
  290. [self showNetCloseAlert];
  291. [self destroySource];
  292. [self clickNaviLeftBtn];
  293. }else{
  294. [self getCloseShow];
  295. }
  296. }
  297. //关播
  298. -(void)getCloseShow {
  299. [self destroySource];
  300. [MBProgressHUD showMessage:@""];
  301. YBWeakSelf;
  302. [YBNetworking postWithUrl:@"Live.stopRoom" Dic:@{@"stream":_hostStream} Suc:^(int code, id info, NSString *msg) {
  303. [MBProgressHUD hideHUD];
  304. if (code == 0) {
  305. [weakSelf dismissVC];
  306. }else {
  307. [MBProgressHUD showPop:msg];
  308. }
  309. } Fail:^(id fail) {
  310. [MBProgressHUD hideHUD];
  311. }];
  312. }
  313. -(void)superStopRoom {
  314. /*
  315. [PublicObj resetUserToVisitor];
  316. [self destroySource];
  317. [self clickNaviLeftBtn];
  318. */
  319. [self getCloseShow];
  320. }
  321. -(void)destroySource {
  322. if (buttleView) {
  323. [self removeButtleView];
  324. }
  325. [self destroyImListView];
  326. [_liveCtrlView destroyCtrSubView];
  327. [self txStopRtmp];
  328. [self destroyTimer];
  329. [[YBSocketLive liveSocketManeger]liveDisconnectSocket];
  330. [[NSNotificationCenter defaultCenter]removeObserver:self];
  331. [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isLiveing"];
  332. // [self destroyMhSource];
  333. }
  334. -(void)dismissVC {
  335. [self destroySource];
  336. [MBProgressHUD showMessage:@""];
  337. YBWeakSelf;
  338. [YBNetworking postWithUrl:@"Live.stopInfo" Dic:@{@"stream":_hostStream} Suc:^(int code, id info, NSString *msg) {
  339. [MBProgressHUD hideHUD];
  340. if (code == 0) {
  341. _liveCtrlView.hidden = YES;
  342. NSDictionary *subdic = [info firstObject];
  343. [weakSelf createEndView:subdic];
  344. }else {
  345. [MBProgressHUD showPop:msg];
  346. }
  347. } Fail:^(id fail) {
  348. [MBProgressHUD hideHUD];
  349. }];
  350. }
  351. -(void)createEndView:(NSDictionary *)endDic {
  352. [self.view addSubview:self.liveEndView];
  353. NSMutableDictionary *allDic = @{@"hostAvatar":[Config getUserAvatar],@"hostName":[Config getOwnNicename]}.mutableCopy;
  354. [allDic addEntriesFromDictionary:endDic];
  355. [_liveEndView updateData:allDic];
  356. }
  357. -(void)destroyTimer {
  358. if (backGroundTimer) {
  359. [backGroundTimer invalidate];
  360. backGroundTimer = nil;
  361. }
  362. if (_listTimer) {
  363. [_listTimer invalidate];
  364. _listTimer = nil;
  365. }
  366. if (_liveTimer) {
  367. [_liveTimer invalidate];
  368. _liveTimer = nil;
  369. }
  370. if (_heartTimer) {
  371. [_heartTimer invalidate];
  372. _heartTimer = nil;
  373. }
  374. }
  375. #pragma mark - TXVideoProcessDelegate
  376. //- (GLuint)onPreProcessTexture:(GLuint)texture width:(CGFloat)width height:(CGFloat)height{
  377. //
  378. // if ([PublicObj isBaseBeauty]) {
  379. // //普通美颜
  380. // return texture;
  381. // }
  382. // GLuint newTexture = texture;
  383. // /*
  384. // if (_needScale){
  385. // newTexture = [_beautyManager processWithTexture:texture width:width height:height scale:0.75];
  386. // }else{
  387. // [self.beautyManager processWithTexture:texture width:width height:height];
  388. // }
  389. // */
  390. // newTexture = [self.beautyManager getTextureProcessWithTexture:texture width:width height:height];
  391. // dispatch_async(dispatch_get_main_queue(), ^{
  392. // if (self.menusView) {
  393. // if (!isLoadWebSprout) {
  394. // isLoadWebSprout = YES;
  395. // [self.menusView setupDefaultBeautyAndFaceValue];
  396. // }
  397. // }
  398. // });
  399. // return newTexture;
  400. //}
  401. //- (void)onTextureDestoryed{
  402. //// [self destroyTiSdk];
  403. //}
  404. //#pragma mark - TXLivePushListener
  405. //-(void) onNetStatus:(NSDictionary*) param{
  406. //
  407. //}
  408. //-(void) onPushEvent:(int)EvtID withParam:(NSDictionary*)param {
  409. // dispatch_async(dispatch_get_main_queue(), ^{
  410. // if (EvtID >= 0) {
  411. // if (EvtID == PUSH_WARNING_HW_ACCELERATION_FAIL) {
  412. // _txLivePublisher.config.enableHWAcceleration = false;
  413. // NSLog(@"PUSH_EVT_PUSH_BEGIN硬编码启动失败,采用软编码");
  414. // }else if (EvtID == PUSH_EVT_CONNECT_SUCC) {
  415. // // 已经连接推流服务器
  416. // NSLog(@" PUSH_EVT_PUSH_BEGIN已经连接推流服务器");
  417. // }else if (EvtID == PUSH_EVT_PUSH_BEGIN) {
  418. // // 已经与服务器握手完毕,开始推流
  419. // [self changePlayState:1];
  420. // NSLog(@"liveshow已经与服务器握手完毕,开始推流");
  421. // }else if (EvtID == PUSH_WARNING_RECONNECT){
  422. // // 网络断连, 已启动自动重连 (自动重连连续失败超过三次会放弃)
  423. // NSLog(@"网络断连, 已启动自动重连 (自动重连连续失败超过三次会放弃)");
  424. // }else if (EvtID == PUSH_WARNING_NET_BUSY) {
  425. // //[_notification displayNotificationWithMessage:@"您当前的网络环境不佳,请尽快更换网络保证正常直播" forDuration:5];
  426. // }
  427. // }else {
  428. // if (EvtID == PUSH_ERR_NET_DISCONNECT) {
  429. // NSLog(@"PUSH_EVT_PUSH_BEGIN网络断连,且经多次重连抢救无效,可以放弃治疗,更多重试请自行重启推流");
  430. // [MBProgressHUD showPop:YZMsg(@"网络断连")];
  431. // [self hostStopRoom];
  432. // }
  433. // }
  434. // });
  435. //}
  436. #pragma mark --RTC推流回调
  437. /**
  438. * 推流器连接状态回调通知
  439. *
  440. * @param status 推流器连接状态 {@link V2TXLivePushStatus}。
  441. * @param msg 连接状态信息。
  442. * @param extraInfo 扩展信息。
  443. */
  444. -(void)ybRTCPushStatusUpdate:(V2TXLivePushStatus)status message:(NSString *)msg extraInfo:(NSDictionary *)extraInfo{
  445. dispatch_async(dispatch_get_main_queue(), ^{
  446. if (status == V2TXLivePushStatusDisconnected) {
  447. NSLog(@"PUSH_EVT_PUSH_BEGIN网络断连,且经多次重连抢救无效,可以放弃治疗,更多重试请自行重启推流");
  448. [MBProgressHUD showPop:YZMsg(@"网络断连")];
  449. [self hostStopRoom];
  450. }else if(status == V2TXLivePushStatusConnecting){
  451. /// 正在连接服务器
  452. // if (isPUSH_WARNING_RECONNECT) {
  453. // isPUSH_WARNING_RECONNECT = NO;
  454. // [self checkLiveingStatus];
  455. // }
  456. }else if(status == V2TXLivePushStatusConnectSuccess){
  457. /// 连接服务器成功
  458. [self changePlayState:1];
  459. }else if(status == V2TXLivePushStatusConnectSuccess){
  460. /// 重连服务器中
  461. NSLog(@"网络断连, 已启动自动重连 (自动重连连续失败超过三次会放弃)");
  462. }
  463. });
  464. }
  465. -(void)ybPushLiveStatus:(V2TXLiveCode)pushStatus
  466. {
  467. if (pushStatus == V2TXLIVE_OK) {
  468. NSLog(@"LIVEBROADCAST --:推流成功、停止推流");
  469. }else if (pushStatus == V2TXLIVE_ERROR_INVALID_PARAMETER){
  470. // [_notification displayNotificationWithMessage:@"操作失败,url 不合法" forDuration:5];
  471. NSLog(@"推流器启动失败");
  472. }else if (pushStatus == V2TXLIVE_ERROR_INVALID_LICENSE){
  473. // [_notification displayNotificationWithMessage:@"操作失败,license 不合法,鉴权失败" forDuration:5];
  474. NSLog(@"推流器启动失败");
  475. }else if (pushStatus == V2TXLIVE_ERROR_REFUSED){
  476. // [_notification displayNotificationWithMessage:@"操作失败,RTC 不支持同一设备上同时推拉同一个 StreamId" forDuration:5];
  477. NSLog(@"推流器启动失败");
  478. }else if (pushStatus == V2TXLIVE_WARNING_NETWORK_BUSY){
  479. // [_notification displayNotificationWithMessage:
  480. // @"您当前的网络环境不佳,请尽快更换网络保证正常直播" forDuration:5];
  481. }
  482. }
  483. #pragma mark - 控制层回调
  484. -(void)controlViewCallBack:(NSString *)eventType andEvnent:(NSDictionary *)eventDic {
  485. YBWeakSelf;
  486. if ([eventType isEqual:@"控制-动画结束"]) {
  487. //开始推流
  488. [self goStartPushing];
  489. }
  490. if ([eventType isEqual:@"控制-关闭"]) {
  491. NSDictionary *contentDic = @{@"title":YZMsg(@"提示"),@"msg":YZMsg(@"是否要结束直播?"),@"left":YZMsg(@"取消"),@"right":YZMsg(@"确定")};
  492. [YBAlertView showAlertView:contentDic complete:^(int eventType) {
  493. if (eventType == 1) {
  494. [weakSelf hostStopRoom];
  495. }
  496. }];
  497. }
  498. if ([eventType isEqual:@"控制-美颜"]) {
  499. [self showBeautyUI];
  500. }
  501. if ([eventType isEqual:@"控制-翻转"]) {
  502. [self rotateCamera];
  503. }
  504. if ([eventType isEqual:@"控制-闪光灯"]) {
  505. if (_isFrontCamera) {
  506. [MBProgressHUD showError:YZMsg(@"只有后置摄像头才能开启闪光灯")];
  507. return;
  508. }
  509. _liveCtrlView.isTorch = !_liveCtrlView.isTorch;
  510. [[YBLiveRTCManager shareInstance]cameraTorch:_liveCtrlView.isTorch];
  511. // if (![_txLivePublisher toggleTorch:_liveCtrlView.isTorch]) {
  512. // _liveCtrlView.isTorch = !_liveCtrlView.isTorch;
  513. // }
  514. // if (_txLivePublisher.frontCamera) {
  515. // [MBProgressHUD showPop:YZMsg(@"只有后置摄像头才能开启闪光灯")];
  516. // }
  517. }
  518. if ([eventType isEqual:@"控制-直播聊天"]) {
  519. //发送socket 'ct' [eventDic valueForKey:@"ct"]
  520. [[YBSocketLive liveSocketManeger] liveSendMsg:minstr([eventDic valueForKey:@"ct"])];
  521. }
  522. if ([eventType isEqual:@"控制-信息卡"]) {
  523. [weakSelf showButtleView:minstr([eventDic valueForKey:@"id"])];
  524. }
  525. if ([eventType isEqual:@"控制-私信"]) {
  526. [weakSelf liveRoomPraviteMsg];
  527. }
  528. if ([eventType isEqual:@"控制-在售"]) {
  529. [[YBSocketLive liveSocketManeger] liveSendOnSaleShowHidden:eventDic];
  530. }
  531. if ([eventType isEqual:@"控制-主播下麦他人"]) {
  532. int isHostToHost = [minstr([eventDic valueForKey:@"hostToHost"]) intValue];
  533. if (isHostToHost) {
  534. [[YBSocketLive liveSocketManeger] liveSendAnchorCtrOfAcntion:@"5" andExtDic:@{@"pkuid":minstr([eventDic valueForKey:@"uid"]),@"pkpull":@""}];
  535. }else {
  536. [[YBSocketLive liveSocketManeger] liveSendHostDisconnect:eventDic];
  537. }
  538. }
  539. if ([eventType isEqual:@"控制-主播和主播连麦请求"]) {
  540. [[YBSocketLive liveSocketManeger] liveSendAnchorStartLink:[eventDic valueForKey:@"otherInfo"] andMyInfo:[eventDic valueForKey:@"myInfo"]];
  541. }
  542. if ([eventType isEqual:@"控制-发起PK"]) {
  543. [[YBSocketLive liveSocketManeger] liveSendStartPk];
  544. }
  545. }
  546. //开始推流
  547. -(void)goStartPushing {
  548. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLiveing"];
  549. _hostUrl = minstr([_roomDic valueForKey:@"push"]);
  550. _hostStream = minstr([_roomDic valueForKey:@"stream"]);
  551. _userlistRefreshTime = [minstr([_roomDic valueForKey:@"userlist_time"]) intValue];
  552. if (_userlistRefreshTime < 10) {
  553. _userlistRefreshTime = 10;
  554. }
  555. if (_liveCtrlView) {
  556. _liveCtrlView.liveUid = [Config getOwnID];
  557. _liveCtrlView.liveStream = _hostStream;
  558. _liveCtrlView.liveTitle = _liveTitle;
  559. _liveCtrlView.liveHostUrl = _hostUrl;
  560. }
  561. // //推流器
  562. // _txLivePublisher.delegate = self;
  563. // [self.txLivePublisher setVideoQuality:VIDEO_QUALITY_HIGH_DEFINITION adjustBitrate:YES adjustResolution:YES];
  564. _hostUrl = [NSString stringWithFormat:@"%@&mix=session_id:%@",_hostUrl,[Config getOwnID]];
  565. _liveCtrlView.liveHostUrl = _hostUrl;
  566. [[YBLiveRTCManager shareInstance]startPush:_hostUrl];
  567. [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
  568. // int isOK = [_txLivePublisher startPush:_hostUrl];
  569. // if (isOK != 0) {
  570. // [MBProgressHUD showPop:[NSString stringWithFormat:@"%@:%d",YZMsg(@"推流器启动失败"),isOK]];
  571. // NSLog(@"推流器启动失败");
  572. // [self callBackClosePreView];
  573. // return;
  574. // }
  575. //
  576. if (!_listTimer) {
  577. _listTimer = [NSTimer scheduledTimerWithTimeInterval:_userlistRefreshTime target:self selector:@selector(reloadUserList) userInfo:nil repeats:YES];
  578. }
  579. if (!_heartTimer) {
  580. _heartTimer = [NSTimer scheduledTimerWithTimeInterval:0.8 target:self selector:@selector(socketLight) userInfo:nil repeats:YES];
  581. }
  582. if (!_liveTimer) {
  583. _liveTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(liveTimeUpdate) userInfo:nil repeats:YES];
  584. }
  585. //socket
  586. [self startSocketBlockMonitor];
  587. [[YBSocketLive liveSocketManeger]liveConnectSocket:_roomDic];
  588. }
  589. -(void)reloadUserList {
  590. [_liveCtrlView timerReloadList];
  591. }
  592. -(void)socketLight {
  593. [_liveCtrlView socketLightAnimation];
  594. }
  595. //直播时长
  596. -(void)liveTimeUpdate {
  597. [_liveCtrlView updateLiveTime];
  598. }
  599. #pragma mark - 直播间私信列表开始
  600. -(void)liveRoomPraviteMsg {
  601. [self destroyImListView];
  602. YBWeakSelf;
  603. _imListView = [YBImRoomSmallView showImListWithLiveUid:[Config getOwnID]];
  604. _imListView.imSmallType = ImSmall_List;
  605. _imListView.imCtrEvent = ^(ImCtrType type, NSDictionary *ctrDic) {
  606. if (type == ImCtr_Dismiss) {
  607. [weakSelf destroyImListView];
  608. }
  609. };
  610. }
  611. -(void)destroyImListView {
  612. if (_imListView) {
  613. [_imListView removeFromSuperview];
  614. _imListView = nil;
  615. }
  616. }
  617. #pragma mark - 直播间私信列表结束
  618. #pragma mark - 用户卡片信息开始
  619. - (void)showButtleView:(NSString *)touid{
  620. if (buttleView) {
  621. [self removeButtleView];
  622. }
  623. buttleView = [[UserBulletWindow alloc]initWithUserID:touid andIsAnchor:YES andAnchorID:[Config getOwnID]];
  624. buttleView.delegate = self;
  625. [self.view addSubview:buttleView];
  626. }
  627. - (void)removeButtleView {
  628. [buttleView destroySubs];
  629. [buttleView removeFromSuperview];
  630. buttleView = nil;
  631. }
  632. -(void)socketShutUp:(NSString *)name andID:(NSString *)ID andType:(NSString *)type {
  633. [[YBSocketLive liveSocketManeger] liveSendShutUpUser:@{@"type":type,@"touid":ID,@"touname":name}];
  634. }
  635. -(void)socketkickuser:(NSString *)name andID:(NSString *)ID {
  636. [[YBSocketLive liveSocketManeger] liveSendKickUser:@{@"touname":name,@"touid":ID}];
  637. }
  638. -(void)pushZhuYe:(NSString *)IDS {
  639. YBCenterVC *center = [[YBCenterVC alloc]init];
  640. center.otherUid = IDS;
  641. center.isPush = YES;
  642. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:center animated:YES];
  643. }
  644. -(void)siXin:(NSString *)icon andName:(NSString *)name andID:(NSString *)ID andIsatt:(NSString *)isatt {
  645. [self destroyImListView];
  646. YBWeakSelf;
  647. [[YBMessageManager shareManager] getChatCellDataWithTouid:ID finish:^(int code, TConversationCellData * _Nonnull cellData) {
  648. dispatch_async(dispatch_get_main_queue(), ^{
  649. if (code == 0) {
  650. _imListView = [YBImRoomSmallView showImListWithLiveUid:[Config getOwnID]];
  651. _imListView.imSmallType = ImSmall_C2C;
  652. [_imListView goChatWithC2CWithConv:cellData];
  653. _imListView.imCtrEvent = ^(ImCtrType type, NSDictionary *ctrDic) {
  654. if (type == ImCtr_Dismiss) {
  655. [weakSelf destroyImListView];
  656. }
  657. };
  658. }
  659. });
  660. }];
  661. }
  662. - (void)hideChatMall{
  663. if (huanxinviews) {
  664. [huanxinviews forMessage];
  665. CATransition *transition = [CATransition animation];
  666. transition.duration = 0.3;
  667. //设置动画淡入淡出的效果
  668. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  669. transition.type = kCATransitionPush;
  670. transition.subtype = kCATransitionFromLeft;
  671. [chatsmall.view.layer addAnimation:transition forKey:nil];
  672. [chatsmall.view removeFromSuperview];
  673. chatsmall.view = nil;
  674. chatsmall = nil;
  675. }else{
  676. [UIView animateWithDuration:0.3 animations:^{
  677. chatsmall.view.frame = CGRectMake(_window_width, _window_height*0.6, _window_width, _window_height*0.4);
  678. } completion:^(BOOL finished) {
  679. [chatsmall.view removeFromSuperview];
  680. chatsmall.view = nil;
  681. chatsmall = nil;
  682. }];
  683. }
  684. }
  685. -(void)setAdminSuccess:(NSString *)isadmin andName:(NSString *)name andID:(NSString *)ID {
  686. [[YBSocketLive liveSocketManeger] liveSendSetAdmin:@{@"isadmin":isadmin,@"touid":ID,@"touname":name}];
  687. }
  688. -(void)adminList {
  689. adminLists *adminVC = [[adminLists alloc]init];
  690. adminVC.delegate = self;
  691. adminVC.modalPresentationStyle = 0;
  692. [self presentViewController:adminVC animated:YES completion:nil];
  693. }
  694. #pragma mark - 用户卡片信息结束
  695. #pragma mark - socket 回调监听
  696. -(void)startSocketBlockMonitor {
  697. YBWeakSelf;
  698. //检查直播状态
  699. [YBSocketLive liveSocketManeger].checkLiveingStatus = ^(NSString *socEvent, NSDictionary *socketDic) {
  700. [weakSelf checkLiveingStatus];
  701. };
  702. //超管关播
  703. [YBSocketLive liveSocketManeger].superStopRoom = ^(NSString *socEvent, NSDictionary *socketDic) {
  704. dispatch_async(dispatch_get_main_queue(), ^{
  705. [weakSelf superStopRoom];
  706. });
  707. };
  708. //用户点亮
  709. [YBSocketLive liveSocketManeger].userLight = ^(NSString *socEvent, NSDictionary *socketDic) {
  710. [weakSelf.liveCtrlView socketLightAnimation];
  711. };
  712. //发言
  713. [YBSocketLive liveSocketManeger].userSendMsg = ^(NSString *socEvent, NSDictionary *socketDic) {
  714. [weakSelf.liveCtrlView socketChatDic:socketDic];
  715. };
  716. //用户离开
  717. [YBSocketLive liveSocketManeger].userLeave = ^(NSString *socEvent, NSDictionary *socketDic) {
  718. [weakSelf.liveCtrlView socketUserLeave:socketDic];
  719. };
  720. //用户进入
  721. [YBSocketLive liveSocketManeger].userEnter = ^(NSString *socEvent, NSDictionary *socketDic) {
  722. [weakSelf.liveCtrlView socketUserEnter:socketDic];
  723. };
  724. //系统消息
  725. [YBSocketLive liveSocketManeger].systemMsg = ^(NSString *socEvent, NSDictionary *socketDic) {
  726. [weakSelf.liveCtrlView socketChatDic:socketDic];
  727. };
  728. //送礼物
  729. [YBSocketLive liveSocketManeger].sendGift = ^(NSString *socEvent, NSDictionary *socketDic) {
  730. [weakSelf.liveCtrlView socketSendGift:socketDic];
  731. };
  732. //其他设备登陆
  733. [YBSocketLive liveSocketManeger].otherDeviceLogin = ^(NSString *socEvent, NSDictionary *socketDic) {
  734. [weakSelf showAlertMsg];
  735. };
  736. //购买守护
  737. [YBSocketLive liveSocketManeger].buyGuardMsg = ^(NSString *socEvent, NSDictionary *socketDic) {
  738. [weakSelf.liveCtrlView socketUpdateGuardMsg:socketDic];
  739. };
  740. //连麦用户流信息
  741. [YBSocketLive liveSocketManeger].linkPlayMsg = ^(NSString *socEvent, NSDictionary *socketDic) {
  742. [weakSelf.liveCtrlView socketShowLinkUserPop:socketDic];
  743. };
  744. //连麦用户挂断
  745. [YBSocketLive liveSocketManeger].linkUserDisconnect = ^(NSString *socEvent, NSDictionary *socketDic) {
  746. [weakSelf.liveCtrlView socketLinkUserDisconnect:socketDic];
  747. };
  748. //收到连麦请求
  749. [YBSocketLive liveSocketManeger].linkHostToHostRequest = ^(NSString *socEvent, NSDictionary *socketDic) {
  750. [weakSelf.liveCtrlView destroyAnchorOnlineList];
  751. };
  752. //主播-主播连成功
  753. [YBSocketLive liveSocketManeger].linkHostToHostMsg = ^(NSString *socEvent, NSDictionary *socketDic) {
  754. [weakSelf.liveCtrlView socketLinkHostToHostSuc:socketDic];
  755. [weakSelf changeLinkPlayer:YES toHostid:minstr([socketDic valueForKey:@"pkuid"])];
  756. };
  757. //主播-主播挂断
  758. [YBSocketLive liveSocketManeger].linkHostToHostDisconnect = ^(NSString *socEvent, NSDictionary *socketDic) {
  759. [weakSelf.liveCtrlView socketlinkHostToHostDisconnect:socketDic];
  760. [weakSelf changeLinkPlayer:NO toHostid:@""];
  761. };
  762. //收到PK请求【只需隐藏自己的发起PK按钮】
  763. [YBSocketLive liveSocketManeger].linkPKGetRequest = ^(NSString *socEvent, NSDictionary *socketDic) {
  764. _liveCtrlView.startPkBtn.hidden = YES;
  765. };
  766. //PK-拒绝
  767. [YBSocketLive liveSocketManeger].linkPKUnAgree = ^(NSString *socEvent, NSDictionary *socketDic) {
  768. [weakSelf startPKBtnShow:YES];
  769. };
  770. //PK-开始
  771. [YBSocketLive liveSocketManeger].linkPKSuc = ^(NSString *socEvent, NSDictionary *socketDic) {
  772. [weakSelf startPKBtnShow:NO];
  773. [weakSelf.liveCtrlView socketLinkPKStart:socketDic];
  774. };
  775. //PK-忙碌
  776. [YBSocketLive liveSocketManeger].linkPKBusy = ^(NSString *socEvent, NSDictionary *socketDic) {
  777. [weakSelf startPKBtnShow:YES];
  778. };
  779. //PK-超时
  780. [YBSocketLive liveSocketManeger].linkPKTimeout = ^(NSString *socEvent, NSDictionary *socketDic) {
  781. [weakSelf startPKBtnShow:YES];
  782. };
  783. //PK-结果
  784. [YBSocketLive liveSocketManeger].linkPKResult = ^(NSString *socEvent, NSDictionary *socketDic) {
  785. [weakSelf.liveCtrlView socketLinkPKReult:socketDic];
  786. };
  787. //PK-进度
  788. [YBSocketLive liveSocketManeger].linkPKProgress = ^(NSString *socEvent, NSDictionary *socketDic) {
  789. [weakSelf.liveCtrlView socketLinkPKProgress:socketDic];
  790. };
  791. }
  792. -(void)startPKBtnShow:(BOOL)isShow{
  793. _liveCtrlView.startPkBtn.hidden = !isShow;
  794. [_liveCtrlView cancelPKAlert];
  795. }
  796. -(void)changeLinkPlayer:(BOOL)isLink toHostid:(NSString *)toHostid{
  797. if (isLink) {
  798. [_liveCtrlView showToHostInfoWithId:toHostid];
  799. _txPushPreview.frame = CGRectMake(0, 130+statusbarHeight, _window_width/2, _window_width*2/3);
  800. }else {
  801. [_liveCtrlView destroyPkTohostInfo];
  802. _txPushPreview.frame = CGRectMake(0, 0, _window_width, _window_height);
  803. }
  804. }
  805. -(void)showAlertMsg {
  806. /// 220421弹窗去掉直接关播
  807. [self hostStopRoom];
  808. /*
  809. YBWeakSelf;
  810. NSDictionary *contentDic = @{@"title":@"",@"msg":YZMsg(@"当前账号已在其他设备登录"),@"left":@"",@"right":YZMsg(@"确定")};
  811. [YBAlertView showAlertView:contentDic complete:^(int eventType) {
  812. if (eventType == 1) {
  813. [weakSelf hostStopRoom];
  814. }
  815. }];
  816. */
  817. }
  818. #pragma mark - 预览层回调
  819. -(void)previewCallBack:(NSString *)eventType andEvnent:(NSString *)eventContent {
  820. NSLog(@"预览type:%@-con:%@",eventType,eventContent);
  821. if ([eventType isEqual:@"预览-切换"]) {
  822. [self rotateCamera];
  823. }
  824. if ([eventType isEqual:@"预览-关闭"]) {
  825. [self callBackClosePreView];
  826. }
  827. if ([eventType isEqual:@"预览-开直播"]) {
  828. [self callBackStartLive];
  829. }
  830. if ([eventType isEqual:@"预览-美颜"]) {
  831. [self showBeautyUI];
  832. }
  833. if ([eventType isEqual:@"预览-分享"]) {
  834. selectShareName = eventContent;
  835. }
  836. if ([eventType isEqual:@"预览-分类"]) {
  837. liveClassStr = eventContent;
  838. }
  839. if ([eventType isEqual:@"预览-上热门"]) {
  840. [self goUpHotView];
  841. }
  842. }
  843. #pragma mark - 上热门
  844. -(void)goUpHotView{
  845. YBWeakSelf;
  846. _hotView = [[UpHotView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
  847. _hotView.closeEvent = ^(NSString *status, NSString *orderid) {
  848. if([status isEqual:@"确定"]){
  849. uphotOrderId = orderid;
  850. }
  851. [weakSelf.hotView removeFromSuperview];
  852. weakSelf.hotView = nil;
  853. };
  854. [self.view addSubview:_hotView];
  855. }
  856. - (void)callBackClosePreView{
  857. [self txStopRtmp];
  858. // [self destroyMhSource];
  859. [UIApplication sharedApplication].idleTimerDisabled = NO;
  860. [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isLiveing"];
  861. [self clickNaviLeftBtn];
  862. }
  863. -(void)callBackStartLive {
  864. if ([PublicObj checkNull:minstr(selectShareName)]) {
  865. [self createRoom];
  866. }else {
  867. YBWeakSelf;
  868. NSDictionary *shareDic = @{@"hostName":[Config getOwnNicename],@"thumb_s":[Config getUserAvatarThumb],@"hostId":[Config getOwnID],@"liveTitle":_liveTitle};
  869. [[YBShareView noShareUIManager]executeShareWithType:RKShareType_LivePre shareType:selectShareName parameter:shareDic complete:^(int codeEvent, NSString *nums) {
  870. [weakSelf createRoom];
  871. }];
  872. }
  873. }
  874. #pragma mark - 开始直播请求
  875. -(void)createRoom {
  876. if ([liveClassStr isEqual:@"-99999999"]) {
  877. [MBProgressHUD showPop:YZMsg(@"请选择直播频道")];
  878. return;
  879. }
  880. _liveTitle = minstr(_livePreview.inputTitleStr);
  881. NSString *deviceinfo = [NSString stringWithFormat:@"%@_%@_%@",[PublicObj iphoneType],[[UIDevice currentDevice] systemVersion],[PublicObj getNetworkType]];
  882. NSDictionary *postDic = @{@"title":minstr(_livePreview.inputTitleStr),
  883. @"province":@"",
  884. @"city":(_livePreview.locationSwitch&&[cityDefault getLocationCity])?[cityDefault getLocationCity]:@"",
  885. @"lng":(_livePreview.locationSwitch&&[cityDefault getLocationLng])?[cityDefault getLocationLng]:@"",
  886. @"lat":(_livePreview.locationSwitch&&[cityDefault getLocationLat])?[cityDefault getLocationLat]:@"",
  887. @"deviceinfo":deviceinfo,
  888. @"uid":[Config getOwnID],
  889. @"token":[Config getOwnToken],
  890. @"isshop":@(_livePreview.cartOpen),
  891. @"liveclassid":liveClassStr,
  892. @"lang":[YBLanguageTools serviceLang],
  893. };
  894. [MBProgressHUD showMessage:@""];
  895. //AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
  896. NSString *postUrl = [purl stringByAppendingFormat:@"?service=Live.createRoom"];
  897. [[YBNetworking ybnetManager] POST:postUrl parameters:postDic headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
  898. if (_livePreview.thumbImage) {
  899. [formData appendPartWithFileData:[Utils compressImage:_livePreview.thumbImage] name:@"file" fileName:@"duibinaf.png" mimeType:@"image/jpeg"];
  900. }
  901. } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  902. [MBProgressHUD hideHUD];
  903. NSString *retStr = minstr([responseObject valueForKey:@"ret"]);
  904. if ([retStr isEqual:@"200"]) {
  905. NSDictionary *data = [responseObject valueForKey:@"data"];
  906. NSString *code = [NSString stringWithFormat:@"%@",[data valueForKey:@"code"]];
  907. if ([code isEqual:@"0"]) {
  908. _roomDic = [[data valueForKey:@"info"]firstObject];
  909. _livePreview.hidden = YES;
  910. _isPreView = NO;
  911. _liveCtrlView.hidden = NO;
  912. //开播动画
  913. [_liveCtrlView startLiveAnimation];
  914. [_liveCtrlView createroomUpdateInfo:_roomDic];
  915. _liveCtrlView.cartBtn.hidden = !_livePreview.cartOpen;
  916. [_liveCtrlView resetPKBtnFrame];
  917. }else {
  918. [MBProgressHUD showPop:[data valueForKey:@"msg"]];
  919. }
  920. }else {
  921. [MBProgressHUD showPop:[responseObject valueForKey:@"msg"]];
  922. }
  923. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  924. [MBProgressHUD hideHUD];
  925. [MBProgressHUD showError:YZMsg(@"网络错误")];
  926. }];
  927. }
  928. #pragma mark - 检查开播状态
  929. - (void)checkLiveingStatus{
  930. [YBNetworking postWithUrl:@"Live.checkLiveing" Dic:@{@"stream":minstr([_roomDic valueForKey:@"stream"])} Suc:^(int code, id info, NSString *msg) {
  931. if (code == 0) {
  932. NSDictionary *dic = [info firstObject];
  933. NSLog(@"info=%@",info);
  934. if ([minstr([dic valueForKey:@"status"]) isEqual:@"0"]) {
  935. [self showNetCloseAlert];
  936. [self getCloseShow];
  937. }
  938. }
  939. } Fail:nil];
  940. }
  941. -(void)showNetCloseAlert {
  942. //[MBProgressHUD showPop:YZMsg(@"网络连接出错,直播结束")];
  943. NSDictionary *contentDic = @{@"title":YZMsg(@"提示"),@"msg":YZMsg(@"网络连接出错,直播结束"),@"left":@"",@"right":YZMsg(@"确定")};
  944. [YBAlertView showAlertView:contentDic complete:^(int eventType) {
  945. }];
  946. }
  947. #pragma mark - 更改开播状态
  948. -(void)changePlayState:(int)status{
  949. NSDictionary *changelive = @{
  950. @"stream":_hostStream,
  951. // @"status":[NSString stringWithFormat:@"%d",status]
  952. @"hotlive_orderid":uphotOrderId
  953. };
  954. [YBNetworking postWithUrl:@"Live.changeLive" Dic:changelive Suc:^(int code, id info, NSString *msg) {
  955. } Fail:nil];
  956. }
  957. #pragma mark - 通知
  958. -(void)addNotification{
  959. //注册进入后台的处理
  960. NSNotificationCenter* notification = [NSNotificationCenter defaultCenter];
  961. [notification addObserver:self selector:@selector(appactive) name:UIApplicationDidBecomeActiveNotification object:nil];
  962. [notification addObserver:self selector:@selector(appnoactive) name:UIApplicationWillResignActiveNotification object:nil];
  963. [notification addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  964. [notification addObserver:self selector:@selector(keyboardWillHidden) name:UIKeyboardWillHideNotification object:nil];
  965. [notification addObserver:self selector:@selector(shajincheng) name:@"shajincheng" object:nil];
  966. //获取所有未读消息
  967. [[V2TIMManager sharedInstance] addConversationListener:self];
  968. }
  969. // 收到所有会话的未读总数变更通知
  970. - (void)onTotalUnreadMessageCountChanged:(UInt64)totalUnreadCount {
  971. [self requestJmsg];
  972. }
  973. -(void)keyboardWillShow:(NSNotification *)not {
  974. //获取键盘的高度
  975. NSDictionary *userInfo = [not userInfo];
  976. NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  977. CGRect keyboardRect = [aValue CGRectValue];
  978. CGFloat height = keyboardRect.size.height;
  979. [UIView animateWithDuration:0.3 animations:^{
  980. if (_liveCtrlView) {
  981. _liveCtrlView.keyBoardIsShow = YES;
  982. _liveCtrlView.top = - height;
  983. [_liveCtrlView keyBoardNoticeIsShow:YES andHeight:height];
  984. }
  985. }completion:^(BOOL finished) {
  986. [self strangeExcursion];
  987. }];
  988. }
  989. -(void)strangeExcursion{
  990. //很奇怪刘海平手机会偏移 37
  991. [UIView animateWithDuration:0.3 animations:^{
  992. self.view.top = 0;
  993. }];
  994. }
  995. -(void)keyboardWillHidden {
  996. [UIView animateWithDuration:0.3 animations:^{
  997. if (_liveCtrlView) {
  998. _liveCtrlView.keyBoardIsShow = NO;
  999. _liveCtrlView.top = 0;
  1000. _liveCtrlView.chatTool.hidden = YES;
  1001. _liveCtrlView.botBtnFunView.hidden = NO;
  1002. [_liveCtrlView keyBoardNoticeIsShow:NO andHeight:0];
  1003. }
  1004. }];
  1005. }
  1006. -(void)shajincheng{
  1007. [self dismissVC];
  1008. }
  1009. -(void)appactive{
  1010. if (_isPreView) {
  1011. return;
  1012. }
  1013. NSLog(@"哈哈哈哈哈哈哈哈哈哈哈哈 app回到前台");
  1014. [[YBLiveRTCManager shareInstance]resumePush];
  1015. [self forwardGround];
  1016. }
  1017. -(void)appnoactive{
  1018. if (_isPreView) {
  1019. return;
  1020. }
  1021. [[YBLiveRTCManager shareInstance]pausePush];
  1022. [self backGround];
  1023. NSLog(@"0000000000000000000 app进入后台");
  1024. }
  1025. -(void)backgroundselector{
  1026. backTime +=1;
  1027. NSLog(@"返回后台时间%d",backTime);
  1028. if (backTime > _canBackTime) {
  1029. [self hostStopRoom];
  1030. }
  1031. }
  1032. -(void)backGround{
  1033. if (!backGroundTimer) {
  1034. [self sendEmccBack];
  1035. backGroundTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backgroundselector) userInfo:nil repeats:YES];
  1036. }
  1037. }
  1038. -(void)forwardGround{
  1039. if (backTime != 0) {
  1040. [[YBSocketLive liveSocketManeger] liveSendPhoneCall:@"主播回来了" andEn:@"The anchor comes back"];
  1041. }
  1042. if (backTime > _canBackTime) {
  1043. [self hostStopRoom];
  1044. }
  1045. if (isclosenetwork == NO) {
  1046. [backGroundTimer invalidate];
  1047. backGroundTimer = nil;
  1048. backTime = 0;
  1049. }
  1050. }
  1051. //来电话
  1052. -(void)sendEmccBack {
  1053. [[YBSocketLive liveSocketManeger] liveSendPhoneCall:@"主播离开一下,精彩不中断,不要走开哦" andEn:@"The anchor leaves for a moment. The stream must go on, don't go away"];
  1054. }
  1055. #pragma mark - 网络
  1056. -(void)netMonitoring {
  1057. YBWeakSelf;
  1058. [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  1059. switch (status) {
  1060. case AFNetworkReachabilityStatusUnknown:
  1061. NSLog(@"未识别的网络");
  1062. isclosenetwork = YES;
  1063. [weakSelf backGround];
  1064. break;
  1065. case AFNetworkReachabilityStatusNotReachable:
  1066. NSLog(@"不可达的网络(未连接)");
  1067. isclosenetwork = YES;
  1068. [weakSelf backGround];
  1069. break;
  1070. case AFNetworkReachabilityStatusReachableViaWWAN:
  1071. isclosenetwork = NO;
  1072. [weakSelf forwardGround];
  1073. if (weakSelf.roomDic) {
  1074. [weakSelf checkLiveingStatus];
  1075. }
  1076. break;
  1077. case AFNetworkReachabilityStatusReachableViaWiFi:
  1078. isclosenetwork = NO;
  1079. if (weakSelf.roomDic) {
  1080. [weakSelf checkLiveingStatus];
  1081. }
  1082. [weakSelf forwardGround];
  1083. break;
  1084. default:
  1085. break;
  1086. }
  1087. }];
  1088. [[AFNetworkReachabilityManager sharedManager] startMonitoring];
  1089. self.callCenter = [CTCallCenter new];
  1090. self.callCenter.callEventHandler = ^(CTCall *call) {
  1091. dispatch_async(dispatch_get_main_queue(), ^{
  1092. if ([call.callState isEqualToString:CTCallStateDialing]) {
  1093. NSLog(@"电话主动拨打电话");
  1094. [weakSelf reciverPhoneCall];
  1095. } else if ([call.callState isEqualToString:CTCallStateConnected]) {
  1096. NSLog(@"电话接通");
  1097. [weakSelf reciverPhoneCall];
  1098. } else if ([call.callState isEqualToString:CTCallStateDisconnected]) {
  1099. NSLog(@"电话挂断");
  1100. [weakSelf phoneCallEnd];
  1101. } else if ([call.callState isEqualToString:CTCallStateIncoming]) {
  1102. NSLog(@"电话被叫");
  1103. [weakSelf reciverPhoneCall];
  1104. } else {
  1105. NSLog(@"电话其他状态");
  1106. }
  1107. });
  1108. };
  1109. }
  1110. - (void)reciverPhoneCall{
  1111. [self appnoactive];
  1112. }
  1113. - (void)phoneCallEnd{
  1114. [self appactive];
  1115. }
  1116. #pragma mark - 私信监听
  1117. //- (void)onReceiveMessageRetractEvent:(JMSGMessageRetractEvent *)retractEvent;{
  1118. // [self requestJmsg];
  1119. //}
  1120. //-(void)onReceiveMessage:(JMSGMessage *)message error:(NSError *)error{
  1121. // [self requestJmsg];
  1122. //}
  1123. -(void)requestJmsg {
  1124. [[YBImManager shareInstance]getAllUnredNumExceptUser:@[@"dsp_admin_1",@"dsp_fans",@"dsp_like",@"dsp_at",@"dsp_comment",@"dsp_user_live",@"goodsorder_admin"] complete:^(int allUnread) {
  1125. unRead = allUnread;
  1126. dispatch_async(dispatch_get_main_queue(), ^{
  1127. if (_liveCtrlView) {
  1128. [_liveCtrlView updateUnread:unRead];
  1129. }
  1130. });
  1131. }];
  1132. }
  1133. #pragma mark - set/get
  1134. - (UIImageView *)pkBgIV {
  1135. if (!_pkBgIV) {
  1136. _pkBgIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
  1137. _pkBgIV.contentMode = UIViewContentModeScaleAspectFill;
  1138. _pkBgIV.clipsToBounds = YES;
  1139. //_pkBgIV.image = [UIImage imageNamed:@"pk背景"];
  1140. _pkBgIV.userInteractionEnabled = YES;
  1141. _pkBgIV.backgroundColor = RGB_COLOR(@"#1e1e1e", 1);
  1142. }
  1143. return _pkBgIV;
  1144. }
  1145. - (YBLivePreview *)livePreview {
  1146. if (!_livePreview) {
  1147. YBWeakSelf;
  1148. _livePreview = [[YBLivePreview alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
  1149. _livePreview.livePreEvent = ^(NSString *eventType, NSString *eventContent) {
  1150. [weakSelf previewCallBack:eventType andEvnent:eventContent];
  1151. };
  1152. }
  1153. return _livePreview;
  1154. }
  1155. - (YBLiveCtrlView *)liveCtrlView {
  1156. if (!_liveCtrlView) {
  1157. YBWeakSelf;
  1158. _liveCtrlView = [[YBLiveCtrlView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
  1159. _liveCtrlView.hidden = YES;
  1160. _liveCtrlView.liveCtrEvent = ^(NSString *eventStr, NSDictionary *eventDic) {
  1161. dispatch_async(dispatch_get_main_queue(), ^{
  1162. [weakSelf controlViewCallBack:eventStr andEvnent:eventDic];
  1163. });
  1164. };
  1165. }
  1166. return _liveCtrlView;
  1167. }
  1168. - (YBLiveEndView *)liveEndView {
  1169. if (!_liveEndView) {
  1170. YBWeakSelf;
  1171. _liveEndView = [[YBLiveEndView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
  1172. _liveEndView.liveEndEvent = ^{
  1173. [weakSelf clickNaviLeftBtn];
  1174. };
  1175. }
  1176. return _liveEndView;
  1177. }
  1178. @end