-
Notifications
You must be signed in to change notification settings - Fork 317
Add opt-in video disk cache for ExoPlayer playback #6533
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
7
commits into
v6
Choose a base branch
from
feature/AND-1275_video_attachments_caching
base: v6
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
7 commits
Select commit
Hold shift + click to select a range
e652f8d
Add opt-in video disk cache for ExoPlayer playback
VelikovPetar 2f0f299
Add LRU eviction test for video cache
VelikovPetar 34c7d78
Merge branch 'v6' into feature/AND-1275_video_attachments_caching
VelikovPetar cad2a12
Make video cache key ignore URL query parameters
VelikovPetar 24a6965
Make cacheKeyFor private
VelikovPetar 372ab0f
Address PR review remarks on video cache
VelikovPetar 154c7c1
compose: Avoid redundant player prepare that double-loads the same video
gpunto 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
54 changes: 54 additions & 0 deletions
54
...-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,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. | ||
| * | ||
| * Pass an instance to [io.getstream.chat.android.client.ChatClient.Builder.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 pass it to | ||
| * [io.getstream.chat.android.client.ChatClient.Builder.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. | ||
| * | ||
| * @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.