webRtcPlayerViewController.m 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. //
  2. // webRtcPlayerViewController.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/9/2.
  6. //
  7. #import "webRtcPlayerViewController.h"
  8. #import "webRtcPlayerViewController+AdjustBtnFrame.h"
  9. #import "webRtcPlayerViewController+AdjustPlayerViewFrame.h"
  10. #import "webRtcPlayerViewController+AppDelegate.h"
  11. #import "playerSetView.h"
  12. @interface webRtcPlayerViewController ()<MediaStreamClientEventsDelegate>
  13. {
  14. BOOL outputVolumeKVO;/*标记声音监听通知*/
  15. double lastTimestamp;/*最后一帧时间戳*/
  16. NSNumber *lastBytesReceived;/*最后一帧数据量*/
  17. UILabel* netWorkInfoLabel;//网络延时等信息 正式环境屏蔽
  18. //记录上次 线性增长接收包数
  19. long lasPacketsReceived;
  20. //记录上次 总丢包数据
  21. long lastAlllostData;
  22. BOOL isExitType;//退出云机
  23. NSInteger checkLinkTimerNumber;//0 --2 开始发起重连
  24. }
  25. @property (nonatomic, copy) NSTimer *playerSecondTimer; // 定时器-控制按钮
  26. @property (nonatomic, assign) RTCIceConnectionState linkState;
  27. @property (nonatomic, assign) BOOL didHandleRotation;//第一次处理旋转
  28. @property (nonatomic, assign) BOOL needToReportWebRtcType;//上报打洞是否成功
  29. @end
  30. @implementation webRtcPlayerViewController
  31. @synthesize controlBtn;
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // Do any additional setup after loading the view.
  35. [self.toolBar setHidden:YES];
  36. [self.navigationBar setHidden:YES];
  37. [self.navBarBGView setHidden:YES];
  38. [self.view setBackgroundColor:[UIColor blackColor]];
  39. if([DFPlayer sharedPlayer].state == DFPlayerStateBuffering
  40. ||[DFPlayer sharedPlayer].state == DFPlayerStatePlaying){
  41. _isCodeSuspendAudioType = YES;
  42. [[DFPlayer sharedPlayer] df_pause];
  43. }
  44. }
  45. - (void)viewWillAppear:(BOOL)animated {
  46. [super viewWillAppear:animated];
  47. // 屏幕常亮
  48. [UIApplication sharedApplication].idleTimerDisabled = YES;
  49. [[UIApplication sharedApplication] setStatusBarHidden:YES];
  50. }
  51. - (void)viewDidAppear:(BOOL)animated
  52. {
  53. [super viewDidAppear:animated];
  54. [self noEnablePanRightBack];
  55. [self addKVOObserverFun];
  56. //ksharedAppDelegate.supportScreenRotateType = YES;
  57. }
  58. - (void)viewDidDisappear:(BOOL)animated{
  59. [super viewDidDisappear:animated];
  60. [self HandleSomethingByExitVC];
  61. }
  62. #pragma mark 退出事件处理
  63. -(void)HandleSomethingByExitVC
  64. {
  65. [UIApplication sharedApplication].idleTimerDisabled = NO;
  66. [[UIApplication sharedApplication] setStatusBarHidden:NO];
  67. isExitType = YES;
  68. if(_mediaStream){
  69. [_mediaStream disconnect];
  70. _mediaStream = nil;
  71. }
  72. [self enablePanRightBack];
  73. [self removeKVOObserverFun];
  74. ksharedAppDelegate.supportScreenRotateType = NO;
  75. ksharedAppDelegate.isPlayerScreenLandscapeType = NO;
  76. [self removeNewIndicator];
  77. if(_playerSecondTimer){
  78. [_playerSecondTimer invalidate];
  79. _playerSecondTimer = nil;
  80. }
  81. _webRtcMsgMod = nil;
  82. }
  83. - (void)setWebRtcMsgMod:(webRtcMsgModel *)webRtcMsgMod
  84. {
  85. _webRtcMsgMod = webRtcMsgMod;
  86. [self beginWebRtcPlayFun];
  87. }
  88. #pragma mark 开始拉流
  89. - (void)beginWebRtcPlayFun
  90. {
  91. if(_mediaStream){
  92. //[_mediaStream disconnect];
  93. //[_mediaStream removeFromSuperview];
  94. //_mediaStream = nil;
  95. }
  96. _mediaStream = [[RTC_OBJC_TYPE(AMediaStream) alloc] initWithFrame:CGRectZero];
  97. [_mediaStream setEventDelegate:self];
  98. [self.view addSubview:_mediaStream];
  99. HLog(@"_mediaStream:%@",_mediaStream)
  100. [_mediaStream mas_makeConstraints:^(MASConstraintMaker *make) {
  101. make.left.mas_equalTo(0.f);
  102. make.bottom.mas_equalTo(0.f);
  103. make.right.mas_equalTo(0.f);
  104. make.top.mas_equalTo(0.f);
  105. }];
  106. //判断是否是全屏
  107. BOOL fullscreenType = [HWDataManager getBoolWithKey:Consn_player_full_screen_show];
  108. CGSize phoneSize = CGSizeMake(1080.0, 1920.0);
  109. if(fullscreenType){
  110. phoneSize = [RCCommandHelp commondToSetFullScreenPhoneSizeBySize];
  111. }
  112. [self linkWebRtcFunWithSize:phoneSize];
  113. [self initBaseUIFun];
  114. [self setTimerCountDown];
  115. //CGFloat curRate = 1080.0/1920.0;
  116. //CGFloat curRate = phoneSize.width/phoneSize.height;
  117. [self showNewIndicatorWithCanBack:YES canTouch:NO];
  118. }
  119. #pragma mark 链接webrtc
  120. - (void)linkWebRtcFunWithSize:(CGSize)phoneSize
  121. {
  122. //链接用
  123. NSString *signallingUrl = [[NSString alloc] initWithFormat:@"%@:%@",_webRtcMsgMod.data.signalling.domainName,_webRtcMsgMod.data.signalling.port];
  124. NSURL *url = [NSURL URLWithString:signallingUrl];
  125. //ice用
  126. NSString *iceUrl = [[NSString alloc] initWithFormat:@"%@:%@",_webRtcMsgMod.data.turn.domainName,_webRtcMsgMod.data.turn.port];
  127. NSMutableDictionary *ice = [NSMutableDictionary new];
  128. if(iceUrl){
  129. [ice setValue:iceUrl forKey:@"CHINANET"];
  130. [ice setValue:iceUrl forKey:@"CMNET"];
  131. [ice setValue:iceUrl forKey:@"UNICOM"];
  132. }
  133. NSString *roomName = _webRtcMsgMod.data.uniqueIdentifier;
  134. NSInteger cardDensity = 480;
  135. //判断是否是全屏
  136. BOOL fullscreenType = [HWDataManager getBoolWithKey:Consn_player_full_screen_show];
  137. if(fullscreenType){
  138. //cardDensity = 380;
  139. cardDensity = 320;
  140. }
  141. NSInteger result = [_mediaStream start:url
  142. ice:ice
  143. sn:roomName
  144. direct:0
  145. fmt:1//1(h264) 5(h265)
  146. videoWidth:(NSInteger)phoneSize.width
  147. videoHeight:(NSInteger)phoneSize.height
  148. fps:30
  149. bitrate:3200//3000
  150. cardWidth:(NSInteger)phoneSize.width//0//1080//0
  151. cardHeight:(NSInteger)phoneSize.height//0//1920//0
  152. cardDensity:cardDensity
  153. token:@"vclusters"];
  154. HLog(@"result:%ld",result)
  155. [_mediaStream setShouldGetStats:YES];
  156. //清晰度码率最大上限 流畅1800 标清2200 高清2800 跟上面的 bitrate是同一个意思
  157. //[_mediaStream setMaxBitrate:@2800];
  158. }
  159. #pragma mark 重连
  160. - (void)relinkWebRtcFun
  161. {
  162. // if(_linkState == RTCIceConnectionStateConnected
  163. // || _linkState == RTCIceConnectionStateCompleted)
  164. // {//链接中 不处理重连事件
  165. // HLog(@"链接中 不处理重连事件")
  166. // return;
  167. // }
  168. if(isExitType){
  169. return;
  170. }
  171. HLog(@"发起重连 ......")
  172. KWeakSelf
  173. mainBlock(^{
  174. [weakSelf showNewIndicatorWithCanBack:YES canTouch:NO];
  175. //判断是否是全屏
  176. BOOL fullscreenType = [HWDataManager getBoolWithKey:Consn_player_full_screen_show];
  177. CGSize phoneSize = CGSizeMake(1080.0, 1920.0);
  178. if(fullscreenType){
  179. phoneSize = [RCCommandHelp commondToSetFullScreenPhoneSizeBySize];
  180. }
  181. [weakSelf linkWebRtcFunWithSize:phoneSize];
  182. });
  183. }
  184. #pragma mark 初始化其他UI
  185. - (void)initBaseUIFun
  186. {
  187. if(netWorkInfoLabel){
  188. [netWorkInfoLabel removeFromSuperview];
  189. netWorkInfoLabel = nil;
  190. }
  191. netWorkInfoLabel = [[UILabel alloc] init];
  192. [netWorkInfoLabel setTextColor:[UIColor greenColor]];
  193. [netWorkInfoLabel setBackgroundColor:HW000000Color60];
  194. [netWorkInfoLabel setFont:[UIFont systemFontOfSize:17]];
  195. [netWorkInfoLabel setNumberOfLines:0];
  196. [self.view addSubview:netWorkInfoLabel];
  197. [netWorkInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  198. make.top.mas_equalTo(80);
  199. make.left.mas_equalTo(20);
  200. }];
  201. if(_bottomContrView){
  202. [_bottomContrView removeFromSuperview];
  203. _bottomContrView = nil;
  204. }
  205. _bottomContrView = [[webRtcPlayerBottomContrView alloc] init];
  206. [self.view addSubview:_bottomContrView];
  207. [self getPlayerBottomNavShowOrHidefun];
  208. KWeakSelf
  209. _bottomContrView.didClickButtonFun = ^(NSInteger tag) {
  210. [weakSelf didClickBottomFunBy:tag];
  211. };
  212. /*控制按钮*/
  213. if(controlBtn){
  214. [controlBtn removeFromSuperview];
  215. controlBtn = nil;
  216. }
  217. UIImage *driftBtnImage = [UIImage imageNamed:@"you_icon"];
  218. controlBtn = [[UIButton alloc] init];
  219. [controlBtn setBackgroundColor:[UIColor clearColor]];
  220. [controlBtn setBackgroundImage:driftBtnImage forState:(UIControlStateNormal)];
  221. [controlBtn addTarget:self
  222. action:@selector(controlBtnPressed:)
  223. forControlEvents:(UIControlEventTouchUpInside)];
  224. [self.view addSubview:controlBtn];
  225. //拖拽事件等
  226. UIPanGestureRecognizer *gester = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(controlBtnPanGestureCallback:)];
  227. [controlBtn addGestureRecognizer:gester];
  228. //gester.delegate = self;
  229. [self initPointForControlBtnFun];
  230. }
  231. #pragma mark 设置 controlBtn 的初始位置
  232. - (void)initPointForControlBtnFun
  233. {
  234. CGFloat h_w_controlBtn = 60.f;
  235. /*区分横竖屏*/
  236. if (_mediaStream.hw_w > _mediaStream.hw_h){
  237. [controlBtn setFrame:CGRectMake((_mediaStream.hw_w - h_w_controlBtn)/2.f, _mediaStream.hw_h - h_w_controlBtn - 20.f, h_w_controlBtn, h_w_controlBtn)];
  238. }else{
  239. //默认居右
  240. //[mPlayerView.controlBtn setFrame:CGRectMake(mPlayerView.width - h_w_controlBtn - 20.f, (SCREEN_H - h_w_controlBtn)/2.f, h_w_controlBtn, h_w_controlBtn)];
  241. //默认居左
  242. [controlBtn setFrame:CGRectMake( h_w_controlBtn + 10.f, (SCREEN_H - h_w_controlBtn)/2.f, h_w_controlBtn, h_w_controlBtn)];
  243. }
  244. }
  245. #pragma mark 定时器
  246. - (void)setTimerCountDown {
  247. HLog(@"开启一个");
  248. if (_playerSecondTimer) {
  249. // 取消定时器
  250. [_playerSecondTimer invalidate];
  251. _playerSecondTimer = nil;
  252. }
  253. // 初始化值计时数据
  254. //self.adjustTime = 1;
  255. //self.concentTime = [iTools getNowTimeStamp];
  256. _playerSecondTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerChange) userInfo:nil repeats:YES];
  257. [[NSRunLoop currentRunLoop] addTimer:_playerSecondTimer forMode:NSRunLoopCommonModes];
  258. }
  259. #pragma mark 定时器响应事件
  260. - (void)timerChange {
  261. [self extensionAdjustBtnFrameCheckAdjustTime];// 1、悬浮球3s后 自动靠边隐藏
  262. //[self checkConcentTime];// 3、30s之后 没有收到拉流数据 自动断开链接
  263. checkLinkTimerNumber ++;
  264. if(checkLinkTimerNumber ==2){
  265. checkLinkTimerNumber = 0;
  266. if(_linkState != RTCIceConnectionStateConnected
  267. && _linkState != RTCIceConnectionStateCompleted){
  268. [self relinkWebRtcFun];
  269. }
  270. }
  271. }
  272. #pragma mark 控制按钮点击事件
  273. - (void)controlBtnPressed:(UIButton*)but
  274. {
  275. playerSetView *nextVC = [[playerSetView alloc] init];
  276. [ksharedAppDelegate.window addSubview:nextVC];
  277. [nextVC mas_makeConstraints:^(MASConstraintMaker *make) {
  278. make.left.mas_equalTo(0);
  279. make.right.mas_equalTo(0);
  280. make.top.mas_equalTo(0.f);
  281. make.bottom.mas_equalTo(0.f);
  282. }];
  283. KWeakSelf
  284. nextVC.didClickButtonFun = ^(NSInteger tag) {
  285. switch (tag) {
  286. case 10:
  287. {//截图
  288. //self->needScreenShotType = YES;
  289. }
  290. break;
  291. #pragma mark 重启盒子
  292. case 11:
  293. {
  294. [weakSelf didClickRestartFun];
  295. }
  296. break;
  297. case 12:
  298. {//退出云机
  299. [weakSelf exitCloudPhoneFun];
  300. }
  301. break;
  302. case 100:
  303. {
  304. BOOL fullscreenType = [HWDataManager getBoolWithKey:Consn_player_full_screen_show];
  305. if(fullscreenType){
  306. [weakSelf showOpenTVP2PFun];
  307. }
  308. else{
  309. [weakSelf openTvShowFun];
  310. }
  311. }
  312. break;
  313. case 101:
  314. {
  315. weakSelf.controlBtn.userInteractionEnabled = NO;
  316. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  317. self->controlBtn.userInteractionEnabled = YES;
  318. });
  319. [weakSelf showCloseTVP2PFun];
  320. }
  321. break;
  322. default:
  323. break;
  324. }
  325. };
  326. }
  327. #pragma mark 点击了重启云机
  328. - (void)didClickRestartFun
  329. {
  330. KWeakSelf
  331. /*弹窗提示重启*/
  332. ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"my_set_no_restart_phone_tips",nil)
  333. msg:@""
  334. imageStr:@""
  335. cancelTitle:NSLocalizedString(@"other_cancel",nil)
  336. okTitle:NSLocalizedString(@"my_set_no_restart_phone_btn_ok",nil) isOkBtnHighlight:NO
  337. didClickOk:^{
  338. [weakSelf needToRebootFun];
  339. //提示语
  340. [[iToast makeText:NSLocalizedString(@"player_link_rebooting_Tips",nil)] show];
  341. } didClickCancel:^{
  342. }];
  343. nextVC.modalPresentationStyle = UIModalPresentationCustom;
  344. [self presentViewController:nextVC animated:YES completion:^{
  345. nextVC.view.superview.backgroundColor = [UIColor clearColor];
  346. }];
  347. }
  348. #pragma mark p2p通道 重启云机
  349. - (void)needToRebootFun
  350. {
  351. [[webRtcManager shareManager] needToRebootFun];
  352. [self startForceStartTimerFun];
  353. [self pauseStream];
  354. [webRtcManager shareManager].isRebootIngType = YES;
  355. [self showNewIndicatorWithCanBack:YES canTouch:NO];
  356. }
  357. #pragma mark X 秒后检查是否软件重启成功
  358. - (void)startForceStartTimerFun
  359. {
  360. KWeakSelf
  361. //1. 5秒后跟硬件发起硬重启
  362. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  363. [weakSelf updateForceStartFun];
  364. });
  365. //2. 20秒后尝试重连
  366. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  367. [weakSelf relinkWebRtcFun];
  368. [[webRtcManager shareManager] relinkWebRtcFun];
  369. });
  370. }
  371. #pragma mark 需要强制重启
  372. - (void)updateForceStartFun{
  373. NSString* curSn = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  374. NSMutableDictionary *paraDict = [NSMutableDictionary dictionary];
  375. [paraDict setValue:curSn forKey:@"sn"];
  376. [paraDict setValue:[NSNumber numberWithBool:YES] forKey:@"isForceStart"];
  377. [[netWorkManager shareInstance] CommonPostCallBackCode:updateForceStart Parameters:paraDict success:^(id _Nonnull responseObject) {
  378. SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil];
  379. if (model.status == 0) {
  380. }
  381. else
  382. {
  383. }
  384. } failure:^(NSError * _Nonnull error) {
  385. HLog(@"%@", error);
  386. }];
  387. }
  388. #pragma mark 退出云机
  389. - (void)exitCloudPhoneFun
  390. {
  391. //[self setShowImgAndVoiceTypeFun:NO];
  392. if(_isCodeSuspendAudioType){
  393. [[DFPlayer sharedPlayer] df_play];
  394. }
  395. if(isLan){//保证竖屏
  396. [self player_rotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
  397. }
  398. [self.navigationController popViewControllerAnimated:YES];
  399. ksharedAppDelegate.supportScreenRotateType = NO;
  400. [self HandleSomethingByExitVC];
  401. }
  402. #pragma mark 显示关闭投屏提示语
  403. - (void)showOpenTVP2PFun{
  404. /*弹窗提示TV投屏*/
  405. ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"File_upload_Record_clear_Tip_title",nil)
  406. msg:NSLocalizedString(@"cloudPhone_fullscreen_tvshow_tip",nil)
  407. imageStr:@""
  408. cancelTitle:NSLocalizedString(@"other_cancel",nil)
  409. okTitle:NSLocalizedString(@"my_set_TVP2P_Open_sure",nil) isOkBtnHighlight:YES
  410. didClickOk:^{
  411. [self openTvShowFun];
  412. //关闭全屏屏
  413. [HWDataManager setBoolWithKey:Consn_player_full_screen_show value:NO];
  414. [self setPlayerFullScreenNotFun];
  415. } didClickCancel:^{
  416. }];
  417. nextVC.modalPresentationStyle = UIModalPresentationCustom;
  418. [self presentViewController:nextVC animated:YES completion:^{
  419. nextVC.view.superview.backgroundColor = [UIColor clearColor];
  420. }];
  421. }
  422. #pragma mark 开始投屏
  423. - (void)openTvShowFun
  424. {
  425. self.controlBtn.userInteractionEnabled = NO;
  426. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  427. self->controlBtn.userInteractionEnabled = YES;
  428. });
  429. [[webRtcManager shareManager] onTvFun];
  430. [[iToast makeText:NSLocalizedString(@"cloudPhone_TV_show_tip",nil)] show];
  431. //数据埋点
  432. [[netWorkManager shareInstance] DataEmbeddingPointBy:3 withEventValue:@"Cloud_tv"];
  433. }
  434. #pragma mark 显示关闭投屏提示语
  435. - (void)showCloseTVP2PFun{
  436. /*弹窗提示TV投屏*/
  437. ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"my_set_no_close_TV_p2p_tip",nil)
  438. msg:@""
  439. imageStr:@""
  440. cancelTitle:NSLocalizedString(@"other_cancel",nil)
  441. okTitle:NSLocalizedString(@"my_set_TVP2P_Open_sure",nil) isOkBtnHighlight:YES
  442. didClickOk:^{
  443. [self gotoCloseTVP2PFun];
  444. } didClickCancel:^{
  445. //HLog(@"2222");
  446. }];
  447. nextVC.modalPresentationStyle = UIModalPresentationCustom;
  448. [self presentViewController:nextVC animated:YES completion:^{
  449. nextVC.view.superview.backgroundColor = [UIColor clearColor];
  450. }];
  451. }
  452. #pragma mark 确认开始TV投屏
  453. -(void)gotoCloseTVP2PFun
  454. {
  455. [[webRtcManager shareManager] offTvFun];
  456. }
  457. #pragma mark 挤下线功能
  458. -(void)offlineOtherPhoneFun
  459. {
  460. NSString *curOaidStr = [RcGameWQKeyChain getOaidStringFun];
  461. if(!curOaidStr){
  462. curOaidStr = @"";
  463. }
  464. NSString *commondStr = [[NSString alloc] initWithFormat:@"{\"type\":\"login\",\"value\":\"%@\"}",curOaidStr];
  465. [self send_dataInPlayer:commondStr];
  466. }
  467. #pragma mark 单点登录被挤下线弹框
  468. - (void)LogoutByOtherFun:(NSString*)oaid
  469. {
  470. if(logoutAlertVC){
  471. return;
  472. }
  473. NSString *curOaidStr = [RcGameWQKeyChain getOaidStringFun];
  474. if(oaid
  475. && oaid.length >0
  476. && [oaid isEqualToString:curOaidStr]){
  477. return;
  478. }
  479. //[self didReceiveLogoutMsgFun];
  480. //yyyy-MM-dd HH:mm:ss
  481. NSString*dateStr = [iTools getNowTimeString2];
  482. if(dateStr && dateStr.length == 19){
  483. dateStr = [dateStr substringWithRange:NSMakeRange(11, 5)];
  484. }
  485. NSString *LogoutTimerStr = [[NSString alloc] initWithFormat:@"%@%@%@",NSLocalizedString(@"single_sign_on_Tips_one",nil),dateStr,NSLocalizedString(@"single_sign_on_Tips_two",nil)];
  486. NSString * loginAgainStr = NSLocalizedString(@"single_sign_on_login_again",nil);
  487. BOOL isOkBtnHighlight = YES;
  488. if(!ksharedAppDelegate.DeviceThirdIdMod.data.isPrivacyMode){
  489. loginAgainStr = @"";
  490. isOkBtnHighlight = NO;
  491. }
  492. KWeakSelf
  493. if(ksharedAppDelegate.isDidShowPwdType){
  494. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  495. [weakSelf LogoutByOtherFun:oaid];
  496. HLog(@"weakSelf LogoutByOtherFun")
  497. });
  498. return;
  499. }
  500. //被挤下线 停止拉流
  501. [self pauseStream];
  502. if(isLan){//保证竖屏
  503. [self player_rotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
  504. }
  505. //退出推流页
  506. [self.navigationController popViewControllerAnimated:NO];
  507. //与安卓保存一致 tabbar 退倒nas页
  508. [ksharedAppDelegate.mainTabBar setSelectedIndex:0];
  509. [self HandleSomethingByExitVC];
  510. /*弹窗提示重启*/
  511. //KWeakSelf
  512. logoutAlertVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"single_sign_on_Tips_logout",nil)
  513. msg:LogoutTimerStr
  514. imageStr:@""
  515. cancelTitle:NSLocalizedString(@"single_sign_on_exit",nil)
  516. okTitle:loginAgainStr isOkBtnHighlight:isOkBtnHighlight
  517. didClickOk:^{
  518. self->logoutAlertVC = nil;
  519. [[NSNotificationCenter defaultCenter] postNotificationName:lockBypwdNotification object:nil];
  520. } didClickCancel:^{
  521. exit(0);/*强制退出app*/
  522. }];
  523. logoutAlertVC.modalPresentationStyle = UIModalPresentationCustom;
  524. logoutAlertVC.view.backgroundColor = [UIColor blackColor];
  525. [[iTools appRootViewController] presentViewController:logoutAlertVC animated:YES completion:^{
  526. self->logoutAlertVC.view.superview.backgroundColor = [UIColor clearColor];
  527. }];
  528. }
  529. #pragma mark 底部按钮事件
  530. - (void)didClickBottomFunBy:(NSInteger)tag
  531. {
  532. int keyType = 0;
  533. if(tag == 1){//task
  534. keyType = 187;
  535. }
  536. else if(tag == 2){//home
  537. keyType = 3;
  538. }
  539. else if(tag == 3){//back
  540. keyType = 4;
  541. }
  542. if(keyType > 0){
  543. [self didClickKeyEventFunBy:keyType];
  544. }
  545. }
  546. #pragma mark 按键事件
  547. // home 3 back 4 task 187 volumeUp 24 volumeDown 25
  548. - (void)didClickKeyEventFunBy:(int)keyType
  549. {
  550. [_mediaStream sendKey:keyType];
  551. }
  552. #pragma mark 云机交互的消息发送 走P2P通道
  553. -(void)send_data:(NSString*)commandStr
  554. {
  555. [[webRtcManager shareManager] send_data:commandStr];
  556. }
  557. #pragma mark 云机交互的消息发送 走音视频推拉流通道
  558. -(void)send_dataInPlayer:(NSString*)commandStr
  559. {
  560. [_mediaStream sendData:commandStr];
  561. }
  562. #pragma mark -- /*底部导航栏开关通知*/
  563. - (void)getPlayerBottomNavShowOrHidefun
  564. {
  565. BOOL haveShowBottonNavType = ![HWDataManager getBoolWithKey:Consn_player_Nav_hide];
  566. if(haveShowBottonNavType){
  567. _bottomContrView.hidden = NO;
  568. }
  569. else{
  570. _bottomContrView.hidden = YES;
  571. }
  572. }
  573. #pragma mark -- /*全面屏开关通知*/
  574. - (void)setPlayerFullScreenNotFun
  575. {
  576. BOOL fullscreenType = [HWDataManager getBoolWithKey:Consn_player_full_screen_show];
  577. CGFloat tempRate = 0.0;
  578. //重新设置分辨率
  579. if(fullscreenType){
  580. NSInteger cardDensity = 320;//380;//480;//422;//380;//460;
  581. CGSize size = [RCCommandHelp commondToSetFullScreenPhoneSizeBySize];
  582. if(isLan){
  583. if(size.width < size.height){
  584. size = CGSizeMake(size.height,size.width);
  585. }
  586. }
  587. [self setCardSize:size.width cardHeight:size.height cardDensity:cardDensity];
  588. tempRate = (size.width *1.0)/(size.height *1.0);
  589. }
  590. else{
  591. CGSize size = CGSizeMake(1080.0, 1920.0);
  592. if(isLan){
  593. size = CGSizeMake(1920.0, 1080.0);
  594. }
  595. [self setCardSize:(NSInteger)size.width cardHeight:(NSInteger)size.height cardDensity:480];
  596. tempRate = size.width/size.height;
  597. }
  598. //dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  599. if (tempRate > 1) {/*横屏*/
  600. [self setLanMas_makeWithImageRate:tempRate];
  601. }else{/*竖屏*/
  602. [self setPoMas_makeWithImageRate:tempRate];
  603. }
  604. // });
  605. }
  606. #pragma mark -- /*推流中修改卡的分辨率*/
  607. - (void)setCardSize:(NSInteger)cardWidth
  608. cardHeight:(NSInteger)cardHeight
  609. cardDensity:(NSInteger)cardDensit
  610. {
  611. if(isLan){
  612. HLog(@"云机屏幕 设置 宽:%ld 高:%ld",cardHeight,cardWidth);
  613. [_mediaStream setCardSize:cardHeight cardHeight:cardWidth cardDensity:cardDensit];
  614. [_mediaStream setVideoSize:cardHeight videoHeight:cardWidth];
  615. }
  616. else{
  617. HLog(@"云机屏幕 设置 宽:%ld 高:%ld",cardWidth,cardHeight);
  618. [_mediaStream setCardSize:cardWidth cardHeight:cardHeight cardDensity:cardDensit];
  619. [_mediaStream setVideoSize:cardWidth videoHeight:cardHeight];
  620. }
  621. }
  622. #pragma mark -- /*通知添加与移除*/
  623. - (void)addKVOObserverFun
  624. {
  625. outputVolumeKVO = YES;
  626. [[AVAudioSession sharedInstance] addObserver:self forKeyPath:@"outputVolume" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:(void *)[AVAudioSession sharedInstance]];
  627. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getPlayerBottomNavShowOrHidefun) name:getPlayerBottomNavNotification object:nil];
  628. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setPlayerFullScreenNotFun) name:setPlayerFullScreenNotification object:nil];
  629. //监听系统 前后台事件
  630. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
  631. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
  632. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
  633. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
  634. //输入密码完成
  635. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didInpuPwdOkFun) name:didInputPWDNotification object:nil];
  636. }
  637. - (void)removeKVOObserverFun
  638. {
  639. if (outputVolumeKVO)
  640. {
  641. [[AVAudioSession sharedInstance] removeObserver:self forKeyPath:@"outputVolume" context:(void *)[AVAudioSession sharedInstance]];
  642. outputVolumeKVO = NO;
  643. }
  644. [[NSNotificationCenter defaultCenter] removeObserver:self];
  645. }
  646. #pragma mark 系统音量键监听
  647. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
  648. if(context == (__bridge void *)[AVAudioSession sharedInstance])
  649. {
  650. /*音量开关打开时 允许发送指令 否则直接返回*/
  651. float newValue = [[change objectForKey:@"new"] floatValue];
  652. float oldValue = [[change objectForKey:@"old"] floatValue];
  653. //HLog(@"音量 --old: %f ---new: %f",oldValue,newValue)
  654. if (newValue > oldValue)
  655. {
  656. HLog(@"\n-----音量增加");
  657. [self didClickKeyEventFunBy:24];
  658. }
  659. else
  660. {
  661. HLog(@"\n-----音量降低");
  662. [self didClickKeyEventFunBy:25];
  663. }
  664. }
  665. }
  666. #pragma mark 监听到云机的宽高以及屏幕方向
  667. - (void)handlUIAfterGetCloudPhoneVideoWidth:(int)videoWidth videoHeight:(int)videoHeight rotation:(int)rotation
  668. {
  669. HLog(@"云机屏幕 回调 宽:%d 高:%d 横竖屏:%d",videoWidth,videoHeight,rotation);
  670. CGFloat curRate = (CGFloat)videoWidth/(CGFloat)videoHeight;
  671. if(lastVideoWHRate == curRate){
  672. return;
  673. }
  674. didAdjusBtnType = NO;
  675. if(rotation == 0){//竖屏
  676. //切换到竖屏
  677. isLan = NO;
  678. [self player_rotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
  679. [self setPoMas_makeWithImageRate:curRate];
  680. }
  681. else if(rotation == 1){//横屏
  682. //切换到横屏屏
  683. isLan = YES;
  684. [self player_rotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
  685. [self setLanMas_makeWithImageRate:curRate];
  686. }
  687. _didHandleRotation = YES;
  688. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  689. [self extensionAdjustBtnFrameCheckAdjustTime];
  690. });
  691. }
  692. #pragma mark WebRTC 回调 MediaStreamClientEventsDelegate
  693. #pragma mark - 宽高变化
  694. -(void)onFrameResolutionChangedFromPeerName:(NSString*)peerName videoWidth:(int)videoWidth videoHeight:(int)videoHeight rotation:(int)rotation {
  695. HLog(@"推拉流 onFrameResolutionChangedFromPeerName:%@---%d---%d--%d",peerName,videoWidth,videoHeight,rotation)
  696. HLog(@"ok _mediaStream:%@",_mediaStream)
  697. mainBlock(^{
  698. [self handlUIAfterGetCloudPhoneVideoWidth:videoWidth videoHeight:videoHeight rotation:rotation];
  699. });
  700. }
  701. //code 0 成功 1失败
  702. -(void)onAuthResultFromPeerName:(NSString*)peerName code:(int)code descriptions:(NSString*)descriptions
  703. {
  704. HLog(@"onAuthResultFromPeerName:%@---%d---%@",peerName,code,descriptions)
  705. }
  706. #pragma mark 链接发生变化
  707. -(void)onChangeConnectionStateFromPeerName:(NSString*)peerName didChangeIceConnectionState:(RTCIceConnectionState)state
  708. {
  709. HLog(@"推拉流 onChangeConnectionStateFromPeerName: state:%ld",state)
  710. _linkState = state;
  711. switch (state) {
  712. case RTCIceConnectionStateConnected:{
  713. //链接成功
  714. mainBlock(^{
  715. [self removeNewIndicator];
  716. });
  717. }
  718. break;
  719. case RTCIceConnectionStateCompleted:
  720. //链接完成
  721. break;
  722. case RTCIceConnectionStateFailed:
  723. case RTCIceConnectionStateDisconnected:
  724. case RTCIceConnectionStateClosed:
  725. //链接关闭
  726. [self relinkWebRtcFun];
  727. break;
  728. default:
  729. break;
  730. }
  731. }
  732. - (void)dataChannelDidChangeFromPeerName:(NSString*)peerName State:(RTCDataChannelState)state;
  733. {
  734. HLog(@"推拉流 dataChannelDidChangeFromPeerName: state:%ld",state)
  735. switch (state) {
  736. case RTCDataChannelStateConnecting:
  737. {
  738. }
  739. break;
  740. case RTCDataChannelStateOpen:
  741. {
  742. //链接成功
  743. [self offlineOtherPhoneFun];
  744. }
  745. break;
  746. case RTCDataChannelStateClosing:
  747. {
  748. }
  749. break;
  750. case RTCDataChannelStateClosed:
  751. {
  752. //链接断开
  753. [self relinkWebRtcFun];
  754. }
  755. break;
  756. default:
  757. break;
  758. }
  759. }
  760. -(void)onChannelDataFromPeerName:(NSString*)peerName buffer:(RTC_OBJC_TYPE(RTCDataBuffer) *)buffer
  761. {
  762. //HLog(@"onIceConnectedFromPeerName:%@",buffer.data);
  763. if(buffer && buffer.data){
  764. KWeakSelf
  765. mainBlock(^{
  766. [weakSelf handleWebRtcMsgResponseBy:buffer.data];
  767. });
  768. }
  769. }
  770. -(void)didGetStats:(NSString*)peerName stats:(RTC_OBJC_TYPE(RTCStatisticsReport) *)stats {
  771. if(!_needToReportWebRtcType){
  772. [[webRtcManager shareManager] reportWebRtcRePoportTypeIsChannel:NO withStats:stats];
  773. _needToReportWebRtcType = YES;
  774. }
  775. NSString *selectedCandidatePairId = nil;
  776. NSString *localCandidateId = nil;
  777. NSString *remoteCandidateId = nil;
  778. /*延时数据*/
  779. NSNumber *currentRoundTripTime = nil;
  780. //线性增长接收包数
  781. long packetsReceived = 0;
  782. //视频丢包数据
  783. long videolostData = 0;
  784. //音频丢包数据
  785. long audiolostData = 0;
  786. //总丢包数据
  787. long alllostData = 0;
  788. //每秒帧数 ----界面展示的帧率
  789. NSInteger framesPerSecond = 0;
  790. //计算传输速度
  791. long preReceive = 0;
  792. KWeakSelf
  793. for (NSString *key in [stats.statistics allKeys]) {
  794. RTCStatistics *rtcStatistics = [stats.statistics objectForKey:key];
  795. //HLog(@"hxd11111---->%@",rtcStatistics.type);
  796. if ([rtcStatistics.type isEqualToString:@"transport"]) {
  797. //HLog(@"传输数据---->%@",rtcStatistics.values);
  798. double timestamp = rtcStatistics.timestamp_us;
  799. NSNumber *bytesReceived = [rtcStatistics.values objectForKey:@"bytesReceived"];
  800. NSNumber *packetsReceivedNumber = [rtcStatistics.values objectForKey:@"packetsReceived"];
  801. packetsReceived = [packetsReceivedNumber longValue];
  802. if (lastTimestamp > 0) {
  803. long diffReceive = [bytesReceived longValue] - [lastBytesReceived longValue];
  804. double diffTime = timestamp - lastTimestamp;
  805. preReceive = (diffReceive / diffTime) * 1000000;
  806. //HLog(@"传输数据速度---->%ld",preReceive);
  807. // mainBlock((^{
  808. // if (self->mPlayerView.upControlView.hidden == NO){
  809. // NSString *str = [NSString stringWithFormat:@"清晰度 %ldk/s",preReceive/1024];
  810. // NSMutableAttributedString *underAttr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:[UIColor hwColor:@"#FFFFFF"]}];
  811. // [underAttr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} range:NSMakeRange(0, str.length - 2)];
  812. // [underAttr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} range:NSMakeRange(str.length - 2,2)];
  813. // [self->mPlayerView.upControlView.articulationBtn setAttributedTitle:underAttr forState:(UIControlStateNormal)];
  814. // }else{
  815. // NSString *str = [NSString stringWithFormat:@"%ldk/s",preReceive/1024];
  816. // NSMutableAttributedString *underAttr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:[UIColor hwColor:@"#FFFFFF"]}];
  817. // [underAttr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} range:NSMakeRange(0, str.length - 2)];
  818. // [underAttr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} range:NSMakeRange(str.length - 2,2)];
  819. // [self->mPlayerView.controlBtn setAttributedTitle:underAttr forState:(UIControlStateNormal)];
  820. // }
  821. // }));
  822. }else{
  823. lastTimestamp = timestamp;
  824. lastBytesReceived = bytesReceived;
  825. }
  826. selectedCandidatePairId = [rtcStatistics.values objectForKey:@"selectedCandidatePairId"];
  827. //HLog(@"三网数据--selectedCandidatePairId-->%@",selectedCandidatePairId);
  828. }
  829. /*丢包数据*/
  830. if ([rtcStatistics.type isEqualToString:@"inbound-rtp"]) {
  831. NSString *kind = [rtcStatistics.values objectForKey:@"kind"];
  832. if([kind isEqualToString:@"video"]){
  833. NSNumber*videolostDataNumber = [rtcStatistics.values objectForKey:@"packetsLost"];
  834. videolostData = [videolostDataNumber longValue];
  835. NSNumber*framesPerSecondNumber = [rtcStatistics.values objectForKey:@"framesPerSecond"];
  836. framesPerSecond = [framesPerSecondNumber integerValue];
  837. }
  838. else if([kind isEqualToString:@"audio"]){
  839. NSNumber*audiolostDataNumber = [rtcStatistics.values objectForKey:@"packetsLost"];
  840. audiolostData = [audiolostDataNumber longValue];
  841. }
  842. }
  843. for (NSString *keyin in [rtcStatistics.values allKeys]) {
  844. if ([keyin isEqualToString:@"currentRoundTripTime"]){
  845. /*延时数据*/
  846. currentRoundTripTime = [rtcStatistics.values objectForKey:@"currentRoundTripTime"];
  847. //[self updateAutoRateAndNetWorkStatesWithDelatyMS:(NSInteger)currentRoundTripTime.floatValue*1000];
  848. break;
  849. }
  850. }
  851. }
  852. //netWorkInfoLabel 网络信息 测试用
  853. if (selectedCandidatePairId) {
  854. for (NSString *key in [stats.statistics allKeys]) {
  855. RTCStatistics *rtcStatistics = [stats.statistics objectForKey:key];
  856. if ([rtcStatistics.type isEqualToString:@"candidate-pair"]){
  857. NSString *tempselectedCandidatePairId = rtcStatistics.id;
  858. if ([tempselectedCandidatePairId isEqualToString:selectedCandidatePairId]) {
  859. localCandidateId = [rtcStatistics.values objectForKey:@"localCandidateId"];
  860. remoteCandidateId = [rtcStatistics.values objectForKey:@"remoteCandidateId"];
  861. // HLog(@"三网数据--localCandidateId-->%@",localCandidateId);
  862. // HLog(@"三网数据--remoteCandidateId-->%@",remoteCandidateId);
  863. }
  864. }
  865. }
  866. }
  867. NSString *remoteCandidateStr = nil;
  868. NSString *localCandidateStr = nil;
  869. if (localCandidateId && remoteCandidateId) {
  870. for (NSString *key in [stats.statistics allKeys]) {
  871. RTCStatistics *rtcStatistics = [stats.statistics objectForKey:key];
  872. if ([rtcStatistics.type isEqualToString:@"local-candidate"]){
  873. NSString *templocalCandidateId = rtcStatistics.id;
  874. if ([templocalCandidateId isEqualToString:localCandidateId]) {
  875. //HLog(@"三网数据--localCandidateIdInfo-->%@",rtcStatistics.values);
  876. localCandidateStr = @"本地\n";
  877. NSString *candidateType = [rtcStatistics.values objectForKey:@"candidateType"];
  878. if (candidateType) {
  879. localCandidateStr = [localCandidateStr stringByAppendingString:[NSString stringWithFormat:@"candidateType:%@",candidateType]];
  880. localCandidateStr = [localCandidateStr stringByAppendingString:@"\n"];
  881. }
  882. NSString *address = [rtcStatistics.values objectForKey:@"address"];
  883. if (address) {
  884. localCandidateStr = [localCandidateStr stringByAppendingString:[NSString stringWithFormat:@"address:%@",address]];
  885. localCandidateStr = [localCandidateStr stringByAppendingString:@"\n"];
  886. }
  887. NSString *ip = [rtcStatistics.values objectForKey:@"ip"];
  888. if (ip) {
  889. localCandidateStr = [localCandidateStr stringByAppendingString:[NSString stringWithFormat:@"ip:%@",ip]];
  890. localCandidateStr = [localCandidateStr stringByAppendingString:@"\n"];
  891. }
  892. NSString *relatedAddress = [rtcStatistics.values objectForKey:@"relatedAddress"];
  893. if (relatedAddress) {
  894. localCandidateStr = [localCandidateStr stringByAppendingString:[NSString stringWithFormat:@"relatedAddress:%@",relatedAddress]];
  895. localCandidateStr = [localCandidateStr stringByAppendingString:@"\n"];
  896. }
  897. }
  898. }else if ([rtcStatistics.type isEqualToString:@"remote-candidate"]){
  899. NSString *tempRemoteCandidateId = rtcStatistics.id;
  900. if ([tempRemoteCandidateId isEqualToString:remoteCandidateId]) {
  901. //HLog(@"三网数据--remoteCandidateIdInfo-->%@",rtcStatistics.values);
  902. remoteCandidateStr = @"远端\n";
  903. NSString *candidateType = [rtcStatistics.values objectForKey:@"candidateType"];
  904. if (candidateType) {
  905. remoteCandidateStr = [remoteCandidateStr stringByAppendingString:[NSString stringWithFormat:@"candidateType:%@",candidateType]];
  906. remoteCandidateStr = [remoteCandidateStr stringByAppendingString:@"\n"];
  907. }
  908. NSString *address = [rtcStatistics.values objectForKey:@"address"];
  909. if (address) {
  910. remoteCandidateStr = [remoteCandidateStr stringByAppendingString:[NSString stringWithFormat:@"address:%@",address]];
  911. remoteCandidateStr = [remoteCandidateStr stringByAppendingString:@"\n"];
  912. }
  913. NSString *ip = [rtcStatistics.values objectForKey:@"ip"];
  914. if (ip) {
  915. remoteCandidateStr = [remoteCandidateStr stringByAppendingString:[NSString stringWithFormat:@"ip:%@",ip]];
  916. remoteCandidateStr = [remoteCandidateStr stringByAppendingString:@"\n"];
  917. }
  918. NSString *relatedAddress = [rtcStatistics.values objectForKey:@"relatedAddress"];
  919. if (relatedAddress) {
  920. remoteCandidateStr = [remoteCandidateStr stringByAppendingString:[NSString stringWithFormat:@"relatedAddress:%@",relatedAddress]];
  921. remoteCandidateStr = [remoteCandidateStr stringByAppendingString:@"\n"];
  922. }
  923. }
  924. }
  925. }
  926. }
  927. NSString *showStr = @"";
  928. if (localCandidateStr) {
  929. showStr = [showStr stringByAppendingString:localCandidateStr];
  930. }
  931. if (remoteCandidateStr) {
  932. showStr = [showStr stringByAppendingString:remoteCandidateStr];
  933. }
  934. //网络延迟, 丢包率,FPS,网速 分辨率 流量
  935. //延时数据
  936. NSString*currentRoundTripTimeStr = [[NSString alloc] initWithFormat:@"延时:%ldms\n",(NSInteger)(currentRoundTripTime.floatValue*1000)];
  937. showStr = [showStr stringByAppendingString:currentRoundTripTimeStr];
  938. //计算上次报道到这一次的丢包率---------界面展示百分比丢包率
  939. long allPacketsReceived = packetsReceived - lasPacketsReceived;
  940. alllostData = audiolostData + videolostData;
  941. NSInteger lostRate = ((alllostData - lastAlllostData) *1.0 / (allPacketsReceived + (alllostData - lastAlllostData))) *100;
  942. //记录上一次丢包数
  943. lastAlllostData = alllostData;
  944. //记录上一次接收包数
  945. lasPacketsReceived = packetsReceived;
  946. NSString*lostDataStr = [[NSString alloc] initWithFormat:@"丢包率:(%ld/%ld) %ld%%\n",alllostData - lastAlllostData,allPacketsReceived + alllostData - lastAlllostData,lostRate];
  947. showStr = [showStr stringByAppendingString:lostDataStr];
  948. //FPS
  949. NSString*FPSStr = [[NSString alloc] initWithFormat:@"FPS:%ld\n",framesPerSecond];
  950. showStr = [showStr stringByAppendingString:FPSStr];
  951. //网速 传输数据速度
  952. NSString *netDataSpeedStr = [NSString stringWithFormat:@"传输数据速度:%ldk/s\n",preReceive/1024];
  953. showStr = [showStr stringByAppendingString:netDataSpeedStr];
  954. mainBlock(^{
  955. [self->netWorkInfoLabel setText:showStr];
  956. });
  957. }
  958. #pragma mark 收到的webrtc消息处理
  959. - (void)handleWebRtcMsgResponseBy:(NSData*)message
  960. {
  961. if([message isKindOfClass:[NSMutableString class]] || [message isKindOfClass:[NSString class]])
  962. {
  963. message = [(NSString *)message dataUsingEncoding:(NSUTF8StringEncoding)];
  964. }
  965. NSError *error = nil;
  966. NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:message options:NSJSONReadingMutableContainers error:&error];
  967. HLog(@"webRtc 音视频推拉流 通道接收消息:------------------%@",dataDict);
  968. if(!dataDict){
  969. //[weakSelf handleDownloadResponseFunBy:message];
  970. return;
  971. }
  972. if(![dataDict isKindOfClass:[NSDictionary class]]){
  973. //[__NSCFString allKeys] unrecognized selector sent to ins
  974. return;
  975. }
  976. if(![[dataDict allKeys] containsObject:@"type"]){
  977. return;
  978. }
  979. NSString *messageType = dataDict[@"type"];
  980. if ([messageType isEqualToString:@"login"]) {
  981. if([[dataDict allKeys] containsObject:@"value"]){
  982. NSString *value = dataDict[@"value"];
  983. [self LogoutByOtherFun:value];
  984. }
  985. }
  986. }
  987. //延迟系统触摸事件
  988. - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures{
  989. return UIRectEdgeAll;
  990. }
  991. @end