- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest; - (void)resourceLoader:(AVAssetResourceLoader *)resourceLoader didCancelLoadingRequest:(AVAssetResourceLoadingRequest *)loadingRequest;
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest { loadingRequest.contentInformationRequest.contentType = (__bridge NSString *)kUTTypeQuickTimeMovie; loadingRequest.contentInformationRequest.contentLength = movieLength; loadingRequest.contentInformationRequest.byteRangeAccessSupported = YES; [loadingRequest.dataRequest respondWithData:[decryptedData subdataWithRange:NSMakeRange((NSUInteger)loadingRequest.dataRequest.requestedOffset, loadingRequest.dataRequest.requestedLength)]]; [loadingRequest finishLoading]; return YES; }
resourceURL = [NSURL URLWithString:[@"encryptedfile://" stringByAppendingString:fake-path-to-file]];
fileHandle = [NSFileHandle fileHandleForReadingFromURL:resourceURL error:nil];
assetPlayer = [AVURLAsset assetWithURL:resourceURL]; [assetPlayer.resourceLoader setDelegate:self queue:dispatch_get_main_queue()]; itemPlayer = [AVPlayerItem playerItemWithAsset:assetPlayer]; avPlayer = [AVPlayer playerWithPlayerItem:itemPlayer];
controller = [[AVPlayerViewController alloc] init]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; controller.player = avPlayer; controller.player.actionAtItemEnd = AVPlayerActionAtItemEndNone; [avPlayer play];
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest { loadingRequest.contentInformationRequest.contentType = (__bridge NSString *)kUTTypeQuickTimeMovie; loadingRequest.contentInformationRequest.contentLength = movieLength; loadingRequest.contentInformationRequest.byteRangeAccessSupported = YES; if(chunkMode){ NSUInteger offset = (NSUInteger)loadingRequest.dataRequest.requestedOffset; if(currentOffset != offset){ currentOffset = offset; NSUInteger requestedBlock = floor(currentOffset/blockSize); if(currentBlockIndex != requestedBlock){ currentBlockIndex = requestedBlock; // Loading other block of data decryptedData = [self getDataFromFile:currentBlockIndex]; } } if(currentOffset > blockSize*currentBlockIndex){ offset = currentOffset - blockSize*currentBlockIndex; } else { offset = 0; } NSUInteger maxLength = [decryptedData length] - offset; if(loadingRequest.dataRequest.requestedLength < maxLength && loadingRequest.dataRequest.requestedLength <= [decryptedData length]){ maxLength = loadingRequest.dataRequest.requestedLength; } [loadingRequest.dataRequest respondWithData:[decryptedData subdataWithRange:NSMakeRange(offset, maxLength)]]; } else { [loadingRequest.dataRequest respondWithData:[decryptedData subdataWithRange:NSMakeRange((NSUInteger)loadingRequest.dataRequest.requestedOffset, loadingRequest.dataRequest.requestedLength)]]; } [loadingRequest finishLoading]; return YES; }
- (NSMutableData *) getDataFromFile:(NSUInteger) index { if(fileHandle){ [fileHandle seekToFileOffset:index*chunksInBlock*chunkSize]; return [NSMutableData dataWithData:[AESCrypt decryptData:[fileHandle readDataOfLength:chunksInBlock*chunkSize] password:PASSWORD chunkSize:blockSize iv:IV]]; } return nil; }
+ (NSData*) decryptData:(NSData*)data password:(NSString *)password chunkSize:(NSUInteger)chunkSize { return [self decryptData:data password:password chunkSize:chunkSize offsetBlock:0 countBlock:0 iv:nil]; } + (NSData*) decryptData:(NSData*)data password:(NSString *)password chunkSize:(NSUInteger)chunkSize iv: (id) iv { return [self decryptData:data password:password chunkSize:chunkSize offsetBlock:0 countBlock:0 iv:iv]; } + (NSData*) decryptData:(NSData*)data password:(NSString *)password chunkSize:(NSUInteger)chunkSize offsetBlock:(NSUInteger)offsetBlock countBlock:(NSUInteger)countBlock iv: (id) iv { NSUInteger length = [data length]; if (chunkSize > length) { chunkSize = floor(length/16)*16; } if(countBlock > 0){ length = (offsetBlock+countBlock)*chunkSize; } if(length > [data length]){ length = [data length]; } NSUInteger offset = offsetBlock * chunkSize; NSMutableData *decryptedData = [NSMutableData alloc]; NSData* encryptedPartOfData; do { NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset; NSData* partOfData = [data subdataWithRange:NSMakeRange(offset, thisChunkSize)]; if(iv == nil){ encryptedPartOfData = [partOfData decryptedAES256DataUsingKey:[[password dataUsingEncoding:NSUTF8StringEncoding] SHA256Hash] error:nil]; } else { encryptedPartOfData = [partOfData decryptedAES256DataUsingKey:[password dataUsingEncoding:NSUTF8StringEncoding] initializationVector:iv error:nil]; } [decryptedData appendData:encryptedPartOfData]; offset += thisChunkSize; } while (offset < length); return decryptedData; }
Source: https://habr.com/ru/post/319732/
All Articles