Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 10 download encrypted HLS stream

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

like image 569
Micky Avatar asked Jun 07 '17 15:06

Micky


1 Answers

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.

like image 92
Jing Chuang Avatar answered Nov 01 '22 09:11

Jing Chuang