neekeetab / CachingPlayerItem

Play and cache media content simultaneously on your iOS device
MIT License
520 stars 89 forks source link

Caching response after the download complete #17

Closed Gruppio closed 1 year ago

Gruppio commented 5 years ago

Hi, First of all thanks for your library, is really useful and easy to use! We have discovered that when using the URL initializer the response was never cached and the video was downloaded again each time.

This is probably caused by an Apple Bug referenced here: https://forums.developer.apple.com/thread/92119

This Bug says that URLCache, that is the object responsible for cache the URLSession responses, does not work with range requests that are the one used to reproduce videos.

We've modified your library so the response id cached manually after the download has completed, in this way for the URLCache it is a normal response and the next time the video is played it will not be downloaded again. We had also modified the initializer so it is possible to provide a custom URLSessionConfiguration that is the object that contains the URLCache used, so it is possible to provide an URLCache with custom sizes for example.

Thank You!

Gruppio

neekeetab commented 5 years ago

Hey,

I'm glad that it's been useful to you.

CachingPlayerItem doesn't perform caching by itself, instead, it delegates this responsibility outside. It's up to a user how they would store loaded data and I think this should stay this way mainly because it's simpler and more flexible. Therefore I'm not sure that an additional caching layer in the form of the URLCache is something that should be added. But I'm open to discussion.

Gruppio commented 5 years ago

Hi,

The modification will not cache everything, it will just honour the cache response from the server. If the server responds to do not cache the response, the URLCache will not cache it. If the server say to cache the video for one hour the URLCache will do that. Instead without this modification if the server say to cache a video for one hour nothing was happening. This behaviour is the default for all the data task performed with a URLSession, but because of the bug linked above this was not true for the range request used for the videos. The Cache rules are fully customizable anyway from the URLSessionConfiguration that is provided in the initializer :-)

neekeetab commented 5 years ago

I see your point.

I assume that developers that decide to go with CachingPlayerItem generally want to store the downloaded data somewhere on disk anyway (otherwise they would use AVPlayerItem, instead), so URLCaching will perform unnecessary additional caching in this case.

Raserad commented 5 years ago

I think, that it may help you https://andreygordeev.com/2018/03/31/cache-avplayeritem/

smhk commented 4 years ago

1) I really agree with @neekeetab that CachingPlayerItem is a fantastic class that should stay SIMPLE. It's amazing like this!

2) It would be great to see some sample code that just shows how to save to disk

3) There is this example, andreygordeev.com/2018/03/31/cache-avplayeritem/ but unfortunately it uses an additional library ("Cache") to do the caching...

As I say, it would be great to have a simple example somewhere of how to just cache to disk simply with no external library! Cheers

Again this is an awesome class just as it is, thanks!