Downloading multiple HLS audio files simultaneously fails.

I have been trying to multiple HLS audio files together. The files are encrypted with a key. I| need to fetch the key and store it locally for offline use. When I download a small number (2 or 3 files) of files simultaneously it works fine, but if I start downloading 10-15 files simultaneously most of them fails with the error message-


Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-17377), NSLocalizedDescription=The operation could not be completed}


I am getting error message from NSURLErrorDomain as well but they are rare.

Below is my download function -


class AudioDownloader {


var productKey: String

var downloadUrl: URL

var downloadSession: AVAssetDownloadURLSession?

var fakeDownloadUrl: URL?

var downloadTask: AVAssetDownloadTask?



func downloadAudio() {


if downloadSession == nil {


let configuration = URLSessionConfiguration.background(withIdentifier: self.productKey)

downloadSession = AVAssetDownloadURLSession(configuration: configuration, assetDownloadDelegate: self, delegateQueue: OperationQueue.main)

configuration.shouldUseExtendedBackgroundIdleMode = true

configuration.httpShouldSetCookies = true

configuration.httpShouldUsePipelining = false

configuration.allowsCellularAccess = true

configuration.isDiscretionary = true


}

self.fakeDownloadUrl = self.convertToScheme(url: self.downloadUrl, scheme: "fakehttp")


let asset = AVURLAsset(url: self.fakeDownloadUrl!)


let loader = asset.resourceLoader

loader.setDelegate(self, queue: DispatchQueue(label: "dispatch2"))

self.downloadTask = downloadSession?.makeAssetDownloadTask(asset: asset, assetTitle: "assetTitle \(self.productKey)", assetArtworkData: nil, options: nil)!

self.downloadTask?.taskDescription = self.productKey

self.downloadTask?.resume()

}

}


The link of the approach which I am using to fetch the key for offline use is below -

https://stackoverflow.com/questions/45670774/playing-offline-hls-with-aes-128-encryption-ios


Any help would be appreciated.

  • Were you able to solve this issue?

Add a Comment

Replies

Whenever you encounter one of these "unknown" errors, it would be helpful if you could file a bug report describing the scenario. (Post the bug number here for reference, please!)


I don't know in detail what this error means, but it indicates some kind of timeout problem. My guess is that by increasing the number of streams you initiate, you slow the throughtput of all of them, proportionately. Eventually, they are all slow enough to hit a timeout threshold, which means they'll all fail.


The workaround, obviously, is to limit the number of streams you initiate. Or, possibly, staggering their startup may help a bit.

I have tried limiting the number of streams too. It improves the performance of app upto some extent but is not foolproof. Sometimes session fails without providing an error as soon as we start it. Anyway I have opened up a bug woth reference number #49685747. Thanks for the suggestions. 🙂