| 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 Talker example. |
| 29 |
* |
| 30 |
* This example implements a very simple IEC 61883 talker application which |
| 31 |
* reads an MPEG-TS stream from stdin, creates AVTP IEC 61883/IIDC packets and |
| 32 |
* transmits them via the network. |
| 33 |
* |
| 34 |
* For simplicity, the example supports only MPEG-TS streams, and only one |
| 35 |
* source packet is packed into each AVTP packet sent. |
| 36 |
* |
| 37 |
* TSN stream parameters (e.g. destination mac address, traffic priority) are |
| 38 |
* passed via command-line arguments. Run 'ieciidc-talker --help' for more |
| 39 |
* information. |
| 40 |
* |
| 41 |
* In order to have this example working properly, make sure you have |
| 42 |
* configured FQTSS feature from your NIC according (for further information |
| 43 |
* see tc-cbs(8)). Also, this example relies on system clock to set the AVTP |
| 44 |
* timestamp so make sure it is synchronized with the PTP Hardware Clock (PHC) |
| 45 |
* from your NIC and that the PHC is synchronized with the network clock. For |
| 46 |
* further information see ptp4l(8) and phc2sys(8). |
| 47 |
* |
| 48 |
* The easiest way to use this example is by combining it with a GStreamer |
| 49 |
* pipeline. We use GStreamer to provide an MPEG-TS stream that is sent to |
| 50 |
* stdout, from where this example reads the stream. So, to generate |
| 51 |
* an MPEG-TS video to send via TSN network, you can do something like: |
| 52 |
* |
| 53 |
* $ gst-launch-1.0 -e -q videotestsrc pattern=ball ! x264enc |
| 54 |
* ! mpegtsmux ! filesink location=/dev/stdout |
| 55 |
* | ieciidc-talker <args> |
| 56 |
* |
| 57 |
*/ |
| 58 |
|
| 59 |
#include <alloca.h> |
| 60 |
#include <argp.h> |
| 61 |
#include <arpa/inet.h> |
| 62 |
#include <assert.h> |
| 63 |
#include <linux/if.h> |
| 64 |
#include <linux/if_ether.h> |
| 65 |
#include <linux/if_packet.h> |
| 66 |
#include <stdio.h> |
| 67 |
#include <stdlib.h> |
| 68 |
#include <string.h> |
| 69 |
#include <sys/ioctl.h> |
| 70 |
#include <time.h> |
| 71 |
#include <unistd.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 |
static char ifname[IFNAMSIZ]; |
| 86 |
static uint8_t macaddr[ETH_ALEN]; |
| 87 |
static int priority = -1; |
| 88 |
static int max_transit_time; |
| 89 |
|
| 90 |
static struct argp_option options[] = { |
| 91 |
{"dst-addr", 'd', "MACADDR", 0, "Stream Destination MAC address" }, |
| 92 |
{"ifname", 'i', "IFNAME", 0, "Network Interface" }, |
| 93 |
{"max-transit-time", 'm', "MSEC", 0, "Maximum Transit Time in ms" }, |
| 94 |
{"prio", 'p', "NUM", 0, "SO_PRIORITY to be set in socket" }, |
| 95 |
{ 0 } |
| 96 |
}; |
| 97 |
|
| 98 |
static error_t parser(int key, char *arg, struct argp_state *state) |
| 99 |
{ |
| 100 |
int res; |
| 101 |
|
| 102 |
switch (key) { |
| 103 |
case 'd': |
| 104 |
res = sscanf(arg, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
| 105 |
&macaddr[0], &macaddr[1], &macaddr[2], |
| 106 |
&macaddr[3], &macaddr[4], &macaddr[5]); |
| 107 |
if (res != 6) { |
| 108 |
fprintf(stderr, "Invalid address\n"); |
| 109 |
exit(EXIT_FAILURE); |
| 110 |
} |
| 111 |
|
| 112 |
break; |
| 113 |
case 'i': |
| 114 |
strncpy(ifname, arg, sizeof(ifname) - 1); |
| 115 |
break; |
| 116 |
case 'm': |
| 117 |
max_transit_time = atoi(arg); |
| 118 |
break; |
| 119 |
case 'p': |
| 120 |
priority = atoi(arg); |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
static struct argp argp = { options, parser }; |
| 128 |
|
| 129 |
static void init_pdu(struct avtp_stream_pdu *pdu) |
| 130 |
{ |
| 131 |
int res; |
| 132 |
|
| 133 |
res = avtp_ieciidc_pdu_init(pdu, AVTP_IECIIDC_TAG_CIP); |
| 134 |
assert(res == 0); |
| 135 |
|
| 136 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_TV, 0); |
| 137 |
assert(res == 0); |
| 138 |
|
| 139 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_STREAM_ID, |
| 140 |
STREAM_ID); |
| 141 |
assert(res == 0); |
| 142 |
|
| 143 |
res = avtp_ieciidc_pdu_set(pdu, |
| 144 |
AVTP_IECIIDC_FIELD_STREAM_DATA_LEN, STREAM_DATA_LEN); |
| 145 |
assert(res == 0); |
| 146 |
|
| 147 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_GV, 0); |
| 148 |
assert(res == 0); |
| 149 |
|
| 150 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_GATEWAY_INFO, 0); |
| 151 |
assert(res == 0); |
| 152 |
|
| 153 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_CHANNEL, 31); |
| 154 |
assert(res == 0); |
| 155 |
|
| 156 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_CIP_QI_1, 0); |
| 157 |
assert(res == 0); |
| 158 |
|
| 159 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_CIP_SID, 63); |
| 160 |
assert(res == 0); |
| 161 |
|
| 162 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_CIP_DBS, 6); |
| 163 |
assert(res == 0); |
| 164 |
|
| 165 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_CIP_FN, 3); |
| 166 |
assert(res == 0); |
| 167 |
|
| 168 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_CIP_QPC, 0); |
| 169 |
assert(res == 0); |
| 170 |
|
| 171 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_CIP_SPH, 1); |
| 172 |
assert(res == 0); |
| 173 |
|
| 174 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_CIP_QI_2, 2); |
| 175 |
assert(res == 0); |
| 176 |
|
| 177 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_CIP_FMT, 32); |
| 178 |
assert(res == 0); |
| 179 |
|
| 180 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_CIP_TSF, 0); |
| 181 |
assert(res == 0); |
| 182 |
} |
| 183 |
|
| 184 |
int main(int argc, char *argv[]) |
| 185 |
{ |
| 186 |
int fd, res; |
| 187 |
struct sockaddr_ll sk_addr; |
| 188 |
struct avtp_stream_pdu *pdu = alloca(PDU_SIZE); |
| 189 |
struct avtp_ieciidc_cip_payload *pay = |
| 190 |
(struct avtp_ieciidc_cip_payload *)pdu->avtp_payload; |
| 191 |
uint8_t seq_num = 0; |
| 192 |
uint8_t dbc = 0; |
| 193 |
|
| 194 |
argp_parse(&argp, argc, argv, 0, NULL, NULL); |
| 195 |
|
| 196 |
fd = create_talker_socket(priority); |
| 197 |
if (fd < 0) |
| 198 |
return 1; |
| 199 |
|
| 200 |
res = setup_socket_address(fd, ifname, macaddr, ETH_P_TSN, &sk_addr); |
| 201 |
if (res < 0) |
| 202 |
goto err; |
| 203 |
|
| 204 |
init_pdu(pdu); |
| 205 |
|
| 206 |
while (1) { |
| 207 |
ssize_t n; |
| 208 |
uint32_t avtp_time; |
| 209 |
struct avtp_ieciidc_cip_source_packet *sp; |
| 210 |
|
| 211 |
memset(pay->cip_data_payload, 0, DATA_LEN); |
| 212 |
sp = (struct avtp_ieciidc_cip_source_packet *) |
| 213 |
pay->cip_data_payload; |
| 214 |
|
| 215 |
n = read(STDIN_FILENO, sp->cip_with_sph_payload, |
| 216 |
MPEG_TS_PACKET_LEN); |
| 217 |
if (n == 0) |
| 218 |
break; |
| 219 |
|
| 220 |
if (n != MPEG_TS_PACKET_LEN) { |
| 221 |
fprintf(stderr, "read %zd bytes, expected %d\n", n, |
| 222 |
MPEG_TS_PACKET_LEN); |
| 223 |
} |
| 224 |
|
| 225 |
res = calculate_avtp_time(&avtp_time, max_transit_time); |
| 226 |
if (res < 0) { |
| 227 |
fprintf(stderr, "Failed to calculate avtp time\n"); |
| 228 |
goto err; |
| 229 |
} |
| 230 |
|
| 231 |
/* There are no helpers for payload fields, so one must remember |
| 232 |
* of byte ordering |
| 233 |
*/ |
| 234 |
sp->avtp_source_packet_header_timestamp = htonl(avtp_time); |
| 235 |
|
| 236 |
res = avtp_ieciidc_pdu_set(pdu, |
| 237 |
AVTP_IECIIDC_FIELD_SEQ_NUM, seq_num++); |
| 238 |
assert(res == 0); |
| 239 |
|
| 240 |
res = avtp_ieciidc_pdu_set(pdu, AVTP_IECIIDC_FIELD_CIP_DBC, |
| 241 |
dbc); |
| 242 |
assert(res == 0); |
| 243 |
/* We just send one MPEG-TS packet per AVTP packet, so we |
| 244 |
* increase dbc by the number of blocks on one MPEG-TS packet |
| 245 |
*/ |
| 246 |
dbc += 8; |
| 247 |
|
| 248 |
n = sendto(fd, pdu, PDU_SIZE, 0, (struct sockaddr *) &sk_addr, |
| 249 |
sizeof(sk_addr)); |
| 250 |
if (n < 0) { |
| 251 |
perror("Failed to send data"); |
| 252 |
goto err; |
| 253 |
} |
| 254 |
|
| 255 |
if (n != PDU_SIZE) { |
| 256 |
fprintf(stderr, "wrote %zd bytes, expected %zd\n", n, |
| 257 |
PDU_SIZE); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
close(fd); |
| 262 |
return 0; |
| 263 |
|
| 264 |
err: |
| 265 |
close(fd); |
| 266 |
return 1; |
| 267 |
} |