1 /* GStreamer
2 * Copyright (C) 2018, Collabora Ltd.
3 * Copyright (C) 2018, SK Telecom, Co., Ltd.
4 * Author: Jeongseok Kim <jeongseok.kim@sk.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later 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 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /**
23 * SECTION:element-srtsink
24 * @title: srtsink
25 *
26 * srtsink is a network sink that sends <ulink url="http://www.srtalliance.org/">SRT</ulink>
27 * packets to the network.
28 *
29 * <refsect2>
30 * <title>Examples</title>
31 * |[
32 * gst-launch-1.0 -v audiotestsrc ! srtsink uri=srt://host
33 * ]| This pipeline shows how to serve SRT packets through the default port.
34 *
35 * |[
36 * gst-launch-1.0 -v audiotestsrc ! srtsink uri=srt://:port
37 * ]| This pipeline shows how to wait SRT callers.
38 * </refsect2>
39 *
40 */
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #include "gstsrtsink.h"
47
48 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
49 GST_PAD_SINK,
50 GST_PAD_ALWAYS,
51 GST_STATIC_CAPS_ANY);
52
53 #define GST_CAT_DEFAULT gst_debug_srt_sink
54 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
55
56 enum
57 {
58 SIG_CALLER_ADDED,
59 SIG_CALLER_REMOVED,
60
61 LAST_SIGNAL
62 };
63
64 static guint signals[LAST_SIGNAL] = { 0 };
65
66 static void gst_srt_sink_uri_handler_init (gpointer g_iface,
67 gpointer iface_data);
68 static gchar *gst_srt_sink_uri_get_uri (GstURIHandler * handler);
69 static gboolean gst_srt_sink_uri_set_uri (GstURIHandler * handler,
70 const gchar * uri, GError ** error);
71
72 #define gst_srt_sink_parent_class parent_class
73 G_DEFINE_TYPE_WITH_CODE (GstSRTSink, gst_srt_sink,
74 GST_TYPE_BASE_SINK,
75 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_srt_sink_uri_handler_init)
76 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "srtsink", 0, "SRT Sink"));
77
78 static void
gst_srt_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)79 gst_srt_sink_set_property (GObject * object,
80 guint prop_id, const GValue * value, GParamSpec * pspec)
81 {
82 GstSRTSink *self = GST_SRT_SINK (object);
83
84 if (!gst_srt_object_set_property_helper (self->srtobject, prop_id, value,
85 pspec)) {
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87 }
88 }
89
90 static void
gst_srt_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)91 gst_srt_sink_get_property (GObject * object,
92 guint prop_id, GValue * value, GParamSpec * pspec)
93 {
94 GstSRTSink *self = GST_SRT_SINK (object);
95
96 if (!gst_srt_object_get_property_helper (self->srtobject, prop_id, value,
97 pspec)) {
98 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
99 }
100 }
101
102 static void
gst_srt_sink_finalize(GObject * object)103 gst_srt_sink_finalize (GObject * object)
104 {
105 GstSRTSink *self = GST_SRT_SINK (object);
106
107 g_clear_object (&self->cancellable);
108 gst_srt_object_destroy (self->srtobject);
109
110 G_OBJECT_CLASS (parent_class)->finalize (object);
111 }
112
113 static void
gst_srt_sink_init(GstSRTSink * self)114 gst_srt_sink_init (GstSRTSink * self)
115 {
116 self->srtobject = gst_srt_object_new (GST_ELEMENT (self));
117 self->cancellable = g_cancellable_new ();
118
119 gst_srt_object_set_uri (self->srtobject, GST_SRT_DEFAULT_URI, NULL);
120 }
121
122 static void
gst_srt_sink_caller_added_cb(int sock,GSocketAddress * addr,GstSRTObject * srtobject)123 gst_srt_sink_caller_added_cb (int sock, GSocketAddress * addr,
124 GstSRTObject * srtobject)
125 {
126 g_signal_emit (srtobject->element, signals[SIG_CALLER_ADDED], 0, sock, addr);
127 }
128
129 static void
gst_srt_sink_caller_removed_cb(int sock,GSocketAddress * addr,GstSRTObject * srtobject)130 gst_srt_sink_caller_removed_cb (int sock, GSocketAddress * addr,
131 GstSRTObject * srtobject)
132 {
133 g_signal_emit (srtobject->element, signals[SIG_CALLER_REMOVED], 0, sock,
134 addr);
135 }
136
137 static gboolean
gst_srt_sink_start(GstBaseSink * bsink)138 gst_srt_sink_start (GstBaseSink * bsink)
139 {
140 GstSRTSink *self = GST_SRT_SINK (bsink);
141 GstSRTConnectionMode connection_mode = GST_SRT_CONNECTION_MODE_NONE;
142
143 GError *error = NULL;
144 gboolean ret = FALSE;
145
146 gst_structure_get_enum (self->srtobject->parameters, "mode",
147 GST_TYPE_SRT_CONNECTION_MODE, (gint *) & connection_mode);
148
149 if (connection_mode == GST_SRT_CONNECTION_MODE_LISTENER) {
150 ret =
151 gst_srt_object_open_full (self->srtobject, gst_srt_sink_caller_added_cb,
152 gst_srt_sink_caller_removed_cb, self->cancellable, &error);
153 } else {
154 ret = gst_srt_object_open (self->srtobject, self->cancellable, &error);
155 }
156
157 if (!ret) {
158 /* ensure error is posted since state change will fail */
159 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_WRITE, (NULL),
160 ("Failed to open SRT: %s", error->message));
161 g_clear_error (&error);
162 }
163
164 return ret;
165 }
166
167 static gboolean
gst_srt_sink_stop(GstBaseSink * bsink)168 gst_srt_sink_stop (GstBaseSink * bsink)
169 {
170 GstSRTSink *self = GST_SRT_SINK (bsink);
171
172 gst_srt_object_close (self->srtobject);
173
174 return TRUE;
175 }
176
177 static GstFlowReturn
gst_srt_sink_render(GstBaseSink * sink,GstBuffer * buffer)178 gst_srt_sink_render (GstBaseSink * sink, GstBuffer * buffer)
179 {
180 GstSRTSink *self = GST_SRT_SINK (sink);
181 GstFlowReturn ret = GST_FLOW_OK;
182 GstMapInfo info;
183 GError *error = NULL;
184
185 if (g_cancellable_is_cancelled (self->cancellable)) {
186 ret = GST_FLOW_FLUSHING;
187 }
188
189 if (self->headers && GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_HEADER)) {
190 GST_DEBUG_OBJECT (self, "Have streamheaders,"
191 " ignoring header %" GST_PTR_FORMAT, buffer);
192 return GST_FLOW_OK;
193 }
194
195 if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) {
196 GST_ELEMENT_ERROR (self, RESOURCE, READ,
197 ("Could not map the input stream"), (NULL));
198 return GST_FLOW_ERROR;
199 }
200
201 if (gst_srt_object_write (self->srtobject, self->headers, &info,
202 self->cancellable, &error) < 0) {
203 ret = GST_FLOW_ERROR;
204 }
205
206 gst_buffer_unmap (buffer, &info);
207
208 GST_TRACE_OBJECT (self, "sending buffer %p, offset %"
209 G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT
210 ", timestamp %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
211 ", size %" G_GSIZE_FORMAT,
212 buffer, GST_BUFFER_OFFSET (buffer),
213 GST_BUFFER_OFFSET_END (buffer),
214 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
215 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
216 gst_buffer_get_size (buffer));
217
218 return ret;
219 }
220
221 static gboolean
gst_srt_sink_unlock(GstBaseSink * bsink)222 gst_srt_sink_unlock (GstBaseSink * bsink)
223 {
224 GstSRTSink *self = GST_SRT_SINK (bsink);
225
226 gst_srt_object_wakeup (self->srtobject, self->cancellable);
227
228 return TRUE;
229 }
230
231 static gboolean
gst_srt_sink_unlock_stop(GstBaseSink * bsink)232 gst_srt_sink_unlock_stop (GstBaseSink * bsink)
233 {
234 GstSRTSink *self = GST_SRT_SINK (bsink);
235
236 g_cancellable_reset (self->cancellable);
237
238 return TRUE;
239 }
240
241 static gboolean
gst_srt_sink_set_caps(GstBaseSink * bsink,GstCaps * caps)242 gst_srt_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
243 {
244 GstSRTSink *self = GST_SRT_SINK (bsink);
245 GstStructure *s;
246 const GValue *streamheader;
247
248 GST_DEBUG_OBJECT (self, "setcaps %" GST_PTR_FORMAT, caps);
249
250 g_clear_pointer (&self->headers, gst_buffer_list_unref);
251
252 s = gst_caps_get_structure (caps, 0);
253 streamheader = gst_structure_get_value (s, "streamheader");
254
255 if (!streamheader) {
256 GST_DEBUG_OBJECT (self, "'streamheader' field not present");
257 } else if (GST_VALUE_HOLDS_BUFFER (streamheader)) {
258 GST_DEBUG_OBJECT (self, "'streamheader' field holds buffer");
259 self->headers = gst_buffer_list_new_sized (1);
260 gst_buffer_list_add (self->headers, g_value_dup_boxed (streamheader));
261 } else if (GST_VALUE_HOLDS_ARRAY (streamheader)) {
262 guint i, size;
263
264 GST_DEBUG_OBJECT (self, "'streamheader' field holds array");
265
266 size = gst_value_array_get_size (streamheader);
267 self->headers = gst_buffer_list_new_sized (size);
268
269 for (i = 0; i < size; i++) {
270 const GValue *v = gst_value_array_get_value (streamheader, i);
271 if (!GST_VALUE_HOLDS_BUFFER (v)) {
272 GST_ERROR_OBJECT (self, "'streamheader' item of unexpected type '%s'",
273 G_VALUE_TYPE_NAME (v));
274 return FALSE;
275 }
276
277 gst_buffer_list_add (self->headers, g_value_dup_boxed (v));
278 }
279 } else {
280 GST_ERROR_OBJECT (self, "'streamheader' field has unexpected type '%s'",
281 G_VALUE_TYPE_NAME (streamheader));
282 return FALSE;
283 }
284
285 GST_DEBUG_OBJECT (self, "Collected streamheaders: %u buffers",
286 self->headers ? gst_buffer_list_length (self->headers) : 0);
287
288 return TRUE;
289 }
290
291 static void
gst_srt_sink_class_init(GstSRTSinkClass * klass)292 gst_srt_sink_class_init (GstSRTSinkClass * klass)
293 {
294 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
295 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
296 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
297
298 gobject_class->set_property = gst_srt_sink_set_property;
299 gobject_class->get_property = gst_srt_sink_get_property;
300 gobject_class->finalize = gst_srt_sink_finalize;
301
302 /**
303 * GstSRTSink::caller-added:
304 * @gstsrtsink: the srtsink element that emitted this signal
305 * @sock: the client socket descriptor that was added to srtsink
306 * @addr: the #GSocketAddress that describes the @sock
307 *
308 * The given socket descriptor was added to srtsink.
309 */
310 signals[SIG_CALLER_ADDED] =
311 g_signal_new ("caller-added", G_TYPE_FROM_CLASS (klass),
312 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSRTSinkClass, caller_added),
313 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
314 2, G_TYPE_INT, G_TYPE_SOCKET_ADDRESS);
315
316 /**
317 * GstSRTSink::caller-removed:
318 * @gstsrtsink: the srtsink element that emitted this signal
319 * @sock: the client socket descriptor that was added to srtsink
320 * @addr: the #GSocketAddress that describes the @sock
321 *
322 * The given socket descriptor was removed from srtsink.
323 */
324 signals[SIG_CALLER_REMOVED] =
325 g_signal_new ("caller-removed", G_TYPE_FROM_CLASS (klass),
326 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSRTSinkClass,
327 caller_added), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
328 2, G_TYPE_INT, G_TYPE_SOCKET_ADDRESS);
329
330 gst_srt_object_install_properties_helper (gobject_class);
331
332 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
333 gst_element_class_set_metadata (gstelement_class,
334 "SRT sink", "Sink/Network",
335 "Send data over the network via SRT",
336 "Justin Kim <justin.joy.9to5@gmail.com>");
337
338 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_srt_sink_start);
339 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_srt_sink_stop);
340 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_srt_sink_render);
341 gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_srt_sink_unlock);
342 gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_srt_sink_unlock_stop);
343 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_srt_sink_set_caps);
344
345 }
346
347 static GstURIType
gst_srt_sink_uri_get_type(GType type)348 gst_srt_sink_uri_get_type (GType type)
349 {
350 return GST_URI_SINK;
351 }
352
353 static const gchar *const *
gst_srt_sink_uri_get_protocols(GType type)354 gst_srt_sink_uri_get_protocols (GType type)
355 {
356 static const gchar *protocols[] = { GST_SRT_DEFAULT_URI_SCHEME, NULL };
357
358 return protocols;
359 }
360
361 static gchar *
gst_srt_sink_uri_get_uri(GstURIHandler * handler)362 gst_srt_sink_uri_get_uri (GstURIHandler * handler)
363 {
364 gchar *uri_str;
365 GstSRTSink *self = GST_SRT_SINK (handler);
366
367 GST_OBJECT_LOCK (self);
368 uri_str = gst_uri_to_string (self->srtobject->uri);
369 GST_OBJECT_UNLOCK (self);
370
371 return uri_str;
372 }
373
374 static gboolean
gst_srt_sink_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)375 gst_srt_sink_uri_set_uri (GstURIHandler * handler,
376 const gchar * uri, GError ** error)
377 {
378 GstSRTSink *self = GST_SRT_SINK (handler);
379
380 return gst_srt_object_set_uri (self->srtobject, uri, error);
381 }
382
383 static void
gst_srt_sink_uri_handler_init(gpointer g_iface,gpointer iface_data)384 gst_srt_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
385 {
386 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
387
388 iface->get_type = gst_srt_sink_uri_get_type;
389 iface->get_protocols = gst_srt_sink_uri_get_protocols;
390 iface->get_uri = gst_srt_sink_uri_get_uri;
391 iface->set_uri = gst_srt_sink_uri_set_uri;
392 }
393