FreeCalypso > hg > themwi-rtp-lib
annotate src/rtp_tx.c @ 25:e67b3bb87d1b
RTP Tx: network byte order
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 07 Jul 2024 17:41:19 +0000 |
parents | 84d427017d2f |
children | f71efdd08c33 |
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> |
25
e67b3bb87d1b
RTP Tx: network byte order
Mychaela Falconia <falcon@freecalypso.org>
parents:
24
diff
changeset
|
9 #include <arpa/inet.h> /* for network byte order functions */ |
24
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <osmocom/core/msgb.h> |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <osmocom/core/osmo_io.h> |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <osmocom/core/timer.h> |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 #include <themwi/rtp/endp.h> |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 #include <themwi/rtp/rtp_basic_hdr.h> |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 #include <themwi/rtp/twjit.h> |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 static uint32_t gen_timestamp(struct timespec *now, |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 struct twrtp_jibuf_inst *twjit) |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 { |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 uint32_t ts; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 ts = now->tv_sec * twjit->ts_units_per_sec + |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 now->tv_nsec / twjit->ns_to_ts_units; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 return ts; |
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 |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 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
|
30 unsigned payload_len, uint8_t payload_type, |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 bool marker, bool send_rtcp) |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 { |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 uint32_t ts_quantum = endp->twjit->ts_quantum; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 struct msgb *msg; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 struct timespec now; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 uint32_t restart_ts; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 int32_t ts_delta; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 struct rtp_basic_hdr *rtph; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 uint8_t *pl_out; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 int rc; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 if (!endp->register_done || !endp->remote_set) |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 return -EINVAL; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 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
|
45 "ThemWi-RTP-Tx"); |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 if (!msg) |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 return -ENOMEM; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 /* timestamp generation is where we do some trickery */ |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 osmo_clock_gettime(CLOCK_REALTIME, &now); |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 if (!endp->tx.started) { |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 endp->tx.ts = gen_timestamp(&now, endp->twjit); |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 endp->tx.started = true; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 endp->tx.restart = false; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 } else if (endp->tx.restart) { |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 restart_ts = gen_timestamp(&now, endp->twjit); |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 ts_delta = (int32_t)(restart_ts - endp->tx.ts); |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 if (ts_delta <= 0) { |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 /* shouldn't happen, unless something funky w/clock */ |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 endp->tx.ts++; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } else { |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 if (ts_delta % ts_quantum == 0) |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 restart_ts++; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 endp->tx.ts = restart_ts; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 } |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 endp->tx.restart = false; |
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 |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 rtph = (struct rtp_basic_hdr *) |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 msgb_put(msg, sizeof(struct rtp_basic_hdr)); |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 rtph->v_p_x_cc = 0x80; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 rtph->m_pt = payload_type; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 if (marker) |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 rtph->m_pt |= 0x80; |
25
e67b3bb87d1b
RTP Tx: network byte order
Mychaela Falconia <falcon@freecalypso.org>
parents:
24
diff
changeset
|
75 rtph->ssrc = htonl(endp->tx.ssrc); |
e67b3bb87d1b
RTP Tx: network byte order
Mychaela Falconia <falcon@freecalypso.org>
parents:
24
diff
changeset
|
76 rtph->seq = htons(endp->tx.seq); |
e67b3bb87d1b
RTP Tx: network byte order
Mychaela Falconia <falcon@freecalypso.org>
parents:
24
diff
changeset
|
77 rtph->tstamp = htonl(endp->tx.ts); |
24
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 pl_out = msgb_put(msg, payload_len); |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 memcpy(pl_out, payload, payload_len); |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 endp->tx.seq++; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 endp->tx.ts += ts_quantum; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 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
|
84 if (rc < 0) { |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 msgb_free(msg); |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 return rc; |
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 |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 /* TODO: send RTCP if requested */ |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 return 0; |
84d427017d2f
endp: implement RTP Tx
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 } |