webRtcManager.m 30 KB

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