media: port to new caps API
[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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, 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_PREPARED,
52   SIGNAL_UNPREPARED,
53   SIGNAL_NEW_STATE,
54   SIGNAL_LAST
55 };
56
57 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
58 #define GST_CAT_DEFAULT rtsp_media_debug
59
60 static GQuark ssrc_stream_map_key;
61
62 static void gst_rtsp_media_get_property (GObject * object, guint propid,
63     GValue * value, GParamSpec * pspec);
64 static void gst_rtsp_media_set_property (GObject * object, guint propid,
65     const GValue * value, GParamSpec * pspec);
66 static void gst_rtsp_media_finalize (GObject * obj);
67
68 static gpointer do_loop (GstRTSPMediaClass * klass);
69 static gboolean default_handle_message (GstRTSPMedia * media,
70     GstMessage * message);
71 static gboolean default_unprepare (GstRTSPMedia * media);
72 static void unlock_streams (GstRTSPMedia * media);
73
74 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
75
76 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
77
78 static void
79 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
80 {
81   GObjectClass *gobject_class;
82   GError *error = NULL;
83
84   gobject_class = G_OBJECT_CLASS (klass);
85
86   gobject_class->get_property = gst_rtsp_media_get_property;
87   gobject_class->set_property = gst_rtsp_media_set_property;
88   gobject_class->finalize = gst_rtsp_media_finalize;
89
90   g_object_class_install_property (gobject_class, PROP_SHARED,
91       g_param_spec_boolean ("shared", "Shared",
92           "If this media pipeline can be shared", DEFAULT_SHARED,
93           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
94
95   g_object_class_install_property (gobject_class, PROP_REUSABLE,
96       g_param_spec_boolean ("reusable", "Reusable",
97           "If this media pipeline can be reused after an unprepare",
98           DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
99
100   g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
101       g_param_spec_flags ("protocols", "Protocols",
102           "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
103           DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
104
105   g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
106       g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
107           "Send an EOS event to the pipeline before unpreparing",
108           DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
109
110   g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
111       g_param_spec_uint ("buffer-size", "Buffer Size",
112           "The kernel UDP buffer size to use", 0, G_MAXUINT,
113           DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
114
115   gst_rtsp_media_signals[SIGNAL_PREPARED] =
116       g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
117       G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
118       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
119
120   gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
121       g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
122       G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
123       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
124
125   gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
126       g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
127       G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
128       g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
129
130   klass->context = g_main_context_new ();
131   klass->loop = g_main_loop_new (klass->context, TRUE);
132
133   GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
134
135   klass->thread = g_thread_create ((GThreadFunc) do_loop, klass, TRUE, &error);
136   if (error != NULL) {
137     g_critical ("could not start bus thread: %s", error->message);
138   }
139   klass->handle_message = default_handle_message;
140   klass->unprepare = default_unprepare;
141
142   ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
143 }
144
145 static void
146 gst_rtsp_media_init (GstRTSPMedia * media)
147 {
148   media->streams = g_array_new (FALSE, TRUE, sizeof (GstRTSPMediaStream *));
149   media->lock = g_mutex_new ();
150   media->cond = g_cond_new ();
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 void
160 gst_rtsp_media_trans_cleanup (GstRTSPMediaTrans * trans)
161 {
162   if (trans->transport) {
163     gst_rtsp_transport_free (trans->transport);
164     trans->transport = NULL;
165   }
166   if (trans->rtpsource) {
167     g_object_set_qdata (trans->rtpsource, ssrc_stream_map_key, NULL);
168     trans->rtpsource = NULL;
169   }
170 }
171
172 static void
173 gst_rtsp_media_stream_free (GstRTSPMediaStream * stream)
174 {
175   if (stream->session)
176     g_object_unref (stream->session);
177
178   if (stream->caps)
179     gst_caps_unref (stream->caps);
180
181   if (stream->send_rtp_sink)
182     gst_object_unref (stream->send_rtp_sink);
183   if (stream->send_rtp_src)
184     gst_object_unref (stream->send_rtp_src);
185   if (stream->send_rtcp_src)
186     gst_object_unref (stream->send_rtcp_src);
187   if (stream->recv_rtcp_sink)
188     gst_object_unref (stream->recv_rtcp_sink);
189   if (stream->recv_rtp_sink)
190     gst_object_unref (stream->recv_rtp_sink);
191
192   g_list_free (stream->transports);
193
194   g_free (stream);
195 }
196
197 static void
198 gst_rtsp_media_finalize (GObject * obj)
199 {
200   GstRTSPMedia *media;
201   guint i;
202
203   media = GST_RTSP_MEDIA (obj);
204
205   GST_INFO ("finalize media %p", media);
206
207   if (media->pipeline) {
208     unlock_streams (media);
209     gst_element_set_state (media->pipeline, GST_STATE_NULL);
210     gst_object_unref (media->pipeline);
211   }
212
213   for (i = 0; i < media->streams->len; i++) {
214     GstRTSPMediaStream *stream;
215
216     stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
217
218     gst_rtsp_media_stream_free (stream);
219   }
220   g_array_free (media->streams, TRUE);
221
222   g_list_foreach (media->dynamic, (GFunc) gst_object_unref, NULL);
223   g_list_free (media->dynamic);
224
225   if (media->source) {
226     g_source_destroy (media->source);
227     g_source_unref (media->source);
228   }
229   g_mutex_free (media->lock);
230   g_cond_free (media->cond);
231
232   G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
233 }
234
235 static void
236 gst_rtsp_media_get_property (GObject * object, guint propid,
237     GValue * value, GParamSpec * pspec)
238 {
239   GstRTSPMedia *media = GST_RTSP_MEDIA (object);
240
241   switch (propid) {
242     case PROP_SHARED:
243       g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
244       break;
245     case PROP_REUSABLE:
246       g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
247       break;
248     case PROP_PROTOCOLS:
249       g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
250       break;
251     case PROP_EOS_SHUTDOWN:
252       g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
253       break;
254     case PROP_BUFFER_SIZE:
255       g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
256       break;
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
259   }
260 }
261
262 static void
263 gst_rtsp_media_set_property (GObject * object, guint propid,
264     const GValue * value, GParamSpec * pspec)
265 {
266   GstRTSPMedia *media = GST_RTSP_MEDIA (object);
267
268   switch (propid) {
269     case PROP_SHARED:
270       gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
271       break;
272     case PROP_REUSABLE:
273       gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
274       break;
275     case PROP_PROTOCOLS:
276       gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
277       break;
278     case PROP_EOS_SHUTDOWN:
279       gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
280       break;
281     case PROP_BUFFER_SIZE:
282       gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
283       break;
284     default:
285       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
286   }
287 }
288
289 static gpointer
290 do_loop (GstRTSPMediaClass * klass)
291 {
292   GST_INFO ("enter mainloop");
293   g_main_loop_run (klass->loop);
294   GST_INFO ("exit mainloop");
295
296   return NULL;
297 }
298
299 static void
300 collect_media_stats (GstRTSPMedia * media)
301 {
302   GstFormat format;
303   gint64 position, duration;
304
305   media->range.unit = GST_RTSP_RANGE_NPT;
306
307   if (media->is_live) {
308     media->range.min.type = GST_RTSP_TIME_NOW;
309     media->range.min.seconds = -1;
310     media->range.max.type = GST_RTSP_TIME_END;
311     media->range.max.seconds = -1;
312   } else {
313     /* get the position */
314     format = GST_FORMAT_TIME;
315     if (!gst_element_query_position (media->pipeline, &format, &position)) {
316       GST_INFO ("position query failed");
317       position = 0;
318     }
319
320     /* get the duration */
321     format = GST_FORMAT_TIME;
322     if (!gst_element_query_duration (media->pipeline, &format, &duration)) {
323       GST_INFO ("duration query failed");
324       duration = -1;
325     }
326
327     GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
328         GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
329
330     if (position == -1) {
331       media->range.min.type = GST_RTSP_TIME_NOW;
332       media->range.min.seconds = -1;
333     } else {
334       media->range.min.type = GST_RTSP_TIME_SECONDS;
335       media->range.min.seconds = ((gdouble) position) / GST_SECOND;
336     }
337     if (duration == -1) {
338       media->range.max.type = GST_RTSP_TIME_END;
339       media->range.max.seconds = -1;
340     } else {
341       media->range.max.type = GST_RTSP_TIME_SECONDS;
342       media->range.max.seconds = ((gdouble) duration) / GST_SECOND;
343     }
344   }
345 }
346
347 /**
348  * gst_rtsp_media_new:
349  *
350  * Create a new #GstRTSPMedia instance. The #GstRTSPMedia object contains the
351  * element to produde RTP data for one or more related (audio/video/..) 
352  * streams.
353  *
354  * Returns: a new #GstRTSPMedia object.
355  */
356 GstRTSPMedia *
357 gst_rtsp_media_new (void)
358 {
359   GstRTSPMedia *result;
360
361   result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
362
363   return result;
364 }
365
366 /**
367  * gst_rtsp_media_set_shared:
368  * @media: a #GstRTSPMedia
369  * @shared: the new value
370  *
371  * Set or unset if the pipeline for @media can be shared will multiple clients.
372  * When @shared is %TRUE, client requests for this media will share the media
373  * pipeline.
374  */
375 void
376 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
377 {
378   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
379
380   media->shared = shared;
381 }
382
383 /**
384  * gst_rtsp_media_is_shared:
385  * @media: a #GstRTSPMedia
386  *
387  * Check if the pipeline for @media can be shared between multiple clients.
388  *
389  * Returns: %TRUE if the media can be shared between clients.
390  */
391 gboolean
392 gst_rtsp_media_is_shared (GstRTSPMedia * media)
393 {
394   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
395
396   return media->shared;
397 }
398
399 /**
400  * gst_rtsp_media_set_reusable:
401  * @media: a #GstRTSPMedia
402  * @reusable: the new value
403  *
404  * Set or unset if the pipeline for @media can be reused after the pipeline has
405  * been unprepared.
406  */
407 void
408 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
409 {
410   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
411
412   media->reusable = reusable;
413 }
414
415 /**
416  * gst_rtsp_media_is_reusable:
417  * @media: a #GstRTSPMedia
418  *
419  * Check if the pipeline for @media can be reused after an unprepare.
420  *
421  * Returns: %TRUE if the media can be reused
422  */
423 gboolean
424 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
425 {
426   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
427
428   return media->reusable;
429 }
430
431 /**
432  * gst_rtsp_media_set_protocols:
433  * @media: a #GstRTSPMedia
434  * @protocols: the new flags
435  *
436  * Configure the allowed lower transport for @media.
437  */
438 void
439 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
440 {
441   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
442
443   media->protocols = protocols;
444 }
445
446 /**
447  * gst_rtsp_media_get_protocols:
448  * @media: a #GstRTSPMedia
449  *
450  * Get the allowed protocols of @media.
451  *
452  * Returns: a #GstRTSPLowerTrans
453  */
454 GstRTSPLowerTrans
455 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
456 {
457   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
458       GST_RTSP_LOWER_TRANS_UNKNOWN);
459
460   return media->protocols;
461 }
462
463 /**
464  * gst_rtsp_media_set_eos_shutdown:
465  * @media: a #GstRTSPMedia
466  * @eos_shutdown: the new value
467  *
468  * Set or unset if an EOS event will be sent to the pipeline for @media before
469  * it is unprepared.
470  */
471 void
472 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
473 {
474   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
475
476   media->eos_shutdown = eos_shutdown;
477 }
478
479 /**
480  * gst_rtsp_media_is_eos_shutdown:
481  * @media: a #GstRTSPMedia
482  *
483  * Check if the pipeline for @media will send an EOS down the pipeline before
484  * unpreparing.
485  *
486  * Returns: %TRUE if the media will send EOS before unpreparing.
487  */
488 gboolean
489 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
490 {
491   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
492
493   return media->eos_shutdown;
494 }
495
496 /**
497  * gst_rtsp_media_set_buffer_size:
498  * @media: a #GstRTSPMedia
499  * @size: the new value
500  *
501  * Set the kernel UDP buffer size.
502  */
503 void
504 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
505 {
506   g_return_if_fail (GST_IS_RTSP_MEDIA (media));
507
508   media->buffer_size = size;
509 }
510
511 /**
512  * gst_rtsp_media_get_buffer_size:
513  * @media: a #GstRTSPMedia
514  *
515  * Get the kernel UDP buffer size.
516  *
517  * Returns: the kernel UDP buffer size.
518  */
519 guint
520 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
521 {
522   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
523
524   return media->buffer_size;
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   old = media->auth;
542
543   if (old != auth) {
544     if (auth)
545       g_object_ref (auth);
546     media->auth = auth;
547     if (old)
548       g_object_unref (old);
549   }
550 }
551
552 /**
553  * gst_rtsp_media_get_auth:
554  * @media: a #GstRTSPMedia
555  *
556  * Get the #GstRTSPAuth used as the authentication manager of @media.
557  *
558  * Returns: the #GstRTSPAuth of @media. g_object_unref() after
559  * usage.
560  */
561 GstRTSPAuth *
562 gst_rtsp_media_get_auth (GstRTSPMedia * media)
563 {
564   GstRTSPAuth *result;
565
566   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
567
568   if ((result = media->auth))
569     g_object_ref (result);
570
571   return result;
572 }
573
574
575 /**
576  * gst_rtsp_media_n_streams:
577  * @media: a #GstRTSPMedia
578  *
579  * Get the number of streams in this media.
580  *
581  * Returns: The number of streams.
582  */
583 guint
584 gst_rtsp_media_n_streams (GstRTSPMedia * media)
585 {
586   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
587
588   return media->streams->len;
589 }
590
591 /**
592  * gst_rtsp_media_get_stream:
593  * @media: a #GstRTSPMedia
594  * @idx: the stream index
595  *
596  * Retrieve the stream with index @idx from @media.
597  *
598  * Returns: the #GstRTSPMediaStream at index @idx or %NULL when a stream with
599  * that index did not exist.
600  */
601 GstRTSPMediaStream *
602 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
603 {
604   GstRTSPMediaStream *res;
605
606   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
607
608   if (idx < media->streams->len)
609     res = g_array_index (media->streams, GstRTSPMediaStream *, idx);
610   else
611     res = NULL;
612
613   return res;
614 }
615
616 /**
617  * gst_rtsp_media_get_range_string:
618  * @media: a #GstRTSPMedia
619  * @play: for the PLAY request
620  *
621  * Get the current range as a string.
622  *
623  * Returns: The range as a string, g_free() after usage.
624  */
625 gchar *
626 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play)
627 {
628   gchar *result;
629   GstRTSPTimeRange range;
630
631   /* make copy */
632   range = media->range;
633
634   if (!play && media->active > 0) {
635     range.min.type = GST_RTSP_TIME_NOW;
636     range.min.seconds = -1;
637   }
638
639   result = gst_rtsp_range_to_string (&range);
640
641   return result;
642 }
643
644 /**
645  * gst_rtsp_media_seek:
646  * @media: a #GstRTSPMedia
647  * @range: a #GstRTSPTimeRange
648  *
649  * Seek the pipeline to @range.
650  *
651  * Returns: %TRUE on success.
652  */
653 gboolean
654 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
655 {
656   GstSeekFlags flags;
657   gboolean res;
658   gint64 start, stop;
659   GstSeekType start_type, stop_type;
660
661   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
662   g_return_val_if_fail (range != NULL, FALSE);
663
664   if (range->unit != GST_RTSP_RANGE_NPT)
665     goto not_supported;
666
667   /* depends on the current playing state of the pipeline. We might need to
668    * queue this until we get EOS. */
669   flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
670
671   start_type = stop_type = GST_SEEK_TYPE_NONE;
672
673   switch (range->min.type) {
674     case GST_RTSP_TIME_NOW:
675       start = -1;
676       break;
677     case GST_RTSP_TIME_SECONDS:
678       /* only seek when something changed */
679       if (media->range.min.seconds == range->min.seconds) {
680         start = -1;
681       } else {
682         start = range->min.seconds * GST_SECOND;
683         start_type = GST_SEEK_TYPE_SET;
684       }
685       break;
686     case GST_RTSP_TIME_END:
687     default:
688       goto weird_type;
689   }
690   switch (range->max.type) {
691     case GST_RTSP_TIME_SECONDS:
692       /* only seek when something changed */
693       if (media->range.max.seconds == range->max.seconds) {
694         stop = -1;
695       } else {
696         stop = range->max.seconds * GST_SECOND;
697         stop_type = GST_SEEK_TYPE_SET;
698       }
699       break;
700     case GST_RTSP_TIME_END:
701       stop = -1;
702       stop_type = GST_SEEK_TYPE_SET;
703       break;
704     case GST_RTSP_TIME_NOW:
705     default:
706       goto weird_type;
707   }
708
709   if (start != -1 || stop != -1) {
710     GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
711         GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
712
713     res = gst_element_seek (media->pipeline, 1.0, GST_FORMAT_TIME,
714         flags, start_type, start, stop_type, stop);
715
716     /* and block for the seek to complete */
717     GST_INFO ("done seeking %d", res);
718     gst_element_get_state (media->pipeline, NULL, NULL, -1);
719     GST_INFO ("prerolled again");
720
721     collect_media_stats (media);
722   } else {
723     GST_INFO ("no seek needed");
724     res = TRUE;
725   }
726
727   return res;
728
729   /* ERRORS */
730 not_supported:
731   {
732     GST_WARNING ("seek unit %d not supported", range->unit);
733     return FALSE;
734   }
735 weird_type:
736   {
737     GST_WARNING ("weird range type %d not supported", range->min.type);
738     return FALSE;
739   }
740 }
741
742 /**
743  * gst_rtsp_media_stream_rtp:
744  * @stream: a #GstRTSPMediaStream
745  * @buffer: a #GstBuffer
746  *
747  * Handle an RTP buffer for the stream. This method is usually called when a
748  * message has been received from a client using the TCP transport.
749  *
750  * This function takes ownership of @buffer.
751  *
752  * Returns: a GstFlowReturn.
753  */
754 GstFlowReturn
755 gst_rtsp_media_stream_rtp (GstRTSPMediaStream * stream, GstBuffer * buffer)
756 {
757   GstFlowReturn ret;
758
759   ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (stream->appsrc[0]), buffer);
760
761   return ret;
762 }
763
764 /**
765  * gst_rtsp_media_stream_rtcp:
766  * @stream: a #GstRTSPMediaStream
767  * @buffer: a #GstBuffer
768  *
769  * Handle an RTCP buffer for the stream. This method is usually called when a
770  * message has been received from a client using the TCP transport.
771  *
772  * This function takes ownership of @buffer.
773  *
774  * Returns: a GstFlowReturn.
775  */
776 GstFlowReturn
777 gst_rtsp_media_stream_rtcp (GstRTSPMediaStream * stream, GstBuffer * buffer)
778 {
779   GstFlowReturn ret;
780
781   ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (stream->appsrc[1]), buffer);
782
783   return ret;
784 }
785
786 /* Allocate the udp ports and sockets */
787 static gboolean
788 alloc_udp_ports (GstRTSPMedia * media, GstRTSPMediaStream * stream)
789 {
790   GstStateChangeReturn ret;
791   GstElement *udpsrc0, *udpsrc1;
792   GstElement *udpsink0, *udpsink1;
793   gint tmp_rtp, tmp_rtcp;
794   guint count;
795   gint rtpport, rtcpport, sockfd;
796   const gchar *host;
797
798   udpsrc0 = NULL;
799   udpsrc1 = NULL;
800   udpsink0 = NULL;
801   udpsink1 = NULL;
802   count = 0;
803
804   /* Start with random port */
805   tmp_rtp = 0;
806
807   if (media->is_ipv6)
808     host = "udp://[::0]";
809   else
810     host = "udp://0.0.0.0";
811
812   /* try to allocate 2 UDP ports, the RTP port should be an even
813    * number and the RTCP port should be the next (uneven) port */
814 again:
815   udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
816   if (udpsrc0 == NULL)
817     goto no_udp_protocol;
818   g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, NULL);
819
820   ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
821   if (ret == GST_STATE_CHANGE_FAILURE) {
822     if (tmp_rtp != 0) {
823       tmp_rtp += 2;
824       if (++count > 20)
825         goto no_ports;
826
827       gst_element_set_state (udpsrc0, GST_STATE_NULL);
828       gst_object_unref (udpsrc0);
829
830       goto again;
831     }
832     goto no_udp_protocol;
833   }
834
835   g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
836
837   /* check if port is even */
838   if ((tmp_rtp & 1) != 0) {
839     /* port not even, close and allocate another */
840     if (++count > 20)
841       goto no_ports;
842
843     gst_element_set_state (udpsrc0, GST_STATE_NULL);
844     gst_object_unref (udpsrc0);
845
846     tmp_rtp++;
847     goto again;
848   }
849
850   /* allocate port+1 for RTCP now */
851   udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
852   if (udpsrc1 == NULL)
853     goto no_udp_rtcp_protocol;
854
855   /* set port */
856   tmp_rtcp = tmp_rtp + 1;
857   g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
858
859   ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
860   /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
861   if (ret == GST_STATE_CHANGE_FAILURE) {
862
863     if (++count > 20)
864       goto no_ports;
865
866     gst_element_set_state (udpsrc0, GST_STATE_NULL);
867     gst_object_unref (udpsrc0);
868
869     gst_element_set_state (udpsrc1, GST_STATE_NULL);
870     gst_object_unref (udpsrc1);
871
872     tmp_rtp += 2;
873     goto again;
874   }
875
876   /* all fine, do port check */
877   g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
878   g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
879
880   /* this should not happen... */
881   if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
882     goto port_error;
883
884   udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
885   if (!udpsink0)
886     goto no_udp_protocol;
887
888   g_object_get (G_OBJECT (udpsrc0), "sock", &sockfd, NULL);
889   g_object_set (G_OBJECT (udpsink0), "sockfd", sockfd, NULL);
890   g_object_set (G_OBJECT (udpsink0), "closefd", FALSE, NULL);
891
892   udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
893   if (!udpsink1)
894     goto no_udp_protocol;
895
896   if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
897           "send-duplicates")) {
898     g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
899     g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
900   } else {
901     g_warning
902         ("old multiudpsink version found without send-duplicates property");
903   }
904
905   if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
906           "buffer-size")) {
907     g_object_set (G_OBJECT (udpsink0), "buffer-size", media->buffer_size, NULL);
908   } else {
909     GST_WARNING ("multiudpsink version found without buffer-size property");
910   }
911
912   g_object_get (G_OBJECT (udpsrc1), "sock", &sockfd, NULL);
913   g_object_set (G_OBJECT (udpsink1), "sockfd", sockfd, NULL);
914   g_object_set (G_OBJECT (udpsink1), "closefd", FALSE, NULL);
915   g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
916   g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
917
918   g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
919   g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
920   g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
921   g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
922
923   /* we keep these elements, we configure all in configure_transport when the
924    * server told us to really use the UDP ports. */
925   stream->udpsrc[0] = udpsrc0;
926   stream->udpsrc[1] = udpsrc1;
927   stream->udpsink[0] = udpsink0;
928   stream->udpsink[1] = udpsink1;
929   stream->server_port.min = rtpport;
930   stream->server_port.max = rtcpport;
931
932   return TRUE;
933
934   /* ERRORS */
935 no_udp_protocol:
936   {
937     goto cleanup;
938   }
939 no_ports:
940   {
941     goto cleanup;
942   }
943 no_udp_rtcp_protocol:
944   {
945     goto cleanup;
946   }
947 port_error:
948   {
949     goto cleanup;
950   }
951 cleanup:
952   {
953     if (udpsrc0) {
954       gst_element_set_state (udpsrc0, GST_STATE_NULL);
955       gst_object_unref (udpsrc0);
956     }
957     if (udpsrc1) {
958       gst_element_set_state (udpsrc1, GST_STATE_NULL);
959       gst_object_unref (udpsrc1);
960     }
961     if (udpsink0) {
962       gst_element_set_state (udpsink0, GST_STATE_NULL);
963       gst_object_unref (udpsink0);
964     }
965     if (udpsink1) {
966       gst_element_set_state (udpsink1, GST_STATE_NULL);
967       gst_object_unref (udpsink1);
968     }
969     return FALSE;
970   }
971 }
972
973 /* executed from streaming thread */
974 static void
975 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPMediaStream * stream)
976 {
977   gchar *capsstr;
978   GstCaps *newcaps, *oldcaps;
979
980   newcaps = gst_pad_get_current_caps (pad);
981
982   oldcaps = stream->caps;
983   stream->caps = newcaps;
984
985   if (oldcaps)
986     gst_caps_unref (oldcaps);
987
988   capsstr = gst_caps_to_string (newcaps);
989   GST_INFO ("stream %p received caps %p, %s", stream, newcaps, capsstr);
990   g_free (capsstr);
991 }
992
993 static void
994 dump_structure (const GstStructure * s)
995 {
996   gchar *sstr;
997
998   sstr = gst_structure_to_string (s);
999   GST_INFO ("structure: %s", sstr);
1000   g_free (sstr);
1001 }
1002
1003 static GstRTSPMediaTrans *
1004 find_transport (GstRTSPMediaStream * stream, const gchar * rtcp_from)
1005 {
1006   GList *walk;
1007   GstRTSPMediaTrans *result = NULL;
1008   const gchar *tmp;
1009   gchar *dest;
1010   guint port;
1011
1012   if (rtcp_from == NULL)
1013     return NULL;
1014
1015   tmp = g_strrstr (rtcp_from, ":");
1016   if (tmp == NULL)
1017     return NULL;
1018
1019   port = atoi (tmp + 1);
1020   dest = g_strndup (rtcp_from, tmp - rtcp_from);
1021
1022   GST_INFO ("finding %s:%d", dest, port);
1023
1024   for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1025     GstRTSPMediaTrans *trans = walk->data;
1026     gint min, max;
1027
1028     min = trans->transport->client_port.min;
1029     max = trans->transport->client_port.max;
1030
1031     if ((strcmp (trans->transport->destination, dest) == 0) && (min == port
1032             || max == port)) {
1033       result = trans;
1034       break;
1035     }
1036   }
1037   g_free (dest);
1038
1039   return result;
1040 }
1041
1042 static void
1043 on_new_ssrc (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1044 {
1045   GstStructure *stats;
1046   GstRTSPMediaTrans *trans;
1047
1048   GST_INFO ("%p: new source %p", stream, source);
1049
1050   /* see if we have a stream to match with the origin of the RTCP packet */
1051   trans = g_object_get_qdata (source, ssrc_stream_map_key);
1052   if (trans == NULL) {
1053     g_object_get (source, "stats", &stats, NULL);
1054     if (stats) {
1055       const gchar *rtcp_from;
1056
1057       dump_structure (stats);
1058
1059       rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1060       if ((trans = find_transport (stream, rtcp_from))) {
1061         GST_INFO ("%p: found transport %p for source  %p", stream, trans,
1062             source);
1063
1064         /* keep ref to the source */
1065         trans->rtpsource = source;
1066
1067         g_object_set_qdata (source, ssrc_stream_map_key, trans);
1068       }
1069       gst_structure_free (stats);
1070     }
1071   } else {
1072     GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1073   }
1074 }
1075
1076 static void
1077 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1078 {
1079   GST_INFO ("%p: new SDES %p", stream, source);
1080 }
1081
1082 static void
1083 on_ssrc_active (GObject * session, GObject * source,
1084     GstRTSPMediaStream * stream)
1085 {
1086   GstRTSPMediaTrans *trans;
1087
1088   trans = g_object_get_qdata (source, ssrc_stream_map_key);
1089
1090   GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1091
1092   if (trans && trans->keep_alive)
1093     trans->keep_alive (trans->ka_user_data);
1094
1095 #ifdef DUMP_STATS
1096   {
1097     GstStructure *stats;
1098     g_object_get (source, "stats", &stats, NULL);
1099     if (stats) {
1100       dump_structure (stats);
1101       gst_structure_free (stats);
1102     }
1103   }
1104 #endif
1105 }
1106
1107 static void
1108 on_bye_ssrc (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1109 {
1110   GST_INFO ("%p: source %p bye", stream, source);
1111 }
1112
1113 static void
1114 on_bye_timeout (GObject * session, GObject * source,
1115     GstRTSPMediaStream * stream)
1116 {
1117   GstRTSPMediaTrans *trans;
1118
1119   GST_INFO ("%p: source %p bye timeout", stream, source);
1120
1121   if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1122     trans->rtpsource = NULL;
1123     trans->timeout = TRUE;
1124   }
1125 }
1126
1127 static void
1128 on_timeout (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1129 {
1130   GstRTSPMediaTrans *trans;
1131
1132   GST_INFO ("%p: source %p timeout", stream, source);
1133
1134   if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1135     trans->rtpsource = NULL;
1136     trans->timeout = TRUE;
1137   }
1138 }
1139
1140 static GstFlowReturn
1141 handle_new_buffer (GstAppSink * sink, gpointer user_data)
1142 {
1143   GList *walk;
1144   GstBuffer *buffer;
1145   GstRTSPMediaStream *stream;
1146
1147   buffer = gst_app_sink_pull_buffer (sink);
1148   if (!buffer)
1149     return GST_FLOW_OK;
1150
1151   stream = (GstRTSPMediaStream *) user_data;
1152
1153   for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1154     GstRTSPMediaTrans *tr = (GstRTSPMediaTrans *) walk->data;
1155
1156     if (GST_ELEMENT_CAST (sink) == stream->appsink[0]) {
1157       if (tr->send_rtp)
1158         tr->send_rtp (buffer, tr->transport->interleaved.min, tr->user_data);
1159     } else {
1160       if (tr->send_rtcp)
1161         tr->send_rtcp (buffer, tr->transport->interleaved.max, tr->user_data);
1162     }
1163   }
1164   gst_buffer_unref (buffer);
1165
1166   return GST_FLOW_OK;
1167 }
1168
1169 static GstFlowReturn
1170 handle_new_buffer_list (GstAppSink * sink, gpointer user_data)
1171 {
1172   GList *walk;
1173   GstBufferList *blist;
1174   GstRTSPMediaStream *stream;
1175
1176   blist = gst_app_sink_pull_buffer_list (sink);
1177   if (!blist)
1178     return GST_FLOW_OK;
1179
1180   stream = (GstRTSPMediaStream *) user_data;
1181
1182   for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1183     GstRTSPMediaTrans *tr = (GstRTSPMediaTrans *) walk->data;
1184
1185     if (GST_ELEMENT_CAST (sink) == stream->appsink[0]) {
1186       if (tr->send_rtp_list)
1187         tr->send_rtp_list (blist, tr->transport->interleaved.min,
1188             tr->user_data);
1189     } else {
1190       if (tr->send_rtcp_list)
1191         tr->send_rtcp_list (blist, tr->transport->interleaved.max,
1192             tr->user_data);
1193     }
1194   }
1195   gst_buffer_list_unref (blist);
1196
1197   return GST_FLOW_OK;
1198 }
1199
1200 static GstAppSinkCallbacks sink_cb = {
1201   NULL,                         /* not interested in EOS */
1202   NULL,                         /* not interested in preroll buffers */
1203   handle_new_buffer,
1204   handle_new_buffer_list
1205 };
1206
1207 /* prepare the pipeline objects to handle @stream in @media */
1208 static gboolean
1209 setup_stream (GstRTSPMediaStream * stream, guint idx, GstRTSPMedia * media)
1210 {
1211   gchar *name;
1212   GstPad *pad, *teepad, *selpad;
1213   GstPadLinkReturn ret;
1214   gint i;
1215
1216   /* allocate udp ports, we will have 4 of them, 2 for receiving RTP/RTCP and 2
1217    * for sending RTP/RTCP. The sender and receiver ports are shared between the
1218    * elements */
1219   if (!alloc_udp_ports (media, stream))
1220     return FALSE;
1221
1222   /* add the ports to the pipeline */
1223   for (i = 0; i < 2; i++) {
1224     gst_bin_add (GST_BIN_CAST (media->pipeline), stream->udpsink[i]);
1225     gst_bin_add (GST_BIN_CAST (media->pipeline), stream->udpsrc[i]);
1226   }
1227
1228   /* create elements for the TCP transfer */
1229   for (i = 0; i < 2; i++) {
1230     stream->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1231     stream->appsink[i] = gst_element_factory_make ("appsink", NULL);
1232     g_object_set (stream->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1233     g_object_set (stream->appsink[i], "emit-signals", FALSE, NULL);
1234     g_object_set (stream->appsink[i], "preroll-queue-len", 1, NULL);
1235     gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appsink[i]);
1236     gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appsrc[i]);
1237     gst_app_sink_set_callbacks (GST_APP_SINK_CAST (stream->appsink[i]),
1238         &sink_cb, stream, NULL);
1239   }
1240
1241   /* hook up the stream to the RTP session elements. */
1242   name = g_strdup_printf ("send_rtp_sink_%d", idx);
1243   stream->send_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
1244   g_free (name);
1245   name = g_strdup_printf ("send_rtp_src_%d", idx);
1246   stream->send_rtp_src = gst_element_get_static_pad (media->rtpbin, name);
1247   g_free (name);
1248   name = g_strdup_printf ("send_rtcp_src_%d", idx);
1249   stream->send_rtcp_src = gst_element_get_request_pad (media->rtpbin, name);
1250   g_free (name);
1251   name = g_strdup_printf ("recv_rtcp_sink_%d", idx);
1252   stream->recv_rtcp_sink = gst_element_get_request_pad (media->rtpbin, name);
1253   g_free (name);
1254   name = g_strdup_printf ("recv_rtp_sink_%d", idx);
1255   stream->recv_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
1256   g_free (name);
1257
1258   /* get the session */
1259   g_signal_emit_by_name (media->rtpbin, "get-internal-session", idx,
1260       &stream->session);
1261
1262   g_signal_connect (stream->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1263       stream);
1264   g_signal_connect (stream->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1265       stream);
1266   g_signal_connect (stream->session, "on-ssrc-active",
1267       (GCallback) on_ssrc_active, stream);
1268   g_signal_connect (stream->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1269       stream);
1270   g_signal_connect (stream->session, "on-bye-timeout",
1271       (GCallback) on_bye_timeout, stream);
1272   g_signal_connect (stream->session, "on-timeout", (GCallback) on_timeout,
1273       stream);
1274
1275   /* link the RTP pad to the session manager */
1276   ret = gst_pad_link (stream->srcpad, stream->send_rtp_sink);
1277   if (ret != GST_PAD_LINK_OK)
1278     goto link_failed;
1279
1280   /* make tee for RTP and link to stream */
1281   stream->tee[0] = gst_element_factory_make ("tee", NULL);
1282   gst_bin_add (GST_BIN_CAST (media->pipeline), stream->tee[0]);
1283
1284   pad = gst_element_get_static_pad (stream->tee[0], "sink");
1285   gst_pad_link (stream->send_rtp_src, pad);
1286   gst_object_unref (pad);
1287
1288   /* link RTP sink, we're pretty sure this will work. */
1289   teepad = gst_element_get_request_pad (stream->tee[0], "src%d");
1290   pad = gst_element_get_static_pad (stream->udpsink[0], "sink");
1291   gst_pad_link (teepad, pad);
1292   gst_object_unref (pad);
1293   gst_object_unref (teepad);
1294
1295   teepad = gst_element_get_request_pad (stream->tee[0], "src%d");
1296   pad = gst_element_get_static_pad (stream->appsink[0], "sink");
1297   gst_pad_link (teepad, pad);
1298   gst_object_unref (pad);
1299   gst_object_unref (teepad);
1300
1301   /* make tee for RTCP */
1302   stream->tee[1] = gst_element_factory_make ("tee", NULL);
1303   gst_bin_add (GST_BIN_CAST (media->pipeline), stream->tee[1]);
1304
1305   pad = gst_element_get_static_pad (stream->tee[1], "sink");
1306   gst_pad_link (stream->send_rtcp_src, pad);
1307   gst_object_unref (pad);
1308
1309   /* link RTCP elements */
1310   teepad = gst_element_get_request_pad (stream->tee[1], "src%d");
1311   pad = gst_element_get_static_pad (stream->udpsink[1], "sink");
1312   gst_pad_link (teepad, pad);
1313   gst_object_unref (pad);
1314   gst_object_unref (teepad);
1315
1316   teepad = gst_element_get_request_pad (stream->tee[1], "src%d");
1317   pad = gst_element_get_static_pad (stream->appsink[1], "sink");
1318   gst_pad_link (teepad, pad);
1319   gst_object_unref (pad);
1320   gst_object_unref (teepad);
1321
1322   /* make selector for the RTP receivers */
1323   stream->selector[0] = gst_element_factory_make ("funnel", NULL);
1324   gst_bin_add (GST_BIN_CAST (media->pipeline), stream->selector[0]);
1325
1326   pad = gst_element_get_static_pad (stream->selector[0], "src");
1327   gst_pad_link (pad, stream->recv_rtp_sink);
1328   gst_object_unref (pad);
1329
1330   selpad = gst_element_get_request_pad (stream->selector[0], "sink%d");
1331   pad = gst_element_get_static_pad (stream->udpsrc[0], "src");
1332   gst_pad_link (pad, selpad);
1333   gst_object_unref (pad);
1334   gst_object_unref (selpad);
1335
1336   selpad = gst_element_get_request_pad (stream->selector[0], "sink%d");
1337   pad = gst_element_get_static_pad (stream->appsrc[0], "src");
1338   gst_pad_link (pad, selpad);
1339   gst_object_unref (pad);
1340   gst_object_unref (selpad);
1341
1342   /* make selector for the RTCP receivers */
1343   stream->selector[1] = gst_element_factory_make ("funnel", NULL);
1344   gst_bin_add (GST_BIN_CAST (media->pipeline), stream->selector[1]);
1345
1346   pad = gst_element_get_static_pad (stream->selector[1], "src");
1347   gst_pad_link (pad, stream->recv_rtcp_sink);
1348   gst_object_unref (pad);
1349
1350   selpad = gst_element_get_request_pad (stream->selector[1], "sink%d");
1351   pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
1352   gst_pad_link (pad, selpad);
1353   gst_object_unref (pad);
1354   gst_object_unref (selpad);
1355
1356   selpad = gst_element_get_request_pad (stream->selector[1], "sink%d");
1357   pad = gst_element_get_static_pad (stream->appsrc[1], "src");
1358   gst_pad_link (pad, selpad);
1359   gst_object_unref (pad);
1360   gst_object_unref (selpad);
1361
1362   /* we set and keep these to playing so that they don't cause NO_PREROLL return
1363    * values */
1364   gst_element_set_state (stream->udpsrc[0], GST_STATE_PLAYING);
1365   gst_element_set_state (stream->udpsrc[1], GST_STATE_PLAYING);
1366   gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1367   gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1368
1369   /* be notified of caps changes */
1370   stream->caps_sig = g_signal_connect (stream->send_rtp_sink, "notify::caps",
1371       (GCallback) caps_notify, stream);
1372
1373   stream->prepared = TRUE;
1374
1375   return TRUE;
1376
1377   /* ERRORS */
1378 link_failed:
1379   {
1380     GST_WARNING ("failed to link stream %d", idx);
1381     return FALSE;
1382   }
1383 }
1384
1385 static void
1386 unlock_streams (GstRTSPMedia * media)
1387 {
1388   guint i, n_streams;
1389
1390   /* unlock the udp src elements */
1391   n_streams = gst_rtsp_media_n_streams (media);
1392   for (i = 0; i < n_streams; i++) {
1393     GstRTSPMediaStream *stream;
1394
1395     stream = gst_rtsp_media_get_stream (media, i);
1396
1397     gst_element_set_locked_state (stream->udpsrc[0], FALSE);
1398     gst_element_set_locked_state (stream->udpsrc[1], FALSE);
1399   }
1400 }
1401
1402 static void
1403 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1404 {
1405   g_mutex_lock (media->lock);
1406   /* never overwrite the error status */
1407   if (media->status != GST_RTSP_MEDIA_STATUS_ERROR)
1408     media->status = status;
1409   GST_DEBUG ("setting new status to %d", status);
1410   g_cond_broadcast (media->cond);
1411   g_mutex_unlock (media->lock);
1412 }
1413
1414 static GstRTSPMediaStatus
1415 gst_rtsp_media_get_status (GstRTSPMedia * media)
1416 {
1417   GstRTSPMediaStatus result;
1418   GTimeVal timeout;
1419
1420   g_mutex_lock (media->lock);
1421   g_get_current_time (&timeout);
1422   g_time_val_add (&timeout, 20 * G_USEC_PER_SEC);
1423   /* while we are preparing, wait */
1424   while (media->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1425     GST_DEBUG ("waiting for status change");
1426     if (!g_cond_timed_wait (media->cond, media->lock, &timeout)) {
1427       GST_DEBUG ("timeout, assuming error status");
1428       media->status = GST_RTSP_MEDIA_STATUS_ERROR;
1429     }
1430   }
1431   /* could be success or error */
1432   result = media->status;
1433   GST_DEBUG ("got status %d", result);
1434   g_mutex_unlock (media->lock);
1435
1436   return result;
1437 }
1438
1439 static gboolean
1440 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1441 {
1442   GstMessageType type;
1443
1444   type = GST_MESSAGE_TYPE (message);
1445
1446   switch (type) {
1447     case GST_MESSAGE_STATE_CHANGED:
1448       break;
1449     case GST_MESSAGE_BUFFERING:
1450     {
1451       gint percent;
1452
1453       gst_message_parse_buffering (message, &percent);
1454
1455       /* no state management needed for live pipelines */
1456       if (media->is_live)
1457         break;
1458
1459       if (percent == 100) {
1460         /* a 100% message means buffering is done */
1461         media->buffering = FALSE;
1462         /* if the desired state is playing, go back */
1463         if (media->target_state == GST_STATE_PLAYING) {
1464           GST_INFO ("Buffering done, setting pipeline to PLAYING");
1465           gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1466         } else {
1467           GST_INFO ("Buffering done");
1468         }
1469       } else {
1470         /* buffering busy */
1471         if (media->buffering == FALSE) {
1472           if (media->target_state == GST_STATE_PLAYING) {
1473             /* we were not buffering but PLAYING, PAUSE  the pipeline. */
1474             GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1475             gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1476           } else {
1477             GST_INFO ("Buffering ...");
1478           }
1479         }
1480         media->buffering = TRUE;
1481       }
1482       break;
1483     }
1484     case GST_MESSAGE_LATENCY:
1485     {
1486       gst_bin_recalculate_latency (GST_BIN_CAST (media->pipeline));
1487       break;
1488     }
1489     case GST_MESSAGE_ERROR:
1490     {
1491       GError *gerror;
1492       gchar *debug;
1493
1494       gst_message_parse_error (message, &gerror, &debug);
1495       GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1496       g_error_free (gerror);
1497       g_free (debug);
1498
1499       gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1500       break;
1501     }
1502     case GST_MESSAGE_WARNING:
1503     {
1504       GError *gerror;
1505       gchar *debug;
1506
1507       gst_message_parse_warning (message, &gerror, &debug);
1508       GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1509       g_error_free (gerror);
1510       g_free (debug);
1511       break;
1512     }
1513     case GST_MESSAGE_ELEMENT:
1514       break;
1515     case GST_MESSAGE_STREAM_STATUS:
1516       break;
1517     case GST_MESSAGE_ASYNC_DONE:
1518       if (!media->adding) {
1519         /* when we are dynamically adding pads, the addition of the udpsrc will
1520          * temporarily produce ASYNC_DONE messages. We have to ignore them and
1521          * wait for the final ASYNC_DONE after everything prerolled */
1522         GST_INFO ("%p: got ASYNC_DONE", media);
1523         collect_media_stats (media);
1524
1525         gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1526       } else {
1527         GST_INFO ("%p: ignoring ASYNC_DONE", media);
1528       }
1529       break;
1530     case GST_MESSAGE_EOS:
1531       GST_INFO ("%p: got EOS", media);
1532       if (media->eos_pending) {
1533         GST_DEBUG ("shutting down after EOS");
1534         gst_element_set_state (media->pipeline, GST_STATE_NULL);
1535         media->eos_pending = FALSE;
1536         g_object_unref (media);
1537       }
1538       break;
1539     default:
1540       GST_INFO ("%p: got message type %s", media,
1541           gst_message_type_get_name (type));
1542       break;
1543   }
1544   return TRUE;
1545 }
1546
1547 static gboolean
1548 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1549 {
1550   GstRTSPMediaClass *klass;
1551   gboolean ret;
1552
1553   klass = GST_RTSP_MEDIA_GET_CLASS (media);
1554
1555   if (klass->handle_message)
1556     ret = klass->handle_message (media, message);
1557   else
1558     ret = FALSE;
1559
1560   return ret;
1561 }
1562
1563 /* called from streaming threads */
1564 static void
1565 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1566 {
1567   GstRTSPMediaStream *stream;
1568   gchar *name;
1569   gint i;
1570
1571   i = media->streams->len + 1;
1572
1573   GST_INFO ("pad added %s:%s, stream %d", GST_DEBUG_PAD_NAME (pad), i);
1574
1575   stream = g_new0 (GstRTSPMediaStream, 1);
1576   stream->payloader = element;
1577
1578   name = g_strdup_printf ("dynpay%d", i);
1579
1580   media->adding = TRUE;
1581
1582   /* ghost the pad of the payloader to the element */
1583   stream->srcpad = gst_ghost_pad_new (name, pad);
1584   gst_pad_set_active (stream->srcpad, TRUE);
1585   gst_element_add_pad (media->element, stream->srcpad);
1586   g_free (name);
1587
1588   /* add stream now */
1589   g_array_append_val (media->streams, stream);
1590
1591   setup_stream (stream, i, media);
1592
1593   for (i = 0; i < 2; i++) {
1594     gst_element_set_state (stream->udpsink[i], GST_STATE_PAUSED);
1595     gst_element_set_state (stream->appsink[i], GST_STATE_PAUSED);
1596     gst_element_set_state (stream->tee[i], GST_STATE_PAUSED);
1597     gst_element_set_state (stream->selector[i], GST_STATE_PAUSED);
1598     gst_element_set_state (stream->appsrc[i], GST_STATE_PAUSED);
1599   }
1600   media->adding = FALSE;
1601 }
1602
1603 static void
1604 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1605 {
1606   GST_INFO ("no more pads");
1607   if (media->fakesink) {
1608     gst_object_ref (media->fakesink);
1609     gst_bin_remove (GST_BIN (media->pipeline), media->fakesink);
1610     gst_element_set_state (media->fakesink, GST_STATE_NULL);
1611     gst_object_unref (media->fakesink);
1612     media->fakesink = NULL;
1613     GST_INFO ("removed fakesink");
1614   }
1615 }
1616
1617 /**
1618  * gst_rtsp_media_prepare:
1619  * @media: a #GstRTSPMedia
1620  *
1621  * Prepare @media for streaming. This function will create the pipeline and
1622  * other objects to manage the streaming.
1623  *
1624  * It will preroll the pipeline and collect vital information about the streams
1625  * such as the duration.
1626  *
1627  * Returns: %TRUE on success.
1628  */
1629 gboolean
1630 gst_rtsp_media_prepare (GstRTSPMedia * media)
1631 {
1632   GstStateChangeReturn ret;
1633   GstRTSPMediaStatus status;
1634   guint i, n_streams;
1635   GstRTSPMediaClass *klass;
1636   GstBus *bus;
1637   GList *walk;
1638
1639   if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1640     goto was_prepared;
1641
1642   if (!media->reusable && media->reused)
1643     goto is_reused;
1644
1645   media->rtpbin = gst_element_factory_make ("gstrtpbin", NULL);
1646   if (media->rtpbin == NULL)
1647     goto no_gstrtpbin;
1648
1649   GST_INFO ("preparing media %p", media);
1650
1651   /* reset some variables */
1652   media->is_live = FALSE;
1653   media->buffering = FALSE;
1654   /* we're preparing now */
1655   media->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1656
1657   bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (media->pipeline));
1658
1659   /* add the pipeline bus to our custom mainloop */
1660   media->source = gst_bus_create_watch (bus);
1661   gst_object_unref (bus);
1662
1663   g_source_set_callback (media->source, (GSourceFunc) bus_message, media, NULL);
1664
1665   klass = GST_RTSP_MEDIA_GET_CLASS (media);
1666   media->id = g_source_attach (media->source, klass->context);
1667
1668   /* add stuff to the bin */
1669   gst_bin_add (GST_BIN (media->pipeline), media->rtpbin);
1670
1671   /* link streams we already have, other streams might appear when we have
1672    * dynamic elements */
1673   n_streams = gst_rtsp_media_n_streams (media);
1674   for (i = 0; i < n_streams; i++) {
1675     GstRTSPMediaStream *stream;
1676
1677     stream = gst_rtsp_media_get_stream (media, i);
1678
1679     setup_stream (stream, i, media);
1680   }
1681
1682   for (walk = media->dynamic; walk; walk = g_list_next (walk)) {
1683     GstElement *elem = walk->data;
1684
1685     GST_INFO ("adding callbacks for dynamic element %p", elem);
1686
1687     g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1688     g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1689
1690     /* we add a fakesink here in order to make the state change async. We remove
1691      * the fakesink again in the no-more-pads callback. */
1692     media->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1693     gst_bin_add (GST_BIN (media->pipeline), media->fakesink);
1694   }
1695
1696   GST_INFO ("setting pipeline to PAUSED for media %p", media);
1697   /* first go to PAUSED */
1698   ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1699   media->target_state = GST_STATE_PAUSED;
1700
1701   switch (ret) {
1702     case GST_STATE_CHANGE_SUCCESS:
1703       GST_INFO ("SUCCESS state change for media %p", media);
1704       break;
1705     case GST_STATE_CHANGE_ASYNC:
1706       GST_INFO ("ASYNC state change for media %p", media);
1707       break;
1708     case GST_STATE_CHANGE_NO_PREROLL:
1709       /* we need to go to PLAYING */
1710       GST_INFO ("NO_PREROLL state change: live media %p", media);
1711       media->is_live = TRUE;
1712       ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1713       if (ret == GST_STATE_CHANGE_FAILURE)
1714         goto state_failed;
1715       break;
1716     case GST_STATE_CHANGE_FAILURE:
1717       goto state_failed;
1718   }
1719
1720   /* now wait for all pads to be prerolled */
1721   status = gst_rtsp_media_get_status (media);
1722   if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1723     goto state_failed;
1724
1725   g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1726
1727   GST_INFO ("object %p is prerolled", media);
1728
1729   return TRUE;
1730
1731   /* OK */
1732 was_prepared:
1733   {
1734     return TRUE;
1735   }
1736   /* ERRORS */
1737 is_reused:
1738   {
1739     GST_WARNING ("can not reuse media %p", media);
1740     return FALSE;
1741   }
1742 no_gstrtpbin:
1743   {
1744     GST_WARNING ("no gstrtpbin element");
1745     g_warning ("failed to create element 'gstrtpbin', check your installation");
1746     return FALSE;
1747   }
1748 state_failed:
1749   {
1750     GST_WARNING ("failed to preroll pipeline");
1751     unlock_streams (media);
1752     gst_element_set_state (media->pipeline, GST_STATE_NULL);
1753     gst_rtsp_media_unprepare (media);
1754     return FALSE;
1755   }
1756 }
1757
1758 /**
1759  * gst_rtsp_media_unprepare:
1760  * @media: a #GstRTSPMedia
1761  *
1762  * Unprepare @media. After this call, the media should be prepared again before
1763  * it can be used again. If the media is set to be non-reusable, a new instance
1764  * must be created.
1765  *
1766  * Returns: %TRUE on success.
1767  */
1768 gboolean
1769 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1770 {
1771   GstRTSPMediaClass *klass;
1772   gboolean success;
1773
1774   if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1775     return TRUE;
1776
1777   GST_INFO ("unprepare media %p", media);
1778   media->target_state = GST_STATE_NULL;
1779
1780   klass = GST_RTSP_MEDIA_GET_CLASS (media);
1781   if (klass->unprepare)
1782     success = klass->unprepare (media);
1783   else
1784     success = TRUE;
1785
1786   media->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1787   media->reused = TRUE;
1788
1789   /* when the media is not reusable, this will effectively unref the media and
1790    * recreate it */
1791   g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1792
1793   return success;
1794 }
1795
1796 static gboolean
1797 default_unprepare (GstRTSPMedia * media)
1798 {
1799   if (media->eos_shutdown) {
1800     GST_DEBUG ("sending EOS for shutdown");
1801     /* ref so that we don't disappear */
1802     g_object_ref (media);
1803     media->eos_pending = TRUE;
1804     gst_element_send_event (media->pipeline, gst_event_new_eos ());
1805     /* we need to go to playing again for the EOS to propagate, normally in this
1806      * state, nothing is receiving data from us anymore so this is ok. */
1807     gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1808   } else {
1809     GST_DEBUG ("shutting down");
1810     gst_element_set_state (media->pipeline, GST_STATE_NULL);
1811   }
1812   return TRUE;
1813 }
1814
1815 static void
1816 add_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1817     gchar * dest, gint min, gint max)
1818 {
1819   GST_INFO ("adding %s:%d-%d", dest, min, max);
1820   g_signal_emit_by_name (stream->udpsink[0], "add", dest, min, NULL);
1821   g_signal_emit_by_name (stream->udpsink[1], "add", dest, max, NULL);
1822 }
1823
1824 static void
1825 remove_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1826     gchar * dest, gint min, gint max)
1827 {
1828   GST_INFO ("removing %s:%d-%d", dest, min, max);
1829   g_signal_emit_by_name (stream->udpsink[0], "remove", dest, min, NULL);
1830   g_signal_emit_by_name (stream->udpsink[1], "remove", dest, max, NULL);
1831 }
1832
1833 /**
1834  * gst_rtsp_media_set_state:
1835  * @media: a #GstRTSPMedia
1836  * @state: the target state of the media
1837  * @transports: a #GArray of #GstRTSPMediaTrans pointers
1838  *
1839  * Set the state of @media to @state and for the transports in @transports.
1840  *
1841  * Returns: %TRUE on success.
1842  */
1843 gboolean
1844 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1845     GArray * transports)
1846 {
1847   gint i;
1848   GstStateChangeReturn ret;
1849   gboolean add, remove, do_state;
1850   gint old_active;
1851
1852   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1853   g_return_val_if_fail (transports != NULL, FALSE);
1854
1855   /* NULL and READY are the same */
1856   if (state == GST_STATE_READY)
1857     state = GST_STATE_NULL;
1858
1859   add = remove = FALSE;
1860
1861   GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1862       media);
1863
1864   switch (state) {
1865     case GST_STATE_NULL:
1866       /* unlock the streams so that they follow the state changes from now on */
1867       unlock_streams (media);
1868       /* fallthrough */
1869     case GST_STATE_PAUSED:
1870       /* we're going from PLAYING to PAUSED, READY or NULL, remove */
1871       if (media->target_state == GST_STATE_PLAYING)
1872         remove = TRUE;
1873       break;
1874     case GST_STATE_PLAYING:
1875       /* we're going to PLAYING, add */
1876       add = TRUE;
1877       break;
1878     default:
1879       break;
1880   }
1881   old_active = media->active;
1882
1883   for (i = 0; i < transports->len; i++) {
1884     GstRTSPMediaTrans *tr;
1885     GstRTSPMediaStream *stream;
1886     GstRTSPTransport *trans;
1887
1888     /* we need a non-NULL entry in the array */
1889     tr = g_array_index (transports, GstRTSPMediaTrans *, i);
1890     if (tr == NULL)
1891       continue;
1892
1893     /* we need a transport */
1894     if (!(trans = tr->transport))
1895       continue;
1896
1897     /* get the stream and add the destinations */
1898     stream = gst_rtsp_media_get_stream (media, tr->idx);
1899     switch (trans->lower_transport) {
1900       case GST_RTSP_LOWER_TRANS_UDP:
1901       case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1902       {
1903         gchar *dest;
1904         gint min, max;
1905
1906         dest = trans->destination;
1907         if (trans->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1908           min = trans->port.min;
1909           max = trans->port.max;
1910         } else {
1911           min = trans->client_port.min;
1912           max = trans->client_port.max;
1913         }
1914
1915         if (add && !tr->active) {
1916           add_udp_destination (media, stream, dest, min, max);
1917           stream->transports = g_list_prepend (stream->transports, tr);
1918           tr->active = TRUE;
1919           media->active++;
1920         } else if (remove && tr->active) {
1921           remove_udp_destination (media, stream, dest, min, max);
1922           stream->transports = g_list_remove (stream->transports, tr);
1923           tr->active = FALSE;
1924           media->active--;
1925         }
1926         break;
1927       }
1928       case GST_RTSP_LOWER_TRANS_TCP:
1929         if (add && !tr->active) {
1930           GST_INFO ("adding TCP %s", trans->destination);
1931           stream->transports = g_list_prepend (stream->transports, tr);
1932           tr->active = TRUE;
1933           media->active++;
1934         } else if (remove && tr->active) {
1935           GST_INFO ("removing TCP %s", trans->destination);
1936           stream->transports = g_list_remove (stream->transports, tr);
1937           tr->active = FALSE;
1938           media->active--;
1939         }
1940         break;
1941       default:
1942         GST_INFO ("Unknown transport %d", trans->lower_transport);
1943         break;
1944     }
1945   }
1946
1947   /* we just added the first media, do the playing state change */
1948   if (old_active == 0 && add)
1949     do_state = TRUE;
1950   /* if we have no more active media, do the downward state changes */
1951   else if (media->active == 0)
1952     do_state = TRUE;
1953   else
1954     do_state = FALSE;
1955
1956   GST_INFO ("state %d active %d media %p do_state %d", state, media->active,
1957       media, do_state);
1958
1959   if (media->target_state != state) {
1960     if (do_state) {
1961       if (state == GST_STATE_NULL) {
1962         gst_rtsp_media_unprepare (media);
1963       } else {
1964         GST_INFO ("state %s media %p", gst_element_state_get_name (state),
1965             media);
1966         media->target_state = state;
1967         ret = gst_element_set_state (media->pipeline, state);
1968       }
1969     }
1970     g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
1971         NULL);
1972   }
1973
1974   /* remember where we are */
1975   if (state == GST_STATE_PAUSED || old_active != media->active)
1976     collect_media_stats (media);
1977
1978   return TRUE;
1979 }
1980
1981 /**
1982  * gst_rtsp_media_remove_elements:
1983  * @media: a #GstRTSPMedia
1984  *
1985  * Remove all elements and the pipeline controlled by @media.
1986  */
1987 void
1988 gst_rtsp_media_remove_elements (GstRTSPMedia * media)
1989 {
1990   gint i, j;
1991
1992   unlock_streams (media);
1993
1994   for (i = 0; i < media->streams->len; i++) {
1995     GstRTSPMediaStream *stream;
1996
1997     GST_INFO ("Removing elements of stream %d from pipeline", i);
1998
1999     stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
2000
2001     gst_pad_unlink (stream->srcpad, stream->send_rtp_sink);
2002
2003     g_signal_handler_disconnect (stream->send_rtp_sink, stream->caps_sig);
2004
2005     for (j = 0; j < 2; j++) {
2006       gst_element_set_state (stream->udpsrc[j], GST_STATE_NULL);
2007       gst_element_set_state (stream->udpsink[j], GST_STATE_NULL);
2008       gst_element_set_state (stream->appsrc[j], GST_STATE_NULL);
2009       gst_element_set_state (stream->appsink[j], GST_STATE_NULL);
2010       gst_element_set_state (stream->tee[j], GST_STATE_NULL);
2011       gst_element_set_state (stream->selector[j], GST_STATE_NULL);
2012
2013       gst_bin_remove (GST_BIN (media->pipeline), stream->udpsrc[j]);
2014       gst_bin_remove (GST_BIN (media->pipeline), stream->udpsink[j]);
2015       gst_bin_remove (GST_BIN (media->pipeline), stream->appsrc[j]);
2016       gst_bin_remove (GST_BIN (media->pipeline), stream->appsink[j]);
2017       gst_bin_remove (GST_BIN (media->pipeline), stream->tee[j]);
2018       gst_bin_remove (GST_BIN (media->pipeline), stream->selector[j]);
2019     }
2020     if (stream->caps)
2021       gst_caps_unref (stream->caps);
2022     stream->caps = NULL;
2023     gst_rtsp_media_stream_free (stream);
2024   }
2025   g_array_remove_range (media->streams, 0, media->streams->len);
2026
2027   gst_element_set_state (media->rtpbin, GST_STATE_NULL);
2028   gst_bin_remove (GST_BIN (media->pipeline), media->rtpbin);
2029
2030   gst_object_unref (media->pipeline);
2031   media->pipeline = NULL;
2032 }