summaryrefslogtreecommitdiff
path: root/ext/avtp/gstavtpsink.c
blob: a825049d8ff0644f485c8ceb874ef288f6d3b213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
 * GStreamer AVTP Plugin
 * Copyright (C) 2019 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later
 * version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 */

/**
 * SECTION:element-avtpsink
 * @see_also: avtpsrc
 *
 * avtpsink is a network sink that sends AVTPDUs to the network. It should be
 * combined with AVTP payloaders to implement an AVTP talker. For more
 * information see https://standards.ieee.org/standard/1722-2016.html.
 *
 * <note>
 * This element opens an AF_PACKET socket which requires CAP_NET_RAW
 * capability. Therefore, applications must have that capability in order to
 * successfully use this element. The capability can be dropped by the
 * application after the element transitions to PAUSED state if wanted.
 * </note>
 *
 * <refsect2>
 * <title>Example pipeline</title>
 * |[
 * gst-launch-1.0 audiotestsrc ! audioconvert ! avtpaafpay ! avtpsink
 * ]| This example pipeline implements an AVTP talker that transmit an AAF
 * stream.
 * </refsect2>
 */

#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <linux/net_tstamp.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>

#include "gstavtpsink.h"

GST_DEBUG_CATEGORY_STATIC (avtpsink_debug);
#define GST_CAT_DEFAULT (avtpsink_debug)

#define DEFAULT_IFNAME "eth0"
#define DEFAULT_ADDRESS "01:AA:AA:AA:AA:AA"
#define DEFAULT_PRIORITY 0

#define NSEC_PER_SEC  1000000000
#define TAI_OFFSET    (37ULL * NSEC_PER_SEC)
#define UTC_TO_TAI(t) (t + TAI_OFFSET)

enum
{
  PROP_0,
  PROP_IFNAME,
  PROP_ADDRESS,
  PROP_PRIORITY,
};

static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-avtp")
    );

#define gst_avtp_sink_parent_class parent_class
G_DEFINE_TYPE (GstAvtpSink, gst_avtp_sink, GST_TYPE_BASE_SINK);

static void gst_avtp_sink_finalize (GObject * gobject);
static void gst_avtp_sink_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_avtp_sink_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

static gboolean gst_avtp_sink_start (GstBaseSink * basesink);
static gboolean gst_avtp_sink_stop (GstBaseSink * basesink);
static GstFlowReturn gst_avtp_sink_render (GstBaseSink * basesink, GstBuffer *
    buffer);
static void gst_avtp_sink_get_times (GstBaseSink * bsink, GstBuffer * buffer,
    GstClockTime * start, GstClockTime * end);

static void
gst_avtp_sink_class_init (GstAvtpSinkClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
  GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS (klass);

  object_class->finalize = gst_avtp_sink_finalize;
  object_class->get_property = gst_avtp_sink_get_property;
  object_class->set_property = gst_avtp_sink_set_property;

  g_object_class_install_property (object_class, PROP_IFNAME,
      g_param_spec_string ("ifname", "Interface Name",
          "Network interface utilized to transmit AVTPDUs",
          DEFAULT_IFNAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
          GST_PARAM_MUTABLE_READY));
  g_object_class_install_property (object_class, PROP_ADDRESS,
      g_param_spec_string ("address", "Destination MAC address",
          "Destination MAC address from Ethernet frames",
          DEFAULT_ADDRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
          GST_PARAM_MUTABLE_READY));
  g_object_class_install_property (object_class, PROP_PRIORITY,
      g_param_spec_int ("priority", "Socket priority",
          "Priority configured into socket (SO_PRIORITY)", 0, G_MAXINT,
          DEFAULT_PRIORITY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
          GST_PARAM_MUTABLE_READY));

  gst_element_class_add_static_pad_template (element_class, &sink_template);

  gst_element_class_set_static_metadata (element_class,
      "Audio/Video Transport Protocol (AVTP) Sink",
      "Sink/Network", "Send AVTPDUs over the network",
      "Andre Guedes <andre.guedes@intel.com>");

  basesink_class->start = GST_DEBUG_FUNCPTR (gst_avtp_sink_start);
  basesink_class->stop = GST_DEBUG_FUNCPTR (gst_avtp_sink_stop);
  basesink_class->render = GST_DEBUG_FUNCPTR (gst_avtp_sink_render);
  basesink_class->get_times = GST_DEBUG_FUNCPTR (gst_avtp_sink_get_times);

  GST_DEBUG_CATEGORY_INIT (avtpsink_debug, "avtpsink", 0, "AVTP Sink");
}

static void
gst_avtp_sink_init (GstAvtpSink * avtpsink)
{
  gst_base_sink_set_sync (GST_BASE_SINK (avtpsink), TRUE);

  avtpsink->ifname = g_strdup (DEFAULT_IFNAME);
  avtpsink->address = g_strdup (DEFAULT_ADDRESS);
  avtpsink->priority = DEFAULT_PRIORITY;
  avtpsink->sk_fd = -1;
  memset (&avtpsink->sk_addr, 0, sizeof (avtpsink->sk_addr));
}

static void
gst_avtp_sink_finalize (GObject * object)
{
  GstAvtpSink *avtpsink = GST_AVTP_SINK (object);

  g_free (avtpsink->ifname);
  g_free (avtpsink->address);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gst_avtp_sink_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstAvtpSink *avtpsink = GST_AVTP_SINK (object);

  GST_DEBUG_OBJECT (avtpsink, "prop_id %u", prop_id);

  switch (prop_id) {
    case PROP_IFNAME:
      g_free (avtpsink->ifname);
      avtpsink->ifname = g_value_dup_string (value);
      break;
    case PROP_ADDRESS:
      g_free (avtpsink->address);
      avtpsink->address = g_value_dup_string (value);
      break;
    case PROP_PRIORITY:
      avtpsink->priority = g_value_get_int (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_avtp_sink_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstAvtpSink *avtpsink = GST_AVTP_SINK (object);

  GST_DEBUG_OBJECT (avtpsink, "prop_id %u", prop_id);

  switch (prop_id) {
    case PROP_IFNAME:
      g_value_set_string (value, avtpsink->ifname);
      break;
    case PROP_ADDRESS:
      g_value_set_string (value, avtpsink->address);
      break;
    case PROP_PRIORITY:
      g_value_set_int (value, avtpsink->priority);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static gboolean
gst_avtp_sink_init_socket (GstAvtpSink * avtpsink)
{
  int fd, res;
  unsigned int index;
  guint8 addr[ETH_ALEN];
  struct sockaddr_ll sk_addr;
  struct sock_txtime txtime_cfg;

  index = if_nametoindex (avtpsink->ifname);
  if (!index) {
    GST_ERROR_OBJECT (avtpsink, "Failed to get if_index: %s", strerror (errno));
    return FALSE;
  }

  fd = socket (AF_PACKET, SOCK_DGRAM, htons (ETH_P_TSN));
  if (fd < 0) {
    GST_ERROR_OBJECT (avtpsink, "Failed to open socket: %s", strerror (errno));
    return FALSE;
  }

  res = setsockopt (fd, SOL_SOCKET, SO_PRIORITY, &avtpsink->priority,
      sizeof (avtpsink->priority));
  if (res < 0) {
    GST_ERROR_OBJECT (avtpsink, "Failed to socket priority: %s", strerror
        (errno));
    goto err;
  }

  txtime_cfg.clockid = CLOCK_TAI;
  txtime_cfg.flags = 0;
  res = setsockopt (fd, SOL_SOCKET, SO_TXTIME, &txtime_cfg,
      sizeof (txtime_cfg));
  if (res < 0) {
    GST_ERROR_OBJECT (avtpsink, "Failed to set SO_TXTIME: %s", strerror
        (errno));
    goto err;
  }

  res = sscanf (avtpsink->address, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
      &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]);
  if (res != 6) {
    GST_ERROR_OBJECT (avtpsink, "Destination MAC address format not valid");
    goto err;
  }

  sk_addr.sll_family = AF_PACKET;
  sk_addr.sll_protocol = htons (ETH_P_TSN);
  sk_addr.sll_halen = ETH_ALEN;
  sk_addr.sll_ifindex = index;
  sk_addr.sll_hatype = 0;
  sk_addr.sll_pkttype = 0;
  memcpy (sk_addr.sll_addr, addr, ETH_ALEN);

  avtpsink->sk_fd = fd;
  avtpsink->sk_addr = sk_addr;

  return TRUE;

err:
  close (fd);
  return FALSE;
}

static void
gst_avtp_sink_init_msghdr (GstAvtpSink * avtpsink)
{
  struct msghdr *msg;
  struct cmsghdr *cmsg;

  msg = g_malloc0 (sizeof (struct msghdr));
  msg->msg_name = &avtpsink->sk_addr;
  msg->msg_namelen = sizeof (avtpsink->sk_addr);
  msg->msg_iovlen = 1;
  msg->msg_iov = g_malloc0 (sizeof (struct iovec));
  msg->msg_controllen = CMSG_SPACE (sizeof (__u64));
  msg->msg_control = g_malloc0 (msg->msg_controllen);

  cmsg = CMSG_FIRSTHDR (msg);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_TXTIME;
  cmsg->cmsg_len = CMSG_LEN (sizeof (__u64));

  avtpsink->msg = msg;
}

static gboolean
gst_avtp_sink_start (GstBaseSink * basesink)
{
  GstAvtpSink *avtpsink = GST_AVTP_SINK (basesink);

  if (!gst_avtp_sink_init_socket (avtpsink))
    return FALSE;

  gst_avtp_sink_init_msghdr (avtpsink);

  GST_DEBUG_OBJECT (avtpsink, "AVTP sink started");

  return TRUE;
}

static gboolean
gst_avtp_sink_stop (GstBaseSink * basesink)
{
  GstAvtpSink *avtpsink = GST_AVTP_SINK (basesink);

  g_free (avtpsink->msg->msg_iov);
  g_free (avtpsink->msg->msg_control);
  g_free (avtpsink->msg);
  close (avtpsink->sk_fd);

  GST_DEBUG_OBJECT (avtpsink, "AVTP sink stopped");
  return TRUE;
}

/* This function was heavily inspired by gst_base_sink_adjust_time() from
 * GstBaseSink.
 */
static GstClockTime
gst_avtp_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
{
  GstClockTimeDiff ts_offset;
  GstClockTime render_delay;

  /* don't do anything funny with invalid timestamps */
  if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
    return time;

  time += gst_base_sink_get_latency (basesink);

  /* apply offset, be careful for underflows */
  ts_offset = gst_base_sink_get_ts_offset (basesink);
  if (ts_offset < 0) {
    ts_offset = -ts_offset;
    if (ts_offset < time)
      time -= ts_offset;
    else
      time = 0;
  } else
    time += ts_offset;

  /* subtract the render delay again, which was included in the latency */
  render_delay = gst_base_sink_get_render_delay (basesink);
  if (time > render_delay)
    time -= render_delay;
  else
    time = 0;

  return time;
}

static GstFlowReturn
gst_avtp_sink_render (GstBaseSink * basesink, GstBuffer * buffer)
{
  ssize_t n;
  GstMapInfo info;
  GstAvtpSink *avtpsink = GST_AVTP_SINK (basesink);
  struct iovec *iov = avtpsink->msg->msg_iov;

  if (G_LIKELY (basesink->sync)) {
    GstClockTime base_time, running_time;
    struct cmsghdr *cmsg = CMSG_FIRSTHDR (avtpsink->msg);
    gint ret;

    g_assert (GST_BUFFER_DTS_OR_PTS (buffer) != GST_CLOCK_TIME_NONE);

    ret = gst_segment_to_running_time_full (&basesink->segment,
        basesink->segment.format, GST_BUFFER_DTS_OR_PTS (buffer),
        &running_time);
    if (ret == -1)
      running_time = -running_time;

    base_time = gst_element_get_base_time (GST_ELEMENT (avtpsink));
    running_time = gst_avtp_sink_adjust_time (basesink, running_time);
    *(__u64 *) CMSG_DATA (cmsg) = UTC_TO_TAI (base_time + running_time);
  }

  if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) {
    GST_ERROR_OBJECT (avtpsink, "Failed to map buffer");
    return GST_FLOW_ERROR;
  }

  iov->iov_base = info.data;
  iov->iov_len = info.size;

  n = sendmsg (avtpsink->sk_fd, avtpsink->msg, 0);
  if (n < 0) {
    GST_INFO_OBJECT (avtpsink, "Failed to send AVTPDU: %s", strerror (errno));
    goto out;
  }
  if (n != info.size) {
    GST_INFO_OBJECT (avtpsink, "Incomplete AVTPDU transmission");
    goto out;
  }

out:
  gst_buffer_unmap (buffer, &info);
  return GST_FLOW_OK;
}

static void
gst_avtp_sink_get_times (GstBaseSink * bsink, GstBuffer * buffer,
    GstClockTime * start, GstClockTime * end)
{
  /* Rendering synchronization is handled by the GstAvtpSink class itself, not
   * GstBaseSink so we set 'start' and 'end' to GST_CLOCK_TIME_NONE to signal
   * that to the base class.
   */
  *start = GST_CLOCK_TIME_NONE;
  *end = GST_CLOCK_TIME_NONE;
}

gboolean
gst_avtp_sink_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "avtpsink", GST_RANK_NONE,
      GST_TYPE_AVTP_SINK);
}