Files
utun2/src/stream/stream_wire.c
T
evgeny 7b4cacc439 Show stream playback buffer chains on desktop and Android
Send coalesced per-hop telemetry every 200 ms ahead of video DATA using the shared ETCP transport. Measure pending frames before ETCP handoff, retain stale and unavailable node values, and display the worst path with playback tempo.

Bump stream wire protocol to v7. Validate with 187 core tests, 21 desktop player tests, two Android indicator tests, and an Android APK build.
2026-10-25 15:21:23 +01:00

184 lines
11 KiB
C

#include <string.h>
#include "../../lib/debug_config.h"
#include "../transport_layer/etcp_api.h"
#include "stream_wire.h"
/* Заголовок одинаков у raw и router payload; cmd raw сохраняется внутри routed-кодограммы. */
int stream_wire_parse(const uint8_t *wire, size_t length, struct stream_message *m) {
if (!wire || !m || length < STREAM_WIRE_HEADER || wire[0] != ETCP_ID_STREAM || wire[1] != STREAM_WIRE_VERSION
|| wire[2] < STREAM_CMD_INFO_REQ || wire[2] > STREAM_CMD_TELEMETRY) {
DEBUG_WARN(DEBUG_CATEGORY_STREAM, "stream wire header invalid bytes=%zu", length); return STREAM_ERR_FORMAT;
}
struct stream_message result = {.command = wire[2], .subblock = wire[3], .payload = wire + STREAM_WIRE_HEADER,
.length = length - STREAM_WIRE_HEADER};
result.descriptor.group_id = stream_read_be(wire + 4, 8);
result.descriptor.source_node_id = stream_read_be(wire + 12, 8);
result.descriptor.source_epoch = stream_read_be(wire + 20, 8);
result.sender_epoch = stream_read_be(wire + 28, 8); result.receiver_epoch = stream_read_be(wire + 36, 8);
result.handle = stream_read_be(wire + 44, 8);
if (!result.descriptor.group_id || !result.descriptor.source_node_id || !result.descriptor.source_epoch || !result.sender_epoch) {
DEBUG_WARN(DEBUG_CATEGORY_STREAM, "stream wire zero key/epoch command=%u", result.command); return STREAM_ERR_FORMAT;
}
*m = result; return STREAM_OK;
}
void stream_wire_header(uint8_t *wire, const struct stream_message *m) {
wire[0] = ETCP_ID_STREAM; wire[1] = STREAM_WIRE_VERSION; wire[2] = m->command; wire[3] = m->subblock;
stream_write_be(wire + 4, m->descriptor.group_id, 8); stream_write_be(wire + 12, m->descriptor.source_node_id, 8);
stream_write_be(wire + 20, m->descriptor.source_epoch, 8); stream_write_be(wire + 28, m->sender_epoch, 8);
stream_write_be(wire + 36, m->receiver_epoch, 8); stream_write_be(wire + 44, m->handle, 8);
}
/* Подсказка не меняет подписанные RS-байты и не используется как доверенный media index. */
void stream_frame_timing_write(uint8_t *wire, const struct stream_frame_timing *t) {
memset(wire, 0, STREAM_FRAME_TIMING_SIZE);
stream_write_be(wire, t->first_frame, 8); stream_write_be(wire + 8, t->last_frame, 8);
stream_write_be(wire + 16, t->first_pts, 8); stream_write_be(wire + 24, t->last_pts, 8);
stream_write_be(wire + 32, t->period_us, 4); stream_write_be(wire + 36, t->started, 4);
stream_write_be(wire + 40, t->audio_us, 4);
wire[44] = t->valid | (t->video << 1) | (t->continuation << 2);
}
int stream_frame_timing_read(const uint8_t *wire, size_t length, struct stream_frame_timing *out) {
if (!wire || !out || length < STREAM_FRAME_TIMING_SIZE || (wire[44] & ~7u) || wire[45] || wire[46] || wire[47]) {
DEBUG_WARN(DEBUG_CATEGORY_STREAM, "stream frame timing invalid header bytes=%zu", length); return STREAM_ERR_FORMAT;
}
struct stream_frame_timing t = {.first_frame = stream_read_be(wire, 8), .last_frame = stream_read_be(wire + 8, 8),
.first_pts = stream_read_be(wire + 16, 8), .last_pts = stream_read_be(wire + 24, 8),
.period_us = (uint32_t)stream_read_be(wire + 32, 4), .started = (uint32_t)stream_read_be(wire + 36, 4),
.audio_us = (uint32_t)stream_read_be(wire + 40, 4), .valid = wire[44] & 1,
.video = (wire[44] >> 1) & 1, .continuation = (wire[44] >> 2) & 1};
if ((!t.valid && (t.video || t.continuation || t.audio_us || t.period_us || t.started || t.first_frame || t.last_frame
|| t.first_pts || t.last_pts))
|| (!t.video && (t.continuation || t.started || t.first_frame || t.last_frame || t.first_pts || t.last_pts))
|| (t.video && (t.last_frame < t.first_frame || t.last_pts < t.first_pts || t.last_pts > INT64_MAX
|| t.started > 16 || (!t.started && !t.continuation))) || t.audio_us > 320000) {
DEBUG_WARN(DEBUG_CATEGORY_STREAM, "stream frame timing invalid values frames=%u video=%u continuation=%u audio_us=%u",
t.started, t.video, t.continuation, t.audio_us); return STREAM_ERR_FORMAT;
}
*out = t; return STREAM_OK;
}
size_t stream_telemetry_write(uint8_t *wire, const struct stream_telemetry *t) {
wire[0] = t->count;
for (unsigned i = 0; i < t->count; i++) {
const struct stream_telemetry_row *r = &t->rows[i]; uint8_t *p = wire + 1 + i * STREAM_TELEMETRY_ROW_SIZE;
stream_write_be(p, r->node, 8); stream_write_be(p + 8, r->buffered_us, 8); stream_write_be(p + 16, r->sample, 8);
stream_write_be(p + 24, r->frames, 4); stream_write_be(p + 28, r->age_ms, 4); p[32] = r->flags;
}
return 1 + t->count * STREAM_TELEMETRY_ROW_SIZE;
}
int stream_telemetry_read(const uint8_t *wire, size_t length, struct stream_telemetry *out) {
if (!wire || !out || !length || !wire[0] || wire[0] > STREAM_PATH_MAX
|| length != 1u + wire[0] * STREAM_TELEMETRY_ROW_SIZE) {
DEBUG_WARN(DEBUG_CATEGORY_STREAM, "stream telemetry invalid size=%zu", length); return STREAM_ERR_FORMAT;
}
struct stream_telemetry t = {.count = wire[0]};
for (unsigned i = 0; i < t.count; i++) {
const uint8_t *p = wire + 1 + i * STREAM_TELEMETRY_ROW_SIZE;
struct stream_telemetry_row *r = &t.rows[i];
*r = (struct stream_telemetry_row){.node = stream_read_be(p, 8), .buffered_us = stream_read_be(p + 8, 8),
.sample = stream_read_be(p + 16, 8), .frames = (uint32_t)stream_read_be(p + 24, 4),
.age_ms = (uint32_t)stream_read_be(p + 28, 4), .flags = p[32]};
if (!r->node || r->buffered_us > INT64_MAX || (r->flags & ~7u)
|| (!(r->flags & STREAM_TELEMETRY_KNOWN) && (r->buffered_us || r->frames))) {
DEBUG_WARN(DEBUG_CATEGORY_STREAM, "stream telemetry invalid row=%u flags=%u", i, r->flags); return STREAM_ERR_FORMAT;
}
for (unsigned j = 0; j < i; j++) if (t.rows[j].node == r->node) {
DEBUG_WARN(DEBUG_CATEGORY_STREAM, "stream telemetry repeated node row=%u", i); return STREAM_ERR_FORMAT;
}
}
*out = t; return STREAM_OK;
}
void stream_telemetry_age(struct stream_telemetry *t, uint64_t now) {
uint64_t elapsed = now >= t->received_ms ? now - t->received_ms : 0;
for (unsigned i = 0; i < t->count; i++) {
struct stream_telemetry_row *r = &t->rows[i];
r->age_ms = elapsed > UINT32_MAX - r->age_ms ? UINT32_MAX : r->age_ms + (uint32_t)elapsed;
if (r->age_ms >= STREAM_TELEMETRY_STALE_MS) r->flags |= STREAM_TELEMETRY_STALE;
}
t->received_ms = now;
}
uint64_t stream_telemetry_total(const struct stream_telemetry *t) {
uint64_t total = 0;
for (unsigned i = 0; i < t->count; i++) {
uint64_t value = t->rows[i].buffered_us;
total = value > UINT64_MAX - total ? UINT64_MAX : total + value;
}
return total;
}
/* История блока проверяется отдельно от BGP-пути; получатель не должен уже встречаться в ней. */
int stream_path_validate(const struct stream_path *path, uint64_t source, uint64_t sender, uint64_t forbidden) {
if (!path || !path->count || path->count > STREAM_PATH_MAX || path->nodes[0] != source
|| path->nodes[path->count - 1] != sender) goto invalid;
for (unsigned i = 0; i < path->count; i++) {
if (!path->nodes[i] || (forbidden && path->nodes[i] == forbidden)) goto invalid;
for (unsigned j = 0; j < i; j++) if (path->nodes[i] == path->nodes[j]) goto invalid;
}
return STREAM_OK;
invalid:
DEBUG_WARN(DEBUG_CATEGORY_STREAM, "stream path invalid/loop source=%llx sender=%llx receiver=%llx count=%u",
(unsigned long long)source, (unsigned long long)sender, (unsigned long long)forbidden, path ? path->count : 0);
return STREAM_ERR_FORMAT;
}
int stream_path_parse(const uint8_t *wire, size_t length, struct stream_path *path, size_t *used) {
if (!wire || !path || !used || !length || !wire[0] || wire[0] > STREAM_PATH_MAX || length < 1u + wire[0] * 8u) {
DEBUG_WARN(DEBUG_CATEGORY_STREAM, "stream path size invalid bytes=%zu", length); return STREAM_ERR_FORMAT;
}
struct stream_path result = {.count = wire[0]};
for (unsigned i = 0; i < result.count; i++) result.nodes[i] = stream_read_be(wire + 1 + i * 8, 8);
*path = result; *used = 1 + result.count * 8; return STREAM_OK;
}
size_t stream_path_write(uint8_t *wire, const struct stream_path *path) {
wire[0] = path->count;
for (unsigned i = 0; i < path->count; i++) stream_write_be(wire + 1 + i * 8, path->nodes[i], 8);
return 1 + path->count * 8;
}
/* Неизвестное измерение явно отличается от нулевого lag/rate; числа не сужаются до старых uint16 RTT. */
void stream_metric_write(uint8_t *wire, const struct stream_metric_snapshot *s, int valid) {
wire[0] = valid ? 1 | (s->source_rate_valid ? 2 : 0) | (s->receive_rate_valid ? 4 : 0) | (s->stopped ? 8 : 0) : 0;
stream_write_be(wire + 1, s->lag_ms, 8); stream_write_be(wire + 9, s->source_rate_bytes_sec, 8);
stream_write_be(wire + 17, s->receive_rate_bytes_sec, 8); stream_write_be(wire + 25, s->blocks, 8);
stream_write_be(wire + 33, s->late_blocks, 8); stream_write_be(wire + 41, s->stops, 8);
}
int stream_metric_read(const uint8_t *wire, struct stream_metric_snapshot *s, int *valid) {
if (wire[0] & ~15u || (!(wire[0] & 1) && wire[0])) {
DEBUG_WARN(DEBUG_CATEGORY_STREAM, "stream metric flags invalid=%u", wire[0]); return STREAM_ERR_FORMAT;
}
*s = (struct stream_metric_snapshot){.lag_ms = stream_read_be(wire + 1, 8), .source_rate_bytes_sec = stream_read_be(wire + 9, 8),
.receive_rate_bytes_sec = stream_read_be(wire + 17, 8), .blocks = stream_read_be(wire + 25, 8),
.late_blocks = stream_read_be(wire + 33, 8), .stops = stream_read_be(wire + 41, 8),
.source_rate_valid = !!(wire[0] & 2), .receive_rate_valid = !!(wire[0] & 4), .stopped = !!(wire[0] & 8)};
*valid = !!(wire[0] & 1); return STREAM_OK;
}
/* END имеет собственный domain и подписывает ключ запуска + последний индекс; UINT64_MAX означает пустой поток. */
int stream_end_sign(const struct stream_descriptor *d, uint64_t last, const uint8_t key[32], uint8_t wire[STREAM_END_SIZE]) {
if (!d || !key || !wire) { DEBUG_ERROR(DEBUG_CATEGORY_STREAM, "stream END sign invalid arguments"); return STREAM_ERR_ARGUMENT; }
memcpy(wire, "stream_end", 10); stream_write_be(wire + 10, d->group_id, 8);
stream_write_be(wire + 18, d->source_node_id, 8); stream_write_be(wire + 26, d->source_epoch, 8);
stream_write_be(wire + 34, last, 8);
if (sc_ed25519_sign(key, wire, 42, wire + 42) != SC_OK) {
DEBUG_ERROR(DEBUG_CATEGORY_STREAM, "stream END signature failed"); return STREAM_ERR_CRYPTO;
}
return STREAM_OK;
}
int stream_end_verify(const struct stream_descriptor *d, const uint8_t key[32], const uint8_t *wire, size_t length, uint64_t *last) {
if (!d || !key || !wire || !last || length != STREAM_END_SIZE || memcmp(wire, "stream_end", 10)
|| stream_read_be(wire + 10, 8) != d->group_id || stream_read_be(wire + 18, 8) != d->source_node_id
|| stream_read_be(wire + 26, 8) != d->source_epoch || sc_ed25519_verify(key, wire, 42, wire + 42) != SC_OK) {
DEBUG_WARN(DEBUG_CATEGORY_STREAM, "stream END invalid signature/key bytes=%zu", length); return STREAM_ERR_SIGNATURE;
}
*last = stream_read_be(wire + 34, 8); return STREAM_OK;
}