Skip to content

update video player to media3-exoplayer and improve caching #403

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

Merged
merged 5 commits into from
Jun 24, 2025

Conversation

michaelknoch
Copy link
Member

@michaelknoch michaelknoch commented Jun 23, 2025

Type of change: update dependency

Motivation (current vs expected behavior)

This change replaces our ExoPlayer v2 integration with the new Media3 APIs for improved compatibility and future support, swaps in an OkHttp-backed OkHttpDataSource.Factory to gain HTTP/2 multiplexing, header compression and on-disk HTTP caching, and centralizes both the OkHttpClient and ExoPlayer’s SimpleCache into a single Media3Singleton—ensuring one LRU index and one connection pool across all videos, for faster startup, fewer stalls, and reduced resource usage.

Disclaimer: I just updated the library, but chatGPT wanted to improve the caching. I'm still trying to measure the differences, but I can confirm the video cache still works. Also scrolling through the video feels smooth on my pixel device.

Please check if the PR fulfills these requirements

  • Self-review: I am confident this is the simplest and clearest way to achieve the expected behaviour
  • There are no dependencies on other PRs or I have linked dependencies through Zenhub
  • The commit messages are clean and understandable
  • Tests for the changes have been added (for bug fixes / features)

@michaelknoch michaelknoch marked this pull request as ready for review June 23, 2025 22:12
@michaelknoch michaelknoch requested a review from ephemer June 23, 2025 22:12
@michaelknoch michaelknoch changed the title update video player to media3-exoplayer v1.7.1 update video player to media3-exoplayer and improve caching Jun 24, 2025
@Suppress("unused")
class AVPlayer(parent: SDLActivity, asset: AVURLAsset) {
internal val exoPlayer: SimpleExoPlayer
private var listener: Player.EventListener
internal val exoPlayer: ExoPlayer
Copy link
Member

Choose a reason for hiding this comment

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

ok I'm confused. there's media3 which contains ExoPlayer now? I thought they just renamed it 😵

Copy link
Member Author

Choose a reason for hiding this comment

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

Jetpack Media3’s ExoPlayer module is still ExoPlayer, it's just repackaged under the AndroidX Media3 umbrella (whatever androidx is, lol)

SeekParameters.CLOSEST_SYNC
}

val seekParameters = if (delta < 250) SeekParameters.EXACT else SeekParameters.CLOSEST_SYNC
Copy link
Member

Choose a reason for hiding this comment

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

I think this should probably be for delta < 100 only

Copy link
Member Author

Choose a reason for hiding this comment

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

You think I should just change it, or do we need to test it manually?

Copy link
Member

Choose a reason for hiding this comment

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

If you want to be more conservative I'd say delta < 150 and not test it. if you want to be sure we could test it at delta < 100. Either way I don't think it'll cause UX issues

init {
Media3Singleton.init(
context = context,
httpCacheSize = 20L * 1024 * 1024,
Copy link
Member

Choose a reason for hiding this comment

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

do you know what the difference is here? why are they different sizes?

Copy link
Member Author

Choose a reason for hiding this comment

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

  • httpCacheSize (20 MiB) is for OkHttp’s disk cache of HTTP responses/validators (ETags, headers, small bodies), so we get fast 304s and header compression—but we don’t store full video ranges there.

  • mediaCacheSize (512 MiB) is ExoPlayer’s SimpleCache, which actually holds the raw MP4 bytes

I just restructured the initialization and size definitions a bit to make it easier to follow

Copy link
Member

Choose a reason for hiding this comment

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

ok sounds good to me! thanks. might be worth leaving a code comment with some of that info

val cacheFactory = CacheDataSourceFactory(
context = context,
maxCacheSize = 512L * 1024 * 1024,
maxFileSize = 64L * 1024 * 1024
Copy link
Member

Choose a reason for hiding this comment

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

do we really want to limit the max file size to 64MB? I would just put these two values the same – if we cache one massive video or lots of little ones doesn't really matter i think?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it should not make a huge difference, since most of our videos are way smaller than < 64mb. But theoretically I agree, I'm happy to increase the number

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, most of them are. But if there is a video larger than 64MB, would we really not want to cache it?

Copy link
Member

@ephemer ephemer left a comment

Choose a reason for hiding this comment

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

LGTM!

@michaelknoch michaelknoch merged commit b01a172 into master Jun 24, 2025
1 check passed
@michaelknoch michaelknoch deleted the update-video-player-2025 branch June 24, 2025 09:39
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.

2 participants