Repository navigation
Expand file tree
/
Copy pathaes67_sdp_utils.c
More file actions
403 lines (346 loc) · 11.6 KB
/
Copy pathaes67_sdp_utils.c
File metadata and controls
403 lines (346 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Copyright (c) 2025
* SPDX-License-Identifier: Apache-2.0
*
* AES67 SDP utility functions implementation.
*/
#include <zephyr/kernel.h>
#include <zephyr/net/socket.h>
#include <zephyr/sys/byteorder.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "aes67_sdp_utils.h"
#include "sap_sdp.h" /* For AES67 defaults */
/* ================================================================
* String Helpers
* ================================================================ */
int aes67_strncasecmp(const char *s1, const char *s2, size_t n)
{
for (size_t i = 0; i < n; i++) {
int c1 = tolower((unsigned char)s1[i]);
int c2 = tolower((unsigned char)s2[i]);
if (c1 != c2) {
return c1 - c2;
}
if (c1 == '\0') {
return 0;
}
}
return 0;
}
const char *aes67_parse_uint(const char *p, const char *end, uint32_t *out)
{
uint32_t val = 0;
while (p < end && *p >= '0' && *p <= '9') {
val = val * 10 + (uint32_t)(*p - '0');
p++;
}
*out = val;
return p;
}
/* ================================================================
* PTP Clock ID Formatting
* ================================================================ */
int aes67_format_clock_id(char *buf, size_t buf_len, const uint8_t id[8])
{
return snprintf(buf, buf_len,
"%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X",
id[0], id[1], id[2], id[3],
id[4], id[5], id[6], id[7]);
}
/* ================================================================
* SDP Generation
*
* Generates RAVENNA-compliant SDP with required extensions:
* - a=clock-domain:PTPv2 <domain> (session-level, REQUIRED)
* - a=sync-time:<timestamp> (stream-level, for phase-accurate playback)
* ================================================================ */
int aes67_sdp_build(char *buf, size_t buf_size,
const struct aes67_sdp_params *params)
{
char origin_str[INET_ADDRSTRLEN];
char conn_str[INET_ADDRSTRLEN];
char clock_id_str[32];
zsock_inet_ntop(AF_INET, ¶ms->origin_addr, origin_str, sizeof(origin_str));
zsock_inet_ntop(AF_INET, ¶ms->connection_addr, conn_str, sizeof(conn_str));
if (params->clock_id) {
aes67_format_clock_id(clock_id_str, sizeof(clock_id_str),
params->clock_id);
}
/* Compute ptime: samples_per_packet * 1000000 / sample_rate */
uint32_t ptime_us = aes67_ptime_us(params->samples_per_packet,
params->sample_rate);
/* Session ID: use origin IP + stream_id for uniqueness */
uint32_t session_id = sys_be32_to_cpu(params->origin_addr.s_addr) +
params->stream_id;
const char *stream_name = params->stream_name;
if (!stream_name || stream_name[0] == '\0') {
stream_name = "AES67 Stream";
}
size_t pos = 0;
/* snprintf() reports the length it WOULD have written, so a single
* truncated line already pushes pos past buf_size. Appending after
* that would compute `buf_size - pos` as a size_t underflow and
* write beyond the buffer — bail out on the first overflow instead
* and let the caller see -ENOMEM. */
#define SDP_APPEND(...) \
do { \
int _n; \
if (pos >= buf_size) { \
return -ENOMEM; \
} \
_n = snprintf(buf + pos, buf_size - pos, __VA_ARGS__); \
if (_n < 0) { \
return -ENOMEM; \
} \
pos += (size_t)_n; \
} while (0)
/* === Session-level description === */
/* v= (protocol version) */
SDP_APPEND("v=0\r\n");
/* o= (origin) */
SDP_APPEND("o=- %u %u IN IP4 %s\r\n",
session_id, params->stream_id, origin_str);
/* s= (session name) */
SDP_APPEND("s=%s\r\n", stream_name);
/* i= (session info) */
SDP_APPEND("i=%uch %ubit %uHz\r\n",
params->channel_count, params->bit_depth,
params->sample_rate);
/* t= (timing) */
SDP_APPEND("t=0 0\r\n");
/* a=clock-domain (RAVENNA REQUIRED - session-level sync source) */
SDP_APPEND("a=clock-domain:PTPv2 %u\r\n", params->ptp_domain);
/* === Media-level description === */
/* m= (media) */
SDP_APPEND("m=audio %u RTP/AVP %u\r\n",
params->port, params->payload_type);
/* c= (connection data). Media-level rather than session-level:
* RFC 4566 allows either, but NMOS controllers (and the
* nmos-testing SDP checks) look for it inside the media section. */
SDP_APPEND("c=IN IP4 %s/32\r\n", conn_str);
/* a=source-filter (RFC 4570): ST 2110-10 requires multicast
* sessions to declare the expected source address (SSM/IGMPv3). */
if ((sys_be32_to_cpu(params->connection_addr.s_addr) >> 28) == 0xe &&
params->origin_addr.s_addr != 0) {
SDP_APPEND("a=source-filter: incl IN IP4 %s %s\r\n",
conn_str, origin_str);
}
/* a=rtpmap */
SDP_APPEND("a=rtpmap:%u L%u/%u/%u\r\n",
params->payload_type, params->bit_depth,
params->sample_rate, params->channel_count);
/* a=ptime */
SDP_APPEND("a=ptime:%u.%03u\r\n", ptime_us / 1000, ptime_us % 1000);
/* a=sync-time (RAVENNA - stream-level timestamp association) */
SDP_APPEND("a=sync-time:%u\r\n", params->sync_time);
/* a=ts-refclk (AES67 reference clock): the elected PTP grandmaster
* incl. domain. Omitted while no grandmaster is known (clock_id ==
* NULL) — a zero placeholder would claim traceability to a clock
* that does not exist. Matches the Linux daemon's behaviour. */
if (params->clock_id) {
SDP_APPEND("a=ts-refclk:ptp=IEEE1588-2008:%s:%u\r\n",
clock_id_str, params->ptp_domain);
}
/* a=mediaclk */
SDP_APPEND("a=mediaclk:direct=0\r\n");
/* a=ssrc (optional) */
if (params->ssrc != 0) {
SDP_APPEND("a=ssrc:%u cname:aes67@%s\r\n",
params->ssrc, origin_str);
}
#undef SDP_APPEND
if (pos >= buf_size) {
return -ENOMEM;
}
return (int)pos;
}
/* ================================================================
* SDP Parsing
* ================================================================ */
int aes67_sdp_parse(const char *sdp, size_t sdp_len,
struct aes67_sdp_parsed *out)
{
memset(out, 0, sizeof(*out));
const char *line = sdp;
const char *end = sdp + sdp_len;
while (line < end) {
const char *eol = memchr(line, '\n', end - line);
size_t line_len = eol ? (size_t)(eol - line) : (size_t)(end - line);
/* Strip trailing \r */
size_t clean_len = line_len;
if (clean_len > 0 && line[clean_len - 1] == '\r') {
clean_len--;
}
if (clean_len < 2) {
goto next_line;
}
char type = line[0];
char eq = line[1];
if (eq != '=') {
goto next_line;
}
const char *val = line + 2;
size_t val_len = clean_len - 2;
switch (type) {
case 's':
/* Session name */
if (val_len >= AES67_SDP_NAME_MAX) {
val_len = AES67_SDP_NAME_MAX - 1;
}
memcpy(out->name, val, val_len);
out->name[val_len] = '\0';
break;
case 'o':
/* Origin: o=<username> <sess_id> <version> IN IP4 <addr> */
{
const char *first_space = memchr(val, ' ', val_len);
size_t name_len = first_space ?
(size_t)(first_space - val) : val_len;
if (!(name_len == 1 && val[0] == '-')) {
name_len = MIN(name_len,
sizeof(out->origin_name) - 1);
memcpy(out->origin_name, val, name_len);
out->origin_name[name_len] = '\0';
}
/* Find last token which is IP address */
const char *p = val + val_len;
while (p > val && *(p - 1) != ' ') {
p--;
}
if (p < val + val_len) {
char addr_buf[INET_ADDRSTRLEN];
size_t addr_len = (val + val_len) - p;
if (addr_len < sizeof(addr_buf)) {
memcpy(addr_buf, p, addr_len);
addr_buf[addr_len] = '\0';
zsock_inet_pton(AF_INET, addr_buf,
&out->origin_addr);
}
}
}
break;
case 'c':
/* Connection: c=IN IP4 <addr>[/TTL] */
{
const char *marker = "IN IP4 ";
size_t marker_len = 7;
const char *p = val;
const char *p_end = val + val_len;
/* Find "IN IP4 " */
for (; p + marker_len <= p_end; p++) {
if (memcmp(p, marker, marker_len) == 0) {
p += marker_len;
break;
}
}
if (p < p_end) {
char addr_buf[INET_ADDRSTRLEN];
const char *slash = memchr(p, '/', p_end - p);
size_t addr_len = slash ?
(size_t)(slash - p) :
(size_t)(p_end - p);
if (addr_len < sizeof(addr_buf)) {
memcpy(addr_buf, p, addr_len);
addr_buf[addr_len] = '\0';
zsock_inet_pton(AF_INET, addr_buf,
&out->connection_addr);
}
}
}
break;
case 'm':
/* Media: m=audio <port> RTP/AVP <pt> */
if (val_len > 6 && memcmp(val, "audio ", 6) == 0) {
uint32_t port_val = 0;
aes67_parse_uint(val + 6, val + val_len, &port_val);
out->port = (uint16_t)port_val;
}
break;
case 'a':
/* Attributes */
if (val_len > 7 && memcmp(val, "rtpmap:", 7) == 0) {
/* a=rtpmap:<pt> L<depth>/<rate>/<ch> */
const char *p = val + 7;
const char *p_end = val + val_len;
/* Skip payload type */
while (p < p_end && *p >= '0' && *p <= '9') p++;
if (p < p_end && *p == ' ') p++;
/* Expect L<depth>/<rate>/<ch> */
if (p < p_end && *p == 'L') {
p++;
uint32_t depth = 0, rate = 0, ch = 0;
p = aes67_parse_uint(p, p_end, &depth);
if (p < p_end && *p == '/') {
p++;
p = aes67_parse_uint(p, p_end, &rate);
}
if (p < p_end && *p == '/') {
p++;
aes67_parse_uint(p, p_end, &ch);
}
out->bit_depth = (uint8_t)depth;
out->sample_rate = rate;
out->channels = (uint8_t)ch;
}
} else if (val_len > 5 && memcmp(val, "ssrc:", 5) == 0) {
/* a=ssrc:<ssrc> cname:... */
uint32_t ssrc_val = 0;
aes67_parse_uint(val + 5, val + val_len, &ssrc_val);
out->ssrc = ssrc_val;
} else if (val_len > 6 && memcmp(val, "ptime:", 6) == 0) {
/* a=ptime:<ms>[.<frac>] */
const char *p = val + 6;
const char *p_end = val + val_len;
uint32_t ptime_ms = 0;
uint32_t ptime_frac_us = 0;
p = aes67_parse_uint(p, p_end, &ptime_ms);
if (p < p_end && *p == '.') {
p++;
/* ptime is in milliseconds, so the first
* fractional digit is worth 100 us.
* Digits past microsecond resolution
* are dropped (scale reaches 0). */
uint32_t scale = 100U;
while (p < p_end && *p >= '0' && *p <= '9' && scale > 0) {
ptime_frac_us += (uint32_t)(*p - '0') * scale;
scale /= 10;
p++;
}
}
/* Store raw ptime in us temporarily */
out->samples_per_packet = (uint16_t)(ptime_ms * 1000U + ptime_frac_us);
} else if (val_len > 13 && memcmp(val, "clock-domain:", 13) == 0) {
/* a=clock-domain:PTPv2 <domain> (RAVENNA session-level) */
const char *p = val + 13;
const char *p_end = val + val_len;
/* Skip "PTPv2 " prefix if present */
if (p + 6 <= p_end && memcmp(p, "PTPv2 ", 6) == 0) {
p += 6;
uint32_t domain = 0;
aes67_parse_uint(p, p_end, &domain);
out->ptp_domain = (uint8_t)domain;
out->has_clock_domain = true;
}
} else if (val_len > 10 && memcmp(val, "sync-time:", 10) == 0) {
/* a=sync-time:<rtp_timestamp> (RAVENNA stream-level) */
uint32_t timestamp = 0;
aes67_parse_uint(val + 10, val + val_len, ×tamp);
out->sync_time = timestamp;
out->has_sync_time = true;
}
break;
}
next_line:
if (!eol) break;
line = eol + 1;
}
/* Convert ptime to samples if we have sample_rate */
if (out->samples_per_packet > 0 && out->sample_rate > 0) {
uint32_t ptime_us = out->samples_per_packet;
out->samples_per_packet = (uint16_t)(
ptime_us * out->sample_rate / 1000000U);
}
return 0;
}