diff --git a/.kotlin/errors/errors-1794932937869.log b/.kotlin/errors/errors-1794932937869.log new file mode 100644 index 0000000..1219b50 --- /dev/null +++ b/.kotlin/errors/errors-1794932937869.log @@ -0,0 +1,4 @@ +kotlin version: 2.0.21 +error message: The daemon has terminated unexpectedly on startup attempt #1 with error code: 0. The daemon process output: + 1. Kotlin compile daemon is ready + diff --git a/README.md b/README.md index 99e8e85..4825d8d 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ Up / CH+ – next channel, down / CH- – previous · digits – channel number **Programme grid** (also *Programme guide* on the start screen): channels down, time across, a "now" line and the details of the highlighted programme on top. Up/down – channel, left/right – programme, CH+/CH- – a page, OK – details with record / watch, RED – record or cancel, GREEN – back to now, BACK – close. Some Sony remotes keep the GUIDE key for the TV's own guide – the right arrow always works. -The tuner card (top right, optional) shows the adapter, frequency, lock, three small charts – signal/SNR, server CPU (whole host and the simply-dvb process, needs simply-dvb 0.1.13+) and the stream bitrate actually received by the TV (multiplex bitrate below) – plus modulation/FEC, BER and error counters. +The tuner card (top right, optional) shows the adapter, frequency, lock, three small charts – signal/SNR, server CPU (whole host and the simply-dvb process, needs simply-dvb 0.1.13+) and the stream bitrate actually received by the TV (multiplex bitrate below) – plus modulation/FEC, BER and error counters. The charts cover the last 3 minutes: signal/SNR and CPU come from the statistics history simply-dvb keeps in memory (every 2 s, 15 minutes; simply-dvb 0.1.18+), the received bitrate is measured by the TV while it plays – so a chart is full as soon as the card opens. **Smooth video:** *Tunnelled video playback* (*Settings*, on by default) lets the TV's own video pipeline decode and sync the picture – the same path the built-in tuner uses. If a channel shows no picture with it, switch it off. The tuner card's white line shows what the TV does with the stream: resolution, frames shown per second, dropped frames (red), buffer and decoder; the last line shows simply-dvb's driver timing (time of a signal statistics read and the longest pause in the stream). diff --git a/README.pl.md b/README.pl.md index 3f157b3..18503a8 100644 --- a/README.pl.md +++ b/README.pl.md @@ -77,7 +77,7 @@ Góra / CH+ – następny kanał, dół / CH- – poprzedni · cyfry – numer k **Siatka programu** (także *Program TV* na ekranie startowym): kanały w pionie, czas w poziomie, linia „teraz” i szczegóły zaznaczonej audycji u góry. Góra/dół – kanał, lewo/prawo – audycja, CH+/CH- – strona, OK – szczegóły z nagrywaniem / oglądaniem, CZERWONY – nagraj lub anuluj, ZIELONY – powrót do „teraz”, WSTECZ – zamknij. Niektóre piloty Sony zostawiają przycisk GUIDE dla przewodnika telewizora – strzałka w prawo działa zawsze. -Karta tunera (prawy górny róg, opcjonalna) pokazuje adapter, częstotliwość, synchronizację, trzy małe wykresy – sygnał/SNR, CPU serwera (cały host i proces simply-dvb, wymaga simply-dvb 0.1.13+) oraz przepływność strumienia faktycznie odbieranego przez telewizor (pod spodem przepływność multipleksu) – a także modulację/FEC, BER i liczniki błędów. +Karta tunera (prawy górny róg, opcjonalna) pokazuje adapter, częstotliwość, synchronizację, trzy małe wykresy – sygnał/SNR, CPU serwera (cały host i proces simply-dvb, wymaga simply-dvb 0.1.13+) oraz przepływność strumienia faktycznie odbieranego przez telewizor (pod spodem przepływność multipleksu) – a także modulację/FEC, BER i liczniki błędów. Wykresy obejmują ostatnie 3 minuty: sygnał/SNR i CPU pochodzą z historii statystyk, którą simply-dvb trzyma w pamięci (co 2 s, 15 minut; simply-dvb 0.1.18+), a przepływność odbieraną mierzy telewizor podczas odtwarzania – wykres jest więc pełny od razu po otwarciu karty. **Płynny obraz:** *Tunelowanie wideo* (*Ustawienia*, domyślnie włączone) przekazuje dekodowanie i synchronizację obrazu do toru wideo telewizora – tego samego, którego używa wbudowany tuner. Jeśli na jakimś kanale nie ma wtedy obrazu, wyłącz je. Biała linia na karcie tunera pokazuje, co telewizor robi ze strumieniem: rozdzielczość, wyświetlane klatki na sekundę, gubione klatki (na czerwono), bufor i dekoder; ostatnia linia to czasy sterownika w simply-dvb (odczyt statystyk sygnału i najdłuższa przerwa w strumieniu). diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 004f768..ff94a42 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -20,8 +20,8 @@ android { applicationId = "io.simplytv" minSdk = 26 targetSdk = 34 - versionCode = 10 - versionName = "0.9.0" + versionCode = 11 + versionName = "0.9.1" } signingConfigs { diff --git a/app/src/main/java/io/simplytv/ui/LiveActivity.kt b/app/src/main/java/io/simplytv/ui/LiveActivity.kt index af5b7d3..de624f1 100644 --- a/app/src/main/java/io/simplytv/ui/LiveActivity.kt +++ b/app/src/main/java/io/simplytv/ui/LiveActivity.kt @@ -67,6 +67,29 @@ class LiveActivity : Activity() { private lateinit var spark: SparkView private lateinit var cpuSpark: SparkView private lateinit var rateSpark: SparkView + + /** Server statistics history already shown (unix s), 0: ask for the last minutes. */ + private var histSince = 0L + /** Tuner + multiplex the signal chart belongs to; another one starts a fresh chart. */ + private var histKey: String? = null + /** Received bitrate, measured here every 2 s while playing (no network). */ + private var rateLast: Float? = null + private var rateBytes = -1L + private var rateAt = 0L + private val rateTick = object : Runnable { + override fun run() { + val now = System.currentTimeMillis() + val b = meter.total() + if (rateBytes >= 0 && now > rateAt) { + val v = ((b - rateBytes) * 8000.0 / (now - rateAt)).toFloat() + rateLast = v + rateSpark.add(now / 1000, v) + } + rateBytes = b + rateAt = now + main.postDelayed(this, 2000) + } + } private val meter = ByteMeter() private var channels: List = emptyList() private var events: Map> = emptyMap() @@ -506,7 +529,7 @@ class LiveActivity : Activity() { private fun showCard(seconds: Int = 8) { cardUntil = System.currentTimeMillis() + seconds * 1000L if (card.visibility != View.VISIBLE) { - spark.clear() + // charts keep their data; the next poll adds what the server saw meanwhile findViewById(R.id.t_name).text = getString(R.string.loading) findViewById(R.id.t_stream).text = "" card.visibility = View.VISIBLE @@ -530,19 +553,74 @@ class LiveActivity : Activity() { } io.execute { val t = try { - Server.getJson(this, "/api/status/inputs?channel=${ch.id}").optJSONArray("entries")?.optJSONObject(0) + val since = if (histSince > 0) histSince else System.currentTimeMillis() / 1000 - 300 + Server.getJson(this, "/api/status/inputs?channel=${ch.id}&since=$since").optJSONArray("entries")?.optJSONObject(0) } catch (_: Exception) { null } main.post { - val rate = meter.rate() - if (t != null) renderCard(t, rate) else renderRate(rate, null) + val rate = rateLast?.toDouble() + if (t != null) { + addHistory(t) + renderCard(t, rate) + } else { + renderRate(rate, null) + } renderVideo() main.postDelayed({ pollCard() }, 1000) } } } + /** + * Adds the server's statistics samples (every 2 s, kept 15 min) to the charts: signal / + * SNR and server CPU show the last minutes as soon as the card opens. + */ + private fun addHistory(t: JSONObject) { + val key = t.optString("input") + "|" + t.optString("stream") + if (key != histKey) { + if (histKey != null) { + // another tuner or multiplex: its own history, from the start + spark.clear() + histKey = key + histSince = 0 + return + } + histKey = key + } + if (!t.has("history")) { + // server older than 0.1.18: no history, chart the live values + val now = System.currentTimeMillis() / 1000 + spark.add(now, num(t, "snr_db")?.toFloat(), num(t, "strength_pct")?.toFloat()) + cpuSpark.add(now, num(t, "server_cpu")?.toFloat(), num(t, "server_proc_cpu")?.toFloat()) + return + } + var newest = histSince + t.optJSONArray("history")?.let { h -> + for (i in 0 until h.length()) { + val r = h.optJSONArray(i) ?: continue + val ts = r.optLong(0) + val sig = if (r.isNull(1)) null else r.optDouble(1).toFloat() + val snr = if (r.isNull(2)) null else r.optDouble(2).toFloat() + spark.add(ts, snr, sig) + newest = maxOf(newest, ts) + } + } + t.optJSONArray("host_history")?.let { h -> + for (i in 0 until h.length()) { + val r = h.optJSONArray(i) ?: continue + val ts = r.optLong(0) + cpuSpark.add( + ts, + if (r.isNull(1)) null else r.optDouble(1).toFloat(), + if (r.isNull(2)) null else r.optDouble(2).toFloat(), + ) + newest = maxOf(newest, ts) + } + } + histSince = newest + } + private fun num(o: JSONObject, k: String): Double? = if (o.isNull(k) || !o.has(k)) null else o.optDouble(k).takeIf { !it.isNaN() } private fun mbit(bps: Double) = if (bps >= 1e6) "%.1f Mb/s".format(Locale.ROOT, bps / 1e6) else "%.0f kb/s".format(Locale.ROOT, bps / 1e3) @@ -579,7 +657,6 @@ class LiveActivity : Activity() { } private fun renderRate(rate: Double?, mux: Long?) { - if (rate != null) rateSpark.push(rate.toFloat()) findViewById(R.id.t_rate).text = rate?.let { mbit(it) } ?: "–" findViewById(R.id.t_rate_sub).text = mux?.takeIf { it > 0 }?.let { "MUX " + mbit(it.toDouble()) } ?: "" } @@ -596,12 +673,10 @@ class LiveActivity : Activity() { findViewById(R.id.t_stream).text = t.optString("stream") findViewById(R.id.t_signal).text = (sig?.let { "%.0f%%".format(Locale.ROOT, it) } ?: "–") + " " + (snr?.let { "%.0fdB".format(Locale.ROOT, it) } ?: "") - spark.add(snr?.toFloat(), sig?.toFloat()) // server load val cpu = num(t, "server_cpu") val proc = num(t, "server_proc_cpu") val temp = num(t, "server_temp") - cpuSpark.push(cpu?.toFloat(), proc?.toFloat()) findViewById(R.id.t_cpu).text = cpu?.let { "%.0f%%".format(Locale.ROOT, it) } ?: "–" findViewById(R.id.t_cpu_sub).text = listOfNotNull( proc?.let { "dvb %.1f%%".format(Locale.ROOT, it) }, @@ -838,8 +913,16 @@ class LiveActivity : Activity() { return true } + override fun onStart() { + super.onStart() + rateBytes = -1 + main.removeCallbacks(rateTick) + main.post(rateTick) + } + override fun onStop() { super.onStop() + main.removeCallbacks(rateTick) player.stop() } diff --git a/app/src/main/java/io/simplytv/ui/SparkView.kt b/app/src/main/java/io/simplytv/ui/SparkView.kt index 6173c3f..5e01fba 100644 --- a/app/src/main/java/io/simplytv/ui/SparkView.kt +++ b/app/src/main/java/io/simplytv/ui/SparkView.kt @@ -9,26 +9,36 @@ import android.util.AttributeSet import android.view.View /** - * Tiny line chart for the tuner card. Each series has its own colour and scale - * (`max` > 0: fixed 0..max, `max` = 0: scaled to the largest visible value). - * By default: SNR (0–35 dB) and signal (0–100 %). + * Tiny time-based line chart for the tuner card: points carry their own time (unix seconds) + * and the chart shows the last [window] seconds, so history loaded from the server and + * points added live line up, and sparse or missing samples stay where they belong in time. + * Each series has its own colour and scale (`max` > 0: fixed 0..max, `max` = 0: scaled to + * the largest visible value). By default: SNR (0–35 dB) and signal (0–100 %). */ class SparkView(ctx: Context, attrs: AttributeSet? = null) : View(ctx, attrs) { - private class Series(val paint: Paint, val max: Float, val fill: Boolean) { - val v = ArrayList() - } + private class Series(val paint: Paint, val max: Float, val fill: Boolean) - private val keep = 90 + private class Point(val t: Long, val v: FloatArray) + + /** Visible time range, seconds. */ + var window = 180L + + /** A gap longer than this (seconds) breaks the line. */ + var maxGap = 15L + + private val points = ArrayList() private var series = listOf( Series(stroke("#7aa7ff", 3f), 35f, false), Series(stroke("#bdd293", 2f).apply { alpha = 170 }, 100f, false), ) private val pGrid = Paint().apply { color = Color.parseColor("#22ffffff"); strokeWidth = 1f } + private val pFill = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL } private fun stroke(c: String, w: Float) = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.parseColor(c) strokeWidth = w style = Paint.Style.STROKE + strokeJoin = Paint.Join.ROUND } /** Replaces the series: (colour, max, filled area). */ @@ -38,49 +48,87 @@ class SparkView(ctx: Context, attrs: AttributeSet? = null) : View(ctx, attrs) { } fun clear() { - series.forEach { it.v.clear() } + points.clear() invalidate() } - /** One value per series (null draws 0). */ - fun push(vararg values: Float?) { - series.forEachIndexed { i, s -> - s.v.add(values.getOrNull(i) ?: 0f) - while (s.v.size > keep) s.v.removeAt(0) + /** Newest point's time, or 0. */ + fun lastTime(): Long = points.lastOrNull()?.t ?: 0 + + /** Adds a point at unix time `t` (seconds); one value per series, null draws 0. */ + fun add(t: Long, vararg values: Float?) { + if (points.isNotEmpty() && t <= points.last().t) { + if (t == points.last().t) return + // out of order (history after live points): insert in place + val i = points.indexOfFirst { it.t >= t } + if (i >= 0 && points[i].t == t) return + points.add(if (i < 0) points.size else i, Point(t, FloatArray(series.size) { values.getOrNull(it) ?: 0f })) + } else { + points.add(Point(t, FloatArray(series.size) { values.getOrNull(it) ?: 0f })) } + val cut = points.last().t - window - maxGap + while (points.isNotEmpty() && points.first().t < cut) points.removeAt(0) invalidate() } - /** Compatibility: SNR (dB) and signal (%). */ - fun add(snrDb: Float?, signalPct: Float?) = push(snrDb, signalPct) - override fun onDraw(c: Canvas) { val w = width.toFloat() val h = height.toFloat() for (i in 1..3) c.drawLine(0f, h * i / 4, w, h * i / 4, pGrid) - for (s in series.asReversed()) line(c, s, w, h) + if (points.size < 2) return + val now = System.currentTimeMillis() / 1000 + val end = maxOf(now, points.last().t) + for (si in series.indices.reversed()) line(c, si, w, h, end) } - private fun line(c: Canvas, s: Series, w: Float, h: Float) { - val v = s.v - if (v.size < 2) return - val top = if (s.max > 0) s.max else (v.maxOrNull() ?: 0f).coerceAtLeast(1e-6f) * 1.15f - val step = w / (keep - 1) - val x0 = w - (v.size - 1) * step + private fun line(c: Canvas, si: Int, w: Float, h: Float, end: Long) { + val s = series[si] + val from = end - window + val visible = points.filter { it.t >= from - maxGap } + if (visible.size < 2) return + val top = if (s.max > 0) s.max else (visible.maxOfOrNull { it.v[si] } ?: 0f).coerceAtLeast(1e-6f) * 1.15f + fun x(t: Long) = w - (end - t).toFloat() / window * w + fun y(v: Float) = h - 2 - (v / top).coerceIn(0f, 1f) * (h - 4) val path = Path() - v.forEachIndexed { i, y -> - val px = x0 + i * step - val py = h - 2 - (y / top).coerceIn(0f, 1f) * (h - 4) - if (i == 0) path.moveTo(px, py) else path.lineTo(px, py) + val area = Path() + var open = false + var startX = 0f + var lastX = 0f + var prevT = 0L + for (p in visible) { + val px = x(p.t) + val py = y(p.v[si]) + if (!open || p.t - prevT > maxGap) { + if (open && s.fill) closeArea(area, lastX, startX, h) + path.moveTo(px, py) + if (s.fill) area.moveTo(px, py) + startX = px + open = true + } else { + path.lineTo(px, py) + if (s.fill) area.lineTo(px, py) + } + lastX = px + prevT = p.t } if (s.fill) { - val area = Path(path) - area.lineTo(w, h) - area.lineTo(x0, h) - area.close() - val p = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = s.paint.color; alpha = 45; style = Paint.Style.FILL } - c.drawPath(area, p) + closeArea(area, lastX, startX, h) + pFill.color = s.paint.color + pFill.alpha = 45 + c.save() + c.clipRect(0f, 0f, w, h) + c.drawPath(area, pFill) + c.restore() } + c.save() + c.clipRect(0f, 0f, w, h) c.drawPath(path, s.paint) + c.restore() + } + + private fun closeArea(area: Path, lastX: Float, startX: Float, h: Float) { + area.lineTo(lastX, h) + area.lineTo(startX, h) + area.close() } }