我观看了Apple FairPlay入门视频,我读了这段代码:
https://gist.github.com/fousa/5709fb7c84e5b53dbdae508c9cb4fadc
我还从苹果浏览了HLS Catalog,最后一个问题是,我只需要播放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
}
}
最佳答案
您可以尝试更改以下行:player.pause()
到player.play()
同样,我认为在 class 里引用球员应该会有所帮助,就像这样:
var player: AVPlayer?