feat(media,android): live channels in ExoPlayer — LiveWatcher, ShadeLiveDataSource

LiveWatcher is the Android LiveWatchClient (OkHttp WebSocket, subscribe
with the read token, reconnect with backoff, backlog deduplicated by the
subscriber). ShadeLiveDataSource turns frames into one endless WebM/fMP4
stream for ExoPlayer's extractors: init, then media from the first
keyframe, and after a gap nothing until the next keyframe. Late joiners
start at 0:00 (fMP4: per-join TimestampAdjuster; WebM: cluster Timecode
rewritten in place). A changed init fails with a ParserException (not
retried). The demo app plays live video and follows log channels.
LiveSubscriber.close() wipes keys, as in TS.

Verified (Robolectric): late join on H.264/AAC fMP4 and VP8/Opus WebM cut
by the TS splitter (fixtures from scripts/make-live-fixtures.ts) plays
from the keyframe, rebased (mutation check: without the rebase the WebM
test fails, 5020 vs 4250 ms); gap recovery; format change; LiveWatcher
against MockWebServer with a dropped connection and backlog replay.
Kotlin 81/0. Not yet run on a device.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 21:53:17 +02:00
parent 8c7300fa51
commit 9efe1bcfd3
13 changed files with 848 additions and 15 deletions

View File

@@ -10,6 +10,7 @@ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
import androidx.annotation.OptIn
import androidx.media3.common.PlaybackException
@@ -17,12 +18,18 @@ import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import no.zyon.shade.media.LiveContent
import no.zyon.shade.media.LiveGrant
import no.zyon.shade.media.MediaGrant
import no.zyon.shade.media.MediaReader
import no.zyon.shade.media.ReceivedFrame
import no.zyon.shade.media.android.LiveWatcher
import no.zyon.shade.media.android.ShadeMedia
import java.util.concurrent.Executors
/**
* Plays a sealed Shade film, video, song or picture-less audio file.
* Plays a sealed Shade film, video or song, or watches a live channel:
* live video/audio through ExoPlayer, a live log as scrolling text.
*
* The link comes from a share (`ACTION_SEND` text), from
* `adb shell am start -n no.zyon.shade.mediademo/.PlayerActivity --es link '<link>'`,
@@ -34,8 +41,11 @@ class PlayerActivity : Activity() {
private val io = Executors.newSingleThreadExecutor()
private var player: ExoPlayer? = null
private var reader: MediaReader? = null
private var watcher: LiveWatcher? = null
private lateinit var status: TextView
private lateinit var view: PlayerView
private lateinit var log: TextView
private lateinit var logScroll: ScrollView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -52,6 +62,16 @@ class PlayerActivity : Activity() {
setPadding(24, 12, 24, 12)
}
view = PlayerView(this).apply { setBackgroundColor(Color.BLACK) }
log = TextView(this).apply {
setTextColor(Color.rgb(214, 219, 225))
typeface = android.graphics.Typeface.MONOSPACE
textSize = 11f
setPadding(24, 12, 24, 12)
}
logScroll = ScrollView(this).apply {
addView(log)
visibility = android.view.View.GONE
}
val bar = LinearLayout(this).apply {
orientation = LinearLayout.HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
@@ -65,6 +85,7 @@ class PlayerActivity : Activity() {
addView(bar, LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT))
addView(status, LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT))
addView(view, LinearLayout.LayoutParams(MATCH_PARENT, 0, 1f))
addView(logScroll, LinearLayout.LayoutParams(MATCH_PARENT, 0, 1f))
},
)
linkFrom(intent)?.let {
@@ -86,36 +107,100 @@ class PlayerActivity : Activity() {
private fun play(link: String) {
if (link.isEmpty()) return
stop()
status.text = "Opening…"
io.execute {
val opened = runCatching { ShadeMedia.open(link) }
runOnUiThread {
opened.fold(
onSuccess = { start(it) },
onFailure = { status.text = "Cannot open: ${it.message}" },
)
val grant = runCatching { ShadeMedia.grant(link) }.getOrElse {
status.text = "Not a Shade link: ${it.message}"
return
}
when (grant) {
is LiveGrant -> watch(grant)
is MediaGrant -> {
status.text = "Opening…"
io.execute {
val opened = runCatching { grant.open() }
runOnUiThread {
opened.fold(
onSuccess = { start(it) },
onFailure = { status.text = "Cannot open: ${it.message}" },
)
}
}
}
}
}
private fun start(r: MediaReader) {
reader = r
private fun watch(g: LiveGrant) {
val name = g.meta?.name ?: "live channel"
val mime = g.meta?.mime ?: ""
if (mime.startsWith("video/") || mime.startsWith("audio/")) {
showPlayer(true)
val p = newPlayer()
status.text = "$name · live · end-to-end encrypted"
p.setMediaSource(ShadeMedia.liveMediaSource(g))
p.prepare()
p.play()
return
}
// Text and images: follow the channel directly.
showPlayer(false)
log.text = ""
status.text = "$name · connecting…"
watcher = LiveWatcher(g, object : LiveWatcher.Listener {
override fun onFrame(frame: ReceivedFrame) {
val c = frame.content
runOnUiThread {
when (c) {
is LiveContent.Text -> appendLog(c.text)
is LiveContent.End -> status.text = "$name · the broadcast has ended"
else -> Unit
}
}
}
override fun onState(state: LiveWatcher.State) {
runOnUiThread { status.text = "$name · ${state.name.lowercase()} · end-to-end encrypted" }
}
})
}
private fun appendLog(text: String) {
val atBottom = !logScroll.canScrollVertically(1)
log.append(text)
// Bound the view: a log channel runs for days.
if (log.length() > MAX_LOG_CHARS) log.text = log.text.subSequence(log.length() - MAX_LOG_CHARS / 2, log.length())
if (atBottom) logScroll.post { logScroll.fullScroll(android.view.View.FOCUS_DOWN) }
}
private fun showPlayer(video: Boolean) {
view.visibility = if (video) android.view.View.VISIBLE else android.view.View.GONE
logScroll.visibility = if (video) android.view.View.GONE else android.view.View.VISIBLE
}
private fun newPlayer(): ExoPlayer {
val p = ExoPlayer.Builder(this).build()
player = p
view.player = p
val name = r.meta.name ?: "sealed file"
status.text = "$name · ${r.meta.mime ?: "?"} · ${r.size / 1024} KiB · end-to-end encrypted"
p.addListener(object : Player.Listener {
override fun onPlayerError(error: PlaybackException) {
status.text = "Playback failed: ${error.cause?.message ?: error.message}"
}
})
return p
}
private fun start(r: MediaReader) {
reader = r
showPlayer(true)
val p = newPlayer()
val name = r.meta.name ?: "sealed file"
status.text = "$name · ${r.meta.mime ?: "?"} · ${r.size / 1024} KiB · end-to-end encrypted"
p.setMediaSource(ShadeMedia.mediaSource(r))
p.prepare()
p.play()
}
private fun stop() {
watcher?.close()
watcher = null
view.player = null
player?.release()
player = null
@@ -133,4 +218,8 @@ class PlayerActivity : Activity() {
io.shutdown()
super.onDestroy()
}
private companion object {
const val MAX_LOG_CHARS = 200_000
}
}