首页 > 解决方案 > 我尝试将 Apple 示例代码(FairPlay Streaming Server SDK v4.2.0)与 Fairplay 在线播放一起使用,但它不起作用

问题描述

我尝试使用 HLSDatalogWithFPS - AVContentKeySession 苹果示例代码播放公平播放视频。我只是修改 2 func "func requestApplicationCertificate() throws -> Data" 和 "func requestContentKeyFromKeySecurityModule(spcData: Data, assetID: String) throws -> Data" 并在 stream.plist 上添加视频 url

func requestApplicationCertificate() throws -> Data {

    let certificateURL = Bundle.main.url(forResource: "fairplay", withExtension: "cer")

    let applicationCertificate: Data? = try? Data(contentsOf: certificateURL!)

    return applicationCertificate!

}



func requestContentKeyFromKeySecurityModule(spcData: Data, assetID: String) throws -> Data {
    // MARK: ADAPT - You must implement this method to request a CKC from your KSM.

    var ckcData: Data? = nil

    let url = URL(string: "http://drmlab.ott.hinet.net:8064/fpsa/v1.0/?deviceId=NDJhNjQ1MmQtZGFkZC0zNjE3LTllOTUtMmNlNWVlMzYwZmRi")!

    var request = URLRequest(url: url)

    let postString = "spc=\(spcData.base64EncodedString())&assetId=\(assetID)"

    let postData = postString.data(using: .utf8, allowLossyConversion: true)       

    request.httpMethod = "POST"

    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

    request.setValue(String(postData!.count), forHTTPHeaderField: "Content-Length")

    request.httpBody = postData

    let session = URLSession.shared

    let semaphore = DispatchSemaphore(value: 0)    

    let task = session.dataTask(with: request) { (data, response, error) in

        guard let data = data, error == nil else {

            print("error=\(error)")

            return

        }      

        print("response = \(response)")       

        let responseData = String(data: data, encoding: .utf8)

        print("responseData = \(responseData)")

        print("ckc = \(ckcData = Data(base64Encoded: responseData!))")

        semaphore.signal()

    }
       task.resume()

    _ = semaphore.wait(timeout: DispatchTime.distantFuture)

    return ckcData!

}

我收到错误消息,我该如何解决?

在此处输入图像描述

标签: sdkstreamingfairplay

解决方案


我认为您应该尝试创建 base64 编码的 url 安全 src 数据。

let base64spc = spcData.base64EncodedString().trimmingCharacters(in: .whitespacesAndNewlines) let base64UrlSafe = base64spc.replacingOccurrences(of: "+", with: "-").replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: "=", with: "") let postString = "spc=\(base64UrlSafe.base64EncodedString())&assetId=\(assetID)"


推荐阅读