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