Debian Codegraph @

examples/aaf-listener.c

raw
1 /*
2 * Copyright (c) 2017, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /* AAF Listener example.
29 *
30 * This example implements a very simple AAF listener application which
31 * receives AFF packets from the network, retrieves the PCM samples, and
32 * writes them to stdout once the presentation time is reached.
33 *
34 * For simplicity, the example accepts only AAF packets with the following
35 * specification:
36 * - Sample format: 16-bit little endian
37 * - Sample rate: 48 kHz
38 * - Number of channels: 2 (stereo)
39 *
40 * TSN stream parameters such as destination mac address are passed via
41 * command-line arguments. Run 'aaf-listener --help' for more information.
42 *
43 * This example relies on the system clock to schedule PCM samples for
44 * playback. So make sure the system clock is synchronized with the PTP
45 * Hardware Clock (PHC) from your NIC and that the PHC is synchronized with
46 * the PTP time from the network. For further information on how to synchronize
47 * those clocks see ptp4l(8) and phc2sys(8) man pages.
48 *
49 * The easiest way to use this example is combining it with 'aplay' tool
50 * provided by alsa-utils. 'aplay' reads a PCM stream from stdin and sends it
51 * to a ALSA playback device (e.g. your speaker). So, to play Audio from a TSN
52 * stream, you should do something like this:
53 *
54 * $ aaf-listener <args> | aplay -f dat -t raw -D <playback-device>
55 */
56
57 #include <assert.h>
58 #include <argp.h>
59 #include <arpa/inet.h>
60 #include <linux/if.h>
61 #include <linux/if_ether.h>
62 #include <linux/if_packet.h>
63 #include <poll.h>
64 #include <stdbool.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <sys/queue.h>
68 #include <sys/timerfd.h>
69 #include <unistd.h>
70 #include <inttypes.h>
71
72 #include "avtp.h"
73 #include "avtp_aaf.h"
74 #include "examples/common.h"
75
76 #define STREAM_ID 0xAABBCCDDEEFF0001
77 #define SAMPLE_SIZE 2 /* Sample size in bytes. */
78 #define NUM_CHANNELS 2
79 #define DATA_LEN (SAMPLE_SIZE * NUM_CHANNELS)
80 #define PDU_SIZE (sizeof(struct avtp_stream_pdu) + DATA_LEN)
81 #define NSEC_PER_SEC 1000000000ULL
82
83 struct sample_entry {
84 STAILQ_ENTRY(sample_entry) entries;
85
86 struct timespec tspec;
87 uint8_t pcm_sample[DATA_LEN];
88 };
89
90 static STAILQ_HEAD(sample_queue, sample_entry) samples;
91 static char ifname[IFNAMSIZ];
92 static uint8_t macaddr[ETH_ALEN];
93 static uint8_t expected_seq;
94
95 static struct argp_option options[] = {
96 {"dst-addr", 'd', "MACADDR", 0, "Stream Destination MAC address" },
97 {"ifname", 'i', "IFNAME", 0, "Network Interface" },
98 { 0 }
99 };
100
101 static error_t parser(int key, char *arg, struct argp_state *state)
102 {
103 int res;
104
105 switch (key) {
106 case 'd':
107 res = sscanf(arg, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
108 &macaddr[0], &macaddr[1], &macaddr[2],
109 &macaddr[3], &macaddr[4], &macaddr[5]);
110 if (res != 6) {
111 fprintf(stderr, "Invalid address\n");
112 exit(EXIT_FAILURE);
113 }
114
115 break;
116 case 'i':
117 strncpy(ifname, arg, sizeof(ifname) - 1);
118 break;
119 }
120
121 return 0;
122 }
123
124 static struct argp argp = { options, parser };
125
126 /* Schedule 'pcm_sample' to be presented at time specified by 'tspec'. */
127 static int schedule_sample(int fd, struct timespec *tspec, uint8_t *pcm_sample)
128 {
129 struct sample_entry *entry;
130
131 entry = malloc(sizeof(*entry));
132 if (!entry) {
133 fprintf(stderr, "Failed to allocate memory\n");
134 return -1;
135 }
136
137 entry->tspec.tv_sec = tspec->tv_sec;
138 entry->tspec.tv_nsec = tspec->tv_nsec;
139 memcpy(entry->pcm_sample, pcm_sample, DATA_LEN);
140
141 STAILQ_INSERT_TAIL(&samples, entry, entries);
142
143 /* If this was the first entry inserted onto the queue, we need to arm
144 * the timer.
145 */
146 if (STAILQ_FIRST(&samples) == entry) {
147 int res;
148
149 res = arm_timer(fd, tspec);
150 if (res < 0) {
151 STAILQ_REMOVE(&samples, entry, sample_entry, entries);
152 free(entry);
153 return -1;
154 }
155 }
156
157 return 0;
158 }
159
160 static bool is_valid_packet(struct avtp_stream_pdu *pdu)
161 {
162 struct avtp_common_pdu *common = (struct avtp_common_pdu *) pdu;
163 uint64_t val64;
164 uint32_t val32;
165 int res;
166
167 res = avtp_pdu_get(common, AVTP_FIELD_SUBTYPE, &val32);
168 if (res < 0) {
169 fprintf(stderr, "Failed to get subtype field: %d\n", res);
170 return false;
171 }
172 if (val32 != AVTP_SUBTYPE_AAF) {
173 fprintf(stderr, "Subtype mismatch: expected %u, got %u\n",
174 AVTP_SUBTYPE_AAF, val32);
175 return false;
176 }
177
178 res = avtp_pdu_get(common, AVTP_FIELD_VERSION, &val32);
179 if (res < 0) {
180 fprintf(stderr, "Failed to get version field: %d\n", res);
181 return false;
182 }
183 if (val32 != 0) {
184 fprintf(stderr, "Version mismatch: expected %u, got %u\n",
185 0, val32);
186 return false;
187 }
188
189 res = avtp_aaf_pdu_get(pdu, AVTP_AAF_FIELD_TV, &val64);
190 if (res < 0) {
191 fprintf(stderr, "Failed to get tv field: %d\n", res);
192 return false;
193 }
194 if (val64 != 1) {
195 fprintf(stderr, "tv mismatch: expected %u, got %" PRIu64 "\n",
196 1, val64);
197 return false;
198 }
199
200 res = avtp_aaf_pdu_get(pdu, AVTP_AAF_FIELD_SP, &val64);
201 if (res < 0) {
202 fprintf(stderr, "Failed to get sp field: %d\n", res);
203 return false;
204 }
205 if (val64 != AVTP_AAF_PCM_SP_NORMAL) {
206 fprintf(stderr, "sp mismatch: expected %u, got %" PRIu64 "\n",
207 AVTP_AAF_PCM_SP_NORMAL, val64);
208 return false;
209 }
210
211 res = avtp_aaf_pdu_get(pdu, AVTP_AAF_FIELD_STREAM_ID, &val64);
212 if (res < 0) {
213 fprintf(stderr, "Failed to get stream ID field: %d\n", res);
214 return false;
215 }
216 if (val64 != STREAM_ID) {
217 fprintf(stderr, "Stream ID mismatch: expected %" PRIu64 ", got %" PRIu64 "\n",
218 STREAM_ID, val64);
219 return false;
220 }
221
222 res = avtp_aaf_pdu_get(pdu, AVTP_AAF_FIELD_SEQ_NUM, &val64);
223 if (res < 0) {
224 fprintf(stderr, "Failed to get sequence num field: %d\n", res);
225 return false;
226 }
227
228 if (val64 != expected_seq) {
229 /* If we have a sequence number mismatch, we simply log the
230 * issue and continue to process the packet. We don't want to
231 * invalidate it since it is a valid packet after all.
232 */
233 fprintf(stderr, "Sequence number mismatch: expected %u, got %" PRIu64 "\n",
234 expected_seq, val64);
235 expected_seq = val64;
236 }
237
238 expected_seq++;
239
240 res = avtp_aaf_pdu_get(pdu, AVTP_AAF_FIELD_FORMAT, &val64);
241 if (res < 0) {
242 fprintf(stderr, "Failed to get format field: %d\n", res);
243 return false;
244 }
245 if (val64 != AVTP_AAF_FORMAT_INT_16BIT) {
246 fprintf(stderr, "Format mismatch: expected %u, got %" PRIu64 "\n",
247 AVTP_AAF_FORMAT_INT_16BIT, val64);
248 return false;
249 }
250
251 res = avtp_aaf_pdu_get(pdu, AVTP_AAF_FIELD_NSR, &val64);
252 if (res < 0) {
253 fprintf(stderr, "Failed to get sample rate field: %d\n", res);
254 return false;
255 }
256 if (val64 != AVTP_AAF_PCM_NSR_48KHZ) {
257 fprintf(stderr, "Sample rate mismatch: expected %u, got %" PRIu64 "\n",
258 AVTP_AAF_PCM_NSR_48KHZ, val64);
259 return false;
260 }
261
262 res = avtp_aaf_pdu_get(pdu, AVTP_AAF_FIELD_CHAN_PER_FRAME, &val64);
263 if (res < 0) {
264 fprintf(stderr, "Failed to get channels field: %d\n", res);
265 return false;
266 }
267 if (val64 != NUM_CHANNELS) {
268 fprintf(stderr, "Channels mismatch: expected %u, got %" PRIu64 "\n",
269 NUM_CHANNELS, val64);
270 return false;
271 }
272
273 res = avtp_aaf_pdu_get(pdu, AVTP_AAF_FIELD_BIT_DEPTH, &val64);
274 if (res < 0) {
275 fprintf(stderr, "Failed to get depth field: %d\n", res);
276 return false;
277 }
278 if (val64 != 16) {
279 fprintf(stderr, "Depth mismatch: expected %u, got %" PRIu64 "\n",
280 16, val64);
281 return false;
282 }
283
284 res = avtp_aaf_pdu_get(pdu, AVTP_AAF_FIELD_STREAM_DATA_LEN, &val64);
285 if (res < 0) {
286 fprintf(stderr, "Failed to get data_len field: %d\n", res);
287 return false;
288 }
289 if (val64 != DATA_LEN) {
290 fprintf(stderr, "Data len mismatch: expected %u, got %" PRIu64 "\n",
291 DATA_LEN, val64);
292 return false;
293 }
294
295 return true;
296 }
297
298 static int new_packet(int sk_fd, int timer_fd)
299 {
300 int res;
301 ssize_t n;
302 uint64_t avtp_time;
303 struct timespec tspec;
304 struct avtp_stream_pdu *pdu = alloca(PDU_SIZE);
305
306 memset(pdu, 0, PDU_SIZE);
307
308 n = recv(sk_fd, pdu, PDU_SIZE, 0);
309 if (n < 0 || n != PDU_SIZE) {
310 perror("Failed to receive data");
311 return -1;
312 }
313
314 if (!is_valid_packet(pdu)) {
315 fprintf(stderr, "Dropping packet\n");
316 return 0;
317 }
318
319 res = avtp_aaf_pdu_get(pdu, AVTP_AAF_FIELD_TIMESTAMP, &avtp_time);
320 if (res < 0) {
321 fprintf(stderr, "Failed to get AVTP time from PDU\n");
322 return -1;
323 }
324
325 res = get_presentation_time(avtp_time, &tspec);
326 if (res < 0)
327 return -1;
328
329 res = schedule_sample(timer_fd, &tspec, pdu->avtp_payload);
330 if (res < 0)
331 return -1;
332
333 return 0;
334 }
335
336 static int timeout(int fd)
337 {
338 int res;
339 ssize_t n;
340 uint64_t expirations;
341 struct sample_entry *entry;
342
343 n = read(fd, &expirations, sizeof(uint64_t));
344 if (n < 0) {
345 perror("Failed to read timerfd");
346 return -1;
347 }
348
349 assert(expirations == 1);
350
351 entry = STAILQ_FIRST(&samples);
352 assert(entry != NULL);
353
354 res = present_data(entry->pcm_sample, DATA_LEN);
355 if (res < 0)
356 return -1;
357
358 STAILQ_REMOVE_HEAD(&samples, entries);
359 free(entry);
360
361 if (!STAILQ_EMPTY(&samples)) {
362 entry = STAILQ_FIRST(&samples);
363
364 res = arm_timer(fd, &entry->tspec);
365 if (res < 0)
366 return -1;
367 }
368
369 return 0;
370 }
371
372 int main(int argc, char *argv[])
373 {
374 int sk_fd, timer_fd, res;
375 struct pollfd fds[2];
376
377 argp_parse(&argp, argc, argv, 0, NULL, NULL);
378
379 STAILQ_INIT(&samples);
380
381 sk_fd = create_listener_socket(ifname, macaddr, ETH_P_TSN);
382 if (sk_fd < 0)
383 return 1;
384
385 timer_fd = timerfd_create(CLOCK_REALTIME, 0);
386 if (timer_fd < 0) {
387 close(sk_fd);
388 return 1;
389 }
390
391 fds[0].fd = sk_fd;
392 fds[0].events = POLLIN;
393 fds[1].fd = timer_fd;
394 fds[1].events = POLLIN;
395
396 while (1) {
397 res = poll(fds, 2, -1);
398 if (res < 0) {
399 perror("Failed to poll() fds");
400 goto err;
401 }
402
403 if (fds[0].revents & POLLIN) {
404 res = new_packet(sk_fd, timer_fd);
405 if (res < 0)
406 goto err;
407 }
408
409 if (fds[1].revents & POLLIN) {
410 res = timeout(timer_fd);
411 if (res < 0)
412 goto err;
413 }
414 }
415
416 return 0;
417
418 err:
419 close(sk_fd);
420 close(timer_fd);
421 return 1;
422 }