PlayerViewController+otherDelegate.mm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. //
  2. // PlayerViewController+otherDelegate.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2023/10/13.
  6. //
  7. #import "PlayerViewController+otherDelegate.h"
  8. #import "RCCommandHelp.h"
  9. #import "couldPhoneBaseInfoModel.h"
  10. #import "cachesFileManager.h"
  11. #import "couldphoneSysInfoModel.h"
  12. @implementation PlayerViewController (otherDelegate)
  13. #pragma mark ComontAlretViewControllerDelegate
  14. - (void)CommonAlertokBtnClickPressed{
  15. //HLog("11111");
  16. self.isLoginAgainType = YES;
  17. [[NSNotificationCenter defaultCenter] postNotificationName:lockBypwdNotification object:nil];
  18. }
  19. - (void)CommonAlertCancelBtnClickPressed{
  20. HLog("强制退出app");
  21. exit(0);/*强制退出app*/
  22. }
  23. - (void)getCouldPhoneBaseInfoResponseFun:(NSDictionary *)dataDict
  24. {
  25. couldPhoneBaseInfoModel *model = [[couldPhoneBaseInfoModel alloc] initWithDictionary:dataDict error:nil];
  26. [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneBaseInfoNotification object:model];
  27. }
  28. - (void)getCouldPhoneSysInfoResponseFun:(NSDictionary *)dataDict{
  29. couldphoneSysInfoModel *model = [[couldphoneSysInfoModel alloc] initWithDictionary:dataDict error:nil];
  30. [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneSysInfoNotification object:model];
  31. }
  32. #pragma mark 监听文件开始的的通知
  33. - (void)beginGotoUploadFileFun:(NSNotification *)notification
  34. {
  35. self.isSuspendUploadType = NO;
  36. uploadFileDataModel *uploadFileDataMod = [notification object];
  37. //HLog(@"%@",uploadFileDataMod);
  38. self.curUploadFileDataMod = uploadFileDataMod;
  39. if(![uploadFileDataMod isKindOfClass:[uploadFileDataMod class]]){
  40. HLog(@"上传的文件类型不对");
  41. return;
  42. }
  43. [self ApplyUploadFileFun];
  44. }
  45. - (void)applyUploadFileServiceResponseFun:(NSDictionary *)dataDict
  46. {
  47. if([dataDict isKindOfClass:[NSDictionary class]]){
  48. if ([[dataDict allKeys] containsObject:@"data"]) {
  49. NSDictionary *data = dataDict[@"data"];
  50. if ([[data allKeys] containsObject:@"status"]) {
  51. NSString *status = [data objectForKey:@"status"];
  52. if(![status isKindOfClass:[NSString class]]){
  53. status = [[NSString alloc] initWithFormat:@"%@",status];
  54. }
  55. //if(1)
  56. if([status isEqualToString:@"1"])
  57. {
  58. //[self tryAgainApplyUploadFun];
  59. [self clearUploadFileDataFun];
  60. [[uploadFileManager shareInstance] uploadFileFailFun];
  61. }else if ([status isEqualToString:@"0"]){
  62. if ([[data allKeys] containsObject:@"taskUid"]) {
  63. NSString *taskUid = data[@"taskUid"];
  64. if(![status isKindOfClass:[NSString class]]){
  65. taskUid = [[NSString alloc] initWithFormat:@"%@",taskUid];
  66. }
  67. if(taskUid && [taskUid isEqualToString:self.taskUid]){
  68. NSNumber * fileLengthNum = @0;
  69. if ([[data allKeys] containsObject:@"fileLength"])
  70. {
  71. fileLengthNum = data[@"fileLength"];
  72. }
  73. // [[uploadFileManager shareInstance] changeUploadFileState:uploadStateUploading withDidUploadBytes:[fileLengthNum integerValue] withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
  74. //
  75. // }];
  76. if([fileLengthNum integerValue] == self.curUploadFileDataMod.totalBytes){
  77. self.taskUid = nil;
  78. NSMutableArray *delArr = [NSMutableArray new];
  79. [delArr addObject:self.curUploadFileDataMod];
  80. [[uploadFileManager shareInstance] deleteUploadFileRecordBy:delArr complete:^(BOOL isSuccess) {
  81. if (isSuccess) {
  82. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileDeteleNotification object:self.curUploadFileDataMod];
  83. }
  84. }];
  85. mainBlock(^{
  86. [[iToast makeText:NSLocalizedString(@"File_upload_file_already_exists",nil)] show];
  87. });
  88. HLog(@"hxd already 上传任务异常终止")
  89. return;
  90. }
  91. if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeImage){
  92. [self beginGotoUploadImageFun:fileLengthNum];
  93. }
  94. else{
  95. [self beginGotoUploadVidelFun:fileLengthNum];
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. else{
  103. //异常
  104. [self tryAgainApplyUploadFun];
  105. }
  106. }
  107. else{
  108. //异常
  109. [self tryAgainApplyUploadFun];
  110. }
  111. }
  112. #pragma mark 图片上传
  113. -(void)beginGotoUploadImageFun:(NSNumber*)fileLengthNum{
  114. self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:[fileLengthNum integerValue]];
  115. if(!self.cutFileDataArr){
  116. [self clearUploadFileDataFun];
  117. [[uploadFileManager shareInstance] uploadFileFailFun];
  118. self.taskUid = nil;
  119. mainBlock(^{
  120. [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
  121. });
  122. HLog(@"hxd fail 上传任务异常终止")
  123. return;
  124. }
  125. //答复的taskUid 跟申请的一致 开发发送内容
  126. [self upLoadFileFun];
  127. }
  128. #pragma mark 视频上传
  129. //每次从文件重新切一片 传一片
  130. -(void)beginGotoUploadVidelFun:(NSNumber*)fileLengthNum{
  131. NSData *data = [self cutVideoFileFunAtIndex:fileLengthNum.longLongValue];
  132. HLog(@"data leng:%ld",[data length]);
  133. self.AllFileData = data;
  134. self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:0];
  135. self.indexOfUploadFlie = 0;
  136. HLog(@"data leng:%ld",[data length]);
  137. //return;
  138. // NSString*videoPathStr = [cachesFileManager getFilePathWithName:self.curUploadFileDataMod.filename type:uploadFileTypeVideo];
  139. // self.AllFileData = [NSData dataWithContentsOfFile:videoPathStr];
  140. // self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:[fileLengthNum integerValue]];
  141. if(!self.cutFileDataArr){
  142. [self clearUploadFileDataFun];
  143. [[uploadFileManager shareInstance] uploadFileFailFun];
  144. self.taskUid = nil;
  145. mainBlock(^{
  146. [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
  147. });
  148. HLog(@"hxd fail 上传任务异常终止")
  149. return;
  150. }
  151. //答复的taskUid 跟申请的一致 开发发送内容
  152. [self upLoadFileFun];
  153. }
  154. #pragma mark 分段读视频文件
  155. -(NSData*)cutVideoFileFunAtIndex:(NSUInteger)dataIndex{
  156. NSString *filePath = [cachesFileManager getFilePathWithName:self.curUploadFileDataMod.filename type:uploadFileTypeVideo]; // 文件路径
  157. NSFileManager *manager0 = [NSFileManager defaultManager];
  158. if(![manager0 fileExistsAtPath:filePath]) {
  159. return [NSData new];
  160. }
  161. NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath]; // 创建文件句柄
  162. // 设置分段读取的大小,这里以每次读取1KB为例
  163. const NSUInteger chunkSize = 50 * 1024 *1024;
  164. NSMutableData *data = [NSMutableData data];
  165. if (fileHandle) {
  166. long long endOfFile = [fileHandle seekToEndOfFile];
  167. if (endOfFile >= chunkSize) {
  168. // 读取文件的分段数据到某个位置
  169. [fileHandle seekToFileOffset:dataIndex];
  170. // 读取文件的分段数据
  171. NSData* chunk = [fileHandle readDataOfLength:chunkSize];
  172. if (chunk) {
  173. [data appendData:chunk];
  174. }
  175. }
  176. else{
  177. // 读取文件的分段数据到某个位置
  178. [fileHandle seekToFileOffset:dataIndex];
  179. [data appendData:[fileHandle readDataToEndOfFile]];
  180. }
  181. // 在这里可以对文件内容进行处理
  182. // ...
  183. // 关闭文件句柄
  184. [fileHandle closeFile];
  185. }
  186. return data;
  187. }
  188. - (void)upLoadFileFunServiceResponseFun:(NSDictionary *)dataDict
  189. {
  190. //{"type":"uploadFileRandomRet","data":{"progress":"9\/10","filePath":"/sdcard/Download/IMG_0036.JPG","status":"0","msg":"success"}}
  191. if([dataDict isKindOfClass:[NSDictionary class]]){
  192. if ([[dataDict allKeys] containsObject:@"data"]) {
  193. NSDictionary *data = dataDict[@"data"];
  194. if ([[data allKeys] containsObject:@"status"]) {
  195. NSString *status = [data objectForKey:@"status"];
  196. if(![status isKindOfClass:[NSString class]]){
  197. status = [[NSString alloc] initWithFormat:@"%@",status];
  198. }
  199. if ([status isEqualToString:@"1"]) {
  200. //[self tryAgainApplyUploadFun];
  201. [self clearUploadFileDataFun];
  202. [[uploadFileManager shareInstance] uploadFileFailFun];
  203. }else if ([status isEqualToString:@"0"]){
  204. NSNumber * fileLengthNum = @0;
  205. if ([[data allKeys] containsObject:@"fileLenth"])
  206. {
  207. fileLengthNum = data[@"fileLenth"];
  208. }
  209. [self handleUploadingFun:fileLengthNum];
  210. }
  211. }
  212. }
  213. else{
  214. //异常
  215. [self tryAgainApplyUploadFun];
  216. }
  217. }
  218. else{
  219. //异常
  220. [self tryAgainApplyUploadFun];
  221. }
  222. }
  223. #pragma mark 申请上传文件
  224. - (void)ApplyUploadFileFun
  225. {
  226. if(!self.taskUid){
  227. self.taskUid = [iTools getTaskUidStr];
  228. self.fileName = self.curUploadFileDataMod.filename;
  229. if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  230. //self.AllFileData = self.curUploadFileDataMod.videoData;
  231. }
  232. else{
  233. self.AllFileData = self.curUploadFileDataMod.imageData;
  234. }
  235. self.indexOfUploadFlie = 0;
  236. //HLog(@"%@ %@",self.taskUid,self.fileName);
  237. //self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData];
  238. }
  239. NSString * commandStr = [RCCommandHelp applyForUploadFileBy:self.taskUid filePath:self.fileName];
  240. [self.commandChannelManager rc_sendData:commandStr];
  241. }
  242. #pragma mark 遇到异常时 重新上传
  243. - (void)tryAgainApplyUploadFun
  244. {
  245. self.taskUid = nil;
  246. [self ApplyUploadFileFun];
  247. }
  248. #pragma mark 处理上传过程
  249. - (void)handleUploadingFun:(NSNumber*)fileLengthNum
  250. {
  251. if(self.isSuspendUploadType){
  252. //暂停
  253. self.taskUid = nil;
  254. NSInteger didUploadSize = (self.indexOfUploadFlie+1) * (self.cutFileEachPieceSzie);
  255. [[uploadFileManager shareInstance] changeUploadFileState:uploadStateSuspend withDidUploadBytes:didUploadSize withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
  256. }];
  257. return;
  258. }
  259. if(self.indexOfUploadFlie < self.cutFileDataArr.count -1) {
  260. //NSInteger didUploadSize = (self.indexOfUploadFlie+1) * (self.cutFileEachPieceSzie);
  261. NSInteger didUploadSize = fileLengthNum.longLongValue;
  262. [[uploadFileManager shareInstance] changeUploadFileState:uploadStateUploading withDidUploadBytes:didUploadSize withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
  263. }];
  264. self.indexOfUploadFlie ++;
  265. [self upLoadFileFun];
  266. }
  267. else if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeVideo && fileLengthNum && fileLengthNum.longLongValue < self.curUploadFileDataMod.totalBytes){
  268. //视频做了二次切割 不一定上传完了
  269. HLog(@"视频上传传完一片 再来一片");
  270. [self beginGotoUploadVidelFun:fileLengthNum];
  271. }
  272. else{
  273. HLog(@"上传完成");
  274. [self clearUploadFileDataFun];
  275. [[uploadFileManager shareInstance] uploadFileDoneFun];
  276. }
  277. }
  278. -(void)clearUploadFileDataFun{
  279. //上传数据清空
  280. self.taskUid = nil;
  281. self.fileName = nil;
  282. self.AllFileData = nil;
  283. self.indexOfUploadFlie = 0;
  284. self.cutFileDataArr = nil;
  285. self.curUploadFileDataMod = nil;
  286. self.isSuspendUploadType = NO;
  287. }
  288. - (Byte)getBCC:(NSData *)data {
  289. Byte bcc = 0;
  290. unsigned char *bytes = (unsigned char *)[data bytes];
  291. for (int i = 0; i < [data length]; i++) {
  292. bcc = (Byte)(bcc ^ bytes[i]);
  293. }
  294. return bcc;
  295. }
  296. #pragma mark 文件切片
  297. -(NSMutableArray *)fileCutPartsBy:(NSData*)curData withDidUpLoadSize:(NSInteger)didLoadSize{
  298. NSData * data = curData;
  299. NSUInteger allLength = data.length;
  300. //NSUInteger subs = 100*1024;//4096;//要切片的大小,我这里设置的是4096字节
  301. NSUInteger subs = 31*1024;//4096;//要切片的大小,内存没有暴增
  302. self.cutFileEachPieceSzie = subs;
  303. NSInteger index = 0;//起始位置
  304. if(didLoadSize != 0){
  305. if (didLoadSize % subs != 0){
  306. //出错了
  307. return nil;
  308. }
  309. else{
  310. // index = didLoadSize/subs;
  311. // allLength = allLength - didLoadSize;
  312. self.indexOfUploadFlie = didLoadSize/subs;
  313. }
  314. }
  315. NSMutableArray *dataArray =[NSMutableArray new];
  316. do {
  317. @autoreleasepool {
  318. if (allLength>subs) {
  319. NSRange range =NSMakeRange(index*subs, subs);
  320. index++;
  321. //NSLog(@"%@",NSStringFromRange(range));
  322. [dataArray addObject:[data subdataWithRange:range]];
  323. allLength = allLength - subs;
  324. }else{
  325. NSRange range = NSMakeRange(index*subs, allLength);
  326. //NSLog(@"%@",NSStringFromRange(range));
  327. [dataArray addObject:[data subdataWithRange:range]];
  328. allLength = 0;
  329. }
  330. }
  331. } while (allLength>0);
  332. HLog(@"dataArray leng:%ld",dataArray.count);//最后得到切片的结果,数组里面是NSData对象
  333. return dataArray;
  334. }
  335. // int转NSData
  336. - (NSData *)int2Data:(int)i{
  337. Byte b0 = i & 0xff;
  338. Byte b1 = i >> 8 & 0xff;
  339. Byte b2 = i >> 16 & 0xff;
  340. Byte b3 = i >> 24 & 0xff;
  341. // 有大小端模式问题?
  342. //Byte result[] = {b0, b1, b2, b3};
  343. Byte result[] = {b3, b2, b1, b0};
  344. return [NSData dataWithBytes:result length:sizeof(result)];
  345. }
  346. // short转NSData
  347. - (NSData *)short2Data:(short)i{
  348. Byte b0 = i & 0xff;
  349. Byte b1 = i >> 8 & 0xff;
  350. // 有大小端模式问题?
  351. //Byte result[] = {b0, b1, b2, b3};
  352. Byte result[] = { b1, b0};
  353. return [NSData dataWithBytes:result length:sizeof(result)];
  354. }
  355. #pragma mark 可以发送文件了
  356. /*
  357. 文件数据字节流byte[]
  358. 格式1:下载日志文件压缩包
  359. |head|msgType|taskId| datalen | curNum | totalNum| data |bcc校验|end|
  360. 长度: 1 1 6 (curNum+totalNum+data) 4 4 datalen- 8 1 1
  361. 0x68 0x0e 0x16
  362. bcc校验从第1位 也就是从msgType起 到 校验位前(不包含校验位)数据作bcc校验
  363. */
  364. - (void)upLoadFileFun
  365. {
  366. //HLog(@"上传文件%ld/%ld",self.indexOfUploadFlie +1,self.cutFileDataArr.count);
  367. //data
  368. NSData *curData = self.cutFileDataArr[self.indexOfUploadFlie];
  369. //HLog(@"headData:%@",curData);
  370. //head
  371. unsigned char headNum = 104;
  372. NSData *headData = [NSMutableData dataWithBytes:&headNum length:sizeof(headNum)];
  373. //HLog(@"headData:%@",headData);
  374. //msgType
  375. //unsigned char msgType = 12;
  376. unsigned char msgType = 15;
  377. NSData *msgTypeData = [NSMutableData dataWithBytes:&msgType length:sizeof(msgType)];
  378. //HLog(@"msgTypeData:%@",msgTypeData);
  379. //taskId
  380. NSData *taskIdData = [self.taskUid dataUsingEncoding:NSUTF8StringEncoding];
  381. //HLog(@"taskIdData:%@",taskIdData);
  382. //datalen
  383. int datalenNum = int([curData length] +(4+4));
  384. //NSData *datalenData = [NSData dataWithBytes:&datalenNum length:sizeof(datalenNum)];
  385. NSData *datalenData = [self int2Data:datalenNum];
  386. //HLog(@"datalenData %@",datalenData);
  387. //curNum
  388. int curNum = (int)self.indexOfUploadFlie;
  389. //NSData *curNumData = [NSData dataWithBytes:&curNum length:sizeof(curNum)];
  390. NSData * curNumData = [self int2Data:curNum];
  391. //HLog(@"curNumData:%@",curNumData);
  392. //totalNum
  393. int totalNum = (int)self.cutFileDataArr.count;
  394. //NSData *totalNumData = [NSData dataWithBytes:&totalNum length:sizeof(totalNum)];
  395. NSData * totalNumData = [self int2Data:totalNum];
  396. //HLog(@"totalNumData:%@",totalNumData);
  397. NSMutableData *checkBCCData = [NSMutableData new];
  398. [checkBCCData appendData:msgTypeData];
  399. [checkBCCData appendData:taskIdData];
  400. [checkBCCData appendData:datalenData];
  401. [checkBCCData appendData:curNumData];
  402. [checkBCCData appendData:totalNumData];
  403. [checkBCCData appendData:curData];
  404. long length = [curData length];
  405. //HLog(@"curData length %d",length);
  406. Byte bccCheck = [self getBCC:checkBCCData];
  407. //HLog(@"bcc 000 %d",bccCheck);
  408. //bcc校验
  409. char bccNum = (char)bccCheck;
  410. NSData *bccData = [NSMutableData dataWithBytes:&bccNum length:sizeof(bccNum)];
  411. //HLog(@"%@",bccData);
  412. //end
  413. unsigned char endNum = 22;
  414. NSData *endData = [NSMutableData dataWithBytes:&endNum length:sizeof(endNum)];
  415. //HLog(@"%@",endData);
  416. NSMutableData *sendData = [NSMutableData new];
  417. [sendData appendData:headData];
  418. [sendData appendData:checkBCCData];
  419. [sendData appendData:bccData];
  420. [sendData appendData:endData];
  421. //HLog(@"sendData length %ld",[sendData length]);
  422. [self.commandChannelManager rc_sendData:sendData];
  423. //HLog(@"文件上传sendData %@",sendData);
  424. }
  425. - (void)suspendUploadFileFun:(NSNotification *)notification
  426. {
  427. self.isSuspendUploadType = YES;
  428. }
  429. @end