| 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 Talker example. |
| 29 |
* |
| 30 |
* This example implements a very simple AAF talker application which reads |
| 31 |
* a PCM stream from stdin, creates AAF packets and transmit them via the |
| 32 |
* network. |
| 33 |
* |
| 34 |
* For simplicity, the example supports only one set of PCM parameters: |
| 35 |
* - Sample format: 16-bit little endian |
| 36 |
* - Sample rate: 48 kHz |
| 37 |
* - Number of channels: 2 (stereo) |
| 38 |
* |
| 39 |
* TSN stream parameters (e.g. destination mac address, traffic priority) are |
| 40 |
* passed via command-line arguments. Run 'aaf-talker --help' for more |
| 41 |
* information. |
| 42 |
* |
| 43 |
* In order to have this example working properly, make sure you have |
| 44 |
* configured FQTSS feature from your NIC according (for further information |
| 45 |
* see tc-cbs(8)). Also, this example relies on system clock to set the AVTP |
| 46 |
* timestamp so make sure it is synchronized with the PTP Hardware Clock (PHC) |
| 47 |
* from your NIC and that the PHC is synchronized with the network clock. For |
| 48 |
* further information see ptp4l(8) and phc2sys(8). |
| 49 |
* |
| 50 |
* The easiest way to use this example is combining it with 'arecord' tool |
| 51 |
* provided by alsa-utils. 'arecord' reads the PCM stream from a capture ALSA |
| 52 |
* device (e.g. your microphone) and writes it to stdout. So to stream Audio |
| 53 |
* captured from your mic to a TSN network you should do something like this: |
| 54 |
* |
| 55 |
* $ arecord -f dat -t raw -D <capture-device> | aaf-talker <args> |
| 56 |
*/ |
| 57 |
|
| 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 <stdlib.h> |
| 64 |
#include <string.h> |
| 65 |
#include <unistd.h> |
| 66 |
|
| 67 |
#include "avtp.h" |
| 68 |
#include "avtp_aaf.h" |
| 69 |
#include "examples/common.h" |
| 70 |
|
| 71 |
#define STREAM_ID 0xAABBCCDDEEFF0001 |
| 72 |
#define SAMPLE_SIZE 2 /* Sample size in bytes. */ |
| 73 |
#define NUM_CHANNELS 2 |
| 74 |
#define DATA_LEN (SAMPLE_SIZE * NUM_CHANNELS) |
| 75 |
#define PDU_SIZE (sizeof(struct avtp_stream_pdu) + DATA_LEN) |
| 76 |
#define NSEC_PER_SEC 1000000000ULL |
| 77 |
#define NSEC_PER_MSEC 1000000ULL |
| 78 |
|
| 79 |
static char ifname[IFNAMSIZ]; |
| 80 |
static uint8_t macaddr[ETH_ALEN]; |
| 81 |
static int priority = -1; |
| 82 |
static int max_transit_time; |
| 83 |
|
| 84 |
static struct argp_option options[] = { |
| 85 |
{"dst-addr", 'd', "MACADDR", 0, "Stream Destination MAC address" }, |
| 86 |
{"ifname", 'i', "IFNAME", 0, "Network Interface" }, |
| 87 |
{"max-transit-time", 'm', "MSEC", 0, "Maximum Transit Time in ms" }, |
| 88 |
{"prio", 'p', "NUM", 0, "SO_PRIORITY to be set in socket" }, |
| 89 |
{ 0 } |
| 90 |
}; |
| 91 |
|
| 92 |
static error_t parser(int key, char *arg, struct argp_state *state) |
| 93 |
{ |
| 94 |
int res; |
| 95 |
|
| 96 |
switch (key) { |
| 97 |
case 'd': |
| 98 |
res = sscanf(arg, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
| 99 |
&macaddr[0], &macaddr[1], &macaddr[2], |
| 100 |
&macaddr[3], &macaddr[4], &macaddr[5]); |
| 101 |
if (res != 6) { |
| 102 |
fprintf(stderr, "Invalid address\n"); |
| 103 |
exit(EXIT_FAILURE); |
| 104 |
} |
| 105 |
|
| 106 |
break; |
| 107 |
case 'i': |
| 108 |
strncpy(ifname, arg, sizeof(ifname) - 1); |
| 109 |
break; |
| 110 |
case 'm': |
| 111 |
max_transit_time = atoi(arg); |
| 112 |
break; |
| 113 |
case 'p': |
| 114 |
priority = atoi(arg); |
| 115 |
break; |
| 116 |
} |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
static struct argp argp = { options, parser }; |
| 122 |
|
| 123 |
static int init_pdu(struct avtp_stream_pdu *pdu) |
| 124 |
{ |
| 125 |
int res; |
| 126 |
|
| 127 |
res = avtp_aaf_pdu_init(pdu); |
| 128 |
if (res < 0) |
| 129 |
return -1; |
| 130 |
|
| 131 |
res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_TV, 1); |
| 132 |
if (res < 0) |
| 133 |
return -1; |
| 134 |
|
| 135 |
res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_STREAM_ID, STREAM_ID); |
| 136 |
if (res < 0) |
| 137 |
return -1; |
| 138 |
|
| 139 |
res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_FORMAT, |
| 140 |
AVTP_AAF_FORMAT_INT_16BIT); |
| 141 |
if (res < 0) |
| 142 |
return -1; |
| 143 |
|
| 144 |
res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_NSR, |
| 145 |
AVTP_AAF_PCM_NSR_48KHZ); |
| 146 |
if (res < 0) |
| 147 |
return -1; |
| 148 |
|
| 149 |
res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_CHAN_PER_FRAME, |
| 150 |
NUM_CHANNELS); |
| 151 |
if (res < 0) |
| 152 |
return -1; |
| 153 |
|
| 154 |
res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_BIT_DEPTH, 16); |
| 155 |
if (res < 0) |
| 156 |
return -1; |
| 157 |
|
| 158 |
res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_STREAM_DATA_LEN, DATA_LEN); |
| 159 |
if (res < 0) |
| 160 |
return -1; |
| 161 |
|
| 162 |
res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_SP, AVTP_AAF_PCM_SP_NORMAL); |
| 163 |
if (res < 0) |
| 164 |
return -1; |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
int main(int argc, char *argv[]) |
| 170 |
{ |
| 171 |
int fd, res; |
| 172 |
struct sockaddr_ll sk_addr; |
| 173 |
struct avtp_stream_pdu *pdu = alloca(PDU_SIZE); |
| 174 |
uint8_t seq_num = 0; |
| 175 |
|
| 176 |
argp_parse(&argp, argc, argv, 0, NULL, NULL); |
| 177 |
|
| 178 |
fd = create_talker_socket(priority); |
| 179 |
if (fd < 0) |
| 180 |
return 1; |
| 181 |
|
| 182 |
res = setup_socket_address(fd, ifname, macaddr, ETH_P_TSN, &sk_addr); |
| 183 |
if (res < 0) |
| 184 |
goto err; |
| 185 |
|
| 186 |
res = init_pdu(pdu); |
| 187 |
if (res < 0) |
| 188 |
goto err; |
| 189 |
|
| 190 |
while (1) { |
| 191 |
ssize_t n; |
| 192 |
uint32_t avtp_time; |
| 193 |
|
| 194 |
memset(pdu->avtp_payload, 0, DATA_LEN); |
| 195 |
|
| 196 |
n = read(STDIN_FILENO, pdu->avtp_payload, DATA_LEN); |
| 197 |
if (n == 0) |
| 198 |
break; |
| 199 |
|
| 200 |
if (n != DATA_LEN) { |
| 201 |
fprintf(stderr, "read %zd bytes, expected %d\n", |
| 202 |
n, DATA_LEN); |
| 203 |
} |
| 204 |
|
| 205 |
res = calculate_avtp_time(&avtp_time, max_transit_time); |
| 206 |
if (res < 0) { |
| 207 |
fprintf(stderr, "Failed to calculate avtp time\n"); |
| 208 |
goto err; |
| 209 |
} |
| 210 |
|
| 211 |
res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_TIMESTAMP, |
| 212 |
avtp_time); |
| 213 |
if (res < 0) |
| 214 |
goto err; |
| 215 |
|
| 216 |
res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_SEQ_NUM, seq_num++); |
| 217 |
if (res < 0) |
| 218 |
goto err; |
| 219 |
|
| 220 |
n = sendto(fd, pdu, PDU_SIZE, 0, |
| 221 |
(struct sockaddr *) &sk_addr, sizeof(sk_addr)); |
| 222 |
if (n < 0) { |
| 223 |
perror("Failed to send data"); |
| 224 |
goto err; |
| 225 |
} |
| 226 |
|
| 227 |
if (n != PDU_SIZE) { |
| 228 |
fprintf(stderr, "wrote %zd bytes, expected %zd\n", |
| 229 |
n, PDU_SIZE); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
close(fd); |
| 234 |
return 0; |
| 235 |
|
| 236 |
err: |
| 237 |
close(fd); |
| 238 |
return 1; |
| 239 |
} |