-
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
base: develop
Are you sure you want to change the base?
Changes from all commits
87610b3
c9aa95b
af32c44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * 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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small doc suggestion. The cache key strips the URL query, so entries are keyed by path alone, and this wraps all ExoPlayer media, not only Stream attachments. Worth one line here noting the cache assumes the path identifies the content? Sets expectations for anyone playing non-Stream URLs where the query is content-significant. |
||
| * | ||
| * @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 | ||
| } | ||
| } | ||
| 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() | ||
| } | ||
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