• 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-srtsrc
24  * @title: srtsrc
25  *
26  * srtsrc is a network source that reads [SRT](http://www.srtalliance.org/)
27  * packets from the network.
28  *
29  * ## Examples
30  * |[
31  * gst-launch-1.0 -v srtsrc uri="srt://127.0.0.1:7001" ! fakesink
32  * ]| This pipeline shows how to connect SRT server by setting #GstSRTSrc:uri property.
33  *
34  * |[
35  * gst-launch-1.0 -v srtsrc uri="srt://:7001?mode=listener" ! fakesink
36  * ]| This pipeline shows how to wait SRT connection by setting #GstSRTSrc:uri property.
37  *
38  * |[
39  * gst-launch-1.0 -v srtclientsrc uri="srt://192.168.1.10:7001?mode=rendez-vous" ! fakesink
40  * ]| This pipeline shows how to connect SRT server by setting #GstSRTSrc:uri property and using the rendez-vous mode.
41  *
42  */
43 
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47 
48 #include "gstsrtelements.h"
49 #include "gstsrtsrc.h"
50 
51 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS_ANY);
55 
56 #define GST_CAT_DEFAULT gst_debug_srt_src
57 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
58 
59 enum
60 {
61   SIG_CALLER_ADDED,
62   SIG_CALLER_REMOVED,
63   SIG_CALLER_REJECTED,
64   SIG_CALLER_CONNECTING,
65   LAST_SIGNAL
66 };
67 
68 static guint signals[LAST_SIGNAL] = { 0 };
69 
70 static void gst_srt_src_uri_handler_init (gpointer g_iface,
71     gpointer iface_data);
72 static gchar *gst_srt_src_uri_get_uri (GstURIHandler * handler);
73 static gboolean gst_srt_src_uri_set_uri (GstURIHandler * handler,
74     const gchar * uri, GError ** error);
75 static gboolean src_default_caller_connecting (GstSRTSrc * self,
76     GSocketAddress * addr, const gchar * username, gpointer data);
77 static gboolean src_authentication_accumulator (GSignalInvocationHint * ihint,
78     GValue * return_accu, const GValue * handler_return, gpointer data);
79 
80 #define gst_srt_src_parent_class parent_class
81 G_DEFINE_TYPE_WITH_CODE (GstSRTSrc, gst_srt_src,
82     GST_TYPE_PUSH_SRC,
83     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_srt_src_uri_handler_init)
84     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "srtsrc", 0, "SRT Source"));
85 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (srtsrc, "srtsrc", GST_RANK_PRIMARY,
86     GST_TYPE_SRT_SRC, srt_element_init (plugin));
87 
88 static gboolean
src_default_caller_connecting(GstSRTSrc * self,GSocketAddress * addr,const gchar * stream_id,gpointer data)89 src_default_caller_connecting (GstSRTSrc * self,
90     GSocketAddress * addr, const gchar * stream_id, gpointer data)
91 {
92   /* Accept all connections. */
93   return TRUE;
94 }
95 
96 static gboolean
src_authentication_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer data)97 src_authentication_accumulator (GSignalInvocationHint * ihint,
98     GValue * return_accu, const GValue * handler_return, gpointer data)
99 {
100   gboolean ret = g_value_get_boolean (handler_return);
101   /* Handlers return TRUE on authentication success and we want to stop on
102    * the first failure. */
103   g_value_set_boolean (return_accu, ret);
104   return ret;
105 }
106 
107 static gboolean
gst_srt_src_start(GstBaseSrc * bsrc)108 gst_srt_src_start (GstBaseSrc * bsrc)
109 {
110   GstSRTSrc *self = GST_SRT_SRC (bsrc);
111   GError *error = NULL;
112   gboolean ret = FALSE;
113   GstSRTConnectionMode connection_mode = GST_SRT_CONNECTION_MODE_NONE;
114 
115   gst_structure_get_enum (self->srtobject->parameters, "mode",
116       GST_TYPE_SRT_CONNECTION_MODE, (gint *) & connection_mode);
117 
118   ret = gst_srt_object_open (self->srtobject, self->cancellable, &error);
119 
120   if (!ret) {
121     /* ensure error is posted since state change will fail */
122     GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL),
123         ("Failed to open SRT: %s", error->message));
124     g_clear_error (&error);
125   }
126 
127   /* Reset expected pktseq */
128   self->next_pktseq = 0;
129 
130   return ret;
131 }
132 
133 static gboolean
gst_srt_src_stop(GstBaseSrc * bsrc)134 gst_srt_src_stop (GstBaseSrc * bsrc)
135 {
136   GstSRTSrc *self = GST_SRT_SRC (bsrc);
137 
138   gst_srt_object_close (self->srtobject);
139 
140   return TRUE;
141 }
142 
143 static GstFlowReturn
gst_srt_src_fill(GstPushSrc * src,GstBuffer * outbuf)144 gst_srt_src_fill (GstPushSrc * src, GstBuffer * outbuf)
145 {
146   GstSRTSrc *self = GST_SRT_SRC (src);
147   GstFlowReturn ret = GST_FLOW_OK;
148   GstMapInfo info;
149   GError *err = NULL;
150   gssize recv_len;
151   GstClock *clock;
152   GstClockTime base_time;
153   GstClockTime capture_time;
154   GstClockTimeDiff delay;
155   int64_t srt_time;
156   SRT_MSGCTRL mctrl;
157 
158   if (g_cancellable_is_cancelled (self->cancellable)) {
159     ret = GST_FLOW_FLUSHING;
160   }
161 
162   if (!gst_buffer_map (outbuf, &info, GST_MAP_WRITE)) {
163     GST_ELEMENT_ERROR (src, RESOURCE, READ,
164         ("Could not map the buffer for writing "), (NULL));
165     ret = GST_FLOW_ERROR;
166     goto out;
167   }
168 
169   /* Get clock and values */
170   clock = gst_element_get_clock (GST_ELEMENT (src));
171   if (!clock) {
172     GST_DEBUG_OBJECT (src, "Clock missing, flushing");
173     return GST_FLOW_FLUSHING;
174   }
175 
176   base_time = gst_element_get_base_time (GST_ELEMENT (src));
177 
178   recv_len = gst_srt_object_read (self->srtobject, info.data,
179       gst_buffer_get_size (outbuf), self->cancellable, &err, &mctrl);
180 
181   /* Capture clock values ASAP */
182   capture_time = gst_clock_get_time (clock);
183 #if SRT_VERSION_VALUE >= 0x10402
184   /* Use SRT clock value if available (SRT > 1.4.2) */
185   srt_time = srt_time_now ();
186 #else
187   /* Else use the unix epoch monotonic clock */
188   srt_time = g_get_real_time ();
189 #endif
190   gst_object_unref (clock);
191 
192   gst_buffer_unmap (outbuf, &info);
193 
194   GST_LOG_OBJECT (src,
195       "recv_len:%" G_GSIZE_FORMAT " pktseq:%d msgno:%d srctime:%"
196       G_GINT64_FORMAT, recv_len, mctrl.pktseq, mctrl.msgno, mctrl.srctime);
197 
198   if (g_cancellable_is_cancelled (self->cancellable)) {
199     ret = GST_FLOW_FLUSHING;
200     goto out;
201   }
202 
203   if (recv_len < 0) {
204     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), ("%s", err->message));
205     ret = GST_FLOW_ERROR;
206     g_clear_error (&err);
207     goto out;
208   } else if (recv_len == 0) {
209     ret = GST_FLOW_EOS;
210     goto out;
211   }
212 
213   /* Detect discontinuities */
214   if (mctrl.pktseq != self->next_pktseq) {
215     GST_WARNING_OBJECT (src, "discont detected %d (expected: %d)",
216         mctrl.pktseq, self->next_pktseq);
217     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
218   }
219   /* pktseq is a 31bit field */
220   self->next_pktseq = (mctrl.pktseq + 1) % G_MAXINT32;
221 
222   /* 0 means we do not have a srctime */
223   if (mctrl.srctime != 0)
224     delay = (srt_time - mctrl.srctime) * GST_USECOND;
225   else
226     delay = 0;
227 
228   GST_LOG_OBJECT (src, "delay: %" GST_STIME_FORMAT, GST_STIME_ARGS (delay));
229 
230   if (delay < 0) {
231     GST_WARNING_OBJECT (src,
232         "Calculated SRT delay %" GST_STIME_FORMAT " is negative, clamping to 0",
233         GST_STIME_ARGS (delay));
234     delay = 0;
235   }
236 
237   /* Subtract the base_time (since the pipeline started) ... */
238   if (capture_time > base_time)
239     capture_time -= base_time;
240   else
241     capture_time = 0;
242   /* And adjust by the delay */
243   if (capture_time > delay)
244     capture_time -= delay;
245   else
246     capture_time = 0;
247   GST_BUFFER_TIMESTAMP (outbuf) = capture_time;
248 
249   gst_buffer_resize (outbuf, 0, recv_len);
250 
251   GST_LOG_OBJECT (src,
252       "filled buffer from _get of size %" G_GSIZE_FORMAT ", ts %"
253       GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
254       ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
255       gst_buffer_get_size (outbuf),
256       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
257       GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)),
258       GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf));
259 
260 out:
261   return ret;
262 }
263 
264 static void
gst_srt_src_init(GstSRTSrc * self)265 gst_srt_src_init (GstSRTSrc * self)
266 {
267   self->srtobject = gst_srt_object_new (GST_ELEMENT (self));
268   self->cancellable = g_cancellable_new ();
269 
270   gst_base_src_set_format (GST_BASE_SRC (self), GST_FORMAT_TIME);
271   gst_base_src_set_live (GST_BASE_SRC (self), TRUE);
272   /* We do the timing ourselves */
273   gst_base_src_set_do_timestamp (GST_BASE_SRC (self), FALSE);
274 
275   gst_srt_object_set_uri (self->srtobject, GST_SRT_DEFAULT_URI, NULL);
276 
277 }
278 
279 static void
gst_srt_src_finalize(GObject * object)280 gst_srt_src_finalize (GObject * object)
281 {
282   GstSRTSrc *self = GST_SRT_SRC (object);
283 
284   g_clear_object (&self->cancellable);
285   gst_srt_object_destroy (self->srtobject);
286 
287   G_OBJECT_CLASS (parent_class)->finalize (object);
288 }
289 
290 static gboolean
gst_srt_src_unlock(GstBaseSrc * bsrc)291 gst_srt_src_unlock (GstBaseSrc * bsrc)
292 {
293   GstSRTSrc *self = GST_SRT_SRC (bsrc);
294 
295   gst_srt_object_wakeup (self->srtobject, self->cancellable);
296 
297   return TRUE;
298 }
299 
300 static gboolean
gst_srt_src_unlock_stop(GstBaseSrc * bsrc)301 gst_srt_src_unlock_stop (GstBaseSrc * bsrc)
302 {
303   GstSRTSrc *self = GST_SRT_SRC (bsrc);
304 
305   g_cancellable_reset (self->cancellable);
306 
307   return TRUE;
308 }
309 
310 static void
gst_srt_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)311 gst_srt_src_set_property (GObject * object,
312     guint prop_id, const GValue * value, GParamSpec * pspec)
313 {
314   GstSRTSrc *self = GST_SRT_SRC (object);
315 
316   if (!gst_srt_object_set_property_helper (self->srtobject, prop_id, value,
317           pspec)) {
318     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
319   }
320 }
321 
322 static void
gst_srt_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)323 gst_srt_src_get_property (GObject * object,
324     guint prop_id, GValue * value, GParamSpec * pspec)
325 {
326   GstSRTSrc *self = GST_SRT_SRC (object);
327 
328   if (!gst_srt_object_get_property_helper (self->srtobject, prop_id, value,
329           pspec)) {
330     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331   }
332 }
333 
334 static gboolean
gst_srt_src_query(GstBaseSrc * basesrc,GstQuery * query)335 gst_srt_src_query (GstBaseSrc * basesrc, GstQuery * query)
336 {
337   GstSRTSrc *self = GST_SRT_SRC (basesrc);
338 
339   if (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY) {
340     gint latency;
341     if (!gst_structure_get_int (self->srtobject->parameters, "latency",
342             &latency))
343       latency = GST_SRT_DEFAULT_LATENCY;
344     gst_query_set_latency (query, TRUE, latency * GST_MSECOND,
345         latency * GST_MSECOND);
346     return TRUE;
347   } else {
348     return GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
349   }
350 }
351 
352 static void
gst_srt_src_class_init(GstSRTSrcClass * klass)353 gst_srt_src_class_init (GstSRTSrcClass * klass)
354 {
355   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
356   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
357   GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
358   GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
359 
360   gobject_class->set_property = gst_srt_src_set_property;
361   gobject_class->get_property = gst_srt_src_get_property;
362   gobject_class->finalize = gst_srt_src_finalize;
363   klass->caller_connecting = src_default_caller_connecting;
364 
365   /**
366    * GstSRTSrc::caller-added:
367    * @gstsrtsrc: the srtsrc element that emitted this signal
368    * @unused: always zero (for ABI compatibility with previous versions)
369    * @addr: the #GSocketAddress of the new caller
370    *
371    * A new caller has connected to srtsrc.
372    */
373   signals[SIG_CALLER_ADDED] =
374       g_signal_new ("caller-added", G_TYPE_FROM_CLASS (klass),
375       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSRTSrcClass, caller_added),
376       NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_SOCKET_ADDRESS);
377 
378   /**
379    * GstSRTSrc::caller-removed:
380    * @gstsrtsrc: the srtsrc element that emitted this signal
381    * @unused: always zero (for ABI compatibility with previous versions)
382    * @addr: the #GSocketAddress of the caller
383    *
384    * The given caller has disconnected.
385    */
386   signals[SIG_CALLER_REMOVED] =
387       g_signal_new ("caller-removed", G_TYPE_FROM_CLASS (klass),
388       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSRTSrcClass,
389           caller_added), NULL, NULL, NULL, G_TYPE_NONE,
390       2, G_TYPE_INT, G_TYPE_SOCKET_ADDRESS);
391 
392   /**
393    * GstSRTSrc::caller-rejected:
394    * @gstsrtsrc: the srtsrc element that emitted this signal
395    * @addr: the #GSocketAddress that describes the client socket
396    * @stream_id: the stream Id to which the caller wants to connect
397    *
398    * A caller's connection to srtsrc in listener mode has been rejected.
399    *
400    * Since: 1.20
401    *
402    */
403   signals[SIG_CALLER_REJECTED] =
404       g_signal_new ("caller-rejected", G_TYPE_FROM_CLASS (klass),
405       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSRTSrcClass, caller_rejected),
406       NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_SOCKET_ADDRESS, G_TYPE_STRING);
407 
408   /**
409    * GstSRTSrc::caller-connecting:
410    * @gstsrtsrc: the srtsrc element that emitted this signal
411    * @addr: the #GSocketAddress that describes the client socket
412    * @stream_id: the stream Id to which the caller wants to connect
413    *
414    * Whether to accept or reject a caller's connection to srtsrc in listener mode.
415    * The Caller's connection is rejected if the callback returns FALSE, else
416    * the connection is accepeted.
417    *
418    * Since: 1.20
419    *
420    */
421   signals[SIG_CALLER_CONNECTING] =
422       g_signal_new ("caller-connecting", G_TYPE_FROM_CLASS (klass),
423       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSRTSrcClass, caller_connecting),
424       src_authentication_accumulator, NULL, NULL, G_TYPE_BOOLEAN,
425       2, G_TYPE_SOCKET_ADDRESS, G_TYPE_STRING);
426 
427   gst_srt_object_install_properties_helper (gobject_class);
428 
429   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
430   gst_element_class_set_metadata (gstelement_class,
431       "SRT source", "Source/Network",
432       "Receive data over the network via SRT",
433       "Justin Kim <justin.joy.9to5@gmail.com>");
434 
435   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_srt_src_start);
436   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_srt_src_stop);
437   gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_srt_src_unlock);
438   gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_srt_src_unlock_stop);
439   gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_srt_src_query);
440 
441   gstpushsrc_class->fill = GST_DEBUG_FUNCPTR (gst_srt_src_fill);
442 }
443 
444 static GstURIType
gst_srt_src_uri_get_type(GType type)445 gst_srt_src_uri_get_type (GType type)
446 {
447   return GST_URI_SRC;
448 }
449 
450 static const gchar *const *
gst_srt_src_uri_get_protocols(GType type)451 gst_srt_src_uri_get_protocols (GType type)
452 {
453   static const gchar *protocols[] = { GST_SRT_DEFAULT_URI_SCHEME, NULL };
454 
455   return protocols;
456 }
457 
458 static gchar *
gst_srt_src_uri_get_uri(GstURIHandler * handler)459 gst_srt_src_uri_get_uri (GstURIHandler * handler)
460 {
461   gchar *uri_str;
462   GstSRTSrc *self = GST_SRT_SRC (handler);
463 
464   GST_OBJECT_LOCK (self);
465   uri_str = gst_uri_to_string (self->srtobject->uri);
466   GST_OBJECT_UNLOCK (self);
467 
468   return uri_str;
469 }
470 
471 static gboolean
gst_srt_src_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)472 gst_srt_src_uri_set_uri (GstURIHandler * handler,
473     const gchar * uri, GError ** error)
474 {
475   GstSRTSrc *self = GST_SRT_SRC (handler);
476   gboolean ret;
477 
478   GST_OBJECT_LOCK (self);
479   ret = gst_srt_object_set_uri (self->srtobject, uri, error);
480   GST_OBJECT_UNLOCK (self);
481 
482   return ret;
483 }
484 
485 static void
gst_srt_src_uri_handler_init(gpointer g_iface,gpointer iface_data)486 gst_srt_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
487 {
488   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
489 
490   iface->get_type = gst_srt_src_uri_get_type;
491   iface->get_protocols = gst_srt_src_uri_get_protocols;
492   iface->get_uri = gst_srt_src_uri_get_uri;
493   iface->set_uri = gst_srt_src_uri_set_uri;
494 }
495