2 * Copyright (C) 2018, Collabora Ltd.
3 * Copyright (C) 2018, SK Telecom, Co., Ltd.
4 * Author: Jeongseok Kim <jeongseok.kim@sk.com>
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.
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.
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.
23 * SECTION:element-srtsrc
26 * srtsrc is a network source that reads [SRT](http://www.srtalliance.org/)
27 * packets from the network.
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.
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.
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.
48 #include "gstsrtelements.h"
49 #include "gstsrtsrc.h"
51 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
56 #define GST_CAT_DEFAULT gst_debug_srt_src
57 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
64 SIG_CALLER_CONNECTING,
68 static guint signals[LAST_SIGNAL] = { 0 };
70 static void gst_srt_src_uri_handler_init (gpointer g_iface,
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);
80 #define gst_srt_src_parent_class parent_class
81 G_DEFINE_TYPE_WITH_CODE (GstSRTSrc, gst_srt_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));
89 src_default_caller_connecting (GstSRTSrc * self,
90 GSocketAddress * addr, const gchar * stream_id, gpointer data)
92 /* Accept all connections. */
97 src_authentication_accumulator (GSignalInvocationHint * ihint,
98 GValue * return_accu, const GValue * handler_return, gpointer data)
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);
108 gst_srt_src_start (GstBaseSrc * bsrc)
110 GstSRTSrc *self = GST_SRT_SRC (bsrc);
111 GError *error = NULL;
112 gboolean ret = FALSE;
113 GstSRTConnectionMode connection_mode = GST_SRT_CONNECTION_MODE_NONE;
115 gst_structure_get_enum (self->srtobject->parameters, "mode",
116 GST_TYPE_SRT_CONNECTION_MODE, (gint *) & connection_mode);
118 ret = gst_srt_object_open (self->srtobject, self->cancellable, &error);
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);
127 /* Reset expected pktseq */
128 self->next_pktseq = 0;
134 gst_srt_src_stop (GstBaseSrc * bsrc)
136 GstSRTSrc *self = GST_SRT_SRC (bsrc);
138 gst_srt_object_close (self->srtobject);
144 gst_srt_src_fill (GstPushSrc * src, GstBuffer * outbuf)
146 GstSRTSrc *self = GST_SRT_SRC (src);
147 GstFlowReturn ret = GST_FLOW_OK;
152 GstClockTime base_time;
153 GstClockTime capture_time;
154 GstClockTimeDiff delay;
158 if (g_cancellable_is_cancelled (self->cancellable)) {
159 ret = GST_FLOW_FLUSHING;
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;
169 /* Get clock and values */
170 clock = gst_element_get_clock (GST_ELEMENT (src));
172 GST_DEBUG_OBJECT (src, "Clock missing, flushing");
173 return GST_FLOW_FLUSHING;
176 base_time = gst_element_get_base_time (GST_ELEMENT (src));
178 recv_len = gst_srt_object_read (self->srtobject, info.data,
179 gst_buffer_get_size (outbuf), self->cancellable, &err, &mctrl);
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 ();
187 /* Else use the unix epoch monotonic clock */
188 srt_time = g_get_real_time ();
190 gst_object_unref (clock);
192 gst_buffer_unmap (outbuf, &info);
195 "recv_len:%" G_GSIZE_FORMAT " pktseq:%d msgno:%d srctime:%"
196 G_GINT64_FORMAT, recv_len, mctrl.pktseq, mctrl.msgno, mctrl.srctime);
198 if (g_cancellable_is_cancelled (self->cancellable)) {
199 ret = GST_FLOW_FLUSHING;
204 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), ("%s", err->message));
205 ret = GST_FLOW_ERROR;
206 g_clear_error (&err);
208 } else if (recv_len == 0) {
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);
219 /* pktseq is a 31bit field */
220 self->next_pktseq = (mctrl.pktseq + 1) % G_MAXINT32;
222 /* 0 means we do not have a srctime */
223 if (mctrl.srctime != 0)
224 delay = (srt_time - mctrl.srctime) * GST_USECOND;
228 GST_LOG_OBJECT (src, "delay: %" GST_STIME_FORMAT, GST_STIME_ARGS (delay));
231 GST_WARNING_OBJECT (src,
232 "Calculated SRT delay %" GST_STIME_FORMAT " is negative, clamping to 0",
233 GST_STIME_ARGS (delay));
237 /* Subtract the base_time (since the pipeline started) ... */
238 if (capture_time > base_time)
239 capture_time -= base_time;
242 /* And adjust by the delay */
243 if (capture_time > delay)
244 capture_time -= delay;
247 GST_BUFFER_TIMESTAMP (outbuf) = capture_time;
249 gst_buffer_resize (outbuf, 0, recv_len);
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));
265 gst_srt_src_init (GstSRTSrc * self)
267 self->srtobject = gst_srt_object_new (GST_ELEMENT (self));
268 self->cancellable = g_cancellable_new ();
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);
275 gst_srt_object_set_uri (self->srtobject, GST_SRT_DEFAULT_URI, NULL);
280 gst_srt_src_finalize (GObject * object)
282 GstSRTSrc *self = GST_SRT_SRC (object);
284 g_clear_object (&self->cancellable);
285 gst_srt_object_destroy (self->srtobject);
287 G_OBJECT_CLASS (parent_class)->finalize (object);
291 gst_srt_src_unlock (GstBaseSrc * bsrc)
293 GstSRTSrc *self = GST_SRT_SRC (bsrc);
295 gst_srt_object_wakeup (self->srtobject, self->cancellable);
301 gst_srt_src_unlock_stop (GstBaseSrc * bsrc)
303 GstSRTSrc *self = GST_SRT_SRC (bsrc);
305 g_cancellable_reset (self->cancellable);
311 gst_srt_src_set_property (GObject * object,
312 guint prop_id, const GValue * value, GParamSpec * pspec)
314 GstSRTSrc *self = GST_SRT_SRC (object);
316 if (!gst_srt_object_set_property_helper (self->srtobject, prop_id, value,
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323 gst_srt_src_get_property (GObject * object,
324 guint prop_id, GValue * value, GParamSpec * pspec)
326 GstSRTSrc *self = GST_SRT_SRC (object);
328 if (!gst_srt_object_get_property_helper (self->srtobject, prop_id, value,
330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
335 gst_srt_src_query (GstBaseSrc * basesrc, GstQuery * query)
337 GstSRTSrc *self = GST_SRT_SRC (basesrc);
339 if (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY) {
341 if (!gst_structure_get_int (self->srtobject->parameters, "latency",
343 latency = GST_SRT_DEFAULT_LATENCY;
344 gst_query_set_latency (query, TRUE, latency * GST_MSECOND,
345 latency * GST_MSECOND);
348 return GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
353 gst_srt_src_class_init (GstSRTSrcClass * klass)
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);
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;
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
371 * A new caller has connected to srtsrc.
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);
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
384 * The given caller has disconnected.
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);
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
398 * A caller's connection to srtsrc in listener mode has been rejected.
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);
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
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.
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);
427 gst_srt_object_install_properties_helper (gobject_class);
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>");
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);
441 gstpushsrc_class->fill = GST_DEBUG_FUNCPTR (gst_srt_src_fill);
445 gst_srt_src_uri_get_type (GType type)
450 static const gchar *const *
451 gst_srt_src_uri_get_protocols (GType type)
453 static const gchar *protocols[] = { GST_SRT_DEFAULT_URI_SCHEME, NULL };
459 gst_srt_src_uri_get_uri (GstURIHandler * handler)
462 GstSRTSrc *self = GST_SRT_SRC (handler);
464 GST_OBJECT_LOCK (self);
465 uri_str = gst_uri_to_string (self->srtobject->uri);
466 GST_OBJECT_UNLOCK (self);
472 gst_srt_src_uri_set_uri (GstURIHandler * handler,
473 const gchar * uri, GError ** error)
475 GstSRTSrc *self = GST_SRT_SRC (handler);
478 GST_OBJECT_LOCK (self);
479 ret = gst_srt_object_set_uri (self->srtobject, uri, error);
480 GST_OBJECT_UNLOCK (self);
486 gst_srt_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
488 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
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;