Files
utun2/src/call/call_audio_batch.c
T

101 lines
5.7 KiB
C

#include "call_audio_batch.h"
#include "../routing_layer/etcp_router.h"
#include "../../lib/debug_config.h"
#include <string.h>
static int valid_header(const struct call_pkt_media *h) {
return h->subcmd == CALL_SUBCMD_MEDIA && h->call_id && h->track_id == CALL_TRACK_AUDIO
&& h->count && h->count <= CALL_AUDIO_BATCH_MAX && h->codec_delay_samples <= CALL_OPUS_SAMPLE_RATE;
}
static int valid_frame(const struct call_pkt_media *h, const struct call_pkt_audio_frame *f) {
return f->length && f->length <= CALL_AUDIO_MAX_OPUS && (f->capture_pts_us == INT64_MIN
|| (f->capture_pts_us >= 0 && f->capture_pts_us <= INT64_MAX / 2 && h->capture_epoch));
}
int call_audio_batch_encode(const struct call_audio_batch_view *batch, uint8_t *out, size_t capacity, size_t *length) {
if (!batch || !out || !length || !valid_header(&batch->header)) goto invalid;
size_t total = CALL_MEDIA_HDR_SIZE;
for (unsigned i = 0; i < batch->header.count; i++) {
if (!batch->frames[i].opus || !valid_frame(&batch->header, &batch->frames[i].header)) goto invalid;
total += CALL_AUDIO_FRAME_HDR_SIZE + batch->frames[i].header.length;
}
if (total > capacity || total > CALL_MEDIA_MAX_BYTES) goto invalid;
memcpy(out, &batch->header, CALL_MEDIA_HDR_SIZE);
size_t offset = CALL_MEDIA_HDR_SIZE;
for (unsigned i = 0; i < batch->header.count; i++) {
const struct call_audio_frame_view *f = &batch->frames[i];
memcpy(out + offset, &f->header, CALL_AUDIO_FRAME_HDR_SIZE); offset += CALL_AUDIO_FRAME_HDR_SIZE;
memcpy(out + offset, f->opus, f->header.length); offset += f->header.length;
}
*length = total;
return 0;
invalid:
DEBUG_WARN(DEBUG_CATEGORY_CALL, "audio batch encode invalid fields/capacity=%zu", capacity); return -1;
}
int call_audio_batch_parse(const uint8_t *wire, size_t length, struct call_audio_batch_view *out) {
if (!wire || !out || length < CALL_MEDIA_HDR_SIZE || length > CALL_MEDIA_MAX_BYTES) goto invalid;
struct call_audio_batch_view result = {0};
memcpy(&result.header, wire, CALL_MEDIA_HDR_SIZE);
if (!valid_header(&result.header)) goto invalid;
size_t offset = CALL_MEDIA_HDR_SIZE;
for (unsigned i = 0; i < result.header.count; i++) {
struct call_audio_frame_view *f = &result.frames[i];
if (length - offset < CALL_AUDIO_FRAME_HDR_SIZE) goto invalid;
memcpy(&f->header, wire + offset, CALL_AUDIO_FRAME_HDR_SIZE); offset += CALL_AUDIO_FRAME_HDR_SIZE;
if (!valid_frame(&result.header, &f->header) || f->header.length > length - offset) goto invalid;
f->opus = wire + offset; offset += f->header.length;
}
if (offset != length) goto invalid;
*out = result;
return 0;
invalid:
DEBUG_WARN(DEBUG_CATEGORY_CALL, "audio batch RX malformed bytes=%zu", length); return -1;
}
void call_audio_batch_init(struct call_audio_batch_control *c, uint64_t now) {
if (!c) { DEBUG_ERROR(DEBUG_CATEGORY_CALL, "audio batch reset without state"); return; }
*c = (struct call_audio_batch_control){.target = 4, .last_us = now};
}
int call_audio_batch_step(struct call_audio_batch_control *c, uint64_t now, const struct ETCP_ROUTER_TX_STATS *net) {
if (!c || !c->target || now < c->last_us || (net && net->last_ack_us > now)) {
DEBUG_ERROR(DEBUG_CATEGORY_CALL, "audio batch invalid control sample time=%llu", (unsigned long long)now); return -1;
}
unsigned previous = c->target;
if ((!net && (c->local_epoch || c->peer_epoch || c->feedback_received))
|| (net && (net->local_epoch != c->local_epoch || net->peer_epoch != c->peer_epoch))) {
call_audio_batch_init(c, now);
if (net) { c->local_epoch = net->local_epoch; c->peer_epoch = net->peer_epoch; }
DEBUG_DEBUG(DEBUG_CATEGORY_CALL, "audio batch reset router epoch=%llx/%llx previous=%u",
(unsigned long long)c->local_epoch, (unsigned long long)c->peer_epoch, previous);
}
c->last_us = now;
c->delay_us = net ? net->delivery_rtt_us : 0;
if (net && net->queued_age_us > c->delay_us) c->delay_us = net->queued_age_us;
if (net && net->inflight_age_us > c->delay_us) c->delay_us = net->inflight_age_us;
uint64_t interval = net && net->delivery_rtt_us > 250000 ? net->delivery_rtt_us * 2 : 500000;
uint64_t fresh_us = interval > 2000000 ? interval : 2000000;
int measured = net && net->ready && !net->no_route && !net->receive_busy && !net->probing
&& net->acked_packets && now - net->last_ack_us <= fresh_us;
if (measured) c->feedback_received = 1;
int usable = measured && net->window.valid;
const char *reason = "hold";
if (!usable) { c->high = c->low = 0; reason = "unavailable"; }
else if (c->delay_us > 100000) {
c->low = 0;
if (!c->high) { c->high = 1; c->high_since_us = now; }
if (c->target < CALL_AUDIO_BATCH_MAX && now >= c->next_change_us && now - c->high_since_us >= 200000) {
c->target++; c->next_change_us = now + interval; reason = "enqueue_ack_delay";
}
} else if (c->delay_us < 40000 && !net->queued_packets) {
c->high = 0;
if (!c->low) { c->low = 1; c->low_since_us = now; }
if (c->target > 1 && now >= c->next_change_us && now - c->low_since_us >= 2000000) {
c->target--; c->next_change_us = now + interval; c->low_since_us = now; reason = "recovered";
}
} else c->high = c->low = 0;
if (c->target != previous) DEBUG_INFO(DEBUG_CATEGORY_CALL,
"audio batch target=%u->%u reason=%s delay_us=%llu router_rtt_us=%llu queued_us=%llu flight_us=%llu",
previous, c->target, reason, (unsigned long long)c->delay_us, (unsigned long long)(net ? net->delivery_rtt_us : 0),
(unsigned long long)(net ? net->queued_age_us : 0), (unsigned long long)(net ? net->inflight_age_us : 0));
return 0;
}