feat(media): shade live images — a webcam, a slideshow or a watched folder, end to end

Pictures were a live content type the watch page could show but nothing
could publish. `shade live images`:
- stdin: back-to-back JPEG/PNG, e.g.
  `ffmpeg -f v4l2 -i /dev/video0 -r 1 -c:v mjpeg -f image2pipe -`;
- files/directories: a slideshow, one picture every --every seconds;
- --watch DIR: each new picture once its size holds still (cameras
  write in pieces).
Each picture is a keyframe, so a late viewer opens on the latest. The meta
carries the first picture's type (MIME_RE rightly refuses image/*).

ImageStreamSplitter walks JPEG markers (lengths to SOS, then the
entropy-coded data, where every 0xFF is stuffed) and PNG chunks to IEND,
so an EXIF thumbnail — a whole JPEG inside APP1 — does not end a picture.
Bounded at 16 MiB per picture, junk refused at its first bytes.

The Android demo shows live picture channels and sealed pictures
(decoded off the UI thread, downsampled to the screen).

Verified: splitter on real ffmpeg MJPEG/PNG under random pipe chunking,
the thumbnail case, truncation, junk and the size cap (4 tests); in
Chromium a 2 fps MJPEG "webcam" through the relay (8 → 14 pictures in 3 s,
960×540); a watched folder with a PNG written in two halves (sent once,
whole) and a .txt ignored; slideshow; error paths. Demo APK builds; not
run on a device (none connected).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 22:53:37 +02:00
parent 1653060494
commit d87db5f4dc
9 changed files with 436 additions and 10 deletions

View File

@@ -2,6 +2,8 @@ package no.zyon.shade.mediademo
import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
@@ -9,6 +11,7 @@ import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
@@ -28,8 +31,9 @@ import no.zyon.shade.media.android.ShadeMedia
import java.util.concurrent.Executors
/**
* Plays a sealed Shade film, video or song, or watches a live channel:
* live video/audio through ExoPlayer, a live log as scrolling text.
* Plays a sealed Shade film, video, song or picture, or watches a live
* channel: live video/audio through ExoPlayer, a live log as scrolling text,
* live pictures (a webcam, a watched folder) one after another.
*
* The link comes from a share (`ACTION_SEND` text), from
* `adb shell am start -n no.zyon.shade.mediademo/.PlayerActivity --es link '<link>'`,
@@ -46,6 +50,7 @@ class PlayerActivity : Activity() {
private lateinit var view: PlayerView
private lateinit var log: TextView
private lateinit var logScroll: ScrollView
private lateinit var picture: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -72,6 +77,11 @@ class PlayerActivity : Activity() {
addView(log)
visibility = android.view.View.GONE
}
picture = ImageView(this).apply {
scaleType = ImageView.ScaleType.FIT_CENTER
setBackgroundColor(Color.BLACK)
visibility = android.view.View.GONE
}
val bar = LinearLayout(this).apply {
orientation = LinearLayout.HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
@@ -86,6 +96,7 @@ class PlayerActivity : Activity() {
addView(status, LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT))
addView(view, LinearLayout.LayoutParams(MATCH_PARENT, 0, 1f))
addView(logScroll, LinearLayout.LayoutParams(MATCH_PARENT, 0, 1f))
addView(picture, LinearLayout.LayoutParams(MATCH_PARENT, 0, 1f))
},
)
linkFrom(intent)?.let {
@@ -132,7 +143,7 @@ class PlayerActivity : Activity() {
val name = g.meta?.name ?: "live channel"
val mime = g.meta?.mime ?: ""
if (mime.startsWith("video/") || mime.startsWith("audio/")) {
showPlayer(true)
show(View.PLAYER)
val p = newPlayer()
status.text = "$name · live · end-to-end encrypted"
p.setMediaSource(ShadeMedia.liveMediaSource(g))
@@ -141,7 +152,7 @@ class PlayerActivity : Activity() {
return
}
// Text and images: follow the channel directly.
showPlayer(false)
show(if (mime.startsWith("image/")) View.PICTURE else View.LOG)
log.text = ""
status.text = "$name · connecting…"
watcher = LiveWatcher(g, object : LiveWatcher.Listener {
@@ -149,7 +160,11 @@ class PlayerActivity : Activity() {
val c = frame.content
runOnUiThread {
when (c) {
is LiveContent.Text -> appendLog(c.text)
is LiveContent.Text -> {
show(View.LOG)
appendLog(c.text)
}
is LiveContent.Image -> showPicture(c.bytes)
is LiveContent.End -> status.text = "$name · the broadcast has ended"
else -> Unit
}
@@ -170,9 +185,33 @@ class PlayerActivity : Activity() {
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 enum class View { PLAYER, LOG, PICTURE }
private fun show(which: View) {
fun vis(on: Boolean) = if (on) android.view.View.VISIBLE else android.view.View.GONE
view.visibility = vis(which == View.PLAYER)
logScroll.visibility = vis(which == View.LOG)
picture.visibility = vis(which == View.PICTURE)
}
/**
* Decode off the UI thread, scaled to the screen: a 40-megapixel photo
* decoded whole is 160 MB of bitmap. A picture that will not decode is
* reported and skipped — the next one may.
*/
private fun showPicture(bytes: ByteArray) {
show(View.PICTURE)
val target = maxOf(resources.displayMetrics.widthPixels, resources.displayMetrics.heightPixels)
io.execute {
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, bounds)
var sample = 1
while (maxOf(bounds.outWidth, bounds.outHeight) / (sample * 2) >= target) sample *= 2
val bmp: Bitmap? = BitmapFactory.decodeByteArray(bytes, 0, bytes.size, BitmapFactory.Options().apply { inSampleSize = sample })
runOnUiThread {
if (bmp == null) status.text = "${status.text} · a picture could not be decoded" else picture.setImageBitmap(bmp)
}
}
}
private fun newPlayer(): ExoPlayer {
@@ -189,7 +228,21 @@ class PlayerActivity : Activity() {
private fun start(f: ShadeFilm) {
film = f
showPlayer(true)
val meta0 = f.reader.meta
if (meta0.mime?.startsWith("image/") == true) {
status.text = "${meta0.name ?: "picture"} · ${meta0.mime} · ${f.reader.size / 1024} KiB · end-to-end encrypted"
if (f.reader.size > MAX_PICTURE_BYTES) {
status.text = "${status.text} · too large to show here"
return
}
show(View.PICTURE)
io.execute {
val bytes = runCatching { f.reader.read(0, f.reader.size.toInt()) }
runOnUiThread { bytes.fold(onSuccess = { showPicture(it) }, onFailure = { status.text = "Cannot read: ${it.message}" }) }
}
return
}
show(View.PLAYER)
val p = newPlayer()
val meta = f.reader.meta
val subs = if (f.subtitles.isEmpty()) "" else " · ${f.subtitles.size} subtitle track(s)"
@@ -200,6 +253,7 @@ class PlayerActivity : Activity() {
}
private fun stop() {
picture.setImageDrawable(null)
watcher?.close()
watcher = null
view.player = null
@@ -222,5 +276,6 @@ class PlayerActivity : Activity() {
private companion object {
const val MAX_LOG_CHARS = 200_000
const val MAX_PICTURE_BYTES = 64L * 1024 * 1024
}
}