CastController.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/media/cast/CastController.kt
1package eu.friedcutlet.yokan.services.media.cast23import kotlinx.coroutines.CoroutineScope4import kotlinx.coroutines.flow.StateFlow56data class CastDevice(val id: String, val name: String, val host: String = "", val port: Int = 8009)78data class CastSubtitle(val url: String, val label: String, val language: String? = null)910data class CastTextStyle(11 val foregroundArgb: Int = 0xFFFFFFFF.toInt(),12 val edgeArgb: Int = 0xFF000000.toInt(),13 val backgroundArgb: Int = 0,14 val fontScale: Double = 1.0,15 val bold: Boolean = false,16 val italic: Boolean = false,17)1819data class CastMedia(20 val url: String,21 val title: String,22 val subtitle: String? = null,23 val contentType: String? = null,24 val posterUrl: String? = null,25 val subtitles: List<CastSubtitle> = emptyList(),26 val itemKey: String = url,27)2829data class CastStatus(30 val device: CastDevice,31 val playing: Boolean = false,32 val buffering: Boolean = false,33 val positionMs: Long = 0,34 val durationMs: Long = 0,35 val queue: List<CastMedia> = emptyList(),36 /** Index into [queue] of the receiver's current item, -1 when idle/unknown. */37 val currentIndex: Int = -1,38 /** Receiver went IDLE with reason FINISHED after the last item. */39 val finished: Boolean = false,40 val error: String? = null,41)4243/**44 * Casts to Chromecast's Default Media Receiver. The receiver fetches media URLs itself, so streams that need45 * request headers (referer, cookies, auth) or are only reachable from this device cannot play there.46 */47interface CastController {48 /** False where the platform has no casting path at runtime (e.g. non-Chromium browser). */49 val isSupported: StateFlow<Boolean>50 /** True when the platform shows its own device picker (web Cast SDK); connect(null) opens it. */51 val usesSystemPicker: Boolean52 val devices: StateFlow<List<CastDevice>>53 /** Null when no session. */54 val status: StateFlow<CastStatus?>55 fun startDiscovery()56 fun stopDiscovery()57 /** Throws on failure; launches the Default Media Receiver (app id CC1AD845). */58 suspend fun connect(device: CastDevice?)59 suspend fun loadQueue(60 items: List<CastMedia>,61 startIndex: Int,62 startPositionMs: Long,63 style: CastTextStyle,64 activeSubtitleIndex: Int?,65 )66 /** Queue insert at end. */67 suspend fun append(items: List<CastMedia>)68 fun play()69 fun pause()70 fun seekTo(positionMs: Long)71 fun next()72 fun previous()73 fun jumpTo(index: Int)74 /** Stops the receiver app and closes the transport. */75 suspend fun disconnect()76 /** Owner shutdown: cancels jobs and closes sockets; idempotent. */77 fun close()78}7980expect fun createCastController(scope: CoroutineScope): CastController81