-
Notifications
You must be signed in to change notification settings - Fork 317
Fall back to a video frame when the server thumbnail is not ready #6524
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
andremion
wants to merge
6
commits into
v6
Choose a base branch
from
andrerego/and-1267-video-preview-shows-blank-until-reload-when-server-thumbnail
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.
+641
−17
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
99c1f2e
Fall back to a video frame when the server thumbnail is not available
andremion 88bed1e
Apply the video thumbnail fallback to XML file and quoted previews
andremion bc99d1b
Extract the fallback video frame with MediaMetadataRetriever
andremion 59a7e03
Tighten interceptor visibility and keep the quoted preview snapshot s…
andremion f555db4
Cover video frame extraction and the empty fallback case with tests
andremion 2acc1f8
Cover Attachment.imagePreviewData with tests
andremion 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
113 changes: 113 additions & 0 deletions
113
...stream/chat/android/compose/ui/util/extensions/internal/AttachmentImagePreviewDataTest.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,113 @@ | ||
| /* | ||
| * 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.compose.ui.util.extensions.internal | ||
|
|
||
| import androidx.compose.ui.test.junit4.createComposeRule | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import io.getstream.chat.android.client.test.MockedChatClientTest | ||
| import io.getstream.chat.android.compose.ui.theme.ChatTheme | ||
| import io.getstream.chat.android.models.Attachment | ||
| import io.getstream.chat.android.models.AttachmentType | ||
| import io.getstream.chat.android.ui.common.images.internal.VideoThumbnailImageData | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNull | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.annotation.Config | ||
| import java.io.File | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| @Config(sdk = [33]) | ||
| internal class AttachmentImagePreviewDataTest : MockedChatClientTest { | ||
|
|
||
| @get:Rule | ||
| val composeTestRule = createComposeRule() | ||
|
|
||
| @Test | ||
| fun `image uses the image url`() { | ||
| val data = previewDataOf(Attachment(type = AttachmentType.IMAGE, imageUrl = IMAGE_URL)) | ||
|
|
||
| assertEquals(IMAGE_URL, data) | ||
| } | ||
|
|
||
| @Test | ||
| fun `image without a url falls back to the local upload`() { | ||
| val upload = File("image") | ||
| val data = previewDataOf(Attachment(type = AttachmentType.IMAGE, imageUrl = null, upload = upload)) | ||
|
|
||
| assertEquals(upload, data) | ||
| } | ||
|
|
||
| @Test | ||
| fun `video uses the thumbnail and the asset as fallback`() { | ||
| val data = previewDataOf(Attachment(type = AttachmentType.VIDEO, thumbUrl = THUMB_URL, assetUrl = VIDEO_URL)) | ||
|
|
||
| assertEquals(VideoThumbnailImageData(thumbnailUrl = THUMB_URL, videoUrl = VIDEO_URL), data) | ||
| } | ||
|
|
||
| @Test | ||
| fun `video without a thumbnail still uses the asset`() { | ||
| val data = previewDataOf(Attachment(type = AttachmentType.VIDEO, thumbUrl = null, assetUrl = VIDEO_URL)) | ||
|
|
||
| assertEquals(VideoThumbnailImageData(thumbnailUrl = null, videoUrl = VIDEO_URL), data) | ||
| } | ||
|
|
||
| @Test | ||
| fun `video without a thumbnail or asset falls back to the local upload`() { | ||
| val upload = File("video") | ||
| val data = previewDataOf( | ||
| Attachment(type = AttachmentType.VIDEO, thumbUrl = null, assetUrl = null, upload = upload), | ||
| ) | ||
|
|
||
| assertEquals(upload, data) | ||
| } | ||
|
|
||
| @Test | ||
| fun `video returns null when video thumbnails are disabled`() { | ||
| val data = previewDataOf( | ||
| attachment = Attachment(type = AttachmentType.VIDEO, thumbUrl = THUMB_URL, assetUrl = VIDEO_URL), | ||
| videoThumbnailsEnabled = false, | ||
| ) | ||
|
|
||
| assertNull(data) | ||
| } | ||
|
|
||
| @Test | ||
| fun `non-media attachment returns null`() { | ||
| val data = previewDataOf(Attachment(type = AttachmentType.FILE, assetUrl = VIDEO_URL)) | ||
|
|
||
| assertNull(data) | ||
| } | ||
|
|
||
| private fun previewDataOf(attachment: Attachment, videoThumbnailsEnabled: Boolean = true): Any? { | ||
| var data: Any? = UNSET | ||
| composeTestRule.setContent { | ||
| ChatTheme(videoThumbnailsEnabled = videoThumbnailsEnabled) { | ||
| data = attachment.imagePreviewData | ||
| } | ||
| } | ||
| return data | ||
| } | ||
|
|
||
| private companion object { | ||
| private val UNSET = Any() | ||
| private const val IMAGE_URL = "https://cdn.example.com/image.jpg" | ||
| private const val THUMB_URL = "https://cdn.example.com/thumb.jpg" | ||
| private const val VIDEO_URL = "https://cdn.example.com/video.mp4" | ||
| } | ||
| } |
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
117 changes: 117 additions & 0 deletions
117
.../src/main/kotlin/io/getstream/chat/android/ui/common/images/internal/VideoFrameFetcher.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,117 @@ | ||
| /* | ||
| * 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.ui.common.images.internal | ||
|
|
||
| import android.media.MediaMetadataRetriever | ||
| import android.media.MediaMetadataRetriever.OPTION_CLOSEST_SYNC | ||
| import android.os.Build | ||
| import coil3.Extras | ||
| import coil3.ImageLoader | ||
| import coil3.Uri | ||
| import coil3.asImage | ||
| import coil3.decode.DataSource | ||
| import coil3.fetch.FetchResult | ||
| import coil3.fetch.Fetcher | ||
| import coil3.fetch.ImageFetchResult | ||
| import coil3.getExtra | ||
| import coil3.network.httpHeaders | ||
| import coil3.request.ImageRequest | ||
| import coil3.request.Options | ||
| import coil3.size.pxOrElse | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
|
|
||
| /** | ||
| * Offset of the extracted frame, kept slightly above zero so the very first frame | ||
| * (often black) is skipped. Matches the iOS preview offset. | ||
| */ | ||
| private const val VIDEO_FRAME_MICROS = 100_000L | ||
|
|
||
| /** | ||
| * Marks a request so [VideoFrameFetcher] extracts a preview frame from the video instead of | ||
| * downloading it through the default network fetcher. | ||
| */ | ||
| internal val videoFramePreviewKey: Extras.Key<Boolean> = Extras.Key(default = false) | ||
|
|
||
| /** | ||
| * Marks this request as a video preview, so the frame is extracted with [MediaMetadataRetriever] | ||
| * range reads rather than downloading the whole file. | ||
| */ | ||
| internal fun ImageRequest.Builder.videoFramePreview(): ImageRequest.Builder = apply { | ||
| extras[videoFramePreviewKey] = true | ||
| } | ||
|
|
||
| /** | ||
| * A Coil [Fetcher] that extracts a preview frame from a remote video using [MediaMetadataRetriever]. | ||
| * | ||
| * Decoding through `VideoFrameDecoder` requires the whole file to be downloaded first. | ||
| * [MediaMetadataRetriever] instead seeks with HTTP range requests and reads only the bytes needed | ||
| * for the requested frame, and the full video is never written to the image disk cache. Only | ||
| * requests marked with [videoFramePreview] are handled; all others fall through to the default | ||
| * network fetcher. | ||
| * | ||
| * The data and headers reaching this fetcher are already resolved by the upstream interceptors | ||
| * (including CDN signing), so the URL is used as-is. | ||
| */ | ||
| internal class VideoFrameFetcher( | ||
| private val data: Uri, | ||
| private val options: Options, | ||
| ) : Fetcher { | ||
|
|
||
| override suspend fun fetch(): FetchResult = withContext(Dispatchers.IO) { | ||
|
Check warning on line 75 in stream-chat-android-ui-common/src/main/kotlin/io/getstream/chat/android/ui/common/images/internal/VideoFrameFetcher.kt
|
||
| val retriever = MediaMetadataRetriever() | ||
| try { | ||
| retriever.setDataSource(data.toString(), options.requestHeaders()) | ||
| val bitmap = retriever.extractFrame() | ||
| ?: error("Could not extract a preview frame from video: $data") | ||
| ImageFetchResult( | ||
| image = bitmap.asImage(), | ||
| isSampled = false, | ||
| dataSource = DataSource.NETWORK, | ||
| ) | ||
| } finally { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
| retriever.close() | ||
| } else { | ||
| @Suppress("DEPRECATION") | ||
| retriever.release() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun Options.requestHeaders(): Map<String, String> = | ||
| httpHeaders.asMap().mapNotNull { (name, values) -> | ||
| values.lastOrNull()?.let { name to it } | ||
| }.toMap() | ||
|
|
||
| private fun MediaMetadataRetriever.extractFrame(): android.graphics.Bitmap? { | ||
| val dstWidth = options.size.width.pxOrElse { 0 } | ||
| val dstHeight = options.size.height.pxOrElse { 0 } | ||
| return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 && dstWidth > 0 && dstHeight > 0) { | ||
| getScaledFrameAtTime(VIDEO_FRAME_MICROS, OPTION_CLOSEST_SYNC, dstWidth, dstHeight) | ||
| } else { | ||
| getFrameAtTime(VIDEO_FRAME_MICROS, OPTION_CLOSEST_SYNC) | ||
| } | ||
| } | ||
|
|
||
| class Factory : Fetcher.Factory<Uri> { | ||
| override fun create(data: Uri, options: Options, imageLoader: ImageLoader): Fetcher? { | ||
| if (!options.getExtra(videoFramePreviewKey)) return null | ||
| return VideoFrameFetcher(data, options) | ||
| } | ||
| } | ||
| } | ||
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.