• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 [SRT](http://www.srtalliance.org/)
27  * packets to the network.
28  *
29  * ## Examples</title>
30  *
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  *
39  */
40 
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44 
45 #include "gstsrtelements.h"
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   SIG_CALLER_REJECTED,
61   SIG_CALLER_CONNECTING,
62   LAST_SIGNAL
63 };
64 
65 static guint signals[LAST_SIGNAL] = { 0 };
66 
67 static void gst_srt_sink_uri_handler_init (gpointer g_iface,
68     gpointer iface_data);
69 static gchar *gst_srt_sink_uri_get_uri (GstURIHandler * handler);
70 static gboolean gst_srt_sink_uri_set_uri (GstURIHandler * handler,
71     const gchar * uri, GError ** error);
72 static gboolean default_caller_connecting (GstSRTSink * self,
73     GSocketAddress * addr, const gchar * username, gpointer data);
74 static gboolean authentication_accumulator (GSignalInvocationHint * ihint,
75     GValue * return_accu, const GValue * handler_return, gpointer data);
76 
77 #define gst_srt_sink_parent_class parent_class
78 G_DEFINE_TYPE_WITH_CODE (GstSRTSink, gst_srt_sink,
79     GST_TYPE_BASE_SINK,
80     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_srt_sink_uri_handler_init)
81     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "srtsink", 0, "SRT Sink"));
82 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (srtsink, "srtsink", GST_RANK_PRIMARY,
83     GST_TYPE_SRT_SINK, srt_element_init (plugin));
84 
85 static gboolean
default_caller_connecting(GstSRTSink * self,GSocketAddress * addr,const gchar * stream_id,gpointer data)86 default_caller_connecting (GstSRTSink * self,
87     GSocketAddress * addr, const gchar * stream_id, gpointer data)
88 {
89   /* Accept all connections. */
90   return TRUE;
91 }
92 
93 static gboolean
authentication_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer data)94 authentication_accumulator (GSignalInvocationHint * ihint,
95     GValue * return_accu, const GValue * handler_return, gpointer data)
96 {
97   gboolean ret = g_value_get_boolean (handler_return);
98   /* Handlers return TRUE on authentication success and we want to stop on
99    * the first failure. */
100   g_value_set_boolean (return_accu, ret);
101   return ret;
102 }
103 
104 static void
gst_srt_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)105 gst_srt_sink_set_property (GObject * object,
106     guint prop_id, const GValue * value, GParamSpec * pspec)
107 {
108   GstSRTSink *self = GST_SRT_SINK (object);
109 
110   if (!gst_srt_object_set_property_helper (self->srtobject, prop_id, value,
111           pspec)) {
112     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113   }
114 }
115 
116 static void
gst_srt_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)117 gst_srt_sink_get_property (GObject * object,
118     guint prop_id, GValue * value, GParamSpec * pspec)
119 {
120   GstSRTSink *self = GST_SRT_SINK (object);
121 
122   if (!gst_srt_object_get_property_helper (self->srtobject, prop_id, value,
123           pspec)) {
124     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125   }
126 }
127 
128 static void
gst_srt_sink_finalize(GObject * object)129 gst_srt_sink_finalize (GObject * object)
130 {
131   GstSRTSink *self = GST_SRT_SINK (object);
132 
133   g_clear_object (&self->cancellable);
134   gst_srt_object_destroy (self->srtobject);
135 
136   G_OBJECT_CLASS (parent_class)->finalize (object);
137 }
138 
139 static void
gst_srt_sink_init(GstSRTSink * self)140 gst_srt_sink_init (GstSRTSink * self)
141 {
142   self->srtobject = gst_srt_object_new (GST_ELEMENT (self));
143   self->cancellable = g_cancellable_new ();
144 
145   gst_srt_object_set_uri (self->srtobject, GST_SRT_DEFAULT_URI, NULL);
146 }
147 
148 static gboolean
gst_srt_sink_start(GstBaseSink * bsink)149 gst_srt_sink_start (GstBaseSink * bsink)
150 {
151   GstSRTSink *self = GST_SRT_SINK (bsink);
152 
153   GError *error = NULL;
154   gboolean ret = FALSE;
155 
156   ret = gst_srt_object_open (self->srtobject, self->cancellable, &error);
157 
158   if (!ret) {
159     /* ensure error is posted since state change will fail */
160     GST_ELEMENT_ERROR (self, RESOURCE, OPEN_WRITE, (NULL),
161         ("Failed to open SRT: %s", error->message));
162     g_clear_error (&error);
163   }
164 
165   return ret;
166 }
167 
168 static gboolean
gst_srt_sink_stop(GstBaseSink * bsink)169 gst_srt_sink_stop (GstBaseSink * bsink)
170 {
171   GstSRTSink *self = GST_SRT_SINK (bsink);
172 
173   g_clear_pointer (&self->headers, gst_buffer_list_unref);
174   gst_srt_object_close (self->srtobject);
175 
176   return TRUE;
177 }
178 
179 static GstFlowReturn
gst_srt_sink_render(GstBaseSink * sink,GstBuffer * buffer)180 gst_srt_sink_render (GstBaseSink * sink, GstBuffer * buffer)
181 {
182   GstSRTSink *self = GST_SRT_SINK (sink);
183   GstFlowReturn ret = GST_FLOW_OK;
184   GstMapInfo info;
185   GError *error = NULL;
186 
187   if (g_cancellable_is_cancelled (self->cancellable)) {
188     ret = GST_FLOW_FLUSHING;
189   }
190 
191   if (self->headers && GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_HEADER)) {
192     GST_DEBUG_OBJECT (self, "Have streamheaders,"
193         " ignoring header %" GST_PTR_FORMAT, buffer);
194     return GST_FLOW_OK;
195   }
196 
197   if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) {
198     GST_ELEMENT_ERROR (self, RESOURCE, READ,
199         ("Could not map the input stream"), (NULL));
200     return GST_FLOW_ERROR;
201   }
202 
203   if (gst_srt_object_write (self->srtobject, self->headers, &info,
204           self->cancellable, &error) < 0) {
205     GST_ELEMENT_ERROR (self, RESOURCE, WRITE,
206         ("Failed to write to SRT socket: %s",
207             error ? error->message : "Unknown error"), (NULL));
208     g_clear_error (&error);
209     ret = GST_FLOW_ERROR;
210   }
211 
212   gst_buffer_unmap (buffer, &info);
213 
214   GST_TRACE_OBJECT (self, "sending buffer %p, offset %"
215       G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT
216       ", timestamp %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
217       ", size %" G_GSIZE_FORMAT,
218       buffer, GST_BUFFER_OFFSET (buffer),
219       GST_BUFFER_OFFSET_END (buffer),
220       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
221       GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
222       gst_buffer_get_size (buffer));
223 
224   return ret;
225 }
226 
227 static gboolean
gst_srt_sink_unlock(GstBaseSink * bsink)228 gst_srt_sink_unlock (GstBaseSink * bsink)
229 {
230   GstSRTSink *self = GST_SRT_SINK (bsink);
231 
232   gst_srt_object_wakeup (self->srtobject, self->cancellable);
233 
234   return TRUE;
235 }
236 
237 static gboolean
gst_srt_sink_unlock_stop(GstBaseSink * bsink)238 gst_srt_sink_unlock_stop (GstBaseSink * bsink)
239 {
240   GstSRTSink *self = GST_SRT_SINK (bsink);
241 
242   g_cancellable_reset (self->cancellable);
243 
244   return TRUE;
245 }
246 
247 static gboolean
gst_srt_sink_set_caps(GstBaseSink * bsink,GstCaps * caps)248 gst_srt_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
249 {
250   GstSRTSink *self = GST_SRT_SINK (bsink);
251   GstStructure *s;
252   const GValue *streamheader;
253 
254   GST_DEBUG_OBJECT (self, "setcaps %" GST_PTR_FORMAT, caps);
255 
256   g_clear_pointer (&self->headers, gst_buffer_list_unref);
257 
258   s = gst_caps_get_structure (caps, 0);
259   streamheader = gst_structure_get_value (s, "streamheader");
260 
261   if (!streamheader) {
262     GST_DEBUG_OBJECT (self, "'streamheader' field not present");
263   } else if (GST_VALUE_HOLDS_BUFFER (streamheader)) {
264     GST_DEBUG_OBJECT (self, "'streamheader' field holds buffer");
265     self->headers = gst_buffer_list_new_sized (1);
266     gst_buffer_list_add (self->headers, g_value_dup_boxed (streamheader));
267   } else if (GST_VALUE_HOLDS_ARRAY (streamheader)) {
268     guint i, size;
269 
270     GST_DEBUG_OBJECT (self, "'streamheader' field holds array");
271 
272     size = gst_value_array_get_size (streamheader);
273     self->headers = gst_buffer_list_new_sized (size);
274 
275     for (i = 0; i < size; i++) {
276       const GValue *v = gst_value_array_get_value (streamheader, i);
277       if (!GST_VALUE_HOLDS_BUFFER (v)) {
278         GST_ERROR_OBJECT (self, "'streamheader' item of unexpected type '%s'",
279             G_VALUE_TYPE_NAME (v));
280         return FALSE;
281       }
282 
283       gst_buffer_list_add (self->headers, g_value_dup_boxed (v));
284     }
285   } else {
286     GST_ERROR_OBJECT (self, "'streamheader' field has unexpected type '%s'",
287         G_VALUE_TYPE_NAME (streamheader));
288     return FALSE;
289   }
290 
291   GST_DEBUG_OBJECT (self, "Collected streamheaders: %u buffers",
292       self->headers ? gst_buffer_list_length (self->headers) : 0);
293 
294   return TRUE;
295 }
296 
297 static void
gst_srt_sink_class_init(GstSRTSinkClass * klass)298 gst_srt_sink_class_init (GstSRTSinkClass * klass)
299 {
300   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
301   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
302   GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
303 
304   gobject_class->set_property = gst_srt_sink_set_property;
305   gobject_class->get_property = gst_srt_sink_get_property;
306   gobject_class->finalize = gst_srt_sink_finalize;
307   klass->caller_connecting = default_caller_connecting;
308 
309   /**
310    * GstSRTSink::caller-added:
311    * @gstsrtsink: the srtsink element that emitted this signal
312    * @unused: always zero (for ABI compatibility with previous versions)
313    * @addr: the #GSocketAddress of the new caller
314    *
315    * A new caller has connected to @gstsrtsink.
316    */
317   signals[SIG_CALLER_ADDED] =
318       g_signal_new ("caller-added", G_TYPE_FROM_CLASS (klass),
319       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSRTSinkClass, caller_added),
320       NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_SOCKET_ADDRESS);
321 
322   /**
323    * GstSRTSink::caller-removed:
324    * @gstsrtsink: the srtsink element that emitted this signal
325    * @unused: always zero (for ABI compatibility with previous versions)
326    * @addr: the #GSocketAddress of the caller
327    *
328    * The given caller has disconnected.
329    */
330   signals[SIG_CALLER_REMOVED] =
331       g_signal_new ("caller-removed", G_TYPE_FROM_CLASS (klass),
332       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSRTSinkClass,
333           caller_added), NULL, NULL, NULL, G_TYPE_NONE,
334       2, G_TYPE_INT, G_TYPE_SOCKET_ADDRESS);
335 
336   /**
337    * GstSRTSink::caller-rejected:
338    * @gstsrtsink: the srtsink element that emitted this signal
339    * @addr: the #GSocketAddress that describes the client socket
340    * @stream_id: the stream Id to which the caller wants to connect
341    *
342    * A caller's connection to srtsink in listener mode has been rejected.
343    *
344    * Since: 1.20
345    *
346    */
347   signals[SIG_CALLER_REJECTED] =
348       g_signal_new ("caller-rejected", G_TYPE_FROM_CLASS (klass),
349       G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE,
350       2, G_TYPE_SOCKET_ADDRESS, G_TYPE_STRING);
351 
352   /**
353    * GstSRTSink::caller-connecting:
354    * @gstsrtsink: the srtsink element that emitted this signal
355    * @addr: the #GSocketAddress that describes the client socket
356    * @stream_id: the stream Id to which the caller wants to connect
357    *
358    * Whether to accept or reject a caller's connection to srtsink in listener mode.
359    * The Caller's connection is rejected if the callback returns FALSE, else
360    * the connection is accepeted.
361    *
362    * Since: 1.20
363    *
364    */
365   signals[SIG_CALLER_CONNECTING] =
366       g_signal_new ("caller-connecting", G_TYPE_FROM_CLASS (klass),
367       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSRTSinkClass, caller_connecting),
368       authentication_accumulator, NULL, NULL, G_TYPE_BOOLEAN,
369       2, G_TYPE_SOCKET_ADDRESS, G_TYPE_STRING);
370 
371   gst_srt_object_install_properties_helper (gobject_class);
372 
373   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
374   gst_element_class_set_metadata (gstelement_class,
375       "SRT sink", "Sink/Network",
376       "Send data over the network via SRT",
377       "Justin Kim <justin.joy.9to5@gmail.com>");
378 
379   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_srt_sink_start);
380   gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_srt_sink_stop);
381   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_srt_sink_render);
382   gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_srt_sink_unlock);
383   gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_srt_sink_unlock_stop);
384   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_srt_sink_set_caps);
385 
386 }
387 
388 static GstURIType
gst_srt_sink_uri_get_type(GType type)389 gst_srt_sink_uri_get_type (GType type)
390 {
391   return GST_URI_SINK;
392 }
393 
394 static const gchar *const *
gst_srt_sink_uri_get_protocols(GType type)395 gst_srt_sink_uri_get_protocols (GType type)
396 {
397   static const gchar *protocols[] = { GST_SRT_DEFAULT_URI_SCHEME, NULL };
398 
399   return protocols;
400 }
401 
402 static gchar *
gst_srt_sink_uri_get_uri(GstURIHandler * handler)403 gst_srt_sink_uri_get_uri (GstURIHandler * handler)
404 {
405   gchar *uri_str;
406   GstSRTSink *self = GST_SRT_SINK (handler);
407 
408   GST_OBJECT_LOCK (self);
409   uri_str = gst_uri_to_string (self->srtobject->uri);
410   GST_OBJECT_UNLOCK (self);
411 
412   return uri_str;
413 }
414 
415 static gboolean
gst_srt_sink_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)416 gst_srt_sink_uri_set_uri (GstURIHandler * handler,
417     const gchar * uri, GError ** error)
418 {
419   GstSRTSink *self = GST_SRT_SINK (handler);
420   gboolean ret;
421 
422   GST_OBJECT_LOCK (self);
423   ret = gst_srt_object_set_uri (self->srtobject, uri, error);
424   GST_OBJECT_UNLOCK (self);
425 
426   return ret;
427 }
428 
429 static void
gst_srt_sink_uri_handler_init(gpointer g_iface,gpointer iface_data)430 gst_srt_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
431 {
432   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
433 
434   iface->get_type = gst_srt_sink_uri_get_type;
435   iface->get_protocols = gst_srt_sink_uri_get_protocols;
436   iface->get_uri = gst_srt_sink_uri_get_uri;
437   iface->set_uri = gst_srt_sink_uri_set_uri;
438 }
439