NSURLProtocol and AVPlayer

Looks like AVPlayer's network calls does not go through URL loading system. So subclass of NSURLProtocol is not able to catch the calls.


Eskimo, can you please confirm above?


And how can I intercept the network requests from AVPlayer besides using a local proxy?


Thanks!

Looks like AVPlayer's network calls does not go through URL loading system. So subclass of NSURLProtocol is not able to catch the calls.

Correct. Well, it does go through the URL loading system, but those requests are made in a helper process (

mediaserverd
) and thus don’t ‘see’ your NSURLProtocol subclass.

And how can I intercept the network requests from AVPlayer besides using a local proxy?

There’s no way to do this per se, but it may be possible to craft an alternative approach. What’s your high-level goạl here?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Sorry to reply so late, Eskimo.


Well, the high level goal is to intercept all network traffic (main concern is video streaming) from our app and redirect or force them to go through a proxy.


We are able to catch most of the traffic. However, for request using low level api (CFStream/CFSocket, for example from a 3rd party library), we still have not found a way to do that.


Pls advise.


Thanks,

Rao

However, for request using low level api (CFStream/CFSocket, for example from a 3rd party library), we still have not found a way to do that.

CFSocketStream can be configured to go through a proxy via the various properties in

<CFNetwork/CFSocketStream.h>
. CFSocket has no proxy support; it’s sufficiently low-level that, if you want proxy support, you’ll have to implement it yourself.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you, Eskimo!

Will look into that ..

I found this old thread when looking into a similar problem. We're trying to use a third-party network stack (Cronet) which uses [NSURLProtocol registerClass] to handle the URL loading. And we wanted to use it with AVPlayer.


It's interesting that when we test it in Xcode with iOS simulator, it works, i.e. able to catch the network calls of AVPlayer into registered class. But when we run the same code on actual iPhone, it did not work, and debugging showing the registered class not called at all.


Is it still true with the iOS 13 that AVPlayer won't see registered protocol class? If yes, why the iOS simulator works?


Thanks!

Is it still true with the iOS 13 that AVPlayer won't see registered protocol class?

Yes.

Well, no, but also yes. AVFoundation has very specific cases where it goes out of its way to use NSURLProtocol subclasses but that’s just a compatibility measure. There are better ways to do this. More on that below.

If yes, why the iOS simulator works?

Because the simulator is not an emulator. The simulator takes all sorts of shortcuts and, IIRC, one of those is that AV playback runs in process.


Taking a step back, using an NSURLProtocol subclass for this sort of thing is no longer a good option. Rather, AVFoundation has its own custom asset loading mechanism, centred around the AVAssetResourceLoader class. I recommend that you explore that option.

ps I’m not an expert in this technology because it’s handled by other folks here in DTS. I’ve retagged this thread to add AVFoundation in the hope that folks who do know this API might respond to any follow-up questions you have. Failing that, you can always open a DTS tech support incident to get help.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

NSURLProtocol and AVPlayer
 
 
Q