8
votes

I'm currently trying to implement download of an encrypted HLS stream to an iOS device. iOS 10 seems to support that out of the box (see https://developer.apple.com/videos/play/wwdc2016/504), but I can't get it to work. I am basically using the AssetPersistenceManager from the Apple sample code (https://developer.apple.com/library/content/samplecode/HLSCatalog/Introduction/Intro.html), which calls makeAssetDownloadTask of AVAssetDownloadURLSession. It works fine in online mode, but the playback fails when playing the asset from the stored file.

Now the WWDC video indicates that you should use AVAssetResourceLoaderDelegate to provide an offline encryption key. As http and https don't seem to be supported by the AVAssetResourceLoader (see e.g. AVAssetResourceLoaderDelegate methods not working on device), I tried using a custom scheme for the key URI and provide a local aes key in shouldWaitForLoadingOfRequestedResource of AVAssetResourceLoaderDelegate. Here it also works when streaming online, but no matter whether I'm using the remote key URL or override it with a local key, AVPlayer always fails playing the AVAsset offline.

Does anyone have experience with downloading encrypted HLS or has a working example on how to provide an encryption key for playing a local encrypted AVURLAsset offline?

The sample HSL file I'm using is here:http://demo.unified-streaming.com/video/tears-of-steel/tears-of-steel-aes.ism/.m3u8

2
Thx for the feedback, I edited the question to include all the new information.Micky
I also have the same problem but in my case HLS video plays fine in offline mode. If I try to play it in online mode it just loads and stops without any error.Martin
did you find solution for this, downloading encrypted video in offline and playing. If you have sample code, can you please provide.Anjaneyulu Battula
@AnjaneyuluBattula No, sorry, we went for not using hls for local storage.Micky

2 Answers

1
votes

First, you need to download the key and save it.

Second, after you finish downloaded the HLS, you will get a folder name like "XXXXXX.movpkg". In the folder, there are two file you need to modified.

  1. XXXXX.m3u8
  2. PlayListXXXXXXXXXX.data

Inside both of files, there is a line

#EXT-X-KEY:METHOD=AES-128,URI="XXXXXX",IV=XXXXXXXXX

replace the URI with your local key location URL

That's it! Now you can get the key from local and play it normally.

0
votes

May be it will help someone,

To download and play a HLS video offline you need to follow following steps,

  1. Use a local server like GCDServer to host m3u8 file post downloading.
  2. Edit m3u8 file for downloaded .ts segment if it is not mentioned there.
  3. use GCDServer local host url to host and play m3u8 file.

Am using it in a working app, So you can use this approach.