Yokan / rebuild
Course 1 8624561

PlatformPictureInPicture.wasm.kt

composeApp/src/wasmJsMain/kotlin/eu/friedcutlet/yokan/services/media/player/PlatformPictureInPicture.wasm.kt

implementation71 lines
1package eu.friedcutlet.yokan.services.media.player23import io.github.kdroidfilter.composemediaplayer.VideoPlayerState4import kotlinx.coroutines.flow.MutableStateFlow5import kotlinx.coroutines.flow.StateFlow6import kotlinx.coroutines.flow.Flow7import kotlinx.coroutines.flow.MutableSharedFlow8import kotlinx.coroutines.flow.update910internal actual val pictureInPicturePlatform: PictureInPicturePlatform = WasmPictureInPicture1112private object WasmPictureInPicture : PictureInPicturePlatform {13    private val activeState = MutableStateFlow(false)14    private val actionEvents = MutableSharedFlow<PictureInPictureAction>(extraBufferCapacity = 8)15    private var playbackAdmitted: () -> Boolean = { false }16    private val surfaces = mutableMapOf<MediaPlayerController, VideoPlayerState>()17    private var requestedSurface: VideoPlayerState? = null1819    override val isSupported: Boolean get() = surfaces.values.any { it.isPictureInPictureSupported() }20    override val active: StateFlow<Boolean> = activeState21    override val actions: Flow<PictureInPictureAction> = actionEvents2223    override fun bindSurface(controller: MediaPlayerController, state: VideoPlayerState): () -> Unit {24        surfaces[controller] = state25        state.onPictureInPictureChanged = { active ->26            if (surfaces[controller] === state && requestedSurface === state) activeState.update { active }27        }28        return {29            if (surfaces[controller] === state) surfaces.remove(controller)30            state.onPictureInPictureChanged = {}31            if (requestedSurface === state) {32                requestedSurface = null33                activeState.update { false }34            }35        }36    }3738    override fun request(controller: MediaPlayerController?): Boolean {39        if (!playbackAdmitted()) return false40        val surface = surfaces[controller] ?: return false41        val previous = requestedSurface42        requestedSurface = surface43        val accepted = surface.requestPictureInPicture()44        if (!accepted && requestedSurface === surface) requestedSurface = previous45        return accepted46    }4748    override fun setPlaybackState(active: Boolean, eligible: Boolean, admitted: () -> Boolean) {49        playbackAdmitted = { active && admitted() }50    }5152    /** The browser PiP window shows Media Session track/seek handlers; play/pause stays on the video element. */53    override fun updateActions(playing: Boolean, replaceWithPrevious: Boolean) {54        setMediaSessionAction("nexttrack") { actionEvents.tryEmit(PictureInPictureAction.Next) }55        if (replaceWithPrevious) {56            clearMediaSessionAction("seekforward")57            setMediaSessionAction("previoustrack") { actionEvents.tryEmit(PictureInPictureAction.Previous) }58        } else {59            clearMediaSessionAction("previoustrack")60            setMediaSessionAction("seekforward") { actionEvents.tryEmit(PictureInPictureAction.SkipForward) }61        }62    }63}6465@OptIn(ExperimentalWasmJsInterop::class)66private fun setMediaSessionAction(action: String, handler: () -> Unit): Unit =67    js("{ try { if (navigator.mediaSession) navigator.mediaSession.setActionHandler(action, () => handler()); } catch (e) {} }")6869@OptIn(ExperimentalWasmJsInterop::class)70private fun clearMediaSessionAction(action: String): Unit =71    js("{ try { if (navigator.mediaSession) navigator.mediaSession.setActionHandler(action, null); } catch (e) {} }")72

Search chapters, reference and source filenames.