Skip to content

Commit

Permalink
Handle live streams appropriately (0xced#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
SoneeJohn committed Feb 18, 2020
1 parent 86bdba1 commit 6126879
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions XCDYouTubeKit/XCDYouTubeVideoQueryOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ - (void) cancel
- (void) startQuery
{
NSMutableArray <XCDURLHeadOperation *>*HEADOperations = [NSMutableArray new];
BOOL isHTTPLiveStream = self.video.streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] != nil;

[self.video.streamURLs enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, NSURL * _Nonnull obj, BOOL * _Nonnull stop)
{
Expand All @@ -113,6 +114,7 @@ - (void) startQuery

NSMutableArray <XCDURLGetOperation *>*GETOperations = [NSMutableArray new];
NSMutableDictionary<id, NSError *> *streamErrors = [NSMutableDictionary new];
NSMutableDictionary *streamURLs = [NSMutableDictionary new];

for (XCDURLHeadOperation *HEADOperation in HEADOperations)
{
Expand All @@ -126,10 +128,32 @@ - (void) startQuery

if (HEADOperation.error == nil && [(NSHTTPURLResponse *)HEADOperation.response statusCode] == 200)
{
[GETOperations addObject:[[XCDURLGetOperation alloc]initWithURL:HEADOperation.url info:HEADOperation.info cookes:HEADOperation.cookies]];
if (isHTTPLiveStream)
{
[streamURLs addEntriesFromDictionary:(NSDictionary *)HEADOperation.info];
}
else
{
[GETOperations addObject:[[XCDURLGetOperation alloc]initWithURL:HEADOperation.url info:HEADOperation.info cookes:HEADOperation.cookies]];
}
}
}

if (isHTTPLiveStream)
{
/**
* When it's a live stream all the other streams plus the live stream will cause the `XCDURLGetOperation` to take a extremely longtime to complete.
* Since it's a live stream clients would tend to be only interested in the `XCDYouTubeVideoQualityHTTPLiveStreaming` value and since we checked this already with the head operation then we consider these URLs to be reachable
*/
[self finishWithStreamURLs:streamURLs streamErrors:streamErrors];
return;
}

[self startGETOperations:GETOperations streamErrors:streamErrors];
}

- (void) startGETOperations:(NSArray <XCDURLGetOperation *>*)GETOperations streamErrors:(NSMutableDictionary <id, NSError *> *)streamErrors
{
[self.queryQueue addOperations:GETOperations waitUntilFinished:YES];

NSMutableDictionary *streamURLs = [NSMutableDictionary new];
Expand All @@ -151,8 +175,13 @@ - (void) startQuery
}
}

[self finishWithStreamURLs:streamURLs streamErrors:streamErrors];
}

- (void) finishWithStreamURLs:(NSDictionary *)streamURLs streamErrors:(NSDictionary *)streamErrors
{
if (streamErrors.count != 0)
self.streamErrors = streamErrors.mutableCopy;
self.streamErrors = streamErrors.copy;

if (streamURLs.count == 0)
{
Expand All @@ -161,11 +190,6 @@ - (void) startQuery
return;
}

[self finishWithStreamURL:streamURLs];
}

- (void) finishWithStreamURL:(NSMutableDictionary *)streamURLs
{
self.streamURLs = [streamURLs copy];
[self finish];
}
Expand Down

0 comments on commit 6126879

Please sign in to comment.