-
Notifications
You must be signed in to change notification settings - Fork 318
Add opt-in video disk cache for ExoPlayer playback
#6542
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
Open
VelikovPetar
wants to merge
8
commits into
develop
Choose a base branch
from
port/v6-to-develop/opt-in-video-disk-cache
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
87610b3
Port V6 fix: Add opt-in video disk cache for ExoPlayer playback
VelikovPetar c9aa95b
Merge branch 'develop' into port/v6-to-develop/opt-in-video-disk-cache
VelikovPetar af32c44
Merge branch 'develop' into port/v6-to-develop/opt-in-video-disk-cache
VelikovPetar 5575eb0
client: Guard SimpleCache construction and document cache key behaviour
VelikovPetar ab3099b
Merge branch 'develop' into port/v6-to-develop/opt-in-video-disk-cache
VelikovPetar 7b0b0a9
client: Fix missing !! on evictionCache in LRU eviction test
VelikovPetar 3f68136
Merge branch 'develop' into port/v6-to-develop/opt-in-video-disk-cache
andremion 30ca642
Merge branch 'develop' into port/v6-to-develop/opt-in-video-disk-cache
VelikovPetar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...-android-client/src/main/java/io/getstream/chat/android/client/cache/StreamCacheConfig.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright (c) 2014-2026 Stream.io Inc. All rights reserved. | ||
| * | ||
| * Licensed under the Stream License; | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://github.com/GetStream/stream-chat-android/blob/main/LICENSE | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.getstream.chat.android.client.cache | ||
|
|
||
| /** | ||
| * Bundles the per-cache configurations exposed by the Stream Chat SDK. | ||
| * | ||
| * Set this on [io.getstream.chat.android.client.api.ChatClientConfig.cacheConfig] to configure the | ||
| * on-disk caches. | ||
| * | ||
| * @param video Configuration for the video playback cache used by SDK. | ||
| */ | ||
| public data class StreamCacheConfig( | ||
| public val video: VideoCacheConfig? = null, | ||
| ) | ||
|
|
||
| /** | ||
| * Configuration for the on-disk cache used when streaming video attachments. | ||
| * | ||
| * Wrap an instance in [StreamCacheConfig] and set it on | ||
| * [io.getstream.chat.android.client.api.ChatClientConfig.cacheConfig] to opt in. When the cache is | ||
| * enabled, replaying or seeking within a previously watched video reuses cached byte ranges | ||
| * instead of re-downloading from the CDN. | ||
|
andremion marked this conversation as resolved.
|
||
| * | ||
| * Cache entries are keyed by URL path only; query parameters are stripped. URLs that differ | ||
| * only in their query string share the same cache entry. | ||
| * | ||
| * @param maxSizeBytes Soft cap on cache size; LRU eviction kicks in once exceeded. Files larger | ||
| * than this cap are not effectively cached. Size [maxSizeBytes] to comfortably exceed the | ||
| * largest expected video. | ||
| */ | ||
| public data class VideoCacheConfig( | ||
| public val maxSizeBytes: Long = DEFAULT_MAX_SIZE_BYTES, | ||
| ) { | ||
| init { | ||
| require(maxSizeBytes > 0) { "maxSizeBytes must be > 0, got $maxSizeBytes" } | ||
| } | ||
|
|
||
| public companion object { | ||
| /** Default cap of 150 MB. */ | ||
| public const val DEFAULT_MAX_SIZE_BYTES: Long = 150L * 1024 * 1024 | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
.../main/java/io/getstream/chat/android/client/cache/internal/VideoCacheDataSourceFactory.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright (c) 2014-2026 Stream.io Inc. All rights reserved. | ||
| * | ||
| * Licensed under the Stream License; | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://github.com/GetStream/stream-chat-android/blob/main/LICENSE | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.getstream.chat.android.client.cache.internal | ||
|
|
||
| import androidx.annotation.OptIn | ||
| import androidx.media3.common.util.UnstableApi | ||
| import androidx.media3.datasource.DataSource | ||
| import androidx.media3.datasource.DataSpec | ||
| import androidx.media3.datasource.cache.CacheDataSource | ||
| import io.getstream.chat.android.client.cdn.internal.CDNDataSourceFactory | ||
|
|
||
| /** | ||
| * A [DataSource.Factory] that serves video bytes from a [VideoMediaCache] on hit and delegates | ||
| * to [upstreamFactory] on miss, writing the fetched bytes back into the cache. | ||
| * | ||
| * Cache entries are keyed by the URI with its query stripped, so rotating signature/expiry | ||
| * parameters on the same path resolve to the same cache entry. The full [DataSpec] still flows | ||
| * to [upstreamFactory] on a miss, so a custom CDN sees the original URL and can re-sign or | ||
| * rewrite it. A caller-supplied [DataSpec.key] takes precedence over the URI-derived key. | ||
| * | ||
| * @param videoCache The cache that holds the cached video spans. | ||
| * @param upstreamFactory Factory invoked on cache miss (typically the [CDNDataSourceFactory] when | ||
| * a custom CDN is configured, or the base data source otherwise). | ||
| */ | ||
| @OptIn(UnstableApi::class) | ||
| internal class VideoCacheDataSourceFactory( | ||
| videoCache: VideoMediaCache, | ||
| upstreamFactory: DataSource.Factory, | ||
| ) : DataSource.Factory { | ||
|
|
||
| private val delegate: DataSource.Factory = CacheDataSource.Factory() | ||
| .setCache(videoCache.cache) | ||
| .setUpstreamDataSourceFactory(upstreamFactory) | ||
| .setCacheKeyFactory(::cacheKeyFor) | ||
| .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR) | ||
|
|
||
| override fun createDataSource(): DataSource = delegate.createDataSource() | ||
|
Check warning on line 51 in stream-chat-android-client/src/main/java/io/getstream/chat/android/client/cache/internal/VideoCacheDataSourceFactory.kt
|
||
|
|
||
| /** | ||
| * Returns the cache key for [dataSpec]. Strips the URI's query so rotating signature or expiry | ||
| * parameters on the same path land on the same cache entry; a caller-supplied [DataSpec.key] is | ||
| * honoured when present. | ||
| */ | ||
| @OptIn(UnstableApi::class) | ||
| private fun cacheKeyFor(dataSpec: DataSpec): String = | ||
| dataSpec.key ?: dataSpec.uri.buildUpon().clearQuery().build().toString() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: GetStream/stream-chat-android
Length of output: 31263
🏁 Script executed:
Repository: GetStream/stream-chat-android
Length of output: 31250
🏁 Script executed:
Repository: GetStream/stream-chat-android
Length of output: 3026
🏁 Script executed:
Repository: GetStream/stream-chat-android
Length of output: 783
🏁 Script executed:
Repository: GetStream/stream-chat-android
Length of output: 7459
🏁 Script executed:
Repository: GetStream/stream-chat-android
Length of output: 14827
🏁 Script executed:
Repository: GetStream/stream-chat-android
Length of output: 7626
🏁 Script executed:
Repository: GetStream/stream-chat-android
Length of output: 1522
🏁 Script executed:
Repository: GetStream/stream-chat-android
Length of output: 1954
Close the video-cache clear/delete race.
VideoMediaCache.clearAll()only tells you whether a live cache existed at that moment; if it returnsfalse, the fallbackfileManager.clearVideoCache(context)inChatClient.kt:1521-1524can run after another thread has created a newVideoMediaCachefor the same directory. ThatdeleteRecursively()path can corrupt the liveSimpleCacheindex/lock. Move the fallback delete behind a single atomicVideoMediaCacheAPI that holds the registry lock across the “clear live caches or delete on disk” decision.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@VelikovPetar what's your take on this one? I think CodeRabbit is right that there is a real race here, though it needs an unusual setup: clearing the cache on one client while another client is being built in the same process. Low probability in practice.
The root of it is that
clearAll()reports whether any cache is live anywhere in the process, and we use that yes/no to decide whether to delete this one directory. The signal and the target do not match, which is also why the cleanup test ends up coupled to global state.A small fix would be to make the decision per directory under the registry lock: check whether a live cache owns
getVideoCache(context), clear it in place if so, otherwise delete the directory while still holding the lock so a concurrentcreate()cannot register a new cache for it in between. That would also remove the test's dependence on global state.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 10865
@andremionAgreed — your fix targets the actual mismatch better than my original suggestion. The registry lock already exists insideVideoMediaCache.create/clear/release, so extending it to cover the "resolve directory → clear-in-place-or-delete" decision is a small, contained change and removes the dependency on global registry state.Concretely, this could be a new internal API on
VideoMediaCache.Companion, something like:ChatClient.clearCacheAndTemporaryFileswould then callVideoMediaCache.clearOrDelete(fileManager.getVideoCache(context)) { fileManager.deleteVideoCacheDir(it) }(or similar), replacing the currentclearAll()boolean + separateclearVideoCache(context)fallback. That also decouples the cleanup test from asserting on global registry state, as you noted.I'll leave the implementation call to you and
@VelikovPetarsince it touchesStreamFileManager's directory-delete helper too — happy to help wire it up if useful.🧬 Code Graph Analysis Results
stream-chat-android-client/src/main/java/io/getstream/chat/android/client/ChatClient.ktSnippet 1 —
clearCacheAndTemporaryFiles(context: Context)Lines: ~340-400
Snippet 2 —
Builder.internalBuild()creation ofVideoMediaCacheLines: ~760-830
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @andremion I missed this, checking this first thing next week!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.