Yokan / rebuild
Course 1 8624561

MpvNativeSurfaceBinding.kt

composeApp/src/nonWebMain/kotlin/eu/friedcutlet/yokan/services/media/player/MpvNativeSurfaceBinding.kt

implementation43 lines
1package eu.friedcutlet.yokan.services.media.player23import io.github.kmpmpv.MpvRenderTarget4import kotlinx.coroutines.flow.MutableStateFlow5import kotlinx.coroutines.flow.updateAndGet67/** Surface callbacks expire by identity and generation; all native work and detachment are joined. */8internal class MpvNativeSurfaceBinding(9    private val resource: PlayerSurfaceResources.Resource,10    private val serialized: suspend (suspend () -> Unit, () -> Unit) -> Unit,11) {12    internal var attachTarget: suspend (MpvRenderTarget) -> Unit = {}13    internal var resizeTarget: suspend (Int, Int) -> Unit = { _, _ -> }14    internal var detachTarget: suspend () -> Unit = {}15    private val generation = MutableStateFlow(0L)16    fun attach(target: MpvRenderTarget, width: Int, height: Int): Boolean {17        val captured = generation.updateAndGet { it + 1L }18        return resource.launch { current ->19            serialized({20                if (generation.value == captured) {21                    attachTarget(target)22                    current()23                    if (generation.value == captured && width > 0 && height > 0) resizeTarget(width, height)24                }25            }, current)26        }27    }28    fun resize(width: Int, height: Int): Boolean {29        if (width <= 0 || height <= 0) return false30        val captured = generation.value31        return resource.launch { current ->32            serialized({ if (generation.value == captured) resizeTarget(width, height) }, current)33        }34    }35    fun detach(): Boolean {36        val captured = generation.updateAndGet { it + 1L }37        return resource.launch { current ->38            serialized({ if (generation.value == captured) detachTarget() }, current)39        }40    }41    fun retire() { generation.updateAndGet { it + 1L }; resource.retire() }42    suspend fun closeAndJoin() { retire(); resource.closeAndJoin() }43}44

Search chapters, reference and source filenames.