v0.9.1
This commit is contained in:
1 parent
271ad2a492
commit
61d4e54b72
6 files changed
+179
-44
No files matched your search
@@ -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<HChannel> = emptyList()
|
||||
private var events: Map<Long, List<HEvent>> = 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<TextView>(R.id.t_name).text = getString(R.string.loading)
|
||||
findViewById<TextView>(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<TextView>(R.id.t_rate).text = rate?.let { mbit(it) } ?: "–"
|
||||
findViewById<TextView>(R.id.t_rate_sub).text = mux?.takeIf { it > 0 }?.let { "MUX " + mbit(it.toDouble()) } ?: ""
|
||||
}
|
||||
@@ -596,12 +673,10 @@ class LiveActivity : Activity() {
|
||||
findViewById<TextView>(R.id.t_stream).text = t.optString("stream")
|
||||
findViewById<TextView>(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<TextView>(R.id.t_cpu).text = cpu?.let { "%.0f%%".format(Locale.ROOT, it) } ?: "–"
|
||||
findViewById<TextView>(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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Float>()
|
||||
}
|
||||
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<Point>()
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user