client: fix compilation
[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 #include <string.h>
21 #include <stdlib.h>
22
23 #include <gst/app/gstappsrc.h>
24 #include <gst/app/gstappsink.h>
25
26 #include "rtsp-media.h"
27
28 #define DEFAULT_SHARED          FALSE
29 #define DEFAULT_REUSABLE        FALSE
30 #define DEFAULT_PROTOCOLS       GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_TCP
31 //#define DEFAULT_PROTOCOLS      GST_RTSP_LOWER_TRANS_UDP_MCAST
32 #define DEFAULT_EOS_SHUTDOWN    FALSE
33 #define DEFAULT_BUFFER_SIZE     0x80000
34
35 /* define to dump received RTCP packets */
36 #undef DUMP_STATS
37
38 enum
39 {
40   PROP_0,
41   PROP_SHARED,
42   PROP_REUSABLE,
43   PROP_PROTOCOLS,
44   PROP_EOS_SHUTDOWN,
45   PROP_BUFFER_SIZE,
46   PROP_LAST
47 };
48
49 enum
50 {
51   SIGNAL_NEW_STREAM,
52   SIGNAL_PREPARED,
53   SIGNAL_UNPREPARED,
54   SIGNAL_NEW_STATE,
55   SIGNAL_LAST
56 };
57
58 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
59 #define GST_CAT_DEFAULT rtsp_media_debug
60
61 static void gst_rtsp_media_get_property (GObject * object, guint propid,
62     GValue * value, GParamSpec * pspec);
63 static void gst_rtsp_media_set_property (GObject * object, guint propid,
64     const GValue * value, GParamSpec * pspec);
65 static void gst_rtsp_media_finalize (GObject * obj);
66
67 static gpointer do_loop (GstRTSPMediaClass * klass);
68 static gboolean default_handle_message (GstRTSPMedia * media,
69     GstMessage * message);
70 static void finish_unprepare (GstRTSPMedia * media);
71 static gboolean default_unprepare (GstRTSPMedia * media);
72
73 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
74
75 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
76
77 static void
78 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
79 {
80   GObjectClass *gobject_class;
81
82   gobject_class = G_OBJECT_CLASS (klass);
83
84   gobject_class->get_property = gst_rtsp_media_get_property;
85   gobject_class->set_property = gst_rtsp_media_set_property;
86   gobject_class->finalize = gst_rtsp_media_finalize;
87
88   g_object_class_install_property (gobject_class, PROP_SHARED,
89       g_param_spec_boolean ("shared", "Shared",
90           "If this media pipeline can be shared", DEFAULT_SHARED,
91           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
92
93   g_object_class_install_property (gobject_class, PROP_REUSABLE,
94       g_param_spec_boolean ("reusable", "Reusable",
95           "If this media pipeline can be reused after an unprepare",
96           DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
97
98   g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
99       g_param_spec_flags ("protocols", "Protocols",
100           "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
101           DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
102
103   g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
104       g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
105           "Send an EOS event to the pipeline before unpreparing",
106           DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
107
108   g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
109       g_param_spec_uint ("buffer-size", "Buffer Size",
110           "The kernel UDP buffer size to use", 0, G_MAXUINT,
111           DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
112
113   gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
114       g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
115       G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
116       g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
117
118   gst_rtsp_media_signals[SIGNAL_PREPARED] =
119       g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
120       G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
121       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
122
123   gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
124       g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
125       G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
126       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
127
128   gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
129       g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
130       G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
131       g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
132
133   klass->context = g_main_context_new ();
134   klass->loop = g_main_loop_new (klass->context, TRUE);
135
136   GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
137
138   klass->thread = g_thread_new ("Bus Thread", (GThreadFunc) do_loop, klass);
139
140   klass->handle_message = default_handle_message;
141   klass->unprepare = default_unprepare;
142 }
143
144 static void
145 gst_rtsp_media_init (GstRTSPMedia * media)
146 {
147   media->streams = g_ptr_array_new_with_free_func (g_object_unref);
148   g_mutex_init (&media->lock);
149   g_cond_init (&media->cond);
150   g_rec_mutex_init (&media->state_lock);
151
152   media->shared = DEFAULT_SHARED;
153   media->reusable = DEFAULT_REUSABLE;
154   media->protocols = DEFAULT_PROTOCOLS;
155   media->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
156   media->buffer_size = DEFAULT_BUFFER_SIZE;
157 }
158
159 static void
160 gst_rtsp_media_finalize (GObject * obj)
161 {
162   GstRTSPMedia *media;
163
164   media = GST_RTSP_MEDIA (obj);
165
166   GST_INFO ("finalize media %p", media);
167
168   gst_rtsp_media_unprepare (media);
169
170   g_ptr_array_unref (media->streams);
171
172   g_list_free_full (media->dynamic, gst_object_unref);
173
174   if (media->auth)
175     g_object_unref (media->auth);
176   if (media->pool)
177     g_object_unref (media->pool);
178   g_mutex_clear (&media->lock);
179   g_cond_clear (&media->cond);
180   g_rec_mutex_clear (&media->state_lock);
181
182   G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
183 }
184
185 static void
186 gst_rtsp_media_get_property (GObject * object, guint propid,
187     GValue * value, GParamSpec * pspec)
188 {
189   GstRTSPMedia *media = GST_RTSP_MEDIA (object);
190
191   switch (propid) {
192     case PROP_SHARED:
193       g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
194       break;
195     case PROP_REUSABLE:
196       g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
197       break;
198     case PROP_PROTOCOLS:
199       g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
200       break;
201     case PROP_EOS_SHUTDOWN:
202       g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
203       break;
204     case PROP_BUFFER_SIZE:
205       g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
206       break;
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
209   }
210 }
211
212 static void
213 gst_rtsp_media_set_property (GObject * object, guint propid,
214     const GValue * value, GParamSpec * pspec)
215 {
216   GstRTSPMedia *media = GST_RTSP_MEDIA (object);
217
218   switch (propid) {
219     case PROP_SHARED:
220       gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
221       break;
222     case PROP_REUSABLE:
223       gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
224       break;
225     case PROP_PROTOCOLS:
226       gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
227       break;
228     case PROP_EOS_SHUTDOWN:
229       gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
230       break;
231     case PROP_BUFFER_SIZE:
232       gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
233       break;
234     default:
235       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
236   }
237 }
238
239 static gpointer
240 do_loop (GstRTSPMediaClass * klass)
241 {
242   GST_INFO ("enter mainloop");
243   g_main_loop_run (klass->loop);
244   GST_INFO ("exit mainloop");
245
246   return NULL;
247 }
248
249 /* must be called with state lock */
250 static void
251 collect_media_stats (GstRTSPMedia * media)
252 {
253   gint64 position, duration;
254
255   media->range.unit = GST_RTSP_RANGE_NPT;
256
257   GST_INFO ("collect media stats");
258
259   if (media->is_live) {
260     media->range.min.type = GST_RTSP_TIME_NOW;
261     media->range.min.seconds = -1;
262     media->range_start = -1;
263     media->range.max.type = GST_RTSP_TIME_END;
264     media->range.max.seconds = -1;
265     media->range_stop = -1;
266   } else {
267     /* get the position */
268     if (!gst_element_query_position (media->pipeline, GST_FORMAT_TIME,
269             &position)) {
270       GST_INFO ("position query failed");
271       position = 0;
272     }
273
274     /* get the duration */
275     if (!gst_element_query_duration (media->pipeline, GST_FORMAT_TIME,
276             &duration)) {
277       GST_INFO ("duration query failed");
278       duration = -1;
279     }
280
281     GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
282         GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
283
284     if (position == -1) {
285       media->range.min.type = GST_RTSP_TIME_NOW;
286       media->range.min.seconds = -1;
287       media->range_start = -1;
288     } else {
289       media->range.min.type = GST_RTSP_TIME_SECONDS;
290       media->range.min.seconds = ((gdouble) position) / GST_SECOND;
291       media->range_start = position;
292     }
293     if (duration == -1) {
294       media->range.max.type = GST_RTSP_TIME_END;
295       media->range.max.seconds = -1;
296       media->range_stop = -1;
297     } else {
298       media->range.max.type = GST_RTSP_TIME_SECONDS;
299       media->range.max.seconds = ((gdouble) duration) / GST_SECOND;
300       media->range_stop = duration;
301     }
302   }
303 }
304
305 /**
306  * gst_rtsp_media_new:
307  *
308  * Create a new #GstRTSPMedia instance. The #GstRTSPMedia object contains the
309  * element to produce RTP data for one or more related (audio/video/..)
310  * streams.
311  *
312  * Returns: a new #GstRTSPMedia object.
313  */
314 GstRTSPMedia *
315 gst_rtsp_media_new (void)
316 {
317   GstRTSPMedia *result;
318
319   result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
320
321   return result;
322 }
323
324 /**
325  * gst_rtsp_media_set_shared:
326  * @media: a #GstRTSPMedia
327  * @shared: the new value
328  *
329  * Set or unset if the pipeline for @media can be shared will multiple clients.
330  * When @shared is %TRUE, client requests for this media will share the media
331  * pipeline.
332  */
333 void
334 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
335 {
336   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
337
338   g_mutex_lock (&media->lock);
339   media->shared = shared;
340   g_mutex_unlock (&media->lock);
341 }
342
343 /**
344  * gst_rtsp_media_is_shared:
345  * @media: a #GstRTSPMedia
346  *
347  * Check if the pipeline for @media can be shared between multiple clients.
348  *
349  * Returns: %TRUE if the media can be shared between clients.
350  */
351 gboolean
352 gst_rtsp_media_is_shared (GstRTSPMedia * media)
353 {
354   gboolean res;
355
356   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
357
358   g_mutex_lock (&media->lock);
359   res = media->shared;
360   g_mutex_unlock (&media->lock);
361
362   return res;
363 }
364
365 /**
366  * gst_rtsp_media_set_reusable:
367  * @media: a #GstRTSPMedia
368  * @reusable: the new value
369  *
370  * Set or unset if the pipeline for @media can be reused after the pipeline has
371  * been unprepared.
372  */
373 void
374 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
375 {
376   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
377
378   g_mutex_lock (&media->lock);
379   media->reusable = reusable;
380   g_mutex_unlock (&media->lock);
381 }
382
383 /**
384  * gst_rtsp_media_is_reusable:
385  * @media: a #GstRTSPMedia
386  *
387  * Check if the pipeline for @media can be reused after an unprepare.
388  *
389  * Returns: %TRUE if the media can be reused
390  */
391 gboolean
392 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
393 {
394   gboolean res;
395
396   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
397
398   g_mutex_lock (&media->lock);
399   res = media->reusable;
400   g_mutex_unlock (&media->lock);
401
402   return res;
403 }
404
405 /**
406  * gst_rtsp_media_set_protocols:
407  * @media: a #GstRTSPMedia
408  * @protocols: the new flags
409  *
410  * Configure the allowed lower transport for @media.
411  */
412 void
413 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
414 {
415   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
416
417   g_mutex_lock (&media->lock);
418   media->protocols = protocols;
419   g_mutex_unlock (&media->lock);
420 }
421
422 /**
423  * gst_rtsp_media_get_protocols:
424  * @media: a #GstRTSPMedia
425  *
426  * Get the allowed protocols of @media.
427  *
428  * Returns: a #GstRTSPLowerTrans
429  */
430 GstRTSPLowerTrans
431 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
432 {
433   GstRTSPLowerTrans res;
434
435   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
436       GST_RTSP_LOWER_TRANS_UNKNOWN);
437
438   g_mutex_lock (&media->lock);
439   res = media->protocols;
440   g_mutex_unlock (&media->lock);
441
442   return res;
443 }
444
445 /**
446  * gst_rtsp_media_set_eos_shutdown:
447  * @media: a #GstRTSPMedia
448  * @eos_shutdown: the new value
449  *
450  * Set or unset if an EOS event will be sent to the pipeline for @media before
451  * it is unprepared.
452  */
453 void
454 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
455 {
456   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
457
458   g_mutex_lock (&media->lock);
459   media->eos_shutdown = eos_shutdown;
460   g_mutex_unlock (&media->lock);
461 }
462
463 /**
464  * gst_rtsp_media_is_eos_shutdown:
465  * @media: a #GstRTSPMedia
466  *
467  * Check if the pipeline for @media will send an EOS down the pipeline before
468  * unpreparing.
469  *
470  * Returns: %TRUE if the media will send EOS before unpreparing.
471  */
472 gboolean
473 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
474 {
475   gboolean res;
476
477   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
478
479   g_mutex_lock (&media->lock);
480   res = media->eos_shutdown;
481   g_mutex_unlock (&media->lock);
482
483   return res;
484 }
485
486 /**
487  * gst_rtsp_media_set_buffer_size:
488  * @media: a #GstRTSPMedia
489  * @size: the new value
490  *
491  * Set the kernel UDP buffer size.
492  */
493 void
494 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
495 {
496   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
497
498   GST_LOG_OBJECT (media, "set buffer size %u", size);
499
500   g_mutex_lock (&media->lock);
501   media->buffer_size = size;
502   g_mutex_unlock (&media->lock);
503 }
504
505 /**
506  * gst_rtsp_media_get_buffer_size:
507  * @media: a #GstRTSPMedia
508  *
509  * Get the kernel UDP buffer size.
510  *
511  * Returns: the kernel UDP buffer size.
512  */
513 guint
514 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
515 {
516   guint res;
517
518   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
519
520   g_mutex_unlock (&media->lock);
521   res = media->buffer_size;
522   g_mutex_unlock (&media->lock);
523
524   return res;
525 }
526
527 /**
528  * gst_rtsp_media_set_auth:
529  * @media: a #GstRTSPMedia
530  * @auth: a #GstRTSPAuth
531  *
532  * configure @auth to be used as the authentication manager of @media.
533  */
534 void
535 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
536 {
537   GstRTSPAuth *old;
538
539   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
540
541   GST_LOG_OBJECT (media, "set auth %p", auth);
542
543   g_mutex_lock (&media->lock);
544   if ((old = media->auth) != auth)
545     media->auth = auth ? g_object_ref (auth) : NULL;
546   else
547     old = NULL;
548   g_mutex_unlock (&media->lock);
549
550   if (old)
551     g_object_unref (old);
552 }
553
554 /**
555  * gst_rtsp_media_get_auth:
556  * @media: a #GstRTSPMedia
557  *
558  * Get the #GstRTSPAuth used as the authentication manager of @media.
559  *
560  * Returns: (transfer full): the #GstRTSPAuth of @media. g_object_unref() after
561  * usage.
562  */
563 GstRTSPAuth *
564 gst_rtsp_media_get_auth (GstRTSPMedia * media)
565 {
566   GstRTSPAuth *result;
567
568   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
569
570   g_mutex_lock (&media->lock);
571   if ((result = media->auth))
572     g_object_ref (result);
573   g_mutex_unlock (&media->lock);
574
575   return result;
576 }
577
578 /**
579  * gst_rtsp_media_set_address_pool:
580  * @media: a #GstRTSPMedia
581  * @pool: a #GstRTSPAddressPool
582  *
583  * configure @pool to be used as the address pool of @media.
584  */
585 void
586 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
587     GstRTSPAddressPool * pool)
588 {
589   GstRTSPAddressPool *old;
590
591   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
592
593   GST_LOG_OBJECT (media, "set address pool %p", pool);
594
595   g_mutex_lock (&media->lock);
596   if ((old = media->pool) != pool)
597     media->pool = pool ? g_object_ref (pool) : NULL;
598   else
599     old = NULL;
600   g_ptr_array_foreach (media->streams, (GFunc) gst_rtsp_stream_set_address_pool,
601       pool);
602   g_mutex_unlock (&media->lock);
603
604   if (old)
605     g_object_unref (old);
606 }
607
608 /**
609  * gst_rtsp_media_get_address_pool:
610  * @media: a #GstRTSPMedia
611  *
612  * Get the #GstRTSPAddressPool used as the address pool of @media.
613  *
614  * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
615  * usage.
616  */
617 GstRTSPAddressPool *
618 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
619 {
620   GstRTSPAddressPool *result;
621
622   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
623
624   g_mutex_lock (&media->lock);
625   if ((result = media->pool))
626     g_object_ref (result);
627   g_mutex_unlock (&media->lock);
628
629   return result;
630 }
631
632 /**
633  * gst_rtsp_media_collect_streams:
634  * @media: a #GstRTSPMedia
635  *
636  * Find all payloader elements, they should be named pay%d in the
637  * element of @media, and create #GstRTSPStreams for them.
638  *
639  * Collect all dynamic elements, named dynpay%d, and add them to
640  * the list of dynamic elements.
641  */
642 void
643 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
644 {
645   GstElement *element, *elem;
646   GstPad *pad;
647   gint i;
648   gboolean have_elem;
649
650   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
651
652   element = media->element;
653
654   have_elem = TRUE;
655   for (i = 0; have_elem; i++) {
656     gchar *name;
657
658     have_elem = FALSE;
659
660     name = g_strdup_printf ("pay%d", i);
661     if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
662       GST_INFO ("found stream %d with payloader %p", i, elem);
663
664       /* take the pad of the payloader */
665       pad = gst_element_get_static_pad (elem, "src");
666       /* create the stream */
667       gst_rtsp_media_create_stream (media, elem, pad);
668       g_object_unref (pad);
669
670       gst_object_unref (elem);
671
672       have_elem = TRUE;
673     }
674     g_free (name);
675
676     name = g_strdup_printf ("dynpay%d", i);
677     if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
678       /* a stream that will dynamically create pads to provide RTP packets */
679
680       GST_INFO ("found dynamic element %d, %p", i, elem);
681
682       g_mutex_lock (&media->lock);
683       media->dynamic = g_list_prepend (media->dynamic, elem);
684       g_mutex_unlock (&media->lock);
685
686       have_elem = TRUE;
687     }
688     g_free (name);
689   }
690 }
691
692 /**
693  * gst_rtsp_media_create_stream:
694  * @media: a #GstRTSPMedia
695  * @payloader: a #GstElement
696  * @srcpad: a source #GstPad
697  *
698  * Create a new stream in @media that provides RTP data on @srcpad.
699  * @srcpad should be a pad of an element inside @media->element.
700  *
701  * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
702  *          as @media exists.
703  */
704 GstRTSPStream *
705 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
706     GstPad * pad)
707 {
708   GstRTSPStream *stream;
709   GstPad *srcpad;
710   gchar *name;
711   gint idx;
712
713   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
714   g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
715   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
716   g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
717
718   g_mutex_lock (&media->lock);
719   idx = media->streams->len;
720
721   name = g_strdup_printf ("src_%u", idx);
722   srcpad = gst_ghost_pad_new (name, pad);
723   gst_pad_set_active (srcpad, TRUE);
724   gst_element_add_pad (media->element, srcpad);
725   g_free (name);
726
727   stream = gst_rtsp_stream_new (idx, payloader, srcpad);
728   if (media->pool)
729     gst_rtsp_stream_set_address_pool (stream, media->pool);
730
731   g_ptr_array_add (media->streams, stream);
732   g_mutex_unlock (&media->lock);
733
734   g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
735       NULL);
736
737   return stream;
738 }
739
740 /**
741  * gst_rtsp_media_n_streams:
742  * @media: a #GstRTSPMedia
743  *
744  * Get the number of streams in this media.
745  *
746  * Returns: The number of streams.
747  */
748 guint
749 gst_rtsp_media_n_streams (GstRTSPMedia * media)
750 {
751   guint res;
752
753   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
754
755   g_mutex_lock (&media->lock);
756   res = media->streams->len;
757   g_mutex_unlock (&media->lock);
758
759   return res;
760 }
761
762 /**
763  * gst_rtsp_media_get_stream:
764  * @media: a #GstRTSPMedia
765  * @idx: the stream index
766  *
767  * Retrieve the stream with index @idx from @media.
768  *
769  * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
770  * that index did not exist.
771  */
772 GstRTSPStream *
773 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
774 {
775   GstRTSPStream *res;
776
777   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
778
779   g_mutex_lock (&media->lock);
780   if (idx < media->streams->len)
781     res = g_ptr_array_index (media->streams, idx);
782   else
783     res = NULL;
784   g_mutex_unlock (&media->lock);
785
786   return res;
787 }
788
789 /**
790  * gst_rtsp_media_get_range_string:
791  * @media: a #GstRTSPMedia
792  * @play: for the PLAY request
793  *
794  * Get the current range as a string.
795  *
796  * Returns: The range as a string, g_free() after usage.
797  */
798 gchar *
799 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play)
800 {
801   gchar *result;
802   GstRTSPTimeRange range;
803
804   g_mutex_lock (&media->lock);
805   /* make copy */
806   range = media->range;
807
808   if (!play && media->n_active > 0) {
809     range.min.type = GST_RTSP_TIME_NOW;
810     range.min.seconds = -1;
811   }
812   g_mutex_unlock (&media->lock);
813
814   result = gst_rtsp_range_to_string (&range);
815
816   return result;
817 }
818
819 /**
820  * gst_rtsp_media_seek:
821  * @media: a #GstRTSPMedia
822  * @range: a #GstRTSPTimeRange
823  *
824  * Seek the pipeline to @range.
825  *
826  * Returns: %TRUE on success.
827  */
828 gboolean
829 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
830 {
831   GstSeekFlags flags;
832   gboolean res;
833   GstClockTime start, stop;
834   GstSeekType start_type, stop_type;
835
836   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
837   g_return_val_if_fail (range != NULL, FALSE);
838
839   g_rec_mutex_lock (&media->state_lock);
840   if (!media->seekable)
841     goto not_seekable;
842
843   /* depends on the current playing state of the pipeline. We might need to
844    * queue this until we get EOS. */
845   flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
846
847   start_type = stop_type = GST_SEEK_TYPE_NONE;
848
849   if (!gst_rtsp_range_get_times (range, &start, &stop))
850     goto not_supported;
851
852   GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
853       GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
854   GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
855       GST_TIME_ARGS (media->range_start), GST_TIME_ARGS (media->range_stop));
856
857   if (media->range_start == start)
858     start = GST_CLOCK_TIME_NONE;
859   else if (start != GST_CLOCK_TIME_NONE)
860     start_type = GST_SEEK_TYPE_SET;
861
862   if (media->range_stop == stop)
863     stop = GST_CLOCK_TIME_NONE;
864   else if (stop != GST_CLOCK_TIME_NONE)
865     stop_type = GST_SEEK_TYPE_SET;
866
867   if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
868     GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
869         GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
870
871     res = gst_element_seek (media->pipeline, 1.0, GST_FORMAT_TIME,
872         flags, start_type, start, stop_type, stop);
873
874     /* and block for the seek to complete */
875     GST_INFO ("done seeking %d", res);
876     gst_element_get_state (media->pipeline, NULL, NULL, -1);
877     GST_INFO ("prerolled again");
878
879     collect_media_stats (media);
880   } else {
881     GST_INFO ("no seek needed");
882     res = TRUE;
883   }
884   g_rec_mutex_unlock (&media->state_lock);
885
886   return res;
887
888   /* ERRORS */
889 not_seekable:
890   {
891     g_rec_mutex_unlock (&media->state_lock);
892     GST_INFO ("pipeline is not seekable");
893     return TRUE;
894   }
895 not_supported:
896   {
897     g_rec_mutex_unlock (&media->state_lock);
898     GST_WARNING ("seek unit %d not supported", range->unit);
899     return FALSE;
900   }
901 }
902
903 static void
904 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
905 {
906   g_mutex_lock (&media->lock);
907   /* never overwrite the error status */
908   if (media->status != GST_RTSP_MEDIA_STATUS_ERROR)
909     media->status = status;
910   GST_DEBUG ("setting new status to %d", status);
911   g_cond_broadcast (&media->cond);
912   g_mutex_unlock (&media->lock);
913 }
914
915 static GstRTSPMediaStatus
916 gst_rtsp_media_get_status (GstRTSPMedia * media)
917 {
918   GstRTSPMediaStatus result;
919   gint64 end_time;
920
921   g_mutex_lock (&media->lock);
922   end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
923   /* while we are preparing, wait */
924   while (media->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
925     GST_DEBUG ("waiting for status change");
926     if (!g_cond_wait_until (&media->cond, &media->lock, end_time)) {
927       GST_DEBUG ("timeout, assuming error status");
928       media->status = GST_RTSP_MEDIA_STATUS_ERROR;
929     }
930   }
931   /* could be success or error */
932   result = media->status;
933   GST_DEBUG ("got status %d", result);
934   g_mutex_unlock (&media->lock);
935
936   return result;
937 }
938
939 /* called with state-lock */
940 static gboolean
941 default_handle_message (GstRTSPMedia * media, GstMessage * message)
942 {
943   GstMessageType type;
944
945   type = GST_MESSAGE_TYPE (message);
946
947   switch (type) {
948     case GST_MESSAGE_STATE_CHANGED:
949       break;
950     case GST_MESSAGE_BUFFERING:
951     {
952       gint percent;
953
954       gst_message_parse_buffering (message, &percent);
955
956       /* no state management needed for live pipelines */
957       if (media->is_live)
958         break;
959
960       if (percent == 100) {
961         /* a 100% message means buffering is done */
962         media->buffering = FALSE;
963         /* if the desired state is playing, go back */
964         if (media->target_state == GST_STATE_PLAYING) {
965           GST_INFO ("Buffering done, setting pipeline to PLAYING");
966           gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
967         } else {
968           GST_INFO ("Buffering done");
969         }
970       } else {
971         /* buffering busy */
972         if (media->buffering == FALSE) {
973           if (media->target_state == GST_STATE_PLAYING) {
974             /* we were not buffering but PLAYING, PAUSE  the pipeline. */
975             GST_INFO ("Buffering, setting pipeline to PAUSED ...");
976             gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
977           } else {
978             GST_INFO ("Buffering ...");
979           }
980         }
981         media->buffering = TRUE;
982       }
983       break;
984     }
985     case GST_MESSAGE_LATENCY:
986     {
987       gst_bin_recalculate_latency (GST_BIN_CAST (media->pipeline));
988       break;
989     }
990     case GST_MESSAGE_ERROR:
991     {
992       GError *gerror;
993       gchar *debug;
994
995       gst_message_parse_error (message, &gerror, &debug);
996       GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
997       g_error_free (gerror);
998       g_free (debug);
999
1000       gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1001       break;
1002     }
1003     case GST_MESSAGE_WARNING:
1004     {
1005       GError *gerror;
1006       gchar *debug;
1007
1008       gst_message_parse_warning (message, &gerror, &debug);
1009       GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1010       g_error_free (gerror);
1011       g_free (debug);
1012       break;
1013     }
1014     case GST_MESSAGE_ELEMENT:
1015       break;
1016     case GST_MESSAGE_STREAM_STATUS:
1017       break;
1018     case GST_MESSAGE_ASYNC_DONE:
1019       if (!media->adding) {
1020         /* when we are dynamically adding pads, the addition of the udpsrc will
1021          * temporarily produce ASYNC_DONE messages. We have to ignore them and
1022          * wait for the final ASYNC_DONE after everything prerolled */
1023         GST_INFO ("%p: got ASYNC_DONE", media);
1024         collect_media_stats (media);
1025
1026         gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1027       } else {
1028         GST_INFO ("%p: ignoring ASYNC_DONE", media);
1029       }
1030       break;
1031     case GST_MESSAGE_EOS:
1032       GST_INFO ("%p: got EOS", media);
1033
1034       if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1035         GST_DEBUG ("shutting down after EOS");
1036         finish_unprepare (media);
1037         g_object_unref (media);
1038       }
1039       break;
1040     default:
1041       GST_INFO ("%p: got message type %s", media,
1042           gst_message_type_get_name (type));
1043       break;
1044   }
1045   return TRUE;
1046 }
1047
1048 static gboolean
1049 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1050 {
1051   GstRTSPMediaClass *klass;
1052   gboolean ret;
1053
1054   klass = GST_RTSP_MEDIA_GET_CLASS (media);
1055
1056   g_rec_mutex_lock (&media->state_lock);
1057   if (klass->handle_message)
1058     ret = klass->handle_message (media, message);
1059   else
1060     ret = FALSE;
1061   g_rec_mutex_unlock (&media->state_lock);
1062
1063   return ret;
1064 }
1065
1066 static void
1067 watch_destroyed (GstRTSPMedia * media)
1068 {
1069   GST_DEBUG_OBJECT (media, "source destroyed");
1070   gst_object_unref (media);
1071 }
1072
1073 /* called from streaming threads */
1074 static void
1075 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1076 {
1077   GstRTSPStream *stream;
1078
1079   /* FIXME, element is likely not a payloader, find the payloader here */
1080   stream = gst_rtsp_media_create_stream (media, element, pad);
1081
1082   GST_INFO ("pad added %s:%s, stream %d", GST_DEBUG_PAD_NAME (pad),
1083       stream->idx);
1084
1085   g_rec_mutex_lock (&media->state_lock);
1086   /* we will be adding elements below that will cause ASYNC_DONE to be
1087    * posted in the bus. We want to ignore those messages until the
1088    * pipeline really prerolled. */
1089   media->adding = TRUE;
1090
1091   /* join the element in the PAUSED state because this callback is
1092    * called from the streaming thread and it is PAUSED */
1093   gst_rtsp_stream_join_bin (stream, GST_BIN (media->pipeline),
1094       media->rtpbin, GST_STATE_PAUSED);
1095
1096   media->adding = FALSE;
1097   g_rec_mutex_unlock (&media->state_lock);
1098 }
1099
1100 static void
1101 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1102 {
1103   GstElement *fakesink;
1104
1105   g_mutex_lock (&media->lock);
1106   GST_INFO ("no more pads");
1107   if ((fakesink = media->fakesink)) {
1108     gst_object_ref (fakesink);
1109     media->fakesink = NULL;
1110     g_mutex_unlock (&media->lock);
1111
1112     gst_bin_remove (GST_BIN (media->pipeline), fakesink);
1113     gst_element_set_state (fakesink, GST_STATE_NULL);
1114     gst_object_unref (fakesink);
1115     GST_INFO ("removed fakesink");
1116   }
1117 }
1118
1119 /**
1120  * gst_rtsp_media_prepare:
1121  * @media: a #GstRTSPMedia
1122  *
1123  * Prepare @media for streaming. This function will create the pipeline and
1124  * other objects to manage the streaming.
1125  *
1126  * It will preroll the pipeline and collect vital information about the streams
1127  * such as the duration.
1128  *
1129  * Returns: %TRUE on success.
1130  */
1131 gboolean
1132 gst_rtsp_media_prepare (GstRTSPMedia * media)
1133 {
1134   GstStateChangeReturn ret;
1135   GstRTSPMediaStatus status;
1136   guint i;
1137   GstRTSPMediaClass *klass;
1138   GstBus *bus;
1139   GList *walk;
1140
1141   g_rec_mutex_lock (&media->state_lock);
1142   if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1143     goto was_prepared;
1144
1145   if (media->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1146     goto wait_status;
1147
1148   if (media->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
1149     goto not_unprepared;
1150
1151   if (!media->reusable && media->reused)
1152     goto is_reused;
1153
1154   media->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1155   if (media->rtpbin == NULL)
1156     goto no_rtpbin;
1157
1158   GST_INFO ("preparing media %p", media);
1159
1160   /* reset some variables */
1161   media->is_live = FALSE;
1162   media->seekable = FALSE;
1163   media->buffering = FALSE;
1164   /* we're preparing now */
1165   media->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1166
1167   bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (media->pipeline));
1168
1169   /* add the pipeline bus to our custom mainloop */
1170   media->source = gst_bus_create_watch (bus);
1171   gst_object_unref (bus);
1172
1173   g_source_set_callback (media->source, (GSourceFunc) bus_message,
1174       gst_object_ref (media), (GDestroyNotify) watch_destroyed);
1175
1176   klass = GST_RTSP_MEDIA_GET_CLASS (media);
1177   media->id = g_source_attach (media->source, klass->context);
1178
1179   /* add stuff to the bin */
1180   gst_bin_add (GST_BIN (media->pipeline), media->rtpbin);
1181
1182   /* link streams we already have, other streams might appear when we have
1183    * dynamic elements */
1184   for (i = 0; i < media->streams->len; i++) {
1185     GstRTSPStream *stream;
1186
1187     stream = g_ptr_array_index (media->streams, i);
1188
1189     gst_rtsp_stream_join_bin (stream, GST_BIN (media->pipeline),
1190         media->rtpbin, GST_STATE_NULL);
1191   }
1192
1193   for (walk = media->dynamic; walk; walk = g_list_next (walk)) {
1194     GstElement *elem = walk->data;
1195
1196     GST_INFO ("adding callbacks for dynamic element %p", elem);
1197
1198     g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1199     g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1200
1201     /* we add a fakesink here in order to make the state change async. We remove
1202      * the fakesink again in the no-more-pads callback. */
1203     media->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1204     gst_bin_add (GST_BIN (media->pipeline), media->fakesink);
1205   }
1206
1207   GST_INFO ("setting pipeline to PAUSED for media %p", media);
1208   /* first go to PAUSED */
1209   ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1210   media->target_state = GST_STATE_PAUSED;
1211
1212   switch (ret) {
1213     case GST_STATE_CHANGE_SUCCESS:
1214       GST_INFO ("SUCCESS state change for media %p", media);
1215       media->seekable = TRUE;
1216       break;
1217     case GST_STATE_CHANGE_ASYNC:
1218       GST_INFO ("ASYNC state change for media %p", media);
1219       media->seekable = TRUE;
1220       break;
1221     case GST_STATE_CHANGE_NO_PREROLL:
1222       /* we need to go to PLAYING */
1223       GST_INFO ("NO_PREROLL state change: live media %p", media);
1224       /* FIXME we disable seeking for live streams for now. We should perform a
1225        * seeking query in preroll instead */
1226       media->seekable = FALSE;
1227       media->is_live = TRUE;
1228       ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1229       if (ret == GST_STATE_CHANGE_FAILURE)
1230         goto state_failed;
1231       break;
1232     case GST_STATE_CHANGE_FAILURE:
1233       goto state_failed;
1234   }
1235 wait_status:
1236   g_rec_mutex_unlock (&media->state_lock);
1237
1238   /* now wait for all pads to be prerolled, FIXME, we should somehow be
1239    * able to do this async so that we don't block the server thread. */
1240   status = gst_rtsp_media_get_status (media);
1241   if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1242     goto state_failed;
1243
1244   g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1245
1246   GST_INFO ("object %p is prerolled", media);
1247
1248   return TRUE;
1249
1250   /* OK */
1251 was_prepared:
1252   {
1253     GST_LOG ("media %p was prepared", media);
1254     g_rec_mutex_unlock (&media->state_lock);
1255     return TRUE;
1256   }
1257   /* ERRORS */
1258 not_unprepared:
1259   {
1260     GST_WARNING ("media %p was not unprepared", media);
1261     g_rec_mutex_unlock (&media->state_lock);
1262     return FALSE;
1263   }
1264 is_reused:
1265   {
1266     g_rec_mutex_unlock (&media->state_lock);
1267     GST_WARNING ("can not reuse media %p", media);
1268     return FALSE;
1269   }
1270 no_rtpbin:
1271   {
1272     g_rec_mutex_unlock (&media->state_lock);
1273     GST_WARNING ("no rtpbin element");
1274     g_warning ("failed to create element 'rtpbin', check your installation");
1275     return FALSE;
1276   }
1277 state_failed:
1278   {
1279     GST_WARNING ("failed to preroll pipeline");
1280     gst_rtsp_media_unprepare (media);
1281     g_rec_mutex_unlock (&media->state_lock);
1282     return FALSE;
1283   }
1284 }
1285
1286 /* must be called with state-lock */
1287 static void
1288 finish_unprepare (GstRTSPMedia * media)
1289 {
1290   gint i;
1291
1292   GST_DEBUG ("shutting down");
1293
1294   gst_element_set_state (media->pipeline, GST_STATE_NULL);
1295
1296   for (i = 0; i < media->streams->len; i++) {
1297     GstRTSPStream *stream;
1298
1299     GST_INFO ("Removing elements of stream %d from pipeline", i);
1300
1301     stream = g_ptr_array_index (media->streams, i);
1302
1303     gst_rtsp_stream_leave_bin (stream, GST_BIN (media->pipeline),
1304         media->rtpbin);
1305   }
1306   g_ptr_array_set_size (media->streams, 0);
1307
1308   gst_bin_remove (GST_BIN (media->pipeline), media->rtpbin);
1309   media->rtpbin = NULL;
1310
1311   gst_object_unref (media->pipeline);
1312   media->pipeline = NULL;
1313
1314   media->reused = TRUE;
1315   media->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1316
1317   if (media->source) {
1318     g_source_destroy (media->source);
1319     g_source_unref (media->source);
1320     media->source = NULL;
1321   }
1322
1323   /* when the media is not reusable, this will effectively unref the media and
1324    * recreate it */
1325   g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1326 }
1327
1328 /* called with state-lock */
1329 static gboolean
1330 default_unprepare (GstRTSPMedia * media)
1331 {
1332   if (media->eos_shutdown) {
1333     GST_DEBUG ("sending EOS for shutdown");
1334     /* ref so that we don't disappear */
1335     g_object_ref (media);
1336     gst_element_send_event (media->pipeline, gst_event_new_eos ());
1337     /* we need to go to playing again for the EOS to propagate, normally in this
1338      * state, nothing is receiving data from us anymore so this is ok. */
1339     gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1340     media->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
1341   } else {
1342     finish_unprepare (media);
1343   }
1344   return TRUE;
1345 }
1346
1347 /**
1348  * gst_rtsp_media_unprepare:
1349  * @media: a #GstRTSPMedia
1350  *
1351  * Unprepare @media. After this call, the media should be prepared again before
1352  * it can be used again. If the media is set to be non-reusable, a new instance
1353  * must be created.
1354  *
1355  * Returns: %TRUE on success.
1356  */
1357 gboolean
1358 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1359 {
1360   gboolean success;
1361
1362   g_rec_mutex_lock (&media->state_lock);
1363   if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1364     goto was_unprepared;
1365
1366   GST_INFO ("unprepare media %p", media);
1367   media->target_state = GST_STATE_NULL;
1368   success = TRUE;
1369
1370   if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
1371     GstRTSPMediaClass *klass;
1372
1373     klass = GST_RTSP_MEDIA_GET_CLASS (media);
1374     if (klass->unprepare)
1375       success = klass->unprepare (media);
1376   } else {
1377     finish_unprepare (media);
1378   }
1379   g_rec_mutex_unlock (&media->state_lock);
1380
1381   return success;
1382
1383 was_unprepared:
1384   {
1385     g_rec_mutex_unlock (&media->state_lock);
1386     GST_INFO ("media %p was already unprepared", media);
1387     return TRUE;
1388   }
1389 }
1390
1391 /**
1392  * gst_rtsp_media_set_state:
1393  * @media: a #GstRTSPMedia
1394  * @state: the target state of the media
1395  * @transports: a #GPtrArray of #GstRTSPStreamTransport pointers
1396  *
1397  * Set the state of @media to @state and for the transports in @transports.
1398  *
1399  * Returns: %TRUE on success.
1400  */
1401 gboolean
1402 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1403     GPtrArray * transports)
1404 {
1405   gint i;
1406   gboolean add, remove, do_state;
1407   gint old_active;
1408
1409   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1410   g_return_val_if_fail (transports != NULL, FALSE);
1411
1412   g_rec_mutex_lock (&media->state_lock);
1413
1414   /* NULL and READY are the same */
1415   if (state == GST_STATE_READY)
1416     state = GST_STATE_NULL;
1417
1418   add = remove = FALSE;
1419
1420   GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1421       media);
1422
1423   switch (state) {
1424     case GST_STATE_NULL:
1425     case GST_STATE_PAUSED:
1426       /* we're going from PLAYING to PAUSED, READY or NULL, remove */
1427       if (media->target_state == GST_STATE_PLAYING)
1428         remove = TRUE;
1429       break;
1430     case GST_STATE_PLAYING:
1431       /* we're going to PLAYING, add */
1432       add = TRUE;
1433       break;
1434     default:
1435       break;
1436   }
1437   old_active = media->n_active;
1438
1439   for (i = 0; i < transports->len; i++) {
1440     GstRTSPStreamTransport *trans;
1441
1442     /* we need a non-NULL entry in the array */
1443     trans = g_ptr_array_index (transports, i);
1444     if (trans == NULL)
1445       continue;
1446
1447     /* we need a transport */
1448     if (!trans->transport)
1449       continue;
1450
1451     if (add) {
1452       if (gst_rtsp_stream_add_transport (trans->stream, trans))
1453         media->n_active++;
1454     } else if (remove) {
1455       if (gst_rtsp_stream_remove_transport (trans->stream, trans))
1456         media->n_active--;
1457     }
1458   }
1459
1460   /* we just added the first media, do the playing state change */
1461   if (old_active == 0 && add)
1462     do_state = TRUE;
1463   /* if we have no more active media, do the downward state changes */
1464   else if (media->n_active == 0)
1465     do_state = TRUE;
1466   else
1467     do_state = FALSE;
1468
1469   GST_INFO ("state %d active %d media %p do_state %d", state, media->n_active,
1470       media, do_state);
1471
1472   if (media->target_state != state) {
1473     if (do_state) {
1474       if (state == GST_STATE_NULL) {
1475         gst_rtsp_media_unprepare (media);
1476       } else {
1477         GST_INFO ("state %s media %p", gst_element_state_get_name (state),
1478             media);
1479         media->target_state = state;
1480         gst_element_set_state (media->pipeline, state);
1481       }
1482     }
1483     g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
1484         NULL);
1485   }
1486
1487   /* remember where we are */
1488   if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
1489           old_active != media->n_active))
1490     collect_media_stats (media);
1491
1492   g_rec_mutex_unlock (&media->state_lock);
1493
1494   return TRUE;
1495 }