Playing Offline HLS with AES-128 encryption iOS

Viewed 5541

I want to integrate offline HLS in iOS through AVFoundation. I have an encrypted HLS with simple AES-128 and it doesn't want to play in offline mode, I was trying to integrate AVAssetResourceLoaderDelegate, but don't know how to integrate applicationCertificate and contentKeyFromKeyServerModuleWithSPCData that are in https://developer.apple.com/streaming/fps/ examples. I have a feeling that I am doing something wrong. It is a sample AES-128 encryption, not even DRM.

Without the internet, AVPlayer is still trying to get encryption key through GET request. It would be great if someone succeeded to save the encrypted key locally and somehow gave it to AVPlayer together with AVURLAsset.

Did someone manage to integrate this?

2 Answers

@Cyklet Answer helped me a ton! Thanks.

There is however an additional step I had to do to get hls streaming to work.


When using shouldWaitForLoadingOfRequestedResource apples documentation states:

contentType

Before finishing loading an AVAssetResourceLoadingRequest instance, if its contentInformationRequest property is not nil, set the value of this property to a UTI indicating the type of data contained by the requested resource.

When trying to implement HLS Streaming there are two UTIs that may be used (as far as I know...).

  • AVStreamingKeyDeliveryPersistentContentKeyType

    • "com.apple.streamingkeydelivery.persistentcontentkey"
  • AVStreamingKeyDeliveryContentKeyType

    • "com.apple.streamingkeydelivery.contentkey"

Check allowedContentTypes what UTI to use. See possible implementation below:

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {

....
   var contentType = AVStreamingKeyDeliveryPersistentContentKeyType
            
   if let allowedContentType = contentInformationRequest.allowedContentTypes?.first{
       if allowedContentType == AVStreamingKeyDeliveryContentKeyType{
           contentType = AVStreamingKeyDeliveryContentKeyType
       }
    }
....
}
Related