Now I want to implement a download manager which can support to download an assert of a hls link. There are some issues:
1.As https://forums.developer.apple.com/message/42352#42352, there is a resume rate limiter. So, it seems we can't create and resume one segment at a time. For this, I do some tests that I create all tasks but just resume the first task at begin, then I resume the next task in "didcompletewitherror":
- (void)beginDownloadWithUrlArray:(NSArray *)downloadURLArray{
for (NSString *urlStr in downloadURLArray) {
NSURL *downloadURL = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];
NSURLSessionTask *task = [self.session downloadTaskWithRequest:request];
[self.taskArray addObject:task];
}
[self runNextTask];
}
- (void)runNextTask{
if (self.taskIndex < self.taskArray.count) {
self.currentDownloadTask = self.taskArray[self.taskIndex];
[self.currentDownloadTask resume];
NSLog(@"nextrequest--%@",self.currentDownloadTask);
}else{
self.currentDownloadTask = nil;
}
self.taskIndex ++ ;
}
- (void)URLSession:(NSURLSession *)session
task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error {
NSLog(@"didCompleteWithError");
if (error) {
NSLog(@"error -- %@",error);
if ([error.userInfo objectForKey:NSURLSessionDownloadTaskResumeData]) {
self.resumeData = [error.userInfo objectForKey:NSURLSessionDownloadTaskResumeData];
NSURLSessionTask *task = [[[self class] sharedManager] downloadTaskWithResumeData:self.resumeData];
[task resume];
}
}else{
[self runNextTask];
}
}
I can see it can continue downloading all tasks without delay. But it is strange that I can see didCompleteWithError is called when all tasks are finished. So I wonder whether "[self runNextTask]" in "didCompleteWithError" is called? But if I remove "[self runNextTask]" from "didCompleteWithError", it just can download the first task. Can someone explain what is the mechanism of this? Thanks. Also we capture the pcap packet to analyze and find that the tasks are not called in order.
2. As apple provides an AVAssetDownloadURLSession for downloading an asset and find that it can download the segment in order and have no delay. What is the mechanism of this. Thanks.