annotate src/rtp_tx.c @ 24:84d427017d2f

endp: implement RTP Tx
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Jul 2024 07:33:48 +0000
parents
children e67b3bb87d1b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Here we implement RTP Tx functionality.
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <stdint.h>
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdbool.h>
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <string.h>
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <errno.h>
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <osmocom/core/msgb.h>
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <osmocom/core/osmo_io.h>
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <osmocom/core/timer.h>
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <themwi/rtp/endp.h>
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <themwi/rtp/rtp_basic_hdr.h>
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include <themwi/rtp/twjit.h>
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 static uint32_t gen_timestamp(struct timespec *now,
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 struct twrtp_jibuf_inst *twjit)
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 {
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 uint32_t ts;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 ts = now->tv_sec * twjit->ts_units_per_sec +
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 now->tv_nsec / twjit->ns_to_ts_units;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 return ts;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 }
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 int twrtp_endp_tx_quantum(struct twrtp_endp *endp, const uint8_t *payload,
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 unsigned payload_len, uint8_t payload_type,
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 bool marker, bool send_rtcp)
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 {
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 uint32_t ts_quantum = endp->twjit->ts_quantum;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 struct msgb *msg;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 struct timespec now;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 uint32_t restart_ts;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 int32_t ts_delta;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 struct rtp_basic_hdr *rtph;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 uint8_t *pl_out;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 int rc;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (!endp->register_done || !endp->remote_set)
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 return -EINVAL;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 msg = msgb_alloc_c(endp, sizeof(struct rtp_basic_hdr) + payload_len,
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 "ThemWi-RTP-Tx");
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 if (!msg)
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 return -ENOMEM;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 /* timestamp generation is where we do some trickery */
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 osmo_clock_gettime(CLOCK_REALTIME, &now);
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (!endp->tx.started) {
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 endp->tx.ts = gen_timestamp(&now, endp->twjit);
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 endp->tx.started = true;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 endp->tx.restart = false;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 } else if (endp->tx.restart) {
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 restart_ts = gen_timestamp(&now, endp->twjit);
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 ts_delta = (int32_t)(restart_ts - endp->tx.ts);
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 if (ts_delta <= 0) {
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 /* shouldn't happen, unless something funky w/clock */
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 endp->tx.ts++;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 } else {
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 if (ts_delta % ts_quantum == 0)
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 restart_ts++;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 endp->tx.ts = restart_ts;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 }
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 endp->tx.restart = false;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 rtph = (struct rtp_basic_hdr *)
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 msgb_put(msg, sizeof(struct rtp_basic_hdr));
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 rtph->v_p_x_cc = 0x80;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 rtph->m_pt = payload_type;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 if (marker)
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 rtph->m_pt |= 0x80;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 rtph->ssrc = endp->tx.ssrc;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 rtph->seq = endp->tx.seq;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 rtph->tstamp = endp->tx.ts;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 pl_out = msgb_put(msg, payload_len);
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 memcpy(pl_out, payload, payload_len);
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 endp->tx.seq++;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 endp->tx.ts += ts_quantum;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 rc = osmo_iofd_sendto_msgb(endp->iofd_rtp, msg, 0, &endp->rtp_remote);
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 if (rc < 0) {
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 msgb_free(msg);
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 return rc;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 /* TODO: send RTCP if requested */
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 return 0;
84d427017d2f endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 }