Fatal Exception: NSInternalInconsistencyException AVAsset failed to associate with AVContentKeySession

Hello,


I am using FairPlay streaming to stream encrypted HLS content to my app. Here is the code I use to playback content:


func load(url: URL) {
    if let currentAsset = self.avPlayer.currentItem?.asset as? AVURLAsset {
        self.contentKeySessionManager?.removeContentKeyRecipient(recipient: currentAsset)
    }
    self.urlAssetObserver?.invalidate()

    let urlAsset = AVURLAsset(url: url)
    self.contentKeySessionManager?.addContentKeyRecipient(recipient: urlAsset)

    let playerItem = AVPlayerItem(asset: urlAsset)

    self.urlAssetObserver = urlAsset.observe(\AVURLAsset.isPlayable, options: [.new, .initial]) { [weak self] (urlAsset, _) in
        guard let strongSelf = self, urlAsset.isPlayable == true else { return }

        strongSelf.avPlayer.replaceCurrentItem(with: playerItem)
        if(playerItem.status == .readyToPlay) {
            strongSelf.internalState = .Paused
        }
    }
}

I am using AVContentKeySession to handle getting the ckc. However, my crash reporting sends me the following crash report:


Fatal Exception: NSInternalInconsistencyException

AVAsset failed to associate with AVContentKeySession


This happens out in the wild. It crashes on line 8 above.

self.contentKeySessionManager?.addContentKeyRecipient(recipient: urlAsset)


I can't find any documentation on this crash. Does anyone know what would cause it?


Thanks

Replies

Hi,


Have you set an instance of AVContentKeySessionDelegate that responds to :

contentKeySession:didProvidePersistableContentKeyRequest:
.


As per the documentation, an NSInternalConsistencyException can be thrown if you call - respondByRequestingPersistableContentKeyRequest in your workflow and do not implement the above method in your receiver...

Thank you so much for the reply.


I have an object which contains my AVContentKeySession (ContentKeySessionManager) and the delegate is set in the init.


func contentKeySession(_ session: AVContentKeySession, didProvide keyRequest: AVPersistableContentKeyRequest)


So i'm confident that function is implemented correctly. I haven't been able to actually replicate this behavior myself. For my experience the playback is fine. It's happening sometimes to live users (getting this crash report back). It's a shame the error isn't any more detailed.
Any other ideas why NSInternalConsistencyException could be thrown? Where do I look in the documentation? I've been unable to find this.


Thanks again

SerioSequax, did you ever find out what the problem was?

Thanks for checking in. Frustratingly no 😟. We've also just had a big spike in this happening on iOS 11. Not been able to replicate it, just can see that it's happening out in the wild. Shame the error message is so uninformative.

We noticed the same issue and found out the reason is that the AVAsset has already downloaded the playlist when add it to ContentKeySessionManager. Not sure if it's case for you.

Thanks for your reply! How did you know this was the case? Also what was your fix for it? I add the AVAsset as a content key reciepient on the line after creating the AVAsset.