webRtcManager.m 30 KB

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