Using NSURLProtocol to cache .ts files?

It was vaguely suggested in https://developer.apple.com/videos/play/wwdc2012-512/ that NSURLProtocol had been used to cache .ts files. However, I haven't found a way yet. On the simulator, things work OK. However, on my iPhone 5S on 9.0, requests for any files will only go through my NSURLProtocol class if they start with an unrecognized URL, like "asdf". So I get my AVPlayerItem to load "asdf://..." for the master and rendition playlist files, and this gets intercepted fine. However, when I try to load a .ts file like this, where it starts off as "asdf://...", after successfully loading the file, I get an AVFoundation error saying "custo m url not redirect". Any ideas how I can use NSURLProtocol to cache files?


For those who don't like the general idea of doing this: I am certainly aware of how HLS is designed to function, and I understand why Apple chose not to include control over .ts file loading in the AVAssetResourceLoaderDelegate class. However, what I'm trying to do is pre-load .ts file chunks in the background while the user before the user gets to the part where they watch videos. One solution would be to kick off the loading, and then wait until it said that it was ready to play. However, this generally requires it to load 3 .ts files, which is more data than I want to load. I also want to prioritize .ts file loading with respect to other loading tasks, so that this pre-loading won't degrade other parts of the app when they need to use the network. Any ideas that would solve this problem without use of NSURLProtocol, such as injecting a .ts file into the file cache that AVFoundation uses, would also be greatly appreciated!

Replies

Also, is there any way to reduce the required number of .ts files to load from 3?

The exact error is


-12881 "CoreMediaErrorDomain" "custom url not redirect" -

Unfortunately I believe that an Apple engineer has already said in the forums that this is intentionally not possible. In response to a similar question, baleighsdad wrote:


We do not support custom URLs for media files. For media files, we only accept custom URLS that return redirects (so you could do ad decisioning, or something like that).


The reason behind this is we want the media stack to fetch the files from the internet; this is the only way that we can make correct stream switching decisions.


https://devforums.apple.com/message/630245#630245


This seems to be confirmed by the error text you see ("custom url not redirect").