Debian Codegraph @

src/proto/rtp.c

raw symbols SCIP
1 #include "rtp.h"
2 #include "protocol_rtp.h"
3 #include "rist_time.h"
4
5 #include "endian-shim.h"
6 #include "rist-private.h"
7 #include "udp-private.h"
8
9 void rist_rtcp_write_empty_rr(uint8_t *buf, int *offset,
10 const uint32_t flow_id) {
11 struct rist_rtcp_rr_empty_pkt *rr =
12 (struct rist_rtcp_rr_empty_pkt *)(buf + RIST_MAX_PAYLOAD_OFFSET +
13 *offset);
14 *offset += sizeof(struct rist_rtcp_rr_empty_pkt);
15 rr->rtcp.flags = RTCP_SR_FLAGS;
16 rr->rtcp.ptype = PTYPE_RR;
17 rr->rtcp.ssrc = htobe32(flow_id);
18 rr->rtcp.len = htons(1);
19 }
20
21 void rist_rtcp_write_rr(uint8_t *buf, int *offset,
22 const struct rist_peer *peer) {
23 struct rist_rtcp_rr_pkt *rr =
24 (struct rist_rtcp_rr_pkt *)(buf + RIST_MAX_PAYLOAD_OFFSET + *offset);
25 *offset += sizeof(struct rist_rtcp_rr_pkt);
26 rr->rtcp.flags = RTCP_RR_FULL_FLAGS;
27 rr->rtcp.ptype = PTYPE_RR;
28 rr->rtcp.ssrc = htobe32(peer->adv_flow_id);
29 rr->rtcp.len = htons(7);
30 /* TODO fix these variables */
31 rr->fraction_lost = 0;
32 rr->cumulative_pkt_loss_msb = 0;
33 rr->cumulative_pkt_loss_lshw = 0;
34 rr->highest_seq = 0;
35 rr->jitter = 0;
36 rr->lsr = htobe32((uint32_t)(peer->last_sender_report_time >> 16));
37 /* expressed in units of 1/65536 == middle 16 bits?!? */
38 rr->dlsr = htobe32(
39 (uint32_t)((timestampNTP_u64() - peer->last_sender_report_ts) >> 16));
40 }
41
42 void rist_rtcp_write_sr(uint8_t *buf, int *offset,
43 struct rist_peer *peer) {
44 struct rist_rtcp_sr_pkt *sr =
45 (struct rist_rtcp_sr_pkt *)(buf + RIST_MAX_PAYLOAD_OFFSET + *offset);
46 *offset += sizeof(struct rist_rtcp_sr_pkt);
47 /* Populate SR for sender */
48 sr->rtcp.flags = RTCP_SR_FLAGS;
49 sr->rtcp.ptype = PTYPE_SR;
50 sr->rtcp.ssrc = htobe32(peer->adv_flow_id);
51 sr->rtcp.len = htons(6);
52 uint64_t now = timestampNTP_u64();
53 uint64_t now_rtc = timestampNTP_RTC_u64();
54 peer->last_sender_report_time = now_rtc;
55 peer->last_sender_report_ts = now;
56 uint32_t ntp_lsw = (uint32_t)now_rtc;
57 // There is 70 years (incl. 17 leap ones) offset to the Unix Epoch.
58 // No leap seconds during that period since they were not invented yet.
59 uint32_t ntp_msw = now_rtc >> 32;
60 sr->ntp_msw = htobe32(ntp_msw);
61 sr->ntp_lsw = htobe32(ntp_lsw);
62 sr->rtp_ts = htobe32(timestampRTP_u32(0, now));
63 sr->sender_pkts = 0; // htonl(f->packets_count);
64 sr->sender_bytes = 0; // htonl(f->bytes_count);
65 }
66
67 void rist_rtcp_write_sdes(uint8_t *buf, int *offset,
68 const char *name,
69 const uint32_t flow_id) {
70 size_t namelen = strnlen(name, RIST_MAX_STRING_SHORT - 1);
71 if (namelen > 255)
72 namelen = 255;
73 size_t sdes_size = ((10 + namelen + 1) + 3) & ~3;
74 size_t padding = sdes_size - namelen - 10;
75 struct rist_rtcp_sdes_pkt *sdes =
76 (struct rist_rtcp_sdes_pkt *)(buf + RIST_MAX_PAYLOAD_OFFSET + *offset);
77 *offset += sdes_size;
78 sdes->rtcp.flags = RTCP_SDES_FLAGS;
79 sdes->rtcp.ptype = PTYPE_SDES;
80 sdes->rtcp.len = htons((uint16_t)((sdes_size - 1) >> 2));
81 sdes->rtcp.ssrc = htobe32(flow_id);
82 sdes->cname = 1;
83 sdes->name_len = (uint8_t)namelen;
84 memcpy(sdes->udn, name, namelen);
85 memset(sdes->udn + namelen, 0, padding);
86 }
87
88 void rist_rtcp_write_echoreq(uint8_t *buf, int *offset,
89 const uint32_t flow_id) {
90 struct rist_rtcp_echoext *echo =
91 (struct rist_rtcp_echoext *)(buf + RIST_MAX_PAYLOAD_OFFSET + *offset);
92 *offset += sizeof(struct rist_rtcp_echoext);
93 echo->flags = RTCP_ECHOEXT_REQ_FLAGS;
94 echo->ptype = PTYPE_NACK_CUSTOM;
95 echo->ssrc = htobe32(flow_id);
96 echo->len = htons(5);
97 memcpy(echo->name, "RIST", 4);
98 uint64_t now = timestampNTP_u64();
99 echo->ntp_msw = htobe32((uint32_t)(now >> 32));
100 echo->ntp_lsw = htobe32((uint32_t)(now & 0x000000000FFFFFFFF));
101 }
102
103 void rist_rtcp_write_echoresp(uint8_t *buf, int *offset,
104 const uint64_t request_time,
105 const uint32_t flow_id) {
106 struct rist_rtcp_echoext *echo =
107 (struct rist_rtcp_echoext *)(buf + RIST_MAX_PAYLOAD_OFFSET + *offset);
108 *offset += sizeof(struct rist_rtcp_echoext);
109 echo->flags = RTCP_ECHOEXT_RESP_FLAGS;
110 echo->ptype = PTYPE_NACK_CUSTOM;
111 echo->len = htons(5);
112 echo->ssrc = htobe32(flow_id);
113 memcpy(echo->name, "RIST", 4);
114 echo->ntp_msw = htobe32((uint32_t)(request_time >> 32));
115 echo->ntp_lsw = htobe32((uint32_t)(request_time & 0x000000000FFFFFFFF));
116 echo->delay = 0;
117 }
118
119 void rist_rtcp_write_xr_echoreq(uint8_t *buf, int *offset,
120 struct rist_peer *peer) {
121 struct rist_rtcp_hdr *xr_hdr =
122 (struct rist_rtcp_hdr *)(buf + RIST_MAX_PAYLOAD_OFFSET + *offset);
123 *offset += sizeof(*xr_hdr);
124 xr_hdr->flags = 0x80; // v=2;p=0;
125 xr_hdr->ptype = PTYPE_XR;
126 xr_hdr->ssrc = htobe32(peer->peer_ssrc);
127 struct rist_rtcp_xr_rrtrb *block =
128 (struct rist_rtcp_xr_rrtrb *)(buf + RIST_MAX_PAYLOAD_OFFSET + *offset);
129 *offset += sizeof(*block);
130 block->block_type = 4;
131 block->length = htobe16(2);
132 block->reserved = 0;
133 uint64_t now = timestampNTP_u64();
134 peer->last_sender_report_ts = now;
135 block->ntp_msw = htobe32((uint32_t)(now >> 32));
136 block->ntp_lsw = htobe32((uint32_t)(now & 0x000000000FFFFFFFF));
137 // RFC 3550: length is (32-bit words in header+payload) - 1
138 xr_hdr->len = htobe16(((sizeof(*xr_hdr) + sizeof(*block)) / 4) - 1);
139 }