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-avtpsrc
24 * @see_also: avtpsink
25 *
26 * avtpsrc is a network source that receives AVTPDUs from the network. It
27 * should be combined with AVTP depayloaders to implement an AVTP listener.
28 * For more information see https://standards.ieee.org/standard/1722-2016.html.
29 *
30 * <note>
31 * Applications must have CAP_NET_RAW capability in order to successfully use
32 * this element. See avtpsink documentation for further information.
33 * </note>
34 *
35 * <refsect2>
36 * <title>Example pipeline</title>
37 * |[
38 * gst-launch-1.0 avtpsrc ! avtpaafdepay ! autoaudiosink
39 * ]| This example pipeline implements an AVTP listener that plays an AAF
40 * stream back.
41 * </refsect2>
42 */
43
44 #include <arpa/inet.h>
45 #include <linux/if_packet.h>
46 #include <net/ethernet.h>
47 #include <net/if.h>
48 #include <stdio.h>
49 #include <sys/ioctl.h>
50 #include <sys/socket.h>
51 #include <unistd.h>
52
53 #include "gstavtpsrc.h"
54
55 GST_DEBUG_CATEGORY_STATIC (avtpsrc_debug);
56 #define GST_CAT_DEFAULT (avtpsrc_debug)
57
58 #define DEFAULT_IFNAME "eth0"
59 #define DEFAULT_ADDRESS "01:AA:AA:AA:AA:AA"
60
61 #define MAX_AVTPDU_SIZE 1500
62
63 enum
64 {
65 PROP_0,
66 PROP_IFNAME,
67 PROP_ADDRESS,
68 };
69
70 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
71 GST_PAD_SRC,
72 GST_PAD_ALWAYS,
73 GST_STATIC_CAPS ("application/x-avtp")
74 );
75
76 #define gst_avtp_src_parent_class parent_class
77 G_DEFINE_TYPE (GstAvtpSrc, gst_avtp_src, GST_TYPE_PUSH_SRC);
78 GST_ELEMENT_REGISTER_DEFINE (avtpsrc, "avtpsrc", GST_RANK_NONE,
79 GST_TYPE_AVTP_SRC);
80
81 static void gst_avtp_src_finalize (GObject * gobject);
82 static void gst_avtp_src_set_property (GObject * object, guint prop_id,
83 const GValue * value, GParamSpec * pspec);
84 static void gst_avtp_src_get_property (GObject * object, guint prop_id,
85 GValue * value, GParamSpec * pspec);
86
87 static gboolean gst_avtp_src_start (GstBaseSrc * basesrc);
88 static gboolean gst_avtp_src_stop (GstBaseSrc * basesrc);
89 static GstFlowReturn gst_avtp_src_fill (GstPushSrc * pushsrc, GstBuffer *
90 buffer);
91
92 static void
gst_avtp_src_class_init(GstAvtpSrcClass * klass)93 gst_avtp_src_class_init (GstAvtpSrcClass * klass)
94 {
95 GObjectClass *object_class = G_OBJECT_CLASS (klass);
96 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
97 GstBaseSrcClass *basesrc_class = GST_BASE_SRC_CLASS (klass);
98 GstPushSrcClass *pushsrc_class = GST_PUSH_SRC_CLASS (klass);
99
100 object_class->finalize = gst_avtp_src_finalize;
101 object_class->get_property = gst_avtp_src_get_property;
102 object_class->set_property = gst_avtp_src_set_property;
103
104 g_object_class_install_property (object_class, PROP_IFNAME,
105 g_param_spec_string ("ifname", "Interface Name",
106 "Network interface utilized to receive AVTPDUs",
107 DEFAULT_IFNAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
108 GST_PARAM_MUTABLE_READY));
109 g_object_class_install_property (object_class, PROP_ADDRESS,
110 g_param_spec_string ("address", "Destination MAC address",
111 "Destination MAC address to listen to",
112 DEFAULT_ADDRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
113 GST_PARAM_MUTABLE_READY));
114
115 gst_element_class_add_static_pad_template (element_class, &src_template);
116
117 gst_element_class_set_static_metadata (element_class,
118 "Audio/Video Transport Protocol (AVTP) Source",
119 "Src/Network", "Receive AVTPDUs from the network",
120 "Andre Guedes <andre.guedes@intel.com>");
121
122 basesrc_class->start = GST_DEBUG_FUNCPTR (gst_avtp_src_start);
123 basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_avtp_src_stop);
124 pushsrc_class->fill = GST_DEBUG_FUNCPTR (gst_avtp_src_fill);
125
126 GST_DEBUG_CATEGORY_INIT (avtpsrc_debug, "avtpsrc", 0, "AVTP Source");
127 }
128
129 static void
gst_avtp_src_init(GstAvtpSrc * avtpsrc)130 gst_avtp_src_init (GstAvtpSrc * avtpsrc)
131 {
132 gst_base_src_set_live (GST_BASE_SRC (avtpsrc), TRUE);
133 gst_base_src_set_format (GST_BASE_SRC (avtpsrc), GST_FORMAT_TIME);
134 gst_base_src_set_blocksize (GST_BASE_SRC (avtpsrc), MAX_AVTPDU_SIZE);
135
136 avtpsrc->ifname = g_strdup (DEFAULT_IFNAME);
137 avtpsrc->address = g_strdup (DEFAULT_ADDRESS);
138 avtpsrc->sk_fd = -1;
139 }
140
141 static void
gst_avtp_src_finalize(GObject * object)142 gst_avtp_src_finalize (GObject * object)
143 {
144 GstAvtpSrc *avtpsrc = GST_AVTP_SRC (object);
145
146 g_free (avtpsrc->ifname);
147 g_free (avtpsrc->address);
148
149 G_OBJECT_CLASS (parent_class)->finalize (object);
150 }
151
152 static void
gst_avtp_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)153 gst_avtp_src_set_property (GObject * object, guint prop_id,
154 const GValue * value, GParamSpec * pspec)
155 {
156 GstAvtpSrc *avtpsrc = GST_AVTP_SRC (object);
157
158 GST_DEBUG_OBJECT (avtpsrc, "prop_id %u", prop_id);
159
160 switch (prop_id) {
161 case PROP_IFNAME:
162 g_free (avtpsrc->ifname);
163 avtpsrc->ifname = g_value_dup_string (value);
164 break;
165 case PROP_ADDRESS:
166 g_free (avtpsrc->address);
167 avtpsrc->address = g_value_dup_string (value);
168 break;
169 default:
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171 break;
172 }
173 }
174
175 static void
gst_avtp_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)176 gst_avtp_src_get_property (GObject * object, guint prop_id,
177 GValue * value, GParamSpec * pspec)
178 {
179 GstAvtpSrc *avtpsrc = GST_AVTP_SRC (object);
180
181 GST_DEBUG_OBJECT (avtpsrc, "prop_id %u", prop_id);
182
183 switch (prop_id) {
184 case PROP_IFNAME:
185 g_value_set_string (value, avtpsrc->ifname);
186 break;
187 case PROP_ADDRESS:
188 g_value_set_string (value, avtpsrc->address);
189 break;
190 default:
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192 break;
193 }
194 }
195
196 static gboolean
gst_avtp_src_start(GstBaseSrc * basesrc)197 gst_avtp_src_start (GstBaseSrc * basesrc)
198 {
199 int fd, res;
200 unsigned int index;
201 guint8 addr[ETH_ALEN];
202 struct sockaddr_ll sk_addr = { 0 };
203 struct packet_mreq mreq = { 0 };
204 GstAvtpSrc *avtpsrc = GST_AVTP_SRC (basesrc);
205
206 index = if_nametoindex (avtpsrc->ifname);
207 if (!index) {
208 GST_ERROR_OBJECT (avtpsrc, "Failed to get if_index: %s",
209 g_strerror (errno));
210 return FALSE;
211 }
212
213 fd = socket (AF_PACKET, SOCK_DGRAM, htons (ETH_P_TSN));
214 if (fd < 0) {
215 GST_ERROR_OBJECT (avtpsrc, "Failed to open socket: %s", g_strerror (errno));
216 return FALSE;
217 }
218
219 sk_addr.sll_family = AF_PACKET;
220 sk_addr.sll_protocol = htons (ETH_P_TSN);
221 sk_addr.sll_ifindex = index;
222
223 res = bind (fd, (struct sockaddr *) &sk_addr, sizeof (sk_addr));
224 if (res < 0) {
225 GST_ERROR_OBJECT (avtpsrc, "Failed to bind socket: %s", g_strerror (errno));
226 goto err;
227 }
228
229 res = sscanf (avtpsrc->address, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
230 &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]);
231 if (res != 6) {
232 GST_ERROR_OBJECT (avtpsrc, "Destination MAC address format not valid");
233 goto err;
234 }
235
236 mreq.mr_ifindex = index;
237 mreq.mr_type = PACKET_MR_MULTICAST;
238 mreq.mr_alen = ETH_ALEN;
239 memcpy (&mreq.mr_address, addr, ETH_ALEN);
240 res = setsockopt (fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
241 sizeof (struct packet_mreq));
242 if (res < 0) {
243 GST_ERROR_OBJECT (avtpsrc, "Failed to set multicast address: %s",
244 g_strerror (errno));
245 goto err;
246 }
247
248 avtpsrc->sk_fd = fd;
249
250 GST_DEBUG_OBJECT (avtpsrc, "AVTP source started");
251 return TRUE;
252
253 err:
254 close (fd);
255 return FALSE;
256 }
257
258 static gboolean
gst_avtp_src_stop(GstBaseSrc * basesrc)259 gst_avtp_src_stop (GstBaseSrc * basesrc)
260 {
261 GstAvtpSrc *avtpsrc = GST_AVTP_SRC (basesrc);
262
263 close (avtpsrc->sk_fd);
264
265 GST_DEBUG_OBJECT (avtpsrc, "AVTP source stopped");
266 return TRUE;
267 }
268
269 static GstFlowReturn
gst_avtp_src_fill(GstPushSrc * pushsrc,GstBuffer * buffer)270 gst_avtp_src_fill (GstPushSrc * pushsrc, GstBuffer * buffer)
271 {
272 GstMapInfo map;
273 gsize buffer_size;
274 ssize_t n = MAX_AVTPDU_SIZE;
275 ssize_t received = -1;
276 GstAvtpSrc *avtpsrc = GST_AVTP_SRC (pushsrc);
277
278 buffer_size = gst_buffer_get_size (buffer);
279 if (G_UNLIKELY (buffer_size < MAX_AVTPDU_SIZE)) {
280 GST_WARNING_OBJECT (avtpsrc,
281 "Buffer size (%" G_GSIZE_FORMAT
282 ") may not be enough to hold AVTPDU (max AVTPDU size %d)", buffer_size,
283 MAX_AVTPDU_SIZE);
284 n = buffer_size;
285 }
286
287 if (!gst_buffer_map (buffer, &map, GST_MAP_WRITE)) {
288 GST_WARNING_OBJECT (avtpsrc, "Failed to map buffer");
289 return GST_FLOW_OK;
290 }
291
292 retry:
293 errno = 0;
294 received = recv (avtpsrc->sk_fd, map.data, n, 0);
295 if (received < 0) {
296 if (errno == EINTR) {
297 goto retry;
298 }
299 GST_ELEMENT_ERROR (avtpsrc, RESOURCE, READ, (NULL),
300 ("Failed to receive AVTPDU: %s", g_strerror (errno)));
301 gst_buffer_unmap (buffer, &map);
302
303 return GST_FLOW_ERROR;
304 }
305
306 gst_buffer_set_size (buffer, received);
307 gst_buffer_unmap (buffer, &map);
308
309 return GST_FLOW_OK;
310 }
311