b827c7279bf458b7332467cb3011dcc45b3104fa
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-media.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /**
20  * SECTION:rtsp-media
21  * @short_description: The media pipeline
22  * @see_also: #GstRTSPMediaFactory, #GstRTSPStream, #GstRTSPSession,
23  *     #GstRTSPSessionMedia
24  *
25  * a #GstRTSPMedia contains the complete GStreamer pipeline to manage the
26  * streaming to the clients. The actual data transfer is done by the
27  * #GstRTSPStream objects that are created and exposed by the #GstRTSPMedia.
28  *
29  * The #GstRTSPMedia is usually created from a #GstRTSPMediaFactory when the
30  * client does a DESCRIBE or SETUP of a resource.
31  *
32  * A media is created with gst_rtsp_media_new() that takes the element that will
33  * provide the streaming elements. For each of the streams, a new #GstRTSPStream
34  * object needs to be made with the gst_rtsp_media_create_stream() which takes
35  * the payloader element and the source pad that produces the RTP stream.
36  *
37  * The pipeline of the media is set to PAUSED with gst_rtsp_media_prepare(). The
38  * prepare method will add rtpbin and sinks and sources to send and receive RTP
39  * and RTCP packets from the clients. Each stream srcpad is connected to an
40  * input into the internal rtpbin.
41  *
42  * It is also possible to dynamically create #GstRTSPStream objects during the
43  * prepare phase. With gst_rtsp_media_get_status() you can check the status of
44  * the prepare phase.
45  *
46  * After the media is prepared, it is ready for streaming. It will usually be
47  * managed in a session with gst_rtsp_session_manage_media(). See
48  * #GstRTSPSession and #GstRTSPSessionMedia.
49  *
50  * The state of the media can be controlled with gst_rtsp_media_set_state ().
51  * Seeking can be done with gst_rtsp_media_seek().
52  *
53  * With gst_rtsp_media_unprepare() the pipeline is stopped and shut down. When
54  * gst_rtsp_media_set_eos_shutdown() an EOS will be sent to the pipeline to
55  * cleanly shut down.
56  *
57  * With gst_rtsp_media_set_shared(), the media can be shared between multiple
58  * clients. With gst_rtsp_media_set_reusable() you can control if the pipeline
59  * can be prepared again after an unprepare.
60  *
61  * Last reviewed on 2013-07-11 (1.0.0)
62  */
63
64 #include <string.h>
65 #include <stdlib.h>
66
67 #include <gst/app/gstappsrc.h>
68 #include <gst/app/gstappsink.h>
69
70 #include "rtsp-media.h"
71
72 #define GST_RTSP_MEDIA_GET_PRIVATE(obj)  \
73      (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_MEDIA, GstRTSPMediaPrivate))
74
75 struct _GstRTSPMediaPrivate
76 {
77   GMutex lock;
78   GCond cond;
79
80   /* protected by lock */
81   GstRTSPPermissions *permissions;
82   gboolean shared;
83   gboolean suspend_mode;
84   gboolean reusable;
85   GstRTSPProfile profiles;
86   GstRTSPLowerTrans protocols;
87   gboolean reused;
88   gboolean eos_shutdown;
89   guint buffer_size;
90   GstRTSPAddressPool *pool;
91   gboolean blocked;
92
93   GstElement *element;
94   GRecMutex state_lock;         /* locking order: state lock, lock */
95   GPtrArray *streams;           /* protected by lock */
96   GList *dynamic;               /* protected by lock */
97   GstRTSPMediaStatus status;    /* protected by lock */
98   gint prepare_count;
99   gint n_active;
100   gboolean adding;
101
102   /* the pipeline for the media */
103   GstElement *pipeline;
104   GstElement *fakesink;         /* protected by lock */
105   GSource *source;
106   guint id;
107   GstRTSPThread *thread;
108
109   gboolean time_provider;
110   GstNetTimeProvider *nettime;
111
112   gboolean is_live;
113   gboolean seekable;
114   gboolean buffering;
115   GstState target_state;
116
117   /* RTP session manager */
118   GstElement *rtpbin;
119
120   /* the range of media */
121   GstRTSPTimeRange range;       /* protected by lock */
122   GstClockTime range_start;
123   GstClockTime range_stop;
124 };
125
126 #define DEFAULT_SHARED          FALSE
127 #define DEFAULT_SUSPEND_MODE    GST_RTSP_SUSPEND_MODE_NONE
128 #define DEFAULT_REUSABLE        FALSE
129 #define DEFAULT_PROFILES        GST_RTSP_PROFILE_AVP
130 #define DEFAULT_PROTOCOLS       GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
131                                         GST_RTSP_LOWER_TRANS_TCP
132 #define DEFAULT_EOS_SHUTDOWN    FALSE
133 #define DEFAULT_BUFFER_SIZE     0x80000
134 #define DEFAULT_TIME_PROVIDER   FALSE
135
136 /* define to dump received RTCP packets */
137 #undef DUMP_STATS
138
139 enum
140 {
141   PROP_0,
142   PROP_SHARED,
143   PROP_SUSPEND_MODE,
144   PROP_REUSABLE,
145   PROP_PROFILES,
146   PROP_PROTOCOLS,
147   PROP_EOS_SHUTDOWN,
148   PROP_BUFFER_SIZE,
149   PROP_ELEMENT,
150   PROP_TIME_PROVIDER,
151   PROP_LAST
152 };
153
154 enum
155 {
156   SIGNAL_NEW_STREAM,
157   SIGNAL_REMOVED_STREAM,
158   SIGNAL_PREPARED,
159   SIGNAL_UNPREPARED,
160   SIGNAL_TARGET_STATE,
161   SIGNAL_NEW_STATE,
162   SIGNAL_LAST
163 };
164
165 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
166 #define GST_CAT_DEFAULT rtsp_media_debug
167
168 static void gst_rtsp_media_get_property (GObject * object, guint propid,
169     GValue * value, GParamSpec * pspec);
170 static void gst_rtsp_media_set_property (GObject * object, guint propid,
171     const GValue * value, GParamSpec * pspec);
172 static void gst_rtsp_media_finalize (GObject * obj);
173
174 static gboolean default_handle_message (GstRTSPMedia * media,
175     GstMessage * message);
176 static void finish_unprepare (GstRTSPMedia * media);
177 static gboolean default_unprepare (GstRTSPMedia * media);
178 static gboolean default_convert_range (GstRTSPMedia * media,
179     GstRTSPTimeRange * range, GstRTSPRangeUnit unit);
180 static gboolean default_query_position (GstRTSPMedia * media,
181     gint64 * position);
182 static gboolean default_query_stop (GstRTSPMedia * media, gint64 * stop);
183 static GstElement *default_create_rtpbin (GstRTSPMedia * media);
184 static gboolean default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
185     GstSDPInfo * info);
186
187 static gboolean wait_preroll (GstRTSPMedia * media);
188
189 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
190
191 #define C_ENUM(v) ((gint) v)
192
193 #define GST_TYPE_RTSP_SUSPEND_MODE (gst_rtsp_suspend_mode_get_type())
194 GType
195 gst_rtsp_suspend_mode_get_type (void)
196 {
197   static gsize id = 0;
198   static const GEnumValue values[] = {
199     {C_ENUM (GST_RTSP_SUSPEND_MODE_NONE), "GST_RTSP_SUSPEND_MODE_NONE", "none"},
200     {C_ENUM (GST_RTSP_SUSPEND_MODE_PAUSE), "GST_RTSP_SUSPEND_MODE_PAUSE",
201         "pause"},
202     {C_ENUM (GST_RTSP_SUSPEND_MODE_RESET), "GST_RTSP_SUSPEND_MODE_RESET",
203         "reset"},
204     {0, NULL, NULL}
205   };
206
207   if (g_once_init_enter (&id)) {
208     GType tmp = g_enum_register_static ("GstRTSPSuspendMode", values);
209     g_once_init_leave (&id, tmp);
210   }
211   return (GType) id;
212 }
213
214 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
215
216 static void
217 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
218 {
219   GObjectClass *gobject_class;
220
221   g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
222
223   gobject_class = G_OBJECT_CLASS (klass);
224
225   gobject_class->get_property = gst_rtsp_media_get_property;
226   gobject_class->set_property = gst_rtsp_media_set_property;
227   gobject_class->finalize = gst_rtsp_media_finalize;
228
229   g_object_class_install_property (gobject_class, PROP_SHARED,
230       g_param_spec_boolean ("shared", "Shared",
231           "If this media pipeline can be shared", DEFAULT_SHARED,
232           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233
234   g_object_class_install_property (gobject_class, PROP_SUSPEND_MODE,
235       g_param_spec_enum ("suspend-mode", "Suspend Mode",
236           "How to suspend the media in PAUSED", GST_TYPE_RTSP_SUSPEND_MODE,
237           DEFAULT_SUSPEND_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238
239   g_object_class_install_property (gobject_class, PROP_REUSABLE,
240       g_param_spec_boolean ("reusable", "Reusable",
241           "If this media pipeline can be reused after an unprepare",
242           DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243
244   g_object_class_install_property (gobject_class, PROP_PROFILES,
245       g_param_spec_flags ("profiles", "Profiles",
246           "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
247           DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248
249   g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
250       g_param_spec_flags ("protocols", "Protocols",
251           "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
252           DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
253
254   g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
255       g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
256           "Send an EOS event to the pipeline before unpreparing",
257           DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258
259   g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
260       g_param_spec_uint ("buffer-size", "Buffer Size",
261           "The kernel UDP buffer size to use", 0, G_MAXUINT,
262           DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263
264   g_object_class_install_property (gobject_class, PROP_ELEMENT,
265       g_param_spec_object ("element", "The Element",
266           "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
267           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
268
269   g_object_class_install_property (gobject_class, PROP_TIME_PROVIDER,
270       g_param_spec_boolean ("time-provider", "Time Provider",
271           "Use a NetTimeProvider for clients",
272           DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
273
274   gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
275       g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
276       G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
277       g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
278
279   gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
280       g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
281       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
282       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
283       GST_TYPE_RTSP_STREAM);
284
285   gst_rtsp_media_signals[SIGNAL_PREPARED] =
286       g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
287       G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
288       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
289
290   gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
291       g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
292       G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
293       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
294
295   gst_rtsp_media_signals[SIGNAL_TARGET_STATE] =
296       g_signal_new ("target-state", G_TYPE_FROM_CLASS (klass),
297       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL,
298       NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
299
300   gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
301       g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
302       G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
303       g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
304
305   GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
306
307   klass->handle_message = default_handle_message;
308   klass->unprepare = default_unprepare;
309   klass->convert_range = default_convert_range;
310   klass->query_position = default_query_position;
311   klass->query_stop = default_query_stop;
312   klass->create_rtpbin = default_create_rtpbin;
313   klass->setup_sdp = default_setup_sdp;
314 }
315
316 static void
317 gst_rtsp_media_init (GstRTSPMedia * media)
318 {
319   GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
320
321   media->priv = priv;
322
323   priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
324   g_mutex_init (&priv->lock);
325   g_cond_init (&priv->cond);
326   g_rec_mutex_init (&priv->state_lock);
327
328   priv->shared = DEFAULT_SHARED;
329   priv->suspend_mode = DEFAULT_SUSPEND_MODE;
330   priv->reusable = DEFAULT_REUSABLE;
331   priv->profiles = DEFAULT_PROFILES;
332   priv->protocols = DEFAULT_PROTOCOLS;
333   priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
334   priv->buffer_size = DEFAULT_BUFFER_SIZE;
335   priv->time_provider = DEFAULT_TIME_PROVIDER;
336 }
337
338 static void
339 gst_rtsp_media_finalize (GObject * obj)
340 {
341   GstRTSPMediaPrivate *priv;
342   GstRTSPMedia *media;
343
344   media = GST_RTSP_MEDIA (obj);
345   priv = media->priv;
346
347   GST_INFO ("finalize media %p", media);
348
349   if (priv->permissions)
350     gst_rtsp_permissions_unref (priv->permissions);
351
352   g_ptr_array_unref (priv->streams);
353
354   g_list_free_full (priv->dynamic, gst_object_unref);
355
356   if (priv->pipeline)
357     gst_object_unref (priv->pipeline);
358   if (priv->nettime)
359     gst_object_unref (priv->nettime);
360   gst_object_unref (priv->element);
361   if (priv->pool)
362     g_object_unref (priv->pool);
363   g_mutex_clear (&priv->lock);
364   g_cond_clear (&priv->cond);
365   g_rec_mutex_clear (&priv->state_lock);
366
367   G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
368 }
369
370 static void
371 gst_rtsp_media_get_property (GObject * object, guint propid,
372     GValue * value, GParamSpec * pspec)
373 {
374   GstRTSPMedia *media = GST_RTSP_MEDIA (object);
375
376   switch (propid) {
377     case PROP_ELEMENT:
378       g_value_set_object (value, media->priv->element);
379       break;
380     case PROP_SHARED:
381       g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
382       break;
383     case PROP_SUSPEND_MODE:
384       g_value_set_enum (value, gst_rtsp_media_get_suspend_mode (media));
385       break;
386     case PROP_REUSABLE:
387       g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
388       break;
389     case PROP_PROFILES:
390       g_value_set_flags (value, gst_rtsp_media_get_profiles (media));
391       break;
392     case PROP_PROTOCOLS:
393       g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
394       break;
395     case PROP_EOS_SHUTDOWN:
396       g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
397       break;
398     case PROP_BUFFER_SIZE:
399       g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
400       break;
401     case PROP_TIME_PROVIDER:
402       g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
403       break;
404     default:
405       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
406   }
407 }
408
409 static void
410 gst_rtsp_media_set_property (GObject * object, guint propid,
411     const GValue * value, GParamSpec * pspec)
412 {
413   GstRTSPMedia *media = GST_RTSP_MEDIA (object);
414
415   switch (propid) {
416     case PROP_ELEMENT:
417       media->priv->element = g_value_get_object (value);
418       gst_object_ref_sink (media->priv->element);
419       break;
420     case PROP_SHARED:
421       gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
422       break;
423     case PROP_SUSPEND_MODE:
424       gst_rtsp_media_set_suspend_mode (media, g_value_get_enum (value));
425       break;
426     case PROP_REUSABLE:
427       gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
428       break;
429     case PROP_PROFILES:
430       gst_rtsp_media_set_profiles (media, g_value_get_flags (value));
431       break;
432     case PROP_PROTOCOLS:
433       gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
434       break;
435     case PROP_EOS_SHUTDOWN:
436       gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
437       break;
438     case PROP_BUFFER_SIZE:
439       gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
440       break;
441     case PROP_TIME_PROVIDER:
442       gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
443       break;
444     default:
445       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
446   }
447 }
448
449 static gboolean
450 default_query_position (GstRTSPMedia * media, gint64 * position)
451 {
452   return gst_element_query_position (media->priv->pipeline, GST_FORMAT_TIME,
453       position);
454 }
455
456 static gboolean
457 default_query_stop (GstRTSPMedia * media, gint64 * stop)
458 {
459   GstQuery *query;
460   gboolean res;
461
462   query = gst_query_new_segment (GST_FORMAT_TIME);
463   if ((res = gst_element_query (media->priv->pipeline, query))) {
464     GstFormat format;
465     gst_query_parse_segment (query, NULL, &format, NULL, stop);
466     if (format != GST_FORMAT_TIME)
467       *stop = -1;
468   }
469   gst_query_unref (query);
470   return res;
471 }
472
473 static GstElement *
474 default_create_rtpbin (GstRTSPMedia * media)
475 {
476   GstElement *rtpbin;
477
478   rtpbin = gst_element_factory_make ("rtpbin", NULL);
479
480   return rtpbin;
481 }
482
483 /* must be called with state lock */
484 static void
485 collect_media_stats (GstRTSPMedia * media)
486 {
487   GstRTSPMediaPrivate *priv = media->priv;
488   gint64 position, stop;
489
490   if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
491       priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
492     return;
493
494   priv->range.unit = GST_RTSP_RANGE_NPT;
495
496   GST_INFO ("collect media stats");
497
498   if (priv->is_live) {
499     priv->range.min.type = GST_RTSP_TIME_NOW;
500     priv->range.min.seconds = -1;
501     priv->range_start = -1;
502     priv->range.max.type = GST_RTSP_TIME_END;
503     priv->range.max.seconds = -1;
504     priv->range_stop = -1;
505   } else {
506     GstRTSPMediaClass *klass;
507     gboolean ret;
508
509     klass = GST_RTSP_MEDIA_GET_CLASS (media);
510
511     /* get the position */
512     ret = FALSE;
513     if (klass->query_position)
514       ret = klass->query_position (media, &position);
515
516     if (!ret) {
517       GST_INFO ("position query failed");
518       position = 0;
519     }
520
521     /* get the current segment stop */
522     ret = FALSE;
523     if (klass->query_stop)
524       ret = klass->query_stop (media, &stop);
525
526     if (!ret) {
527       GST_INFO ("stop query failed");
528       stop = -1;
529     }
530
531     GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
532         GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
533
534     if (position == -1) {
535       priv->range.min.type = GST_RTSP_TIME_NOW;
536       priv->range.min.seconds = -1;
537       priv->range_start = -1;
538     } else {
539       priv->range.min.type = GST_RTSP_TIME_SECONDS;
540       priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
541       priv->range_start = position;
542     }
543     if (stop == -1) {
544       priv->range.max.type = GST_RTSP_TIME_END;
545       priv->range.max.seconds = -1;
546       priv->range_stop = -1;
547     } else {
548       priv->range.max.type = GST_RTSP_TIME_SECONDS;
549       priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
550       priv->range_stop = stop;
551     }
552   }
553 }
554
555 /**
556  * gst_rtsp_media_new:
557  * @element: (transfer full): a #GstElement
558  *
559  * Create a new #GstRTSPMedia instance. @element is the bin element that
560  * provides the different streams. The #GstRTSPMedia object contains the
561  * element to produce RTP data for one or more related (audio/video/..)
562  * streams.
563  *
564  * Ownership is taken of @element.
565  *
566  * Returns: (transfer full): a new #GstRTSPMedia object.
567  */
568 GstRTSPMedia *
569 gst_rtsp_media_new (GstElement * element)
570 {
571   GstRTSPMedia *result;
572
573   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
574
575   result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
576
577   return result;
578 }
579
580 /**
581  * gst_rtsp_media_get_element:
582  * @media: a #GstRTSPMedia
583  *
584  * Get the element that was used when constructing @media.
585  *
586  * Returns: (transfer full): a #GstElement. Unref after usage.
587  */
588 GstElement *
589 gst_rtsp_media_get_element (GstRTSPMedia * media)
590 {
591   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
592
593   return gst_object_ref (media->priv->element);
594 }
595
596 /**
597  * gst_rtsp_media_take_pipeline:
598  * @media: a #GstRTSPMedia
599  * @pipeline: (transfer full): a #GstPipeline
600  *
601  * Set @pipeline as the #GstPipeline for @media. Ownership is
602  * taken of @pipeline.
603  */
604 void
605 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
606 {
607   GstRTSPMediaPrivate *priv;
608   GstElement *old;
609   GstNetTimeProvider *nettime;
610
611   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
612   g_return_if_fail (GST_IS_PIPELINE (pipeline));
613
614   priv = media->priv;
615
616   g_mutex_lock (&priv->lock);
617   old = priv->pipeline;
618   priv->pipeline = GST_ELEMENT_CAST (pipeline);
619   nettime = priv->nettime;
620   priv->nettime = NULL;
621   g_mutex_unlock (&priv->lock);
622
623   if (old)
624     gst_object_unref (old);
625
626   if (nettime)
627     gst_object_unref (nettime);
628
629   gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
630 }
631
632 /**
633  * gst_rtsp_media_set_permissions:
634  * @media: a #GstRTSPMedia
635  * @permissions: (transfer none): a #GstRTSPPermissions
636  *
637  * Set @permissions on @media.
638  */
639 void
640 gst_rtsp_media_set_permissions (GstRTSPMedia * media,
641     GstRTSPPermissions * permissions)
642 {
643   GstRTSPMediaPrivate *priv;
644
645   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
646
647   priv = media->priv;
648
649   g_mutex_lock (&priv->lock);
650   if (priv->permissions)
651     gst_rtsp_permissions_unref (priv->permissions);
652   if ((priv->permissions = permissions))
653     gst_rtsp_permissions_ref (permissions);
654   g_mutex_unlock (&priv->lock);
655 }
656
657 /**
658  * gst_rtsp_media_get_permissions:
659  * @media: a #GstRTSPMedia
660  *
661  * Get the permissions object from @media.
662  *
663  * Returns: (transfer full): a #GstRTSPPermissions object, unref after usage.
664  */
665 GstRTSPPermissions *
666 gst_rtsp_media_get_permissions (GstRTSPMedia * media)
667 {
668   GstRTSPMediaPrivate *priv;
669   GstRTSPPermissions *result;
670
671   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
672
673   priv = media->priv;
674
675   g_mutex_lock (&priv->lock);
676   if ((result = priv->permissions))
677     gst_rtsp_permissions_ref (result);
678   g_mutex_unlock (&priv->lock);
679
680   return result;
681 }
682
683 /**
684  * gst_rtsp_media_set_suspend_mode:
685  * @media: a #GstRTSPMedia
686  * @mode: the new #GstRTSPSuspendMode
687  *
688  * Control how @ media will be suspended after the SDP has been generated and
689  * after a PAUSE request has been performed.
690  *
691  * Media must be unprepared when setting the suspend mode.
692  */
693 void
694 gst_rtsp_media_set_suspend_mode (GstRTSPMedia * media, GstRTSPSuspendMode mode)
695 {
696   GstRTSPMediaPrivate *priv;
697
698   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
699
700   priv = media->priv;
701
702   g_rec_mutex_lock (&priv->state_lock);
703   if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
704     goto was_prepared;
705   priv->suspend_mode = mode;
706   g_rec_mutex_unlock (&priv->state_lock);
707
708   return;
709
710   /* ERRORS */
711 was_prepared:
712   {
713     GST_WARNING ("media %p was prepared", media);
714     g_rec_mutex_unlock (&priv->state_lock);
715   }
716 }
717
718 /**
719  * gst_rtsp_media_get_suspend_mode:
720  * @media: a #GstRTSPMedia
721  *
722  * Get how @media will be suspended.
723  *
724  * Returns: #GstRTSPSuspendMode.
725  */
726 GstRTSPSuspendMode
727 gst_rtsp_media_get_suspend_mode (GstRTSPMedia * media)
728 {
729   GstRTSPMediaPrivate *priv;
730   GstRTSPSuspendMode res;
731
732   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_SUSPEND_MODE_NONE);
733
734   priv = media->priv;
735
736   g_rec_mutex_lock (&priv->state_lock);
737   res = priv->suspend_mode;
738   g_rec_mutex_unlock (&priv->state_lock);
739
740   return res;
741 }
742
743 /**
744  * gst_rtsp_media_set_shared:
745  * @media: a #GstRTSPMedia
746  * @shared: the new value
747  *
748  * Set or unset if the pipeline for @media can be shared will multiple clients.
749  * When @shared is %TRUE, client requests for this media will share the media
750  * pipeline.
751  */
752 void
753 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
754 {
755   GstRTSPMediaPrivate *priv;
756
757   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
758
759   priv = media->priv;
760
761   g_mutex_lock (&priv->lock);
762   priv->shared = shared;
763   g_mutex_unlock (&priv->lock);
764 }
765
766 /**
767  * gst_rtsp_media_is_shared:
768  * @media: a #GstRTSPMedia
769  *
770  * Check if the pipeline for @media can be shared between multiple clients.
771  *
772  * Returns: %TRUE if the media can be shared between clients.
773  */
774 gboolean
775 gst_rtsp_media_is_shared (GstRTSPMedia * media)
776 {
777   GstRTSPMediaPrivate *priv;
778   gboolean res;
779
780   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
781
782   priv = media->priv;
783
784   g_mutex_lock (&priv->lock);
785   res = priv->shared;
786   g_mutex_unlock (&priv->lock);
787
788   return res;
789 }
790
791 /**
792  * gst_rtsp_media_set_reusable:
793  * @media: a #GstRTSPMedia
794  * @reusable: the new value
795  *
796  * Set or unset if the pipeline for @media can be reused after the pipeline has
797  * been unprepared.
798  */
799 void
800 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
801 {
802   GstRTSPMediaPrivate *priv;
803
804   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
805
806   priv = media->priv;
807
808   g_mutex_lock (&priv->lock);
809   priv->reusable = reusable;
810   g_mutex_unlock (&priv->lock);
811 }
812
813 /**
814  * gst_rtsp_media_is_reusable:
815  * @media: a #GstRTSPMedia
816  *
817  * Check if the pipeline for @media can be reused after an unprepare.
818  *
819  * Returns: %TRUE if the media can be reused
820  */
821 gboolean
822 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
823 {
824   GstRTSPMediaPrivate *priv;
825   gboolean res;
826
827   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
828
829   priv = media->priv;
830
831   g_mutex_lock (&priv->lock);
832   res = priv->reusable;
833   g_mutex_unlock (&priv->lock);
834
835   return res;
836 }
837
838 static void
839 do_set_profiles (GstRTSPStream * stream, GstRTSPProfile * profiles)
840 {
841   gst_rtsp_stream_set_profiles (stream, *profiles);
842 }
843
844 /**
845  * gst_rtsp_media_set_profiles:
846  * @media: a #GstRTSPMedia
847  * @profiles: the new flags
848  *
849  * Configure the allowed lower transport for @media.
850  */
851 void
852 gst_rtsp_media_set_profiles (GstRTSPMedia * media, GstRTSPProfile profiles)
853 {
854   GstRTSPMediaPrivate *priv;
855
856   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
857
858   priv = media->priv;
859
860   g_mutex_lock (&priv->lock);
861   priv->profiles = profiles;
862   g_ptr_array_foreach (priv->streams, (GFunc) do_set_profiles, &profiles);
863   g_mutex_unlock (&priv->lock);
864 }
865
866 /**
867  * gst_rtsp_media_get_profiles:
868  * @media: a #GstRTSPMedia
869  *
870  * Get the allowed profiles of @media.
871  *
872  * Returns: a #GstRTSPProfile
873  */
874 GstRTSPProfile
875 gst_rtsp_media_get_profiles (GstRTSPMedia * media)
876 {
877   GstRTSPMediaPrivate *priv;
878   GstRTSPProfile res;
879
880   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_PROFILE_UNKNOWN);
881
882   priv = media->priv;
883
884   g_mutex_lock (&priv->lock);
885   res = priv->profiles;
886   g_mutex_unlock (&priv->lock);
887
888   return res;
889 }
890
891 static void
892 do_set_protocols (GstRTSPStream * stream, GstRTSPLowerTrans * protocols)
893 {
894   gst_rtsp_stream_set_protocols (stream, *protocols);
895 }
896
897 /**
898  * gst_rtsp_media_set_protocols:
899  * @media: a #GstRTSPMedia
900  * @protocols: the new flags
901  *
902  * Configure the allowed lower transport for @media.
903  */
904 void
905 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
906 {
907   GstRTSPMediaPrivate *priv;
908
909   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
910
911   priv = media->priv;
912
913   g_mutex_lock (&priv->lock);
914   priv->protocols = protocols;
915   g_ptr_array_foreach (priv->streams, (GFunc) do_set_protocols, &protocols);
916   g_mutex_unlock (&priv->lock);
917 }
918
919 /**
920  * gst_rtsp_media_get_protocols:
921  * @media: a #GstRTSPMedia
922  *
923  * Get the allowed protocols of @media.
924  *
925  * Returns: a #GstRTSPLowerTrans
926  */
927 GstRTSPLowerTrans
928 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
929 {
930   GstRTSPMediaPrivate *priv;
931   GstRTSPLowerTrans res;
932
933   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
934       GST_RTSP_LOWER_TRANS_UNKNOWN);
935
936   priv = media->priv;
937
938   g_mutex_lock (&priv->lock);
939   res = priv->protocols;
940   g_mutex_unlock (&priv->lock);
941
942   return res;
943 }
944
945 /**
946  * gst_rtsp_media_set_eos_shutdown:
947  * @media: a #GstRTSPMedia
948  * @eos_shutdown: the new value
949  *
950  * Set or unset if an EOS event will be sent to the pipeline for @media before
951  * it is unprepared.
952  */
953 void
954 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
955 {
956   GstRTSPMediaPrivate *priv;
957
958   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
959
960   priv = media->priv;
961
962   g_mutex_lock (&priv->lock);
963   priv->eos_shutdown = eos_shutdown;
964   g_mutex_unlock (&priv->lock);
965 }
966
967 /**
968  * gst_rtsp_media_is_eos_shutdown:
969  * @media: a #GstRTSPMedia
970  *
971  * Check if the pipeline for @media will send an EOS down the pipeline before
972  * unpreparing.
973  *
974  * Returns: %TRUE if the media will send EOS before unpreparing.
975  */
976 gboolean
977 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
978 {
979   GstRTSPMediaPrivate *priv;
980   gboolean res;
981
982   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
983
984   priv = media->priv;
985
986   g_mutex_lock (&priv->lock);
987   res = priv->eos_shutdown;
988   g_mutex_unlock (&priv->lock);
989
990   return res;
991 }
992
993 /**
994  * gst_rtsp_media_set_buffer_size:
995  * @media: a #GstRTSPMedia
996  * @size: the new value
997  *
998  * Set the kernel UDP buffer size.
999  */
1000 void
1001 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
1002 {
1003   GstRTSPMediaPrivate *priv;
1004
1005   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1006
1007   GST_LOG_OBJECT (media, "set buffer size %u", size);
1008
1009   priv = media->priv;
1010
1011   g_mutex_lock (&priv->lock);
1012   priv->buffer_size = size;
1013   g_mutex_unlock (&priv->lock);
1014 }
1015
1016 /**
1017  * gst_rtsp_media_get_buffer_size:
1018  * @media: a #GstRTSPMedia
1019  *
1020  * Get the kernel UDP buffer size.
1021  *
1022  * Returns: the kernel UDP buffer size.
1023  */
1024 guint
1025 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
1026 {
1027   GstRTSPMediaPrivate *priv;
1028   guint res;
1029
1030   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1031
1032   priv = media->priv;
1033
1034   g_mutex_unlock (&priv->lock);
1035   res = priv->buffer_size;
1036   g_mutex_unlock (&priv->lock);
1037
1038   return res;
1039 }
1040
1041 /**
1042  * gst_rtsp_media_use_time_provider:
1043  * @media: a #GstRTSPMedia
1044  * @time_provider: if a #GstNetTimeProvider should be used
1045  *
1046  * Set @media to provide a #GstNetTimeProvider.
1047  */
1048 void
1049 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
1050 {
1051   GstRTSPMediaPrivate *priv;
1052
1053   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1054
1055   priv = media->priv;
1056
1057   g_mutex_lock (&priv->lock);
1058   priv->time_provider = time_provider;
1059   g_mutex_unlock (&priv->lock);
1060 }
1061
1062 /**
1063  * gst_rtsp_media_is_time_provider:
1064  * @media: a #GstRTSPMedia
1065  *
1066  * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
1067  *
1068  * Use gst_rtsp_media_get_time_provider() to get the network clock.
1069  *
1070  * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
1071  */
1072 gboolean
1073 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
1074 {
1075   GstRTSPMediaPrivate *priv;
1076   gboolean res;
1077
1078   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1079
1080   priv = media->priv;
1081
1082   g_mutex_unlock (&priv->lock);
1083   res = priv->time_provider;
1084   g_mutex_unlock (&priv->lock);
1085
1086   return res;
1087 }
1088
1089 /**
1090  * gst_rtsp_media_set_address_pool:
1091  * @media: a #GstRTSPMedia
1092  * @pool: (transfer none): a #GstRTSPAddressPool
1093  *
1094  * configure @pool to be used as the address pool of @media.
1095  */
1096 void
1097 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
1098     GstRTSPAddressPool * pool)
1099 {
1100   GstRTSPMediaPrivate *priv;
1101   GstRTSPAddressPool *old;
1102
1103   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1104
1105   priv = media->priv;
1106
1107   GST_LOG_OBJECT (media, "set address pool %p", pool);
1108
1109   g_mutex_lock (&priv->lock);
1110   if ((old = priv->pool) != pool)
1111     priv->pool = pool ? g_object_ref (pool) : NULL;
1112   else
1113     old = NULL;
1114   g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
1115       pool);
1116   g_mutex_unlock (&priv->lock);
1117
1118   if (old)
1119     g_object_unref (old);
1120 }
1121
1122 /**
1123  * gst_rtsp_media_get_address_pool:
1124  * @media: a #GstRTSPMedia
1125  *
1126  * Get the #GstRTSPAddressPool used as the address pool of @media.
1127  *
1128  * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
1129  * usage.
1130  */
1131 GstRTSPAddressPool *
1132 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
1133 {
1134   GstRTSPMediaPrivate *priv;
1135   GstRTSPAddressPool *result;
1136
1137   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1138
1139   priv = media->priv;
1140
1141   g_mutex_lock (&priv->lock);
1142   if ((result = priv->pool))
1143     g_object_ref (result);
1144   g_mutex_unlock (&priv->lock);
1145
1146   return result;
1147 }
1148
1149 /**
1150  * gst_rtsp_media_collect_streams:
1151  * @media: a #GstRTSPMedia
1152  *
1153  * Find all payloader elements, they should be named pay\%d in the
1154  * element of @media, and create #GstRTSPStreams for them.
1155  *
1156  * Collect all dynamic elements, named dynpay\%d, and add them to
1157  * the list of dynamic elements.
1158  */
1159 void
1160 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
1161 {
1162   GstRTSPMediaPrivate *priv;
1163   GstElement *element, *elem;
1164   GstPad *pad;
1165   gint i;
1166   gboolean have_elem;
1167
1168   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1169
1170   priv = media->priv;
1171   element = priv->element;
1172
1173   have_elem = TRUE;
1174   for (i = 0; have_elem; i++) {
1175     gchar *name;
1176
1177     have_elem = FALSE;
1178
1179     name = g_strdup_printf ("pay%d", i);
1180     if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1181       GST_INFO ("found stream %d with payloader %p", i, elem);
1182
1183       /* take the pad of the payloader */
1184       pad = gst_element_get_static_pad (elem, "src");
1185       /* create the stream */
1186       gst_rtsp_media_create_stream (media, elem, pad);
1187       gst_object_unref (pad);
1188       gst_object_unref (elem);
1189
1190       have_elem = TRUE;
1191     }
1192     g_free (name);
1193
1194     name = g_strdup_printf ("dynpay%d", i);
1195     if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1196       /* a stream that will dynamically create pads to provide RTP packets */
1197       GST_INFO ("found dynamic element %d, %p", i, elem);
1198
1199       g_mutex_lock (&priv->lock);
1200       priv->dynamic = g_list_prepend (priv->dynamic, elem);
1201       g_mutex_unlock (&priv->lock);
1202
1203       have_elem = TRUE;
1204     }
1205     g_free (name);
1206   }
1207 }
1208
1209 /**
1210  * gst_rtsp_media_create_stream:
1211  * @media: a #GstRTSPMedia
1212  * @payloader: a #GstElement
1213  * @srcpad: a source #GstPad
1214  *
1215  * Create a new stream in @media that provides RTP data on @srcpad.
1216  * @srcpad should be a pad of an element inside @media->element.
1217  *
1218  * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1219  * as @media exists.
1220  */
1221 GstRTSPStream *
1222 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1223     GstPad * pad)
1224 {
1225   GstRTSPMediaPrivate *priv;
1226   GstRTSPStream *stream;
1227   GstPad *srcpad;
1228   gchar *name;
1229   gint idx;
1230
1231   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1232   g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1233   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1234   g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
1235
1236   priv = media->priv;
1237
1238   g_mutex_lock (&priv->lock);
1239   idx = priv->streams->len;
1240
1241   GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1242
1243   name = g_strdup_printf ("src_%u", idx);
1244   srcpad = gst_ghost_pad_new (name, pad);
1245   gst_pad_set_active (srcpad, TRUE);
1246   gst_element_add_pad (priv->element, srcpad);
1247   g_free (name);
1248
1249   stream = gst_rtsp_stream_new (idx, payloader, srcpad);
1250   if (priv->pool)
1251     gst_rtsp_stream_set_address_pool (stream, priv->pool);
1252   gst_rtsp_stream_set_profiles (stream, priv->profiles);
1253   gst_rtsp_stream_set_protocols (stream, priv->protocols);
1254
1255   g_ptr_array_add (priv->streams, stream);
1256   g_mutex_unlock (&priv->lock);
1257
1258   g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1259       NULL);
1260
1261   return stream;
1262 }
1263
1264 static void
1265 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1266 {
1267   GstRTSPMediaPrivate *priv;
1268   GstPad *srcpad;
1269
1270   priv = media->priv;
1271
1272   g_mutex_lock (&priv->lock);
1273   /* remove the ghostpad */
1274   srcpad = gst_rtsp_stream_get_srcpad (stream);
1275   gst_element_remove_pad (priv->element, srcpad);
1276   gst_object_unref (srcpad);
1277   /* now remove the stream */
1278   g_object_ref (stream);
1279   g_ptr_array_remove (priv->streams, stream);
1280   g_mutex_unlock (&priv->lock);
1281
1282   g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1283       stream, NULL);
1284
1285   g_object_unref (stream);
1286 }
1287
1288 /**
1289  * gst_rtsp_media_n_streams:
1290  * @media: a #GstRTSPMedia
1291  *
1292  * Get the number of streams in this media.
1293  *
1294  * Returns: The number of streams.
1295  */
1296 guint
1297 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1298 {
1299   GstRTSPMediaPrivate *priv;
1300   guint res;
1301
1302   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1303
1304   priv = media->priv;
1305
1306   g_mutex_lock (&priv->lock);
1307   res = priv->streams->len;
1308   g_mutex_unlock (&priv->lock);
1309
1310   return res;
1311 }
1312
1313 /**
1314  * gst_rtsp_media_get_stream:
1315  * @media: a #GstRTSPMedia
1316  * @idx: the stream index
1317  *
1318  * Retrieve the stream with index @idx from @media.
1319  *
1320  * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
1321  * that index did not exist.
1322  */
1323 GstRTSPStream *
1324 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1325 {
1326   GstRTSPMediaPrivate *priv;
1327   GstRTSPStream *res;
1328
1329   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1330
1331   priv = media->priv;
1332
1333   g_mutex_lock (&priv->lock);
1334   if (idx < priv->streams->len)
1335     res = g_ptr_array_index (priv->streams, idx);
1336   else
1337     res = NULL;
1338   g_mutex_unlock (&priv->lock);
1339
1340   return res;
1341 }
1342
1343 /**
1344  * gst_rtsp_media_find_stream:
1345  * @media: a #GstRTSPMedia
1346  * @control: the control of the stream
1347  *
1348  * Find a stream in @media with @control as the control uri.
1349  *
1350  * Returns: (transfer none): the #GstRTSPStream with control uri @control
1351  * or %NULL when a stream with that control did not exist.
1352  */
1353 GstRTSPStream *
1354 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
1355 {
1356   GstRTSPMediaPrivate *priv;
1357   GstRTSPStream *res;
1358   gint i;
1359
1360   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1361   g_return_val_if_fail (control != NULL, NULL);
1362
1363   priv = media->priv;
1364
1365   res = NULL;
1366
1367   g_mutex_lock (&priv->lock);
1368   for (i = 0; i < priv->streams->len; i++) {
1369     GstRTSPStream *test;
1370
1371     test = g_ptr_array_index (priv->streams, i);
1372     if (gst_rtsp_stream_has_control (test, control)) {
1373       res = test;
1374       break;
1375     }
1376   }
1377   g_mutex_unlock (&priv->lock);
1378
1379   return res;
1380 }
1381
1382 /* called with state-lock */
1383 static gboolean
1384 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
1385     GstRTSPRangeUnit unit)
1386 {
1387   return gst_rtsp_range_convert_units (range, unit);
1388 }
1389
1390 /**
1391  * gst_rtsp_media_get_range_string:
1392  * @media: a #GstRTSPMedia
1393  * @play: for the PLAY request
1394  * @unit: the unit to use for the string
1395  *
1396  * Get the current range as a string. @media must be prepared with
1397  * gst_rtsp_media_prepare ().
1398  *
1399  * Returns: (transfer full): The range as a string, g_free() after usage.
1400  */
1401 gchar *
1402 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1403     GstRTSPRangeUnit unit)
1404 {
1405   GstRTSPMediaClass *klass;
1406   GstRTSPMediaPrivate *priv;
1407   gchar *result;
1408   GstRTSPTimeRange range;
1409
1410   klass = GST_RTSP_MEDIA_GET_CLASS (media);
1411   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1412   g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1413
1414   priv = media->priv;
1415
1416   g_rec_mutex_lock (&priv->state_lock);
1417   if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
1418       priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
1419     goto not_prepared;
1420
1421   g_mutex_lock (&priv->lock);
1422
1423   /* Update the range value with current position/duration */
1424   collect_media_stats (media);
1425
1426   /* make copy */
1427   range = priv->range;
1428
1429   if (!play && priv->n_active > 0) {
1430     range.min.type = GST_RTSP_TIME_NOW;
1431     range.min.seconds = -1;
1432   }
1433   g_mutex_unlock (&priv->lock);
1434   g_rec_mutex_unlock (&priv->state_lock);
1435
1436   if (!klass->convert_range (media, &range, unit))
1437     goto conversion_failed;
1438
1439   result = gst_rtsp_range_to_string (&range);
1440
1441   return result;
1442
1443   /* ERRORS */
1444 not_prepared:
1445   {
1446     GST_WARNING ("media %p was not prepared", media);
1447     g_rec_mutex_unlock (&priv->state_lock);
1448     return NULL;
1449   }
1450 conversion_failed:
1451   {
1452     GST_WARNING ("range conversion to unit %d failed", unit);
1453     return NULL;
1454   }
1455 }
1456
1457 static void
1458 stream_update_blocked (GstRTSPStream * stream, GstRTSPMedia * media)
1459 {
1460   gst_rtsp_stream_set_blocked (stream, media->priv->blocked);
1461 }
1462
1463 static void
1464 media_streams_set_blocked (GstRTSPMedia * media, gboolean blocked)
1465 {
1466   GstRTSPMediaPrivate *priv = media->priv;
1467
1468   GST_DEBUG ("media %p set blocked %d", media, blocked);
1469   priv->blocked = blocked;
1470   g_ptr_array_foreach (priv->streams, (GFunc) stream_update_blocked, media);
1471 }
1472
1473 static void
1474 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1475 {
1476   GstRTSPMediaPrivate *priv = media->priv;
1477
1478   g_mutex_lock (&priv->lock);
1479   priv->status = status;
1480   GST_DEBUG ("setting new status to %d", status);
1481   g_cond_broadcast (&priv->cond);
1482   g_mutex_unlock (&priv->lock);
1483 }
1484
1485 /**
1486  * gst_rtsp_media_get_status:
1487  * @media: a #GstRTSPMedia
1488  *
1489  * Get the status of @media. When @media is busy preparing, this function waits
1490  * until @media is prepared or in error.
1491  *
1492  * Returns: the status of @media.
1493  */
1494 GstRTSPMediaStatus
1495 gst_rtsp_media_get_status (GstRTSPMedia * media)
1496 {
1497   GstRTSPMediaPrivate *priv = media->priv;
1498   GstRTSPMediaStatus result;
1499   gint64 end_time;
1500
1501   g_mutex_lock (&priv->lock);
1502   end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1503   /* while we are preparing, wait */
1504   while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1505     GST_DEBUG ("waiting for status change");
1506     if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1507       GST_DEBUG ("timeout, assuming error status");
1508       priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1509     }
1510   }
1511   /* could be success or error */
1512   result = priv->status;
1513   GST_DEBUG ("got status %d", result);
1514   g_mutex_unlock (&priv->lock);
1515
1516   return result;
1517 }
1518
1519 /**
1520  * gst_rtsp_media_seek:
1521  * @media: a #GstRTSPMedia
1522  * @range: (transfer none): a #GstRTSPTimeRange
1523  *
1524  * Seek the pipeline of @media to @range. @media must be prepared with
1525  * gst_rtsp_media_prepare().
1526  *
1527  * Returns: %TRUE on success.
1528  */
1529 gboolean
1530 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1531 {
1532   GstRTSPMediaClass *klass;
1533   GstRTSPMediaPrivate *priv;
1534   gboolean res;
1535   GstClockTime start, stop;
1536   GstSeekType start_type, stop_type;
1537   GstQuery *query;
1538
1539   klass = GST_RTSP_MEDIA_GET_CLASS (media);
1540
1541   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1542   g_return_val_if_fail (range != NULL, FALSE);
1543   g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1544
1545   priv = media->priv;
1546
1547   g_rec_mutex_lock (&priv->state_lock);
1548   if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1549     goto not_prepared;
1550
1551   /* Update the seekable state of the pipeline in case it changed */
1552   query = gst_query_new_seeking (GST_FORMAT_TIME);
1553   if (gst_element_query (priv->pipeline, query)) {
1554     GstFormat format;
1555     gboolean seekable;
1556     gint64 start, end;
1557
1558     gst_query_parse_seeking (query, &format, &seekable, &start, &end);
1559     priv->seekable = seekable;
1560   }
1561   gst_query_unref (query);
1562
1563   if (!priv->seekable)
1564     goto not_seekable;
1565
1566   start_type = stop_type = GST_SEEK_TYPE_NONE;
1567
1568   if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1569     goto not_supported;
1570   gst_rtsp_range_get_times (range, &start, &stop);
1571
1572   GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1573       GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1574   GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1575       GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1576
1577   if (start != GST_CLOCK_TIME_NONE)
1578     start_type = GST_SEEK_TYPE_SET;
1579
1580   if (priv->range_stop == stop)
1581     stop = GST_CLOCK_TIME_NONE;
1582   else if (stop != GST_CLOCK_TIME_NONE)
1583     stop_type = GST_SEEK_TYPE_SET;
1584
1585   if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1586     GstSeekFlags flags;
1587
1588     GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1589         GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1590
1591     gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
1592     if (priv->blocked)
1593       media_streams_set_blocked (media, TRUE);
1594
1595     /* depends on the current playing state of the pipeline. We might need to
1596      * queue this until we get EOS. */
1597     flags = GST_SEEK_FLAG_FLUSH;
1598
1599     /* if range start was not supplied we must continue from current position.
1600      * but since we're doing a flushing seek, let us query the current position
1601      * so we end up at exactly the same position after the seek. */
1602     if (range->min.type == GST_RTSP_TIME_END) { /* Yepp, that's right! */
1603       gint64 position;
1604       gboolean ret = FALSE;
1605
1606       if (klass->query_position)
1607         ret = klass->query_position (media, &position);
1608
1609       if (!ret) {
1610         GST_WARNING ("position query failed");
1611       } else {
1612         GST_DEBUG ("doing accurate seek to %" GST_TIME_FORMAT,
1613             GST_TIME_ARGS (position));
1614         start = position;
1615         start_type = GST_SEEK_TYPE_SET;
1616         flags |= GST_SEEK_FLAG_ACCURATE;
1617       }
1618     } else {
1619       /* only set keyframe flag when modifying start */
1620       if (start_type != GST_SEEK_TYPE_NONE)
1621         flags |= GST_SEEK_FLAG_KEY_UNIT;
1622     }
1623
1624     /* FIXME, we only do forwards */
1625     res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1626         flags, start_type, start, stop_type, stop);
1627
1628     /* and block for the seek to complete */
1629     GST_INFO ("done seeking %d", res);
1630     g_rec_mutex_unlock (&priv->state_lock);
1631
1632     /* wait until pipeline is prerolled again, this will also collect stats */
1633     if (!wait_preroll (media))
1634       goto preroll_failed;
1635
1636     g_rec_mutex_lock (&priv->state_lock);
1637     GST_INFO ("prerolled again");
1638   } else {
1639     GST_INFO ("no seek needed");
1640     res = TRUE;
1641   }
1642   g_rec_mutex_unlock (&priv->state_lock);
1643
1644   return res;
1645
1646   /* ERRORS */
1647 not_prepared:
1648   {
1649     g_rec_mutex_unlock (&priv->state_lock);
1650     GST_INFO ("media %p is not prepared", media);
1651     return FALSE;
1652   }
1653 not_seekable:
1654   {
1655     g_rec_mutex_unlock (&priv->state_lock);
1656     GST_INFO ("pipeline is not seekable");
1657     return FALSE;
1658   }
1659 not_supported:
1660   {
1661     g_rec_mutex_unlock (&priv->state_lock);
1662     GST_WARNING ("conversion to npt not supported");
1663     return FALSE;
1664   }
1665 preroll_failed:
1666   {
1667     GST_WARNING ("failed to preroll after seek");
1668     return FALSE;
1669   }
1670 }
1671
1672 static void
1673 stream_collect_blocking (GstRTSPStream * stream, gboolean * blocked)
1674 {
1675   *blocked &= gst_rtsp_stream_is_blocking (stream);
1676 }
1677
1678 static gboolean
1679 media_streams_blocking (GstRTSPMedia * media)
1680 {
1681   gboolean blocking = TRUE;
1682
1683   g_ptr_array_foreach (media->priv->streams, (GFunc) stream_collect_blocking,
1684       &blocking);
1685
1686   return blocking;
1687 }
1688
1689 static GstStateChangeReturn
1690 set_state (GstRTSPMedia * media, GstState state)
1691 {
1692   GstRTSPMediaPrivate *priv = media->priv;
1693   GstStateChangeReturn ret;
1694
1695   GST_INFO ("set state to %s for media %p", gst_element_state_get_name (state),
1696       media);
1697   ret = gst_element_set_state (priv->pipeline, state);
1698
1699   return ret;
1700 }
1701
1702 static GstStateChangeReturn
1703 set_target_state (GstRTSPMedia * media, GstState state, gboolean do_state)
1704 {
1705   GstRTSPMediaPrivate *priv = media->priv;
1706   GstStateChangeReturn ret;
1707
1708   GST_INFO ("set target state to %s for media %p",
1709       gst_element_state_get_name (state), media);
1710   priv->target_state = state;
1711
1712   g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
1713       priv->target_state, NULL);
1714
1715   if (do_state)
1716     ret = set_state (media, state);
1717   else
1718     ret = GST_STATE_CHANGE_SUCCESS;
1719
1720   return ret;
1721 }
1722
1723 /* called with state-lock */
1724 static gboolean
1725 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1726 {
1727   GstRTSPMediaPrivate *priv = media->priv;
1728   GstMessageType type;
1729
1730   type = GST_MESSAGE_TYPE (message);
1731
1732   switch (type) {
1733     case GST_MESSAGE_STATE_CHANGED:
1734       break;
1735     case GST_MESSAGE_BUFFERING:
1736     {
1737       gint percent;
1738
1739       gst_message_parse_buffering (message, &percent);
1740
1741       /* no state management needed for live pipelines */
1742       if (priv->is_live)
1743         break;
1744
1745       if (percent == 100) {
1746         /* a 100% message means buffering is done */
1747         priv->buffering = FALSE;
1748         /* if the desired state is playing, go back */
1749         if (priv->target_state == GST_STATE_PLAYING) {
1750           GST_INFO ("Buffering done, setting pipeline to PLAYING");
1751           set_state (media, GST_STATE_PLAYING);
1752         } else {
1753           GST_INFO ("Buffering done");
1754         }
1755       } else {
1756         /* buffering busy */
1757         if (priv->buffering == FALSE) {
1758           if (priv->target_state == GST_STATE_PLAYING) {
1759             /* we were not buffering but PLAYING, PAUSE  the pipeline. */
1760             GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1761             set_state (media, GST_STATE_PAUSED);
1762           } else {
1763             GST_INFO ("Buffering ...");
1764           }
1765         }
1766         priv->buffering = TRUE;
1767       }
1768       break;
1769     }
1770     case GST_MESSAGE_LATENCY:
1771     {
1772       gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
1773       break;
1774     }
1775     case GST_MESSAGE_ERROR:
1776     {
1777       GError *gerror;
1778       gchar *debug;
1779
1780       gst_message_parse_error (message, &gerror, &debug);
1781       GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1782       g_error_free (gerror);
1783       g_free (debug);
1784
1785       gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1786       break;
1787     }
1788     case GST_MESSAGE_WARNING:
1789     {
1790       GError *gerror;
1791       gchar *debug;
1792
1793       gst_message_parse_warning (message, &gerror, &debug);
1794       GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1795       g_error_free (gerror);
1796       g_free (debug);
1797       break;
1798     }
1799     case GST_MESSAGE_ELEMENT:
1800     {
1801       const GstStructure *s;
1802
1803       s = gst_message_get_structure (message);
1804       if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
1805         GST_DEBUG ("media received blocking message");
1806         if (priv->blocked && media_streams_blocking (media)) {
1807           GST_DEBUG ("media is blocking");
1808           collect_media_stats (media);
1809
1810           if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1811             gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1812         }
1813       }
1814       break;
1815     }
1816     case GST_MESSAGE_STREAM_STATUS:
1817       break;
1818     case GST_MESSAGE_ASYNC_DONE:
1819       if (priv->adding) {
1820         /* when we are dynamically adding pads, the addition of the udpsrc will
1821          * temporarily produce ASYNC_DONE messages. We have to ignore them and
1822          * wait for the final ASYNC_DONE after everything prerolled */
1823         GST_INFO ("%p: ignoring ASYNC_DONE", media);
1824       } else {
1825         GST_INFO ("%p: got ASYNC_DONE", media);
1826         collect_media_stats (media);
1827
1828         if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1829           gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1830       }
1831       break;
1832     case GST_MESSAGE_EOS:
1833       GST_INFO ("%p: got EOS", media);
1834
1835       if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1836         GST_DEBUG ("shutting down after EOS");
1837         finish_unprepare (media);
1838       }
1839       break;
1840     default:
1841       GST_INFO ("%p: got message type %d (%s)", media, type,
1842           gst_message_type_get_name (type));
1843       break;
1844   }
1845   return TRUE;
1846 }
1847
1848 static gboolean
1849 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1850 {
1851   GstRTSPMediaPrivate *priv = media->priv;
1852   GstRTSPMediaClass *klass;
1853   gboolean ret;
1854
1855   klass = GST_RTSP_MEDIA_GET_CLASS (media);
1856
1857   g_rec_mutex_lock (&priv->state_lock);
1858   if (klass->handle_message)
1859     ret = klass->handle_message (media, message);
1860   else
1861     ret = FALSE;
1862   g_rec_mutex_unlock (&priv->state_lock);
1863
1864   return ret;
1865 }
1866
1867 static void
1868 watch_destroyed (GstRTSPMedia * media)
1869 {
1870   GST_DEBUG_OBJECT (media, "source destroyed");
1871   g_object_unref (media);
1872 }
1873
1874 static GstElement *
1875 find_payload_element (GstElement * payloader)
1876 {
1877   GstElement *pay = NULL;
1878
1879   if (GST_IS_BIN (payloader)) {
1880     GstIterator *iter;
1881     GValue item = { 0 };
1882
1883     iter = gst_bin_iterate_recurse (GST_BIN (payloader));
1884     while (gst_iterator_next (iter, &item) == GST_ITERATOR_OK) {
1885       GstElement *element = (GstElement *) g_value_get_object (&item);
1886       GstElementClass *eclass = GST_ELEMENT_GET_CLASS (element);
1887       const gchar *klass;
1888
1889       klass =
1890           gst_element_class_get_metadata (eclass, GST_ELEMENT_METADATA_KLASS);
1891       if (klass == NULL)
1892         continue;
1893
1894       if (strstr (klass, "Payloader") && strstr (klass, "RTP")) {
1895         pay = gst_object_ref (element);
1896         g_value_unset (&item);
1897         break;
1898       }
1899       g_value_unset (&item);
1900     }
1901     gst_iterator_free (iter);
1902   } else {
1903     pay = g_object_ref (payloader);
1904   }
1905
1906   return pay;
1907 }
1908
1909 /* called from streaming threads */
1910 static void
1911 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1912 {
1913   GstRTSPMediaPrivate *priv = media->priv;
1914   GstRTSPStream *stream;
1915   GstElement *pay;
1916
1917   /* find the real payload element */
1918   pay = find_payload_element (element);
1919   stream = gst_rtsp_media_create_stream (media, pay, pad);
1920   gst_object_unref (pay);
1921
1922   GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1923
1924   g_rec_mutex_lock (&priv->state_lock);
1925   if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
1926     goto not_preparing;
1927
1928   g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
1929
1930   /* we will be adding elements below that will cause ASYNC_DONE to be
1931    * posted in the bus. We want to ignore those messages until the
1932    * pipeline really prerolled. */
1933   priv->adding = TRUE;
1934
1935   /* join the element in the PAUSED state because this callback is
1936    * called from the streaming thread and it is PAUSED */
1937   if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
1938           priv->rtpbin, GST_STATE_PAUSED)) {
1939     GST_WARNING ("failed to join bin element");
1940   }
1941
1942   priv->adding = FALSE;
1943   g_rec_mutex_unlock (&priv->state_lock);
1944
1945   return;
1946
1947   /* ERRORS */
1948 not_preparing:
1949   {
1950     gst_rtsp_media_remove_stream (media, stream);
1951     g_rec_mutex_unlock (&priv->state_lock);
1952     GST_INFO ("ignore pad because we are not preparing");
1953     return;
1954   }
1955 }
1956
1957 static void
1958 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1959 {
1960   GstRTSPMediaPrivate *priv = media->priv;
1961   GstRTSPStream *stream;
1962
1963   stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
1964   if (stream == NULL)
1965     return;
1966
1967   GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
1968
1969   g_rec_mutex_lock (&priv->state_lock);
1970   gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
1971   g_rec_mutex_unlock (&priv->state_lock);
1972
1973   gst_rtsp_media_remove_stream (media, stream);
1974 }
1975
1976 static void
1977 remove_fakesink (GstRTSPMediaPrivate * priv)
1978 {
1979   GstElement *fakesink;
1980
1981   g_mutex_lock (&priv->lock);
1982   if ((fakesink = priv->fakesink))
1983     gst_object_ref (fakesink);
1984   priv->fakesink = NULL;
1985   g_mutex_unlock (&priv->lock);
1986
1987   if (fakesink) {
1988     gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
1989     gst_element_set_state (fakesink, GST_STATE_NULL);
1990     gst_object_unref (fakesink);
1991     GST_INFO ("removed fakesink");
1992   }
1993 }
1994
1995 static void
1996 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1997 {
1998   GstRTSPMediaPrivate *priv = media->priv;
1999
2000   GST_INFO ("no more pads");
2001   remove_fakesink (priv);
2002 }
2003
2004 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
2005
2006 struct _DynPaySignalHandlers
2007 {
2008   gulong pad_added_handler;
2009   gulong pad_removed_handler;
2010   gulong no_more_pads_handler;
2011 };
2012
2013 static gboolean
2014 start_preroll (GstRTSPMedia * media)
2015 {
2016   GstRTSPMediaPrivate *priv = media->priv;
2017   GstStateChangeReturn ret;
2018
2019   GST_INFO ("setting pipeline to PAUSED for media %p", media);
2020   /* first go to PAUSED */
2021   ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
2022
2023   switch (ret) {
2024     case GST_STATE_CHANGE_SUCCESS:
2025       GST_INFO ("SUCCESS state change for media %p", media);
2026       priv->seekable = TRUE;
2027       break;
2028     case GST_STATE_CHANGE_ASYNC:
2029       GST_INFO ("ASYNC state change for media %p", media);
2030       priv->seekable = TRUE;
2031       break;
2032     case GST_STATE_CHANGE_NO_PREROLL:
2033       /* we need to go to PLAYING */
2034       GST_INFO ("NO_PREROLL state change: live media %p", media);
2035       /* FIXME we disable seeking for live streams for now. We should perform a
2036        * seeking query in preroll instead */
2037       priv->seekable = FALSE;
2038       priv->is_live = TRUE;
2039       /* start blocked  to make sure nothing goes to the sink */
2040       media_streams_set_blocked (media, TRUE);
2041       ret = set_state (media, GST_STATE_PLAYING);
2042       if (ret == GST_STATE_CHANGE_FAILURE)
2043         goto state_failed;
2044       break;
2045     case GST_STATE_CHANGE_FAILURE:
2046       goto state_failed;
2047   }
2048
2049   return TRUE;
2050
2051 state_failed:
2052   {
2053     GST_WARNING ("failed to preroll pipeline");
2054     return FALSE;
2055   }
2056 }
2057
2058 static gboolean
2059 wait_preroll (GstRTSPMedia * media)
2060 {
2061   GstRTSPMediaStatus status;
2062
2063   GST_DEBUG ("wait to preroll pipeline");
2064
2065   /* wait until pipeline is prerolled */
2066   status = gst_rtsp_media_get_status (media);
2067   if (status == GST_RTSP_MEDIA_STATUS_ERROR)
2068     goto preroll_failed;
2069
2070   return TRUE;
2071
2072 preroll_failed:
2073   {
2074     GST_WARNING ("failed to preroll pipeline");
2075     return FALSE;
2076   }
2077 }
2078
2079 static gboolean
2080 start_prepare (GstRTSPMedia * media)
2081 {
2082   GstRTSPMediaPrivate *priv = media->priv;
2083   guint i;
2084   GList *walk;
2085
2086   /* link streams we already have, other streams might appear when we have
2087    * dynamic elements */
2088   for (i = 0; i < priv->streams->len; i++) {
2089     GstRTSPStream *stream;
2090
2091     stream = g_ptr_array_index (priv->streams, i);
2092
2093     if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2094             priv->rtpbin, GST_STATE_NULL)) {
2095       goto join_bin_failed;
2096     }
2097   }
2098
2099   for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2100     GstElement *elem = walk->data;
2101     DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
2102
2103     GST_INFO ("adding callbacks for dynamic element %p", elem);
2104
2105     handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
2106         (GCallback) pad_added_cb, media);
2107     handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
2108         (GCallback) pad_removed_cb, media);
2109     handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
2110         (GCallback) no_more_pads_cb, media);
2111
2112     g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
2113
2114     /* we add a fakesink here in order to make the state change async. We remove
2115      * the fakesink again in the no-more-pads callback. */
2116     priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
2117     gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
2118   }
2119
2120   if (!start_preroll (media))
2121     goto preroll_failed;
2122
2123   return FALSE;
2124
2125 join_bin_failed:
2126   {
2127     GST_WARNING ("failed to join bin element");
2128     gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2129     return FALSE;
2130   }
2131 preroll_failed:
2132   {
2133     GST_WARNING ("failed to preroll pipeline");
2134     gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2135     return FALSE;
2136   }
2137 }
2138
2139 /**
2140  * gst_rtsp_media_prepare:
2141  * @media: a #GstRTSPMedia
2142  * @thread: (transfer full): a #GstRTSPThread to run the bus handler or %NULL
2143  *
2144  * Prepare @media for streaming. This function will create the objects
2145  * to manage the streaming. A pipeline must have been set on @media with
2146  * gst_rtsp_media_take_pipeline().
2147  *
2148  * It will preroll the pipeline and collect vital information about the streams
2149  * such as the duration.
2150  *
2151  * Returns: %TRUE on success.
2152  */
2153 gboolean
2154 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2155 {
2156   GstRTSPMediaPrivate *priv;
2157   GstBus *bus;
2158   GSource *source;
2159   GstRTSPMediaClass *klass;
2160   GMainContext *context;
2161
2162   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2163
2164   priv = media->priv;
2165
2166   g_rec_mutex_lock (&priv->state_lock);
2167   priv->prepare_count++;
2168
2169   if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
2170       priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
2171     goto was_prepared;
2172
2173   if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2174     goto wait_status;
2175
2176   if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
2177     goto not_unprepared;
2178
2179   if (!priv->reusable && priv->reused)
2180     goto is_reused;
2181
2182   klass = GST_RTSP_MEDIA_GET_CLASS (media);
2183
2184   if (!klass->create_rtpbin)
2185     goto no_create_rtpbin;
2186
2187   priv->rtpbin = klass->create_rtpbin (media);
2188   if (priv->rtpbin != NULL) {
2189     gboolean success = TRUE;
2190
2191     if (klass->setup_rtpbin)
2192       success = klass->setup_rtpbin (media, priv->rtpbin);
2193
2194     if (success == FALSE) {
2195       gst_object_unref (priv->rtpbin);
2196       priv->rtpbin = NULL;
2197     }
2198   }
2199   if (priv->rtpbin == NULL)
2200     goto no_rtpbin;
2201
2202   GST_INFO ("preparing media %p", media);
2203
2204   /* reset some variables */
2205   priv->is_live = FALSE;
2206   priv->seekable = FALSE;
2207   priv->buffering = FALSE;
2208   priv->thread = thread;
2209   context = (thread != NULL) ? (thread->context) : NULL;
2210
2211   /* we're preparing now */
2212   gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
2213
2214   bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
2215
2216   /* add the pipeline bus to our custom mainloop */
2217   priv->source = gst_bus_create_watch (bus);
2218   gst_object_unref (bus);
2219
2220   g_source_set_callback (priv->source, (GSourceFunc) bus_message,
2221       g_object_ref (media), (GDestroyNotify) watch_destroyed);
2222
2223   priv->id = g_source_attach (priv->source, context);
2224
2225   /* add stuff to the bin */
2226   gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
2227
2228   /* do remainder in context */
2229   source = g_idle_source_new ();
2230   g_source_set_callback (source, (GSourceFunc) start_prepare, media, NULL);
2231   g_source_attach (source, context);
2232   g_source_unref (source);
2233
2234 wait_status:
2235   g_rec_mutex_unlock (&priv->state_lock);
2236
2237   /* now wait for all pads to be prerolled, FIXME, we should somehow be
2238    * able to do this async so that we don't block the server thread. */
2239   if (!wait_preroll (media))
2240     goto preroll_failed;
2241
2242   g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
2243
2244   GST_INFO ("object %p is prerolled", media);
2245
2246   return TRUE;
2247
2248   /* OK */
2249 was_prepared:
2250   {
2251     GST_LOG ("media %p was prepared", media);
2252     /* we are not going to use the giving thread, so stop it. */
2253     if (thread)
2254       gst_rtsp_thread_stop (thread);
2255     g_rec_mutex_unlock (&priv->state_lock);
2256     return TRUE;
2257   }
2258   /* ERRORS */
2259 not_unprepared:
2260   {
2261     GST_WARNING ("media %p was not unprepared", media);
2262     priv->prepare_count--;
2263     g_rec_mutex_unlock (&priv->state_lock);
2264     return FALSE;
2265   }
2266 is_reused:
2267   {
2268     priv->prepare_count--;
2269     g_rec_mutex_unlock (&priv->state_lock);
2270     GST_WARNING ("can not reuse media %p", media);
2271     return FALSE;
2272   }
2273 no_create_rtpbin:
2274   {
2275     priv->prepare_count--;
2276     g_rec_mutex_unlock (&priv->state_lock);
2277     GST_ERROR ("no create_rtpbin function");
2278     g_critical ("no create_rtpbin vmethod function set");
2279     return FALSE;
2280   }
2281 no_rtpbin:
2282   {
2283     priv->prepare_count--;
2284     g_rec_mutex_unlock (&priv->state_lock);
2285     GST_WARNING ("no rtpbin element");
2286     g_warning ("failed to create element 'rtpbin', check your installation");
2287     return FALSE;
2288   }
2289 preroll_failed:
2290   {
2291     GST_WARNING ("failed to preroll pipeline");
2292     gst_rtsp_media_unprepare (media);
2293     return FALSE;
2294   }
2295 }
2296
2297 /* must be called with state-lock */
2298 static void
2299 finish_unprepare (GstRTSPMedia * media)
2300 {
2301   GstRTSPMediaPrivate *priv = media->priv;
2302   gint i;
2303   GList *walk;
2304
2305   GST_DEBUG ("shutting down");
2306
2307   /* release the lock on shutdown, otherwise pad_added_cb might try to
2308    * acquire the lock and then we deadlock */
2309   g_rec_mutex_unlock (&priv->state_lock);
2310   set_state (media, GST_STATE_NULL);
2311   g_rec_mutex_lock (&priv->state_lock);
2312   remove_fakesink (priv);
2313
2314   for (i = 0; i < priv->streams->len; i++) {
2315     GstRTSPStream *stream;
2316
2317     GST_INFO ("Removing elements of stream %d from pipeline", i);
2318
2319     stream = g_ptr_array_index (priv->streams, i);
2320
2321     gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2322   }
2323
2324   /* remove the pad signal handlers */
2325   for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2326     GstElement *elem = walk->data;
2327     DynPaySignalHandlers *handlers;
2328
2329     handlers =
2330         g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
2331     g_assert (handlers != NULL);
2332
2333     g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
2334     g_signal_handler_disconnect (G_OBJECT (elem),
2335         handlers->pad_removed_handler);
2336     g_signal_handler_disconnect (G_OBJECT (elem),
2337         handlers->no_more_pads_handler);
2338
2339     g_slice_free (DynPaySignalHandlers, handlers);
2340   }
2341
2342   gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
2343   priv->rtpbin = NULL;
2344
2345   if (priv->nettime)
2346     gst_object_unref (priv->nettime);
2347   priv->nettime = NULL;
2348
2349   priv->reused = TRUE;
2350   gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARED);
2351
2352   /* when the media is not reusable, this will effectively unref the media and
2353    * recreate it */
2354   g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
2355
2356   /* the source has the last ref to the media */
2357   if (priv->source) {
2358     GST_DEBUG ("destroy source");
2359     g_source_destroy (priv->source);
2360     g_source_unref (priv->source);
2361   }
2362   if (priv->thread) {
2363     GST_DEBUG ("stop thread");
2364     gst_rtsp_thread_stop (priv->thread);
2365   }
2366 }
2367
2368 /* called with state-lock */
2369 static gboolean
2370 default_unprepare (GstRTSPMedia * media)
2371 {
2372   GstRTSPMediaPrivate *priv = media->priv;
2373
2374   if (priv->eos_shutdown) {
2375     GST_DEBUG ("sending EOS for shutdown");
2376     /* ref so that we don't disappear */
2377     gst_element_send_event (priv->pipeline, gst_event_new_eos ());
2378     /* we need to go to playing again for the EOS to propagate, normally in this
2379      * state, nothing is receiving data from us anymore so this is ok. */
2380     set_state (media, GST_STATE_PLAYING);
2381   } else {
2382     finish_unprepare (media);
2383   }
2384   return TRUE;
2385 }
2386
2387 /**
2388  * gst_rtsp_media_unprepare:
2389  * @media: a #GstRTSPMedia
2390  *
2391  * Unprepare @media. After this call, the media should be prepared again before
2392  * it can be used again. If the media is set to be non-reusable, a new instance
2393  * must be created.
2394  *
2395  * Returns: %TRUE on success.
2396  */
2397 gboolean
2398 gst_rtsp_media_unprepare (GstRTSPMedia * media)
2399 {
2400   GstRTSPMediaPrivate *priv;
2401   gboolean success;
2402
2403   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2404
2405   priv = media->priv;
2406
2407   g_rec_mutex_lock (&priv->state_lock);
2408   if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
2409     goto was_unprepared;
2410
2411   priv->prepare_count--;
2412   if (priv->prepare_count > 0)
2413     goto is_busy;
2414
2415   GST_INFO ("unprepare media %p", media);
2416   if (priv->blocked)
2417     media_streams_set_blocked (media, FALSE);
2418   set_target_state (media, GST_STATE_NULL, FALSE);
2419   success = TRUE;
2420
2421   gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARING);
2422
2423   if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
2424     GstRTSPMediaClass *klass;
2425
2426     klass = GST_RTSP_MEDIA_GET_CLASS (media);
2427     if (klass->unprepare)
2428       success = klass->unprepare (media);
2429   } else {
2430     finish_unprepare (media);
2431   }
2432   g_rec_mutex_unlock (&priv->state_lock);
2433
2434   return success;
2435
2436 was_unprepared:
2437   {
2438     g_rec_mutex_unlock (&priv->state_lock);
2439     GST_INFO ("media %p was already unprepared", media);
2440     return TRUE;
2441   }
2442 is_busy:
2443   {
2444     GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
2445     g_rec_mutex_unlock (&priv->state_lock);
2446     return TRUE;
2447   }
2448 }
2449
2450 /* should be called with state-lock */
2451 static GstClock *
2452 get_clock_unlocked (GstRTSPMedia * media)
2453 {
2454   if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
2455     GST_DEBUG_OBJECT (media, "media was not prepared");
2456     return NULL;
2457   }
2458   return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
2459 }
2460
2461 /**
2462  * gst_rtsp_media_get_clock:
2463  * @media: a #GstRTSPMedia
2464  *
2465  * Get the clock that is used by the pipeline in @media.
2466  *
2467  * @media must be prepared before this method returns a valid clock object.
2468  *
2469  * Returns: (transfer full): the #GstClock used by @media. unref after usage.
2470  */
2471 GstClock *
2472 gst_rtsp_media_get_clock (GstRTSPMedia * media)
2473 {
2474   GstClock *clock;
2475   GstRTSPMediaPrivate *priv;
2476
2477   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2478
2479   priv = media->priv;
2480
2481   g_rec_mutex_lock (&priv->state_lock);
2482   clock = get_clock_unlocked (media);
2483   g_rec_mutex_unlock (&priv->state_lock);
2484
2485   return clock;
2486 }
2487
2488 /**
2489  * gst_rtsp_media_get_base_time:
2490  * @media: a #GstRTSPMedia
2491  *
2492  * Get the base_time that is used by the pipeline in @media.
2493  *
2494  * @media must be prepared before this method returns a valid base_time.
2495  *
2496  * Returns: the base_time used by @media.
2497  */
2498 GstClockTime
2499 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
2500 {
2501   GstClockTime result;
2502   GstRTSPMediaPrivate *priv;
2503
2504   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
2505
2506   priv = media->priv;
2507
2508   g_rec_mutex_lock (&priv->state_lock);
2509   if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2510     goto not_prepared;
2511
2512   result = gst_element_get_base_time (media->priv->pipeline);
2513   g_rec_mutex_unlock (&priv->state_lock);
2514
2515   return result;
2516
2517   /* ERRORS */
2518 not_prepared:
2519   {
2520     g_rec_mutex_unlock (&priv->state_lock);
2521     GST_DEBUG_OBJECT (media, "media was not prepared");
2522     return GST_CLOCK_TIME_NONE;
2523   }
2524 }
2525
2526 /**
2527  * gst_rtsp_media_get_time_provider:
2528  * @media: a #GstRTSPMedia
2529  * @address: an address or %NULL
2530  * @port: a port or 0
2531  *
2532  * Get the #GstNetTimeProvider for the clock used by @media. The time provider
2533  * will listen on @address and @port for client time requests.
2534  *
2535  * Returns: (transfer full): the #GstNetTimeProvider of @media.
2536  */
2537 GstNetTimeProvider *
2538 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
2539     guint16 port)
2540 {
2541   GstRTSPMediaPrivate *priv;
2542   GstNetTimeProvider *provider = NULL;
2543
2544   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2545
2546   priv = media->priv;
2547
2548   g_rec_mutex_lock (&priv->state_lock);
2549   if (priv->time_provider) {
2550     if ((provider = priv->nettime) == NULL) {
2551       GstClock *clock;
2552
2553       if (priv->time_provider && (clock = get_clock_unlocked (media))) {
2554         provider = gst_net_time_provider_new (clock, address, port);
2555         gst_object_unref (clock);
2556
2557         priv->nettime = provider;
2558       }
2559     }
2560   }
2561   g_rec_mutex_unlock (&priv->state_lock);
2562
2563   if (provider)
2564     gst_object_ref (provider);
2565
2566   return provider;
2567 }
2568
2569 static gboolean
2570 default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp, GstSDPInfo * info)
2571 {
2572   return gst_rtsp_sdp_from_media (sdp, info, media);
2573 }
2574
2575 /**
2576  * gst_rtsp_media_setup_sdp:
2577  * @media: a #GstRTSPMedia
2578  * @sdp: (transfer none): a #GstSDPMessage
2579  * @info: (transfer none): a #GstSDPInfo
2580  *
2581  * Add @media specific info to @sdp. @info is used to configure the connection
2582  * information in the SDP.
2583  *
2584  * Returns: TRUE on success.
2585  */
2586 gboolean
2587 gst_rtsp_media_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
2588     GstSDPInfo * info)
2589 {
2590   GstRTSPMediaPrivate *priv;
2591   GstRTSPMediaClass *klass;
2592   gboolean res;
2593
2594   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2595   g_return_val_if_fail (sdp != NULL, FALSE);
2596   g_return_val_if_fail (info != NULL, FALSE);
2597
2598   priv = media->priv;
2599
2600   g_rec_mutex_lock (&priv->state_lock);
2601
2602   klass = GST_RTSP_MEDIA_GET_CLASS (media);
2603
2604   if (!klass->setup_sdp)
2605     goto no_setup_sdp;
2606
2607   res = klass->setup_sdp (media, sdp, info);
2608
2609   g_rec_mutex_unlock (&priv->state_lock);
2610
2611   return res;
2612
2613   /* ERRORS */
2614 no_setup_sdp:
2615   {
2616     g_rec_mutex_unlock (&priv->state_lock);
2617     GST_ERROR ("no setup_sdp function");
2618     g_critical ("no setup_sdp vmethod function set");
2619     return FALSE;
2620   }
2621 }
2622
2623 /**
2624  * gst_rtsp_media_suspend:
2625  * @media: a #GstRTSPMedia
2626  *
2627  * Suspend @media. The state of the pipeline managed by @media is set to
2628  * GST_STATE_NULL but all streams are kept. @media can be prepared again
2629  * with gst_rtsp_media_unsuspend()
2630  *
2631  * @media must be prepared with gst_rtsp_media_prepare();
2632  *
2633  * Returns: %TRUE on success.
2634  */
2635 gboolean
2636 gst_rtsp_media_suspend (GstRTSPMedia * media)
2637 {
2638   GstRTSPMediaPrivate *priv = media->priv;
2639   GstStateChangeReturn ret;
2640
2641   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2642
2643   GST_FIXME ("suspend for dynamic pipelines needs fixing");
2644
2645   g_rec_mutex_lock (&priv->state_lock);
2646   if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2647     goto not_prepared;
2648
2649   /* don't attempt to suspend when something is busy */
2650   if (priv->n_active > 0)
2651     goto done;
2652
2653   switch (priv->suspend_mode) {
2654     case GST_RTSP_SUSPEND_MODE_NONE:
2655       GST_DEBUG ("media %p no suspend", media);
2656       break;
2657     case GST_RTSP_SUSPEND_MODE_PAUSE:
2658       GST_DEBUG ("media %p suspend to PAUSED", media);
2659       ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
2660       if (ret == GST_STATE_CHANGE_FAILURE)
2661         goto state_failed;
2662       break;
2663     case GST_RTSP_SUSPEND_MODE_RESET:
2664       GST_DEBUG ("media %p suspend to NULL", media);
2665       ret = set_target_state (media, GST_STATE_NULL, TRUE);
2666       if (ret == GST_STATE_CHANGE_FAILURE)
2667         goto state_failed;
2668       break;
2669     default:
2670       break;
2671   }
2672   /* let the streams do the state changes freely, if any */
2673   media_streams_set_blocked (media, FALSE);
2674   gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_SUSPENDED);
2675 done:
2676   g_rec_mutex_unlock (&priv->state_lock);
2677
2678   return TRUE;
2679
2680   /* ERRORS */
2681 not_prepared:
2682   {
2683     g_rec_mutex_unlock (&priv->state_lock);
2684     GST_WARNING ("media %p was not prepared", media);
2685     return FALSE;
2686   }
2687 state_failed:
2688   {
2689     g_rec_mutex_unlock (&priv->state_lock);
2690     gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2691     GST_WARNING ("failed changing pipeline's state for media %p", media);
2692     return FALSE;
2693   }
2694 }
2695
2696 /**
2697  * gst_rtsp_media_unsuspend:
2698  * @media: a #GstRTSPMedia
2699  *
2700  * Unsuspend @media if it was in a suspended state. This method does nothing
2701  * when the media was not in the suspended state.
2702  *
2703  * Returns: %TRUE on success.
2704  */
2705 gboolean
2706 gst_rtsp_media_unsuspend (GstRTSPMedia * media)
2707 {
2708   GstRTSPMediaPrivate *priv = media->priv;
2709
2710   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2711
2712   g_rec_mutex_lock (&priv->state_lock);
2713   if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
2714     goto done;
2715
2716   switch (priv->suspend_mode) {
2717     case GST_RTSP_SUSPEND_MODE_NONE:
2718       gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2719       break;
2720     case GST_RTSP_SUSPEND_MODE_PAUSE:
2721       gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2722       break;
2723     case GST_RTSP_SUSPEND_MODE_RESET:
2724     {
2725       gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
2726       if (!start_preroll (media))
2727         goto start_failed;
2728       g_rec_mutex_unlock (&priv->state_lock);
2729
2730       if (!wait_preroll (media))
2731         goto preroll_failed;
2732
2733       g_rec_mutex_lock (&priv->state_lock);
2734     }
2735     default:
2736       break;
2737   }
2738 done:
2739   g_rec_mutex_unlock (&priv->state_lock);
2740
2741   return TRUE;
2742
2743   /* ERRORS */
2744 start_failed:
2745   {
2746     g_rec_mutex_unlock (&priv->state_lock);
2747     GST_WARNING ("failed to preroll pipeline");
2748     gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2749     return FALSE;
2750   }
2751 preroll_failed:
2752   {
2753     GST_WARNING ("failed to preroll pipeline");
2754     return FALSE;
2755   }
2756 }
2757
2758 /* must be called with state-lock */
2759 static void
2760 media_set_pipeline_state_locked (GstRTSPMedia * media, GstState state)
2761 {
2762   GstRTSPMediaPrivate *priv = media->priv;
2763
2764   if (state == GST_STATE_NULL) {
2765     gst_rtsp_media_unprepare (media);
2766   } else {
2767     GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
2768     set_target_state (media, state, FALSE);
2769     /* when we are buffering, don't update the state yet, this will be done
2770      * when buffering finishes */
2771     if (priv->buffering) {
2772       GST_INFO ("Buffering busy, delay state change");
2773     } else {
2774       if (state == GST_STATE_PLAYING)
2775         /* make sure pads are not blocking anymore when going to PLAYING */
2776         media_streams_set_blocked (media, FALSE);
2777
2778       set_state (media, state);
2779
2780       /* and suspend after pause */
2781       if (state == GST_STATE_PAUSED)
2782         gst_rtsp_media_suspend (media);
2783     }
2784   }
2785 }
2786
2787 /**
2788  * gst_rtsp_media_set_pipeline_state:
2789  * @media: a #GstRTSPMedia
2790  * @state: the target state of the pipeline
2791  *
2792  * Set the state of the pipeline managed by @media to @state
2793  */
2794 void
2795 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
2796 {
2797   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
2798
2799   g_rec_mutex_lock (&media->priv->state_lock);
2800   media_set_pipeline_state_locked (media, state);
2801   g_rec_mutex_unlock (&media->priv->state_lock);
2802 }
2803
2804 /**
2805  * gst_rtsp_media_set_state:
2806  * @media: a #GstRTSPMedia
2807  * @state: the target state of the media
2808  * @transports: (transfer none) (element-type GstRtspServer.RTSPStreamTransport):
2809  * a #GPtrArray of #GstRTSPStreamTransport pointers
2810  *
2811  * Set the state of @media to @state and for the transports in @transports.
2812  *
2813  * @media must be prepared with gst_rtsp_media_prepare();
2814  *
2815  * Returns: %TRUE on success.
2816  */
2817 gboolean
2818 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
2819     GPtrArray * transports)
2820 {
2821   GstRTSPMediaPrivate *priv;
2822   gint i;
2823   gboolean activate, deactivate, do_state;
2824   gint old_active;
2825
2826   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2827   g_return_val_if_fail (transports != NULL, FALSE);
2828
2829   priv = media->priv;
2830
2831   g_rec_mutex_lock (&priv->state_lock);
2832   if (priv->status == GST_RTSP_MEDIA_STATUS_ERROR)
2833     goto error_status;
2834   if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
2835       priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
2836     goto not_prepared;
2837
2838   /* NULL and READY are the same */
2839   if (state == GST_STATE_READY)
2840     state = GST_STATE_NULL;
2841
2842   activate = deactivate = FALSE;
2843
2844   GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
2845       media);
2846
2847   switch (state) {
2848     case GST_STATE_NULL:
2849     case GST_STATE_PAUSED:
2850       /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
2851       if (priv->target_state == GST_STATE_PLAYING)
2852         deactivate = TRUE;
2853       break;
2854     case GST_STATE_PLAYING:
2855       /* we're going to PLAYING, activate */
2856       activate = TRUE;
2857       break;
2858     default:
2859       break;
2860   }
2861   old_active = priv->n_active;
2862
2863   for (i = 0; i < transports->len; i++) {
2864     GstRTSPStreamTransport *trans;
2865
2866     /* we need a non-NULL entry in the array */
2867     trans = g_ptr_array_index (transports, i);
2868     if (trans == NULL)
2869       continue;
2870
2871     if (activate) {
2872       if (gst_rtsp_stream_transport_set_active (trans, TRUE))
2873         priv->n_active++;
2874     } else if (deactivate) {
2875       if (gst_rtsp_stream_transport_set_active (trans, FALSE))
2876         priv->n_active--;
2877     }
2878   }
2879
2880   /* we just activated the first media, do the playing state change */
2881   if (old_active == 0 && activate)
2882     do_state = TRUE;
2883   /* if we have no more active media, do the downward state changes */
2884   else if (priv->n_active == 0)
2885     do_state = TRUE;
2886   else
2887     do_state = FALSE;
2888
2889   GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
2890       media, do_state);
2891
2892   if (priv->target_state != state) {
2893     if (do_state)
2894       media_set_pipeline_state_locked (media, state);
2895
2896     g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2897         NULL);
2898   }
2899
2900   /* remember where we are */
2901   if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
2902           old_active != priv->n_active))
2903     collect_media_stats (media);
2904
2905   g_rec_mutex_unlock (&priv->state_lock);
2906
2907   return TRUE;
2908
2909   /* ERRORS */
2910 not_prepared:
2911   {
2912     GST_WARNING ("media %p was not prepared", media);
2913     g_rec_mutex_unlock (&priv->state_lock);
2914     return FALSE;
2915   }
2916 error_status:
2917   {
2918     GST_WARNING ("media %p in error status while changing to state %d",
2919         media, state);
2920     if (state == GST_STATE_NULL) {
2921       for (i = 0; i < transports->len; i++) {
2922         GstRTSPStreamTransport *trans;
2923
2924         /* we need a non-NULL entry in the array */
2925         trans = g_ptr_array_index (transports, i);
2926         if (trans == NULL)
2927           continue;
2928
2929         gst_rtsp_stream_transport_set_active (trans, FALSE);
2930       }
2931       priv->n_active = 0;
2932     }
2933     g_rec_mutex_unlock (&priv->state_lock);
2934     return FALSE;
2935   }
2936 }