Swift,AVAssetResourceDelegate中的Fairplay实现

时间:2018-01-19 12:11:44

标签: ios swift video avplayer fairplay

我看过Apple FairPlay介绍视频,我读了这段代码: https://gist.github.com/fousa/5709fb7c84e5b53dbdae508c9cb4fadc 我还从苹果公司看过HLS目录,最后一个问题是我只需要播放DRM视频而不需要任何下载和所有这些东西,所以我从GitHub示例开始。 我有证书,FairPlay中的视频和密钥服务器模块。 我的第一个主要问题是,当我给AVURLAsset提供视频网址时,AVResourceDelegate没有调用。我在堆栈上读到我需要将方案更改为其他方式,例如来自https的“DRM”和右侧AVResourceDelegate调用,但我没有.m3u8文件,因为视频链接错误! 你能不能请男/女帮助我。

import Foundation
import AVKit
import NotificationCenter

public struct DRMVideoData{
   var drmKey: String?
   var proxyFairPlay: String
   var fileFairPlay: String
   var idVideo: String
}

class VODDRMImplementation: NSObject, AVAssetResourceLoaderDelegate {

let domain = "DRMDelegate.ContentKeyQueue"
let contentKeyDelegateQueue = DispatchQueue(label: "DRMDelegate.ContentKeyQueue")

var drmData: DRMVideoData?

func startPlayerWithDRM(_ videoDRM: DRMVideoData,_ player: AVPlayer?,_ playerLayer: AVPlayerLayer?, c: @escaping (AVPlayer?, AVPlayerLayer?) -> Void) {

    var urlcomp = URLComponents(string: videoDRM.fileFairPlay)

    urlcomp?.scheme = "drm"

    if let url = try? urlcomp?.asURL(){

        self.drmData = videoDRM

        let url = url

        let asset = AVURLAsset(url: url!)
        asset.resourceLoader.setDelegate(self, queue: self.contentKeyDelegateQueue)

        let playerItem = AVPlayerItem(asset: asset)
        let player = AVPlayer(playerItem: playerItem)
        let playerLayer = AVPlayerLayer(player: player)
        player.pause()

        c(player, playerLayer)
    }else{
        c(nil, nil)
    }

    }

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
    log.debug("DRM: started")

    // getting data for KSM server
    guard let urlToConvert = loadingRequest.request.url,
        let drmData = drmData else {
            log.debug("DRM: unable to read URL from loadingRequest")
            loadingRequest.finishLoading(with: NSError(domain: domain, code: -1, userInfo: nil))
            return false
    }

    do{

        log.debug("DRM: video link \(urlToVideo)")

        guard let certificateData = getCertificateFromServer() else {
                log.debug("DRM: false to get public certificate")
                loadingRequest.finishLoading(with: NSError(domain: domain, code: -3, userInfo: nil))
                return false
        }

        let contentId = drmData.idVideo // content id
        guard let contentIdData = contentId.data(using: String.Encoding.utf8),
            let spcData = try? loadingRequest.streamingContentKeyRequestData(forApp: certificateData, contentIdentifier: contentIdData, options: nil),
            let dataRequest = loadingRequest.dataRequest else {
                loadingRequest.finishLoading(with: NSError(domain: domain, code: -3, userInfo: nil))
                log.debug("DRM: false to get SPC Data from video")
                return false
        }

        let ksmServer = URL(string: drmData.proxyFairPlay)! // KSM link
        var request = URLRequest(url: ksmServer)
        request.httpMethod = "GET"
        request.httpBody = spcData
        let session = URLSession(configuration: .default)
        let task = session.dataTask(with: request) { data, response, error in
            guard let data = data else {
                log.debug("DRM: unable to fetch ckc key :/")
                loadingRequest.finishLoading(with: NSError(domain: self.domain, code: -4, userInfo: nil))
                return
            }
            dataRequest.respond(with: data)
            loadingRequest.finishLoading()
        }
        task.resume()

    }catch{
        loadingRequest.finishLoading(with: NSError(domain: domain, code: -3, userInfo: nil))
        log.debug("DRM: cannot generate url to video")
        return false
    }


    return true


}

func takeURLFromId(_ videoLink: String) -> URL{
    let urlString = videoLink
    let url = URLComponents(string: urlString)
    do{
        let urlToReturn = try url?.asURL()
        guard let urlToReturn2 = urlToReturn else {
            let error = NSError(domain: domain, code: 0, userInfo: nil)
            throw error }
        return urlToReturn2
    }catch{
        if let url = NSURL(string: videoLink){
            return url as URL
        }else{
            return NSURL(string: videoLink)! as URL
        }
    }

}

func getCertificateFromServer() -> Data?{
    let filePath = Bundle.main.path(forResource: "privatekey", ofType: "pem")

    guard let data = try? Data(contentsOf: URL(string: filePath!)!) else {
        return nil
    }

    return data
}

}

1 个答案:

答案 0 :(得分:0)

您可以尝试更改行:

select concat(cast(year(cast('2018-12-04' as date)) as varchar(4)),'-', cast(month(cast('2018-12-04' as date)) as varchar(2)), '-', cast(day(cast('2018-12-04' as date)) as varchar(2))) from tablename player.pause()

另外,我认为在班级中引用球员应该会有所帮助,例如:

player.play()