webRtcManager.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. //
  2. // webRtcManager.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/9/5.
  6. //
  7. #import "webRtcManager.h"
  8. #import "webRtcManager+StatisticsReport.h"
  9. #import "RcGameWQKeyChain.h"
  10. #import "errorAlertTool.h"
  11. #import "webRtcManager+downloadNasFile.h"
  12. @interface webRtcManager ()<MediaStreamClientEventsDelegate>
  13. {
  14. NSMutableArray *commandSendCheckArr;//需要检测任务是否发出的的指令
  15. NSTimer *linkCheckSecondTimer; // 检测链接状态
  16. NSInteger tryRelinkNum;//重连次数
  17. }
  18. //第一次链接设备 要发送指令信息 实现单点登录
  19. @property (nonatomic, assign)BOOL didSendfristMsg;
  20. @end
  21. @implementation webRtcManager
  22. + (instancetype)shareManager {
  23. static webRtcManager *_instance;
  24. static dispatch_once_t onceToken;
  25. dispatch_once(&onceToken, ^{
  26. _instance = [[self alloc] init];
  27. });
  28. return _instance;
  29. }
  30. - (instancetype)init {
  31. if (self = [super init]) {
  32. //[self registeNotification];
  33. _mediaStream = [[RTC_OBJC_TYPE(AMediaStream) alloc] initWithFrame:CGRectZero];
  34. [_mediaStream setEventDelegate:self];
  35. linkCheckSecondTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerChange) userInfo:nil repeats:YES];
  36. [[NSRunLoop currentRunLoop] addTimer:linkCheckSecondTimer forMode:NSRunLoopCommonModes];
  37. }
  38. return self;
  39. }
  40. #pragma mark 十秒一次的timer检测
  41. - (void)timerChange{
  42. [self checkChannelLinkStateFun];
  43. }
  44. #pragma mark 检测链接是否OK
  45. - (void)checkChannelLinkStateFun
  46. {
  47. if(_channelState != RTCDataChannelStateOpen
  48. ||(_ConnectionState != RTCIceConnectionStateConnected
  49. && _ConnectionState != RTCIceConnectionStateCompleted)
  50. ){
  51. if(!_didReportWebRtcType){//还没上报过通道链接情况
  52. tryRelinkNum ++;
  53. if(tryRelinkNum == 2){
  54. _didReportWebRtcType = YES;
  55. [self reportWebRtcRePoportTypeIsChannel:YES withStats:nil];
  56. }
  57. }
  58. [self relinkWebRtcFun];
  59. }
  60. }
  61. #pragma mark 关闭链接
  62. - (void)closeLinkWebRtcFun
  63. {
  64. if((_ConnectionState == RTCIceConnectionStateConnected
  65. && _ConnectionState == RTCIceConnectionStateCompleted)
  66. || _channelState == RTCDataChannelStateOpen)
  67. {
  68. KWeakSelf
  69. mainBlock(^{
  70. [weakSelf.mediaStream disconnect];
  71. });
  72. }
  73. }
  74. #pragma mark 开始链接
  75. - (void)beginToLinkWebRtcFun
  76. {
  77. KWeakSelf
  78. mainBlock(^{
  79. [weakSelf secondBeginToLinkWebRtcFun];
  80. });
  81. }
  82. - (void)secondBeginToLinkWebRtcFun
  83. {
  84. if(ksharedAppDelegate.isWebSockLinkOKAginType
  85. || !ksharedAppDelegate.DeviceWebRtcMsgMod){
  86. return;
  87. }
  88. webRtcMsgModel * _webRtcMsgMod = ksharedAppDelegate.DeviceWebRtcMsgMod;
  89. //链接用
  90. NSString *signallingUrl = [[NSString alloc] initWithFormat:@"%@:%@",_webRtcMsgMod.data.signalling.domainName,_webRtcMsgMod.data.signalling.port];
  91. NSURL *url = [NSURL URLWithString:signallingUrl];
  92. //ice用
  93. NSString *iceUrl = [[NSString alloc] initWithFormat:@"%@:%@",_webRtcMsgMod.data.turn.domainName,_webRtcMsgMod.data.turn.port];
  94. NSMutableDictionary *ice = [NSMutableDictionary new];
  95. if(iceUrl){
  96. [ice setValue:iceUrl forKey:@"CHINANET"];
  97. [ice setValue:iceUrl forKey:@"CMNET"];
  98. [ice setValue:iceUrl forKey:@"UNICOM"];
  99. }
  100. NSString *roomName = _webRtcMsgMod.data.uniqueIdentifier;
  101. NSInteger result = [_mediaStream startUploadChannel:url ice:ice sn:roomName token:@"vclusters"];
  102. HLog(@"webrtc 发起连接 result:%ld",result)
  103. [_mediaStream setShouldGetStats:YES];
  104. }
  105. - (void)relinkWebRtcFun{
  106. ksharedAppDelegate.isWebSockLinkOKAginType = NO;
  107. if(!_isChangeBoxType){
  108. [self beginToLinkWebRtcFun];
  109. }
  110. }
  111. #pragma mark webrtc P2P通道发送消息
  112. - (void)send_data:(NSString *)dataStr
  113. {
  114. [_mediaStream sendData:dataStr];
  115. HLog(@"客户端发出命令:%@",dataStr);
  116. }
  117. #pragma mark 盒子链接成功后需要处理的各种各样事情
  118. - (void)handlAllMsgAfterDidLinkFun
  119. {
  120. _isRebootIngType = NO;
  121. _isResetingType = NO;
  122. _isChangeBoxType = NO;
  123. ksharedAppDelegate.isWebSockLinkOKAginType = YES;
  124. //获取云机尺寸 兼容 720*1080 &1080*1920 不同分辨率的展示和触控
  125. NSString *getPhoneSizeStr = [RCCommandHelp getPhoneSizecommand];
  126. [self send_data:getPhoneSizeStr];
  127. //判断是否为需要改机
  128. BOOL isNeedRandomChangeParams = [HWDataManager getBoolWithKey:Const_need_random_Change_Params];
  129. if(isNeedRandomChangeParams)
  130. {
  131. NSString *commondStr = @"{\"type\":\"randomChangeParams\"}";
  132. [self send_data:commondStr];
  133. [HWDataManager setBoolWithKey:Const_need_random_Change_Params value:NO];
  134. }
  135. [self fristConnectNeedGiveAMsgFun];
  136. [self updateCopydata];
  137. // 报链接失败 后面又连接上了
  138. [[errorAlertTool shareInstance] dismissErrorAlertFun];
  139. //处理相册备份
  140. [[nasBackupsManager shareInstance] AutohandlePhotosBackupsFun];
  141. //获取磁盘外挂
  142. [self getExtraFilesListFun];
  143. [self getBaseInfoFun];
  144. [self getTvStatusFun];
  145. }
  146. #pragma mark 盒子第一次连接成功 给ws发送信息 单点登录 把其他账号挤下去
  147. - (void)fristConnectNeedGiveAMsgFun{
  148. if(!_didSendfristMsg )
  149. {
  150. [self getSysInfoFun];
  151. // NSString *curOaidStr = [RcGameWQKeyChain getOaidStringFun];
  152. // if(!curOaidStr){
  153. // curOaidStr = @"";
  154. // }
  155. // //未调通挤下线
  156. // NSString *commondStr = [[NSString alloc] initWithFormat:@"{\"type\":\"login\",\"value\":\"%@\"}",curOaidStr];
  157. // [self send_data:commondStr];
  158. // [self addCommandSendTaskFunWithType:@"offline_notification" WithCommandStr:commondStr];
  159. _didSendfristMsg = YES;
  160. [self getPreferredLanguage];
  161. NSString *commondStr2 = @"{\"type\":\"TvStatus\"}";
  162. [self send_data:commondStr2];
  163. }
  164. }
  165. #pragma mark 语言和市区 同步云机
  166. -(void)getPreferredLanguage
  167. {//{"data":{"language":"zh-CN","timeZone":"Etc/GMT+8"},"type":"setLanguages"}
  168. // iOS 获取设备当前语言的代码
  169. NSString *preferredLanguage = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
  170. HLog(@"当前语言:%@", preferredLanguage);
  171. //en-US 英文 ja-JP 日文
  172. NSArray *arLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
  173. NSLog(@"arLanguages:%@",arLanguages);
  174. ///获取设备当前地区的代码和APP语言环境
  175. NSString *languageCode = [NSLocale preferredLanguages][0];
  176. //目前支持 中文(简体 繁体) 英文 日语
  177. if([languageCode rangeOfString:@"zh-Hans"].location != NSNotFound)
  178. {
  179. preferredLanguage = @"zh-CN";
  180. }
  181. else if([languageCode rangeOfString:@"zh-Hant"].location != NSNotFound)
  182. {
  183. preferredLanguage = @"zh-HK";
  184. }
  185. else if([languageCode rangeOfString:@"ja-"].location != NSNotFound)
  186. {
  187. preferredLanguage = @"ja-JP";
  188. }
  189. else{
  190. preferredLanguage = @"en-US";
  191. }
  192. // if([languageCode rangeOfString:preferredLanguage].location != NSNotFound){
  193. // preferredLanguage = languageCode;
  194. // }
  195. // else{
  196. // preferredLanguage = @"en-US";
  197. // }
  198. NSString*gmtStr = [self UTCOffset];
  199. HLog(@"%@",gmtStr);
  200. // //获取名字,如“GMT+08:00
  201. NSString *commondStr = [NSString stringWithFormat:@"{\"type\":\"setLanguages\",\"data\":{\"language\":\"%@\",\"timeZone\":\"%@\"}}",preferredLanguage,gmtStr];
  202. [self send_data:commondStr];
  203. }
  204. #pragma mark 获取云机系统镜像等信息
  205. - (void)getSysInfoFun
  206. {
  207. NSString *commondStr = @"{\"type\":\"getSysInfo\"}";
  208. [self send_data:commondStr];
  209. }
  210. -(NSString *)UTCOffset {
  211. NSTimeZone *localTZ = [NSTimeZone localTimeZone];
  212. float offset = localTZ.secondsFromGMT/3600.0;
  213. if(offset > 0){
  214. return [NSString stringWithFormat:@"Etc/GMT+%g",offset];
  215. }
  216. return [NSString stringWithFormat:@"Etc/GMT%g",offset];
  217. }
  218. #pragma mark 收到系统信息消息回调处理
  219. - (void)getCouldPhoneSysInfoResponseFun:(NSDictionary *)dataDict{
  220. couldphoneSysInfoModel *model = [[couldphoneSysInfoModel alloc] initWithDictionary:dataDict error:nil];
  221. //[[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneSysInfoNotification object:model];
  222. // NSString *hostImgVer = model.data.data.hostImgVer;
  223. // NSString *MyNewVersion = model.data.data.MyNewVersion;
  224. NSString *hostImgVer = model.data.hostImgVer;
  225. NSString *MyNewVersion = model.data.MyNewVersion;
  226. //test code
  227. // hostImgVer = @"1.2.3";
  228. // MyNewVersion = @"1.2.0";
  229. //判断当前版本号 待更新版本
  230. if (!hostImgVer || hostImgVer.length < 3
  231. ||!MyNewVersion || MyNewVersion.length < 3) {
  232. return;
  233. }
  234. NSArray *versionArr = [hostImgVer componentsSeparatedByString:@"."];
  235. NSArray *MyNewVersionArr = [MyNewVersion componentsSeparatedByString:@"."];
  236. NSInteger versionArrCount = versionArr.count;
  237. NSInteger MyNewVersionArrCount = MyNewVersionArr.count;
  238. NSInteger maxCount = versionArrCount < MyNewVersionArrCount ? versionArrCount:MyNewVersionArrCount;
  239. BOOL isNeedShowType = NO;
  240. //判断是否要更新镜像框
  241. if(maxCount > 0){
  242. for (int i=0; i<maxCount; i++) {
  243. NSString * numberStr1 = versionArr[i];
  244. NSString * numberStr2 = MyNewVersionArr[i];
  245. if(numberStr2.intValue > numberStr1.intValue){
  246. isNeedShowType =YES;
  247. break;
  248. }
  249. }
  250. }
  251. if(isNeedShowType){
  252. ksharedAppDelegate.isNeedShowImageNewType = YES;
  253. }
  254. //是否禁用文件传输 (1.3以及以上的镜像可以使用文件传输)
  255. if(versionArr.count >= 3)
  256. {
  257. NSString * oneStr = versionArr[0];
  258. NSString * twoStr = versionArr[1];
  259. NSString * threeStr = versionArr[2];
  260. if(oneStr.integerValue <=1 && twoStr.integerValue <=3 && threeStr.integerValue <=0){//禁用
  261. ksharedAppDelegate.DisabledFileTransferType = YES;
  262. if(MyNewVersionArr.count >= 3)
  263. {
  264. NSString * newOneStr = MyNewVersionArr[0];
  265. NSString * newTwoStr = MyNewVersionArr[1];
  266. NSString * newThreeStr = MyNewVersionArr[2];
  267. if(newOneStr.integerValue >=1 && newTwoStr.integerValue >=3 && newThreeStr.integerValue >= 1){
  268. ksharedAppDelegate.isImageNewFor130 = YES;
  269. }
  270. else{
  271. ksharedAppDelegate.isImageNewFor130 = NO;
  272. }
  273. }
  274. }
  275. else{
  276. ksharedAppDelegate.DisabledFileTransferType = NO;
  277. }
  278. }
  279. [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneSysInfoNotification object:model];
  280. }
  281. #pragma mark 添加消息重复机制
  282. -(void)addCommandSendTaskFunWithType:(NSString*)type WithCommandStr:(NSString*)commandStr
  283. {
  284. if(!commandSendCheckArr){
  285. commandSendCheckArr = [NSMutableArray new];
  286. }
  287. BOOL didAddType = NO;
  288. for (commandSendCheckModel *model in commandSendCheckArr) {
  289. if([model.type isEqualToString:type]){
  290. didAddType = YES;
  291. model.reSendNum = 0;
  292. model.sendTimerStamp = [iTools getNowTimeStamp];
  293. break;
  294. }
  295. }
  296. if(!didAddType){
  297. commandSendCheckModel *model = [commandSendCheckModel new];
  298. model.commandStr = commandStr;
  299. model.type = type;
  300. model.reSendNum = 0;
  301. model.sendTimerStamp = [iTools getNowTimeStamp];
  302. [commandSendCheckArr addObject:model];
  303. }
  304. }
  305. #pragma mark 删除代理确认收到的消息
  306. -(void)deleteCommandSendTaskFunWith:(NSString*)type
  307. {
  308. if(commandSendCheckArr && commandSendCheckArr.count >0){
  309. NSArray *taskArr = [NSArray arrayWithArray:commandSendCheckArr];
  310. for (commandSendCheckModel *model in taskArr) {
  311. if([type isEqualToString:model.type]){
  312. [commandSendCheckArr removeObject:model];
  313. }
  314. }
  315. }
  316. }
  317. #pragma mark 复制手机消息到云机
  318. - (void)updateCopydata{
  319. UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
  320. NSString *str = [pasteboard string];
  321. HLog(@"__________%s______%@____",__func__,str);
  322. if ([str rangeOfString:@"CVLUSTERS_NOUSE_"].location != NSNotFound)
  323. {
  324. str = nil;
  325. }
  326. if (str && str.length >0)
  327. {
  328. //HLog(@"hxd111 cutting %@",str);
  329. /*发送数据*/
  330. NSString *dataStr = [RCCommandHelp commandCuttingWithContent:str];
  331. [self send_data:dataStr];
  332. //pasteboard.string = @"";
  333. }
  334. }
  335. #pragma mark 获取云机以及外挂磁盘
  336. - (void)getExtraFilesListFun
  337. {
  338. // NSString *ExtraCommondStr = [RCCommandHelp getExtraFilesList];
  339. // [self send_data:ExtraCommondStr];
  340. //改走http方案
  341. [[NSNotificationCenter defaultCenter] postNotificationName:getExtraFilesDoneNotification object:nil];/*发送通知*/
  342. }
  343. #pragma mark 获取到云机以及外挂磁盘信息
  344. - (void)getExtraFilesResponseFun:(NSDictionary *)dataDict
  345. {
  346. cloudPhoneExtraFileListModel *model = [[cloudPhoneExtraFileListModel alloc] initWithDictionary:dataDict error:nil];
  347. ksharedAppDelegate.cloudPhoneExtraFileListMod = model;
  348. [[NSNotificationCenter defaultCenter] postNotificationName:getExtraFilesDoneNotification object:dataDict];/*发送通知*/
  349. }
  350. #pragma mark 获取云机基本信息
  351. - (void)getBaseInfoFun
  352. {
  353. NSString *commondStr = @"{\"type\":\"getBaseInfo\"}";
  354. [self send_data:commondStr];
  355. }
  356. #pragma mark 获取到云机基本信息
  357. - (void)getCouldPhoneBaseInfoResponseFun:(NSDictionary *)dataDict
  358. {
  359. couldPhoneBaseInfoModel *model = [[couldPhoneBaseInfoModel alloc] initWithDictionary:dataDict error:nil];
  360. [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneBaseInfoNotification object:model];
  361. }
  362. #pragma mark 获取到TV投屏状态
  363. - (void)getCouldPhoneTvStatusResponseFun:(NSDictionary *)dataDict
  364. {
  365. TvStatusModel *model = [[TvStatusModel alloc] initWithDictionary:dataDict error:nil];
  366. ksharedAppDelegate.TvStatusMod = model;
  367. [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneTvStatusNotification object:model];
  368. // if(![model.msg containsString:@"PushStreamBActivity"])
  369. // {
  370. // return;
  371. // }
  372. //
  373. // UIViewController*topVc = self.navigationController.viewControllers.lastObject;
  374. // if([topVc isKindOfClass:[PlayerViewController class]]){
  375. // [[iToast makeText:NSLocalizedString(@"tv_p2p_ing",nil)] show];
  376. // }
  377. }
  378. #pragma mark 重启云机
  379. - (void)needToRebootFun
  380. {
  381. NSString *commondStr = @"{\"type\":\"reboot\"}";
  382. [self send_data:commondStr];
  383. //添加到任务监听
  384. [self addCommandSendTaskFunWithType:@"reboot" WithCommandStr:commondStr];
  385. //数据埋点
  386. [[netWorkManager shareInstance] DataEmbeddingPointBy:3 withEventValue:@"Cloud_restart"];
  387. _isRebootIngType = YES;
  388. }
  389. #pragma mark 恢复出厂设置
  390. - (void)needToResetFun
  391. {
  392. NSString *commondStr = @"{\"type\":\"reset\"}";
  393. [self send_data:commondStr];
  394. //添加到任务监听
  395. [self addCommandSendTaskFunWithType:@"reset" WithCommandStr:commondStr];
  396. //数据埋点
  397. [[netWorkManager shareInstance] DataEmbeddingPointBy:3 withEventValue:@"Cloud_restore_factory"];
  398. _isResetingType = YES;
  399. }
  400. #pragma mark 云机截图保存到云机图库
  401. - (void)screenshotInCloudPhoneFun{
  402. NSString*taskUid = [iTools getTaskUidStr];
  403. NSString *commondStr = [RCCommandHelp commandCloudPhoneScreenshotWithTaskUid:taskUid];
  404. [self send_data:commondStr];
  405. }
  406. #pragma mark 获取TV投屏状态
  407. - (void)getTvStatusFun
  408. {
  409. NSString *commondStr = @"{\"type\":\"TvStatus\"}";
  410. [self send_data:commondStr];
  411. }
  412. #pragma mark 关闭TV投屏状态
  413. - (void)offTvFun
  414. {
  415. NSString *commondStr = @"{\"type\":\"TvOff\"}";
  416. [self send_data:commondStr];
  417. }
  418. #pragma mark 开启TV投屏状态
  419. - (void)onTvFun
  420. {
  421. NSString *commondStr = @"{\"type\":\"wakeupTV\"}";
  422. [self send_data:commondStr];
  423. }
  424. #pragma mark 创建备份文件夹
  425. - (void)createBackupsFolderBy:(NSString*)backupsDefaultPath
  426. {
  427. NSString *folderName = backupsDefaultPath;
  428. if(folderName && folderName.length >0){
  429. NSString * commandStr = [RCCommandHelp applyForCreateFolderwithFolderName:folderName];
  430. [self send_data:commandStr];
  431. }
  432. }
  433. #pragma mark 获取备份文件夹列表
  434. - (void)getBackupFolderListFun
  435. {
  436. NSString * commandStr = [RCCommandHelp getCreateFolderList];
  437. [self send_data:commandStr];
  438. }
  439. #pragma mark 创建文件夹回调
  440. - (void)createFolderResponseFun:(NSDictionary *)dataDict
  441. {
  442. couldPhoneCommonModel *model = [[couldPhoneCommonModel alloc] initWithDictionary:dataDict error:nil];
  443. if(model){
  444. NSNumber *curNum = [NSNumber numberWithInteger:model.status];
  445. [[NSNotificationCenter defaultCenter] postNotificationName:createFolderDoneNotification object:curNum];/*发送通知*/
  446. }
  447. }
  448. - (void)getFolderListResponseFun:(NSDictionary *)dataDict
  449. {
  450. [[NSNotificationCenter defaultCenter] postNotificationName:getFolderListDoneNotification object:dataDict];/*发送通知*/
  451. }
  452. - (void)searchFileListResponseFun:(NSDictionary *)dataDict
  453. {
  454. [[NSNotificationCenter defaultCenter] postNotificationName:searchFileListDoneNotification object:dataDict];/*发送通知*/
  455. }
  456. #pragma mark U盘插入相关
  457. - (void)getExtraMediaEventResponseFun:(NSDictionary *)dataDict
  458. {
  459. mainBlock((^{
  460. extraMediaEventModel *model = [[extraMediaEventModel alloc] initWithDictionary:dataDict error:nil];
  461. NSString *tip = nil;
  462. if(model.data.event == 0){
  463. tip = NSLocalizedString(@"disk_insertion_tip",nil);
  464. [self showInsertPopViewFun:model.data.name];
  465. }
  466. else if(model.data.event == 1){
  467. tip = NSLocalizedString(@"disk_extract_tip",nil);
  468. }
  469. else if(model.data.event == 2){
  470. tip = NSLocalizedString(@"disk_save_extract_tip",nil);
  471. }
  472. NSString *totalTips = [[NSString alloc] initWithFormat:@"%@%@",model.data.name,tip];
  473. [[iToast makeText:totalTips] show];
  474. }));
  475. }
  476. #pragma mark 显示插入UI弹框
  477. - (void)showInsertPopViewFun:(NSString*)name
  478. {
  479. if(self->curUSBInsertPopV){
  480. [self->curUSBInsertPopV removeFromSuperview];
  481. self->curUSBInsertPopV = nil;
  482. }
  483. self->curUSBInsertPopV = [[USBInsertPopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) withName:name];
  484. [[iTools getKeyWindow] addSubview:self->curUSBInsertPopV];
  485. }
  486. #pragma mark 收到的webrtc消息处理
  487. - (void)handleWebRtcMsgResponseBy:(NSData*)message
  488. {
  489. if([message isKindOfClass:[NSMutableString class]] || [message isKindOfClass:[NSString class]])
  490. {
  491. message = [(NSString *)message dataUsingEncoding:(NSUTF8StringEncoding)];
  492. }
  493. NSError *error = nil;
  494. NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:message options:NSJSONReadingMutableContainers error:&error];
  495. HLog(@"webRtc P2P 通道接收消息:------------------%@",dataDict);
  496. // if([message isKindOfClass:[NSData class]] && [message length] == 0){
  497. // if(type == RCSocketCloudPhoneReceiveTypeForPong)
  498. // {
  499. // [weakSelf keepWebSocketOKFun];
  500. // }
  501. // return;
  502. // }
  503. //
  504. // HLog(@"webSocket指令通道接收消息:------------------\n%@",message);
  505. //
  506. // if([message isKindOfClass:[NSMutableString class]] || [message isKindOfClass:[NSString class]])
  507. // {
  508. // message = [(NSString *)message dataUsingEncoding:(NSUTF8StringEncoding)];
  509. // }
  510. //
  511. // NSError *error = nil;
  512. // NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:message options:NSJSONReadingMutableContainers error:&error];
  513. if(!dataDict){
  514. //[weakSelf handleDownloadResponseFunBy:message];
  515. return;
  516. }
  517. if(![dataDict isKindOfClass:[NSDictionary class]]){
  518. //[__NSCFString allKeys] unrecognized selector sent to ins
  519. return;
  520. }
  521. if(![[dataDict allKeys] containsObject:@"type"]){
  522. return;
  523. }
  524. NSString *messageType = dataDict[@"type"];
  525. if ([messageType isEqualToString:@"cutting"]) {
  526. //[[iToast makeText:@"复制成功"] show];
  527. }
  528. // else if ([messageType isEqualToString:@"forwardMsgRep"]){/*转发的回复*/
  529. //
  530. // cloudPhoneCommonModel *model = [[cloudPhoneCommonModel alloc] initWithDictionary:dataDict error:nil];
  531. // if(!model){
  532. // return;
  533. // }
  534. //
  535. // if([model.data.msg isEqualToString:@"only one socket"]||
  536. // [model.data.msg isEqualToString:@"only one socket2"]){
  537. // [weakSelf deleteCommandSendTaskFunWith:@"offline_notification"];
  538. // }
  539. // }
  540. // else if ([messageType isEqualToString:@"forwardMsg"]){/*转发*/
  541. // /*获取指令类型*/
  542. // NSString *code = nil;
  543. // if ([[dataDict allKeys] containsObject:@"data"]) {
  544. // NSDictionary *data = dataDict[@"data"];
  545. //
  546. // if([data isKindOfClass:[NSString class]]){
  547. // NSString * dataStr = (NSString*)data;
  548. // if([dataStr isEqualToString:@"offline_notification"]){
  549. // HLog(@"被别人挤下线了");
  550. // [weakSelf LogoutByOtherFun];
  551. // }
  552. //
  553. // return;
  554. // }
  555. // else if ([data isKindOfClass:[NSDictionary class]] && [[data allKeys] containsObject:@"code"]) {
  556. // code = [data objectForKey:@"code"];
  557. //
  558. // if (![code isKindOfClass:[NSString class]]) {
  559. // code = [NSString stringWithFormat:@"%ld",[code integerValue]];
  560. // }
  561. // }
  562. // }
  563. //
  564. // /*获取用户名*/
  565. // NSString *useName = nil;
  566. // if ([[dataDict allKeys] containsObject:@"data"]) {
  567. // NSDictionary *data = dataDict[@"data"];
  568. // if ([[data allKeys] containsObject:@"userName"]) {
  569. // //useName = [data objectForKey:@"userName"];
  570. // }
  571. // }
  572. //
  573. //
  574. // if ([code isEqualToString:@"phoneSizeChange"]){/*分辨率改变*/
  575. // if ([[dataDict allKeys] containsObject:@"data"]) {
  576. // NSDictionary *data = dataDict[@"data"];
  577. // if ([[data allKeys] containsObject:@"width"]) {
  578. // ksharedAppDelegate.couldPhone_W_PHONE = [[data objectForKey:@"width"] integerValue];
  579. // }
  580. // if ([[data allKeys] containsObject:@"height"]) {
  581. // ksharedAppDelegate.couldPhone_H_PHONE = [[data objectForKey:@"height"] integerValue];
  582. // }
  583. //
  584. // if (ksharedAppDelegate.couldPhone_W_PHONE > ksharedAppDelegate.couldPhone_H_PHONE) {
  585. // CGFloat temp = ksharedAppDelegate.couldPhone_W_PHONE;
  586. // ksharedAppDelegate.couldPhone_W_PHONE = ksharedAppDelegate.couldPhone_H_PHONE;
  587. // ksharedAppDelegate.couldPhone_H_PHONE = temp;
  588. // }
  589. // }
  590. // }
  591. // }
  592. else if ([messageType isEqualToString:@"getPhoneSize"] || [messageType isEqualToString:@"setPhoneSize"]){
  593. //NSString *sn = nil;
  594. if([messageType isEqualToString:@"setPhoneSize"]){
  595. self.isDiDChangePhoneSizeType = YES;
  596. }
  597. if ([[dataDict allKeys] containsObject:@"data"]) {
  598. NSDictionary *data = dataDict[@"data"];
  599. if ([[data allKeys] containsObject:@"status"]) {
  600. NSInteger status = [[data objectForKey:@"status"] integerValue];
  601. if (status == 0) {/*不是当前设备直接返回*/
  602. if ([[data allKeys] containsObject:@"width"]) {
  603. ksharedAppDelegate.couldPhone_W_PHONE = [[data objectForKey:@"width"] integerValue];
  604. }
  605. if ([[data allKeys] containsObject:@"height"]) {
  606. ksharedAppDelegate.couldPhone_H_PHONE = [[data objectForKey:@"height"] integerValue];
  607. }
  608. if (ksharedAppDelegate.couldPhone_W_PHONE > ksharedAppDelegate.couldPhone_H_PHONE) {
  609. CGFloat temp = ksharedAppDelegate.couldPhone_W_PHONE;
  610. ksharedAppDelegate.couldPhone_W_PHONE = ksharedAppDelegate.couldPhone_H_PHONE;
  611. ksharedAppDelegate.couldPhone_H_PHONE = temp;
  612. }
  613. }
  614. }
  615. }
  616. }
  617. // else if ([messageType isEqualToString:@"sync_wifi"]){
  618. // [weakSelf sync_wifiBackHandleFun];
  619. // }
  620. // else if ([messageType isEqualToString:@"reProduceText"]){
  621. // if ([[dataDict allKeys] containsObject:@"data"]) {
  622. // NSDictionary *data = dataDict[@"data"];
  623. //
  624. // if ([[data allKeys] containsObject:@"text"]) {
  625. // NSString *pasteboardStr = [data objectForKey:@"text"];
  626. // UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
  627. // pasteboard.string = pasteboardStr;
  628. // }
  629. // }
  630. // }
  631. // else if ([messageType isEqualToString:@"downAdnInstallRep"]){
  632. // if ([[dataDict allKeys] containsObject:@"data"]) {
  633. // NSDictionary *data = dataDict[@"data"];
  634. //
  635. // if ([[data allKeys] containsObject:@"status"]) {
  636. // NSString *status = [data objectForKey:@"status"];
  637. // if ([status isEqualToString:@"1"]) {
  638. // mainBlock(^{
  639. // // [[iToast makeText:@"App下载完成"] show];
  640. // });
  641. // }else if ([status isEqualToString:@"0"]){
  642. // mainBlock(^{
  643. // //[[iToast makeText:@"App下载中"] show];
  644. // });
  645. // }
  646. // }
  647. // }
  648. // }
  649. // else if ([messageType isEqualToString:@"shakeit"]){
  650. // HLog(@"\n-----摇一摇成功------");
  651. // }else if ([messageType isEqualToString:@"keyboardFeedbackBean"]){/*调起键盘*/
  652. // HLog(@"\n-----待处理 调起键盘------");
  653. // // [weakSelf keyboardFeedbackBeanFun];
  654. // }else if ([messageType isEqualToString:@"FileRandomReady"]
  655. // ||[messageType isEqualToString:@"FilePartReady"]
  656. // ){/*申请文件上传得到答复*/
  657. // [weakSelf applyUploadFileServiceResponseFun:dataDict];
  658. // }
  659. // else if ([messageType isEqualToString:@"uploadFileRandomRet"]
  660. // ||[messageType isEqualToString:@"uploadFilePartRet"]
  661. // ){/*文件上传得到答复*/
  662. // [weakSelf upLoadFileFunServiceResponseFun:dataDict];
  663. // }
  664. // else if ([messageType isEqualToString:@"backUpFileRandomReady"]
  665. // ||[messageType isEqualToString:@"backUpPartReady"]
  666. // ){/*文件备份得到答复*/
  667. // [weakSelf applyBackupsFileServiceResponseFun:dataDict];
  668. // }
  669. // else if ([messageType isEqualToString:@"backUpFileRandomRet"]
  670. // ||[messageType isEqualToString:@"backUpFilePartRet"]){/*文件备份得到答复*/
  671. // [weakSelf backupsFileFunServiceResponseFun:dataDict];
  672. // }
  673. else if ([messageType isEqualToString:@"getBaseInfo"]){/*获取云机的基本信息*/
  674. [self getCouldPhoneBaseInfoResponseFun:dataDict];
  675. }
  676. else if ([messageType isEqualToString:@"getSysInfo"]){/*获取云机的系统信息*/
  677. [self getCouldPhoneSysInfoResponseFun:dataDict];
  678. }
  679. else if ([messageType isEqualToString:@"TvStatus"]){/*获取TV投屏信息*/
  680. [self getCouldPhoneTvStatusResponseFun:dataDict];
  681. }
  682. else if ([messageType isEqualToString:@"TvOff"]){/*关闭TV投屏*/
  683. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  684. [self getTvStatusFun];
  685. });
  686. }
  687. else if ([messageType isEqualToString:@"wakeupTV"]){/*开启TV投屏*/
  688. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  689. [self getTvStatusFun];
  690. });
  691. }
  692. else if ([messageType isEqualToString:@"reboot"]){/*重启*/
  693. //[weakSelf stopForceStartTimerFun];
  694. }
  695. else if ([messageType isEqualToString:@"mkdir"]){/*创建文件夹*/
  696. [self createFolderResponseFun:dataDict];
  697. }
  698. else if ([messageType isEqualToString:@"getBackupPath"]){/*创建文件夹*/
  699. [self getFolderListResponseFun:dataDict];
  700. }
  701. else if ([messageType isEqualToString:@"search"]){/*创建文件夹*/
  702. [self searchFileListResponseFun:dataDict];
  703. }
  704. else if ([messageType isEqualToString:@"getExtraFiles"]){/*获取云机产品信息*/
  705. [self getExtraFilesResponseFun:dataDict];
  706. }
  707. else if ([messageType isEqualToString:@"extraMediaEvent"]){/*磁盘插拔*/
  708. [self getExtraMediaEventResponseFun:dataDict];
  709. [self getExtraFilesListFun];
  710. }
  711. else if ([messageType isEqualToString:@"reset"]){/**/
  712. [self deleteCommandSendTaskFunWith:@"reset"];
  713. }
  714. else if ([messageType isEqualToString:@"shortcatRep"]){/*截图*/
  715. [[iToast makeText:NSLocalizedString(@"shortcatRep_tip",nil)] show];
  716. }
  717. }
  718. #pragma mark WebRTC 回调 MediaStreamClientEventsDelegate
  719. #pragma mark 不能再这里函数判断 这个是推拉流的
  720. -(void)onChangeConnectionStateFromPeerName:(NSString*)peerName didChangeIceConnectionState:(RTCIceConnectionState)state
  721. {
  722. HLog(@"channel P2P onChangeConnectionStateFromPeerName: state:%ld",state)
  723. self.ConnectionState = state;
  724. switch (state) {
  725. case RTCIceConnectionStateConnected:{
  726. //链接成功
  727. }
  728. break;
  729. case RTCIceConnectionStateCompleted:
  730. //链接完成
  731. break;
  732. case RTCIceConnectionStateFailed:
  733. case RTCIceConnectionStateDisconnected:
  734. case RTCIceConnectionStateClosed:{
  735. if(!_didReportWebRtcType){//还没上报过通道链接情况
  736. [self reportWebRtcRePoportTypeIsChannel:YES withStats:nil];
  737. }
  738. //链接关闭
  739. [self relinkWebRtcFun];
  740. }
  741. break;
  742. default:
  743. break;
  744. }
  745. }
  746. #pragma mark 通道连接状态变化监听
  747. - (void)dataChannelDidChangeFromPeerName:(NSString*)peerName State:(RTCDataChannelState)state
  748. {
  749. HLog(@"webRtc P2P dataChannelDidChangeFromPeerName: state:%ld",state)
  750. self.channelState = state;
  751. switch (state) {
  752. case RTCDataChannelStateConnecting:
  753. {
  754. }
  755. break;
  756. case RTCDataChannelStateOpen:
  757. {
  758. //链接成功
  759. [self handlAllMsgAfterDidLinkFun];
  760. }
  761. break;
  762. case RTCDataChannelStateClosing:
  763. {
  764. }
  765. break;
  766. case RTCDataChannelStateClosed:
  767. {
  768. //链接断开
  769. [self relinkWebRtcFun];
  770. }
  771. break;
  772. default:
  773. break;
  774. }
  775. }
  776. -(void)onChannelDataFromPeerName:(NSString*)peerName buffer:(RTC_OBJC_TYPE(RTCDataBuffer) *)buffer
  777. {
  778. //HLog(@"onIceConnectedFromPeerName:%@",buffer.data);
  779. if(buffer && buffer.data){
  780. KWeakSelf
  781. mainBlock(^{
  782. [weakSelf handleWebRtcMsgResponseBy:buffer.data];
  783. });
  784. }
  785. }
  786. -(void)didGetStats:(NSString*)peerName stats:(RTC_OBJC_TYPE(RTCStatisticsReport) *)stats
  787. {
  788. //HLog(@"didGetStats:%@",stats)
  789. if(!_didReportWebRtcType
  790. && (self.ConnectionState == RTCIceConnectionStateConnected ||self.ConnectionState == RTCIceConnectionStateCompleted)){
  791. [self reportWebRtcRePoportTypeIsChannel:YES withStats:stats];
  792. _didReportWebRtcType = YES;
  793. [_mediaStream setShouldGetStats:NO];
  794. }
  795. }
  796. -(void)onAuthResultFromPeerName:(NSString*)peerName code:(int)code descriptions:(NSString*)descriptions
  797. {
  798. HLog(@"webRtc P2P onAuthResultFromPeerName")
  799. }
  800. @end