离线Fairplay AVAssetDownloadTask下载时出现未知错误回调(仅发生在iOS 10.2上)

时间:2017-01-11 04:23:06

标签: ios swift fairplay avassetdownloadtask

我们正在为我们的客户端应用构建离线fairplay内容;我们通过参考HLSCatalog演示应用程序中的Apple示例下载管理器AssetPersistenceManager类来实现该功能。 在AssetPersistenceManager类中有一个函数和两个回调,我想在这里重点介绍,它是

    /
    func downloadStream(for asset: Asset) {
        /
         For the initial download, we ask the URLSession for an AVAssetDownloadTask
         with a minimum bitrate corresponding with one of the lower bitrate variants
         in the asset.
         */
        guard let task = assetDownloadURLSession.makeAssetDownloadTask(asset: asset.urlAsset, assetTitle: asset.name, assetArtworkData: nil, options: [AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: 265000]) else { return }

        /
        task.taskDescription = asset.name

        activeDownloadsMap[task] = asset

        task.resume()

        var userInfo = [String: Any]()
        userInfo[Asset.Keys.name] = asset.name
        userInfo[Asset.Keys.downloadState] = Asset.DownloadState.downloading.rawValue

        NotificationCenter.default.post(name: AssetDownloadStateChangedNotification, object: nil, userInfo:  userInfo)
    }

当它完成下载流程时回调

func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
    let userDefaults = UserDefaults.standard

    /
     This delegate callback should only be used to save the location URL
     somewhere in your application. Any additional work should be done in
     `URLSessionTaskDelegate.urlSession(_:task:didCompleteWithError:)`.
     */
    if let asset = activeDownloadsMap[assetDownloadTask] {

        userDefaults.set(location.relativePath, forKey: asset.name)
    }
}

最后一个是didCompleteWithError回调

func urlSession(_ session: URLSession, task: URLSessionTask,
didCompleteWithError error: Error?)

一切似乎都适用于iOS< 10.2,但在运行最新iOS 10.2的某些设备上测试后,应用程序总是回调到didFinishDownloadTo委托,而只有13-15%完成百分比,之后     didCompleteWithError 被叫,我们收到以下错误

> "=======> completed percent 11.2888760669556" .
> "=======> completed percent 11.44566601233" 
> "=======> completed percent 11.7592459030787"
> "=======> completed percent 12.0728257938275" 
> "=======> completed percent 12.5431956299506" 
> "=======> completed percent 13.0135654660738" 
> "=======> completed percent 13.3271453568226" 
> "=======> completed percent 13.6407252475713" 
> "=======> completed percent 13.9543051383201" 
> "=======> completed percent 14.1110950836945" 
> "=======> completed percent 14.2678850290689" 
> "Error Domain=AVFoundationErrorDomain Code=-11800 \"The operation could not
> be completed\" UserInfo={NSLocalizedDescription=The operation could
> not be completed, NSLocalizedFailureReason=An unknown error occurred
> (-12667)}"

使用代理调试应用程序检查,它指出应用程序在整个接收响应之前关闭连接。

Status
Complete
Failure
Client closed connection before receiving entire response
Response Code
206 Partial Content

只有iOS 10.2出现该错误,在该版本以下的其他操作系统上测试的相同流仍然可以正常工作。 试图找到关于这部分的iOS 10.2的一些更新日志,但我什么都没找到?你们有什么建议吗?

1 个答案:

答案 0 :(得分:3)

经过一周的坚持,我们找到了自己的答案。原来它是iOS 10.0 - 10.1而不是10.2的错误。 30秒后出现错误与DRM错误有关,它发生在iOS 10.2上,AVAssetDownloadTask恢复后,AVAssetResourceLoaderDelegate发出回调,你需要点击服务器密钥才能实现Fairplay DRM内容密钥

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

否则,下载将被强制停止,您将收到如上所述的未知错误。 Apple示例代码HLSCatalog只是非DRM流的演示,他们甚至没有提到演示中所需的内容密钥实现。

我们已经按照他们的样本看似iOS 10.0 - 10.1,AVFoundation没有检查处理下载的内容密钥,这就是为什么当我们第一次实现我们的下载功能时,我们认为内容密钥不是&n下载时需要这样做,这导致我们在iOS 10.2中陷入浪费的一周......