PlayerViewController.mm 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062
  1. //
  2. // ViewController.m
  3. // VideoTest
  4. //
  5. // Created by APPLE on 2019/5/29.
  6. // Copyright © 2019 APPLE. All rights reserved.
  7. //
  8. #import "PlayerViewController.h"
  9. #import "PlayerViewController+TouchPointXD.h"
  10. #import "PlayerViewController+AdjustBtnFrame.h"
  11. #import "AFNetworkReachabilityManager.h"
  12. #import <AVFoundation/AVFoundation.h>
  13. #import <ifaddrs.h>
  14. #import <arpa/inet.h>
  15. #import <SystemConfiguration/CaptiveNetwork.h>
  16. #import <SDWebImage.h>
  17. #import "FLAnimatedImage.h"
  18. #import "VclCrash.h"
  19. #import "HWBuryPointModel.h"
  20. #import "BuryPoint.h"
  21. #import<objc/runtime.h>
  22. #import "CloudPhoneModel.h"
  23. #import "BGTool.h"
  24. #import "AppDelegate.h"
  25. #import <RCCloudPhoneSDK/RCAudioPlayer.h>
  26. #import <RCCloudPhoneSDK/RCRecordManager.h>
  27. //#import <RCCloudPhoneSDK/RCCommandChannel.h>
  28. #import <RCCloudPhoneSDK/RCLiveSteamManager.h>
  29. #import "ShortcutManager.h"
  30. #import "PhoneP2PModel.h"
  31. #import "PlayerViewController+RBD.h"
  32. #import "P2PDataManager.h"
  33. #import "UseAccountManage.h"
  34. #import "RCCommandHelp.h"
  35. #import "UIView+View.h"
  36. #import "SafeForKey.h"
  37. #import "CloudPhoneAPI.h"
  38. #import "P2PDataManager.h"
  39. #import "MySetViewController.h"
  40. typedef enum : NSUInteger {
  41. BITRATE_EXTREME_SPEED = 2,//极速
  42. // BITRATE_HIGH_SPEED = 843000,//高速
  43. BITRATE_AUTO = 6,//自动
  44. // BITRATE_STANDARD_CLEARING = 2,//wwan标清
  45. // BITRATE_HD = 3,//wwan高清
  46. BITRATE_WIFI_STANDARD_CLEARING = 3, //wifi标清
  47. BITRATE_WIFI_HD = 4, //wifi高清
  48. } BITRATE_TYPE;
  49. //当手机采用wifi时不做码率调整,当手机采用流量时,高清采用2052000,标清采用1652000调整(默认)
  50. ///*测试*/
  51. //#include "socket.h"
  52. PlayerViewController * _Nullable mySelf;
  53. BOOL isLan;
  54. //PlayerViewController *mySelf;
  55. int portConnect;
  56. NSString *ipConnect;
  57. @interface PlayerViewController ()
  58. <PlayerViewDelegate,
  59. RecoderManagerDelegate,
  60. AudioRecoderMamagerDelegate,
  61. RCLiveSteamManagerDelegate,
  62. UITextFieldDelegate>
  63. {
  64. // __block int _ihandle;
  65. int rate;/*0:极速 1:高速 2:高清 3:标清 4:d自动*/
  66. NSUInteger rateForAuto;
  67. BOOL showWattingViewImageView;
  68. RCPCMPlayer *pcmPlayer;
  69. NSInteger gpsOnOff;
  70. BOOL canRate;
  71. CIContext *videoContext;
  72. NSInteger reConnectAccount;/*记录重连次数*/
  73. BOOL isInPan;/*标记是否有手指在活动*/
  74. int socketFortest;
  75. UIView *loadingWithTipsView;
  76. NSInteger netWorkStates;/*0很差 1较差 2流畅*/
  77. BOOL outputVolumeKVO;/*标记声音监听通知*/
  78. NSDate *rootSetTime;
  79. UITextField *hidenTextField;
  80. NSString *hidenTextFieldOldStr;
  81. }
  82. @property (nonatomic, strong, nullable) RCVideoRecoderManager *recoderManager;
  83. @property (nonatomic, strong, nullable) RCAudioRecoderMamager *audioRecoderMamager;
  84. @property (nonatomic, strong) RCLiveSteamManager *liveStreamManager;
  85. //@property (nonatomic, strong) RCCommandChannelManager *commandChannelManager;//指令通道
  86. //@property (nonatomic, strong) RCCommandChannelManager *businessCommandChannelManager;//业务通道
  87. //需不需要显示音量条
  88. @property (nonatomic, assign) BOOL isNeedShowVoiceBar;
  89. //切换云手机 保存切换过的云手机数组, 返回的时刷新对应的云机websorket封面
  90. @property (nonatomic, strong) NSMutableArray *switchCloudPhoneArray;
  91. @property (nonatomic, copy) NSString *screenShotFileName;
  92. @property (nonatomic, copy) NSTimer *timer; // 定时器-控制按钮
  93. /**30s后 渲染时间戳无更新 自动断开链接*/
  94. @property (nonatomic, assign) NSInteger concentTime; // 拉流数据 渲染更新时间戳
  95. /**定时器计数*/
  96. @property (nonatomic, assign) NSInteger currentTime;
  97. /**是否是收入进入云手机*/
  98. @property (nonatomic, assign) BOOL isFirstEnterPhone;
  99. @end
  100. @implementation PlayerViewController
  101. @synthesize internetVideoPort;
  102. @synthesize ip;
  103. @synthesize canControl;
  104. @synthesize phoneIndex;
  105. @synthesize deviceId;
  106. @synthesize vpnStatus;
  107. @synthesize internetHttp;
  108. @synthesize deviceModel;
  109. @synthesize chaofenDeviceModel;
  110. @synthesize rbdNotification;
  111. - (void)viewDidLoad {
  112. [super viewDidLoad];
  113. if (@available(iOS 11.0, *)) { // 兼容低版本ios
  114. [self setNeedsUpdateOfHomeIndicatorAutoHidden];
  115. }
  116. /*请求设备列表*/
  117. [[UseAccountManage shareInstance] CommonGetWithCallBackCode:LoadDeviceAPI Parameters:[NSMutableDictionary new] success:^(id _Nonnull responseObject) {
  118. CloduPhoneListDataModel *model = [[CloduPhoneListDataModel alloc] initWithDictionary:responseObject error:nil];
  119. KyoLog(@"\n----modelstatus-%@---",model.status);
  120. if (model.status.integerValue == 0)
  121. {
  122. [UseAccountManage shareInstance].cloudPhoneListModel = model.data;
  123. if ([UseAccountManage shareInstance].cloudPhoneListModel.diskInfo.count > 0){
  124. /*提取第一个设备*/
  125. self->deviceModel = [UseAccountManage shareInstance].cloudPhoneListModel.diskInfo.firstObject;
  126. /*先判断本地有无缓存,无缓存时进行超分,有缓存直接进行链接*/
  127. PhoneP2PModel *p2pModel = [[P2PDataManager shareInstance] findRecoderWithPhoneID:self->deviceModel.phoneId];
  128. if (p2pModel) {
  129. if (p2pModel.ctime + 24*3600 > [[NSDate date] timeIntervalSince1970]) {/*在有效时间内*/
  130. /*替换相关参数并且进入云手机*/
  131. self->deviceModel.sn = p2pModel.sn;
  132. self->deviceModel.internetVideoPort = p2pModel.internetVideoPort;
  133. self->deviceModel.internetBusinessPort = p2pModel.internetBusinessPort;
  134. self->deviceModel.internetIp = p2pModel.internetIp;
  135. self->deviceModel.roomInternetIp = p2pModel.roomInternetIp;
  136. self->deviceModel.vpnStatus = p2pModel.vpnStatus;
  137. self->deviceModel.videoCode = p2pModel.videoCode;
  138. self->deviceModel.deviceBoard = p2pModel.deviceBoard;
  139. self->deviceModel.internetHttps = p2pModel.internetHttps;
  140. self->deviceModel.internetHttp = p2pModel.internetHttp;
  141. self->deviceModel.localIp = p2pModel.localIp;
  142. self->deviceModel.cardToken = p2pModel.cardToken;
  143. self->deviceModel.pushFlowCode = p2pModel.pushFlowCode;
  144. self->deviceModel.resolvingPower = p2pModel.resolvingPower;
  145. self->deviceModel.width = p2pModel.width;
  146. self->deviceModel.high = p2pModel.high;
  147. self->deviceModel.dpi = p2pModel.dpi;
  148. self->deviceModel.vpnMode = p2pModel.vpnMode;
  149. self.phoneIndex = 0;
  150. /*传入IP与port*/
  151. self.ip = self->deviceModel.internetIp;
  152. self.internetVideoPort = self->deviceModel.internetVideoPort;
  153. self.deviceId = [NSString stringWithFormat:@"%ld",(long)self->deviceModel.phoneId];
  154. self.canControl = YES;
  155. self.buyVipType = self->deviceModel.buyVipType;
  156. self.internetHttp = self->deviceModel.internetHttp;
  157. HLog(@"__________连接000000__________deviceModel.cardToken = %@ 旧",self->deviceModel.cardToken);
  158. if (self->deviceModel){
  159. if (self.isNeedRecyclResource) {
  160. self.liveStreamManager = [[RCLiveSteamManager alloc] init];
  161. self.liveStreamManager.delegate = self;
  162. [self opencommandChannelManagerrc_openURL];
  163. [self openbusinessCommandChannelManagerrc_openURL];
  164. portConnect = [self->internetVideoPort intValue];
  165. ipConnect = self->ip;
  166. isLan = NO;
  167. globalBlock(^{
  168. // 初始化成功后连接
  169. @synchronized (self) {
  170. if ([self initVideoServer])
  171. {
  172. [self connectVideoServer];
  173. }
  174. }
  175. });
  176. [self addNSNotification];
  177. }
  178. self.isNeedRecyclResource = YES;
  179. }
  180. }
  181. }else{
  182. /*主动超分*/
  183. [self extensionPhoneGetRBD];
  184. }
  185. /*初始化数据*/
  186. if (self.deviceModel.width > 200 && self.deviceModel.high > 200) {
  187. ksharedAppDelegate.couldPhone_W_PHONE = self.deviceModel.width;
  188. ksharedAppDelegate.couldPhone_H_PHONE = self.deviceModel.high;
  189. }else{
  190. if ([self.deviceModel.resolvingPower isEqualToString:@"720"]) {
  191. ksharedAppDelegate.couldPhone_W_PHONE = 720;
  192. ksharedAppDelegate.couldPhone_H_PHONE = 1280;
  193. }else{
  194. ksharedAppDelegate.couldPhone_W_PHONE = 1080;
  195. ksharedAppDelegate.couldPhone_H_PHONE = 1920;
  196. }
  197. }
  198. }
  199. }
  200. else
  201. {
  202. ;
  203. }
  204. } failure:^(NSError * _Nonnull error) {
  205. HLog(@"%@", error);
  206. }];
  207. outputVolumeKVO = NO;
  208. rbdNotification = 0;
  209. // 初始化
  210. self.isNeedRecyclResource = YES;
  211. self.isFirstEnterPhone = YES;
  212. [self initData];
  213. self.toolBar.hidden = YES;
  214. self.navigationBar.hidden = YES;
  215. [self drawView];
  216. [self showNewIndicator];
  217. mySelf = self;
  218. haveBack = NO;
  219. }
  220. - (void)initData
  221. {
  222. isInPan = NO;
  223. reConnectAccount = 0;
  224. netWorkStates = 0;
  225. canRate = NO;
  226. rate = -1;
  227. isLan = NO;
  228. // _ihandle = -1;
  229. [hidenTextField resignFirstResponder];
  230. hidenTextField.text = @"";
  231. hidenTextFieldOldStr = nil;
  232. }
  233. - (void)viewWillAppear:(BOOL)animated {
  234. [super viewWillAppear:animated];
  235. // 屏幕常亮
  236. [UIApplication sharedApplication].idleTimerDisabled = YES;
  237. [[UIApplication sharedApplication] setStatusBarHidden:YES];
  238. [self setTimerCountDown];
  239. }
  240. - (void)viewDidLayoutSubviews {
  241. [super viewDidLayoutSubviews];
  242. if (mPlayerView.controlBtn) {
  243. [self.navigationController.view bringSubviewToFront:mPlayerView.controlBtn];
  244. }
  245. }
  246. - (void)viewWillDisappear:(BOOL)animated {
  247. // 退出控制器 回收资源
  248. [self recyclResource];
  249. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  250. // app.allowAutoRotate = NO;
  251. [super viewWillDisappear:animated];
  252. }
  253. // 退出控制器 回收资源
  254. - (void)recyclResource {
  255. // 移除Alert
  256. if (self.presentedViewController) {
  257. [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
  258. }
  259. [mPlayerView.controlBtn removeFromSuperview];
  260. if ([[[UIApplication sharedApplication] keyWindow] viewWithTag:1235]) {
  261. [[[[UIApplication sharedApplication] keyWindow] viewWithTag:1235] removeFromSuperview];
  262. }
  263. // 销毁定时器
  264. [self cancelTimer];
  265. [self enablePanRightBack];
  266. // 播放器内部页面跳转-不能回收的资源
  267. if (self.isNeedRecyclResource) {
  268. // 屏幕常亮
  269. [UIApplication sharedApplication].idleTimerDisabled = NO;
  270. [[UIApplication sharedApplication] setStatusBarHidden:NO];
  271. if (USEAAC)
  272. {
  273. if (aacPlayer)
  274. {
  275. [aacPlayer stop];
  276. aacPlayer = nil;
  277. }
  278. }
  279. else
  280. {
  281. if (pcmPlayer)
  282. {
  283. [pcmPlayer stop];
  284. pcmPlayer = nil;
  285. }
  286. }
  287. [self toStopRecoder];
  288. [self toStopAudioRecoder];
  289. [self removeNSNotification];
  290. CloudPhoneModel *deviceDcit = [[UseAccountManage shareInstance].cloudPhoneListModel.diskInfo objectAtIndex:phoneIndex];
  291. //清空WebSocket连接
  292. [self.commandChannelManager rc_close];
  293. [self.businessCommandChannelManager rc_close];
  294. globalBlock(^{
  295. [self.liveStreamManager rc_release];
  296. });
  297. }
  298. }
  299. - (void)viewDidAppear:(BOOL)animated
  300. {
  301. [super viewDidAppear:animated];
  302. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  303. // app.allowAutoRotate = YES;
  304. [[UIApplication sharedApplication] setStatusBarHidden:YES];
  305. [self noEnablePanRightBack];
  306. [self.navigationController.view addSubview:mPlayerView.controlBtn];
  307. [self.navigationController.view bringSubviewToFront:mPlayerView.controlBtn];
  308. if (deviceModel){
  309. if (self.isNeedRecyclResource) {
  310. self.liveStreamManager = [[RCLiveSteamManager alloc] init];
  311. self.liveStreamManager.delegate = self;
  312. [self opencommandChannelManagerrc_openURL];
  313. [self openbusinessCommandChannelManagerrc_openURL];
  314. portConnect = [internetVideoPort intValue];
  315. ipConnect = ip;
  316. isLan = NO;
  317. globalBlock(^{
  318. // 初始化成功后连接
  319. @synchronized (self) {
  320. if ([self initVideoServer])
  321. {
  322. [self connectVideoServer];
  323. }
  324. }
  325. });
  326. [self addNSNotification];
  327. }
  328. self.isNeedRecyclResource = YES;
  329. }
  330. }
  331. - (void)opencommandChannelManagerrc_openURL
  332. {
  333. KWeakSelf
  334. //初始化指令通道
  335. self.commandChannelManager = [[RCCommandChannelManager alloc] init];
  336. NSString *instructionsChannelUrl = [[UseAccountManage shareInstance] getInstructionsChannelUrlWithInternetHttp:deviceModel.internetHttp localIp:deviceModel.localIp cardToken:deviceModel.cardToken];
  337. if (deviceModel.internetHttp && deviceModel.localIp && deviceModel.cardToken && instructionsChannelUrl) { //当都有值才可连接
  338. HLog(@"WebSocket11111指令通道连接开始11111 url = %@ ",instructionsChannelUrl);
  339. [self.commandChannelManager rc_openURL:instructionsChannelUrl connected:^{
  340. HLog(@"WebSocket11111指令通道连接成功11111 url = %@ ",instructionsChannelUrl);
  341. if (self.commandChannelManager.rc_socketStatus == RCSocketCloudPhoneStatusConnected){
  342. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  343. // /*获取自己的用户名(电话号码)*/
  344. // NSString *selfUseName = nil;
  345. // NSDictionary *dict111 = [[UseAccountManage shareInstance] getLoginInfo];
  346. // if ([[dict111 allKeys] containsObject:@"userName"]) {
  347. // selfUseName = [dict111 valueForKey:@"userName"];
  348. // /*查询是否有设备在远程控制*/
  349. // [self.commandChannelManager rc_sendData:[RCCommandHelp askControllcommandWithUseName:selfUseName]];
  350. // }
  351. /*开启输入法透传*/
  352. NSString *dataStr = [RCCommandHelp commandOpenLoaclKeyboad];
  353. [self.commandChannelManager rc_sendData:dataStr];
  354. });
  355. }
  356. } receive:^(id message, FLSocketCloudPhoneReceiveType type) {
  357. HLog(@"webSocket指令通道接收消息:------------------\n%@",message);
  358. if([message isKindOfClass:[NSMutableString class]] || [message isKindOfClass:[NSString class]])
  359. {
  360. message = [(NSString *)message dataUsingEncoding:(NSUTF8StringEncoding)];
  361. }
  362. NSError *error = nil;
  363. NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:message options:NSJSONReadingMutableContainers error:&error];
  364. NSString *messageType = dataDict[@"type"];
  365. if ([messageType isEqualToString:@"cutting"]) {
  366. [[iToast makeText:@"复制成功"] show];
  367. }
  368. else if ([messageType isEqualToString:@"forwardMsg"]){/*转发*/
  369. /*获取指令类型*/
  370. NSString *code = nil;
  371. if ([[dataDict allKeys] containsObject:@"data"]) {
  372. NSDictionary *data = dataDict[@"data"];
  373. if ([[data allKeys] containsObject:@"code"]) {
  374. code = [data objectForKey:@"code"];
  375. if (![code isKindOfClass:[NSString class]]) {
  376. code = [NSString stringWithFormat:@"%ld",[code integerValue]];
  377. }
  378. }
  379. }
  380. /*获取用户名*/
  381. NSString *useName = nil;
  382. if ([[dataDict allKeys] containsObject:@"data"]) {
  383. NSDictionary *data = dataDict[@"data"];
  384. if ([[data allKeys] containsObject:@"userName"]) {
  385. useName = [data objectForKey:@"userName"];
  386. }
  387. }
  388. /*获取自己的用户名(电话号码)*/
  389. NSString *selfUseName = nil;
  390. NSDictionary *dict111 = [[UseAccountManage shareInstance] getLoginInfo];
  391. if ([[dict111 allKeys] containsObject:@"userName"]) {
  392. selfUseName = [dict111 valueForKey:@"userName"];
  393. }else{
  394. return;
  395. }
  396. /*无控制权提示*/
  397. //- (void)tipsNoDeviceContrWithPhoneIndex:(NSInteger)phoneIndex
  398. /*提醒拿回控制权*/
  399. //- (void)tipsGetBackDeviceContrWithPhoneIndex:(NSInteger)phoneIndex
  400. /*提醒控制权被拿回*/
  401. //- (void)tipsMainUserGetDeviceAuthBack
  402. // if ([code isEqualToString:@"3000"]) {/*有人询问控制权情况*/
  403. // if (self->deviceModel.authPhone==0 && self->deviceModel.phoneAuthStatus == 0){
  404. // return;/*自己的云手机且未授权直接返回*/
  405. // }
  406. //
  407. // if (![useName isEqualToString:selfUseName]){
  408. // /*根据实际情况进行相关回复*/
  409. // [self.commandChannelManager rc_sendData:[RCCommandHelp faceBackControllcommandWithUseName:selfUseName haveControl:self->canControl]];
  410. // }
  411. // }else if ([code isEqualToString:@"4000"]){/*有在控制*/
  412. // if (self->deviceModel.authPhone==0 && self->deviceModel.phoneAuthStatus == 0){
  413. // return;/*自己的云手机且未授权直接返回*/
  414. // }
  415. //
  416. // if (self->deviceModel.authPhone==0) {/*是自己的设备时候要询问是否夺回控制权*/
  417. // if (![useName isEqualToString:selfUseName]) {
  418. // /*弹窗询问是否夺回控制权*/
  419. // [self tipsGetBackDeviceContrWithPhoneIndex:self->deviceModel];
  420. // }
  421. // }else if (![useName isEqualToString:selfUseName]) {
  422. // if (self->canControl == YES) {
  423. // [self tipsNoDeviceContrWithPhoneIndex:self->deviceModel];
  424. // }
  425. // self->canControl = NO;
  426. // if (!self->canControl&&self->controlView){
  427. // [self->controlView diasbleControltPhoneView];
  428. // }else if (self->controlView){
  429. // [self->controlView enableControltPhoneView];
  430. // }
  431. // }
  432. // }else if ([code isEqualToString:@"4100"]){/*未控制*/
  433. // /*不用处理默认可以控制*/
  434. // }else if ([code isEqualToString:@"5000"]){/*夺回控制权*/
  435. // if (self->deviceModel.authPhone==0 && self->deviceModel.phoneAuthStatus == 0){
  436. // return;/*自己的云手机且未授权直接返回*/
  437. // }
  438. //
  439. // if ([useName isEqualToString:selfUseName]){
  440. // return;
  441. // }
  442. //
  443. // if (self->deviceModel.authPhone!=0) {/*不是自己的设备时候要进行关闭控制权*/
  444. // if (self->canControl == YES) {
  445. // /*弹窗提示控制权已经被夺回*/
  446. // [self tipsMainUserGetDeviceAuthBack];
  447. // }
  448. // self->canControl = NO;
  449. // if (!self->canControl&&self->controlView){
  450. // [self->controlView diasbleControltPhoneView];
  451. // }else if (self->controlView){
  452. // [self->controlView enableControltPhoneView];
  453. // }
  454. // }
  455. // }else
  456. if ([code isEqualToString:@"phoneSizeChange"]){/*分辨率改变*/
  457. if ([[dataDict allKeys] containsObject:@"data"]) {
  458. NSDictionary *data = dataDict[@"data"];
  459. if ([[data allKeys] containsObject:@"width"]) {
  460. self->deviceModel.width = [[data objectForKey:@"width"] integerValue];
  461. ksharedAppDelegate.couldPhone_W_PHONE = self->deviceModel.width;
  462. }
  463. if ([[data allKeys] containsObject:@"dpi"]) {
  464. self->deviceModel.dpi = [[data objectForKey:@"dpi"] integerValue];
  465. }
  466. if ([[data allKeys] containsObject:@"height"]) {
  467. self->deviceModel.high = [[data objectForKey:@"height"] integerValue];
  468. ksharedAppDelegate.couldPhone_H_PHONE = self->deviceModel.high;
  469. }
  470. if (ksharedAppDelegate.couldPhone_W_PHONE > ksharedAppDelegate.couldPhone_H_PHONE) {
  471. CGFloat temp = ksharedAppDelegate.couldPhone_W_PHONE;
  472. ksharedAppDelegate.couldPhone_W_PHONE = ksharedAppDelegate.couldPhone_H_PHONE;
  473. ksharedAppDelegate.couldPhone_H_PHONE = temp;
  474. }
  475. if (self->deviceModel.width > self->deviceModel.high) {
  476. CGFloat temp = self->deviceModel.width;
  477. self->deviceModel.width = self->deviceModel.high;
  478. self->deviceModel.high = temp;
  479. }
  480. /*更新点对点数据*/
  481. PhoneP2PModel *p2pModel = [[P2PDataManager shareInstance] findRecoderWithPhoneID:self->deviceModel.phoneId];
  482. p2pModel.width = self->deviceModel.width;
  483. p2pModel.high = self->deviceModel.high;
  484. p2pModel.dpi = self->deviceModel.dpi;
  485. [[P2PDataManager shareInstance] saveOrUpdateRecoderWithModel:p2pModel];
  486. }
  487. }
  488. }
  489. else if ([messageType isEqualToString:@"getPhoneSize"] || [messageType isEqualToString:@"setPhoneSize"]){
  490. NSString *sn = nil;
  491. if ([[dataDict allKeys] containsObject:@"data"]) {
  492. NSDictionary *data = dataDict[@"data"];
  493. if ([[data allKeys] containsObject:@"sn"]) {
  494. sn = [data objectForKey:@"sn"];
  495. if (![sn isEqualToString:self->deviceModel.sn]) {/*不是当前设备直接返回*/
  496. return;
  497. }
  498. }
  499. if ([[data allKeys] containsObject:@"status"]) {
  500. NSInteger status = [[data objectForKey:@"status"] integerValue];
  501. if (status == 0) {/*不是当前设备直接返回*/
  502. if ([[data allKeys] containsObject:@"width"]) {
  503. self->deviceModel.width = [[data objectForKey:@"width"] integerValue];
  504. ksharedAppDelegate.couldPhone_W_PHONE = self->deviceModel.width;
  505. }
  506. if ([[data allKeys] containsObject:@"dpi"]) {
  507. self->deviceModel.dpi = [[data objectForKey:@"dpi"] integerValue];
  508. }
  509. if ([[data allKeys] containsObject:@"height"]) {
  510. self->deviceModel.high = [[data objectForKey:@"height"] integerValue];
  511. ksharedAppDelegate.couldPhone_H_PHONE = self->deviceModel.high;
  512. }
  513. if (ksharedAppDelegate.couldPhone_W_PHONE > ksharedAppDelegate.couldPhone_H_PHONE) {
  514. CGFloat temp = ksharedAppDelegate.couldPhone_W_PHONE;
  515. ksharedAppDelegate.couldPhone_W_PHONE = ksharedAppDelegate.couldPhone_H_PHONE;
  516. ksharedAppDelegate.couldPhone_H_PHONE = temp;
  517. }
  518. if (self->deviceModel.width > self->deviceModel.high) {
  519. CGFloat temp = self->deviceModel.width;
  520. self->deviceModel.width = self->deviceModel.high;
  521. self->deviceModel.high = temp;
  522. }
  523. /*更新点对点数据*/
  524. PhoneP2PModel *p2pModel = [[P2PDataManager shareInstance] findRecoderWithPhoneID:self->deviceModel.phoneId];
  525. p2pModel.width = self->deviceModel.width;
  526. p2pModel.high = self->deviceModel.high;
  527. p2pModel.dpi = self->deviceModel.dpi;
  528. [[P2PDataManager shareInstance] saveOrUpdateRecoderWithModel:p2pModel];
  529. }
  530. }
  531. }
  532. // {
  533. // "type":"getPhoneSize", --接口类型
  534. // "data":{
  535. // "sn":"RK_XXX", --安卓卡SN
  536. // "status":"0", --状态:0:成功 1:失败
  537. // "msg":"success", --请求状态:success 成功 failed 失败
  538. // "width":xxx,
  539. // "height":xxx,
  540. // "dpi":yyy --屏幕密度
  541. // }
  542. // }
  543. }
  544. else if ([messageType isEqualToString:@"sync_wifi"]){
  545. [self.commandChannelManager rc_sendData:[RCCommandHelp noSyncWifiBack]];
  546. [[iToast makeText:@"本机参数已同步完成,IOS无法同步wifi"] show];
  547. }
  548. else if ([messageType isEqualToString:@"reProduceText"]){
  549. NSString *sn = nil;
  550. if ([[dataDict allKeys] containsObject:@"data"]) {
  551. NSDictionary *data = dataDict[@"data"];
  552. if ([[data allKeys] containsObject:@"text"]) {
  553. NSString *pasteboardStr = [data objectForKey:@"text"];
  554. UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
  555. pasteboard.string = pasteboardStr;
  556. }
  557. }
  558. }
  559. else if ([messageType isEqualToString:@"downAdnInstallRep"]){
  560. if ([[dataDict allKeys] containsObject:@"data"]) {
  561. NSDictionary *data = dataDict[@"data"];
  562. if ([[data allKeys] containsObject:@"status"]) {
  563. NSString *status = [data objectForKey:@"status"];
  564. if ([status isEqualToString:@"1"]) {
  565. mainBlock(^{
  566. [[iToast makeText:@"App下载完成"] show];
  567. });
  568. }else if ([status isEqualToString:@"0"]){
  569. mainBlock(^{
  570. [[iToast makeText:@"App下载中"] show];
  571. });
  572. }
  573. }
  574. }
  575. }
  576. else if ([messageType isEqualToString:@"shakeit"]){
  577. HLog(@"\n-----摇一摇成功------");
  578. }else if ([messageType isEqualToString:@"keyboardFeedbackBean"]){/*调起键盘*/
  579. [self->hidenTextField becomeFirstResponder];
  580. // [self->hidenTextField setText:@"1234"];
  581. [self->hidenTextField setEnabled:YES];
  582. }
  583. } failure:^(NSError *error) {
  584. }];
  585. }
  586. }
  587. - (void)openbusinessCommandChannelManagerrc_openURL
  588. {
  589. KWeakSelf
  590. //初始化业务通道
  591. self.businessCommandChannelManager = [[RCCommandChannelManager alloc] init];
  592. NSString *businessChannelUrl = [[UseAccountManage shareInstance] getBusinessChannelUrlWithInternetHttp:deviceModel.internetHttp localIp:deviceModel.localIp cardToken:deviceModel.cardToken];
  593. if (deviceModel.internetHttp && deviceModel.localIp && deviceModel.cardToken && businessChannelUrl) { //当都有值才可连接
  594. [self.businessCommandChannelManager rc_openURL:businessChannelUrl connected:^{
  595. if (self.businessCommandChannelManager.rc_socketStatus == RCSocketCloudPhoneStatusConnected){
  596. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  597. // //连接成功,发送预览图指令
  598. // NSString *taskUid = [iTools getTaskUidStr];
  599. // NSString *dataStr = [RCCommandHelp commandGetScreenPicWithTaskUid:taskUid intervalTime:0];
  600. // [self.businessCommandChannelManager rc_sendData:dataStr];
  601. //
  602. // });
  603. }
  604. HLog(@"WebSocket11111业务通道连接成功11111 url = %@ ",businessChannelUrl);
  605. } receive:^(id message, FLSocketCloudPhoneReceiveType type) {
  606. HLog(@"webSocket业务通道接收消息:------------------\n%@",message);
  607. if([message isKindOfClass:[NSMutableString class]] || [message isKindOfClass:[NSString class]])
  608. {
  609. message = [(NSString *)message dataUsingEncoding:(NSUTF8StringEncoding)];
  610. }
  611. NSError *error = nil;
  612. NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:message options:NSJSONReadingMutableContainers error:&error];
  613. NSString *messageType = dataDict[@"type"];
  614. if ([messageType isEqualToString:@"cutting"]) {
  615. }
  616. else if ([messageType isEqualToString:@"getPhoneSize"] || [messageType isEqualToString:@"setPhoneSize"]){
  617. NSString *sn = nil;
  618. if ([[dataDict allKeys] containsObject:@"data"]) {
  619. NSDictionary *data = dataDict[@"data"];
  620. if ([[data allKeys] containsObject:@"sn"]) {
  621. sn = [data objectForKey:@"sn"];
  622. if (![sn isEqualToString:self->deviceModel.sn]) {/*不是当前设备直接返回*/
  623. return;
  624. }
  625. }
  626. if ([[data allKeys] containsObject:@"status"]) {
  627. NSInteger status = [[data objectForKey:@"status"] integerValue];
  628. if (status == 0) {/*不是当前设备直接返回*/
  629. if ([[data allKeys] containsObject:@"width"]) {
  630. self->deviceModel.width = [[data objectForKey:@"width"] integerValue];
  631. ksharedAppDelegate.couldPhone_W_PHONE = self->deviceModel.width;
  632. }
  633. if ([[data allKeys] containsObject:@"dpi"]) {
  634. self->deviceModel.dpi = [[data objectForKey:@"dpi"] integerValue];
  635. }
  636. if ([[data allKeys] containsObject:@"height"]) {
  637. self->deviceModel.high = [[data objectForKey:@"height"] integerValue];
  638. ksharedAppDelegate.couldPhone_H_PHONE = self->deviceModel.high;
  639. }
  640. if (ksharedAppDelegate.couldPhone_W_PHONE > ksharedAppDelegate.couldPhone_H_PHONE) {
  641. CGFloat temp = ksharedAppDelegate.couldPhone_W_PHONE;
  642. ksharedAppDelegate.couldPhone_W_PHONE = ksharedAppDelegate.couldPhone_H_PHONE;
  643. ksharedAppDelegate.couldPhone_H_PHONE = temp;
  644. }
  645. if (self->deviceModel.width > self->deviceModel.high) {
  646. CGFloat temp = self->deviceModel.width;
  647. self->deviceModel.width = self->deviceModel.high;
  648. self->deviceModel.high = temp;
  649. }
  650. /*更新点对点数据*/
  651. PhoneP2PModel *p2pModel = [[P2PDataManager shareInstance] findRecoderWithPhoneID:self->deviceModel.phoneId];
  652. p2pModel.width = self->deviceModel.width;
  653. p2pModel.high = self->deviceModel.high;
  654. p2pModel.dpi = self->deviceModel.dpi;
  655. [[P2PDataManager shareInstance] saveOrUpdateRecoderWithModel:p2pModel];
  656. }
  657. }
  658. }
  659. // {
  660. // "type":"getPhoneSize", --接口类型
  661. // "data":{
  662. // "sn":"RK_XXX", --安卓卡SN
  663. // "status":"0", --状态:0:成功 1:失败
  664. // "msg":"success", --请求状态:success 成功 failed 失败
  665. // "width":xxx,
  666. // "height":xxx,
  667. // "dpi":yyy --屏幕密度
  668. // }
  669. // }
  670. }
  671. else if ([messageType isEqualToString:@"VPNstatus"]){
  672. NSString *sn = nil;
  673. if ([[dataDict allKeys] containsObject:@"data"]) {
  674. NSDictionary *data = dataDict[@"data"];
  675. if ([[data allKeys] containsObject:@"sn"]) {
  676. sn = [data objectForKey:@"sn"];
  677. if (![sn isEqualToString:self->deviceModel.sn]) {/*不是当前设备直接返回*/
  678. return;
  679. }
  680. }
  681. if ([[data allKeys] containsObject:@"status"]) {
  682. NSInteger status = [[data objectForKey:@"status"] integerValue];
  683. if (self->deviceModel.vpnMode == 0) {
  684. if (status != self.vpnStatus) {
  685. self.vpnStatus = status;
  686. [self reconnectDevice];
  687. }
  688. }
  689. }
  690. }
  691. }
  692. else if ([messageType isEqualToString:@"reProduceText"]){
  693. NSString *sn = nil;
  694. if ([[dataDict allKeys] containsObject:@"data"]) {
  695. NSDictionary *data = dataDict[@"data"];
  696. if ([[data allKeys] containsObject:@"text"]) {
  697. NSString *pasteboardStr = [data objectForKey:@"text"];
  698. UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
  699. pasteboard.string = pasteboardStr;
  700. }
  701. }
  702. }
  703. else if ([messageType isEqualToString:@"keyboardFeedbackBean"]){/*调起键盘*/
  704. // [self->hidenTextField setKeyboardType:(UIKeyboardTypeURL)];
  705. if ([[dataDict allKeys] containsObject:@"data"]) {
  706. NSDictionary *data = dataDict[@"data"];
  707. if ([[data allKeys] containsObject:@"type"]) {
  708. NSInteger keyboadType = [[data objectForKey:@"type"] integerValue];
  709. if (1 == keyboadType){
  710. [self->hidenTextField setKeyboardType:(UIKeyboardTypeTwitter)];
  711. }else if (2 == keyboadType){
  712. [self->hidenTextField setKeyboardType:(UIKeyboardTypeNumberPad)];
  713. }else if (3 == keyboadType){
  714. [self->hidenTextField setKeyboardType:(UIKeyboardTypePhonePad)];
  715. }else{
  716. [self->hidenTextField setKeyboardType:(UIKeyboardTypeURL)];
  717. }
  718. }
  719. }
  720. [self->hidenTextField becomeFirstResponder];
  721. [self->hidenTextField setText:@"1"];
  722. self->hidenTextFieldOldStr = @"1";
  723. }
  724. } failure:^(NSError *error) {
  725. }];
  726. }
  727. }
  728. - (void)textDidChange:(UITextField *)textField{
  729. if (textField.markedTextRange == nil) {
  730. /*新内容 完全包含前面的内容 就是删除*/
  731. NSRange newInOld = [hidenTextFieldOldStr rangeOfString:textField.text];
  732. if (newInOld.location == 0 && newInOld.length == textField.text.length){/*删除*/
  733. NSString *dataStr = [RCCommandHelp commandKeyboadInputStr:nil];
  734. [self.businessCommandChannelManager rc_sendData:dataStr];
  735. /*删除以后在输入框前面新增一个字符*/
  736. NSString *tempStr = textField.text;
  737. if (tempStr.length > 0){
  738. }else{
  739. tempStr = @" ";
  740. }
  741. textField.text = tempStr;
  742. }else{
  743. /*新增*/
  744. NSRange oldInNew = [textField.text rangeOfString:hidenTextFieldOldStr];
  745. if (oldInNew.location == 0 && oldInNew.length == hidenTextFieldOldStr.length){/*新增*/
  746. NSString *addStr = [textField.text substringWithRange:(NSMakeRange(hidenTextFieldOldStr.length, textField.text.length - hidenTextFieldOldStr.length))];
  747. NSLog(@"键盘透传--@@@@新增内容:%@",addStr);
  748. NSString *dataStr = [RCCommandHelp commandKeyboadInputStr:addStr];
  749. [self.businessCommandChannelManager rc_sendData:dataStr];
  750. }else{/*部分覆盖*/
  751. /*覆盖分为两部分 1、覆盖的内容更长 2、覆盖的内容更短*/
  752. /*删除不同的部分 加上变化的部分*/
  753. /*后面不同内容的长度*/
  754. NSInteger sameLength = 0;
  755. if (hidenTextFieldOldStr.length < textField.text.length){
  756. sameLength = [self sameStrLengthSmallStr:hidenTextFieldOldStr longStr:textField.text];
  757. }else{
  758. sameLength = [self sameStrLengthSmallStr:textField.text longStr:hidenTextFieldOldStr];
  759. }
  760. /*需要删除的长度*/
  761. NSInteger needDeleteLength = hidenTextFieldOldStr.length - sameLength;
  762. for (NSInteger nFori = 0; nFori < needDeleteLength; nFori++) {
  763. NSString *dataStr = [RCCommandHelp commandKeyboadInputStr:nil];
  764. [self.businessCommandChannelManager rc_sendData:dataStr];
  765. }
  766. /*需要增加的内容*/
  767. NSInteger needLength = textField.text.length - sameLength;
  768. NSString *addStr = [textField.text substringWithRange:(NSMakeRange(sameLength, needLength))];
  769. NSString *dataStr = [RCCommandHelp commandKeyboadInputStr:addStr];
  770. [self.businessCommandChannelManager rc_sendData:dataStr];
  771. }
  772. }
  773. hidenTextFieldOldStr = textField.text;
  774. }
  775. }
  776. - (NSInteger)sameStrLengthSmallStr:(NSString *)litStr longStr:(NSString *)longStr{
  777. NSInteger samenumber = 0;
  778. for (NSInteger nFori = 1; nFori < litStr.length+1; nFori++) {
  779. NSString *temp0 = [litStr substringToIndex:nFori];
  780. NSString *temp1 = [longStr substringToIndex:nFori];
  781. if (![temp0 isEqualToString:temp1]){
  782. break;
  783. }
  784. samenumber++;
  785. }
  786. return samenumber;
  787. }
  788. #pragma mark - UI相关
  789. - (void)drawView
  790. {
  791. hidenTextField = [[UITextField alloc] initWithFrame:(CGRectMake(0, 0, 0, 0))];
  792. hidenTextField.delegate = self;
  793. [hidenTextField addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
  794. [self.view addSubview:hidenTextField];
  795. [self.view setBackgroundColor:[UIColor whiteColor]];
  796. mPlayerView = [[PlayerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
  797. mPlayerView.buyVipType = self.buyVipType;
  798. [mPlayerView initPoMas_makeWithImageRate:[deviceModel isOpenVirtualKey] isUsageMode:[deviceModel isOpenUsageMode]];
  799. [mPlayerView setBackgroundColor:[UIColor blackColor]];
  800. mPlayerView.delegate = self;
  801. mPlayerView.sn = self.deviceModel.sn;
  802. deviceShareStates = 2;
  803. CloudPhoneModel *deviceDcit = [[UseAccountManage shareInstance].cloudPhoneListModel.diskInfo objectAtIndex:phoneIndex];
  804. /*判断是否已分享*/
  805. NSInteger screenShareStatus = deviceDcit.screenShareStatus;
  806. deviceShareStates = screenShareStatus;
  807. [self.view addSubview:mPlayerView];
  808. [mPlayerView mas_makeConstraints:^(MASConstraintMaker *make) {
  809. make.left.mas_equalTo(0);
  810. make.right.mas_equalTo(0);
  811. make.top.mas_equalTo(0);
  812. make.bottom.mas_equalTo(0);
  813. }];
  814. [self reSetPlayerViewFloatBtnFrame];
  815. if (isLan)/*直接g或者变为横屏*/
  816. {
  817. [mPlayerView setLanMas_makeWithImageRate:ksharedAppDelegate.couldPhone_W_PHONE/ksharedAppDelegate.couldPhone_H_PHONE isOpenVirtual:[deviceModel isOpenVirtualKey] isUsageMode:[deviceModel isOpenUsageMode]];
  818. }
  819. else
  820. {
  821. [mPlayerView setPoMas_makeWithImageRate:ksharedAppDelegate.couldPhone_W_PHONE/ksharedAppDelegate.couldPhone_H_PHONE isOpenVirtual:[deviceModel isOpenVirtualKey] isUsageMode:[deviceModel isOpenUsageMode]];
  822. }
  823. }
  824. /**
  825. * @brief 漂浮按钮位置初始化
  826. *
  827. *
  828. */
  829. - (void)reSetPlayerViewFloatBtnFrame
  830. {
  831. // [[[UIApplication sharedApplication] keyWindow] addSubview:mPlayerView.controlBtn];
  832. [self.navigationController.view addSubview:mPlayerView.controlBtn];
  833. [self.navigationController.view bringSubviewToFront:mPlayerView.controlBtn];
  834. NSDictionary *dict = [[UseAccountManage shareInstance] getDviceDriftInfoWithdeviceSN:deviceId];
  835. CGFloat h_w_controlBtn = 60.f;
  836. if (dict)
  837. {
  838. CGFloat pointx = [[dict objectForKey:@"pointx"] floatValue];
  839. CGFloat pointy = [[dict objectForKey:@"pointy"] floatValue];
  840. if (mPlayerView.width > mPlayerView.height){
  841. if (pointx < 30)
  842. {
  843. pointx = 30;
  844. }
  845. else if (pointx > mPlayerView.width - h_w_controlBtn - 10.f)
  846. {
  847. pointx = mPlayerView.width - h_w_controlBtn - 10.f;
  848. }
  849. if (pointy < 10)
  850. {
  851. pointy = 10;
  852. }
  853. else if (pointy > mPlayerView.height - h_w_controlBtn - 10.f)
  854. {
  855. pointy = mPlayerView.height - h_w_controlBtn - 10.f;
  856. }
  857. }else{
  858. if (pointx < 10)
  859. {
  860. pointx = 10;
  861. }
  862. else if (pointx > mPlayerView.width - h_w_controlBtn - 10.f)
  863. {
  864. pointx = mPlayerView.width - h_w_controlBtn - 10.f;
  865. }
  866. if (pointy < 10)
  867. {
  868. pointy = 10;
  869. }
  870. else if (pointy > mPlayerView.height - h_w_controlBtn - 10.f)
  871. {
  872. pointy = mPlayerView.height - h_w_controlBtn - 10.f;
  873. }
  874. }
  875. // BOOL hidden = [[dict objectForKey:@"hidden"] boolValue];
  876. [mPlayerView.controlBtn setFrame:CGRectMake(pointx, pointy, h_w_controlBtn, h_w_controlBtn)];
  877. }
  878. else
  879. {
  880. /*区分横竖屏*/
  881. if (mPlayerView.width > mPlayerView.height){
  882. [mPlayerView.controlBtn setFrame:CGRectMake((mPlayerView.width - h_w_controlBtn)/2.f, mPlayerView.height - h_w_controlBtn - 20.f, h_w_controlBtn, h_w_controlBtn)];
  883. }else{
  884. [mPlayerView.controlBtn setFrame:CGRectMake(mPlayerView.width - h_w_controlBtn - 20.f, (SCREEN_H - h_w_controlBtn)/2.f, h_w_controlBtn, h_w_controlBtn)];
  885. }
  886. [mPlayerView.controlBtn setHidden:NO];
  887. }
  888. HLog(@"云手机推流界面 控制按钮中点%@",NSStringFromCGPoint(mPlayerView.controlBtn.center));
  889. }
  890. #pragma mark - 手势权限控制处理
  891. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer
  892. *)gestureRecognizer{
  893. return NO; //YES:允许右滑返回 NO:禁止右滑返回
  894. }
  895. - (void)enablePanRightBack
  896. {
  897. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  898. self.navigationController.interactivePopGestureRecognizer.enabled = YES;
  899. }
  900. if ([self.tabBarController.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  901. self.tabBarController.navigationController.interactivePopGestureRecognizer.enabled = YES;
  902. }
  903. }
  904. - (void)noEnablePanRightBack
  905. {
  906. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  907. self.navigationController.interactivePopGestureRecognizer.enabled = NO;
  908. }
  909. if ([self.tabBarController.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  910. self.tabBarController.navigationController.interactivePopGestureRecognizer.enabled = NO;
  911. }
  912. }
  913. - (void)saveCIImageAction:(NSString *)deviceId{
  914. UIImage *image = [UIImage imageWithCIImage:saveCIImage];
  915. if(image){ //执行返回最后一帧
  916. [mPlayerView.showImageView setImage:image];
  917. CloudPhoneModel *deviceDcit1 = [[UseAccountManage shareInstance].cloudPhoneListModel.diskInfo objectAtIndex:phoneIndex];
  918. /*保存上传推流设备最新视图*/
  919. NSString *account = nil;
  920. NSString *imageUrl1 = nil;
  921. NSDictionary *dictLogin = [[UseAccountManage shareInstance] getLoginInfo];
  922. if ([[dictLogin allKeys] containsObject:@"account"])
  923. {
  924. account = [dictLogin objectForKey:@"account"];
  925. imageUrl1 = [HWDataManager pathForDevicePhotoWithAccount:account deviceId:[NSString stringWithFormat:@"%ld",deviceDcit1.phoneId]];
  926. }
  927. deviceDcit1.needUpdateImageInSever = NO;
  928. [[NSFileManager defaultManager] removeItemAtPath:imageUrl1 error:nil];
  929. [UIImagePNGRepresentation(image) writeToFile:imageUrl1 atomically:NO];
  930. }
  931. }
  932. #pragma mark - RCLiveSteamManagerDelegate
  933. - (void)liveStreamManagerOutPut:(RCLiveSteamManager *)manager didOutPutVideoPixelBuffer:(CVPixelBufferRef)pixelBuffer {
  934. if (!mySelf || mySelf->haveBack) {
  935. return;
  936. }
  937. if (!pixelBuffer) {
  938. return;
  939. }
  940. [mySelf updateAutoRateAndNetWorkStates];//更新网络标识 以及自动码率
  941. if (mySelf&&!(mySelf->haveBack))
  942. {
  943. mySelf->reConnectAccount = 0;
  944. @autoreleasepool {
  945. if (!mySelf)
  946. {
  947. return ;
  948. }
  949. CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
  950. if (mySelf->saveCIImage) {
  951. mySelf->saveCIImage = nil;
  952. }
  953. mySelf->saveCIImage = ciImage;
  954. if (!mySelf->videoContext) mySelf->videoContext = [CIContext contextWithOptions:nil];
  955. if (USEOPENGL)
  956. {
  957. if (ciImage)
  958. {
  959. [mySelf showImageOpenglWithCIImage:ciImage];/*使用Opengl渲染*/
  960. mySelf.concentTime = [iTools getNowTimeStamp];
  961. mySelf.isConnectSuccess = YES;
  962. }
  963. }
  964. else
  965. {
  966. if (ciImage)
  967. {
  968. [mySelf showImageUIImageViewWithCIImage:ciImage pixelBufferRef:pixelBuffer];/*使用UIImageView渲染*/
  969. mySelf.concentTime = [iTools getNowTimeStamp];
  970. mySelf.isConnectSuccess = YES;
  971. }
  972. }
  973. if (mySelf&&!mySelf->haveBack && mySelf->wattingView)
  974. {
  975. mainBlock(^{
  976. [mySelf removeNewIndicator];
  977. });
  978. }
  979. }
  980. }
  981. }
  982. - (void)liveStreamManagerOutPut:(RCLiveSteamManager *)manager didOutPutAudioData:(NSData *)pcmData {
  983. @autoreleasepool {
  984. if (mySelf&&!(mySelf->haveBack))
  985. {
  986. [mySelf->connectLock lock];
  987. if (!mySelf->pcmPlayer)
  988. {
  989. mySelf->pcmPlayer = [[RCPCMPlayer alloc] initWithAudioBufferSize:4096 mSampleRate:44100 mChannelsPerFrame:2];
  990. mySelf.isNeedShowVoiceBar = NO;
  991. [mySelf->pcmPlayer start];
  992. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  993. mySelf.isNeedShowVoiceBar = YES;
  994. });
  995. }
  996. if (pcmData)
  997. {
  998. mySelf.isNeedShowVoiceBar = NO;
  999. [mySelf->pcmPlayer play:pcmData];
  1000. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1001. mySelf.isNeedShowVoiceBar = YES;
  1002. });
  1003. }
  1004. [mySelf->connectLock unlock];
  1005. }
  1006. }
  1007. }
  1008. - (void)liveStreamManager:(RCLiveSteamManager *)manager connectState:(ConnectState)state {
  1009. HLog(@"\n------状态回调---state = %d--", state);
  1010. //[[VclCrash sharedInstance] writeContent:[NSString stringWithFormat:@"_connectDisconnect连接返回句柄 = %d--state--=%d-",ihandle,state]];
  1011. switch (state)
  1012. {
  1013. case 0:/*连接已释放*/
  1014. {
  1015. }
  1016. break;
  1017. case 1:/*正在连接*/
  1018. {
  1019. }
  1020. break;
  1021. case 2:/*连接成功*/
  1022. {
  1023. HLog(@"\n-----_%s------SSSSSSSSSSS连接成功",__FUNCTION__);
  1024. if (self.isFirstEnterPhone) {
  1025. // 埋点
  1026. HWBuryPointModel *pointModel = [HWBuryPointModel buryPointWithEventType:(EventCodeTypeCloudPhonePushEvent) buttonType:(ClickButtonTypeCloudPhonePushEnter)];
  1027. pointModel.eventResult = @"1";
  1028. [BuryPoint saveHWBuryPointModelListWithModel:pointModel];
  1029. self.isFirstEnterPhone = NO;
  1030. }
  1031. }
  1032. break;
  1033. case 3:/*正在渲染*/
  1034. {
  1035. }
  1036. break;
  1037. case 4:/*正在关闭*/
  1038. {
  1039. }
  1040. break;
  1041. case 5:/*已关闭*/
  1042. {
  1043. }
  1044. break;
  1045. case 6:
  1046. {
  1047. mainBlock(^{
  1048. [self showNewIndicator];
  1049. [self tryAgain];
  1050. });
  1051. }
  1052. break;
  1053. case ConnectStateVerifyErr:
  1054. {
  1055. HLog(@"\n------token异常----重新超分-----");
  1056. /*鉴权失败 进行超分操作*/
  1057. /*先清除当前的token*/
  1058. // NSDictionary *dictLogin = [[UseAccountManage shareInstance] getLoginInfo];
  1059. // if (dictLogin && [[dictLogin allKeys] containsObject:@"userName"])
  1060. // {
  1061. // /*判断数据库中有没有对应phoneid*/
  1062. // NSString* username = [dictLogin objectForKey:@"userName"];
  1063. // NSString * bg_tableName = P2PModelList_bg_tableName(username);
  1064. // //先显示本地缓存数据
  1065. // NSArray* finfAlls = [PhoneP2PListModel bg_findAll:bg_tableName];
  1066. // if (finfAlls && finfAlls.count >0) {
  1067. // PhoneP2PListModel *listMod = finfAlls[0];
  1068. // for (PhoneP2PModel *p2pModel in listMod.data) {
  1069. // if (p2pModel.phoneId == self.deviceModel.phoneId){
  1070. // [listMod.data removeObject:p2pModel];
  1071. // [listMod bg_cover];
  1072. // break;
  1073. // }
  1074. // }
  1075. // }
  1076. // }
  1077. [[P2PDataManager shareInstance] deleteRecoderWithPhoneID:self.deviceModel.phoneId];
  1078. /*开启超分流程*/
  1079. [self extensionPhoneGetRBD];
  1080. }
  1081. break;
  1082. default:
  1083. break;
  1084. }
  1085. }
  1086. - (void)openVideForLiveStreamManager:(AVCaptureDevicePosition)position needPost:(BOOL)needPost
  1087. {
  1088. // 限免规则判断
  1089. NSMutableArray *freeCodes = [UseAccountManage shareInstance].cloudPhoneListModel.freeCodes;
  1090. BOOL isFreeUse = NO;
  1091. for (NSString *code in freeCodes) { // 限免增值服务数组
  1092. if ([code isEqualToString:@"PHONE_CAMERA"]) {
  1093. isFreeUse = YES;
  1094. break;
  1095. }
  1096. }
  1097. if (mySelf->deviceModel.phoneCameraStatus == 2) {
  1098. // 当前云手机摄像头不可用
  1099. mainBlock(^{
  1100. [[iToast makeText:@"当前云手机摄像头不可用"] show];
  1101. });
  1102. return;
  1103. }else if (mySelf->deviceModel.phoneCameraStatus == 0 && !isFreeUse) {
  1104. //未购买增值服务的摄像头功能&&不限免
  1105. mainBlock(^{
  1106. [[iToast makeText:@"当前未购买此功能\n请在增值服务-云机摄像头开通此功能"] show];
  1107. });
  1108. return;
  1109. }
  1110. if (mySelf&&!(mySelf->haveBack))
  1111. {
  1112. if (mySelf->canControl)
  1113. {
  1114. [mySelf toStopRecoder];
  1115. HLog(@"\n-----打开视频透传 后置摄像头-------");
  1116. [mySelf toStartRecoderWithAVCaptureDevicePosition:(position)];
  1117. if (needPost) {
  1118. // 云机摄像头使用上报
  1119. [mySelf postCloudCamera];
  1120. }
  1121. }
  1122. }
  1123. }
  1124. - (void)liveStreamManager:(RCLiveSteamManager *)manager didReceiveEvent:(EventState)event {
  1125. EventState state = event;
  1126. if (0 == state)/*横屏*/
  1127. {
  1128. isLan = YES;
  1129. mySelf->mPlayerView.isLan = YES;
  1130. [[VclCrash sharedInstance] writeContent:[NSString stringWithFormat:@"______________视频数据_______"]];
  1131. /*横屏处理*/
  1132. [self setDeviceOrientation:(UIDeviceOrientationLandscapeLeft)];
  1133. HLog(@"\n---000000--横屏--000000-----");
  1134. }
  1135. else if (1 == state)/*竖屏*/
  1136. {
  1137. isLan = NO;
  1138. mySelf->mPlayerView.isLan = NO;
  1139. [[VclCrash sharedInstance] writeContent:[NSString stringWithFormat:@"______________视频数据_______"]];
  1140. HLog(@"\n---1111111--竖屏--1111111-----");
  1141. /*横屏处理*/
  1142. [self setDeviceOrientation:(UIDeviceOrientationPortrait)];
  1143. }
  1144. else if (2 == state)/*打开音频透传*/
  1145. {
  1146. if (mySelf&&!(mySelf->haveBack))
  1147. {
  1148. if (mySelf->canControl)
  1149. {
  1150. HLog(@"\n-----打开视频透传-------");
  1151. [mySelf toStartAudioRecoder];
  1152. }
  1153. }
  1154. }
  1155. else if (3 == state)/*关闭音频透传*/
  1156. {
  1157. [mySelf toStopAudioRecoder];
  1158. }
  1159. else if (4 == state)/*打开视频透传*/
  1160. {
  1161. [self openVideForLiveStreamManager:AVCaptureDevicePositionBack needPost:YES];
  1162. }
  1163. else if (5 == state)/*关闭视频透传*/
  1164. {
  1165. [mySelf toStopRecoder];
  1166. }
  1167. else if (7 == state)/*打开视频透传--前置摄像头*/
  1168. {
  1169. [self openVideForLiveStreamManager:AVCaptureDevicePositionFront needPost:YES];
  1170. }
  1171. else if (EventStateTurnOnVideoDontNeedToServer == state)/*打开视频透传*/
  1172. {
  1173. [self openVideForLiveStreamManager:AVCaptureDevicePositionBack needPost:NO];
  1174. }
  1175. else if (EventStateTurnOnVideoFrontDontNeedToServer == state)/*打开视频透传--前置摄像头*/
  1176. {
  1177. [self openVideForLiveStreamManager:AVCaptureDevicePositionFront needPost:NO];
  1178. }
  1179. else if (EventStateTurnOnVideoDontNeedToServer == state)/*打开视频透传*/
  1180. {
  1181. [self openVideForLiveStreamManager:AVCaptureDevicePositionBack needPost:NO];
  1182. }
  1183. else if (EventStateTurnOnVideoFrontDontNeedToServer == state)/*打开视频透传--前置摄像头*/
  1184. {
  1185. [self openVideForLiveStreamManager:AVCaptureDevicePositionFront needPost:NO];
  1186. }
  1187. }
  1188. - (void)liveStreamManager:(RCLiveSteamManager *)manager didReceiveMessage:(NSString *)message {
  1189. }
  1190. - (void)liveStreamManager:(RCLiveSteamManager *)manager didReceiNetEstimator:(NetEstimator)estimator{
  1191. }
  1192. - (void)liveStreamManager:(RCLiveSteamManager *)manager didReceiveJitterDelay:(int)jitterDelayMs{
  1193. }
  1194. #pragma mark - 视频流回调
  1195. void connectStateCallBack (void *client, ConnectState state, void *context)
  1196. {
  1197. NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:1],@"ihandle",[NSNumber numberWithInt:state],@"state" ,nil];
  1198. [[NSNotificationCenter defaultCenter] postNotificationName:NotNameByCONNECT_DISCONNECT object:dict];
  1199. }
  1200. static int delayVideo = 0;/*当前最高延时*/
  1201. static int okdelayVideo = 0;/*当前连续延时较好帧数*/
  1202. static int baddelayVideo = 0;/*当前较坏延时帧数个数*/
  1203. static int havedelayVideoCount = 0;/*当前帧数个数*/
  1204. NSDate *lastVideoTome;/*上一帧数据时间*/
  1205. - (void)toStartRecoderWithAVCaptureDevicePosition:(AVCaptureDevicePosition)position
  1206. {
  1207. if (!self.recoderManager)
  1208. {
  1209. self.recoderManager = [[RCVideoRecoderManager alloc] initWithAVCaptureDevicePosition:position];
  1210. self.recoderManager.delegate = self;
  1211. }
  1212. if (!self.recoderManager.isRecoding)
  1213. {
  1214. [self.recoderManager startRecording];
  1215. }
  1216. }
  1217. - (void)toStopRecoder
  1218. {
  1219. if (self.recoderManager)
  1220. {
  1221. [self.recoderManager stopRecording];
  1222. self.recoderManager = nil;
  1223. }
  1224. }
  1225. - (void)toStartAudioRecoder
  1226. {
  1227. if (!self.audioRecoderMamager)
  1228. {
  1229. self.audioRecoderMamager = [[RCAudioRecoderMamager alloc] init];
  1230. self.audioRecoderMamager.delegate = self;
  1231. }
  1232. if (!self.audioRecoderMamager.isRecoding)
  1233. {
  1234. [self.audioRecoderMamager startRecording];
  1235. }
  1236. }
  1237. - (void)toStopAudioRecoder
  1238. {
  1239. if (self.audioRecoderMamager)
  1240. {
  1241. [self.audioRecoderMamager stopRecording];
  1242. self.audioRecoderMamager = nil;
  1243. }
  1244. }
  1245. /**
  1246. * @brief 请求I帧数据
  1247. */
  1248. - (void)requestIFrame
  1249. {
  1250. [self.liveStreamManager rc_requestIDFrame];
  1251. }
  1252. #pragma mark - 流媒体服务器
  1253. // 初始化
  1254. - (BOOL)initVideoServer {
  1255. return YES;
  1256. }
  1257. // 连接
  1258. - (void)connectVideoServer {
  1259. if (!mySelf || haveBack){
  1260. return;
  1261. }
  1262. if (!connectLock)
  1263. {
  1264. connectLock = [[NSLock alloc] init];
  1265. }
  1266. [connectLock lock];
  1267. delayVideo = 0;
  1268. okdelayVideo = 0;
  1269. havedelayVideoCount = 0;
  1270. HLog(@"__________连接000000__________deviceModel.cardToken = %@",deviceModel.cardToken);
  1271. RCStateInt result = -1;
  1272. if([deviceModel.pushFlowCode isEqualToString:@"H265"])
  1273. {
  1274. if (deviceModel.vpnMode == 1){//UPD
  1275. NSString *tempIP = ip;
  1276. result = [self.liveStreamManager rc_openWithIp:ip
  1277. port:internetVideoPort.intValue
  1278. connectType:RCCloudPhoneConnectType_udp
  1279. videoQuality:RCCloudPhoneVideoQuality_High
  1280. verify:deviceModel.cardToken
  1281. streamAESKey:nil];
  1282. if ([tempIP isEqualToString:ip])
  1283. {
  1284. if (result < 0)
  1285. {
  1286. [self tryAgain];
  1287. }
  1288. else if (result == 12)
  1289. {
  1290. [[iToast makeText:@"画面异常,请重新进入"] show];
  1291. }
  1292. }
  1293. KyoLog(@"__________连接方式000000___UDP____H265___");
  1294. }
  1295. else if (deviceModel.vpnMode == 2){//TCP
  1296. NSString *tempIP = ip;
  1297. result = [self.liveStreamManager rc_openWithIp:ip
  1298. port:internetVideoPort.intValue
  1299. connectType:RCCloudPhoneConnectType_websocket
  1300. videoQuality:RCCloudPhoneVideoQuality_High
  1301. verify:deviceModel.cardToken
  1302. streamAESKey:nil];
  1303. if ([tempIP isEqualToString:ip])
  1304. {
  1305. if (result < 0)
  1306. {
  1307. [self tryAgain];
  1308. }
  1309. else if (result == 12)
  1310. {
  1311. [[iToast makeText:@"画面异常,请重新进入"] show];
  1312. }
  1313. }
  1314. HLog(@"__________连接方式000000___TCP__H265_____");
  1315. }
  1316. else{
  1317. if (vpnStatus == 1) {
  1318. NSString *tempIP = ip;
  1319. result = [self.liveStreamManager rc_openWithIp:ip
  1320. port:internetVideoPort.intValue
  1321. connectType:RCCloudPhoneConnectType_websocket
  1322. videoQuality:RCCloudPhoneVideoQuality_High
  1323. verify:deviceModel.cardToken
  1324. streamAESKey:nil];
  1325. if ([tempIP isEqualToString:ip])
  1326. {
  1327. if (result < 0)
  1328. {
  1329. [self tryAgain];
  1330. }
  1331. else if (result == 12)
  1332. {
  1333. [[iToast makeText:@"画面异常,请重新进入"] show];
  1334. }
  1335. }
  1336. HLog(@"__________连接方式000000___TCP__H265_____");
  1337. }
  1338. else //默认使用UDP
  1339. {
  1340. NSString *tempIP = ip;
  1341. result = [self.liveStreamManager rc_openWithIp:ip
  1342. port:internetVideoPort.intValue
  1343. connectType:RCCloudPhoneConnectType_udp
  1344. videoQuality:RCCloudPhoneVideoQuality_High
  1345. verify:deviceModel.cardToken
  1346. streamAESKey:nil];
  1347. if ([tempIP isEqualToString:ip])
  1348. {
  1349. if (result < 0)
  1350. {
  1351. [self tryAgain];
  1352. }
  1353. else if (result == 12)
  1354. {
  1355. [[iToast makeText:@"画面异常,请重新进入"] show];
  1356. }
  1357. }
  1358. KyoLog(@"__________连接方式000000___UDP____H265___");
  1359. }
  1360. }
  1361. }
  1362. else
  1363. {
  1364. if (deviceModel.vpnMode == 1) {
  1365. NSString *tempIP = ip;
  1366. result = [self.liveStreamManager rc_openWithIp:ip
  1367. port:internetVideoPort.intValue
  1368. connectType:RCCloudPhoneConnectType_udp
  1369. videoQuality:RCCloudPhoneVideoQuality_Default
  1370. verify:deviceModel.cardToken
  1371. streamAESKey:nil];
  1372. if ([tempIP isEqualToString:ip])
  1373. {
  1374. if (result < 0)
  1375. {
  1376. [self tryAgain];
  1377. }
  1378. else if (result == 12)
  1379. {
  1380. [[iToast makeText:@"画面异常,请重新进入"] show];
  1381. }
  1382. }
  1383. if (result > 0) {
  1384. KyoLog(@"__________连接方式__000000___UDP____H264_ip = %@__ port = %@", ip, internetVideoPort);
  1385. } else {
  1386. KyoLog(@"__________连接失败__000000___UDP____H264_ip = %@__ port = %@", ip, internetVideoPort);
  1387. }
  1388. }
  1389. else if (deviceModel.vpnMode == 2){
  1390. NSString *tempIP = ip;
  1391. result = [self.liveStreamManager rc_openWithIp:ip
  1392. port:internetVideoPort.intValue
  1393. connectType:RCCloudPhoneConnectType_websocket
  1394. videoQuality:RCCloudPhoneVideoQuality_Default
  1395. verify:deviceModel.cardToken
  1396. streamAESKey:nil];
  1397. if ([tempIP isEqualToString:ip])
  1398. {
  1399. if (result < 0)
  1400. {
  1401. [self tryAgain];
  1402. }
  1403. else if (result == 12)
  1404. {
  1405. [[iToast makeText:@"画面异常,请重新进入"] show];
  1406. }
  1407. }
  1408. KyoLog(@"__________连接方式000000___TCP____H264___");
  1409. }else{
  1410. //如果安卓卡用户开启VPN,需切换TCP
  1411. if (vpnStatus == 1) {
  1412. NSString *tempIP = ip;
  1413. result = [self.liveStreamManager rc_openWithIp:ip
  1414. port:internetVideoPort.intValue
  1415. connectType:RCCloudPhoneConnectType_websocket
  1416. videoQuality:RCCloudPhoneVideoQuality_Default
  1417. verify:deviceModel.cardToken
  1418. streamAESKey:nil];
  1419. if ([tempIP isEqualToString:ip])
  1420. {
  1421. if (result < 0)
  1422. {
  1423. [self tryAgain];
  1424. }
  1425. else if (result == 12)
  1426. {
  1427. [[iToast makeText:@"画面异常,请重新进入"] show];
  1428. }
  1429. }
  1430. KyoLog(@"__________连接方式000000___TCP____H264___");
  1431. }
  1432. else //默认使用UDP
  1433. {
  1434. NSString *tempIP = ip;
  1435. result = [self.liveStreamManager rc_openWithIp:ip
  1436. port:internetVideoPort.intValue
  1437. connectType:RCCloudPhoneConnectType_udp
  1438. videoQuality:RCCloudPhoneVideoQuality_Default
  1439. verify:deviceModel.cardToken
  1440. streamAESKey:nil];
  1441. if ([tempIP isEqualToString:ip])
  1442. {
  1443. if (result < 0)
  1444. {
  1445. [self tryAgain];
  1446. }
  1447. else if (result == 12)
  1448. {
  1449. [[iToast makeText:@"画面异常,请重新进入"] show];
  1450. }
  1451. }
  1452. if (result > 0) {
  1453. KyoLog(@"__________连接方式__000000___UDP____H264_ip = %@__ port = %@", ip, internetVideoPort);
  1454. } else {
  1455. KyoLog(@"__________连接失败__000000___UDP____H264_ip = %@__ port = %@", ip, internetVideoPort);
  1456. }
  1457. }
  1458. }
  1459. }
  1460. HLog(@"______连接____ip = %@ port = %@",ip,internetVideoPort);
  1461. [[VclCrash sharedInstance] writeContent:[NSString stringWithFormat:@"connectVideoServer连接返回句柄 = %d--",result]];
  1462. HLog(@"__________连接1111111__________");
  1463. HLog(@"__________连接成功__________");
  1464. lastVideoTome = [NSDate date];
  1465. NSString *dataStr = [RCCommandHelp commandResolution500WithType:(NSUInteger)BITRATE_WIFI_HD];
  1466. [self.liveStreamManager rc_sendJsonData:[dataStr UTF8String]];
  1467. // // 20210305 因为安卓做了分辨率适应 而iOS不需要 所以现在指定接收码率 这个就不用修改对应触摸点的范围比例了 W_PHONE H_PHONE
  1468. // // 发送数据
  1469. // if ([deviceModel.buyVipType isEqualToString:CloudPhoneTypeWuJiPro]) {
  1470. // NSString *commondStr = @"{\"data\":{\"height\":1920,\"width\":1080},\"type\":\"setPhoneSize\"}";
  1471. // [self send_data:commondStr];
  1472. // }
  1473. //
  1474. // //记录推流开始时间
  1475. // NSString *connectedDataStr = [RCCommandHelp commandConnectedWithDeviceId:deviceId];
  1476. // [self send_data:connectedDataStr];
  1477. [connectLock unlock];
  1478. }
  1479. // 断开连接
  1480. - (void)disconnectVideoServer {
  1481. NSString *commondStr = [RCCommandHelp toStopControllerDevice500];
  1482. [self send_data:commondStr];
  1483. HLog(@"\n-----...commondStr = %@....",commondStr);
  1484. [self.liveStreamManager rc_disconnect];
  1485. }
  1486. - (void)send_data:(NSString *)dataStr
  1487. {
  1488. if (canControl)
  1489. {
  1490. KyoLog(@"\n-commandChannelManager---%ld--%@",self.commandChannelManager.rc_socketStatus,dataStr);
  1491. [self.commandChannelManager rc_sendData:dataStr];
  1492. }
  1493. }
  1494. #pragma mark - PlayerViewDelegate---下方虚拟按键
  1495. - (void)mueBtnBePressed:(id)sender
  1496. {
  1497. HLog(@"__________%s__________",__func__);
  1498. NSString *dataStr = [RCCommandHelp commandBackOrHomeVolum500WithKeyCode:187];
  1499. HLog(@"\n-----...dataStr = %@....",dataStr);
  1500. [self send_data:dataStr];
  1501. }
  1502. - (void)homeBtnBePressed:(id)sender
  1503. {
  1504. HLog(@"__________%s__________",__func__);
  1505. NSString *dataStr = [RCCommandHelp commandBackOrHomeVolum500WithKeyCode:3];
  1506. HLog(@"\n-----...dataStr = %@....",dataStr);
  1507. [self send_data:dataStr];
  1508. }
  1509. - (void)backBtnBePressed:(id)sender
  1510. {
  1511. HLog(@"__________%s__________",__func__);
  1512. NSString *dataStr = [RCCommandHelp commandBackOrHomeVolum500WithKeyCode:4];
  1513. HLog(@"\n-----...dataStr = %@....",dataStr);
  1514. [self send_data:dataStr];
  1515. }
  1516. - (void)useInfoBtnBePressed:(id)sender
  1517. {
  1518. HLog(@"云手机推流界面 控制按钮点击手势开始");
  1519. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchBeganNotification object:nil userInfo:nil];
  1520. HLog(@"云手机推流界面 控制按钮点击手势结束");
  1521. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchEndNotification object:nil userInfo:nil];
  1522. HLog(@"__________%s__________",__func__);
  1523. MySetViewController *nextVC = [[MySetViewController alloc] init];
  1524. [self.navigationController pushViewController:nextVC animated:YES];
  1525. self.isNeedRecyclResource = NO;
  1526. }
  1527. #pragma mark - PlayerControlViewDelegate---控制器按键
  1528. //- (void)stopShareBtnBePressed:(id)sender
  1529. //{
  1530. // HLog(@"__________%s__________",__func__);
  1531. // /*关闭分享成功后调用*/
  1532. // CloudPhoneModel *deviceDcit = [[UseAccountManage shareInstance].cloudPhoneListModel.diskInfo objectAtIndex:phoneIndex];
  1533. // NSString *userCardId = @"";
  1534. // userCardId = [NSString stringWithFormat:@"%ld",deviceDcit.phoneId];
  1535. //
  1536. // NSString *username = deviceDcit.username;
  1537. //
  1538. // if (0 == deviceShareStates || deviceShareStates == 3)/*分享中*/
  1539. // {
  1540. // /*暂停分享*/
  1541. // NSString *reqsturl = [NSString stringWithFormat:SuspendScreenShareAPI,userCardId,username];
  1542. // [[UseAccountManage shareInstance] CommonGetWithCallBackCode:reqsturl Parameters:nil success:^(id _Nonnull responseObject) {
  1543. // SuperModel *mod = [[SuperModel alloc] initWithDictionary:responseObject error:nil];
  1544. // [self removeNewIndicator];
  1545. // NSString *showStr = nil;
  1546. // showStr = mod.msg;
  1547. // if (mod.status && mod.status.integerValue == 0)
  1548. // {
  1549. // deviceDcit.screenShareStatus = 1;
  1550. // self->deviceShareStates = 1;
  1551. // [self->controlView pauseShareView];
  1552. // /*刷新视图 更新设备列表*/
  1553. // deviceDcit.screenShareStatus = self->deviceShareStates;
  1554. // }
  1555. // else
  1556. // {
  1557. // if (mod.msg == nil || [mod.msg isEqualToString:@""])
  1558. // {
  1559. // showStr = @"暂停分享失败";
  1560. // }
  1561. // [[iToast makeText:showStr] show];
  1562. // }
  1563. // }
  1564. // failure:^(NSError * _Nonnull error) {
  1565. // [[iToast makeText:@"暂停分享失败"] show];
  1566. // [self removeNewIndicator];
  1567. // }];
  1568. // }
  1569. // else if (1 == deviceShareStates)/*暂停分享*/
  1570. // {
  1571. // /*开启分享*/
  1572. // NSString *reqsturl = [NSString stringWithFormat:StartAgainScreenShareAPI,userCardId,username];
  1573. // [[UseAccountManage shareInstance] CommonGetWithCallBackCode:reqsturl Parameters:nil success:^(id _Nonnull responseObject) {
  1574. // SuperModel *mod = [[SuperModel alloc] initWithDictionary:responseObject error:nil];
  1575. // [self removeNewIndicator];
  1576. // NSString *showStr = nil;
  1577. // showStr = mod.msg;
  1578. // if (mod.status && mod.status.integerValue == 0)
  1579. // {
  1580. // self->deviceShareStates = 3;
  1581. // deviceDcit.screenShareStatus = self->deviceShareStates;
  1582. // [self->controlView shareingShareView];
  1583. // }
  1584. // else
  1585. // {
  1586. // if (mod.msg == nil || [mod.msg isEqualToString:@""])
  1587. // {
  1588. // showStr = @"开启分享失败";
  1589. // }
  1590. // [[iToast makeText:showStr] show];
  1591. // }
  1592. // }
  1593. // failure:^(NSError * _Nonnull error) {
  1594. // [[iToast makeText:@"开启分享失败"] show];
  1595. // [self removeNewIndicator];
  1596. // }];
  1597. // }
  1598. //}
  1599. - (void)exitAndClosePlayerView
  1600. {
  1601. // 埋点
  1602. HWBuryPointModel *pointModel = [HWBuryPointModel buryPointWithEventType:(EventCodeTypeCloudPhonePushEvent) buttonType:(ClickButtonTypeCloudPhonePushExit)];
  1603. [BuryPoint saveHWBuryPointModelListWithModel:pointModel];
  1604. }
  1605. - (void)exitPlayerViewBtnBePressed:(id)sender
  1606. {
  1607. // 埋点
  1608. HWBuryPointModel *pointModel = [HWBuryPointModel buryPointWithEventType:(EventCodeTypeCloudPhonePushEvent) buttonType:(ClickButtonTypeCloudPhonePushExit)];
  1609. [BuryPoint saveHWBuryPointModelListWithModel:pointModel];
  1610. [self exitPlayerView];
  1611. }
  1612. - (void)exitAndClosePlayerViewBtnBePressed:(id)sender{
  1613. [self exitAndClosePlayerView];
  1614. }
  1615. - (void)exitPlayerView{
  1616. [[UseAccountManage shareInstance] saveDviceDriftInfoWith:self->mPlayerView.controlBtn.frame.origin.x
  1617. ponitY:self->mPlayerView.controlBtn.frame.origin.y
  1618. hidden:self->mPlayerView.controlBtn.hidden
  1619. deviceSN:self->deviceId];
  1620. [self.navigationController popViewControllerAnimated:YES];
  1621. [self removeNewIndicator];
  1622. [self->mPlayerView.controlBtn removeFromSuperview];
  1623. [self->connectLock lock];
  1624. mySelf = nil;
  1625. self->haveBack = YES;
  1626. [self->connectLock unlock];
  1627. //返回回调
  1628. mainBlock(^{
  1629. //退出返回最后一帧
  1630. [self saveCIImageAction:self->deviceId];
  1631. });
  1632. }
  1633. - (void)showAlertToWaitForTrial:(NSInteger)mins {
  1634. NSString *tipsStr = [NSString stringWithFormat:@"由于系统资源繁忙\n请在%ld分钟后再次进入云手机", mins];
  1635. NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:tipsStr attributes: @{NSFontAttributeName: [UIFont systemFontOfSize:16],NSForegroundColorAttributeName: HWCFD1D4Color}];
  1636. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  1637. }];
  1638. [sureAction setValue:HW3B7FFFColor forKey:@"titleTextColor"];//iOS8.3
  1639. [self showAlertWithTitle:nil message:string tilteAlignment:NSTextAlignmentCenter messageAlignment:NSTextAlignmentCenter sureAction:sureAction cancelAction:nil];
  1640. }
  1641. #pragma mark - 新的手势回调委托
  1642. - (void)playerShowImageViewtouchesStr:(NSString *)commondStr
  1643. {
  1644. if (canControl)
  1645. {
  1646. [self send_data:commondStr];
  1647. }
  1648. }
  1649. -(void)initAlertWithTitle:(NSString *_Nullable)title message:(NSString *)message sureActionBlock:(sureActionBlock)sureActionBlock {
  1650. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  1651. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  1652. sureActionBlock(alertController);
  1653. }];
  1654. [alertController addAction:cancelAction];
  1655. // 弹窗边框
  1656. UIView *subview = alertController.view.subviews.firstObject;
  1657. UIView *alertContentView = subview.subviews.firstObject;
  1658. alertContentView.layer.cornerRadius = 10;
  1659. // alertContentView.layer.masksToBounds = YES;
  1660. alertContentView.layer.shadowColor = [UIColor colorWithRed:30/255.0 green:32/255.0 blue:34/255.0 alpha:1.0].CGColor;
  1661. alertContentView.layer.shadowOffset = CGSizeMake(0,0);
  1662. alertContentView.layer.shadowOpacity = 1;
  1663. alertContentView.layer.shadowRadius = 7;
  1664. // 弹窗背景颜色
  1665. if (@available(iOS 13.0, *)) {
  1666. alertController.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;
  1667. }
  1668. UIViewController *currentVC = [[ShortcutManager shareInstance] findCurrentShowingViewController];
  1669. [currentVC presentViewController:alertController animated:YES completion:nil];
  1670. // [self presentViewController:alertController animated:YES completion:nil];
  1671. }
  1672. #pragma - mark - 监听网络变化
  1673. - (void)networkStateChange:(NSNotification *)notification
  1674. {
  1675. /*退出登录状态 直接无视通知*/
  1676. NSDictionary *dictLogin = [[UseAccountManage shareInstance] getLoginInfo];
  1677. if ([[dictLogin allKeys] containsObject:@"account"])
  1678. {
  1679. /*处理*/
  1680. switch ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus)
  1681. {
  1682. case AFNetworkReachabilityStatusUnknown:/*网络状态未知*/
  1683. {
  1684. ;
  1685. }
  1686. break;
  1687. case AFNetworkReachabilityStatusNotReachable:/*网络不可用*/
  1688. {
  1689. [[iToast makeText:@"网络异常,请稍后重试!"] show];
  1690. // if (_ihandle > 0)
  1691. // {
  1692. [self disconnectVideoServer];
  1693. // }
  1694. }
  1695. break;
  1696. case AFNetworkReachabilityStatusReachableViaWWAN:/*流量*/
  1697. {
  1698. mainBlock(^{
  1699. [self showNewIndicator];
  1700. });
  1701. [self tryAgain];
  1702. }
  1703. break;
  1704. case AFNetworkReachabilityStatusReachableViaWiFi:/*WiFi*/
  1705. {
  1706. mainBlock(^{
  1707. [self showNewIndicator];
  1708. });
  1709. [self tryAgain];
  1710. }
  1711. break;
  1712. default:
  1713. break;
  1714. }
  1715. }
  1716. else
  1717. {
  1718. /*设备已在别处登录*/
  1719. [self initAlertWithTitle:nil message:NSLocalizedString(@"log_login_other",nil) sureActionBlock:^(UIAlertController * _Nonnull alertView) {
  1720. [self.tabBarController.navigationController popToRootViewControllerAnimated:YES];
  1721. }];
  1722. }
  1723. }
  1724. BOOL inReconnect = NO;
  1725. - (void)tryAgain
  1726. {
  1727. reConnectAccount++;
  1728. if (reConnectAccount == 4)
  1729. {
  1730. mainBlock(^{
  1731. if (self.isFirstEnterPhone) {
  1732. // 埋点
  1733. HWBuryPointModel *pointModel = [HWBuryPointModel buryPointWithEventType:(EventCodeTypeCloudPhonePushEvent) buttonType:(ClickButtonTypeCloudPhonePushEnter)];
  1734. pointModel.eventResult = @"0";
  1735. [BuryPoint saveHWBuryPointModelListWithModel:pointModel];
  1736. self.isFirstEnterPhone = NO;
  1737. }
  1738. [self exitPlayerView];
  1739. [[iToast makeText:@"云手机连接超时,请稍后再试!"] show];
  1740. });
  1741. return;
  1742. }
  1743. [self reconnectDevice];
  1744. }
  1745. - (void)reconnectDevice{
  1746. globalBlock(^{
  1747. @synchronized (self) {
  1748. inReconnect = YES;
  1749. [self disconnectVideoServer];
  1750. if ([self initVideoServer])
  1751. {
  1752. [self connectVideoServer];
  1753. }
  1754. }
  1755. });
  1756. }
  1757. #pragma mark -- /*视频码率按钮文字、索引、指令 之间相互转换*/
  1758. - (NSUInteger)resolutionBtnIndexToTypeWithIndex:(NSInteger)index
  1759. {
  1760. NSInteger type = BITRATE_EXTREME_SPEED/*BITRATE_STANDARD_CLEARING*/;
  1761. AFNetworkReachabilityStatus netStatus = [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus;
  1762. if (netWorkStates == AFNetworkReachabilityStatusReachableViaWiFi) {
  1763. type = BITRATE_WIFI_STANDARD_CLEARING;
  1764. }
  1765. switch (index)
  1766. {
  1767. case 2:/*极速*/
  1768. type = BITRATE_EXTREME_SPEED;
  1769. break;
  1770. // case 1:/*高速*/
  1771. // type = BITRATE_HIGH_SPEED;
  1772. // break;
  1773. case 0:/*高清*/
  1774. if (netStatus == AFNetworkReachabilityStatusReachableViaWiFi) {
  1775. type = BITRATE_WIFI_HD;
  1776. } else {
  1777. type = BITRATE_WIFI_STANDARD_CLEARING/*BITRATE_HD*/;
  1778. }
  1779. break;
  1780. case 1:/*标清*/
  1781. if (netStatus == AFNetworkReachabilityStatusReachableViaWiFi) {
  1782. type = BITRATE_WIFI_STANDARD_CLEARING;
  1783. } else {
  1784. type = BITRATE_EXTREME_SPEED/*BITRATE_STANDARD_CLEARING*/;
  1785. }
  1786. break;
  1787. case 3:/*自动*/
  1788. type = BITRATE_AUTO;
  1789. break;
  1790. default:
  1791. break;
  1792. }
  1793. return type;
  1794. }
  1795. - (NSInteger)resolutionBtnTypeToIndexWithType:(NSInteger)type
  1796. {
  1797. NSInteger index = 2;
  1798. switch (type)
  1799. {
  1800. case BITRATE_EXTREME_SPEED:
  1801. index = 2;
  1802. break;
  1803. // case BITRATE_HIGH_SPEED:
  1804. // index = 1;
  1805. // break;
  1806. case BITRATE_WIFI_STANDARD_CLEARING/*BITRATE_HD*/:
  1807. index = 0;
  1808. break;
  1809. // case BITRATE_EXTREME_SPEED/*BITRATE_STANDARD_CLEARING*/:
  1810. // index = 1;
  1811. // break;
  1812. case BITRATE_AUTO:
  1813. index = 3;
  1814. break;
  1815. default:
  1816. index = 2;
  1817. break;
  1818. }
  1819. return index;
  1820. }
  1821. - (NSString *)resolutionBtnIndexToStringWithType:(NSInteger)index
  1822. {
  1823. NSString *qualitySelectBtnString = @"标清";
  1824. switch (index)
  1825. {
  1826. case 2:
  1827. qualitySelectBtnString = @"极速";
  1828. break;
  1829. // case 1:
  1830. // qualitySelectBtnString = @"高速";
  1831. // break;
  1832. case 0:
  1833. qualitySelectBtnString = @"高清";
  1834. break;
  1835. case 1:
  1836. qualitySelectBtnString = @"标清";
  1837. break;
  1838. case 4:
  1839. qualitySelectBtnString = @"自动";
  1840. break;
  1841. default:
  1842. break;
  1843. }
  1844. return qualitySelectBtnString;
  1845. }
  1846. #pragma mark -- /*通知添加与移除*/
  1847. - (void)addNSNotification
  1848. {
  1849. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange:) name:NetWorkChangeNotification object:nil];
  1850. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectDisconnect:) name:NotNameByCONNECT_DISCONNECT object:nil];
  1851. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
  1852. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
  1853. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
  1854. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
  1855. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
  1856. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:AVSystemController_SystemVolumeDidChangeNotification object:nil];
  1857. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(extensionRBDGetRbdDeviceNotification:) name:GetRbdDeviceNotification object:nil];
  1858. outputVolumeKVO = YES;
  1859. [[AVAudioSession sharedInstance] addObserver:self forKeyPath:@"outputVolume" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:(void *)[AVAudioSession sharedInstance]];
  1860. // 控制按钮收拾开始、结束
  1861. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerViewControlBtnTouchBegan) name:PlayerViewControlBtnTouchBeganNotification object:nil];
  1862. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerViewControlBtnTouchEnd) name:PlayerViewControlBtnTouchEndNotification object:nil];
  1863. }
  1864. - (void)removeNSNotification
  1865. {
  1866. [[NSNotificationCenter defaultCenter] removeObserver:self name:NetWorkChangeNotification object:nil];
  1867. [[NSNotificationCenter defaultCenter] removeObserver:self name:NotNameByCONNECT_DISCONNECT object:nil];
  1868. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];
  1869. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
  1870. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
  1871. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
  1872. if (outputVolumeKVO)
  1873. {
  1874. [[AVAudioSession sharedInstance] removeObserver:self forKeyPath:@"outputVolume" context:(void *)[AVAudioSession sharedInstance]];
  1875. outputVolumeKVO = NO;
  1876. }
  1877. [[NSNotificationCenter defaultCenter] removeObserver:self];
  1878. }
  1879. #pragma mark -- 音视频数据透传回调
  1880. static double firstTime;
  1881. static double lastTime;
  1882. static int couneeee = 0;
  1883. - (void)videoOutPutH264Data:(void *)h264Data dataLenth:(NSInteger)lenth
  1884. {
  1885. if (couneeee == 0)
  1886. {
  1887. firstTime = [[NSDate date] timeIntervalSince1970];
  1888. }
  1889. couneeee++;
  1890. [self.liveStreamManager rc_sendTransportData:(const char *)h264Data length:(int)lenth dataType:VirtualDataTypeVideo];
  1891. if (couneeee == 100)
  1892. {
  1893. lastTime = [[NSDate date] timeIntervalSince1970];
  1894. HLog(@"\nvideoOutPutH264DataTime --%f",lastTime - firstTime);
  1895. }
  1896. }
  1897. - (void)audioOutPutData:(void *)audioData dataLenth:(NSInteger)lenth
  1898. {
  1899. [self.liveStreamManager rc_sendTransportData:(const char *)audioData length:(int)lenth dataType:VirtualDataTypeAudio];
  1900. }
  1901. -(NSString *)hexStringWithData:(NSData *)data
  1902. {
  1903. const unsigned char* dataBuffer = (const unsigned char*)[data bytes];
  1904. if(!dataBuffer){
  1905. return nil;
  1906. }
  1907. NSUInteger dataLength = [data length];
  1908. NSMutableString* hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
  1909. for(int i = 0; i < dataLength; i++){
  1910. [hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];
  1911. }
  1912. NSString* result = [NSString stringWithString:hexString];
  1913. return result;
  1914. }
  1915. // 板卡切换通知
  1916. - (void)changeCardNotification:(NSNotification *)notification {
  1917. CloudPhoneModel *model = [[UseAccountManage shareInstance].cloudPhoneListModel.diskInfo objectAtIndex:self->phoneIndex];
  1918. NSDictionary *dict = [notification userInfo];
  1919. if ([[dict allKeys] containsObject:@"data"])
  1920. {
  1921. NSDictionary *data = [dict objectForKey:@"data"];
  1922. if ([[data allKeys] containsObject:@"userCardId"]) {
  1923. NSInteger userCardId = [[data objectForKey:@"userCardId"] integerValue];
  1924. if (model.phoneId == userCardId) { // 通知是否是当前设备
  1925. [[iToast makeText:@"设备信息已切换,请重新进入云机"] show];
  1926. [self exitPlayerView];
  1927. }
  1928. }
  1929. }
  1930. }
  1931. - (NSArray *)getAllIvar:(id)object
  1932. {
  1933. NSMutableArray *array = [NSMutableArray array];
  1934. unsigned int count;
  1935. Ivar *ivars = class_copyIvarList([object class], &count);
  1936. for (int i = 0; i < count; i++) {
  1937. Ivar ivar = ivars[i];
  1938. const char *keyChar = ivar_getName(ivar);
  1939. NSString *keyStr = [NSString stringWithCString:keyChar encoding:NSUTF8StringEncoding];
  1940. @try {
  1941. id valueStr = [object valueForKey:keyStr];
  1942. NSDictionary *dic = nil;
  1943. if (valueStr) {
  1944. dic = @{keyStr : valueStr};
  1945. } else {
  1946. dic = @{keyStr : @"值为nil"};
  1947. }
  1948. [array addObject:dic];
  1949. }
  1950. @catch (NSException *exception) {}
  1951. }
  1952. return [array copy];
  1953. }
  1954. //系统声音改变
  1955. #pragma -mark 系统声音改变
  1956. -(void)volumeChanged:(NSNotification *)notification
  1957. {
  1958. if (!self.isNeedShowVoiceBar) { // 不需要展示音量工具条
  1959. return;
  1960. }
  1961. /*音量开关打开时 允许发送指令 否则直接返回*/
  1962. NSDictionary *loginDict = [[UseAccountManage shareInstance] getLoginInfo];
  1963. NSString *account = [loginDict objectForKey:@"account"];
  1964. NSString *key = [account stringByAppendingString:Const_Volume_Info_Dict];
  1965. NSDictionary *volumeDict = [[NSUserDefaults standardUserDefaults] objectForKey:key];
  1966. NSString *volumeStr = [volumeDict safeStringForKey:deviceId];
  1967. if (!volumeStr || ![volumeStr isEqualToString:@"1"]){
  1968. return;
  1969. }
  1970. float volume = [[[notification userInfo] objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
  1971. HLog(@"FlyElephant-系统音量:%f", volume);
  1972. if (volume == 0)
  1973. {
  1974. NSString *dataStr = [RCCommandHelp commandBackOrHomeVolum500WithKeyCode:25];
  1975. [self send_data:dataStr];
  1976. }
  1977. else if (volume == 1)
  1978. {
  1979. NSString *dataStr = [RCCommandHelp commandBackOrHomeVolum500WithKeyCode:24];
  1980. [self send_data:dataStr];
  1981. }
  1982. }
  1983. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
  1984. if(context == (__bridge void *)[AVAudioSession sharedInstance])
  1985. {
  1986. /*音量开关打开时 允许发送指令 否则直接返回*/
  1987. NSDictionary *loginDict = [[UseAccountManage shareInstance] getLoginInfo];
  1988. NSString *account = [loginDict objectForKey:@"account"];
  1989. NSString *key = [account stringByAppendingString:Const_Volume_Info_Dict];
  1990. NSDictionary *volumeDict = [[NSUserDefaults standardUserDefaults] objectForKey:key];
  1991. NSString *volumeStr = [volumeDict safeStringForKey:deviceId];
  1992. if (!volumeStr || ![volumeStr isEqualToString:@"1"]){
  1993. return;
  1994. }
  1995. float newValue = [[change objectForKey:@"new"] floatValue];
  1996. float oldValue = [[change objectForKey:@"old"] floatValue];
  1997. if (newValue > oldValue)
  1998. {
  1999. HLog(@"\n-----音量增加");
  2000. NSString *dataStr = [RCCommandHelp commandBackOrHomeVolum500WithKeyCode:24];
  2001. [self send_data:dataStr];
  2002. }
  2003. else
  2004. {
  2005. HLog(@"\n-----音量降低");
  2006. NSString *dataStr = [RCCommandHelp commandBackOrHomeVolum500WithKeyCode:25];
  2007. [self send_data:dataStr];
  2008. }
  2009. }
  2010. }
  2011. #pragma mark-----------------------------------------------------------------------------------------------
  2012. #pragma mark - 画面渲染、画面调整相关
  2013. #pragma mark - 更新网络标识 以及自动码率
  2014. - (void)updateAutoRateAndNetWorkStates
  2015. {
  2016. int audio_time = 0;
  2017. NSDate *nowTime = [NSDate date];
  2018. NSString *nowTimeStr = [NSString stringWithFormat:@"%f", (double)[nowTime timeIntervalSince1970]];
  2019. NSString *lastTimeStr = [NSString stringWithFormat:@"%f", (double)[lastVideoTome timeIntervalSince1970]];
  2020. audio_time = ([nowTimeStr doubleValue] - [lastTimeStr doubleValue]) * 1000;
  2021. lastVideoTome = nowTime;
  2022. static int frameCount = 200;
  2023. if (0 == frameCount)
  2024. {
  2025. frameCount = 200;
  2026. if (audio_time > 500 && !haveBack && self->netWorkStates != 0)
  2027. {
  2028. self->netWorkStates = 0;
  2029. mainBlock(^{
  2030. [self->mPlayerView.controlBtn setImage:[UIImage imageNamed:@"you_icon"] forState:(UIControlStateNormal)];
  2031. });
  2032. }
  2033. else if (audio_time > 100 && !haveBack && self->netWorkStates != 1)
  2034. {
  2035. self->netWorkStates = 1;
  2036. mainBlock(^{
  2037. [self->mPlayerView.controlBtn setImage:[UIImage imageNamed:@"you_icon"] forState:(UIControlStateNormal)];
  2038. });
  2039. }
  2040. else if (!haveBack && self->netWorkStates != 2)
  2041. {
  2042. self->netWorkStates = 2;
  2043. mainBlock(^{
  2044. [self->mPlayerView.controlBtn setImage:[UIImage imageNamed:@"you_icon"] forState:(UIControlStateNormal)];
  2045. });
  2046. }
  2047. }
  2048. else
  2049. {
  2050. frameCount--;
  2051. }
  2052. if (delayVideo < audio_time)
  2053. {
  2054. delayVideo = audio_time;
  2055. }
  2056. if (delayVideo < 80)
  2057. {
  2058. okdelayVideo++;
  2059. }
  2060. else
  2061. {
  2062. okdelayVideo = 0;
  2063. }
  2064. if (delayVideo > 120)
  2065. {
  2066. baddelayVideo++;
  2067. }
  2068. havedelayVideoCount++;
  2069. if (havedelayVideoCount % 1000 == 0)
  2070. {
  2071. baddelayVideo = 0;
  2072. }
  2073. if (!haveBack && self->rate == 3) {
  2074. if (baddelayVideo > 10)
  2075. {
  2076. /*降低码率*/
  2077. if (self->rateForAuto == BITRATE_WIFI_STANDARD_CLEARING/*BITRATE_HD*/ || self->rateForAuto == BITRATE_WIFI_HD)
  2078. {
  2079. if (self->rateForAuto == BITRATE_WIFI_STANDARD_CLEARING/*BITRATE_HD*/) {
  2080. self->rateForAuto = BITRATE_EXTREME_SPEED/*BITRATE_STANDARD_CLEARING*/;
  2081. } else {
  2082. self->rateForAuto = BITRATE_WIFI_STANDARD_CLEARING;
  2083. }
  2084. }
  2085. else if (self->rateForAuto == BITRATE_EXTREME_SPEED/*BITRATE_STANDARD_CLEARING*/ || self->rateForAuto == BITRATE_WIFI_STANDARD_CLEARING)
  2086. {
  2087. self->rateForAuto = BITRATE_EXTREME_SPEED;
  2088. }
  2089. // else if (mySelf->rateForAuto == BITRATE_HIGH_SPEED)
  2090. // {
  2091. // mySelf->rateForAuto = BITRATE_EXTREME_SPEED;
  2092. // NSString *dataStr = [RCCommandHelp commondResolution500WithType:mySelf->rateForAuto];
  2093. // int result = send_data(mySelf->_ihandle, [dataStr UTF8String], (int)dataStr.length, 10.f);
  2094. // }
  2095. NSString *dataStr = [RCCommandHelp commandResolution500WithType:self->rateForAuto];
  2096. // send_data(mySelf->_ihandle, [dataStr UTF8String], (int)dataStr.length, 10.f);
  2097. [self.liveStreamManager rc_sendJsonData:[dataStr UTF8String]];
  2098. baddelayVideo = 0;
  2099. }
  2100. if (okdelayVideo > 1000)
  2101. {
  2102. /*上升码率*/
  2103. if (self->rateForAuto == BITRATE_EXTREME_SPEED/*BITRATE_STANDARD_CLEARING*/ || self->rateForAuto == BITRATE_WIFI_STANDARD_CLEARING)
  2104. {
  2105. if (self->rateForAuto == BITRATE_EXTREME_SPEED/*BITRATE_STANDARD_CLEARING*/) {
  2106. self->rateForAuto = BITRATE_WIFI_STANDARD_CLEARING/*BITRATE_HD*/;
  2107. } else {
  2108. self->rateForAuto = BITRATE_WIFI_HD;
  2109. }
  2110. }
  2111. // else if (mySelf->rateForAuto == BITRATE_HIGH_SPEED)
  2112. // {
  2113. // mySelf->rateForAuto = BITRATE_EXTREME_SPEED/*BITRATE_STANDARD_CLEARING*/;
  2114. // NSString *dataStr = [RCCommandHelp commondResolution500WithType:mySelf->rateForAuto];
  2115. // int result = send_data(mySelf->_ihandle, [dataStr UTF8String], (int)dataStr.length, 10.f);
  2116. // }
  2117. else if (self->rateForAuto == BITRATE_EXTREME_SPEED)
  2118. {
  2119. if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWiFi) {
  2120. self->rateForAuto = BITRATE_WIFI_STANDARD_CLEARING;
  2121. } else {
  2122. self->rateForAuto = BITRATE_EXTREME_SPEED/*BITRATE_STANDARD_CLEARING*/;
  2123. }
  2124. }
  2125. NSString *dataStr = [RCCommandHelp commandResolution500WithType:self->rateForAuto];
  2126. // send_data(mySelf->_ihandle, [dataStr UTF8String], (int)dataStr.length, 10.f);
  2127. [self.liveStreamManager rc_sendJsonData:[dataStr UTF8String]];
  2128. okdelayVideo = 0;
  2129. }
  2130. }
  2131. }
  2132. #pragma mark - opengl渲染
  2133. - (void)showImageOpenglWithCIImage:(CIImage *)ciImage
  2134. {
  2135. // CGFloat tempRate = ciImage.extent.size.width/ciImage.extent.size.height;
  2136. // if (w_h_rate != tempRate)
  2137. // {
  2138. //// ksharedAppDelegate.couldPhone_W_PHONE = ciImage.extent.size.width;
  2139. //// ksharedAppDelegate.couldPhone_H_PHONE = ciImage.extent.size.height;
  2140. //
  2141. // w_h_rate = tempRate;
  2142. // mainBlock(^{
  2143. // [self updatePlayViewRate:tempRate];
  2144. // });
  2145. // }
  2146. [mPlayerView glkShowImage:ciImage];
  2147. }
  2148. #pragma mark - UIImageView渲染
  2149. - (void)showImageUIImageViewWithCIImage:(CIImage *)ciImage pixelBufferRef:(CVPixelBufferRef)pixelBuff1
  2150. {
  2151. CGImageRef videoImage = [videoContext createCGImage:ciImage fromRect:CGRectMake(0, 0, CVPixelBufferGetWidth(pixelBuff1), CVPixelBufferGetHeight(pixelBuff1))];
  2152. UIImage *image = [UIImage imageWithCGImage:videoImage];
  2153. CGFloat tempRate = image.size.width/image.size.height;
  2154. mainBlock(^{
  2155. if (image)
  2156. {
  2157. // if (w_h_rate != tempRate)
  2158. // {
  2159. //// ksharedAppDelegate.couldPhone_W_PHONE = ciImage.extent.size.width;
  2160. //// ksharedAppDelegate.couldPhone_H_PHONE = ciImage.extent.size.height;
  2161. //
  2162. // [self updatePlayViewRate:tempRate];
  2163. // }
  2164. self->mPlayerView.showImageView.image = image;
  2165. }
  2166. });
  2167. CGImageRelease(videoImage);
  2168. }
  2169. #pragma mark -- /*画面布局调整*/
  2170. - (void)updatePlayViewRate:(CGFloat)tempRate
  2171. {
  2172. /*设置横竖屏*/
  2173. if (tempRate > 1) {/*横屏*/
  2174. [self setDeviceOrientation:(UIDeviceOrientationLandscapeRight)];
  2175. // [self saveCIImageAction:self->deviceId];
  2176. [mPlayerView setLanMas_makeWithImageRate:tempRate isOpenVirtual:[deviceModel isOpenVirtualKey] isUsageMode:[deviceModel isOpenUsageMode]];
  2177. }else{/*竖屏*/
  2178. [self setDeviceOrientation:(UIDeviceOrientationPortrait)];
  2179. // [self saveCIImageAction:self->deviceId];
  2180. [mPlayerView setPoMas_makeWithImageRate:tempRate isOpenVirtual:[deviceModel isOpenVirtualKey] isUsageMode:[deviceModel isOpenUsageMode]];
  2181. }
  2182. }
  2183. #pragma mark------------------------------------------------------------------------------------------------------------------
  2184. #pragma mark-提示 loading 相关
  2185. #pragma mark-添加带有文字loading
  2186. - (void)addLoadingWithTipsView:(NSString *)tipsStr
  2187. {
  2188. if (loadingWithTipsView)
  2189. {
  2190. [loadingWithTipsView removeFromSuperview];
  2191. loadingWithTipsView = nil;
  2192. }
  2193. loadingWithTipsView = [self loadingViewWithTipsStr:tipsStr];
  2194. [self.view addSubview:loadingWithTipsView];
  2195. }
  2196. - (void)removeLoadingWithTipsView
  2197. {
  2198. if (loadingWithTipsView)
  2199. {
  2200. [loadingWithTipsView removeFromSuperview];
  2201. loadingWithTipsView = nil;
  2202. }
  2203. }
  2204. - (UIView *)loadingViewWithTipsStr:(NSString*)tipsStr
  2205. {
  2206. UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
  2207. [bgView setBackgroundColor:RGBACOLOR(0, 0, 0, 0.5)];
  2208. FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"loadingWhiteGif" withExtension:@"gif"]]];
  2209. FLAnimatedImageView *upView = [[FLAnimatedImageView alloc] init];
  2210. [upView setFrame:CGRectMake((SCREEN_W - 65.f)/2.f, (SCREEN_H - 65.f)/2.f, 65.f, 65.f)];
  2211. [upView setContentMode:UIViewContentModeScaleAspectFill];
  2212. [upView setBackgroundColor:[ UIColor clearColor]];
  2213. upView.animatedImage = image;
  2214. [bgView addSubview:upView];
  2215. /*标签文字*/
  2216. UILabel *tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_W - 100.f)/2.f, (SCREEN_H - 65.f)/2.f + 65.f + 10.f, 100.f*AUTOSCALE, 20.f)];
  2217. [tipsLabel setText:tipsStr];
  2218. [tipsLabel setBackgroundColor:[UIColor clearColor]];
  2219. [tipsLabel setTextColor:[UIColor whiteColor]];
  2220. [tipsLabel setFont:[UIFont systemFontOfSize:14.f]];
  2221. [tipsLabel setTextAlignment:(NSTextAlignmentCenter)];
  2222. [bgView addSubview:tipsLabel];
  2223. return bgView;
  2224. }
  2225. #pragma mark-添加loading
  2226. -(void)showNewIndicator
  2227. {
  2228. dispatch_async(dispatch_get_main_queue(), ^{
  2229. if (self->wattingView == nil)
  2230. {
  2231. HLog(@"Tan +++++开");
  2232. self->wattingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
  2233. [self->wattingView setBackgroundColor:RGBACOLOR(0.f, 0.f, 0.f, 0.7)];
  2234. [self->wattingView.layer setCornerRadius:10.f];
  2235. self->wattingView.center = [[UIApplication sharedApplication] keyWindow].center;// 将wattingVideoImageindicator放于播放画面的中央位置
  2236. self->wattingView.hidden = NO;
  2237. FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"loadingWhiteGif" withExtension:@"gif"]]];
  2238. FLAnimatedImageView *upView = [[FLAnimatedImageView alloc] init];
  2239. [upView setFrame:CGRectMake((100 - 65.f)/2.f, (100 - 65.f)/2.f, 65.f, 65.f)];
  2240. [upView setContentMode:UIViewContentModeScaleAspectFill];
  2241. [upView setBackgroundColor:[ UIColor clearColor]];
  2242. upView.animatedImage = image;
  2243. [self->wattingView addSubview:upView];
  2244. [self->mPlayerView addSubview:self->wattingView];
  2245. }
  2246. });
  2247. }
  2248. -(void)removeNewIndicator
  2249. {
  2250. if (self->wattingView != nil)
  2251. {
  2252. [[self->wattingView viewWithTag:1111].layer removeAllAnimations];
  2253. self->wattingView.hidden = YES;
  2254. [self->wattingView removeFromSuperview];
  2255. self->wattingView = nil;
  2256. }
  2257. }
  2258. #pragma mark 定时器
  2259. - (void)setTimerCountDown {
  2260. HLog(@"开启一个");
  2261. if (_timer) {
  2262. // 取消定时器
  2263. [self cancelTimer];
  2264. }
  2265. // 初始化值计时数据
  2266. self.adjustTime = 3;
  2267. self.concentTime = [iTools getNowTimeStamp];
  2268. self.currentTime = 0;
  2269. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerChange) userInfo:nil repeats:YES];
  2270. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  2271. }
  2272. - (void)cancelTimer {
  2273. // 取消定时器
  2274. [_timer invalidate];
  2275. _timer = nil;
  2276. }
  2277. - (void)timerChange {
  2278. self.currentTime++;// 0、定时器计数
  2279. [self extensionAdjustBtnFrameCheckAdjustTime];// 1、悬浮球3s后 自动靠边隐藏
  2280. // [self checkConcentTime];// 3、30s之后 没有收到拉流数据 自动断开链接
  2281. [self checkCloudPhoneIsV];// 4、60s 检测一次 云手机是否过期
  2282. }
  2283. // 3、30s之后 没有收到拉流数据 自动断开链接
  2284. - (void)checkConcentTime {
  2285. NSInteger now = [iTools getNowTimeStamp];
  2286. NSInteger timeDiff = now - self.concentTime; // 渲染时间与当前时间 时间差
  2287. if (self.concentTime == 0) { //云手机未连接成功
  2288. HLog(@"云手机未连接成功");
  2289. }else if (timeDiff >= 30) { // 30s 无渲染数据
  2290. HLog(@"云手机30s 无渲染数据 自动断开连接");
  2291. [[iToast makeText:@"云手机连接超时,请稍后再试!"] show];
  2292. [self exitPlayerView];
  2293. }else {
  2294. // HLog(@"云手机渲染数据时间差:%zd", timeDiff);
  2295. }
  2296. }
  2297. // 4、60s 检测一次 云手机是否过期
  2298. - (void)checkCloudPhoneIsV {
  2299. if (self.currentTime % 60 == 0) { // 每60s检测一次
  2300. //判断云手机是否已过期
  2301. [self judgeDeviceVaildTimeIsExpire];
  2302. }
  2303. }
  2304. //判断云手机是否已过期
  2305. - (void)judgeDeviceVaildTimeIsExpire
  2306. {
  2307. if ([UseAccountManage shareInstance].cloudPhoneListModel.diskInfo && [[UseAccountManage shareInstance].cloudPhoneListModel.diskInfo count] > phoneIndex)
  2308. {
  2309. /*倒计时*/
  2310. CloudPhoneModel *deviceDcit = [[UseAccountManage shareInstance].cloudPhoneListModel.diskInfo objectAtIndex:self->phoneIndex];
  2311. int timeMin = (int)deviceDcit.validTime;
  2312. double nowDataTime = [[NSDate date] timeIntervalSince1970];
  2313. timeMin = timeMin - (nowDataTime - deviceDcit.getDeviceTime)/60;
  2314. if (deviceDcit.userCardType == 1 || deviceDcit.userCardType == 2 || deviceDcit.userCardType == 3) {
  2315. timeMin = (int)deviceDcit.validTime;
  2316. }
  2317. if (0 >= timeMin) //已到期
  2318. {
  2319. // 通知代理云手机已过期
  2320. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  2321. if ([self->_delegate respondsToSelector:@selector(playerViewControllerBackWithPhoneIndex:)]) {
  2322. [self->_delegate playerViewControllerBackWithPhoneIndex:self->phoneIndex];
  2323. }
  2324. });
  2325. [self exitPlayerView];
  2326. }
  2327. }
  2328. }
  2329. #pragma mark 云机摄像头使用记录上报
  2330. - (void)postCloudCamera {
  2331. HLog(@"\n----摄像头---调用上报----");
  2332. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  2333. [params setObject:@"PHONE_CAMERA" forKey:@"code"];
  2334. [params setObject:deviceId forKey:@"userCardId"];
  2335. [[UseAccountManage shareInstance] CommonPostCallBackCode:PostServiceUseAPI Parameters:params success:^(id _Nonnull responseObject) {
  2336. if (CODE == 0) {
  2337. }
  2338. } failure:^(NSError * _Nonnull error) {
  2339. HLog(@"%@",error);
  2340. }];
  2341. }
  2342. #pragma mark - 提示框
  2343. - (void)showAlertWithTitle:(NSAttributedString*_Nullable)atbTitle
  2344. message:(NSAttributedString*_Nullable)atbmessage
  2345. tilteAlignment:(NSTextAlignment)tilteAlignment
  2346. messageAlignment:(NSTextAlignment)messageAlignment
  2347. sureAction:(UIAlertAction*_Nullable)sureAction
  2348. cancelAction:(UIAlertAction*_Nullable)cancelAction
  2349. {
  2350. // 当前控制器是UIAlertController 退出
  2351. UIViewController *currentVC = [[ShortcutManager shareInstance] findCurrentShowingViewController];
  2352. if ([currentVC isKindOfClass:[UIAlertController class]]) {
  2353. //获得极致体验服务 请购买云手机(不退出 优先级最高)
  2354. UIAlertController *alertVC = (UIAlertController *)currentVC;
  2355. NSMutableAttributedString *attriMsg = [alertVC valueForKey:@"_attributedMessage"];
  2356. if ([attriMsg.string containsString:@"获得极致体验服务"]) {
  2357. return;
  2358. }
  2359. HLog(@"%@",attriMsg.string);
  2360. [currentVC dismissViewControllerAnimated:NO completion:nil];
  2361. }
  2362. self.alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
  2363. if (atbTitle)
  2364. {
  2365. [self.alertController setValue:atbTitle forKey:@"_attributedTitle"];
  2366. }
  2367. if (atbmessage)
  2368. {
  2369. [self.alertController setValue:atbmessage forKey:@"_attributedMessage"];
  2370. }
  2371. UIView *subView1 = self.alertController.view.subviews[0];
  2372. if (subView1 && [subView1 subviews])
  2373. {
  2374. UIView *subView2 = subView1.subviews[0];
  2375. if (subView2 && [subView2 subviews])
  2376. {
  2377. UIView *subView3 = subView2.subviews[0];
  2378. if (subView3 && [subView3 subviews])
  2379. {
  2380. UIView *subView4 = subView3.subviews[0];
  2381. if (subView4 && [subView4 subviews])
  2382. {
  2383. UIView *subView5 = subView4.subviews[0];
  2384. if (subView5 && [[subView5 subviews] count] > 1)
  2385. {
  2386. for (UIView *msg in subView5.subviews)
  2387. {
  2388. if ([msg isKindOfClass:[UILabel class]])
  2389. {
  2390. NSArray *trmp = [self getAllIvar:msg];
  2391. for (NSDictionary *dict in trmp)
  2392. {
  2393. if ([[dict allKeys] containsObject:@"_content"])
  2394. {
  2395. NSAttributedString *dsfds = [dict objectForKey:@"_content"];
  2396. if ([atbTitle.string isEqualToString:dsfds.string])
  2397. {
  2398. [(UILabel*)msg setTextAlignment:(tilteAlignment)];
  2399. }
  2400. else if ([atbmessage.string isEqualToString:dsfds.string])
  2401. {
  2402. if (((UILabel*)msg).numberOfLines == 0)
  2403. {
  2404. [(UILabel*)msg setTextAlignment:(messageAlignment)];
  2405. }
  2406. else if (((UILabel*)msg).numberOfLines == 1)
  2407. {
  2408. [(UILabel*)msg setTextAlignment:NSTextAlignmentCenter];
  2409. }
  2410. else
  2411. {
  2412. [(UILabel*)msg setTextAlignment:(NSTextAlignmentCenter)];
  2413. }
  2414. }
  2415. }
  2416. }
  2417. }
  2418. }
  2419. }
  2420. }
  2421. }
  2422. }
  2423. }
  2424. if (sureAction)
  2425. {
  2426. [self.alertController addAction:sureAction];
  2427. }
  2428. if (cancelAction)
  2429. {
  2430. [self.alertController addAction:cancelAction];
  2431. }
  2432. // 弹窗边框
  2433. UIView *subview = self.alertController.view.subviews.firstObject;
  2434. UIView *alertContentView = subview.subviews.firstObject;
  2435. alertContentView.layer.cornerRadius = 10;
  2436. // alertContentView.layer.masksToBounds = YES;
  2437. alertContentView.layer.shadowColor = [UIColor colorWithRed:30/255.0 green:32/255.0 blue:34/255.0 alpha:1.0].CGColor;
  2438. alertContentView.layer.shadowOffset = CGSizeMake(0,0);
  2439. alertContentView.layer.shadowOpacity = 1;
  2440. alertContentView.layer.shadowRadius = 7;
  2441. // 弹窗背景颜色
  2442. if (@available(iOS 13.0, *)) {
  2443. self.alertController.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;
  2444. }
  2445. [self presentViewController:self.alertController animated:YES completion:nil];
  2446. }
  2447. #pragma mark - 屏幕旋转相关
  2448. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  2449. return UIInterfaceOrientationPortrait;
  2450. }
  2451. - (BOOL)shouldAutorotate {
  2452. if (canRate)/*画面发现变化 状态变为YES*/
  2453. {
  2454. canRate = NO;/*关键点 只能旋转一次*/
  2455. return YES;
  2456. }
  2457. //是否允许转屏
  2458. return NO;/*平时禁止*/
  2459. }
  2460. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  2461. return
  2462. UIInterfaceOrientationMaskPortrait |
  2463. UIInterfaceOrientationMaskLandscapeLeft |
  2464. UIInterfaceOrientationMaskLandscapeRight;
  2465. }
  2466. #pragma mark -- /* 屏幕旋转通知接收函数*/
  2467. - (void)orientationChanged:(NSNotification *)notification
  2468. {
  2469. KyoLog(@"rotated");
  2470. }
  2471. - (void)setDeviceOrientation:(UIDeviceOrientation)orientationPortrait
  2472. {
  2473. mainBlock(^{
  2474. UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  2475. //isPortraitMode
  2476. BOOL ScreenMode = ((UIDeviceOrientation)orientation == orientationPortrait);
  2477. NSNumber *value = nil;
  2478. if (ScreenMode)
  2479. {
  2480. }
  2481. else
  2482. {
  2483. self.adjustTime = 3;
  2484. if (orientationPortrait == UIDeviceOrientationLandscapeLeft){
  2485. [self->mPlayerView setLanMas_makeWithImageRate:16.0/9.0 isOpenVirtual:[self->deviceModel isOpenVirtualKey] isUsageMode:[self->deviceModel isOpenUsageMode]];
  2486. // [self.basebgView mas_remakeConstraints:^(MASConstraintMaker *make) {
  2487. // make.left.mas_equalTo(0);
  2488. // make.right.mas_equalTo(0);
  2489. // make.top.mas_equalTo(0);
  2490. // make.bottom.mas_equalTo(0);
  2491. // }];
  2492. /*悬浮按钮旋转*/
  2493. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  2494. [self reSetPlayerViewFloatBtnFrame];
  2495. });
  2496. [self->mPlayerView.controlBtn setHidden:NO];
  2497. }else{
  2498. [self->mPlayerView setPoMas_makeWithImageRate:9.0/16.0 isOpenVirtual:[self->deviceModel isOpenVirtualKey] isUsageMode:[self->deviceModel isOpenUsageMode]];
  2499. // [self.basebgView mas_remakeConstraints:^(MASConstraintMaker *make) {
  2500. // make.left.mas_equalTo(0);
  2501. // make.right.mas_equalTo(0);
  2502. // make.top.mas_equalTo(0);
  2503. // make.bottom.mas_equalTo(0);
  2504. // }];
  2505. }
  2506. self->canRate = YES;/*关键点 设置临时允许*/
  2507. value = [NSNumber numberWithInteger:orientationPortrait];//横竖屏值设置
  2508. [[UIDevice currentDevice] setValue:value forKey:@"orientation"];/*先将状态栏旋转(影响系统通知弹框方向)*/
  2509. [[NSNotificationCenter defaultCenter] postNotificationName:UIDeviceOrientationDidChangeNotification object:nil];/*发送通知*/
  2510. }
  2511. });
  2512. }
  2513. /*上传用户设置的分辨率*/
  2514. - (void)uploadPhoneReslotingsWithWidth:(NSInteger)width high:(NSInteger)high userCardId:(NSInteger)userCardId dpi:(NSInteger)dpi resolvingPowerId:(NSInteger)resolvingPowerId{
  2515. NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
  2516. [params setObject:[NSNumber numberWithInteger:userCardId] forKey:@"userCardId"];
  2517. [params setObject:[NSNumber numberWithInteger:width] forKey:@"width"];
  2518. [params setObject:[NSNumber numberWithInteger:high] forKey:@"high"];
  2519. [params setObject:[NSNumber numberWithInteger:dpi] forKey:@"dpi"];
  2520. [params setObject:[NSNumber numberWithInteger:resolvingPowerId] forKey:@"resolvingPowerId"];
  2521. [[UseAccountManage shareInstance] CommonPostCallBackCode:PostUploadPhoneResolvingsAPI
  2522. Parameters:params
  2523. success:^(id _Nonnull responseObject) {
  2524. if (CODE != 0) {
  2525. [[UseAccountManage shareInstance] CommonPostCallBackCode:PostUploadPhoneResolvingsAPI
  2526. Parameters:params
  2527. success:^(id _Nonnull responseObject) {
  2528. }
  2529. failure:^(NSError * _Nonnull error) {
  2530. }];
  2531. }
  2532. }
  2533. failure:^(NSError * _Nonnull error) {
  2534. [[UseAccountManage shareInstance] CommonPostCallBackCode:PostUploadPhoneResolvingsAPI
  2535. Parameters:params
  2536. success:^(id _Nonnull responseObject) {
  2537. }
  2538. failure:^(NSError * _Nonnull error) {
  2539. }];
  2540. }];
  2541. // PostUploadPhoneResolvingsAPI
  2542. }
  2543. /*设置设备的分辨率*/
  2544. - (void)setPhoneReslotingsInWebsocketWidth:(NSInteger)width high:(NSInteger)high dpi:(NSInteger)dpi{
  2545. HLog(@"\n---设置设备的分辨率--width:%ld---high:%ld---dpi:%ld-",width,high,dpi);
  2546. [self.commandChannelManager rc_sendData:[RCCommandHelp setPhoneSizecommandWithWidth:width
  2547. high:high
  2548. dpi:dpi]];
  2549. }
  2550. /*切换连接方式*/
  2551. - (void)connectTypeChange{
  2552. [self reconnectDevice];
  2553. }
  2554. - (void)cloudPhonechangeVPNNotification:(NSNotification *)notification{
  2555. if (deviceModel.vpnMode!=0) {
  2556. return;
  2557. }else{
  2558. NSDictionary *dict = [notification userInfo];
  2559. if ([[dict allKeys] containsObject:@"data"])
  2560. {
  2561. NSDictionary *data = [dict objectForKey:@"data"];
  2562. if ([[data allKeys] containsObject:@"userCardId"]) {
  2563. NSInteger userCardId = [[data objectForKey:@"userCardId"] integerValue];
  2564. if (deviceModel.phoneId == userCardId) { // 通知是否是当前设备
  2565. if ([[data allKeys] containsObject:@"vpnStatus"])
  2566. {
  2567. NSInteger tempvpnStatus = [[data objectForKey:@"vpnStatus"] integerValue];
  2568. if (tempvpnStatus != self.vpnStatus) {
  2569. self.vpnStatus = tempvpnStatus;
  2570. [self reconnectDevice];
  2571. }
  2572. }
  2573. }
  2574. }
  2575. }
  2576. }
  2577. }
  2578. #pragma mark - UITextFieldDelegate 透传输入法委托
  2579. //- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  2580. //
  2581. // /*长度为0时表示删除内容*/
  2582. // if (string.length > 0){
  2583. // NSString *replaceStr = [textField.text substringWithRange:range];
  2584. // HLog(@"\n000000要增加的文字:--%@-------",replaceStr);
  2585. // replaceStr = [replaceStr stringByReplacingOccurrencesOfString:@" " withString:@""];
  2586. // HLog(@"\n111111要增加的文字:--%@-------",replaceStr);
  2587. // for (NSInteger nFori = 0; nFori < replaceStr.length; nFori++) {
  2588. // NSString *dataStr = [RCCommandHelp commandKeyboadInputStr:nil];
  2589. // [self.businessCommandChannelManager rc_sendData:dataStr];
  2590. // }
  2591. // HLog(@"\n-----输入内容:--%@-------",string);
  2592. // NSString *dataStr = [RCCommandHelp commandKeyboadInputStr:string];
  2593. // [self.businessCommandChannelManager rc_sendData:dataStr];
  2594. // }
  2595. // else{
  2596. // HLog(@"\n-----删除:--%ld-------",range.length);
  2597. // NSInteger needDeleteLength = range.length;
  2598. // for (NSInteger nFori = 0; nFori < needDeleteLength; nFori++) {
  2599. // NSString *dataStr = [RCCommandHelp commandKeyboadInputStr:nil];
  2600. // [self.businessCommandChannelManager rc_sendData:dataStr];
  2601. //
  2602. // /*初始内容无法删除问题处理*/
  2603. // hidenTextField.text = @"1234";
  2604. // hidenTextFieldOldStr = @"1234";
  2605. //
  2606. // /*如果两者内容差距较大 是否会有删除后出现两者内容不一样的情况*/
  2607. // if (![hidenTextField.text isEqualToString:hidenTextFieldOldStr]){
  2608. // /*两者不相等时需要做等值处理*/
  2609. // hidenTextFieldOldStr = hidenTextField.text;
  2610. // }
  2611. // }
  2612. // }
  2613. //
  2614. // return YES;
  2615. //}
  2616. - (void)textFieldDidEndEditing:(UITextField *)textField{
  2617. HLog(@"\n-----输入法透传 1111111 关闭键盘-------");
  2618. NSString *dataStr = [RCCommandHelp commandClosePhoneKeyboad];
  2619. [self.businessCommandChannelManager rc_sendData:dataStr];
  2620. }
  2621. -(BOOL)prefersHomeIndicatorAutoHidden {
  2622. return YES;
  2623. }
  2624. @end