Need some help here from strong AVKit'ers and AVFoundation'ers
I'm struggling to resume previously partly downloaded HLS stream.
URL, that I have, changes every 24H. And if user tries to resume download - it just starts again and ignores earlier download progress.
I use code from apple - Using AVFoundation to Play and Persist HTTP Live Streams
For now it works like that:
- Stream start download via
assetDownloadURLSession.aggregateAssetDownloadTask
- User presses Pause. In this situation I save all progress to local file and use bookmarkData() to not lose that local URL:
let bookmark = try tempDownloadUrl?.bookmarkData() userDefaults.set(bookmark, forKey: assetId)
- Link is dead, user taps Resume ->
didFinishDownloadingTo
fromAVAssetDownloadDelegate
immediately fires with 403 - Forbidden. - I try to restart download task with creating new
AVAggregateAssetDownloadTask
usingassetDownloadURLSession.aggregateAssetDownloadTask
and inserting there local URL to file (.movpkg). If link doesn't change, everything works as expected. - We are here
Some thoughts:
- Probably we can somehow copy downloaded amount if m3u8 chunks to new download task
- Probably we can change some kind of serverURL inside that previously downloaded .movpkg file.
Or something new. (I can share a link that dies after 2 minutes for testing purposes)
Thanks in advance.
Okay. I figured out how to deal with such a complex task.
Firstly we need to read contents of our local file .movpkg file.
Nice, now we need to iterate through local directory and to find .xml and .frag files, that are holding our old url. This can be done in such way
[Sorry for some messy code, it is not production ready code, it's just points to a right direction of problem solving. Please refactor it on your own purposes]:
So, in code above we search for our oldUrlString and replace it with newUrlString. You can obtain oldUrlString also from xml file or from CoreData (if you store oldUrl somewhere).
Make to remove last path component from oldUrlString and newUrlString before update. So link is similar to:
And not
It's because chunks are placed like that and replace will not work, if you will not remove chunklist_b1728000.m3u8 from path:
That's it. After URL is replaced, resume downloading successfully achieved.