微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Apple Fairplay 流媒体在解析服务器播放上下文时出错

如何解决Apple Fairplay 流媒体在解析服务器播放上下文时出错

我目前正在尝试在我的 iOS 应用中播放 Fairplay 流媒体。

当我尝试添加流 URL 和证书时,我将 Apple 的 HLSCatalog 作为参考。下面是我的代码

 func resourceLoader(_ resourceLoader: AVAssetResourceLoader,shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
 
    // We first check if a url is set in the manifest.
    guard let url = loadingRequest.request.url else {
        print("Unable to read the url/host data.")
        loadingRequest.finishLoading(with: DRMError.noURLFound)
        return false
    }
    
    // Get the content id. Content id will be stored in the host of the request url
    guard let contentId = url.host,let contentIdData = contentId.data(using: String.Encoding.utf8) else {
        print("Unable to read the content id.")
        loadingRequest.finishLoading(with: DRMError.noContentIdFound)
        return false
    }
    
    // Request SPC data from OS
    var _spcData: Data?
    var _spcError: Error?
    do {
        _spcData = try loadingRequest.streamingContentKeyRequestData(forApp: certificateData,contentIdentifier: contentIdData,options: [AVAssetResourceLoadingRequestStreamingContentKeyRequestRequiresPersistentKey: true as AnyObject])
    } catch {
        _spcError = error
        print("Failed to get stream content key with error: \(error)")
    }

    guard let spcData = _spcData,let dataRequest = loadingRequest.dataRequest else {
        loadingRequest.finishLoading(with: DRMError.noSPCFound(underlyingError: _spcError))
        print("Unable to read the SPC data.")
        return false
    }
    
    let stringBody: String = "spc=\(spcData.base64EncodedString())&assetId=\(contentId)"
    var ckcRequest = URLRequest(url: self.keyServerUrl)
    ckcRequest.httpMethod = "POST"
    ckcRequest.httpBody = stringBody.data(using: String.Encoding.utf8)
    URLSession(configuration: URLSessionConfiguration.default).dataTask(with: ckcRequest) { data,_,error in
        guard let data = data else {
            print("Error in response data in CKC request: \(error)")
            loadingRequest.finishLoading(with: DRMError.unabletoFetchKey(underlyingError: _spcError))
            return
        }
        // The CKC is correctly returned and is Now send to the `AVPlayer` instance so we
        // can continue to play the stream.
        
        var ckcString = String(data: data,encoding: .utf8)!
        if ckcString.prefix(5) == "<ckc>" {
            let start = ckcString.index(ckcString.startIndex,offsetBy: 5)
            let end = ckcString.index(ckcString.index(before: ckcString.endindex),offsetBy: -5)
            let range = start..<end
            ckcString = String(ckcString[range])
        }
        guard let ckcData = Data(base64Encoded: ckcString) else {
            print("Can't create base64 encoded data")
            loadingRequest.finishLoading(with: DRMError.cannotEncodeCKCData)
            return
        }
        // If we need non-persistent token,then complete loading
        // dataRequest.respond(with: data)
        // loadingRequest.finishLoading()
                                                                                           
        // If we need persistent token,then it is time to add persistence option
        var persistentKeyData: Data?
        do {
            persistentKeyData = try loadingRequest.persistentContentKey(fromKeyvendorResponse: ckcData,options: nil)
        } catch {
            print("Failed to get persistent key with error: \(error)")
            loadingRequest.finishLoading(with: DRMError.unabletoGeneratePersistentKey)
            return
        }
        // set type of the key
        loadingRequest.contentinformationRequest?.contentType = AVStreamingKeyDeliveryPersistentContentKeyType
        dataRequest.respond(with: persistentKeyData!)
        loadingRequest.finishLoading()
    
    }.resume()
    return true
}

当我点击 URL 请求并尝试获取内容键时,它给了我以下错误

“FPS 错误代码 = -42581 | 解析服务器播放上下文以检索 assetId 和 HU 时出错。”

流可以通过我们的门户在苹果的 safari 浏览器中播放,所以我认为流不是问题,但我的实现缺少一些步骤。

提前致谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?