summaryrefslogtreecommitdiffstats
path: root/PICA_media.c
diff options
context:
space:
mode:
Diffstat (limited to 'PICA_media.c')
-rw-r--r--PICA_media.c144
1 files changed, 144 insertions, 0 deletions
diff --git a/PICA_media.c b/PICA_media.c
index 835ebb7..afb862f 100644
--- a/PICA_media.c
+++ b/PICA_media.c
@@ -600,6 +600,150 @@ void PICA_media_call_free(struct PICA_c2c *c2c)
free(mc);
}
+/* seq_bits - 16 for audio, 15 for video, whose top bit is the last fragment
+ * marker */
+static void rxstream_packet(struct PICA_media_rxstream *s, uint16_t seq_num, int seq_bits, unsigned int size)
+{
+ const uint32_t modulus = 1u << seq_bits;
+ const uint32_t seq = seq_num & (modulus - 1);
+ uint32_t ahead;
+
+ if (!s->started)
+ {
+ s->started = 1;
+ s->max_seq = seq;
+ /* so that the first packet counts as expected */
+ s->period_max_seq = seq - 1;
+ }
+ else
+ {
+ /* how far the packet is ahead of the highest one so far, modulo the
+ * counter's range: up to half of it ahead, the rest is a late one
+ * that arrives out of order, or a duplicate */
+ ahead = (seq - s->max_seq) & (modulus - 1);
+
+ if (ahead > 0 && ahead < modulus / 2)
+ s->max_seq += ahead;
+ }
+
+ s->received++;
+ s->octets += size;
+}
+
+void PICA_media_rx_packet(struct PICA_c2c *c2c, int video, uint16_t seq_num,
+ uint32_t timestamp, unsigned int size)
+{
+ struct PICA_media_rxstats *rx;
+ int64_t delay;
+
+ if (!c2c->media_call)
+ return;
+
+ rx = &c2c->media_call->rx;
+
+ if (video)
+ {
+ rxstream_packet(&rx->video, seq_num, 15, size);
+ return;
+ }
+
+ rxstream_packet(&rx->audio, seq_num, 16, size);
+
+ /* Audio only: a packet every 20 milliseconds or so, each sent as it is
+ * encoded, which makes it the steadier probe of the delay. The fragments
+ * of a video frame all carry the frame's timestamp and go out in a burst,
+ * so the later ones of a large frame arrive late by the time it takes to
+ * send the earlier ones.
+ */
+ delay = (int64_t)PICA_media_now() - (int64_t)timestamp;
+
+ if (rx->delay_count == 0 || delay < rx->delay_min)
+ rx->delay_min = delay;
+
+ rx->delay_sum += delay;
+ rx->delay_count++;
+}
+
+static uint16_t clamp16(uint32_t v)
+{
+ return v > 0xFFFF ? 0xFFFF : (uint16_t)v;
+}
+
+/* fills in the received and lost counts of one stream and starts its next
+ * period */
+static void rxstream_report(struct PICA_media_rxstream *s, uint16_t *received, uint16_t *lost,
+ uint32_t *octets)
+{
+ const uint32_t expected = s->started ? s->max_seq - s->period_max_seq : 0;
+
+ *received = clamp16(s->received);
+ /* duplicates can make more arrive than was expected */
+ *lost = clamp16(expected > s->received ? expected - s->received : 0);
+ *octets = s->octets;
+
+ s->period_max_seq = s->max_seq;
+ s->received = 0;
+ s->octets = 0;
+}
+
+int PICA_media_rx_report(struct PICA_c2c *c2c, struct PICA_call_reception_report *r)
+{
+ struct PICA_media_rxstats *rx;
+ uint64_t now = PICA_media_now();
+ int64_t baseline;
+ int i;
+
+ if (!c2c->media_call)
+ return 0;
+
+ rx = &c2c->media_call->rx;
+
+ if (rx->period_started == 0)
+ {
+ rx->period_started = now;
+ return 0;
+ }
+
+ if (now - rx->period_started < PICA_MEDIA_REPORT_INTERVAL)
+ return 0;
+
+ r->period = clamp16((uint32_t)(now - rx->period_started));
+ rx->period_started = now;
+
+ rxstream_report(&rx->audio, &r->audio_received, &r->audio_lost, &r->audio_octets);
+ rxstream_report(&rx->video, &r->video_received, &r->video_lost, &r->video_octets);
+
+ r->queuing_delay = PICA_PROTO_CALL_RECEPTION_REPORT_DELAY_UNKNOWN;
+
+ if (rx->delay_count > 0)
+ {
+ rx->delay_mins[rx->delay_mins_pos] = rx->delay_min;
+ rx->delay_mins_pos = (rx->delay_mins_pos + 1) % PICA_MEDIA_DELAY_BASELINE_PERIODS;
+
+ if (rx->delay_mins_count < PICA_MEDIA_DELAY_BASELINE_PERIODS)
+ rx->delay_mins_count++;
+
+ baseline = rx->delay_mins[0];
+
+ for (i = 1; i < rx->delay_mins_count; i++)
+ if (rx->delay_mins[i] < baseline)
+ baseline = rx->delay_mins[i];
+
+ /* the average rather than the largest, so that one packet held up
+ * by the scheduler does not read as a queue */
+ r->queuing_delay = clamp16((uint32_t)(rx->delay_sum / rx->delay_count - baseline));
+
+ /* 0xFFFF is taken */
+ if (r->queuing_delay == PICA_PROTO_CALL_RECEPTION_REPORT_DELAY_UNKNOWN)
+ r->queuing_delay--;
+ }
+
+ rx->delay_sum = 0;
+ rx->delay_count = 0;
+
+ return 1;
+}
+
/* Attempt 1, over the address of the directc2c connection. It goes first
* because that address is known to work already - the port behind it is
* public or forwarded, whatever the NATs on the way do with UDP.