0

I'm using AVAggregateAssetDownloadTask to download HLS videos in my application. I just added a pause/resume functionality. Simply, I'm suspending the task when the user wants to pause the download.

But, somehow in this case, actually task won't stop. It keeps downloading the asset. When I press the resume button I'm calling the task's .resume() method. Somehow, the process going super fast till the %100, and it gives an error (keep reading or goto Error Message title).

How do I test

  • Start to download an item via AVAggregateAssetDownloadTask
  • Tap Pause at the %3.
  • Call the task?.suspend()
  • Wait for 1 minute or 2
  • Tap the resume download button
  • Call the task?.resume()
  • Task resumes from %29
  • Task fails at %48

Code

@objc public func pauseDownload(for productId: String) {
    var asset: Asset?
    var task: AVAggregateAssetDownloadTask?
    for (taskKey, assetValue) in activeDownloadsMap where productId == assetValue.productId {
        asset = assetValue
        task = taskKey
        task?.suspend() // **task is not nil!**
        break
    }

    // Here: I'm sending a notification to the UI cases
}

@objc public func resumeDownload(for productId: String) {
    var asset: Asset?
    var task: AVAggregateAssetDownloadTask?
    for (taskKey, assetValue) in activeDownloadsMap where productId == assetValue.productId {
        asset = assetValue
        task = taskKey
        task?.resume() // **task is not nil!**
        break
    }

    // Here: I'm sending a notification to the UI cases
}

Other Cases

  • Without suspending the task, it downloads flawlessly.

Technical Details

  • Xcode Version 12.4 (12D4e)
  • iOS Version 14.4

Error Message

Error Domain=CoreMediaErrorDomain Code=-16657

"(null)" UserInfo={_NSURLErrorRelatedURLSessionTaskErrorKey=( "BackgroundAVAssetDownloadTask .<1>"), _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundAVAssetDownloadTask .<1>}

developer.apple.com

Here is the same issue at developer.apple.com:

https://developer.apple.com/forums/thread/674090

Reported Bug

FB9043262

If you believe you are faced with the same issue, please consider the help.

Best.

MGY
  • 7,245
  • 5
  • 41
  • 74
  • Same here, iPhone X iOS 14.2, Xcode 12.4 (12D4e) – frouo Sep 29 '21 at 14:16
  • Find `AVAssetDownloadURLSession` soooo buggy... Apple's API are nice but `AVAssetDownloadURLSession`, well, what happened? – frouo Sep 29 '21 at 14:19
  • iOS14? Please fist update your iOS, probably that’ll fix. – MGY Sep 29 '21 at 17:20
  • That is not something I am gonna tell to my users who want to stay on iOS 14. For whatever reason. – frouo Sep 29 '21 at 20:04
  • I can understand that of course :) But this was fixed after iOS14.2, you can go till iOS14.8, it will work, I hope :) And still, you'll be in the iOS14. – MGY Sep 30 '21 at 12:34
  • I was running into this issue too. It looks like it has been fixed in the current version of iOS 14. – M. LeRouge Jul 05 '21 at 22:14

1 Answers1

3

I just had this error on iOS 16.1

Error Domain=CoreMediaErrorDomain Code=-16657 "(null)" UserInfo={_NSURLErrorRelatedURLSessionTaskErrorKey=(
    "BackgroundAVAssetDownloadTask <6B7F1201-FF3E-4B81-B3C5-6C85D8718BDA>.<2>"
), _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundAVAssetDownloadTask <6B7F1201-FF3E-4B81-B3C5-6C85D8718BDA>.<2>}

It seems that a change in AVAggregateAssetDownloadTask as part of iOS16 is causing the the root .m3u8 to be retrieved multiple times.

We generate these root .m3u8 playlists on-the-fly and put a token on URIs pointing to child subtitle .m3u8 files. Each time the root playlist was retrieved a new token was generated, and it had a slightly different URI for the subtitle playlist. In a proxyman network trace, we could see both subtitle playlists being retrieved, and the eventual .vtt subtitle files coming from the URIs specified in the second playlist.

Ensuring a consistent URI value for the subtitle playlist across multiple root playlist retrievals fixed the issue.

user2735792
  • 51
  • 1
  • 4
  • Thanks for sharing this. I'm seeing the same issue. Can you please provide more detail on the consistent URI part? Thank you. – iangetz Nov 23 '22 at 14:58
  • Can you please provide more information how can we solve this issue @user2735792. – Aman Gupta Jan 03 '23 at 16:11
  • I faced the same issue with media playlists and not with master playlist. Having different URI generated for the same media playlist URL, and downloading multiple streams at the same time generate the logs `downloading media failed validation check` and `download failed with error -16657` – vmeyer Jan 04 '23 at 17:19