Skip to content

Commit fe26402

Browse files
authored
SurfaceView blur region fix (#1724)
- Switch to our internal videos - Add a card on top of the video player to be more clear for demarcation of the player. - Fix the radius corners when they dont overlap fully on the content
2 parents a481640 + 0f1f861 commit fe26402

8 files changed

Lines changed: 155 additions & 35 deletions

File tree

‎Jetchat/app/build.gradle.kts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ dependencies {
117117
implementation(libs.androidx.compose.ui.googlefonts)
118118

119119
implementation(libs.androidx.media3.exoplayer)
120-
implementation(libs.androidx.media3.exoplayer.hls)
121120
implementation(libs.androidx.media3.ui.compose)
122121

123122
debugImplementation(libs.androidx.compose.ui.test.manifest)
7.35 MB
Binary file not shown.

‎Jetchat/app/src/main/java/com/example/compose/jetchat/conversation/Conversation.kt‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ fun ConversationContent(
207207
}
208208

209209
var activeVideoUri by rememberSaveable { mutableStateOf<String?>(null) }
210+
var activeVideoText by rememberSaveable { mutableStateOf<String?>(null) }
210211

211212
Box(modifier = modifier.fillMaxSize()) {
212213
Scaffold(
@@ -287,7 +288,10 @@ fun ConversationContent(
287288
modifier = Modifier.weight(1f),
288289
scrollState = scrollState,
289290
contentPadding = paddingValues,
290-
onVideoClick = { videoUri -> activeVideoUri = videoUri },
291+
onVideoClick = { videoUri, messageText ->
292+
activeVideoUri = videoUri
293+
activeVideoText = messageText
294+
},
291295
onMessageLikeToggled = onMessageLikeToggled,
292296
)
293297
}
@@ -301,7 +305,11 @@ fun ConversationContent(
301305
activeVideoUri?.let { uri ->
302306
FullScreenVideoPlayer(
303307
videoUri = uri,
304-
onDismiss = { activeVideoUri = null },
308+
messageText = activeVideoText,
309+
onDismiss = {
310+
activeVideoUri = null
311+
activeVideoText = null
312+
},
305313
)
306314
}
307315
}
@@ -435,7 +443,7 @@ fun Messages(
435443
scrollState: LazyListState,
436444
modifier: Modifier = Modifier,
437445
contentPadding: PaddingValues = PaddingValues(0.dp),
438-
onVideoClick: (String) -> Unit = {},
446+
onVideoClick: (videoUri: String, messageText: String) -> Unit = { _, _ -> },
439447
onMessageLikeToggled: (messageId: String) -> Unit = {},
440448
) {
441449
val scope = rememberCoroutineScope()
@@ -518,7 +526,7 @@ fun Message(
518526
isUserMe: Boolean,
519527
isFirstMessageByAuthor: Boolean,
520528
isLastMessageByAuthor: Boolean,
521-
onVideoClick: (String) -> Unit = {},
529+
onVideoClick: (videoUri: String, messageText: String) -> Unit = { _, _ -> },
522530
onLikeToggled: (messageId: String) -> Unit = {},
523531
) {
524532
val borderColor = if (isUserMe) {
@@ -605,7 +613,7 @@ fun AuthorAndTextMessage(
605613
isLastMessageByAuthor: Boolean,
606614
authorClicked: (String) -> Unit,
607615
modifier: Modifier = Modifier,
608-
onVideoClick: (String) -> Unit = {},
616+
onVideoClick: (videoUri: String, messageText: String) -> Unit = { _, _ -> },
609617
onLikeToggled: (messageId: String) -> Unit = {},
610618
) {
611619
Column(
@@ -698,7 +706,7 @@ fun ChatItemBubble(
698706
message: Message,
699707
isUserMe: Boolean,
700708
authorClicked: (String) -> Unit,
701-
onVideoClick: (String) -> Unit = {},
709+
onVideoClick: (videoUri: String, messageText: String) -> Unit = { _, _ -> },
702710
onLikeToggled: (messageId: String) -> Unit = {},
703711
) {
704712
val isLiked = message.isLiked
@@ -847,7 +855,7 @@ fun ChatItemBubble(
847855
) {
848856
VideoThumbnail(
849857
videoUri = videoUri,
850-
onClick = { onVideoClick(videoUri) },
858+
onClick = { onVideoClick(videoUri, message.content) },
851859
shape = bubbleShape,
852860
modifier = Modifier
853861
.padding(if (isLiked) 8.dp else 0.dp)

‎Jetchat/app/src/main/java/com/example/compose/jetchat/conversation/SurfaceViewBlur.kt‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ data class BlurRegionSpec(
4747
val boundsInSurface: RectF = RectF(),
4848
val boundsInWindow: IntRect = IntRect.Zero,
4949
val cornerRadiusPx: Float = 0f,
50+
val cornerRadiiPx: List<Float>? = null,
5051
val blurRadiusPx: Float = 50f,
5152
val alpha: Float = 1.0f,
5253
)
@@ -97,7 +98,11 @@ object SurfaceViewBlurHelper {
9798
if (spec.boundsInSurface.width() <= 0f || spec.boundsInSurface.height() <= 0f) continue
9899
val roundedRectRegion = RoundedRectBlurRegion().apply {
99100
bounds = spec.boundsInSurface
100-
setCornerRadii(spec.cornerRadiusPx.coerceAtLeast(0f))
101+
if (spec.cornerRadiiPx != null && spec.cornerRadiiPx.size == 8) {
102+
setCornerRadii(spec.cornerRadiiPx.toFloatArray())
103+
} else {
104+
setCornerRadii(spec.cornerRadiusPx.coerceAtLeast(0f))
105+
}
101106
alpha = spec.alpha.coerceIn(0f, 1f)
102107
blurRadius = spec.blurRadiusPx.coerceAtLeast(0f)
103108
}
@@ -152,6 +157,9 @@ fun Modifier.registerBlurRegion(
152157
),
153158
)
154159
}
160+
} else if (lastBounds != null) {
161+
lastBounds = null
162+
currentOnRemoveRegion(id)
155163
}
156164
}
157165
}

‎Jetchat/app/src/main/java/com/example/compose/jetchat/data/FakeData.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ val initialMessages = listOf(
3636
"Taylor Brooks",
3737
"Here's a demo of the new blur regions feature in action! 🎥",
3838
"8:06 PM",
39-
videoUri = "https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8",
39+
videoUri = "asset:///delightful_tips.mp4",
4040
),
4141
Message(
4242
"me",

‎Jetchat/app/src/main/java/com/example/compose/jetchat/video/VideoPlayer.kt‎

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ import kotlinx.coroutines.Dispatchers
8585
import kotlinx.coroutines.delay
8686
import kotlinx.coroutines.withContext
8787

88-
const val DEFAULT_VIDEO_URL =
89-
"https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_5MB.mp4"
88+
const val DEFAULT_VIDEO_URL = "asset:///delightful_tips.mp4"
9089

9190
/**
9291
* Lightweight video thumbnail displayed inside scrollable lists (LazyColumn).
@@ -100,20 +99,26 @@ fun VideoThumbnail(videoUri: String, onClick: () -> Unit, modifier: Modifier = M
10099

101100
val thumbnailBitmap by produceState<Bitmap?>(initialValue = null, resolvedUri) {
102101
withContext(Dispatchers.IO) {
102+
val retriever = MediaMetadataRetriever()
103103
try {
104-
val retriever = MediaMetadataRetriever()
105104
val uriString = resolvedUri.toString()
106-
if (uriString.startsWith("http://") || uriString.startsWith("https://")) {
107-
retriever.setDataSource(uriString, HashMap<String, String>())
105+
if (uriString.startsWith("asset:///") || uriString.startsWith("file:///android_asset/")) {
106+
val assetPath = uriString
107+
.removePrefix("asset:///")
108+
.removePrefix("file:///android_asset/")
109+
context.assets.openFd(assetPath).use { afd ->
110+
retriever.setDataSource(afd.fileDescriptor, afd.startOffset, afd.length)
111+
}
108112
} else {
109113
retriever.setDataSource(context, resolvedUri)
110114
}
111115
val bitmap = retriever.getFrameAtTime(1_000_000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC)
112116
?: retriever.frameAtTime
113-
retriever.release()
114117
value = bitmap
115118
} catch (e: Throwable) {
116-
// Fallback to stylized dark gradient placeholder on network/codec error
119+
// Fallback to stylized dark gradient placeholder on codec error
120+
} finally {
121+
retriever.release()
117122
}
118123
}
119124
}
@@ -181,7 +186,7 @@ fun VideoThumbnail(videoUri: String, onClick: () -> Unit, modifier: Modifier = M
181186
*/
182187
@OptIn(UnstableApi::class)
183188
@Composable
184-
fun FullScreenVideoPlayer(videoUri: String, onDismiss: () -> Unit, modifier: Modifier = Modifier) {
189+
fun FullScreenVideoPlayer(videoUri: String, onDismiss: () -> Unit, modifier: Modifier = Modifier, messageText: String? = null) {
185190
val context = LocalContext.current
186191
val resolvedUri = remember(videoUri) { resolveVideoUri(videoUri) }
187192

@@ -252,10 +257,31 @@ fun FullScreenVideoPlayer(videoUri: String, onDismiss: () -> Unit, modifier: Mod
252257
val bottom = boxBottom.coerceIn(0f, surfaceH)
253258

254259
if (right > left && bottom > top) {
255-
val cornerRadius = spec.cornerRadiusPx
260+
val r = spec.cornerRadiusPx.coerceAtLeast(0f)
261+
val clipLeft = boxLeft <= 0f
262+
val clipTop = boxTop <= 0f
263+
val clipRight = boxRight >= surfaceW
264+
val clipBottom = boxBottom >= surfaceH
265+
266+
val topLeft = if (clipLeft || clipTop) 0f else r
267+
val topRight = if (clipRight || clipTop) 0f else r
268+
val bottomLeft = if (clipLeft || clipBottom) 0f else r
269+
val bottomRight = if (clipRight || clipBottom) 0f else r
270+
271+
val allSame = topLeft == topRight && topRight == bottomLeft && bottomLeft == bottomRight
256272
spec.copy(
257273
boundsInSurface = RectF(left, top, right, bottom),
258-
cornerRadiusPx = cornerRadius,
274+
cornerRadiusPx = if (allSame) topLeft else 0f,
275+
cornerRadiiPx = if (allSame) {
276+
null
277+
} else {
278+
listOf(
279+
topLeft, topLeft,
280+
topRight, topRight,
281+
bottomLeft, bottomLeft,
282+
bottomRight, bottomRight,
283+
)
284+
},
259285
)
260286
} else {
261287
null
@@ -376,6 +402,7 @@ fun FullScreenVideoPlayer(videoUri: String, onDismiss: () -> Unit, modifier: Mod
376402
},
377403
onToggleFullscreen = onDismiss,
378404
onUpdateBlurRegions = { updateBlurRegions() },
405+
messageText = messageText,
379406
modifier = Modifier.fillMaxSize(),
380407
)
381408
}
@@ -387,13 +414,20 @@ fun FullScreenVideoPlayer(videoUri: String, onDismiss: () -> Unit, modifier: Mod
387414
* Displays a thumbnail in place and launches the fullscreen player screen when tapped.
388415
*/
389416
@Composable
390-
fun VideoPlayer(videoUri: String, modifier: Modifier = Modifier, autoPlay: Boolean = false, shape: Shape = RoundedCornerShape(16.dp)) {
417+
fun VideoPlayer(
418+
videoUri: String,
419+
modifier: Modifier = Modifier,
420+
autoPlay: Boolean = false,
421+
shape: Shape = RoundedCornerShape(16.dp),
422+
messageText: String? = null,
423+
) {
391424
var isPlayerOpen by rememberSaveable { mutableStateOf(autoPlay) }
392425

393426
if (isPlayerOpen) {
394427
FullScreenVideoPlayer(
395428
videoUri = videoUri,
396429
onDismiss = { isPlayerOpen = false },
430+
messageText = messageText,
397431
)
398432
} else {
399433
VideoThumbnail(

0 commit comments

Comments
 (0)