Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@

package io.getstream.webrtc.android.compose

import android.view.ViewGroup.LayoutParams
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.layout
import androidx.compose.ui.unit.constrainHeight
import androidx.compose.ui.unit.constrainWidth
import androidx.compose.ui.viewinterop.AndroidView
import io.getstream.webrtc.android.compose.VideoScalingType.Companion.toCommonScalingType
import io.getstream.webrtc.android.ui.VideoTextureViewRenderer
Expand All @@ -48,6 +53,18 @@ public fun VideoRenderer(
) {
val trackState: MutableState<VideoTrack?> = remember { mutableStateOf(null) }
var view: VideoTextureViewRenderer? by remember { mutableStateOf(null) }
val currentRendererEvents by rememberUpdatedState(rendererEvents)
val forwardingRendererEvents = remember {
object : RendererEvents {
override fun onFirstFrameRendered() {
currentRendererEvents.onFirstFrameRendered()
}

override fun onFrameResolutionChanged(videoWidth: Int, videoHeight: Int, rotation: Int) {
currentRendererEvents.onFrameResolutionChanged(videoWidth, videoHeight, rotation)
}
}
}

DisposableEffect(videoTrack) {
onDispose {
Expand All @@ -58,22 +75,30 @@ public fun VideoRenderer(
AndroidView(
factory = { context ->
VideoTextureViewRenderer(context).apply {
init(eglBaseContext, rendererEvents)
layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
init(eglBaseContext, forwardingRendererEvents)
setScalingType(scalingType = videoScalingType.toCommonScalingType())
setupVideo(trackState, videoTrack, this)
onTextureViewCreated.invoke(this)
view = this
}
},
update = { v -> setupVideo(trackState, videoTrack, v) },
modifier = modifier,
update = { v ->
v.setScalingType(scalingType = videoScalingType.toCommonScalingType())
setupVideo(trackState, videoTrack, v)
},
modifier = modifier.layout { measurable, constraints ->
val placeable = measurable.measure(constraints.copy(minWidth = 0, minHeight = 0))
val width = constraints.constrainWidth(placeable.width)
val height = constraints.constrainHeight(placeable.height)
layout(width, height) {
placeable.placeRelative((width - placeable.width) / 2, (height - placeable.height) / 2)
}
},
)
}

private fun cleanTrack(
view: VideoTextureViewRenderer?,
trackState: MutableState<VideoTrack?>,
) {
private fun cleanTrack(view: VideoTextureViewRenderer?, trackState: MutableState<VideoTrack?>) {
view?.let { trackState.value?.removeSink(it) }
trackState.value = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ public enum class VideoScalingType {
@JvmField
public val SCAPE_ASPECT_FILL: VideoScalingType = SCALE_ASPECT_FILL

internal fun VideoScalingType.toCommonScalingType(): RendererCommon.ScalingType {
return when (this) {
SCALE_ASPECT_FIT -> RendererCommon.ScalingType.SCALE_ASPECT_FIT
SCALE_ASPECT_FILL -> RendererCommon.ScalingType.SCALE_ASPECT_FILL
SCALE_ASPECT_BALANCED -> RendererCommon.ScalingType.SCALE_ASPECT_BALANCED
}
internal fun VideoScalingType.toCommonScalingType(): RendererCommon.ScalingType = when (this) {
SCALE_ASPECT_FIT -> RendererCommon.ScalingType.SCALE_ASPECT_FIT
SCALE_ASPECT_FILL -> RendererCommon.ScalingType.SCALE_ASPECT_FILL
SCALE_ASPECT_BALANCED -> RendererCommon.ScalingType.SCALE_ASPECT_BALANCED
}
}
}