11package dev.silenium.multimedia.compose.player
22
33import androidx.compose.foundation.layout.BoxWithConstraints
4- import androidx.compose.foundation.layout.fillMaxSize
54import androidx.compose.foundation.layout.padding
65import androidx.compose.foundation.layout.width
76import androidx.compose.material.MaterialTheme
@@ -10,48 +9,55 @@ import androidx.compose.runtime.*
109import androidx.compose.ui.Modifier
1110import androidx.compose.ui.graphics.Color
1211import androidx.compose.ui.unit.dp
13- import dev.silenium.compose.gl.surface.FBOSizeOverride
14- import dev.silenium.compose.gl.surface.GLSurfaceView
15- import dev.silenium.compose.gl.surface.rememberGLSurfaceState
12+ import dev.silenium.compose.gl.surface.*
1613import dev.silenium.multimedia.core.annotation.InternalMultimediaApi
1714
1815@Composable
19- fun VideoSurface (
16+ private fun createGLSurface (
2017 player : VideoPlayer ,
21- showStats : Boolean = false,
22- modifier : Modifier = Modifier ,
18+ surfaceState : GLSurfaceState = rememberGLSurfaceState(),
2319 onInitialized : () -> Unit = {},
24- ) {
25- val surfaceState = rememberGLSurfaceState()
20+ ): GLSurface {
21+ var initialized by remember { mutableStateOf( false ) }
2622
2723 @OptIn(InternalMultimediaApi ::class )
28- val dwidth by player.property<Long >(" width " )
24+ val dwidth by player.property<Long >(" dwidth " )
2925
3026 @OptIn(InternalMultimediaApi ::class )
31- val dheight by player.property<Long >(" height " )
27+ val dheight by player.property<Long >(" dheight " )
3228 val fboSizeOverride = remember(dwidth, dheight) {
3329 dwidth?.let { w ->
3430 dheight?.let { h ->
3531 FBOSizeOverride (w.toInt(), h.toInt())
3632 }
3733 }
3834 }
35+ return rememberGLSurface(
36+ surfaceState,
37+ presentMode = GLSurface .PresentMode .MAILBOX ,
38+ swapChainSize = 3 ,
39+ fboSizeOverride = fboSizeOverride,
40+ draw = {
41+ player.onRender(this , surfaceState)
42+ if (! initialized) {
43+ initialized = true
44+ onInitialized()
45+ }
46+ }
47+ )
48+ }
3949
50+ @Composable
51+ fun VideoSurface (
52+ player : VideoPlayer ,
53+ showStats : Boolean = false,
54+ modifier : Modifier = Modifier ,
55+ ) {
56+ val surfaceState = rememberGLSurfaceState()
4057 BoxWithConstraints (modifier = modifier) {
41- var initialized by remember { mutableStateOf(false ) }
4258 GLSurfaceView (
43- surfaceState,
44- modifier = Modifier .fillMaxSize(),
45- presentMode = GLSurfaceView .PresentMode .MAILBOX ,
46- swapChainSize = 3 ,
47- draw = {
48- player.onRender(this , surfaceState)
49- if (! initialized) {
50- initialized = true
51- onInitialized()
52- }
53- },
54- fboSizeOverride = fboSizeOverride,
59+ surface = player.surface ? : createGLSurface(player, surfaceState),
60+ modifier = Modifier .matchParentSize(),
5561 )
5662 if (showStats) {
5763 Surface (
@@ -66,8 +72,16 @@ fun VideoSurface(
6672}
6773
6874@Composable
69- fun rememberVideoPlayer (): VideoPlayer {
75+ fun rememberVideoPlayer (
76+ surfaceState : GLSurfaceState = rememberGLSurfaceState(),
77+ onInitialized : () -> Unit = {},
78+ ): VideoPlayer {
7079 val player = remember { VideoPlayer () }
80+ val surface = createGLSurface(player, surfaceState, onInitialized)
81+
82+ LaunchedEffect (player, surface) {
83+ player.surface = surface
84+ }
7185 DisposableEffect (player) {
7286 onDispose {
7387 player.close()
0 commit comments