VIResourceLoadingRequestWorker.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // VIResourceLoadingRequestWorker.m
  3. // VIMediaCacheDemo
  4. //
  5. // Created by Vito on 4/21/16.
  6. // Copyright © 2016 Vito. All rights reserved.
  7. //
  8. #import "VIResourceLoadingRequestWorker.h"
  9. #import "VIMediaDownloader.h"
  10. #import "VIContentInfo.h"
  11. @import MobileCoreServices;
  12. @import AVFoundation;
  13. @import UIKit;
  14. @interface VIResourceLoadingRequestWorker () <VIMediaDownloaderDelegate>
  15. @property (nonatomic, strong, readwrite) AVAssetResourceLoadingRequest *request;
  16. @property (nonatomic, strong) VIMediaDownloader *mediaDownloader;
  17. @end
  18. @implementation VIResourceLoadingRequestWorker
  19. - (instancetype)initWithMediaDownloader:(VIMediaDownloader *)mediaDownloader resourceLoadingRequest:(AVAssetResourceLoadingRequest *)request {
  20. self = [super init];
  21. if (self) {
  22. _mediaDownloader = mediaDownloader;
  23. _mediaDownloader.delegate = self;
  24. _request = request;
  25. [self fullfillContentInfo];
  26. }
  27. return self;
  28. }
  29. - (void)startWork {
  30. AVAssetResourceLoadingDataRequest *dataRequest = self.request.dataRequest;
  31. long long offset = dataRequest.requestedOffset;
  32. NSInteger length = dataRequest.requestedLength;
  33. if (dataRequest.currentOffset != 0) {
  34. offset = dataRequest.currentOffset;
  35. }
  36. BOOL toEnd = NO;
  37. if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
  38. if (dataRequest.requestsAllDataToEndOfResource) {
  39. toEnd = YES;
  40. }
  41. }
  42. [self.mediaDownloader downloadTaskFromOffset:offset length:length toEnd:toEnd];
  43. }
  44. - (void)cancel {
  45. [self.mediaDownloader cancel];
  46. }
  47. - (void)finish {
  48. if (!self.request.isFinished) {
  49. [self.request finishLoadingWithError:[self loaderCancelledError]];
  50. }
  51. }
  52. - (NSError *)loaderCancelledError{
  53. NSError *error = [[NSError alloc] initWithDomain:@"com.resourceloader"
  54. code:-3
  55. userInfo:@{NSLocalizedDescriptionKey:@"Resource loader cancelled"}];
  56. return error;
  57. }
  58. - (void)fullfillContentInfo {
  59. AVAssetResourceLoadingContentInformationRequest *contentInformationRequest = self.request.contentInformationRequest;
  60. if (self.mediaDownloader.info && !contentInformationRequest.contentType) {
  61. // Fullfill content information
  62. contentInformationRequest.contentType = self.mediaDownloader.info.contentType;
  63. contentInformationRequest.contentLength = self.mediaDownloader.info.contentLength;
  64. contentInformationRequest.byteRangeAccessSupported = self.mediaDownloader.info.byteRangeAccessSupported;
  65. }
  66. }
  67. #pragma mark - VIMediaDownloaderDelegate
  68. - (void)mediaDownloader:(VIMediaDownloader *)downloader didReceiveResponse:(NSURLResponse *)response {
  69. [self fullfillContentInfo];
  70. }
  71. - (void)mediaDownloader:(VIMediaDownloader *)downloader didReceiveData:(NSData *)data {
  72. [self.request.dataRequest respondWithData:data];
  73. }
  74. - (void)mediaDownloader:(VIMediaDownloader *)downloader didFinishedWithError:(NSError *)error {
  75. if (error.code == NSURLErrorCancelled) {
  76. return;
  77. }
  78. if (!error) {
  79. [self.request finishLoading];
  80. } else {
  81. [self.request finishLoadingWithError:error];
  82. }
  83. [self.delegate resourceLoadingRequestWorker:self didCompleteWithError:error];
  84. }
  85. @end