2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3 * Copyright (C) 2015 Centricular Ltd
4 * Author: Sebastian Dröge <sebastian@centricular.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 * @short_description: The media pipeline
24 * @see_also: #GstRTSPMediaFactory, #GstRTSPStream, #GstRTSPSession,
25 * #GstRTSPSessionMedia
27 * a #GstRTSPMedia contains the complete GStreamer pipeline to manage the
28 * streaming to the clients. The actual data transfer is done by the
29 * #GstRTSPStream objects that are created and exposed by the #GstRTSPMedia.
31 * The #GstRTSPMedia is usually created from a #GstRTSPMediaFactory when the
32 * client does a DESCRIBE or SETUP of a resource.
34 * A media is created with gst_rtsp_media_new() that takes the element that will
35 * provide the streaming elements. For each of the streams, a new #GstRTSPStream
36 * object needs to be made with the gst_rtsp_media_create_stream() which takes
37 * the payloader element and the source pad that produces the RTP stream.
39 * The pipeline of the media is set to PAUSED with gst_rtsp_media_prepare(). The
40 * prepare method will add rtpbin and sinks and sources to send and receive RTP
41 * and RTCP packets from the clients. Each stream srcpad is connected to an
42 * input into the internal rtpbin.
44 * It is also possible to dynamically create #GstRTSPStream objects during the
45 * prepare phase. With gst_rtsp_media_get_status() you can check the status of
48 * After the media is prepared, it is ready for streaming. It will usually be
49 * managed in a session with gst_rtsp_session_manage_media(). See
50 * #GstRTSPSession and #GstRTSPSessionMedia.
52 * The state of the media can be controlled with gst_rtsp_media_set_state ().
53 * Seeking can be done with gst_rtsp_media_seek().
55 * With gst_rtsp_media_unprepare() the pipeline is stopped and shut down. When
56 * gst_rtsp_media_set_eos_shutdown() an EOS will be sent to the pipeline to
59 * With gst_rtsp_media_set_shared(), the media can be shared between multiple
60 * clients. With gst_rtsp_media_set_reusable() you can control if the pipeline
61 * can be prepared again after an unprepare.
63 * Last reviewed on 2013-07-11 (1.0.0)
70 #include <gst/app/gstappsrc.h>
71 #include <gst/app/gstappsink.h>
73 #include <gst/sdp/gstmikey.h>
74 #include <gst/rtp/gstrtppayloads.h>
76 #define AES_128_KEY_LEN 16
77 #define AES_256_KEY_LEN 32
79 #define HMAC_32_KEY_LEN 4
80 #define HMAC_80_KEY_LEN 10
82 #include "rtsp-media.h"
84 #define GST_RTSP_MEDIA_GET_PRIVATE(obj) \
85 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_MEDIA, GstRTSPMediaPrivate))
87 struct _GstRTSPMediaPrivate
92 /* protected by lock */
93 GstRTSPPermissions *permissions;
95 gboolean suspend_mode;
97 GstRTSPProfile profiles;
98 GstRTSPLowerTrans protocols;
100 gboolean eos_shutdown;
102 GstRTSPAddressPool *pool;
104 GstRTSPTransportMode transport_mode;
105 gboolean stop_on_disconnect;
108 GRecMutex state_lock; /* locking order: state lock, lock */
109 GPtrArray *streams; /* protected by lock */
110 GList *dynamic; /* protected by lock */
111 GstRTSPMediaStatus status; /* protected by lock */
116 /* the pipeline for the media */
117 GstElement *pipeline;
118 GstElement *fakesink; /* protected by lock */
121 GstRTSPThread *thread;
123 gboolean time_provider;
124 GstNetTimeProvider *nettime;
129 GstState target_state;
131 /* RTP session manager */
134 /* the range of media */
135 GstRTSPTimeRange range; /* protected by lock */
136 GstClockTime range_start;
137 GstClockTime range_stop;
139 GList *payloads; /* protected by lock */
140 GstClockTime rtx_time; /* protected by lock */
141 guint latency; /* protected by lock */
142 GstClock *clock; /* protected by lock */
145 #define DEFAULT_SHARED FALSE
146 #define DEFAULT_SUSPEND_MODE GST_RTSP_SUSPEND_MODE_NONE
147 #define DEFAULT_REUSABLE FALSE
148 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
149 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
150 GST_RTSP_LOWER_TRANS_TCP
151 #define DEFAULT_EOS_SHUTDOWN FALSE
152 #define DEFAULT_BUFFER_SIZE 0x80000
153 #define DEFAULT_TIME_PROVIDER FALSE
154 #define DEFAULT_LATENCY 200
155 #define DEFAULT_TRANSPORT_MODE GST_RTSP_TRANSPORT_MODE_PLAY
156 #define DEFAULT_STOP_ON_DISCONNECT TRUE
158 /* define to dump received RTCP packets */
175 PROP_STOP_ON_DISCONNECT,
183 SIGNAL_REMOVED_STREAM,
191 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
192 #define GST_CAT_DEFAULT rtsp_media_debug
194 static void gst_rtsp_media_get_property (GObject * object, guint propid,
195 GValue * value, GParamSpec * pspec);
196 static void gst_rtsp_media_set_property (GObject * object, guint propid,
197 const GValue * value, GParamSpec * pspec);
198 static void gst_rtsp_media_finalize (GObject * obj);
200 static gboolean default_handle_message (GstRTSPMedia * media,
201 GstMessage * message);
202 static void finish_unprepare (GstRTSPMedia * media);
203 static gboolean default_prepare (GstRTSPMedia * media, GstRTSPThread * thread);
204 static gboolean default_unprepare (GstRTSPMedia * media);
205 static gboolean default_suspend (GstRTSPMedia * media);
206 static gboolean default_unsuspend (GstRTSPMedia * media);
207 static gboolean default_convert_range (GstRTSPMedia * media,
208 GstRTSPTimeRange * range, GstRTSPRangeUnit unit);
209 static gboolean default_query_position (GstRTSPMedia * media,
211 static gboolean default_query_stop (GstRTSPMedia * media, gint64 * stop);
212 static GstElement *default_create_rtpbin (GstRTSPMedia * media);
213 static gboolean default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
215 static gboolean default_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp);
217 static gboolean wait_preroll (GstRTSPMedia * media);
219 static GstElement *find_payload_element (GstElement * payloader);
221 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
223 #define C_ENUM(v) ((gint) v)
226 gst_rtsp_suspend_mode_get_type (void)
229 static const GEnumValue values[] = {
230 {C_ENUM (GST_RTSP_SUSPEND_MODE_NONE), "GST_RTSP_SUSPEND_MODE_NONE", "none"},
231 {C_ENUM (GST_RTSP_SUSPEND_MODE_PAUSE), "GST_RTSP_SUSPEND_MODE_PAUSE",
233 {C_ENUM (GST_RTSP_SUSPEND_MODE_RESET), "GST_RTSP_SUSPEND_MODE_RESET",
238 if (g_once_init_enter (&id)) {
239 GType tmp = g_enum_register_static ("GstRTSPSuspendMode", values);
240 g_once_init_leave (&id, tmp);
245 #define C_FLAGS(v) ((guint) v)
248 gst_rtsp_transport_mode_get_type (void)
251 static const GFlagsValue values[] = {
252 {C_FLAGS (GST_RTSP_TRANSPORT_MODE_PLAY), "GST_RTSP_TRANSPORT_MODE_PLAY",
254 {C_FLAGS (GST_RTSP_TRANSPORT_MODE_RECORD), "GST_RTSP_TRANSPORT_MODE_RECORD",
259 if (g_once_init_enter (&id)) {
260 GType tmp = g_flags_register_static ("GstRTSPTransportMode", values);
261 g_once_init_leave (&id, tmp);
266 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
269 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
271 GObjectClass *gobject_class;
273 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
275 gobject_class = G_OBJECT_CLASS (klass);
277 gobject_class->get_property = gst_rtsp_media_get_property;
278 gobject_class->set_property = gst_rtsp_media_set_property;
279 gobject_class->finalize = gst_rtsp_media_finalize;
281 g_object_class_install_property (gobject_class, PROP_SHARED,
282 g_param_spec_boolean ("shared", "Shared",
283 "If this media pipeline can be shared", DEFAULT_SHARED,
284 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286 g_object_class_install_property (gobject_class, PROP_SUSPEND_MODE,
287 g_param_spec_enum ("suspend-mode", "Suspend Mode",
288 "How to suspend the media in PAUSED", GST_TYPE_RTSP_SUSPEND_MODE,
289 DEFAULT_SUSPEND_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
291 g_object_class_install_property (gobject_class, PROP_REUSABLE,
292 g_param_spec_boolean ("reusable", "Reusable",
293 "If this media pipeline can be reused after an unprepare",
294 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296 g_object_class_install_property (gobject_class, PROP_PROFILES,
297 g_param_spec_flags ("profiles", "Profiles",
298 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
299 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
301 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
302 g_param_spec_flags ("protocols", "Protocols",
303 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
304 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
307 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
308 "Send an EOS event to the pipeline before unpreparing",
309 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
312 g_param_spec_uint ("buffer-size", "Buffer Size",
313 "The kernel UDP buffer size to use", 0, G_MAXUINT,
314 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
316 g_object_class_install_property (gobject_class, PROP_ELEMENT,
317 g_param_spec_object ("element", "The Element",
318 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
319 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
321 g_object_class_install_property (gobject_class, PROP_TIME_PROVIDER,
322 g_param_spec_boolean ("time-provider", "Time Provider",
323 "Use a NetTimeProvider for clients",
324 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_LATENCY,
327 g_param_spec_uint ("latency", "Latency",
328 "Latency used for receiving media in milliseconds", 0, G_MAXUINT,
329 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (gobject_class, PROP_TRANSPORT_MODE,
332 g_param_spec_flags ("transport-mode", "Transport Mode",
333 "If this media pipeline can be used for PLAY or RECORD",
334 GST_TYPE_RTSP_TRANSPORT_MODE, DEFAULT_TRANSPORT_MODE,
335 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_STOP_ON_DISCONNECT,
338 g_param_spec_boolean ("stop-on-disconnect", "Stop On Disconnect",
339 "If this media pipeline should be stopped "
340 "when a client disconnects without TEARDOWN",
341 DEFAULT_STOP_ON_DISCONNECT,
342 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344 g_object_class_install_property (gobject_class, PROP_CLOCK,
345 g_param_spec_object ("clock", "Clock",
346 "Clock to be used by the media pipeline",
347 GST_TYPE_CLOCK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
349 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
350 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
351 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
352 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
354 gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
355 g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
356 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
357 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
358 GST_TYPE_RTSP_STREAM);
360 gst_rtsp_media_signals[SIGNAL_PREPARED] =
361 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
362 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
363 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
365 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
366 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
367 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
368 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
370 gst_rtsp_media_signals[SIGNAL_TARGET_STATE] =
371 g_signal_new ("target-state", G_TYPE_FROM_CLASS (klass),
372 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, target_state),
373 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
375 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
376 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
377 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
378 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
380 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
382 klass->handle_message = default_handle_message;
383 klass->prepare = default_prepare;
384 klass->unprepare = default_unprepare;
385 klass->suspend = default_suspend;
386 klass->unsuspend = default_unsuspend;
387 klass->convert_range = default_convert_range;
388 klass->query_position = default_query_position;
389 klass->query_stop = default_query_stop;
390 klass->create_rtpbin = default_create_rtpbin;
391 klass->setup_sdp = default_setup_sdp;
392 klass->handle_sdp = default_handle_sdp;
396 gst_rtsp_media_init (GstRTSPMedia * media)
398 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
402 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
403 g_mutex_init (&priv->lock);
404 g_cond_init (&priv->cond);
405 g_rec_mutex_init (&priv->state_lock);
407 priv->shared = DEFAULT_SHARED;
408 priv->suspend_mode = DEFAULT_SUSPEND_MODE;
409 priv->reusable = DEFAULT_REUSABLE;
410 priv->profiles = DEFAULT_PROFILES;
411 priv->protocols = DEFAULT_PROTOCOLS;
412 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
413 priv->buffer_size = DEFAULT_BUFFER_SIZE;
414 priv->time_provider = DEFAULT_TIME_PROVIDER;
415 priv->transport_mode = DEFAULT_TRANSPORT_MODE;
416 priv->stop_on_disconnect = DEFAULT_STOP_ON_DISCONNECT;
420 gst_rtsp_media_finalize (GObject * obj)
422 GstRTSPMediaPrivate *priv;
425 media = GST_RTSP_MEDIA (obj);
428 GST_INFO ("finalize media %p", media);
430 if (priv->permissions)
431 gst_rtsp_permissions_unref (priv->permissions);
433 g_ptr_array_unref (priv->streams);
435 g_list_free_full (priv->dynamic, gst_object_unref);
438 gst_object_unref (priv->pipeline);
440 gst_object_unref (priv->nettime);
441 gst_object_unref (priv->element);
443 g_object_unref (priv->pool);
445 g_list_free (priv->payloads);
446 g_mutex_clear (&priv->lock);
447 g_cond_clear (&priv->cond);
448 g_rec_mutex_clear (&priv->state_lock);
450 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
454 gst_rtsp_media_get_property (GObject * object, guint propid,
455 GValue * value, GParamSpec * pspec)
457 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
461 g_value_set_object (value, media->priv->element);
464 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
466 case PROP_SUSPEND_MODE:
467 g_value_set_enum (value, gst_rtsp_media_get_suspend_mode (media));
470 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
473 g_value_set_flags (value, gst_rtsp_media_get_profiles (media));
476 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
478 case PROP_EOS_SHUTDOWN:
479 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
481 case PROP_BUFFER_SIZE:
482 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
484 case PROP_TIME_PROVIDER:
485 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
488 g_value_set_uint (value, gst_rtsp_media_get_latency (media));
490 case PROP_TRANSPORT_MODE:
491 g_value_set_flags (value, gst_rtsp_media_get_transport_mode (media));
493 case PROP_STOP_ON_DISCONNECT:
494 g_value_set_boolean (value, gst_rtsp_media_is_stop_on_disconnect (media));
497 g_value_take_object (value, gst_rtsp_media_get_clock (media));
500 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
505 gst_rtsp_media_set_property (GObject * object, guint propid,
506 const GValue * value, GParamSpec * pspec)
508 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
512 media->priv->element = g_value_get_object (value);
513 gst_object_ref_sink (media->priv->element);
516 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
518 case PROP_SUSPEND_MODE:
519 gst_rtsp_media_set_suspend_mode (media, g_value_get_enum (value));
522 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
525 gst_rtsp_media_set_profiles (media, g_value_get_flags (value));
528 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
530 case PROP_EOS_SHUTDOWN:
531 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
533 case PROP_BUFFER_SIZE:
534 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
536 case PROP_TIME_PROVIDER:
537 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
540 gst_rtsp_media_set_latency (media, g_value_get_uint (value));
542 case PROP_TRANSPORT_MODE:
543 gst_rtsp_media_set_transport_mode (media, g_value_get_flags (value));
545 case PROP_STOP_ON_DISCONNECT:
546 gst_rtsp_media_set_stop_on_disconnect (media,
547 g_value_get_boolean (value));
550 gst_rtsp_media_set_clock (media, g_value_get_object (value));
553 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
561 } DoQueryPositionData;
564 do_query_position (GstRTSPStream * stream, DoQueryPositionData * data)
568 if (gst_rtsp_stream_query_position (stream, &tmp)) {
569 data->position = MAX (data->position, tmp);
575 default_query_position (GstRTSPMedia * media, gint64 * position)
577 GstRTSPMediaPrivate *priv;
578 DoQueryPositionData data;
585 g_ptr_array_foreach (priv->streams, (GFunc) do_query_position, &data);
587 *position = data.position;
599 do_query_stop (GstRTSPStream * stream, DoQueryStopData * data)
603 if (gst_rtsp_stream_query_stop (stream, &tmp)) {
604 data->stop = MAX (data->stop, tmp);
610 default_query_stop (GstRTSPMedia * media, gint64 * stop)
612 GstRTSPMediaPrivate *priv;
613 DoQueryStopData data;
620 g_ptr_array_foreach (priv->streams, (GFunc) do_query_stop, &data);
628 default_create_rtpbin (GstRTSPMedia * media)
632 rtpbin = gst_element_factory_make ("rtpbin", NULL);
637 /* must be called with state lock */
639 collect_media_stats (GstRTSPMedia * media)
641 GstRTSPMediaPrivate *priv = media->priv;
642 gint64 position = 0, stop = -1;
644 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
645 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
648 priv->range.unit = GST_RTSP_RANGE_NPT;
650 GST_INFO ("collect media stats");
653 priv->range.min.type = GST_RTSP_TIME_NOW;
654 priv->range.min.seconds = -1;
655 priv->range_start = -1;
656 priv->range.max.type = GST_RTSP_TIME_END;
657 priv->range.max.seconds = -1;
658 priv->range_stop = -1;
660 GstRTSPMediaClass *klass;
663 klass = GST_RTSP_MEDIA_GET_CLASS (media);
665 /* get the position */
667 if (klass->query_position)
668 ret = klass->query_position (media, &position);
671 GST_INFO ("position query failed");
675 /* get the current segment stop */
677 if (klass->query_stop)
678 ret = klass->query_stop (media, &stop);
681 GST_INFO ("stop query failed");
685 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
686 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
688 if (position == -1) {
689 priv->range.min.type = GST_RTSP_TIME_NOW;
690 priv->range.min.seconds = -1;
691 priv->range_start = -1;
693 priv->range.min.type = GST_RTSP_TIME_SECONDS;
694 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
695 priv->range_start = position;
698 priv->range.max.type = GST_RTSP_TIME_END;
699 priv->range.max.seconds = -1;
700 priv->range_stop = -1;
702 priv->range.max.type = GST_RTSP_TIME_SECONDS;
703 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
704 priv->range_stop = stop;
710 * gst_rtsp_media_new:
711 * @element: (transfer full): a #GstElement
713 * Create a new #GstRTSPMedia instance. @element is the bin element that
714 * provides the different streams. The #GstRTSPMedia object contains the
715 * element to produce RTP data for one or more related (audio/video/..)
718 * Ownership is taken of @element.
720 * Returns: (transfer full): a new #GstRTSPMedia object.
723 gst_rtsp_media_new (GstElement * element)
725 GstRTSPMedia *result;
727 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
729 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
735 * gst_rtsp_media_get_element:
736 * @media: a #GstRTSPMedia
738 * Get the element that was used when constructing @media.
740 * Returns: (transfer full): a #GstElement. Unref after usage.
743 gst_rtsp_media_get_element (GstRTSPMedia * media)
745 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
747 return gst_object_ref (media->priv->element);
751 * gst_rtsp_media_take_pipeline:
752 * @media: a #GstRTSPMedia
753 * @pipeline: (transfer full): a #GstPipeline
755 * Set @pipeline as the #GstPipeline for @media. Ownership is
756 * taken of @pipeline.
759 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
761 GstRTSPMediaPrivate *priv;
763 GstNetTimeProvider *nettime;
765 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
766 g_return_if_fail (GST_IS_PIPELINE (pipeline));
770 g_mutex_lock (&priv->lock);
771 old = priv->pipeline;
772 priv->pipeline = GST_ELEMENT_CAST (pipeline);
773 nettime = priv->nettime;
774 priv->nettime = NULL;
775 g_mutex_unlock (&priv->lock);
778 gst_object_unref (old);
781 gst_object_unref (nettime);
783 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
787 * gst_rtsp_media_set_permissions:
788 * @media: a #GstRTSPMedia
789 * @permissions: (transfer none): a #GstRTSPPermissions
791 * Set @permissions on @media.
794 gst_rtsp_media_set_permissions (GstRTSPMedia * media,
795 GstRTSPPermissions * permissions)
797 GstRTSPMediaPrivate *priv;
799 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
803 g_mutex_lock (&priv->lock);
804 if (priv->permissions)
805 gst_rtsp_permissions_unref (priv->permissions);
806 if ((priv->permissions = permissions))
807 gst_rtsp_permissions_ref (permissions);
808 g_mutex_unlock (&priv->lock);
812 * gst_rtsp_media_get_permissions:
813 * @media: a #GstRTSPMedia
815 * Get the permissions object from @media.
817 * Returns: (transfer full): a #GstRTSPPermissions object, unref after usage.
820 gst_rtsp_media_get_permissions (GstRTSPMedia * media)
822 GstRTSPMediaPrivate *priv;
823 GstRTSPPermissions *result;
825 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
829 g_mutex_lock (&priv->lock);
830 if ((result = priv->permissions))
831 gst_rtsp_permissions_ref (result);
832 g_mutex_unlock (&priv->lock);
838 * gst_rtsp_media_set_suspend_mode:
839 * @media: a #GstRTSPMedia
840 * @mode: the new #GstRTSPSuspendMode
842 * Control how @ media will be suspended after the SDP has been generated and
843 * after a PAUSE request has been performed.
845 * Media must be unprepared when setting the suspend mode.
848 gst_rtsp_media_set_suspend_mode (GstRTSPMedia * media, GstRTSPSuspendMode mode)
850 GstRTSPMediaPrivate *priv;
852 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
856 g_rec_mutex_lock (&priv->state_lock);
857 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
859 priv->suspend_mode = mode;
860 g_rec_mutex_unlock (&priv->state_lock);
867 GST_WARNING ("media %p was prepared", media);
868 g_rec_mutex_unlock (&priv->state_lock);
873 * gst_rtsp_media_get_suspend_mode:
874 * @media: a #GstRTSPMedia
876 * Get how @media will be suspended.
878 * Returns: #GstRTSPSuspendMode.
881 gst_rtsp_media_get_suspend_mode (GstRTSPMedia * media)
883 GstRTSPMediaPrivate *priv;
884 GstRTSPSuspendMode res;
886 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_SUSPEND_MODE_NONE);
890 g_rec_mutex_lock (&priv->state_lock);
891 res = priv->suspend_mode;
892 g_rec_mutex_unlock (&priv->state_lock);
898 * gst_rtsp_media_set_shared:
899 * @media: a #GstRTSPMedia
900 * @shared: the new value
902 * Set or unset if the pipeline for @media can be shared will multiple clients.
903 * When @shared is %TRUE, client requests for this media will share the media
907 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
909 GstRTSPMediaPrivate *priv;
911 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
915 g_mutex_lock (&priv->lock);
916 priv->shared = shared;
917 g_mutex_unlock (&priv->lock);
921 * gst_rtsp_media_is_shared:
922 * @media: a #GstRTSPMedia
924 * Check if the pipeline for @media can be shared between multiple clients.
926 * Returns: %TRUE if the media can be shared between clients.
929 gst_rtsp_media_is_shared (GstRTSPMedia * media)
931 GstRTSPMediaPrivate *priv;
934 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
938 g_mutex_lock (&priv->lock);
940 g_mutex_unlock (&priv->lock);
946 * gst_rtsp_media_set_reusable:
947 * @media: a #GstRTSPMedia
948 * @reusable: the new value
950 * Set or unset if the pipeline for @media can be reused after the pipeline has
954 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
956 GstRTSPMediaPrivate *priv;
958 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
962 g_mutex_lock (&priv->lock);
963 priv->reusable = reusable;
964 g_mutex_unlock (&priv->lock);
968 * gst_rtsp_media_is_reusable:
969 * @media: a #GstRTSPMedia
971 * Check if the pipeline for @media can be reused after an unprepare.
973 * Returns: %TRUE if the media can be reused
976 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
978 GstRTSPMediaPrivate *priv;
981 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
985 g_mutex_lock (&priv->lock);
986 res = priv->reusable;
987 g_mutex_unlock (&priv->lock);
993 do_set_profiles (GstRTSPStream * stream, GstRTSPProfile * profiles)
995 gst_rtsp_stream_set_profiles (stream, *profiles);
999 * gst_rtsp_media_set_profiles:
1000 * @media: a #GstRTSPMedia
1001 * @profiles: the new flags
1003 * Configure the allowed lower transport for @media.
1006 gst_rtsp_media_set_profiles (GstRTSPMedia * media, GstRTSPProfile profiles)
1008 GstRTSPMediaPrivate *priv;
1010 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1014 g_mutex_lock (&priv->lock);
1015 priv->profiles = profiles;
1016 g_ptr_array_foreach (priv->streams, (GFunc) do_set_profiles, &profiles);
1017 g_mutex_unlock (&priv->lock);
1021 * gst_rtsp_media_get_profiles:
1022 * @media: a #GstRTSPMedia
1024 * Get the allowed profiles of @media.
1026 * Returns: a #GstRTSPProfile
1029 gst_rtsp_media_get_profiles (GstRTSPMedia * media)
1031 GstRTSPMediaPrivate *priv;
1034 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_PROFILE_UNKNOWN);
1038 g_mutex_lock (&priv->lock);
1039 res = priv->profiles;
1040 g_mutex_unlock (&priv->lock);
1046 do_set_protocols (GstRTSPStream * stream, GstRTSPLowerTrans * protocols)
1048 gst_rtsp_stream_set_protocols (stream, *protocols);
1052 * gst_rtsp_media_set_protocols:
1053 * @media: a #GstRTSPMedia
1054 * @protocols: the new flags
1056 * Configure the allowed lower transport for @media.
1059 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
1061 GstRTSPMediaPrivate *priv;
1063 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1067 g_mutex_lock (&priv->lock);
1068 priv->protocols = protocols;
1069 g_ptr_array_foreach (priv->streams, (GFunc) do_set_protocols, &protocols);
1070 g_mutex_unlock (&priv->lock);
1074 * gst_rtsp_media_get_protocols:
1075 * @media: a #GstRTSPMedia
1077 * Get the allowed protocols of @media.
1079 * Returns: a #GstRTSPLowerTrans
1082 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
1084 GstRTSPMediaPrivate *priv;
1085 GstRTSPLowerTrans res;
1087 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
1088 GST_RTSP_LOWER_TRANS_UNKNOWN);
1092 g_mutex_lock (&priv->lock);
1093 res = priv->protocols;
1094 g_mutex_unlock (&priv->lock);
1100 * gst_rtsp_media_set_eos_shutdown:
1101 * @media: a #GstRTSPMedia
1102 * @eos_shutdown: the new value
1104 * Set or unset if an EOS event will be sent to the pipeline for @media before
1108 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
1110 GstRTSPMediaPrivate *priv;
1112 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1116 g_mutex_lock (&priv->lock);
1117 priv->eos_shutdown = eos_shutdown;
1118 g_mutex_unlock (&priv->lock);
1122 * gst_rtsp_media_is_eos_shutdown:
1123 * @media: a #GstRTSPMedia
1125 * Check if the pipeline for @media will send an EOS down the pipeline before
1128 * Returns: %TRUE if the media will send EOS before unpreparing.
1131 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
1133 GstRTSPMediaPrivate *priv;
1136 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1140 g_mutex_lock (&priv->lock);
1141 res = priv->eos_shutdown;
1142 g_mutex_unlock (&priv->lock);
1148 * gst_rtsp_media_set_buffer_size:
1149 * @media: a #GstRTSPMedia
1150 * @size: the new value
1152 * Set the kernel UDP buffer size.
1155 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
1157 GstRTSPMediaPrivate *priv;
1160 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1162 GST_LOG_OBJECT (media, "set buffer size %u", size);
1166 g_mutex_lock (&priv->lock);
1167 priv->buffer_size = size;
1169 for (i = 0; i < priv->streams->len; i++) {
1170 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1171 gst_rtsp_stream_set_buffer_size (stream, size);
1173 g_mutex_unlock (&priv->lock);
1177 * gst_rtsp_media_get_buffer_size:
1178 * @media: a #GstRTSPMedia
1180 * Get the kernel UDP buffer size.
1182 * Returns: the kernel UDP buffer size.
1185 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
1187 GstRTSPMediaPrivate *priv;
1190 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1194 g_mutex_lock (&priv->lock);
1195 res = priv->buffer_size;
1196 g_mutex_unlock (&priv->lock);
1202 * gst_rtsp_media_set_stop_on_disconnect:
1203 * @media: a #GstRTSPMedia
1204 * @stop_on_disconnect: the new value
1206 * Set or unset if the pipeline for @media should be stopped when a
1207 * client disconnects without sending TEARDOWN.
1210 gst_rtsp_media_set_stop_on_disconnect (GstRTSPMedia * media,
1211 gboolean stop_on_disconnect)
1213 GstRTSPMediaPrivate *priv;
1215 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1219 g_mutex_lock (&priv->lock);
1220 priv->stop_on_disconnect = stop_on_disconnect;
1221 g_mutex_unlock (&priv->lock);
1225 * gst_rtsp_media_is_stop_on_disconnect:
1226 * @media: a #GstRTSPMedia
1228 * Check if the pipeline for @media will be stopped when a client disconnects
1229 * without sending TEARDOWN.
1231 * Returns: %TRUE if the media will be stopped when a client disconnects
1232 * without sending TEARDOWN.
1235 gst_rtsp_media_is_stop_on_disconnect (GstRTSPMedia * media)
1237 GstRTSPMediaPrivate *priv;
1240 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), TRUE);
1244 g_mutex_lock (&priv->lock);
1245 res = priv->stop_on_disconnect;
1246 g_mutex_unlock (&priv->lock);
1252 * gst_rtsp_media_set_retransmission_time:
1253 * @media: a #GstRTSPMedia
1254 * @time: the new value
1256 * Set the amount of time to store retransmission packets.
1259 gst_rtsp_media_set_retransmission_time (GstRTSPMedia * media, GstClockTime time)
1261 GstRTSPMediaPrivate *priv;
1264 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1266 GST_LOG_OBJECT (media, "set retransmission time %" G_GUINT64_FORMAT, time);
1270 g_mutex_lock (&priv->lock);
1271 priv->rtx_time = time;
1272 for (i = 0; i < priv->streams->len; i++) {
1273 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1275 gst_rtsp_stream_set_retransmission_time (stream, time);
1279 g_object_set (priv->rtpbin, "do-retransmission", time > 0, NULL);
1280 g_mutex_unlock (&priv->lock);
1284 * gst_rtsp_media_get_retransmission_time:
1285 * @media: a #GstRTSPMedia
1287 * Get the amount of time to store retransmission data.
1289 * Returns: the amount of time to store retransmission data.
1292 gst_rtsp_media_get_retransmission_time (GstRTSPMedia * media)
1294 GstRTSPMediaPrivate *priv;
1297 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1301 g_mutex_unlock (&priv->lock);
1302 res = priv->rtx_time;
1303 g_mutex_unlock (&priv->lock);
1309 * gst_rtsp_media_set_latency:
1310 * @media: a #GstRTSPMedia
1311 * @latency: latency in milliseconds
1313 * Configure the latency used for receiving media.
1316 gst_rtsp_media_set_latency (GstRTSPMedia * media, guint latency)
1318 GstRTSPMediaPrivate *priv;
1320 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1322 GST_LOG_OBJECT (media, "set latency %ums", latency);
1326 g_mutex_lock (&priv->lock);
1327 priv->latency = latency;
1329 g_object_set (priv->rtpbin, "latency", latency, NULL);
1330 g_mutex_unlock (&priv->lock);
1334 * gst_rtsp_media_get_latency:
1335 * @media: a #GstRTSPMedia
1337 * Get the latency that is used for receiving media.
1339 * Returns: latency in milliseconds
1342 gst_rtsp_media_get_latency (GstRTSPMedia * media)
1344 GstRTSPMediaPrivate *priv;
1347 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1351 g_mutex_unlock (&priv->lock);
1352 res = priv->latency;
1353 g_mutex_unlock (&priv->lock);
1359 * gst_rtsp_media_use_time_provider:
1360 * @media: a #GstRTSPMedia
1361 * @time_provider: if a #GstNetTimeProvider should be used
1363 * Set @media to provide a #GstNetTimeProvider.
1366 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
1368 GstRTSPMediaPrivate *priv;
1370 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1374 g_mutex_lock (&priv->lock);
1375 priv->time_provider = time_provider;
1376 g_mutex_unlock (&priv->lock);
1380 * gst_rtsp_media_is_time_provider:
1381 * @media: a #GstRTSPMedia
1383 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
1385 * Use gst_rtsp_media_get_time_provider() to get the network clock.
1387 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
1390 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
1392 GstRTSPMediaPrivate *priv;
1395 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1399 g_mutex_unlock (&priv->lock);
1400 res = priv->time_provider;
1401 g_mutex_unlock (&priv->lock);
1407 * gst_rtsp_media_set_clock:
1408 * @media: a #GstRTSPMedia
1409 * @clock: #GstClock to be used
1411 * Configure the clock used for the media.
1414 gst_rtsp_media_set_clock (GstRTSPMedia * media, GstClock * clock)
1416 GstRTSPMediaPrivate *priv;
1418 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1419 g_return_if_fail (GST_IS_CLOCK (clock) || clock == NULL);
1421 GST_LOG_OBJECT (media, "setting clock %" GST_PTR_FORMAT, clock);
1425 g_mutex_lock (&priv->lock);
1427 gst_object_unref (priv->clock);
1428 priv->clock = clock ? gst_object_ref (clock) : NULL;
1429 if (priv->pipeline) {
1431 gst_pipeline_use_clock (GST_PIPELINE_CAST (priv->pipeline), clock);
1433 gst_pipeline_auto_clock (GST_PIPELINE_CAST (priv->pipeline));
1436 g_mutex_unlock (&priv->lock);
1440 * gst_rtsp_media_set_address_pool:
1441 * @media: a #GstRTSPMedia
1442 * @pool: (transfer none): a #GstRTSPAddressPool
1444 * configure @pool to be used as the address pool of @media.
1447 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
1448 GstRTSPAddressPool * pool)
1450 GstRTSPMediaPrivate *priv;
1451 GstRTSPAddressPool *old;
1453 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1457 GST_LOG_OBJECT (media, "set address pool %p", pool);
1459 g_mutex_lock (&priv->lock);
1460 if ((old = priv->pool) != pool)
1461 priv->pool = pool ? g_object_ref (pool) : NULL;
1464 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
1466 g_mutex_unlock (&priv->lock);
1469 g_object_unref (old);
1473 * gst_rtsp_media_get_address_pool:
1474 * @media: a #GstRTSPMedia
1476 * Get the #GstRTSPAddressPool used as the address pool of @media.
1478 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
1481 GstRTSPAddressPool *
1482 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
1484 GstRTSPMediaPrivate *priv;
1485 GstRTSPAddressPool *result;
1487 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1491 g_mutex_lock (&priv->lock);
1492 if ((result = priv->pool))
1493 g_object_ref (result);
1494 g_mutex_unlock (&priv->lock);
1500 _find_payload_types (GstRTSPMedia * media)
1503 GQueue queue = G_QUEUE_INIT;
1505 n = media->priv->streams->len;
1506 for (i = 0; i < n; i++) {
1507 GstRTSPStream *stream = g_ptr_array_index (media->priv->streams, i);
1508 guint pt = gst_rtsp_stream_get_pt (stream);
1510 g_queue_push_tail (&queue, GUINT_TO_POINTER (pt));
1517 _next_available_pt (GList * payloads)
1521 for (i = 96; i <= 127; i++) {
1522 GList *iter = g_list_find (payloads, GINT_TO_POINTER (i));
1524 return GPOINTER_TO_UINT (i);
1531 * gst_rtsp_media_collect_streams:
1532 * @media: a #GstRTSPMedia
1534 * Find all payloader elements, they should be named pay\%d in the
1535 * element of @media, and create #GstRTSPStreams for them.
1537 * Collect all dynamic elements, named dynpay\%d, and add them to
1538 * the list of dynamic elements.
1540 * Find all depayloader elements, they should be named depay\%d in the
1541 * element of @media, and create #GstRTSPStreams for them.
1544 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
1546 GstRTSPMediaPrivate *priv;
1547 GstElement *element, *elem;
1551 gboolean more_elem_remaining = TRUE;
1552 GstRTSPTransportMode mode = 0;
1554 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1557 element = priv->element;
1560 for (i = 0; more_elem_remaining; i++) {
1563 more_elem_remaining = FALSE;
1565 name = g_strdup_printf ("pay%d", i);
1566 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1568 GST_INFO ("found stream %d with payloader %p", i, elem);
1570 /* take the pad of the payloader */
1571 pad = gst_element_get_static_pad (elem, "src");
1573 /* find the real payload element in case elem is a GstBin */
1574 pay = find_payload_element (elem);
1576 /* create the stream */
1578 GST_WARNING ("could not find real payloader, using bin");
1579 gst_rtsp_media_create_stream (media, elem, pad);
1581 gst_rtsp_media_create_stream (media, pay, pad);
1582 gst_object_unref (pay);
1585 gst_object_unref (pad);
1586 gst_object_unref (elem);
1589 more_elem_remaining = TRUE;
1590 mode |= GST_RTSP_TRANSPORT_MODE_PLAY;
1594 name = g_strdup_printf ("dynpay%d", i);
1595 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1596 /* a stream that will dynamically create pads to provide RTP packets */
1597 GST_INFO ("found dynamic element %d, %p", i, elem);
1599 g_mutex_lock (&priv->lock);
1600 priv->dynamic = g_list_prepend (priv->dynamic, elem);
1601 g_mutex_unlock (&priv->lock);
1604 more_elem_remaining = TRUE;
1605 mode |= GST_RTSP_TRANSPORT_MODE_PLAY;
1609 name = g_strdup_printf ("depay%d", i);
1610 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1611 GST_INFO ("found stream %d with depayloader %p", i, elem);
1613 /* take the pad of the payloader */
1614 pad = gst_element_get_static_pad (elem, "sink");
1615 /* create the stream */
1616 gst_rtsp_media_create_stream (media, elem, pad);
1617 gst_object_unref (pad);
1618 gst_object_unref (elem);
1621 more_elem_remaining = TRUE;
1622 mode |= GST_RTSP_TRANSPORT_MODE_RECORD;
1628 if (priv->transport_mode != mode)
1629 GST_WARNING ("found different mode than expected (0x%02x != 0x%02d)",
1630 priv->transport_mode, mode);
1635 * gst_rtsp_media_create_stream:
1636 * @media: a #GstRTSPMedia
1637 * @payloader: a #GstElement
1640 * Create a new stream in @media that provides RTP data on @pad.
1641 * @pad should be a pad of an element inside @media->element.
1643 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1647 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1650 GstRTSPMediaPrivate *priv;
1651 GstRTSPStream *stream;
1656 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1657 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1658 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1662 g_mutex_lock (&priv->lock);
1663 idx = priv->streams->len;
1665 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1667 if (GST_PAD_IS_SRC (pad))
1668 name = g_strdup_printf ("src_%u", idx);
1670 name = g_strdup_printf ("sink_%u", idx);
1672 ghostpad = gst_ghost_pad_new (name, pad);
1673 gst_pad_set_active (ghostpad, TRUE);
1674 gst_element_add_pad (priv->element, ghostpad);
1677 stream = gst_rtsp_stream_new (idx, payloader, ghostpad);
1679 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1680 gst_rtsp_stream_set_profiles (stream, priv->profiles);
1681 gst_rtsp_stream_set_protocols (stream, priv->protocols);
1682 gst_rtsp_stream_set_retransmission_time (stream, priv->rtx_time);
1683 gst_rtsp_stream_set_buffer_size (stream, priv->buffer_size);
1685 g_ptr_array_add (priv->streams, stream);
1687 if (GST_PAD_IS_SRC (pad)) {
1691 g_list_free (priv->payloads);
1692 priv->payloads = _find_payload_types (media);
1694 n = priv->streams->len;
1695 for (i = 0; i < n; i++) {
1696 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1697 guint rtx_pt = _next_available_pt (priv->payloads);
1700 GST_WARNING ("Ran out of space of dynamic payload types");
1704 gst_rtsp_stream_set_retransmission_pt (stream, rtx_pt);
1707 g_list_append (priv->payloads, GUINT_TO_POINTER (rtx_pt));
1710 g_mutex_unlock (&priv->lock);
1712 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1719 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1721 GstRTSPMediaPrivate *priv;
1726 g_mutex_lock (&priv->lock);
1727 /* remove the ghostpad */
1728 srcpad = gst_rtsp_stream_get_srcpad (stream);
1729 gst_element_remove_pad (priv->element, srcpad);
1730 gst_object_unref (srcpad);
1731 /* now remove the stream */
1732 g_object_ref (stream);
1733 g_ptr_array_remove (priv->streams, stream);
1734 g_mutex_unlock (&priv->lock);
1736 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1739 g_object_unref (stream);
1743 * gst_rtsp_media_n_streams:
1744 * @media: a #GstRTSPMedia
1746 * Get the number of streams in this media.
1748 * Returns: The number of streams.
1751 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1753 GstRTSPMediaPrivate *priv;
1756 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1760 g_mutex_lock (&priv->lock);
1761 res = priv->streams->len;
1762 g_mutex_unlock (&priv->lock);
1768 * gst_rtsp_media_get_stream:
1769 * @media: a #GstRTSPMedia
1770 * @idx: the stream index
1772 * Retrieve the stream with index @idx from @media.
1774 * Returns: (nullable) (transfer none): the #GstRTSPStream at index
1775 * @idx or %NULL when a stream with that index did not exist.
1778 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1780 GstRTSPMediaPrivate *priv;
1783 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1787 g_mutex_lock (&priv->lock);
1788 if (idx < priv->streams->len)
1789 res = g_ptr_array_index (priv->streams, idx);
1792 g_mutex_unlock (&priv->lock);
1798 * gst_rtsp_media_find_stream:
1799 * @media: a #GstRTSPMedia
1800 * @control: the control of the stream
1802 * Find a stream in @media with @control as the control uri.
1804 * Returns: (nullable) (transfer none): the #GstRTSPStream with
1805 * control uri @control or %NULL when a stream with that control did
1809 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
1811 GstRTSPMediaPrivate *priv;
1815 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1816 g_return_val_if_fail (control != NULL, NULL);
1822 g_mutex_lock (&priv->lock);
1823 for (i = 0; i < priv->streams->len; i++) {
1824 GstRTSPStream *test;
1826 test = g_ptr_array_index (priv->streams, i);
1827 if (gst_rtsp_stream_has_control (test, control)) {
1832 g_mutex_unlock (&priv->lock);
1837 /* called with state-lock */
1839 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
1840 GstRTSPRangeUnit unit)
1842 return gst_rtsp_range_convert_units (range, unit);
1846 * gst_rtsp_media_get_range_string:
1847 * @media: a #GstRTSPMedia
1848 * @play: for the PLAY request
1849 * @unit: the unit to use for the string
1851 * Get the current range as a string. @media must be prepared with
1852 * gst_rtsp_media_prepare ().
1854 * Returns: (transfer full): The range as a string, g_free() after usage.
1857 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1858 GstRTSPRangeUnit unit)
1860 GstRTSPMediaClass *klass;
1861 GstRTSPMediaPrivate *priv;
1863 GstRTSPTimeRange range;
1865 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1866 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1867 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1871 g_rec_mutex_lock (&priv->state_lock);
1872 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
1873 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
1876 g_mutex_lock (&priv->lock);
1878 /* Update the range value with current position/duration */
1879 collect_media_stats (media);
1882 range = priv->range;
1884 if (!play && priv->n_active > 0) {
1885 range.min.type = GST_RTSP_TIME_NOW;
1886 range.min.seconds = -1;
1888 g_mutex_unlock (&priv->lock);
1889 g_rec_mutex_unlock (&priv->state_lock);
1891 if (!klass->convert_range (media, &range, unit))
1892 goto conversion_failed;
1894 result = gst_rtsp_range_to_string (&range);
1901 GST_WARNING ("media %p was not prepared", media);
1902 g_rec_mutex_unlock (&priv->state_lock);
1907 GST_WARNING ("range conversion to unit %d failed", unit);
1913 stream_update_blocked (GstRTSPStream * stream, GstRTSPMedia * media)
1915 gst_rtsp_stream_set_blocked (stream, media->priv->blocked);
1919 media_streams_set_blocked (GstRTSPMedia * media, gboolean blocked)
1921 GstRTSPMediaPrivate *priv = media->priv;
1923 GST_DEBUG ("media %p set blocked %d", media, blocked);
1924 priv->blocked = blocked;
1925 g_ptr_array_foreach (priv->streams, (GFunc) stream_update_blocked, media);
1929 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1931 GstRTSPMediaPrivate *priv = media->priv;
1933 g_mutex_lock (&priv->lock);
1934 priv->status = status;
1935 GST_DEBUG ("setting new status to %d", status);
1936 g_cond_broadcast (&priv->cond);
1937 g_mutex_unlock (&priv->lock);
1941 * gst_rtsp_media_get_status:
1942 * @media: a #GstRTSPMedia
1944 * Get the status of @media. When @media is busy preparing, this function waits
1945 * until @media is prepared or in error.
1947 * Returns: the status of @media.
1950 gst_rtsp_media_get_status (GstRTSPMedia * media)
1952 GstRTSPMediaPrivate *priv = media->priv;
1953 GstRTSPMediaStatus result;
1956 g_mutex_lock (&priv->lock);
1957 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1958 /* while we are preparing, wait */
1959 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1960 GST_DEBUG ("waiting for status change");
1961 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1962 GST_DEBUG ("timeout, assuming error status");
1963 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1966 /* could be success or error */
1967 result = priv->status;
1968 GST_DEBUG ("got status %d", result);
1969 g_mutex_unlock (&priv->lock);
1975 * gst_rtsp_media_seek:
1976 * @media: a #GstRTSPMedia
1977 * @range: (transfer none): a #GstRTSPTimeRange
1979 * Seek the pipeline of @media to @range. @media must be prepared with
1980 * gst_rtsp_media_prepare().
1982 * Returns: %TRUE on success.
1985 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1987 GstRTSPMediaClass *klass;
1988 GstRTSPMediaPrivate *priv;
1990 GstClockTime start, stop;
1991 GstSeekType start_type, stop_type;
1993 gint64 current_position;
1995 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1997 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1998 g_return_val_if_fail (range != NULL, FALSE);
1999 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
2003 g_rec_mutex_lock (&priv->state_lock);
2004 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2007 /* Update the seekable state of the pipeline in case it changed */
2008 if ((priv->transport_mode & GST_RTSP_TRANSPORT_MODE_RECORD)) {
2009 /* TODO: Seeking for RECORD? */
2010 priv->seekable = FALSE;
2012 query = gst_query_new_seeking (GST_FORMAT_TIME);
2013 if (gst_element_query (priv->pipeline, query)) {
2018 gst_query_parse_seeking (query, &format, &seekable, &start, &end);
2019 priv->seekable = seekable;
2021 gst_query_unref (query);
2024 if (!priv->seekable)
2027 start_type = stop_type = GST_SEEK_TYPE_NONE;
2029 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
2031 gst_rtsp_range_get_times (range, &start, &stop);
2033 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
2034 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
2035 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
2036 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
2038 current_position = -1;
2039 if (klass->query_position)
2040 klass->query_position (media, ¤t_position);
2041 GST_INFO ("current media position %" GST_TIME_FORMAT,
2042 GST_TIME_ARGS (current_position));
2044 if (start != GST_CLOCK_TIME_NONE)
2045 start_type = GST_SEEK_TYPE_SET;
2047 if (priv->range_stop == stop)
2048 stop = GST_CLOCK_TIME_NONE;
2049 else if (stop != GST_CLOCK_TIME_NONE)
2050 stop_type = GST_SEEK_TYPE_SET;
2052 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
2055 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
2056 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
2058 /* depends on the current playing state of the pipeline. We might need to
2059 * queue this until we get EOS. */
2060 flags = GST_SEEK_FLAG_FLUSH;
2062 /* if range start was not supplied we must continue from current position.
2063 * but since we're doing a flushing seek, let us query the current position
2064 * so we end up at exactly the same position after the seek. */
2065 if (range->min.type == GST_RTSP_TIME_END) { /* Yepp, that's right! */
2066 if (current_position == -1) {
2067 GST_WARNING ("current position unknown");
2069 GST_DEBUG ("doing accurate seek to %" GST_TIME_FORMAT,
2070 GST_TIME_ARGS (current_position));
2071 start = current_position;
2072 start_type = GST_SEEK_TYPE_SET;
2073 flags |= GST_SEEK_FLAG_ACCURATE;
2076 /* only set keyframe flag when modifying start */
2077 if (start_type != GST_SEEK_TYPE_NONE)
2078 flags |= GST_SEEK_FLAG_KEY_UNIT;
2081 if (start == current_position && stop_type == GST_SEEK_TYPE_NONE) {
2082 GST_DEBUG ("not seeking because no position change");
2085 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
2087 media_streams_set_blocked (media, TRUE);
2089 /* FIXME, we only do forwards playback, no trick modes yet */
2090 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
2091 flags, start_type, start, stop_type, stop);
2093 /* and block for the seek to complete */
2094 GST_INFO ("done seeking %d", res);
2098 g_rec_mutex_unlock (&priv->state_lock);
2100 /* wait until pipeline is prerolled again, this will also collect stats */
2101 if (!wait_preroll (media))
2102 goto preroll_failed;
2104 g_rec_mutex_lock (&priv->state_lock);
2105 GST_INFO ("prerolled again");
2108 GST_INFO ("no seek needed");
2111 g_rec_mutex_unlock (&priv->state_lock);
2118 g_rec_mutex_unlock (&priv->state_lock);
2119 GST_INFO ("media %p is not prepared", media);
2124 g_rec_mutex_unlock (&priv->state_lock);
2125 GST_INFO ("pipeline is not seekable");
2130 g_rec_mutex_unlock (&priv->state_lock);
2131 GST_WARNING ("conversion to npt not supported");
2136 g_rec_mutex_unlock (&priv->state_lock);
2137 GST_INFO ("seeking failed");
2138 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2143 GST_WARNING ("failed to preroll after seek");
2149 stream_collect_blocking (GstRTSPStream * stream, gboolean * blocked)
2151 *blocked &= gst_rtsp_stream_is_blocking (stream);
2155 media_streams_blocking (GstRTSPMedia * media)
2157 gboolean blocking = TRUE;
2159 g_ptr_array_foreach (media->priv->streams, (GFunc) stream_collect_blocking,
2165 static GstStateChangeReturn
2166 set_state (GstRTSPMedia * media, GstState state)
2168 GstRTSPMediaPrivate *priv = media->priv;
2169 GstStateChangeReturn ret;
2171 GST_INFO ("set state to %s for media %p", gst_element_state_get_name (state),
2173 ret = gst_element_set_state (priv->pipeline, state);
2178 static GstStateChangeReturn
2179 set_target_state (GstRTSPMedia * media, GstState state, gboolean do_state)
2181 GstRTSPMediaPrivate *priv = media->priv;
2182 GstStateChangeReturn ret;
2184 GST_INFO ("set target state to %s for media %p",
2185 gst_element_state_get_name (state), media);
2186 priv->target_state = state;
2188 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
2189 priv->target_state, NULL);
2192 ret = set_state (media, state);
2194 ret = GST_STATE_CHANGE_SUCCESS;
2199 /* called with state-lock */
2201 default_handle_message (GstRTSPMedia * media, GstMessage * message)
2203 GstRTSPMediaPrivate *priv = media->priv;
2204 GstMessageType type;
2206 type = GST_MESSAGE_TYPE (message);
2209 case GST_MESSAGE_STATE_CHANGED:
2211 GstState old, new, pending;
2213 if (GST_MESSAGE_SRC (message) != GST_OBJECT (priv->pipeline))
2216 gst_message_parse_state_changed (message, &old, &new, &pending);
2218 GST_DEBUG ("%p: went from %s to %s (pending %s)", media,
2219 gst_element_state_get_name (old), gst_element_state_get_name (new),
2220 gst_element_state_get_name (pending));
2221 if ((priv->transport_mode & GST_RTSP_TRANSPORT_MODE_RECORD)
2222 && old == GST_STATE_READY && new == GST_STATE_PAUSED) {
2223 GST_INFO ("%p: went to PAUSED, prepared now", media);
2224 collect_media_stats (media);
2226 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2227 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2232 case GST_MESSAGE_BUFFERING:
2236 gst_message_parse_buffering (message, &percent);
2238 /* no state management needed for live pipelines */
2242 if (percent == 100) {
2243 /* a 100% message means buffering is done */
2244 priv->buffering = FALSE;
2245 /* if the desired state is playing, go back */
2246 if (priv->target_state == GST_STATE_PLAYING) {
2247 GST_INFO ("Buffering done, setting pipeline to PLAYING");
2248 set_state (media, GST_STATE_PLAYING);
2250 GST_INFO ("Buffering done");
2253 /* buffering busy */
2254 if (priv->buffering == FALSE) {
2255 if (priv->target_state == GST_STATE_PLAYING) {
2256 /* we were not buffering but PLAYING, PAUSE the pipeline. */
2257 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
2258 set_state (media, GST_STATE_PAUSED);
2260 GST_INFO ("Buffering ...");
2263 priv->buffering = TRUE;
2267 case GST_MESSAGE_LATENCY:
2269 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
2272 case GST_MESSAGE_ERROR:
2277 gst_message_parse_error (message, &gerror, &debug);
2278 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
2279 g_error_free (gerror);
2282 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2285 case GST_MESSAGE_WARNING:
2290 gst_message_parse_warning (message, &gerror, &debug);
2291 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
2292 g_error_free (gerror);
2296 case GST_MESSAGE_ELEMENT:
2298 const GstStructure *s;
2300 s = gst_message_get_structure (message);
2301 if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
2302 GST_DEBUG ("media received blocking message");
2303 if (priv->blocked && media_streams_blocking (media)) {
2304 GST_DEBUG ("media is blocking");
2305 collect_media_stats (media);
2307 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2308 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2313 case GST_MESSAGE_STREAM_STATUS:
2315 case GST_MESSAGE_ASYNC_DONE:
2317 /* when we are dynamically adding pads, the addition of the udpsrc will
2318 * temporarily produce ASYNC_DONE messages. We have to ignore them and
2319 * wait for the final ASYNC_DONE after everything prerolled */
2320 GST_INFO ("%p: ignoring ASYNC_DONE", media);
2322 GST_INFO ("%p: got ASYNC_DONE", media);
2323 collect_media_stats (media);
2325 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2326 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2329 case GST_MESSAGE_EOS:
2330 GST_INFO ("%p: got EOS", media);
2332 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
2333 GST_DEBUG ("shutting down after EOS");
2334 finish_unprepare (media);
2338 GST_INFO ("%p: got message type %d (%s)", media, type,
2339 gst_message_type_get_name (type));
2346 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
2348 GstRTSPMediaPrivate *priv = media->priv;
2349 GstRTSPMediaClass *klass;
2352 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2354 g_rec_mutex_lock (&priv->state_lock);
2355 if (klass->handle_message)
2356 ret = klass->handle_message (media, message);
2359 g_rec_mutex_unlock (&priv->state_lock);
2365 watch_destroyed (GstRTSPMedia * media)
2367 GST_DEBUG_OBJECT (media, "source destroyed");
2368 g_object_unref (media);
2372 find_payload_element (GstElement * payloader)
2374 GstElement *pay = NULL;
2376 if (GST_IS_BIN (payloader)) {
2378 GValue item = { 0 };
2380 iter = gst_bin_iterate_recurse (GST_BIN (payloader));
2381 while (gst_iterator_next (iter, &item) == GST_ITERATOR_OK) {
2382 GstElement *element = (GstElement *) g_value_get_object (&item);
2383 GstElementClass *eclass = GST_ELEMENT_GET_CLASS (element);
2387 gst_element_class_get_metadata (eclass, GST_ELEMENT_METADATA_KLASS);
2391 if (strstr (klass, "Payloader") && strstr (klass, "RTP")) {
2392 pay = gst_object_ref (element);
2393 g_value_unset (&item);
2396 g_value_unset (&item);
2398 gst_iterator_free (iter);
2400 pay = g_object_ref (payloader);
2406 /* called from streaming threads */
2408 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
2410 GstRTSPMediaPrivate *priv = media->priv;
2411 GstRTSPStream *stream;
2414 /* find the real payload element */
2415 pay = find_payload_element (element);
2416 stream = gst_rtsp_media_create_stream (media, pay, pad);
2417 gst_object_unref (pay);
2419 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
2421 g_rec_mutex_lock (&priv->state_lock);
2422 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
2425 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
2427 /* we will be adding elements below that will cause ASYNC_DONE to be
2428 * posted in the bus. We want to ignore those messages until the
2429 * pipeline really prerolled. */
2430 priv->adding = TRUE;
2432 /* join the element in the PAUSED state because this callback is
2433 * called from the streaming thread and it is PAUSED */
2434 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2435 priv->rtpbin, GST_STATE_PAUSED)) {
2436 GST_WARNING ("failed to join bin element");
2439 priv->adding = FALSE;
2440 g_rec_mutex_unlock (&priv->state_lock);
2447 gst_rtsp_media_remove_stream (media, stream);
2448 g_rec_mutex_unlock (&priv->state_lock);
2449 GST_INFO ("ignore pad because we are not preparing");
2455 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
2457 GstRTSPMediaPrivate *priv = media->priv;
2458 GstRTSPStream *stream;
2460 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
2464 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
2466 g_rec_mutex_lock (&priv->state_lock);
2467 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2468 g_rec_mutex_unlock (&priv->state_lock);
2470 gst_rtsp_media_remove_stream (media, stream);
2474 remove_fakesink (GstRTSPMediaPrivate * priv)
2476 GstElement *fakesink;
2478 g_mutex_lock (&priv->lock);
2479 if ((fakesink = priv->fakesink))
2480 gst_object_ref (fakesink);
2481 priv->fakesink = NULL;
2482 g_mutex_unlock (&priv->lock);
2485 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
2486 gst_element_set_state (fakesink, GST_STATE_NULL);
2487 gst_object_unref (fakesink);
2488 GST_INFO ("removed fakesink");
2493 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
2495 GstRTSPMediaPrivate *priv = media->priv;
2497 GST_INFO ("no more pads");
2498 remove_fakesink (priv);
2501 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
2503 struct _DynPaySignalHandlers
2505 gulong pad_added_handler;
2506 gulong pad_removed_handler;
2507 gulong no_more_pads_handler;
2511 start_preroll (GstRTSPMedia * media)
2513 GstRTSPMediaPrivate *priv = media->priv;
2514 GstStateChangeReturn ret;
2516 GST_INFO ("setting pipeline to PAUSED for media %p", media);
2517 /* first go to PAUSED */
2518 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
2521 case GST_STATE_CHANGE_SUCCESS:
2522 GST_INFO ("SUCCESS state change for media %p", media);
2523 priv->seekable = TRUE;
2525 case GST_STATE_CHANGE_ASYNC:
2526 GST_INFO ("ASYNC state change for media %p", media);
2527 priv->seekable = TRUE;
2529 case GST_STATE_CHANGE_NO_PREROLL:
2530 /* we need to go to PLAYING */
2531 GST_INFO ("NO_PREROLL state change: live media %p", media);
2532 /* FIXME we disable seeking for live streams for now. We should perform a
2533 * seeking query in preroll instead */
2534 priv->seekable = FALSE;
2535 priv->is_live = TRUE;
2536 if (!(priv->transport_mode & GST_RTSP_TRANSPORT_MODE_RECORD)) {
2537 /* start blocked to make sure nothing goes to the sink */
2538 media_streams_set_blocked (media, TRUE);
2540 ret = set_state (media, GST_STATE_PLAYING);
2541 if (ret == GST_STATE_CHANGE_FAILURE)
2544 case GST_STATE_CHANGE_FAILURE:
2552 GST_WARNING ("failed to preroll pipeline");
2558 wait_preroll (GstRTSPMedia * media)
2560 GstRTSPMediaStatus status;
2562 GST_DEBUG ("wait to preroll pipeline");
2564 /* wait until pipeline is prerolled */
2565 status = gst_rtsp_media_get_status (media);
2566 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
2567 goto preroll_failed;
2573 GST_WARNING ("failed to preroll pipeline");
2579 request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPMedia * media)
2581 GstRTSPMediaPrivate *priv = media->priv;
2582 GstRTSPStream *stream = NULL;
2585 g_mutex_lock (&priv->lock);
2586 for (i = 0; i < priv->streams->len; i++) {
2587 stream = g_ptr_array_index (priv->streams, i);
2589 if (sessid == gst_rtsp_stream_get_index (stream))
2592 g_mutex_unlock (&priv->lock);
2594 return gst_rtsp_stream_request_aux_sender (stream, sessid);
2598 start_prepare (GstRTSPMedia * media)
2600 GstRTSPMediaPrivate *priv = media->priv;
2604 g_rec_mutex_lock (&priv->state_lock);
2605 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
2606 goto no_longer_preparing;
2608 /* link streams we already have, other streams might appear when we have
2609 * dynamic elements */
2610 for (i = 0; i < priv->streams->len; i++) {
2611 GstRTSPStream *stream;
2613 stream = g_ptr_array_index (priv->streams, i);
2615 if (priv->rtx_time > 0) {
2616 /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
2617 g_signal_connect (priv->rtpbin, "request-aux-sender",
2618 (GCallback) request_aux_sender, media);
2621 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2622 priv->rtpbin, GST_STATE_NULL)) {
2623 goto join_bin_failed;
2628 g_object_set (priv->rtpbin, "do-retransmission", priv->rtx_time > 0, NULL);
2630 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2631 GstElement *elem = walk->data;
2632 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
2634 GST_INFO ("adding callbacks for dynamic element %p", elem);
2636 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
2637 (GCallback) pad_added_cb, media);
2638 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
2639 (GCallback) pad_removed_cb, media);
2640 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
2641 (GCallback) no_more_pads_cb, media);
2643 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
2645 if (!priv->fakesink) {
2646 /* we add a fakesink here in order to make the state change async. We remove
2647 * the fakesink again in the no-more-pads callback. */
2648 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
2649 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
2653 if (!start_preroll (media))
2654 goto preroll_failed;
2656 g_rec_mutex_unlock (&priv->state_lock);
2660 no_longer_preparing:
2662 GST_INFO ("media is no longer preparing");
2663 g_rec_mutex_unlock (&priv->state_lock);
2668 GST_WARNING ("failed to join bin element");
2669 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2670 g_rec_mutex_unlock (&priv->state_lock);
2675 GST_WARNING ("failed to preroll pipeline");
2676 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2677 g_rec_mutex_unlock (&priv->state_lock);
2683 default_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2685 GstRTSPMediaPrivate *priv;
2686 GstRTSPMediaClass *klass;
2688 GMainContext *context;
2693 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2695 if (!klass->create_rtpbin)
2696 goto no_create_rtpbin;
2698 priv->rtpbin = klass->create_rtpbin (media);
2699 if (priv->rtpbin != NULL) {
2700 gboolean success = TRUE;
2702 g_object_set (priv->rtpbin, "latency", priv->latency, NULL);
2704 if (klass->setup_rtpbin)
2705 success = klass->setup_rtpbin (media, priv->rtpbin);
2707 if (success == FALSE) {
2708 gst_object_unref (priv->rtpbin);
2709 priv->rtpbin = NULL;
2712 if (priv->rtpbin == NULL)
2715 priv->thread = thread;
2716 context = (thread != NULL) ? (thread->context) : NULL;
2718 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
2720 /* add the pipeline bus to our custom mainloop */
2721 priv->source = gst_bus_create_watch (bus);
2722 gst_object_unref (bus);
2724 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
2725 g_object_ref (media), (GDestroyNotify) watch_destroyed);
2727 priv->id = g_source_attach (priv->source, context);
2729 /* add stuff to the bin */
2730 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
2732 /* do remainder in context */
2733 source = g_idle_source_new ();
2734 g_source_set_callback (source, (GSourceFunc) start_prepare,
2735 g_object_ref (media), (GDestroyNotify) g_object_unref);
2736 g_source_attach (source, context);
2737 g_source_unref (source);
2744 GST_ERROR ("no create_rtpbin function");
2745 g_critical ("no create_rtpbin vmethod function set");
2750 GST_WARNING ("no rtpbin element");
2751 g_warning ("failed to create element 'rtpbin', check your installation");
2757 * gst_rtsp_media_prepare:
2758 * @media: a #GstRTSPMedia
2759 * @thread: (transfer full) (allow-none): a #GstRTSPThread to run the
2760 * bus handler or %NULL
2762 * Prepare @media for streaming. This function will create the objects
2763 * to manage the streaming. A pipeline must have been set on @media with
2764 * gst_rtsp_media_take_pipeline().
2766 * It will preroll the pipeline and collect vital information about the streams
2767 * such as the duration.
2769 * Returns: %TRUE on success.
2772 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2774 GstRTSPMediaPrivate *priv;
2775 GstRTSPMediaClass *klass;
2777 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2781 g_rec_mutex_lock (&priv->state_lock);
2782 priv->prepare_count++;
2784 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
2785 priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
2788 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2791 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
2792 goto not_unprepared;
2794 if (!priv->reusable && priv->reused)
2797 GST_INFO ("preparing media %p", media);
2799 /* reset some variables */
2800 priv->is_live = FALSE;
2801 priv->seekable = FALSE;
2802 priv->buffering = FALSE;
2804 /* we're preparing now */
2805 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
2807 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2808 if (klass->prepare) {
2809 if (!klass->prepare (media, thread))
2810 goto prepare_failed;
2814 g_rec_mutex_unlock (&priv->state_lock);
2816 /* now wait for all pads to be prerolled, FIXME, we should somehow be
2817 * able to do this async so that we don't block the server thread. */
2818 if (!wait_preroll (media))
2819 goto preroll_failed;
2821 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
2823 GST_INFO ("object %p is prerolled", media);
2830 /* we are not going to use the giving thread, so stop it. */
2832 gst_rtsp_thread_stop (thread);
2837 GST_LOG ("media %p was prepared", media);
2838 /* we are not going to use the giving thread, so stop it. */
2840 gst_rtsp_thread_stop (thread);
2841 g_rec_mutex_unlock (&priv->state_lock);
2847 /* we are not going to use the giving thread, so stop it. */
2849 gst_rtsp_thread_stop (thread);
2850 GST_WARNING ("media %p was not unprepared", media);
2851 priv->prepare_count--;
2852 g_rec_mutex_unlock (&priv->state_lock);
2857 /* we are not going to use the giving thread, so stop it. */
2859 gst_rtsp_thread_stop (thread);
2860 priv->prepare_count--;
2861 g_rec_mutex_unlock (&priv->state_lock);
2862 GST_WARNING ("can not reuse media %p", media);
2867 /* we are not going to use the giving thread, so stop it. */
2869 gst_rtsp_thread_stop (thread);
2870 priv->prepare_count--;
2871 g_rec_mutex_unlock (&priv->state_lock);
2872 GST_ERROR ("failed to prepare media");
2877 GST_WARNING ("failed to preroll pipeline");
2878 gst_rtsp_media_unprepare (media);
2883 /* must be called with state-lock */
2885 finish_unprepare (GstRTSPMedia * media)
2887 GstRTSPMediaPrivate *priv = media->priv;
2891 GST_DEBUG ("shutting down");
2893 /* release the lock on shutdown, otherwise pad_added_cb might try to
2894 * acquire the lock and then we deadlock */
2895 g_rec_mutex_unlock (&priv->state_lock);
2896 set_state (media, GST_STATE_NULL);
2897 g_rec_mutex_lock (&priv->state_lock);
2898 remove_fakesink (priv);
2900 for (i = 0; i < priv->streams->len; i++) {
2901 GstRTSPStream *stream;
2903 GST_INFO ("Removing elements of stream %d from pipeline", i);
2905 stream = g_ptr_array_index (priv->streams, i);
2907 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2910 /* remove the pad signal handlers */
2911 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2912 GstElement *elem = walk->data;
2913 DynPaySignalHandlers *handlers;
2916 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
2917 g_assert (handlers != NULL);
2919 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
2920 g_signal_handler_disconnect (G_OBJECT (elem),
2921 handlers->pad_removed_handler);
2922 g_signal_handler_disconnect (G_OBJECT (elem),
2923 handlers->no_more_pads_handler);
2925 g_slice_free (DynPaySignalHandlers, handlers);
2928 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
2929 priv->rtpbin = NULL;
2932 gst_object_unref (priv->nettime);
2933 priv->nettime = NULL;
2935 priv->reused = TRUE;
2936 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARED);
2938 /* when the media is not reusable, this will effectively unref the media and
2940 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
2942 /* the source has the last ref to the media */
2944 GST_DEBUG ("destroy source");
2945 g_source_destroy (priv->source);
2946 g_source_unref (priv->source);
2949 GST_DEBUG ("stop thread");
2950 gst_rtsp_thread_stop (priv->thread);
2954 /* called with state-lock */
2956 default_unprepare (GstRTSPMedia * media)
2958 GstRTSPMediaPrivate *priv = media->priv;
2960 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARING);
2962 if (priv->eos_shutdown) {
2963 GST_DEBUG ("sending EOS for shutdown");
2964 /* ref so that we don't disappear */
2965 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
2966 /* we need to go to playing again for the EOS to propagate, normally in this
2967 * state, nothing is receiving data from us anymore so this is ok. */
2968 set_state (media, GST_STATE_PLAYING);
2970 finish_unprepare (media);
2976 * gst_rtsp_media_unprepare:
2977 * @media: a #GstRTSPMedia
2979 * Unprepare @media. After this call, the media should be prepared again before
2980 * it can be used again. If the media is set to be non-reusable, a new instance
2983 * Returns: %TRUE on success.
2986 gst_rtsp_media_unprepare (GstRTSPMedia * media)
2988 GstRTSPMediaPrivate *priv;
2991 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2995 g_rec_mutex_lock (&priv->state_lock);
2996 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
2997 goto was_unprepared;
2999 priv->prepare_count--;
3000 if (priv->prepare_count > 0)
3003 GST_INFO ("unprepare media %p", media);
3005 media_streams_set_blocked (media, FALSE);
3006 set_target_state (media, GST_STATE_NULL, FALSE);
3009 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
3010 GstRTSPMediaClass *klass;
3012 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3013 if (klass->unprepare)
3014 success = klass->unprepare (media);
3016 finish_unprepare (media);
3018 g_rec_mutex_unlock (&priv->state_lock);
3024 g_rec_mutex_unlock (&priv->state_lock);
3025 GST_INFO ("media %p was already unprepared", media);
3030 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
3031 g_rec_mutex_unlock (&priv->state_lock);
3036 /* should be called with state-lock */
3038 get_clock_unlocked (GstRTSPMedia * media)
3040 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
3041 GST_DEBUG_OBJECT (media, "media was not prepared");
3044 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
3048 * gst_rtsp_media_get_clock:
3049 * @media: a #GstRTSPMedia
3051 * Get the clock that is used by the pipeline in @media.
3053 * @media must be prepared before this method returns a valid clock object.
3055 * Returns: (transfer full): the #GstClock used by @media. unref after usage.
3058 gst_rtsp_media_get_clock (GstRTSPMedia * media)
3061 GstRTSPMediaPrivate *priv;
3063 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
3067 g_rec_mutex_lock (&priv->state_lock);
3068 clock = get_clock_unlocked (media);
3069 g_rec_mutex_unlock (&priv->state_lock);
3075 * gst_rtsp_media_get_base_time:
3076 * @media: a #GstRTSPMedia
3078 * Get the base_time that is used by the pipeline in @media.
3080 * @media must be prepared before this method returns a valid base_time.
3082 * Returns: the base_time used by @media.
3085 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
3087 GstClockTime result;
3088 GstRTSPMediaPrivate *priv;
3090 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
3094 g_rec_mutex_lock (&priv->state_lock);
3095 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
3098 result = gst_element_get_base_time (media->priv->pipeline);
3099 g_rec_mutex_unlock (&priv->state_lock);
3106 g_rec_mutex_unlock (&priv->state_lock);
3107 GST_DEBUG_OBJECT (media, "media was not prepared");
3108 return GST_CLOCK_TIME_NONE;
3113 * gst_rtsp_media_get_time_provider:
3114 * @media: a #GstRTSPMedia
3115 * @address: (allow-none): an address or %NULL
3116 * @port: a port or 0
3118 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
3119 * will listen on @address and @port for client time requests.
3121 * Returns: (transfer full): the #GstNetTimeProvider of @media.
3123 GstNetTimeProvider *
3124 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
3127 GstRTSPMediaPrivate *priv;
3128 GstNetTimeProvider *provider = NULL;
3130 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
3134 g_rec_mutex_lock (&priv->state_lock);
3135 if (priv->time_provider) {
3136 if ((provider = priv->nettime) == NULL) {
3139 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
3140 provider = gst_net_time_provider_new (clock, address, port);
3141 gst_object_unref (clock);
3143 priv->nettime = provider;
3147 g_rec_mutex_unlock (&priv->state_lock);
3150 gst_object_ref (provider);
3156 default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp, GstSDPInfo * info)
3158 return gst_rtsp_sdp_from_media (sdp, info, media);
3162 * gst_rtsp_media_setup_sdp:
3163 * @media: a #GstRTSPMedia
3164 * @sdp: (transfer none): a #GstSDPMessage
3165 * @info: (transfer none): a #GstSDPInfo
3167 * Add @media specific info to @sdp. @info is used to configure the connection
3168 * information in the SDP.
3170 * Returns: TRUE on success.
3173 gst_rtsp_media_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
3176 GstRTSPMediaPrivate *priv;
3177 GstRTSPMediaClass *klass;
3180 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3181 g_return_val_if_fail (sdp != NULL, FALSE);
3182 g_return_val_if_fail (info != NULL, FALSE);
3186 g_rec_mutex_lock (&priv->state_lock);
3188 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3190 if (!klass->setup_sdp)
3193 res = klass->setup_sdp (media, sdp, info);
3195 g_rec_mutex_unlock (&priv->state_lock);
3202 g_rec_mutex_unlock (&priv->state_lock);
3203 GST_ERROR ("no setup_sdp function");
3204 g_critical ("no setup_sdp vmethod function set");
3209 static const gchar *
3210 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
3219 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
3222 if (sscanf (attr, "%d ", &val) != 1)
3231 #define PARSE_INT(p, del, res) \
3234 p = strstr (p, del); \
3244 #define PARSE_STRING(p, del, res) \
3247 p = strstr (p, del); \
3259 #define SKIP_SPACES(p) \
3260 while (*p && g_ascii_isspace (*p)) \
3265 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
3268 parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
3269 gint * rate, gchar ** params)
3273 p = (gchar *) rtpmap;
3275 PARSE_INT (p, " ", *payload);
3283 PARSE_STRING (p, "/", *name);
3284 if (*name == NULL) {
3285 GST_DEBUG ("no rate, name %s", p);
3286 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
3287 * streams seem to omit the rate. */
3294 p = strstr (p, "/");
3312 * Mapping of caps to and from SDP fields:
3314 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
3315 * a=framesize:<payload> <width>-<height>
3316 * a=fmtp:<payload> <param>[=<value>];...
3319 media_to_caps (gint pt, const GstSDPMedia * media)
3322 const gchar *rtpmap;
3324 const gchar *framesize;
3327 gchar *params = NULL;
3333 /* get and parse rtpmap */
3334 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
3337 ret = parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
3339 g_warning ("error parsing rtpmap, ignoring");
3343 /* dynamic payloads need rtpmap or we fail */
3344 if (rtpmap == NULL && pt >= 96)
3347 /* check if we have a rate, if not, we need to look up the rate from the
3348 * default rates based on the payload types. */
3350 const GstRTPPayloadInfo *info;
3352 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
3353 /* dynamic types, use media and encoding_name */
3354 tmp = g_ascii_strdown (media->media, -1);
3355 info = gst_rtp_payload_info_for_name (tmp, name);
3358 /* static types, use payload type */
3359 info = gst_rtp_payload_info_for_pt (pt);
3363 if ((rate = info->clock_rate) == 0)
3366 /* we fail if we cannot find one */
3371 tmp = g_ascii_strdown (media->media, -1);
3372 caps = gst_caps_new_simple ("application/x-unknown",
3373 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
3375 s = gst_caps_get_structure (caps, 0);
3377 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
3379 /* encoding name must be upper case */
3381 tmp = g_ascii_strup (name, -1);
3382 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
3386 /* params must be lower case */
3387 if (params != NULL) {
3388 tmp = g_ascii_strdown (params, -1);
3389 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
3393 /* parse optional fmtp: field */
3394 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
3400 /* p is now of the format <payload> <param>[=<value>];... */
3401 PARSE_INT (p, " ", payload);
3402 if (payload != -1 && payload == pt) {
3406 /* <param>[=<value>] are separated with ';' */
3407 pairs = g_strsplit (p, ";", 0);
3408 for (i = 0; pairs[i]; i++) {
3410 const gchar *val, *key;
3412 const gchar *reserved_keys[] =
3413 { "media", "payload", "clock-rate", "encoding-name",
3417 /* the key may not have a '=', the value can have other '='s */
3418 valpos = strstr (pairs[i], "=");
3420 /* we have a '=' and thus a value, remove the '=' with \0 */
3422 /* value is everything between '=' and ';'. We split the pairs at ;
3423 * boundaries so we can take the remainder of the value. Some servers
3424 * put spaces around the value which we strip off here. Alternatively
3425 * we could strip those spaces in the depayloaders should these spaces
3426 * actually carry any meaning in the future. */
3427 val = g_strstrip (valpos + 1);
3429 /* simple <param>;.. is translated into <param>=1;... */
3432 /* strip the key of spaces, convert key to lowercase but not the value. */
3433 key = g_strstrip (pairs[i]);
3435 /* skip keys from the fmtp, which we already use ourselves for the
3436 * caps. Some software is adding random things like clock-rate into
3437 * the fmtp, and we would otherwise here set a string-typed clock-rate
3438 * in the caps... and thus fail to create valid RTP caps
3440 for (j = 0; j < G_N_ELEMENTS (reserved_keys); j++) {
3441 if (g_ascii_strcasecmp (reserved_keys[j], key) == 0) {
3447 if (strlen (key) > 1) {
3448 tmp = g_ascii_strdown (key, -1);
3449 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
3457 /* parse framesize: field */
3458 if ((framesize = gst_sdp_media_get_attribute_val (media, "framesize"))) {
3461 /* p is now of the format <payload> <width>-<height> */
3462 p = (gchar *) framesize;
3464 PARSE_INT (p, " ", payload);
3465 if (payload != -1 && payload == pt) {
3466 gst_structure_set (s, "a-framesize", G_TYPE_STRING, p, NULL);
3474 g_warning ("rtpmap type not given for dynamic payload %d", pt);
3479 g_warning ("rate unknown for payload type %d", pt);
3485 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
3487 gboolean res = FALSE;
3490 GstMIKEYMessage *msg;
3491 const GstMIKEYPayload *payload;
3492 const gchar *srtp_cipher;
3493 const gchar *srtp_auth;
3499 p = orig_value = g_strdup (keymgmt);
3503 g_free (orig_value);
3507 PARSE_STRING (p, " ", kmpid);
3508 if (kmpid == NULL || !g_str_equal (kmpid, "mikey")) {
3509 g_free (orig_value);
3512 data = g_base64_decode (p, &size);
3514 g_free (orig_value); /* Don't need this any more */
3520 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
3525 srtp_cipher = "aes-128-icm";
3526 srtp_auth = "hmac-sha1-80";
3528 /* check the Security policy if any */
3529 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
3530 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
3533 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
3536 len = gst_mikey_payload_sp_get_n_params (payload);
3537 for (i = 0; i < len; i++) {
3538 const GstMIKEYPayloadSPParam *param =
3539 gst_mikey_payload_sp_get_param (payload, i);
3541 switch (param->type) {
3542 case GST_MIKEY_SP_SRTP_ENC_ALG:
3543 switch (param->val[0]) {
3545 srtp_cipher = "null";
3549 srtp_cipher = "aes-128-icm";
3555 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
3556 switch (param->val[0]) {
3557 case AES_128_KEY_LEN:
3558 srtp_cipher = "aes-128-icm";
3560 case AES_256_KEY_LEN:
3561 srtp_cipher = "aes-256-icm";
3567 case GST_MIKEY_SP_SRTP_AUTH_ALG:
3568 switch (param->val[0]) {
3574 srtp_auth = "hmac-sha1-80";
3580 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
3581 switch (param->val[0]) {
3582 case HMAC_32_KEY_LEN:
3583 srtp_auth = "hmac-sha1-32";
3585 case HMAC_80_KEY_LEN:
3586 srtp_auth = "hmac-sha1-80";
3592 case GST_MIKEY_SP_SRTP_SRTP_ENC:
3594 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
3602 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
3605 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
3606 const GstMIKEYPayload *sub;
3607 GstMIKEYPayloadKeyData *pkd;
3610 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
3613 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
3616 if (sub->type != GST_MIKEY_PT_KEY_DATA)
3619 pkd = (GstMIKEYPayloadKeyData *) sub;
3621 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
3623 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
3626 gst_caps_set_simple (caps,
3627 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
3628 "srtp-auth", G_TYPE_STRING, srtp_auth,
3629 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
3630 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
3634 gst_mikey_message_unref (msg);
3640 * Mapping SDP attributes to caps
3642 * prepend 'a-' to IANA registered sdp attributes names
3643 * (ie: not prefixed with 'x-') in order to avoid
3644 * collision with gstreamer standard caps properties names
3647 sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
3649 if (attributes->len > 0) {
3653 s = gst_caps_get_structure (caps, 0);
3655 for (i = 0; i < attributes->len; i++) {
3656 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
3657 gchar *tofree, *key;
3661 /* skip some of the attribute we already handle */
3662 if (!strcmp (key, "fmtp"))
3664 if (!strcmp (key, "rtpmap"))
3666 if (!strcmp (key, "control"))
3668 if (!strcmp (key, "range"))
3670 if (!strcmp (key, "framesize"))
3672 if (g_str_equal (key, "key-mgmt")) {
3673 parse_keymgmt (attr->value, caps);
3677 /* string must be valid UTF8 */
3678 if (!g_utf8_validate (attr->value, -1, NULL))
3681 if (!g_str_has_prefix (key, "x-"))
3682 tofree = key = g_strdup_printf ("a-%s", key);
3686 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
3687 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
3694 default_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp)
3696 GstRTSPMediaPrivate *priv = media->priv;
3699 medias_len = gst_sdp_message_medias_len (sdp);
3700 if (medias_len != priv->streams->len) {
3701 GST_ERROR ("%p: Media has more or less streams than SDP (%d /= %d)", media,
3702 priv->streams->len, medias_len);
3706 for (i = 0; i < medias_len; i++) {
3707 const gchar *proto, *media_type;
3708 const GstSDPMedia *sdp_media = gst_sdp_message_get_media (sdp, i);
3709 GstRTSPStream *stream;
3710 gint j, formats_len;
3711 const gchar *control;
3712 GstRTSPProfile profile, profiles;
3714 stream = g_ptr_array_index (priv->streams, i);
3716 /* TODO: Should we do something with the other SDP information? */
3719 proto = gst_sdp_media_get_proto (sdp_media);
3720 if (proto == NULL) {
3721 GST_ERROR ("%p: SDP media %d has no proto", media, i);
3725 if (g_str_equal (proto, "RTP/AVP")) {
3726 media_type = "application/x-rtp";
3727 profile = GST_RTSP_PROFILE_AVP;
3728 } else if (g_str_equal (proto, "RTP/SAVP")) {
3729 media_type = "application/x-srtp";
3730 profile = GST_RTSP_PROFILE_SAVP;
3731 } else if (g_str_equal (proto, "RTP/AVPF")) {
3732 media_type = "application/x-rtp";
3733 profile = GST_RTSP_PROFILE_AVPF;
3734 } else if (g_str_equal (proto, "RTP/SAVPF")) {
3735 media_type = "application/x-srtp";
3736 profile = GST_RTSP_PROFILE_SAVPF;
3738 GST_ERROR ("%p: unsupported profile '%s' for stream %d", media, proto, i);
3742 profiles = gst_rtsp_stream_get_profiles (stream);
3743 if ((profiles & profile) == 0) {
3744 GST_ERROR ("%p: unsupported profile '%s' for stream %d", media, proto, i);
3748 formats_len = gst_sdp_media_formats_len (sdp_media);
3749 for (j = 0; j < formats_len; j++) {
3754 pt = atoi (gst_sdp_media_get_format (sdp_media, j));
3756 GST_DEBUG (" looking at %d pt: %d", j, pt);
3759 caps = media_to_caps (pt, sdp_media);
3761 GST_WARNING (" skipping pt %d without caps", pt);
3765 /* do some tweaks */
3766 GST_DEBUG ("mapping sdp session level attributes to caps");
3767 sdp_attributes_to_caps (sdp->attributes, caps);
3768 GST_DEBUG ("mapping sdp media level attributes to caps");
3769 sdp_attributes_to_caps (sdp_media->attributes, caps);
3771 s = gst_caps_get_structure (caps, 0);
3772 gst_structure_set_name (s, media_type);
3774 gst_rtsp_stream_set_pt_map (stream, pt, caps);
3775 gst_caps_unref (caps);
3778 control = gst_sdp_media_get_attribute_val (sdp_media, "control");
3780 gst_rtsp_stream_set_control (stream, control);
3788 * gst_rtsp_media_handle_sdp:
3789 * @media: a #GstRTSPMedia
3790 * @sdp: (transfer none): a #GstSDPMessage
3792 * Configure an SDP on @media for receiving streams
3794 * Returns: TRUE on success.
3797 gst_rtsp_media_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp)
3799 GstRTSPMediaPrivate *priv;
3800 GstRTSPMediaClass *klass;
3803 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3804 g_return_val_if_fail (sdp != NULL, FALSE);
3808 g_rec_mutex_lock (&priv->state_lock);
3810 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3812 if (!klass->handle_sdp)
3815 res = klass->handle_sdp (media, sdp);
3817 g_rec_mutex_unlock (&priv->state_lock);
3824 g_rec_mutex_unlock (&priv->state_lock);
3825 GST_ERROR ("no handle_sdp function");
3826 g_critical ("no handle_sdp vmethod function set");
3832 do_set_seqnum (GstRTSPStream * stream)
3835 seq_num = gst_rtsp_stream_get_current_seqnum (stream);
3836 gst_rtsp_stream_set_seqnum_offset (stream, seq_num + 1);
3839 /* call with state_lock */
3841 default_suspend (GstRTSPMedia * media)
3843 GstRTSPMediaPrivate *priv = media->priv;
3844 GstStateChangeReturn ret;
3845 gboolean unblock = FALSE;
3847 switch (priv->suspend_mode) {
3848 case GST_RTSP_SUSPEND_MODE_NONE:
3849 GST_DEBUG ("media %p no suspend", media);
3851 case GST_RTSP_SUSPEND_MODE_PAUSE:
3852 GST_DEBUG ("media %p suspend to PAUSED", media);
3853 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
3854 if (ret == GST_STATE_CHANGE_FAILURE)
3858 case GST_RTSP_SUSPEND_MODE_RESET:
3859 GST_DEBUG ("media %p suspend to NULL", media);
3860 ret = set_target_state (media, GST_STATE_NULL, TRUE);
3861 if (ret == GST_STATE_CHANGE_FAILURE)
3863 /* Because payloader needs to set the sequence number as
3864 * monotonic, we need to preserve the sequence number
3865 * after pause. (otherwise going from pause to play, which
3866 * is actually from NULL to PLAY will create a new sequence
3868 g_ptr_array_foreach (priv->streams, (GFunc) do_set_seqnum, NULL);
3875 /* let the streams do the state changes freely, if any */
3877 media_streams_set_blocked (media, FALSE);
3884 GST_WARNING ("failed changing pipeline's state for media %p", media);
3890 * gst_rtsp_media_suspend:
3891 * @media: a #GstRTSPMedia
3893 * Suspend @media. The state of the pipeline managed by @media is set to
3894 * GST_STATE_NULL but all streams are kept. @media can be prepared again
3895 * with gst_rtsp_media_unsuspend()
3897 * @media must be prepared with gst_rtsp_media_prepare();
3899 * Returns: %TRUE on success.
3902 gst_rtsp_media_suspend (GstRTSPMedia * media)
3904 GstRTSPMediaPrivate *priv = media->priv;
3905 GstRTSPMediaClass *klass;
3907 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3909 GST_FIXME ("suspend for dynamic pipelines needs fixing");
3911 g_rec_mutex_lock (&priv->state_lock);
3912 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
3915 /* don't attempt to suspend when something is busy */
3916 if (priv->n_active > 0)
3919 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3920 if (klass->suspend) {
3921 if (!klass->suspend (media))
3922 goto suspend_failed;
3925 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_SUSPENDED);
3927 g_rec_mutex_unlock (&priv->state_lock);
3934 g_rec_mutex_unlock (&priv->state_lock);
3935 GST_WARNING ("media %p was not prepared", media);
3940 g_rec_mutex_unlock (&priv->state_lock);
3941 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
3942 GST_WARNING ("failed to suspend media %p", media);
3947 /* call with state_lock */
3949 default_unsuspend (GstRTSPMedia * media)
3951 GstRTSPMediaPrivate *priv = media->priv;
3953 switch (priv->suspend_mode) {
3954 case GST_RTSP_SUSPEND_MODE_NONE:
3955 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
3957 case GST_RTSP_SUSPEND_MODE_PAUSE:
3958 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
3960 case GST_RTSP_SUSPEND_MODE_RESET:
3962 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
3963 if (!start_preroll (media))
3965 g_rec_mutex_unlock (&priv->state_lock);
3967 if (!wait_preroll (media))
3968 goto preroll_failed;
3970 g_rec_mutex_lock (&priv->state_lock);
3981 GST_WARNING ("failed to preroll pipeline");
3986 GST_WARNING ("failed to preroll pipeline");
3992 * gst_rtsp_media_unsuspend:
3993 * @media: a #GstRTSPMedia
3995 * Unsuspend @media if it was in a suspended state. This method does nothing
3996 * when the media was not in the suspended state.
3998 * Returns: %TRUE on success.
4001 gst_rtsp_media_unsuspend (GstRTSPMedia * media)
4003 GstRTSPMediaPrivate *priv = media->priv;
4004 GstRTSPMediaClass *klass;
4006 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
4008 g_rec_mutex_lock (&priv->state_lock);
4009 if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
4012 klass = GST_RTSP_MEDIA_GET_CLASS (media);
4013 if (klass->unsuspend) {
4014 if (!klass->unsuspend (media))
4015 goto unsuspend_failed;
4019 g_rec_mutex_unlock (&priv->state_lock);
4026 g_rec_mutex_unlock (&priv->state_lock);
4027 GST_WARNING ("failed to unsuspend media %p", media);
4028 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
4033 /* must be called with state-lock */
4035 media_set_pipeline_state_locked (GstRTSPMedia * media, GstState state)
4037 GstRTSPMediaPrivate *priv = media->priv;
4039 if (state == GST_STATE_NULL) {
4040 gst_rtsp_media_unprepare (media);
4042 GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
4043 set_target_state (media, state, FALSE);
4044 /* when we are buffering, don't update the state yet, this will be done
4045 * when buffering finishes */
4046 if (priv->buffering) {
4047 GST_INFO ("Buffering busy, delay state change");
4049 if (state == GST_STATE_PLAYING)
4050 /* make sure pads are not blocking anymore when going to PLAYING */
4051 media_streams_set_blocked (media, FALSE);
4053 set_state (media, state);
4055 /* and suspend after pause */
4056 if (state == GST_STATE_PAUSED)
4057 gst_rtsp_media_suspend (media);
4063 * gst_rtsp_media_set_pipeline_state:
4064 * @media: a #GstRTSPMedia
4065 * @state: the target state of the pipeline
4067 * Set the state of the pipeline managed by @media to @state
4070 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
4072 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
4074 g_rec_mutex_lock (&media->priv->state_lock);
4075 media_set_pipeline_state_locked (media, state);
4076 g_rec_mutex_unlock (&media->priv->state_lock);
4080 * gst_rtsp_media_set_state:
4081 * @media: a #GstRTSPMedia
4082 * @state: the target state of the media
4083 * @transports: (transfer none) (element-type GstRtspServer.RTSPStreamTransport):
4084 * a #GPtrArray of #GstRTSPStreamTransport pointers
4086 * Set the state of @media to @state and for the transports in @transports.
4088 * @media must be prepared with gst_rtsp_media_prepare();
4090 * Returns: %TRUE on success.
4093 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
4094 GPtrArray * transports)
4096 GstRTSPMediaPrivate *priv;
4098 gboolean activate, deactivate, do_state;
4101 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
4102 g_return_val_if_fail (transports != NULL, FALSE);
4106 g_rec_mutex_lock (&priv->state_lock);
4107 if (priv->status == GST_RTSP_MEDIA_STATUS_ERROR)
4109 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
4110 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
4113 /* NULL and READY are the same */
4114 if (state == GST_STATE_READY)
4115 state = GST_STATE_NULL;
4117 activate = deactivate = FALSE;
4119 GST_INFO ("going to state %s media %p, target state %s",
4120 gst_element_state_get_name (state), media,
4121 gst_element_state_get_name (priv->target_state));
4124 case GST_STATE_NULL:
4125 /* we're going from PLAYING or PAUSED to READY or NULL, deactivate */
4126 if (priv->target_state >= GST_STATE_PAUSED)
4129 case GST_STATE_PAUSED:
4130 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
4131 if (priv->target_state == GST_STATE_PLAYING)
4134 case GST_STATE_PLAYING:
4135 /* we're going to PLAYING, activate */
4141 old_active = priv->n_active;
4143 GST_DEBUG ("%d transports, activate %d, deactivate %d", transports->len,
4144 activate, deactivate);
4145 for (i = 0; i < transports->len; i++) {
4146 GstRTSPStreamTransport *trans;
4148 /* we need a non-NULL entry in the array */
4149 trans = g_ptr_array_index (transports, i);
4154 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
4156 } else if (deactivate) {
4157 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
4162 /* we just activated the first media, do the playing state change */
4163 if (old_active == 0 && activate)
4165 /* if we have no more active media, do the downward state changes */
4166 else if (priv->n_active == 0)
4171 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
4174 if (priv->target_state != state) {
4176 media_set_pipeline_state_locked (media, state);
4178 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
4182 /* remember where we are */
4183 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
4184 old_active != priv->n_active))
4185 collect_media_stats (media);
4187 g_rec_mutex_unlock (&priv->state_lock);
4194 GST_WARNING ("media %p was not prepared", media);
4195 g_rec_mutex_unlock (&priv->state_lock);
4200 GST_WARNING ("media %p in error status while changing to state %d",
4202 if (state == GST_STATE_NULL) {
4203 for (i = 0; i < transports->len; i++) {
4204 GstRTSPStreamTransport *trans;
4206 /* we need a non-NULL entry in the array */
4207 trans = g_ptr_array_index (transports, i);
4211 gst_rtsp_stream_transport_set_active (trans, FALSE);
4215 g_rec_mutex_unlock (&priv->state_lock);
4221 * gst_rtsp_media_set_transport_mode:
4222 * @media: a #GstRTSPMedia
4223 * @mode: the new value
4225 * Sets if the media pipeline can work in PLAY or RECORD mode
4228 gst_rtsp_media_set_transport_mode (GstRTSPMedia * media,
4229 GstRTSPTransportMode mode)
4231 GstRTSPMediaPrivate *priv;
4233 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
4237 g_mutex_lock (&priv->lock);
4238 priv->transport_mode = mode;
4239 g_mutex_unlock (&priv->lock);
4243 * gst_rtsp_media_get_transport_mode:
4244 * @media: a #GstRTSPMedia
4246 * Check if the pipeline for @media can be used for PLAY or RECORD methods.
4248 * Returns: The transport mode.
4250 GstRTSPTransportMode
4251 gst_rtsp_media_get_transport_mode (GstRTSPMedia * media)
4253 GstRTSPMediaPrivate *priv;
4254 GstRTSPTransportMode res;
4256 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
4260 g_mutex_lock (&priv->lock);
4261 res = priv->transport_mode;
4262 g_mutex_unlock (&priv->lock);