1

I'm trying to use AVPlayer with custom URL loading (a custom NSURLProtocol subclass). But it seems [NSURLProtocol registerClass] does not work with AVPlayer in real device (see this thread).

Now I'm trying to use AVAssetResourceLoaderDelegate to do the custom URL loading. However it is a bit confusing to me how the delegate will be triggered. The URL I deal with looks like this https://some_ip_address:port/resource/, but it seems like my delegate is not called for such URL. I tried to change the scheme to non-HTTP (e.g. "quic") and was able to trigger the delegate code but I really don't want to hack the scheme.

Here is the related code:

(delegate is implemented in a different file)

    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];  
    AVAssetResourceLoader *resourceLoader = asset.resourceLoader;  
    [resourceLoader setDelegate:delegate  
                          queue:dispatch_queue_create("MyURLDelegate loader", nil)];  
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];  
    AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];  
    AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];  
    controller.player = player;  
    [player play];  
    [self presentViewController:controller animated:false completion:^{}];  

With the above, I cannot see any methods are triggered in the delegate if url has the scheme of "https". What am I missing to allow the delegate to do custom URL loading for "https" URLs ?

Thanks

user1783732
  • 1,599
  • 5
  • 22
  • 44
  • You can assess whether the `delegate` of `AVAssetDownloadURLSession.shared` is being called when you load the item. Also, you kinda want the `AVAssetResourceLoader` with completely private network handling to load the assets via custom `NSURLProtocol` - does not sound like a good design. Can you implement streaming fully on `AVAssetDownloadURLSession` by yourself? – Eugene Dudnyk Feb 12 '20 at 00:57
  • @EugeneDudnyk From Apple's doc (https://developer.apple.com/documentation/avfoundation/avassetdownloadurlsession?language=objc), I cannot find a `shared` instance of `AVAssetDownloadURLSession` (am using obj-c). I will look into that more. Regarding your question, yes I am using `Cronet` of `Chromium` that provides a complete network stack (I want to support QUIC). – user1783732 Feb 12 '20 at 04:26
  • `sharedSession` is declared in the superclass, `NSURLSession`. – Eugene Dudnyk Feb 12 '20 at 04:29

1 Answers1

2

Just to follow up with an answer as I solved the problem now. Simply put, AVPlayer and AVAssetResourceLoader ignores the delegate if the URL has scheme of "http" or "https".

Yes, the workaround is to replace the URL scheme with some custom scheme, and replace it back inside the delegate callback when fetching data.

- (BOOL) resourceLoader:(AVAssetResourceLoader *)resourceLoader
shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest

URL string is loadingRequest.request.URL.absoluteString

user1783732
  • 1,599
  • 5
  • 22
  • 44