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