1 /*
2 * GStreamer AVTP Plugin
3 * Copyright (C) 2019 Intel Corporation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later
9 * version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * SECTION:element-avtpsink
24 * @see_also: avtpsrc
25 *
26 * avtpsink is a network sink that sends AVTPDUs to the network. It should be
27 * combined with AVTP payloaders to implement an AVTP talker. For more
28 * information see https://standards.ieee.org/standard/1722-2016.html.
29 *
30 * <note>
31 * This element opens an AF_PACKET socket which requires CAP_NET_RAW
32 * capability. Therefore, applications must have that capability in order to
33 * successfully use this element. The capability can be dropped by the
34 * application after the element transitions to PAUSED state if wanted.
35 * </note>
36 *
37 * <refsect2>
38 * <title>Example pipeline</title>
39 * |[
40 * gst-launch-1.0 audiotestsrc ! audioconvert ! avtpaafpay ! avtpsink
41 * ]| This example pipeline implements an AVTP talker that transmit an AAF
42 * stream.
43 * </refsect2>
44 */
45
46 #include <arpa/inet.h>
47 #include <linux/errqueue.h>
48 #include <linux/if_packet.h>
49 #include <linux/net_tstamp.h>
50 #include <net/ethernet.h>
51 #include <net/if.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <unistd.h>
57
58 #include "gstavtpsink.h"
59
60 GST_DEBUG_CATEGORY_STATIC (avtpsink_debug);
61 #define GST_CAT_DEFAULT (avtpsink_debug)
62
63 #define DEFAULT_IFNAME "eth0"
64 #define DEFAULT_ADDRESS "01:AA:AA:AA:AA:AA"
65 #define DEFAULT_PRIORITY 0
66
67 #define NSEC_PER_SEC 1000000000
68 #define TAI_OFFSET (37ULL * NSEC_PER_SEC)
69 #define UTC_TO_TAI(t) (t + TAI_OFFSET)
70
71 enum
72 {
73 PROP_0,
74 PROP_IFNAME,
75 PROP_ADDRESS,
76 PROP_PRIORITY,
77 };
78
79 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
80 GST_PAD_SINK,
81 GST_PAD_ALWAYS,
82 GST_STATIC_CAPS ("application/x-avtp")
83 );
84
85 #define gst_avtp_sink_parent_class parent_class
86 G_DEFINE_TYPE (GstAvtpSink, gst_avtp_sink, GST_TYPE_BASE_SINK);
87 GST_ELEMENT_REGISTER_DEFINE (avtpsink, "avtpsink", GST_RANK_NONE,
88 GST_TYPE_AVTP_SINK);
89 static void gst_avtp_sink_finalize (GObject * gobject);
90 static void gst_avtp_sink_set_property (GObject * object, guint prop_id,
91 const GValue * value, GParamSpec * pspec);
92 static void gst_avtp_sink_get_property (GObject * object, guint prop_id,
93 GValue * value, GParamSpec * pspec);
94
95 static gboolean gst_avtp_sink_start (GstBaseSink * basesink);
96 static gboolean gst_avtp_sink_stop (GstBaseSink * basesink);
97 static GstFlowReturn gst_avtp_sink_render (GstBaseSink * basesink, GstBuffer *
98 buffer);
99 static void gst_avtp_sink_get_times (GstBaseSink * bsink, GstBuffer * buffer,
100 GstClockTime * start, GstClockTime * end);
101
102 static void
gst_avtp_sink_class_init(GstAvtpSinkClass * klass)103 gst_avtp_sink_class_init (GstAvtpSinkClass * klass)
104 {
105 GObjectClass *object_class = G_OBJECT_CLASS (klass);
106 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
107 GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS (klass);
108
109 object_class->finalize = gst_avtp_sink_finalize;
110 object_class->get_property = gst_avtp_sink_get_property;
111 object_class->set_property = gst_avtp_sink_set_property;
112
113 g_object_class_install_property (object_class, PROP_IFNAME,
114 g_param_spec_string ("ifname", "Interface Name",
115 "Network interface utilized to transmit AVTPDUs",
116 DEFAULT_IFNAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
117 GST_PARAM_MUTABLE_READY));
118 g_object_class_install_property (object_class, PROP_ADDRESS,
119 g_param_spec_string ("address", "Destination MAC address",
120 "Destination MAC address from Ethernet frames",
121 DEFAULT_ADDRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
122 GST_PARAM_MUTABLE_READY));
123 g_object_class_install_property (object_class, PROP_PRIORITY,
124 g_param_spec_int ("priority", "Socket priority",
125 "Priority configured into socket (SO_PRIORITY)", 0, G_MAXINT,
126 DEFAULT_PRIORITY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
127 GST_PARAM_MUTABLE_READY));
128
129 gst_element_class_add_static_pad_template (element_class, &sink_template);
130
131 gst_element_class_set_static_metadata (element_class,
132 "Audio/Video Transport Protocol (AVTP) Sink",
133 "Sink/Network", "Send AVTPDUs over the network",
134 "Andre Guedes <andre.guedes@intel.com>");
135
136 basesink_class->start = GST_DEBUG_FUNCPTR (gst_avtp_sink_start);
137 basesink_class->stop = GST_DEBUG_FUNCPTR (gst_avtp_sink_stop);
138 basesink_class->render = GST_DEBUG_FUNCPTR (gst_avtp_sink_render);
139 basesink_class->get_times = GST_DEBUG_FUNCPTR (gst_avtp_sink_get_times);
140
141 GST_DEBUG_CATEGORY_INIT (avtpsink_debug, "avtpsink", 0, "AVTP Sink");
142 }
143
144 static void
gst_avtp_sink_init(GstAvtpSink * avtpsink)145 gst_avtp_sink_init (GstAvtpSink * avtpsink)
146 {
147 gst_base_sink_set_sync (GST_BASE_SINK (avtpsink), TRUE);
148
149 avtpsink->ifname = g_strdup (DEFAULT_IFNAME);
150 avtpsink->address = g_strdup (DEFAULT_ADDRESS);
151 avtpsink->priority = DEFAULT_PRIORITY;
152 avtpsink->sk_fd = -1;
153 memset (&avtpsink->sk_addr, 0, sizeof (avtpsink->sk_addr));
154 }
155
156 static void
gst_avtp_sink_finalize(GObject * object)157 gst_avtp_sink_finalize (GObject * object)
158 {
159 GstAvtpSink *avtpsink = GST_AVTP_SINK (object);
160
161 g_free (avtpsink->ifname);
162 g_free (avtpsink->address);
163
164 G_OBJECT_CLASS (parent_class)->finalize (object);
165 }
166
167 static void
gst_avtp_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)168 gst_avtp_sink_set_property (GObject * object, guint prop_id,
169 const GValue * value, GParamSpec * pspec)
170 {
171 GstAvtpSink *avtpsink = GST_AVTP_SINK (object);
172
173 GST_DEBUG_OBJECT (avtpsink, "prop_id %u", prop_id);
174
175 switch (prop_id) {
176 case PROP_IFNAME:
177 g_free (avtpsink->ifname);
178 avtpsink->ifname = g_value_dup_string (value);
179 break;
180 case PROP_ADDRESS:
181 g_free (avtpsink->address);
182 avtpsink->address = g_value_dup_string (value);
183 break;
184 case PROP_PRIORITY:
185 avtpsink->priority = g_value_get_int (value);
186 break;
187 default:
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 break;
190 }
191 }
192
193 static void
gst_avtp_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)194 gst_avtp_sink_get_property (GObject * object, guint prop_id,
195 GValue * value, GParamSpec * pspec)
196 {
197 GstAvtpSink *avtpsink = GST_AVTP_SINK (object);
198
199 GST_DEBUG_OBJECT (avtpsink, "prop_id %u", prop_id);
200
201 switch (prop_id) {
202 case PROP_IFNAME:
203 g_value_set_string (value, avtpsink->ifname);
204 break;
205 case PROP_ADDRESS:
206 g_value_set_string (value, avtpsink->address);
207 break;
208 case PROP_PRIORITY:
209 g_value_set_int (value, avtpsink->priority);
210 break;
211 default:
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213 break;
214 }
215 }
216
217 static gboolean
gst_avtp_sink_init_socket(GstAvtpSink * avtpsink)218 gst_avtp_sink_init_socket (GstAvtpSink * avtpsink)
219 {
220 int fd, res;
221 unsigned int index;
222 guint8 addr[ETH_ALEN];
223 struct sockaddr_ll sk_addr;
224 struct sock_txtime txtime_cfg;
225
226 index = if_nametoindex (avtpsink->ifname);
227 if (!index) {
228 GST_ERROR_OBJECT (avtpsink, "Failed to get if_index: %s",
229 g_strerror (errno));
230 return FALSE;
231 }
232
233 fd = socket (AF_PACKET, SOCK_DGRAM, htons (ETH_P_TSN));
234 if (fd < 0) {
235 GST_ERROR_OBJECT (avtpsink, "Failed to open socket: %s",
236 g_strerror (errno));
237 return FALSE;
238 }
239
240 res = setsockopt (fd, SOL_SOCKET, SO_PRIORITY, &avtpsink->priority,
241 sizeof (avtpsink->priority));
242 if (res < 0) {
243 GST_ERROR_OBJECT (avtpsink, "Failed to socket priority: %s", g_strerror
244 (errno));
245 goto err;
246 }
247
248 txtime_cfg.clockid = CLOCK_TAI;
249 txtime_cfg.flags = SOF_TXTIME_REPORT_ERRORS;
250 res = setsockopt (fd, SOL_SOCKET, SO_TXTIME, &txtime_cfg,
251 sizeof (txtime_cfg));
252 if (res < 0) {
253 GST_ERROR_OBJECT (avtpsink, "Failed to set SO_TXTIME: %s", g_strerror
254 (errno));
255 goto err;
256 }
257
258 res = sscanf (avtpsink->address, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
259 &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]);
260 if (res != 6) {
261 GST_ERROR_OBJECT (avtpsink, "Destination MAC address format not valid");
262 goto err;
263 }
264
265 sk_addr.sll_family = AF_PACKET;
266 sk_addr.sll_protocol = htons (ETH_P_TSN);
267 sk_addr.sll_halen = ETH_ALEN;
268 sk_addr.sll_ifindex = index;
269 sk_addr.sll_hatype = 0;
270 sk_addr.sll_pkttype = 0;
271 memcpy (sk_addr.sll_addr, addr, ETH_ALEN);
272
273 avtpsink->sk_fd = fd;
274 avtpsink->sk_addr = sk_addr;
275
276 return TRUE;
277
278 err:
279 close (fd);
280 return FALSE;
281 }
282
283 static void
gst_avtp_sink_init_msghdr(GstAvtpSink * avtpsink)284 gst_avtp_sink_init_msghdr (GstAvtpSink * avtpsink)
285 {
286 struct msghdr *msg;
287 struct cmsghdr *cmsg;
288
289 msg = g_malloc0 (sizeof (struct msghdr));
290 msg->msg_name = &avtpsink->sk_addr;
291 msg->msg_namelen = sizeof (avtpsink->sk_addr);
292 msg->msg_iovlen = 1;
293 msg->msg_iov = g_malloc0 (sizeof (struct iovec));
294 msg->msg_controllen = CMSG_SPACE (sizeof (__u64));
295 msg->msg_control = g_malloc0 (msg->msg_controllen);
296
297 cmsg = CMSG_FIRSTHDR (msg);
298 cmsg->cmsg_level = SOL_SOCKET;
299 cmsg->cmsg_type = SCM_TXTIME;
300 cmsg->cmsg_len = CMSG_LEN (sizeof (__u64));
301
302 avtpsink->msg = msg;
303 }
304
305 static gboolean
gst_avtp_sink_start(GstBaseSink * basesink)306 gst_avtp_sink_start (GstBaseSink * basesink)
307 {
308 GstAvtpSink *avtpsink = GST_AVTP_SINK (basesink);
309
310 if (!gst_avtp_sink_init_socket (avtpsink))
311 return FALSE;
312
313 gst_avtp_sink_init_msghdr (avtpsink);
314
315 GST_DEBUG_OBJECT (avtpsink, "AVTP sink started");
316
317 return TRUE;
318 }
319
320 static gboolean
gst_avtp_sink_stop(GstBaseSink * basesink)321 gst_avtp_sink_stop (GstBaseSink * basesink)
322 {
323 GstAvtpSink *avtpsink = GST_AVTP_SINK (basesink);
324
325 g_free (avtpsink->msg->msg_iov);
326 g_free (avtpsink->msg->msg_control);
327 g_free (avtpsink->msg);
328 close (avtpsink->sk_fd);
329
330 GST_DEBUG_OBJECT (avtpsink, "AVTP sink stopped");
331 return TRUE;
332 }
333
334 /* This function was heavily inspired by gst_base_sink_adjust_time() from
335 * GstBaseSink.
336 */
337 static GstClockTime
gst_avtp_sink_adjust_time(GstBaseSink * basesink,GstClockTime time)338 gst_avtp_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
339 {
340 GstClockTimeDiff ts_offset;
341 GstClockTime render_delay;
342
343 /* don't do anything funny with invalid timestamps */
344 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
345 return time;
346
347 time += gst_base_sink_get_latency (basesink);
348
349 /* apply offset, be careful for underflows */
350 ts_offset = gst_base_sink_get_ts_offset (basesink);
351 if (ts_offset < 0) {
352 ts_offset = -ts_offset;
353 if (ts_offset < time)
354 time -= ts_offset;
355 else
356 time = 0;
357 } else
358 time += ts_offset;
359
360 /* subtract the render delay again, which was included in the latency */
361 render_delay = gst_base_sink_get_render_delay (basesink);
362 if (time > render_delay)
363 time -= render_delay;
364 else
365 time = 0;
366
367 return time;
368 }
369
370 static void
gst_avtp_sink_process_error_queue(GstAvtpSink * avtpsink,int fd)371 gst_avtp_sink_process_error_queue (GstAvtpSink * avtpsink, int fd)
372 {
373 uint8_t msg_control[CMSG_SPACE (sizeof (struct sock_extended_err))];
374 unsigned char err_buffer[256];
375 struct sock_extended_err *serr;
376 struct cmsghdr *cmsg;
377
378 struct iovec iov = {
379 .iov_base = err_buffer,
380 .iov_len = sizeof (err_buffer)
381 };
382 struct msghdr msg = {
383 .msg_iov = &iov,
384 .msg_iovlen = 1,
385 .msg_control = msg_control,
386 .msg_controllen = sizeof (msg_control)
387 };
388
389 if (recvmsg (fd, &msg, MSG_ERRQUEUE) == -1) {
390 GST_LOG_OBJECT (avtpsink, "Could not get socket errqueue: recvmsg failed");
391 return;
392 }
393
394 cmsg = CMSG_FIRSTHDR (&msg);
395 while (cmsg != NULL) {
396 serr = (void *) CMSG_DATA (cmsg);
397 if (serr->ee_origin == SO_EE_ORIGIN_TXTIME) {
398 switch (serr->ee_code) {
399 case SO_EE_CODE_TXTIME_INVALID_PARAM:
400 case SO_EE_CODE_TXTIME_MISSED:
401 GST_INFO_OBJECT (avtpsink, "AVTPDU dropped due to being late. "
402 "Check stream spec and pipeline settings.");
403 break;
404 default:
405 break;
406 }
407
408 return;
409 }
410
411 cmsg = CMSG_NXTHDR (&msg, cmsg);
412 }
413 }
414
415 static GstFlowReturn
gst_avtp_sink_render(GstBaseSink * basesink,GstBuffer * buffer)416 gst_avtp_sink_render (GstBaseSink * basesink, GstBuffer * buffer)
417 {
418 ssize_t n;
419 GstMapInfo info;
420 GstAvtpSink *avtpsink = GST_AVTP_SINK (basesink);
421 struct iovec *iov = avtpsink->msg->msg_iov;
422
423 if (G_LIKELY (basesink->sync)) {
424 GstClockTime base_time, running_time;
425 struct cmsghdr *cmsg = CMSG_FIRSTHDR (avtpsink->msg);
426 gint ret;
427
428 g_assert (GST_BUFFER_DTS_OR_PTS (buffer) != GST_CLOCK_TIME_NONE);
429
430 ret = gst_segment_to_running_time_full (&basesink->segment,
431 basesink->segment.format, GST_BUFFER_DTS_OR_PTS (buffer),
432 &running_time);
433 if (ret == -1)
434 running_time = -running_time;
435
436 base_time = gst_element_get_base_time (GST_ELEMENT (avtpsink));
437 running_time = gst_avtp_sink_adjust_time (basesink, running_time);
438 *(__u64 *) CMSG_DATA (cmsg) = UTC_TO_TAI (base_time + running_time);
439 }
440
441 if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) {
442 GST_ERROR_OBJECT (avtpsink, "Failed to map buffer");
443 return GST_FLOW_ERROR;
444 }
445
446 iov->iov_base = info.data;
447 iov->iov_len = info.size;
448
449 n = sendmsg (avtpsink->sk_fd, avtpsink->msg, 0);
450 if (n < 0) {
451 GST_INFO_OBJECT (avtpsink, "Failed to send AVTPDU: %s", g_strerror (errno));
452
453 if (G_LIKELY (basesink->sync))
454 gst_avtp_sink_process_error_queue (avtpsink, avtpsink->sk_fd);
455
456 goto out;
457 }
458 if (n != info.size) {
459 GST_INFO_OBJECT (avtpsink, "Incomplete AVTPDU transmission");
460 goto out;
461 }
462
463 out:
464 gst_buffer_unmap (buffer, &info);
465 return GST_FLOW_OK;
466 }
467
468 static void
gst_avtp_sink_get_times(GstBaseSink * bsink,GstBuffer * buffer,GstClockTime * start,GstClockTime * end)469 gst_avtp_sink_get_times (GstBaseSink * bsink, GstBuffer * buffer,
470 GstClockTime * start, GstClockTime * end)
471 {
472 /* Rendering synchronization is handled by the GstAvtpSink class itself, not
473 * GstBaseSink so we set 'start' and 'end' to GST_CLOCK_TIME_NONE to signal
474 * that to the base class.
475 */
476 *start = GST_CLOCK_TIME_NONE;
477 *end = GST_CLOCK_TIME_NONE;
478 }
479