Azure Media Service Fairplay DRM AVPlayer swift implementation - ios

I am trying to play a Fairplay DRM protected (encrypted through Azure Media Services) HLS video stream on iOS Device.
I have used the code and process described in the following links:
https://icapps.com/blog/how-integrate-basic-hls-stream-fairplay
https://gist.github.com/fousa/5709fb7c84e5b53dbdae508c9cb4fadc
Following is the code I have written for this.
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAssetResourceLoaderDelegate {
#IBOutlet weak var videoView: UIView!
var player: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
let streamURL = "someexampleurl.com/stream.m3u8"
if let url = URL(string: streamURL) {
//2. Create AVPlayer object
let asset = AVURLAsset(url: url)
let queue = DispatchQueue(label: "Some queue")
asset.resourceLoader.setDelegate(self, queue: queue)
let playerItem = AVPlayerItem(asset: asset)
player = AVPlayer(playerItem: playerItem)
//3. Create AVPlayerLayer object
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.videoView.bounds //bounds of the view in which AVPlayer should be displayed
playerLayer.videoGravity = .resizeAspect
//4. Add playerLayer to view's layer
self.videoView.layer.addSublayer(playerLayer)
//5. Play Video
player.play()
}
// Do any additional setup after loading the view.
}
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("🔑", #function, "Unable to read the url/host data.")
loadingRequest.finishLoading(with: NSError(domain: "com.error", code: -1, userInfo:
nil))
return false
}
print("🔑", #function, url)
// When the url is correctly found we try to load the certificate date. Watch out! For this
// example the certificate resides inside the bundle. But it should be preferably fetched from
// the server.
guard
let certificateURL = Bundle.main.url(forResource: "certfps", withExtension: "cer"),
let certificateData = try? Data(contentsOf: certificateURL) else {
print("🔑", #function, "Unable to read the certificate data.")
loadingRequest.finishLoading(with: NSError(domain: "com.error", code: -2, userInfo: nil))
return false
}
// Request the Server Playback Context.
let contentId = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
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: "com.error", code: -3, userInfo: nil))
print("🔑", #function, "Unable to read the SPC data.")
return false
}
// Request the Content Key Context from the Key Server Module.
let ckcURL = URL(string: "https://xxxxx.keydelivery.northeurope.media.azure.net/FairPlay/?kid=xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")!
var request = URLRequest(url: ckcURL)
request.httpMethod = "POST"
let assetIDString = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
let postString = "spc=\(spcData.base64EncodedString())&assetId=\(assetIDString)"
request.setValue(String(postString.count), forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpBody = postString.data(using: .ascii, allowLossyConversion: true)
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request) { data, response, error in
if let data = data {
// The CKC is correctly returned and is now send to the `AVPlayer` instance so we
// can continue to play the stream.
if var responseString = String(data: data, encoding: .utf8) {
responseString = responseString.replacingOccurrences(of: "<ckc>", with: "").replacingOccurrences(of: "</ckc>", with: "")
var ckcData = Data(base64Encoded: responseString)!
dataRequest.respond(with: ckcData)
loadingRequest.finishLoading()
} else {
// print("Error encountered while fetching FairPlay license for URL: \(self.drmUrl), \(error?.localizedDescription ?? "Unknown error")")
}
task.resume()
return true
}
}
Everything above works but in the CKC response I get
{
"Error": {
"Message": "Failed content key policy evaluation.",
"Code": "AuthorizationPolicyEvaluationFailure"
}
}
Can anyone please here let me know what I am missing here, this is my first time trying this out
so I could be making a very obvious mistake so please bear with that.
Any help regarding this would be really great (I have been hitting my head on this for multiple days now.)
Thanks.

One thing that will probably help with troubleshooting is to enable the license delivery logging. You can do this in the Azure portal by going to your Media Services account, in the Monitoring section go to Diagnostic settings. Click 'Add diagnostic setting'. Give the setting a name and then, at least initially, tell it to archive to a storage account. Log the 'KeyDeliveryRequests'. Once you save this reproduce the issue. Then go to your Storage account and look for the log result. The Storage container ‘insights-logs-keydeliveryrequests’ will contain the logs.

you can add request header parameter like "authorization" (probably a base 64 token called JWT), "mimetype" in making CKC request, it would work.

Finally, I figured the thing I was missing was not passing the JWT in the "Authorization" header for the CKC request.
Passing the JWT did the trick. :)
Note: JWT stands for the JSON web token generated during the media encryption in azure media services.

Related

AVPlayer playing wrong video file

I am having a weird situation and have no clue how to handle this , I am downloading the videos from firestorage and caching into device for future use , meanwhile the background thread is already doing its job , I am passing a video url to the function to play the video. The issue is that sometimes avplayer is playing the right video and sometimes taking some other video url from the cache.
you can find the code in below :
func cacheVideo(for exercise: Exercise) {
print(exercise.imageFileName)
guard let filePath = filePathURL(for: exercise.imageFileName) else { return }
if fileManager.fileExists(atPath: filePath.path) {
// print("already exists")
} else {
exercise.loadRealURL { (url) in
print(url)
self.getFileWith(with: url, saveTo: filePath)
}
}
}
writing file here
func getFileWith(with url: URL, saveTo saveFilePathURL: URL) {
DispatchQueue.global(qos: .background).async {
print(saveFilePathURL.path)
if let videoData = NSData(contentsOf: url) {
videoData.write(to: saveFilePathURL, atomically: true)
DispatchQueue.main.async {
// print("downloaded")
}
} else {
DispatchQueue.main.async {
let error = NSError(domain: "SomeErrorDomain", code: -2001 /* some error code */, userInfo: ["description": "Can't download video"])
print(error.debugDescription)
}
}
}
}
now playing the video using this
func startPlayingVideoOnDemand(url : URL) {
activityIndicatorView.startAnimating()
activityIndicatorView.isHidden = false
print(url)
let cachingPlayerItem = CachingPlayerItem(url: url)
cachingPlayerItem.delegate = self
cachingPlayerItem.download()
// cachingPlayerItem.preferredPeakBitRate = 0
let avasset = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: avasset)
let player = AVPlayer(playerItem: playerItem)
player.automaticallyWaitsToMinimizeStalling = false
initializeVideoLayer(for: player)
}
any suggestions would be highly appreciated.
this was solved because the data model which i was using to download bunch of videos files was accessed in background thread and meanwhile i was trying to assign the url to the same data model class in order to fetch the video and play in avplayer. Hence this was the issue and resolved by simply adding a new attribute into data model for assigning the url to play right away.

Can we manually download/cache the transport stream while streaming m3u8 file

I am trying to play transport stream from m3u8 file. My requirement is to process the downloaded data before giving it to AVPlayer. For that I am using a proxy server[GCDWebserver] to intercept all the request. In proxy server I will download the data process it and feed it back.
I was able to download the media file and also have tried returning data using GCDWebServerDataResponse(data: apiData, contentType: apiResponse.mimeType ?? ""). But player is not playing the media content.
I am using GCDWebserver as my proxy server.
I have created an instance of AVPlayerItem with the following url
http://34.55.7.151:8080/
which is actually my local servers ip & port number.
/// Initialise AVPlayer Item
let url = URL(string: "http://10.155.177.151:8080/")!
let playerItem = AVPlayerItem(url: url)
player = AVPlayer(playerItem: playerItem)
player.automaticallyWaitsToMinimizeStalling = false
let playerViewController = AVPlayerViewController()
playerViewController.player = player
DispatchQueue.main.async {
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
/// Http proxy handler
webServer.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self) { (request, completion) in
let mediaUrl = URL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!
if self.session == nil {
self.session = URLSession(configuration: URLSessionConfiguration.default)
}
let dataTask = self.session?.dataTask(with: mediaUrl, completionHandler: { (data, response, error) in
if let apiData = data, let apiResponse = response {
completion(GCDWebServerDataResponse(data: apiData, contentType: apiResponse.mimeType ?? ""))
} else {
completion(GCDWebServerDataResponse(text: "Error"))
}
})
dataTask!.resume()
}
Something similar has been implemented here: https://github.com/StyleShare/HLSCachingReverseProxyServer
It downloads the segments while streaming, if the segment is available locally, it will use that instead.

NSURLErrorDomain Code=-2000 "can’t load from network"

My app started accidentally with an error that I never had before and I can't find any solution around the net. I think it has nothing to do with my code but if it helps, here it is:
class InterfaceController: WKInterfaceController {
#IBOutlet var tableView: WKInterfaceTable!
final let url = URL(string: "http://...")
private var tasks = [Task]()
override func awake(withContext context: Any?) {
super.awake(withContext: context)
downloadJson()
}
func downloadJson() {
guard let downloadURL = url else { return }
URLSession.shared.dataTask(with: downloadURL) { data, urlResponse, error in
guard let data = data, error == nil, urlResponse != nil else {
print("something is wrong")
return
}
do
{
let decoder = JSONDecoder()
let downloadedTasks = try decoder.decode(Tasks.self, from: data)
self.tasks = downloadedTasks.tasks
print(self.tasks)
} catch {
print("somehting went wrong after downloading")
}
}.resume()
}
}
The error message I'm getting in the console is:
2018-11-07 21:34:15.538369+0100 BJwatch WatchKit Extension[1884:84116] Task <82BE34C9-CCAB-4076-8012-CC9FF61AE556>.<1> load failed with error Error Domain=NSURLErrorDomain Code=-2000 "can’t load from network" UserInfo={NSLocalizedDescription=can’t load from network, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <82BE34C9-CCAB-4076-8012-CC9FF61AE556>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <82BE34C9-CCAB-4076-8012-CC9FF61AE556>.<1>"
), NSErrorFailingURLStringKey=http://..., _kCFNetworkErrorConditionalRequestKey=<CFMutableURLRequest 0x7c09bc70 [0x34f528c]> {url = http://..., cs = 0x0}, _kCFNetworkErrorCachedResponseKey=<CFCachedURLResponse 0x7afc2840 [0x34f528c]>, NSUnderlyingError=0x7c1eb930 {Error Domain=kCFErrorDomainCFNetwork Code=-2000 "(null)" UserInfo={_kCFNetworkErrorCachedResponseKey=<CFCachedURLResponse 0x7afc2840 [0x34f528c]>, _kCFNetworkErrorConditionalRequestKey=<CFMutableURLRequest 0x7c09bc70 [0x34f528c]> {url = http://..., cs = 0x0}}}, NSErrorFailingURLKey=http://...} [-2000]
[BJwatch_WatchKit_Extension.Task, BJwatch_WatchKit_Extension.Task, BJwatch_WatchKit_Extension.Task, BJwatch_WatchKit_Extension.Task]
The URL is not "http://..." in the real app. It is a URL that gives a JSON array and it is working.
NSURLErrorCannotLoadFromNetwork
This error is sent when the task needs to load from the network, but is blocked from doing so by the “load only from cache” directive.
The default policy is NSURLRequest.CachePolicy.useProtocolCachePolicy
useProtocolCachePolicy: Use the caching logic defined in the protocol implementation, if any, for a particular URL load request.
Important: If you are making HTTP or HTTPS byte-range requests, always use the NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData policy instead.
var request = URLRequest(url: URL(string:"http://...")!)
request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
URLSession.shared.dataTask(with: request) {...
Correct answer to fix this is to change URLSessionConfiguration. If anyone still want to use .useProtocolCachePolicy policy, use background configuration.
let configuration = URLSessionConfiguration.background(withIdentifier: "xxx.xxx.xxxxx")
let session = URLSession(configuration: configuration)
Below is what I get from Apple's support.
Watch apps tend to be suspended very quickly so we recommend that developers use a background url session to ensure their api calls are still performed should an event such as backgrounding or suspension occur.

Fairplay implementation in Swift, AVAssetResourceDelegate

I watched Apple FairPlay introduction videos, I read a this code:
https://gist.github.com/fousa/5709fb7c84e5b53dbdae508c9cb4fadc
And I also went through HLS Catalog from apple and with the last the problem is that I need only playing DRM videos without any downloading and all this stuff so I started from GitHub example.
I have a certificate, videos in FairPlay and the key server module.
My first and main problem is that AVResourceDelegate isn't calling when I'm giving AVURLAsset with video url. I read at stack that I need to change scheme to sth else, e.g "DRM" from https and right AVResourceDelegate calling then but I don't have .m3u8 file because video link is wrong!
Could you please guys/girls help me.
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
}
}
You can try changing the line:
player.pause() to player.play()
Also I think keeping a reference to the player in your class should help, like so:
var player: AVPlayer?

Video Streaming Fails when using AVAssetResourceLoader

We are using video streaming in our Swift IOS application and it works very well. The problem is that we would like to use AVAssetResourceLoader so we can make the requests to the streaming server using our own URLSession rather than whatever AVPlayer uses (I have been completely unable to find out what session AVPlayer uses or how to influence what session it uses.)
The exact behavior is that shouldWaitForLoadingOfRequestedResource is called once for two bytes on on the .m3u8 file then it is called again asking for the whole file, and then (based on the start time) the correct .ts file is requested. After we fetch the .ts file the video player simply doesn't do anything further.
This happens whether or not we use a sample streaming video file "https://tungsten.aaplimg.com/VOD/bipbop_adv_example_v2/master.m3u8" or our own server. The sequence is identical if we don't use AVAssetResourceLoader (we can tell from our server.) right up until the .ts file is requested. At that point when we don't use the custom loader the AvPlayer brings up the video and keeps requesting .ts files. If we comment out all other interactions with the AVPlayer including setting the initial time the behavior is identical so I am only going to include the code from viewDidLoad and shouldWaitForLoadingOfRequestedResource.
Again if we simply remove the "xyzzy" prefix so that AVAssetResourceLoader isn't used, everything works. Also, I guess importantly, if we target a video file that is not a streaming file everything works either way.
One more thing our transformation of the mime type for the .ts file produced some kind of weird dynamic uti, but this doesn't seem to have anything to do with the problem because even if we hardcode the uti the same thing happens.
override func viewDidLoad() {
super.viewDidLoad()
avPlayer = AVPlayer()
avPlayerLayer = AVPlayerLayer(player: avPlayer)
videoView.layer.insertSublayer(avPlayerLayer, at: 0)
videoView.backgroundColor = UIColor.black
url = URL(string: "xyzzy" + currentPatient.videoURL())!
let asset = AVURLAsset(url: url)
asset.resourceLoader.setDelegate(self, queue: DispatchQueue.main)
let item = AVPlayerItem(asset: asset)
let avPlayerItem = item
avPlayer.replaceCurrentItem(with: avPlayerItem)
videoScrollView.delegate = self
videoScrollView.minimumZoomScale = 1.0
videoScrollView.maximumZoomScale = 6.0
}
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
let urlString = loadingRequest.request.url?.absoluteString
let urlComponents = urlString?.components(separatedBy: "xyzzy")
let url = URL(string: urlComponents![1])
let request = loadingRequest.dataRequest!
let infoRequest = loadingRequest.contentInformationRequest
let task = globalSession.dataTask(with: url!) { (data, response, error) in
self.avPlayerThread.async {
if error == nil && data != nil {
let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, response?.mimeType as! CFString, nil)
if let infoRequest = infoRequest {
infoRequest.contentType = uti?.takeRetainedValue() as? String
if request.requestsAllDataToEndOfResource == false {
infoRequest.contentLength = Int64(request.requestedLength)
} else {
infoRequest.contentLength = Int64((data?.count)!)
}
infoRequest.isByteRangeAccessSupported = true
}
if infoRequest == nil || request.requestsAllDataToEndOfResource == true {
loadingRequest.dataRequest?.respond(with: data!)
}
loadingRequest.finishLoading()
} else {
print ("error \(error)")
loadingRequest.finishLoading(with: error)
}
}
}
task.resume()
return true
}

Resources