Debian Codegraph @

examples/cvf-listener.c

raw
1 /*
2 * Copyright (c) 2019, 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 /* CVF Listener example.
29 *
30 * This example implements a very simple CVF listener application which
31 * receives CVF packets from the network, retrieves video data and writes
32 * them to stdout once the presentation time is reached.
33 *
34 * For simplicity, this examples accepts only CVF H.264 packets, and the H.264
35 * data must be composed of NAL and each NAL unit can not exceed 1400 bytes.
36 *
37 * The H.264 data sent to output is in H.264 byte-stream format.
38 *
39 * TSN stream parameters such as destination mac address are passed via
40 * command-line arguments. Run 'cvf-listener --help' for more information.
41 *
42 * This example relies on the system clock to schedule video data samples for
43 * presentation. So make sure the system clock is synchronized with the PTP
44 * Hardware Clock (PHC) from your NIC and that the PHC is synchronized with
45 * the PTP time from the network. For further information on how to synchronize
46 * those clocks see ptp4l(8) and phc2sys(8) man pages.
47 *
48 * The easiest way to use this example is by combining it with a GStreamer
49 * pipeline. We use GStreamer to read the H.264 byte-stream from stdin and
50 * present it. So, to play an H.264 video from a TSN strem and show it on a X
51 * display, you can do something like:
52 *
53 * $ cvf-listener <args> | gst-launch-1.0 filesrc location=/dev/stdin \
54 * ! decodebin ! videoconvert ! autovideosink
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
71 #include "avtp.h"
72 #include "avtp_cvf.h"
73 #include "examples/common.h"
74
75 #define STREAM_ID 0xAABBCCDDEEFF0001
76 #define DATA_LEN 1400
77 #define AVTP_H264_HEADER_LEN (sizeof(uint32_t))
78 #define AVTP_FULL_HEADER_LEN (sizeof(struct avtp_stream_pdu) + AVTP_H264_HEADER_LEN)
79 #define MAX_PDU_SIZE (AVTP_FULL_HEADER_LEN + DATA_LEN)
80
81 struct nal_entry {
82 STAILQ_ENTRY(nal_entry) entries;
83
84 uint16_t len;
85 struct timespec tspec;
86 uint8_t nal[DATA_LEN];
87 };
88
89 static STAILQ_HEAD(nal_queue, nal_entry) nals;
90 static char ifname[IFNAMSIZ];
91 static uint8_t macaddr[ETH_ALEN];
92 static uint8_t expected_seq;
93
94 static struct argp_option options[] = {
95 {"dst-addr", 'd', "MACADDR", 0, "Stream Destination MAC address" },
96 {"ifname", 'i', "IFNAME", 0, "Network Interface" },
97 { 0 }
98 };
99
100 static error_t parser(int key, char *arg, struct argp_state *state)
101 {
102 int res;
103
104 switch (key) {
105 case 'd':
106 res = sscanf(arg, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
107 &macaddr[0], &macaddr[1], &macaddr[2],
108 &macaddr[3], &macaddr[4], &macaddr[5]);
109 if (res != 6) {
110 fprintf(stderr, "Invalid address\n");
111 exit(EXIT_FAILURE);
112 }
113
114 break;
115 case 'i':
116 strncpy(ifname, arg, sizeof(ifname) - 1);
117 break;
118 }
119
120 return 0;
121 }
122
123 static struct argp argp = { options, parser };
124
125 static int schedule_nal(int fd, struct timespec *tspec, uint8_t *nal,
126 ssize_t len)
127 {
128 struct nal_entry *entry;
129
130 entry = malloc(sizeof(*entry));
131 if (!entry) {
132 fprintf(stderr, "Failed to allocate memory\n");
133 return -1;
134 }
135
136 entry->len = len;
137 entry->tspec.tv_sec = tspec->tv_sec;
138 entry->tspec.tv_nsec = tspec->tv_nsec;
139 memcpy(entry->nal, nal, entry->len);
140
141 STAILQ_INSERT_TAIL(&nals, 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(&nals) == entry) {
147 int res;
148
149 res = arm_timer(fd, tspec);
150 if (res < 0) {
151 STAILQ_REMOVE(&nals, entry, nal_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_CVF) {
173 fprintf(stderr, "Subtype mismatch: expected %u, got %u\n",
174 AVTP_SUBTYPE_CVF, 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_cvf_pdu_get(pdu, AVTP_CVF_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 %lu\n",
196 1, val64);
197 return false;
198 }
199
200 res = avtp_cvf_pdu_get(pdu, AVTP_CVF_FIELD_STREAM_ID, &val64);
201 if (res < 0) {
202 fprintf(stderr, "Failed to get stream ID field: %d\n", res);
203 return false;
204 }
205 if (val64 != STREAM_ID) {
206 fprintf(stderr, "Stream ID mismatch: expected %lu, got %lu\n",
207 STREAM_ID, val64);
208 return false;
209 }
210
211 res = avtp_cvf_pdu_get(pdu, AVTP_CVF_FIELD_SEQ_NUM, &val64);
212 if (res < 0) {
213 fprintf(stderr, "Failed to get sequence num field: %d\n", res);
214 return false;
215 }
216
217 if (val64 != expected_seq) {
218 /* If we have a sequence number mismatch, we simply log the
219 * issue and continue to process the packet. We don't want to
220 * invalidate it since it is a valid packet after all.
221 */
222 fprintf(stderr,
223 "Sequence number mismatch: expected %u, got %lu\n",
224 expected_seq, val64);
225 expected_seq = val64;
226 }
227
228 expected_seq++;
229
230 res = avtp_cvf_pdu_get(pdu, AVTP_CVF_FIELD_FORMAT, &val64);
231 if (res < 0) {
232 fprintf(stderr, "Failed to get format field: %d\n", res);
233 return false;
234 }
235 if (val64 != AVTP_CVF_FORMAT_RFC) {
236 fprintf(stderr, "Format mismatch: expected %u, got %lu\n",
237 AVTP_CVF_FORMAT_RFC, val64);
238 return false;
239 }
240
241 res = avtp_cvf_pdu_get(pdu, AVTP_CVF_FIELD_FORMAT_SUBTYPE, &val64);
242 if (res < 0) {
243 fprintf(stderr, "Failed to get format subtype field: %d\n",
244 res);
245 return false;
246 }
247 if (val64 != AVTP_CVF_FORMAT_SUBTYPE_H264) {
248 fprintf(stderr, "Format mismatch: expected %u, got %lu\n",
249 AVTP_CVF_FORMAT_SUBTYPE_H264, val64);
250 return false;
251 }
252
253 return true;
254 }
255
256 static int get_h264_data_len(struct avtp_stream_pdu *pdu,
257 uint16_t *stream_data_len)
258 {
259 int res;
260 uint64_t val;
261
262 res = avtp_cvf_pdu_get(pdu, AVTP_CVF_FIELD_STREAM_DATA_LEN, &val);
263 if (res < 0) {
264 fprintf(stderr, "Failed to get data_len field\n");
265 return -1;
266 }
267 *stream_data_len = val - AVTP_H264_HEADER_LEN;
268
269 return 0;
270 }
271
272 static int new_packet(int sk_fd, int timer_fd)
273 {
274 int res;
275 ssize_t n;
276 uint16_t h264_data_len;
277 uint64_t avtp_time;
278 struct timespec tspec;
279 struct avtp_stream_pdu *pdu = alloca(MAX_PDU_SIZE);
280 struct avtp_cvf_h264_payload *h264_pay =
281 (struct avtp_cvf_h264_payload *)pdu->avtp_payload;
282
283 memset(pdu, 1, MAX_PDU_SIZE);
284
285 n = recv(sk_fd, pdu, MAX_PDU_SIZE, 0);
286 if (n < 0 || n > MAX_PDU_SIZE) {
287 perror("Failed to receive data");
288 return -1;
289 }
290
291 if (!is_valid_packet(pdu)) {
292 fprintf(stderr, "Dropping packet\n");
293 return 0;
294 }
295
296 res = avtp_cvf_pdu_get(pdu, AVTP_CVF_FIELD_TIMESTAMP, &avtp_time);
297 if (res < 0) {
298 fprintf(stderr, "Failed to get AVTP time from PDU\n");
299 return -1;
300 }
301
302 res = get_presentation_time(avtp_time, &tspec);
303 if (res < 0)
304 return -1;
305
306 res = get_h264_data_len(pdu, &h264_data_len);
307 if (res < 0)
308 return -1;
309
310 res = schedule_nal(timer_fd, &tspec, h264_pay->h264_data,
311 h264_data_len);
312 if (res < 0)
313 return -1;
314
315 return 0;
316 }
317
318 static int timeout(int fd)
319 {
320 int res;
321 ssize_t n;
322 uint64_t expirations;
323 struct nal_entry *entry;
324
325 n = read(fd, &expirations, sizeof(uint64_t));
326 if (n < 0) {
327 perror("Failed to read timerfd");
328 return -1;
329 }
330
331 assert(expirations == 1);
332
333 entry = STAILQ_FIRST(&nals);
334 assert(entry != NULL);
335
336 res = present_data(entry->nal, entry->len);
337 if (res < 0)
338 return -1;
339
340 STAILQ_REMOVE_HEAD(&nals, entries);
341 free(entry);
342
343 if (!STAILQ_EMPTY(&nals)) {
344 entry = STAILQ_FIRST(&nals);
345
346 res = arm_timer(fd, &entry->tspec);
347 if (res < 0)
348 return -1;
349 }
350
351 return 0;
352 }
353
354 int main(int argc, char *argv[])
355 {
356 int sk_fd, timer_fd, res;
357 struct pollfd fds[2];
358
359 argp_parse(&argp, argc, argv, 0, NULL, NULL);
360
361 STAILQ_INIT(&nals);
362
363 sk_fd = create_listener_socket(ifname, macaddr, ETH_P_TSN);
364 if (sk_fd < 0)
365 return 1;
366
367 timer_fd = timerfd_create(CLOCK_REALTIME, 0);
368 if (timer_fd < 0) {
369 close(sk_fd);
370 return 1;
371 }
372
373 fds[0].fd = sk_fd;
374 fds[0].events = POLLIN;
375 fds[1].fd = timer_fd;
376 fds[1].events = POLLIN;
377
378 while (1) {
379 res = poll(fds, 2, -1);
380 if (res < 0) {
381 perror("Failed to poll() fds");
382 goto err;
383 }
384
385 if (fds[0].revents & POLLIN) {
386 res = new_packet(sk_fd, timer_fd);
387 if (res < 0)
388 goto err;
389 }
390
391 if (fds[1].revents & POLLIN) {
392 res = timeout(timer_fd);
393 if (res < 0)
394 goto err;
395 }
396 }
397
398 return 0;
399
400 err:
401 close(sk_fd);
402 close(timer_fd);
403 return 1;
404 }