Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache videos #1717

Open
Tracked by #2214
danieldaquino opened this issue Nov 15, 2023 · 10 comments
Open
Tracked by #2214

Cache videos #1717

danieldaquino opened this issue Nov 15, 2023 · 10 comments
Assignees

Comments

@danieldaquino
Copy link
Contributor

At some point we investigated whether or not videos are cached, and unfortunately it seems like they are not (See this comment).

This is a concern in terms of mobile data/bandwidth usage. If we cache videos, we might be able to reduce data usage.

@alltheseas
Copy link
Collaborator

How will cached videos affects storage, as we know cached images can stack up storage space quickly.

@jb55
Copy link
Collaborator

jb55 commented Nov 15, 2023 via email

@jb55
Copy link
Collaborator

jb55 commented Nov 15, 2023 via email

@alltheseas
Copy link
Collaborator

Related to low data/cellular mode #919

@danieldaquino danieldaquino self-assigned this Mar 12, 2024
@jb55 jb55 moved this to Backlog 🪵 in Damus Roadmap 🛣️ Mar 14, 2024
@danieldaquino danieldaquino moved this from Backlog 🪵 to In Progress 🏗️ in Damus Roadmap 🛣️ Mar 22, 2024
@jb55
Copy link
Collaborator

jb55 commented Apr 8, 2024

If this is a large ticket, maybe we can break it down into smaller parts and convert this one into a tracking issue?

@danieldaquino
Copy link
Contributor Author

If this is a large ticket, maybe we can break it down into smaller parts and convert this one into a tracking issue?

The only meaningful way I currently see to split this ticket is by splitting it into progressively better cache versions. As of now, I know 3 ways of implementing this cache:

  1. The simplest method: Simple cache with whole file downloads and some initial download bandwidth overhead
    • This is the current patch
  2. The AVExportSession method: simple-ish cache that saves videos to the cache using AVExportSession which uses data downloaded by the AVPlayer
    • I have a draft for this one but I am hitting an issue with it that I could not solve after a good amount of research and experimentation
  3. The AVAssetResourceLoadingRequest method: We tap into the AVPlayer loader and manually load the bytes for them and direct those bytes to the cache as well. This might be the most efficient as it would enable us to do partial cache downloads
    • I am currently studying and experimenting with this in hopes that it will be easier than troubleshooting whatever obscure issue is on (2), and has the highest payoff

@danieldaquino
Copy link
Contributor Author

  1. The AVAssetResourceLoadingRequest method: We tap into the AVPlayer loader and manually load the bytes for them and direct those bytes to the cache as well. This might be the most efficient as it would enable us to do partial cache downloads

    • I am currently studying and experimenting with this in hopes that it will be easier than troubleshooting whatever obscure issue is on (2), and has the highest payoff

@jb55, I have a draft and a closer understand of this technique now. Not fully functional yet, but I think this is doable in the short term

@jb55
Copy link
Collaborator

jb55 commented Apr 9, 2024 via email

@danieldaquino
Copy link
Contributor Author

Created a ticket to address any remaining video loading issues: #2181

@danieldaquino danieldaquino moved this from In Review to Published to Testflight in Damus Roadmap 🛣️ Apr 22, 2024
@danieldaquino danieldaquino moved this from Published to Testflight to Done in Damus Roadmap 🛣️ Apr 22, 2024
jb55 pushed a commit that referenced this issue Apr 25, 2024
This commit brings significant improvements to the video cache feature.

Previously, the cache would merely download the video when requested, in
parallel with AVPlayer which also triggers a video download.

The video cache has been updated to tap into the AVPlayer loading
process, removing the download duplication.

Here is how that works:
1. The player requests an AVAsset from the cache.
2. The cache will return a cached asset if possible, or a special AVURLAsset with a custom `AVAssetResourceLoaderDelegate`.
3. The video player will start sending loading requests to this loader delegate.
4. Upon receiving the first request, the loader delegate begins to download the video data on the background.
5. Upon receiving these requests, the loader delegate will also record the requests, so that it can serve them once possible
6. The loader delegate keeps track of all video data chunks as it receives them from the download task, through the `URLSessionDataDelegate` and `URLSessionTaskDelegate` protocols
7. As it receives data, it checks all pending loading requests from the AVPlayer, and fulfills them as soon as possible
8. If the download fails (e.g. timeout errors, loss of connection), it attempts to restart the download.
9. If the download succeeds, it saves the video to the cache on disk.

Closes: #1717
Changelog-Added: Add video cache to save network bandwidth
Signed-off-by: Daniel D’Aquino <[email protected]>
Link: [email protected]
Signed-off-by: William Casarin <[email protected]>
jb55 pushed a commit that referenced this issue Apr 25, 2024
This commit brings significant improvements to the video cache feature.

Previously, the cache would merely download the video when requested, in
parallel with AVPlayer which also triggers a video download.

The video cache has been updated to tap into the AVPlayer loading
process, removing the download duplication.

Here is how that works:
1. The player requests an AVAsset from the cache.
2. The cache will return a cached asset if possible, or a special AVURLAsset with a custom `AVAssetResourceLoaderDelegate`.
3. The video player will start sending loading requests to this loader delegate.
4. Upon receiving the first request, the loader delegate begins to download the video data on the background.
5. Upon receiving these requests, the loader delegate will also record the requests, so that it can serve them once possible
6. The loader delegate keeps track of all video data chunks as it receives them from the download task, through the `URLSessionDataDelegate` and `URLSessionTaskDelegate` protocols
7. As it receives data, it checks all pending loading requests from the AVPlayer, and fulfills them as soon as possible
8. If the download fails (e.g. timeout errors, loss of connection), it attempts to restart the download.
9. If the download succeeds, it saves the video to the cache on disk

Closes: #1717
Changelog-Added: Add video cache to save network bandwidth
Signed-off-by: Daniel D’Aquino <[email protected]>
Link: [email protected]
Signed-off-by: William Casarin <[email protected]>
@jb55 jb55 mentioned this issue May 6, 2024
9 tasks
@jb55 jb55 reopened this May 6, 2024
@jb55 jb55 closed this as completed May 6, 2024
@jb55 jb55 reopened this May 6, 2024
@jb55
Copy link
Collaborator

jb55 commented May 6, 2024

Opened this again due to video cache revert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

No branches or pull requests

3 participants