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

feat(android): cache #3514

Merged
merged 25 commits into from
May 1, 2024

Conversation

lovegaoshi
Copy link
Contributor

@lovegaoshi lovegaoshi commented Feb 1, 2024

Describe the changes

closes #3428
this PR adds a simpleCache for android that the progressiveMedia sources will be cached to device. Via this when repeating a video, exoplayer no longer repeatably requests the source URI but resorts to the cached source instead.

bufferConfig now has a new property, bufferSize that denotes the size of this simpleCache in MB.

The basic example has been set up to show this problem; first remove the bufferConfig prop and watch the network request (with HTTP Toolkit) while bigbunny is playing; then add bufferConfig and watch the network request again.

@lovegaoshi lovegaoshi changed the title feat: android cache feat(android): cache Feb 1, 2024
Copy link
Collaborator

@freeboub freeboub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some comments, but it is a good PR , thank you !

docs/pages/component/props.md Outdated Show resolved Hide resolved
docs/pages/component/props.md Outdated Show resolved Hide resolved
@lovegaoshi
Copy link
Contributor Author

lovegaoshi commented Feb 16, 2024 via email

@aljidy
Copy link

aljidy commented Feb 19, 2024

nope i did not test that. and im assuming each video component creates a new exoplayerView, thus simpleCache is not shared? if cache objects cant share folders i can add like an instance cache id for different cache folders, but devs should remember to clean them afterwards.

On Fri, Feb 16, 2024, 12:35 AM Adam @.> wrote: @.* commented on this pull request. ------------------------------ In android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java <#3514 (comment)> : > minBufferMs = newMinBufferMs; maxBufferMs = newMaxBufferMs; bufferForPlaybackMs = newBufferForPlaybackMs; bufferForPlaybackAfterRebufferMs = newBufferForPlaybackAfterRebufferMs; maxHeapAllocationPercent = newMaxHeapAllocationPercent; minBackBufferMemoryReservePercent = newMinBackBufferMemoryReservePercent; minBufferMemoryReservePercent = newMinBufferMemoryReservePercent; + if (cacheSize == 0) { + cacheDataSourceFactory = null; + } else { + SimpleCache simpleCache = new SimpleCache(new File(this.getContext().getCacheDir(), "RNVCache"), new LeastRecentlyUsedCacheEvictor((long) cacheSize10241024), new StandaloneDatabaseProvider(this.getContext())); Have you tested what happens when you have multiple videos on the same screen? I was playing around with this and I think that they both try and create a folder/cache at the same time and you get an error but it might have been something weird with my setup. — Reply to this email directly, view it on GitHub <#3514 (review)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AZMOVVT2323Y3CSUHKE2QYDYT4K4BAVCNFSM6AAAAABCVWEF6KVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMYTQOBUGU3TAOJTGA . You are receiving this because you authored the thread.Message ID: @.*** com>

Yeah the issue is the fact multiple instances of SimpleCache are trying to use the same folder at the same time. I believe you have to release() the SimpleCache before you can create another instance with the same folder.

I believe the typical approach of using a cache with Exoplayer is to have a single shared cache instance at app level and pass it where it's needed. Like seen here

I don't know if it's possible but it would be ideal I think if the cache could be instantiated once per app and then used by every Video instace

@lovegaoshi
Copy link
Contributor Author

nope i did not test that. and im assuming each video component creates a new exoplayerView, thus simpleCache is not shared? if cache objects cant share folders i can add like an instance cache id for different cache folders, but devs should remember to clean them afterwards.

On Fri, Feb 16, 2024, 12:35 AM Adam @.> wrote: _@**._* commented on this pull request. ------------------------------ In android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java <#3514 (comment)> : > minBufferMs = newMinBufferMs; maxBufferMs = newMaxBufferMs; bufferForPlaybackMs = newBufferForPlaybackMs; bufferForPlaybackAfterRebufferMs = newBufferForPlaybackAfterRebufferMs; maxHeapAllocationPercent = newMaxHeapAllocationPercent; minBackBufferMemoryReservePercent = newMinBackBufferMemoryReservePercent; minBufferMemoryReservePercent = newMinBufferMemoryReservePercent; + if (cacheSize == 0) { + cacheDataSourceFactory = null; + } else { + SimpleCache simpleCache = new SimpleCache(new File(this.getContext().getCacheDir(), "RNVCache"), new LeastRecentlyUsedCacheEvictor((long) cacheSize_1024_1024), new StandaloneDatabaseProvider(this.getContext())); Have you tested what happens when you have multiple videos on the same screen? I was playing around with this and I think that they both try and create a folder/cache at the same time and you get an error but it might have been something weird with my setup. — Reply to this email directly, view it on GitHub <#3514 (review)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AZMOVVT2323Y3CSUHKE2QYDYT4K4BAVCNFSM6AAAAABCVWEF6KVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMYTQOBUGU3TAOJTGA . You are receiving this because you authored the thread.Message ID: _@_.* com>

Yeah the issue is the fact multiple instances of SimpleCache are trying to use the same folder at the same time. I believe you have to release() the SimpleCache before you can create another instance with the same folder.

I believe the typical approach of using a cache with Exoplayer is to have a single shared cache instance at app level and pass it where it's needed. Like seen here

I don't know if it's possible but it would be ideal I think if the cache could be instantiated once per app and then used by every Video instace

heres a singleton flavor if it works
lovegaoshi@650a743

@lovegaoshi
Copy link
Contributor Author

found a bug: simpleCache should not be used when using file:/// URIs
simply change mediaSourceFactory to the non cached one will do.

@aljidy
Copy link

aljidy commented Mar 4, 2024

@lovegaoshi The singleton solution seems to do the trick with multiple video instances! Will keep giving it a poke and a prod but looks good so far!

@freeboub
Copy link
Collaborator

freeboub commented Mar 7, 2024

@lovegaoshi can you please also fix conflict and check linter issue ?

@lovegaoshi
Copy link
Contributor Author

@freeboub lint is clear

Copy link
Collaborator

@freeboub freeboub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I forget to push my comment ...

@freeboub
Copy link
Collaborator

@lovegaoshi Addtionnally if you can add a button in the sample to enable caching, it would be wonderful !

@lovegaoshi
Copy link
Contributor Author

@freeboub tbh I dont know how it behaves changing cache on/off on the fly, but its added.

@freeboub
Copy link
Collaborator

One additional comment added, can you please double check it ?
I also open a PR to improve sample implementation here : https://github.com/lovegaoshi/react-native-video/pull/1/files

@lovegaoshi
Copy link
Contributor Author

lovegaoshi commented Apr 25, 2024 via email

@freeboub
Copy link
Collaborator

@lovegaoshi I don't ask to hardcode the cache size ! :) (would be better to be able to configure it on iOS :D (I don't ask for it ! ))

I understand that you are unmounting the <Video object when you change video content, but this should not be necessary...
Then between 2 playbacks on the same <Video maybe users would like to change the cache size, I think It would be your case if you don't unmount the <Video Object ?! BTW, I am not sure this is really important.
The use case which makes me threaten a little bit more is users with multiple <Video Views. if user specify multiple different cache size, what would happen ?

I am ok just to add notes in documentation, and we can go back on the feature latter !

@lovegaoshi
Copy link
Contributor Author

lovegaoshi commented Apr 25, 2024 via email

@lovegaoshi
Copy link
Contributor Author

lovegaoshi commented Apr 25, 2024 via email

@freeboub
Copy link
Collaborator

Thanks for the update @lovegaoshi !
@KrzysztofMoch if ok for you, I think we can merge !

Copy link
Collaborator

@KrzysztofMoch KrzysztofMoch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok for me

@KrzysztofMoch KrzysztofMoch merged commit ecc946d into TheWidlarzGroup:master May 1, 2024
6 checks passed
@lovegaoshi lovegaoshi deleted the dev-android-cache branch May 9, 2024 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug][android] duplicated source requests sent in looped videos?
5 participants