| 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 Talker example. |
| 29 |
* |
| 30 |
* This example implements a very simple CVF talker application which reads |
| 31 |
* an H.264 byte-stream from stdin, creates CVF packets and transmit them via |
| 32 |
* network. |
| 33 |
* |
| 34 |
* For simplicity, this example supports only NAL units in byte-stream format, |
| 35 |
* and each NAL unit can not exceed 1400 bytes. |
| 36 |
* |
| 37 |
* TSN stream parameters (e.g. destination mac address, traffic priority) are |
| 38 |
* passed via command-line arguments. Run 'cvf-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 H.264 stream that is sent to |
| 50 |
* stdout, from where this example reads the stream. So, to generate |
| 51 |
* an H.264 video to send via TSN network, you can do something like: |
| 52 |
* |
| 53 |
* $ gst-launch-1.0 -e -q videotestsrc pattern=ball \ |
| 54 |
* ! video/x-raw,width=192,height=144 ! x264enc \ |
| 55 |
* ! video/x-h264,stream-format=byte-stream ! filesink location=/dev/stdout \ |
| 56 |
* | cvf-talker <args> |
| 57 |
* |
| 58 |
* Note that the `x264enc` may be changed by any other H.264 encoder |
| 59 |
* available, as long as it generates a byte-stream with NAL units no longer |
| 60 |
* than 1400 bytes. |
| 61 |
*/ |
| 62 |
|
| 63 |
#include <alloca.h> |
| 64 |
#include <argp.h> |
| 65 |
#include <arpa/inet.h> |
| 66 |
#include <assert.h> |
| 67 |
#include <linux/if.h> |
| 68 |
#include <linux/if_ether.h> |
| 69 |
#include <linux/if_packet.h> |
| 70 |
#include <stdbool.h> |
| 71 |
#include <stdlib.h> |
| 72 |
#include <string.h> |
| 73 |
#include <unistd.h> |
| 74 |
|
| 75 |
#include "avtp.h" |
| 76 |
#include "avtp_cvf.h" |
| 77 |
#include "examples/common.h" |
| 78 |
|
| 79 |
#define STREAM_ID 0xAABBCCDDEEFF0001 |
| 80 |
#define DATA_LEN 1400 |
| 81 |
#define AVTP_H264_HEADER_LEN (sizeof(uint32_t)) |
| 82 |
#define AVTP_FULL_HEADER_LEN (sizeof(struct avtp_stream_pdu) + AVTP_H264_HEADER_LEN) |
| 83 |
#define MAX_PDU_SIZE (AVTP_FULL_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 char buffer[MAX_PDU_SIZE * 2]; |
| 91 |
static size_t buffer_level; |
| 92 |
|
| 93 |
static uint8_t seq_num; |
| 94 |
|
| 95 |
enum process_result {PROCESS_OK, PROCESS_NONE, PROCESS_ERROR}; |
| 96 |
|
| 97 |
static struct argp_option options[] = { |
| 98 |
{"dst-addr", 'd', "MACADDR", 0, "Stream Destination MAC address" }, |
| 99 |
{"ifname", 'i', "IFNAME", 0, "Network Interface" }, |
| 100 |
{"max-transit-time", 'm', "MSEC", 0, "Maximum Transit Time in ms" }, |
| 101 |
{"prio", 'p', "NUM", 0, "SO_PRIORITY to be set in socket" }, |
| 102 |
{ 0 } |
| 103 |
}; |
| 104 |
|
| 105 |
static error_t parser(int key, char *arg, struct argp_state *state) |
| 106 |
{ |
| 107 |
int res; |
| 108 |
|
| 109 |
switch (key) { |
| 110 |
case 'd': |
| 111 |
res = sscanf(arg, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
| 112 |
&macaddr[0], &macaddr[1], &macaddr[2], |
| 113 |
&macaddr[3], &macaddr[4], &macaddr[5]); |
| 114 |
if (res != 6) { |
| 115 |
fprintf(stderr, "Invalid address\n"); |
| 116 |
exit(EXIT_FAILURE); |
| 117 |
} |
| 118 |
|
| 119 |
break; |
| 120 |
case 'i': |
| 121 |
strncpy(ifname, arg, sizeof(ifname) - 1); |
| 122 |
break; |
| 123 |
case 'm': |
| 124 |
max_transit_time = atoi(arg); |
| 125 |
break; |
| 126 |
case 'p': |
| 127 |
priority = atoi(arg); |
| 128 |
break; |
| 129 |
} |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
static struct argp argp = { options, parser }; |
| 135 |
|
| 136 |
static int init_pdu(struct avtp_stream_pdu *pdu) |
| 137 |
{ |
| 138 |
int res; |
| 139 |
|
| 140 |
res = avtp_cvf_pdu_init(pdu, AVTP_CVF_FORMAT_SUBTYPE_H264); |
| 141 |
if (res < 0) |
| 142 |
return -1; |
| 143 |
|
| 144 |
res = avtp_cvf_pdu_set(pdu, AVTP_CVF_FIELD_TV, 1); |
| 145 |
if (res < 0) |
| 146 |
return -1; |
| 147 |
|
| 148 |
res = avtp_cvf_pdu_set(pdu, AVTP_CVF_FIELD_STREAM_ID, STREAM_ID); |
| 149 |
if (res < 0) |
| 150 |
return -1; |
| 151 |
|
| 152 |
/* Just state that all data is part of the frame (M=1) */ |
| 153 |
res = avtp_cvf_pdu_set(pdu, AVTP_CVF_FIELD_M, 1); |
| 154 |
if (res < 0) |
| 155 |
return -1; |
| 156 |
|
| 157 |
/* No H.264 timestamp now */ |
| 158 |
res = avtp_cvf_pdu_set(pdu, AVTP_CVF_FIELD_H264_TIMESTAMP, 0); |
| 159 |
if (res < 0) |
| 160 |
return -1; |
| 161 |
|
| 162 |
/* No H.264 timestamp means no PTV */ |
| 163 |
res = avtp_cvf_pdu_set(pdu, AVTP_CVF_FIELD_H264_PTV, 0); |
| 164 |
if (res < 0) |
| 165 |
return -1; |
| 166 |
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
static ssize_t fill_buffer(void) |
| 171 |
{ |
| 172 |
ssize_t n; |
| 173 |
|
| 174 |
n = read(STDIN_FILENO, buffer + buffer_level, |
| 175 |
sizeof(buffer) - buffer_level); |
| 176 |
if (n < 0) { |
| 177 |
perror("Could not read from standard input"); |
| 178 |
} |
| 179 |
|
| 180 |
buffer_level += n; |
| 181 |
|
| 182 |
return n; |
| 183 |
} |
| 184 |
|
| 185 |
static ssize_t start_code_position(size_t offset) |
| 186 |
{ |
| 187 |
assert(offset < buffer_level); |
| 188 |
|
| 189 |
/* Simplified Boyer-Moore, inspired by gstreamer */ |
| 190 |
while (offset < buffer_level - 2) { |
| 191 |
if (buffer[offset + 2] == 0x1) { |
| 192 |
if (buffer[offset] == 0x0 && buffer[offset + 1] == 0x0) |
| 193 |
return offset; |
| 194 |
offset += 3; |
| 195 |
} else if (buffer[offset + 2] == 0x0) { |
| 196 |
offset++; |
| 197 |
} else { |
| 198 |
offset += 3; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return -1; |
| 203 |
} |
| 204 |
|
| 205 |
static int prepare_packet(struct avtp_stream_pdu *pdu, char *nal_data, |
| 206 |
size_t nal_data_len) |
| 207 |
{ |
| 208 |
int res; |
| 209 |
uint32_t avtp_time; |
| 210 |
struct avtp_cvf_h264_payload *h264_pay = |
| 211 |
(struct avtp_cvf_h264_payload *) pdu->avtp_payload; |
| 212 |
|
| 213 |
res = calculate_avtp_time(&avtp_time, max_transit_time); |
| 214 |
if (res < 0) { |
| 215 |
fprintf(stderr, "Failed to calculate avtp time\n"); |
| 216 |
return -1; |
| 217 |
} |
| 218 |
|
| 219 |
res = avtp_cvf_pdu_set(pdu, AVTP_CVF_FIELD_TIMESTAMP, |
| 220 |
avtp_time); |
| 221 |
if (res < 0) |
| 222 |
return -1; |
| 223 |
|
| 224 |
res = avtp_cvf_pdu_set(pdu, AVTP_CVF_FIELD_SEQ_NUM, seq_num++); |
| 225 |
if (res < 0) |
| 226 |
return -1; |
| 227 |
|
| 228 |
/* Stream data len includes AVTP H264 header, as this is part |
| 229 |
* of the payload too*/ |
| 230 |
res = avtp_cvf_pdu_set(pdu, AVTP_CVF_FIELD_STREAM_DATA_LEN, |
| 231 |
nal_data_len + AVTP_H264_HEADER_LEN); |
| 232 |
if (res < 0) |
| 233 |
return -1; |
| 234 |
|
| 235 |
memcpy(h264_pay->h264_data, nal_data, nal_data_len); |
| 236 |
|
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
static int process_nal(struct avtp_stream_pdu *pdu, bool process_last, |
| 241 |
size_t *nal_len) |
| 242 |
{ |
| 243 |
int res; |
| 244 |
ssize_t start, end; |
| 245 |
|
| 246 |
*nal_len = 0; |
| 247 |
|
| 248 |
start = start_code_position(0); |
| 249 |
if (start == -1) { |
| 250 |
fprintf(stderr, "Unable to find NAL start\n"); |
| 251 |
return PROCESS_NONE; |
| 252 |
} |
| 253 |
/* Now, let's find where the next starts. This is where current ends */ |
| 254 |
end = start_code_position(start + 1); |
| 255 |
if (end == -1) { |
| 256 |
if (process_last == false) { |
| 257 |
return PROCESS_NONE; |
| 258 |
} else { |
| 259 |
end = buffer_level; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
*nal_len = end - start; |
| 264 |
if (*nal_len > DATA_LEN) { |
| 265 |
fprintf(stderr, "NAL length bigger than expected. Expected %u, " |
| 266 |
"found %zd\n", DATA_LEN, *nal_len); |
| 267 |
goto err; |
| 268 |
} |
| 269 |
|
| 270 |
/* Sets AVTP packet headers and content - the NAL unit */ |
| 271 |
res = prepare_packet(pdu, &buffer[start], *nal_len); |
| 272 |
if (res < 0) { |
| 273 |
goto err; |
| 274 |
} |
| 275 |
|
| 276 |
/* Finally, let's offset any remaining data on the buffer to the |
| 277 |
* beginning. Not really efficient, but keep things simple */ |
| 278 |
memmove(buffer, buffer + end, buffer_level - end); |
| 279 |
buffer_level -= end; |
| 280 |
|
| 281 |
return PROCESS_OK; |
| 282 |
|
| 283 |
err: |
| 284 |
return PROCESS_ERROR; |
| 285 |
} |
| 286 |
|
| 287 |
int main(int argc, char *argv[]) |
| 288 |
{ |
| 289 |
int fd, res; |
| 290 |
struct sockaddr_ll sk_addr; |
| 291 |
struct avtp_stream_pdu *pdu = alloca(MAX_PDU_SIZE); |
| 292 |
|
| 293 |
argp_parse(&argp, argc, argv, 0, NULL, NULL); |
| 294 |
|
| 295 |
fd = create_talker_socket(priority); |
| 296 |
if (fd < 0) |
| 297 |
return 1; |
| 298 |
|
| 299 |
res = setup_socket_address(fd, ifname, macaddr, ETH_P_TSN, &sk_addr); |
| 300 |
if (res < 0) |
| 301 |
goto err; |
| 302 |
|
| 303 |
res = init_pdu(pdu); |
| 304 |
if (res < 0) |
| 305 |
goto err; |
| 306 |
|
| 307 |
while (1) { |
| 308 |
ssize_t n; |
| 309 |
bool end = false; |
| 310 |
|
| 311 |
n = fill_buffer(); |
| 312 |
if (n == 0) |
| 313 |
end = true; |
| 314 |
|
| 315 |
while (buffer_level > 0) { |
| 316 |
enum process_result pr = |
| 317 |
process_nal(pdu, end, (size_t *)&n); |
| 318 |
if (pr == PROCESS_ERROR) |
| 319 |
goto err; |
| 320 |
if (pr == PROCESS_NONE) |
| 321 |
break; |
| 322 |
|
| 323 |
n = sendto(fd, pdu, AVTP_FULL_HEADER_LEN + n, 0, |
| 324 |
(struct sockaddr *) &sk_addr, sizeof(sk_addr)); |
| 325 |
if (n < 0) { |
| 326 |
perror("Failed to send data"); |
| 327 |
goto err; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
if (end) |
| 332 |
break; |
| 333 |
} |
| 334 |
|
| 335 |
close(fd); |
| 336 |
return 0; |
| 337 |
|
| 338 |
err: |
| 339 |
close(fd); |
| 340 |
return 1; |
| 341 |
} |