I am playing the protected HLS streams and the authorization token expires in 5 minutes,I am trying to achieve this with 'AVAssetResourceLoaderDelegate' and I'm getting an error 401 Unauthorized.
The question is how to update the token inside asset? I've already tried to change it in resourceLoader loadingRequest.allHTTPHeaderFields but it is not working:
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
guard let url = loadingRequest.request.url else {
loadingRequest.finishLoading(with: NSError(domain: "Invalid URL", code: -1, userInfo: nil))
return false
}
// Create a URLRequest with the initial token
var request = URLRequest(url: url)
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.allHTTPHeaderFields = loadingRequest.request.allHTTPHeaderFields
// Perform the request
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("Error performing request: \(error.localizedDescription)")
loadingRequest.finishLoading(with: error)
return
}
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
let error = NSError(domain: "HTTP Error", code: (response as? HTTPURLResponse)?.statusCode ?? -1, userInfo: nil)
print("HTTP Error: \(error.localizedDescription)")
loadingRequest.finishLoading(with: error)
return
}
if let data = data {
loadingRequest.dataRequest?.respond(with: data)
}
loadingRequest.finishLoading()
}
task.resume()
return true
}
return false
}
Post
Replies
Boosts
Views
Activity
I have a FairPlay-encrypted HLS stream and played the video in an AVPlayer.And I want to generate scrubbing thumbnails using the AVAssetImageGenerator.
Also, I am able to generate thumbnails for clear streams but get errors for protected content.
*How to generate thumbnails for protected content.
func getImageThumbnail(forTime: CMTime) {
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
generator.cancelAllCGImageGeneration()
generator.generateCGImagesAsynchronously(forTimes: [NSValue(time: forTime)]) { [weak self] requestedTime, image, actualTime, result, error in
if let error = error {
print("Error generate: \(error.localizedDescription)")
return
}
if let image = image {
DispatchQueue.main.async {
let image = UIImage(cgImage: image).jpegData(compressionQuality: 1.0)
self?.playerImg.image = UIImage(data: image!)
}
}
}
}