| 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 |
/* IEC 61883/IIDC Listener example. |
| 29 |
* |
| 30 |
* This example implements a very simple IEC 61883/IIDC listener application |
| 31 |
* which receives AVTP packets from the network, retrieves the MPEG-TS packets, |
| 32 |
* and writes them to stdout once the presentation time is reached. |
| 33 |
* |
| 34 |
* For simplicity, the example supports MPEG-TS streams, and expects that each |
| 35 |
* AVTP packet contains only one source packet. |
| 36 |
* |
| 37 |
* TSN stream parameters such as destination mac address are passed via |
| 38 |
* command-line arguments. Run 'ieciidc-listener --help' for more information. |
| 39 |
* |
| 40 |
* This example relies on the system clock to schedule MPEG-TS packets for |
| 41 |
* playback. So make sure the system clock is synchronized with the PTP |
| 42 |
* Hardware Clock (PHC) from your NIC and that the PHC is synchronized with |
| 43 |
* the PTP time from the network. For further information on how to synchronize |
| 44 |
* those clocks see ptp4l(8) and phc2sys(8) man pages. |
| 45 |
* |
| 46 |
* The easiest way to use this example is by combining it with a GStreamer |
| 47 |
* pipeline. We provide an MPEG-TS stream that is sent to stdout, from where |
| 48 |
* GStreameer reads the stream. So, to send the MPEG-TS stream received from |
| 49 |
* the TSN network to GStreamer, you can do something like: |
| 50 |
* |
| 51 |
* $ ieciidc-listener <args> | gst-launch-1.0 -e -q filesrc location=/dev/stdin |
| 52 |
* ! tsdemux ! decodebin ! videoconvert ! autovideosink |
| 53 |
*/ |
| 54 |
|
| 55 |
#include <assert.h> |
| 56 |
#include <argp.h> |
| 57 |
#include <arpa/inet.h> |
| 58 |
#include <linux/if.h> |
| 59 |
#include <linux/if_ether.h> |
| 60 |
#include <linux/if_packet.h> |
| 61 |
#include <poll.h> |
| 62 |
#include <stdbool.h> |
| 63 |
#include <stdint.h> |
| 64 |
#include <stdio.h> |
| 65 |
#include <stdlib.h> |
| 66 |
#include <string.h> |
| 67 |
#include <sys/ioctl.h> |
| 68 |
#include <sys/queue.h> |
| 69 |
#include <sys/timerfd.h> |
| 70 |
#include <unistd.h> |
| 71 |
#include <inttypes.h> |
| 72 |
|
| 73 |
#include "avtp.h" |
| 74 |
#include "avtp_ieciidc.h" |
| 75 |
#include "examples/common.h" |
| 76 |
|
| 77 |
#define STREAM_ID 0xAABBCCDDEEFF0001 |
| 78 |
#define MPEG_TS_PACKET_LEN 188 |
| 79 |
|
| 80 |
#define DATA_LEN (MPEG_TS_PACKET_LEN + sizeof(uint32_t)) /* MPEG-TS size + SPH */ |
| 81 |
#define CIP_HEADER_LEN (sizeof(uint32_t) * 2) |
| 82 |
#define STREAM_DATA_LEN DATA_LEN + CIP_HEADER_LEN |
| 83 |
#define PDU_SIZE (sizeof(struct avtp_stream_pdu) + CIP_HEADER_LEN + DATA_LEN) |
| 84 |
|
| 85 |
struct packet_entry { |
| 86 |
STAILQ_ENTRY(packet_entry) entries; |
| 87 |
|
| 88 |
struct timespec tspec; |
| 89 |
uint8_t mpegts_packet[MPEG_TS_PACKET_LEN]; |
| 90 |
}; |
| 91 |
|
| 92 |
static STAILQ_HEAD(packet_queue, packet_entry) packets; |
| 93 |
static char ifname[IFNAMSIZ]; |
| 94 |
static uint8_t macaddr[ETH_ALEN]; |
| 95 |
static uint8_t expected_seq; |
| 96 |
static uint8_t expected_dbc; |
| 97 |
|
| 98 |
static struct argp_option options[] = { |
| 99 |
{"dst-addr", 'd', "MACADDR", 0, "Stream Destination MAC address" }, |
| 100 |
{"ifname", 'i', "IFNAME", 0, "Network Interface" }, |
| 101 |
{ 0 } |
| 102 |
}; |
| 103 |
|
| 104 |
static error_t parser(int key, char *arg, struct argp_state *state) |
| 105 |
{ |
| 106 |
int res; |
| 107 |
|
| 108 |
switch (key) { |
| 109 |
case 'd': |
| 110 |
res = sscanf(arg, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
| 111 |
&macaddr[0], &macaddr[1], &macaddr[2], |
| 112 |
&macaddr[3], &macaddr[4], &macaddr[5]); |
| 113 |
if (res != 6) { |
| 114 |
fprintf(stderr, "Invalid address\n"); |
| 115 |
exit(EXIT_FAILURE); |
| 116 |
} |
| 117 |
|
| 118 |
break; |
| 119 |
case 'i': |
| 120 |
strncpy(ifname, arg, sizeof(ifname) - 1); |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
static struct argp argp = { options, parser }; |
| 128 |
|
| 129 |
/* Schedule 'MPEG-TS packet' to be presented at time specified by 'tspec'. */ |
| 130 |
static int schedule_packet(int fd, struct timespec *tspec, uint8_t *mpeg_tsp) |
| 131 |
{ |
| 132 |
struct packet_entry *entry; |
| 133 |
|
| 134 |
entry = malloc(sizeof(*entry)); |
| 135 |
if (!entry) { |
| 136 |
fprintf(stderr, "Failed to allocate memory\n"); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
|
| 140 |
entry->tspec.tv_sec = tspec->tv_sec; |
| 141 |
entry->tspec.tv_nsec = tspec->tv_nsec; |
| 142 |
memcpy(entry->mpegts_packet, mpeg_tsp, MPEG_TS_PACKET_LEN); |
| 143 |
|
| 144 |
STAILQ_INSERT_TAIL(&packets, entry, entries); |
| 145 |
|
| 146 |
/* If this was the first entry inserted onto the queue, we need to arm |
| 147 |
* the timer. |
| 148 |
*/ |
| 149 |
if (STAILQ_FIRST(&packets) == entry) { |
| 150 |
int res; |
| 151 |
|
| 152 |
res = arm_timer(fd, tspec); |
| 153 |
if (res < 0) { |
| 154 |
STAILQ_REMOVE(&packets, entry, packet_entry, entries); |
| 155 |
free(entry); |
| 156 |
return -1; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
static bool is_valid_packet(struct avtp_stream_pdu *pdu) |
| 164 |
{ |
| 165 |
struct avtp_common_pdu *common = (struct avtp_common_pdu *) pdu; |
| 166 |
uint64_t val64; |
| 167 |
uint32_t val32; |
| 168 |
int res; |
| 169 |
|
| 170 |
res = avtp_pdu_get(common, AVTP_FIELD_SUBTYPE, &val32); |
| 171 |
assert(res == 0); |
| 172 |
if (val32 != AVTP_SUBTYPE_61883_IIDC) { |
| 173 |
fprintf(stderr, "Subtype mismatch: expected %u, got %u\n", |
| 174 |
AVTP_SUBTYPE_61883_IIDC, val32); |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
res = avtp_pdu_get(common, AVTP_FIELD_VERSION, &val32); |
| 179 |
assert(res == 0); |
| 180 |
if (val32 != 0) { |
| 181 |
fprintf(stderr, "Version mismatch: expected %u, got %u\n", |
| 182 |
0, val32); |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_TV, &val64); |
| 187 |
assert(res == 0); |
| 188 |
if (val64 != 0) { |
| 189 |
fprintf(stderr, "tv mismatch: expected %u, got %" PRIu64 "\n", |
| 190 |
0, val64); |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_STREAM_ID, &val64); |
| 195 |
assert(res == 0); |
| 196 |
if (val64 != STREAM_ID) { |
| 197 |
fprintf(stderr, "Stream ID mismatch: expected %" PRIu64 |
| 198 |
", got %" PRIu64 "\n", STREAM_ID, val64); |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_SEQ_NUM, &val64); |
| 203 |
assert(res == 0); |
| 204 |
|
| 205 |
if (val64 != expected_seq) { |
| 206 |
/* If we have a sequence number mismatch, we simply log the |
| 207 |
* issue and continue to process the packet. We don't want to |
| 208 |
* invalidate it since it is a valid packet after all. |
| 209 |
*/ |
| 210 |
fprintf(stderr, "Sequence number mismatch: expected %u, got %" |
| 211 |
PRIu64 "\n", expected_seq, val64); |
| 212 |
expected_seq = val64; |
| 213 |
} |
| 214 |
|
| 215 |
expected_seq++; |
| 216 |
|
| 217 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_STREAM_DATA_LEN, |
| 218 |
&val64); |
| 219 |
assert(res == 0); |
| 220 |
if (val64 != STREAM_DATA_LEN) { |
| 221 |
fprintf(stderr, "Data len mismatch: expected %lu, got %" |
| 222 |
PRIu64 "\n", STREAM_DATA_LEN, val64); |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_TAG, &val64); |
| 227 |
assert(res == 0); |
| 228 |
if (val64 != AVTP_IECIIDC_TAG_CIP) { |
| 229 |
fprintf(stderr, "tag mismatch: expected %u, got %" PRIu64 "\n", |
| 230 |
AVTP_IECIIDC_TAG_CIP, val64); |
| 231 |
return false; |
| 232 |
} |
| 233 |
|
| 234 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_CHANNEL, &val64); |
| 235 |
assert(res == 0); |
| 236 |
if (val64 != 31) { |
| 237 |
fprintf(stderr, "channel mismatch: expected %u, got %" PRIu64 |
| 238 |
"\n", 31, val64); |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_CIP_SID, &val64); |
| 243 |
assert(res == 0); |
| 244 |
if (val64 != 63) { |
| 245 |
fprintf(stderr, "sid mismatch: expected %u, got %" PRIu64 "\n", |
| 246 |
63, val64); |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_CIP_DBS, &val64); |
| 251 |
assert(res == 0); |
| 252 |
if (val64 != 6) { |
| 253 |
fprintf(stderr, "dbs mismatch: expected %u, got %" PRIu64 "\n", |
| 254 |
6, val64); |
| 255 |
return false; |
| 256 |
} |
| 257 |
|
| 258 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_CIP_FN, &val64); |
| 259 |
assert(res == 0); |
| 260 |
if (val64 != 3) { |
| 261 |
fprintf(stderr, "fn mismatch: expected %u, got %" PRIu64 "\n", |
| 262 |
3, val64); |
| 263 |
return false; |
| 264 |
} |
| 265 |
|
| 266 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_CIP_QPC, &val64); |
| 267 |
assert(res == 0); |
| 268 |
if (val64 != 0) { |
| 269 |
fprintf(stderr, "GV mismatch: expected %u, got %" PRIu64 "\n", |
| 270 |
0, val64); |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_CIP_SPH, &val64); |
| 275 |
assert(res == 0); |
| 276 |
if (val64 != 1) { |
| 277 |
fprintf(stderr, "sph mismatch: expected %u, got %" PRIu64 "\n", |
| 278 |
1, val64); |
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_CIP_FMT, &val64); |
| 283 |
assert(res == 0); |
| 284 |
if (val64 != 32) { |
| 285 |
fprintf(stderr, "fmt mismatch: expected %u, got %" PRIu64 "\n", |
| 286 |
32, val64); |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_CIP_TSF, &val64); |
| 291 |
assert(res == 0); |
| 292 |
if (val64 != 0) { |
| 293 |
fprintf(stderr, "tsf mismatch: expected %u, got %" PRIu64 "\n", |
| 294 |
0, val64); |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
res = avtp_ieciidc_pdu_get(pdu, AVTP_IECIIDC_FIELD_CIP_DBC, &val64); |
| 299 |
assert(res == 0); |
| 300 |
if (val64 != expected_dbc) { |
| 301 |
/* As with sequence mismatch, we'll not discard this packet, |
| 302 |
* only log that the mismatch happened */ |
| 303 |
fprintf(stderr, "dbc mismatch: expected %u, got %" PRIu64 "\n", |
| 304 |
expected_dbc, val64); |
| 305 |
} |
| 306 |
expected_dbc += 8; |
| 307 |
|
| 308 |
return true; |
| 309 |
} |
| 310 |
|
| 311 |
static int new_packet(int sk_fd, int timer_fd) |
| 312 |
{ |
| 313 |
int res; |
| 314 |
ssize_t n; |
| 315 |
uint32_t avtp_time; |
| 316 |
struct timespec tspec; |
| 317 |
struct avtp_stream_pdu *pdu = alloca(PDU_SIZE); |
| 318 |
struct avtp_ieciidc_cip_payload *pay = |
| 319 |
(struct avtp_ieciidc_cip_payload *) pdu->avtp_payload; |
| 320 |
struct avtp_ieciidc_cip_source_packet *sp = |
| 321 |
(struct avtp_ieciidc_cip_source_packet *) pay->cip_data_payload; |
| 322 |
|
| 323 |
memset(pdu, 0, PDU_SIZE); |
| 324 |
|
| 325 |
n = recv(sk_fd, pdu, PDU_SIZE, 0); |
| 326 |
if (n < 0 || n != PDU_SIZE) { |
| 327 |
perror("Failed to receive data"); |
| 328 |
return -1; |
| 329 |
} |
| 330 |
|
| 331 |
if (!is_valid_packet(pdu)) { |
| 332 |
fprintf(stderr, "Dropping packet\n"); |
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
/* There are no helpers for payload fields, so one must remember |
| 337 |
* of byte ordering */ |
| 338 |
avtp_time = ntohl(sp->avtp_source_packet_header_timestamp); |
| 339 |
|
| 340 |
res = get_presentation_time(avtp_time, &tspec); |
| 341 |
if (res < 0) |
| 342 |
return -1; |
| 343 |
|
| 344 |
res = schedule_packet(timer_fd, &tspec, sp->cip_with_sph_payload); |
| 345 |
if (res < 0) |
| 346 |
return -1; |
| 347 |
|
| 348 |
return 0; |
| 349 |
} |
| 350 |
|
| 351 |
static int timeout(int fd) |
| 352 |
{ |
| 353 |
int res; |
| 354 |
ssize_t n; |
| 355 |
uint64_t expirations; |
| 356 |
struct packet_entry *entry; |
| 357 |
|
| 358 |
n = read(fd, &expirations, sizeof(uint64_t)); |
| 359 |
if (n < 0) { |
| 360 |
perror("Failed to read timerfd"); |
| 361 |
return -1; |
| 362 |
} |
| 363 |
|
| 364 |
assert(expirations == 1); |
| 365 |
|
| 366 |
entry = STAILQ_FIRST(&packets); |
| 367 |
assert(entry != NULL); |
| 368 |
|
| 369 |
res = present_data(entry->mpegts_packet, MPEG_TS_PACKET_LEN); |
| 370 |
if (res < 0) |
| 371 |
return -1; |
| 372 |
|
| 373 |
STAILQ_REMOVE_HEAD(&packets, entries); |
| 374 |
free(entry); |
| 375 |
|
| 376 |
if (!STAILQ_EMPTY(&packets)) { |
| 377 |
entry = STAILQ_FIRST(&packets); |
| 378 |
|
| 379 |
res = arm_timer(fd, &entry->tspec); |
| 380 |
if (res < 0) |
| 381 |
return -1; |
| 382 |
} |
| 383 |
|
| 384 |
return 0; |
| 385 |
} |
| 386 |
|
| 387 |
int main(int argc, char *argv[]) |
| 388 |
{ |
| 389 |
int sk_fd, timer_fd, res; |
| 390 |
struct pollfd fds[2]; |
| 391 |
|
| 392 |
argp_parse(&argp, argc, argv, 0, NULL, NULL); |
| 393 |
|
| 394 |
STAILQ_INIT(&packets); |
| 395 |
|
| 396 |
sk_fd = create_listener_socket(ifname, macaddr, ETH_P_TSN); |
| 397 |
if (sk_fd < 0) |
| 398 |
return 1; |
| 399 |
|
| 400 |
timer_fd = timerfd_create(CLOCK_REALTIME, 0); |
| 401 |
if (timer_fd < 0) { |
| 402 |
close(sk_fd); |
| 403 |
return 1; |
| 404 |
} |
| 405 |
|
| 406 |
fds[0].fd = sk_fd; |
| 407 |
fds[0].events = POLLIN; |
| 408 |
fds[1].fd = timer_fd; |
| 409 |
fds[1].events = POLLIN; |
| 410 |
|
| 411 |
while (1) { |
| 412 |
res = poll(fds, 2, -1); |
| 413 |
if (res < 0) { |
| 414 |
perror("Failed to poll() fds"); |
| 415 |
goto err; |
| 416 |
} |
| 417 |
|
| 418 |
if (fds[0].revents & POLLIN) { |
| 419 |
res = new_packet(sk_fd, timer_fd); |
| 420 |
if (res < 0) |
| 421 |
goto err; |
| 422 |
} |
| 423 |
|
| 424 |
if (fds[1].revents & POLLIN) { |
| 425 |
res = timeout(timer_fd); |
| 426 |
if (res < 0) |
| 427 |
goto err; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
return 0; |
| 432 |
|
| 433 |
err: |
| 434 |
close(sk_fd); |
| 435 |
close(timer_fd); |
| 436 |
return 1; |
| 437 |
} |