rtsp-media: Do not leak the element in take_pipeline
[platform/upstream/gstreamer.git] / tests / check / gst / media.c
1 /* GStreamer
2  * Copyright (C) 2012 Wim Taymans <wim.taymans@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 <gst/check/gstcheck.h>
21
22 #include <rtsp-media-factory.h>
23
24 GST_START_TEST (test_launch)
25 {
26   GstRTSPMediaFactory *factory;
27   GstRTSPMedia *media;
28   GstRTSPUrl *url;
29   GstRTSPStream *stream;
30   GstRTSPTimeRange *range;
31   gchar *str;
32
33   factory = gst_rtsp_media_factory_new ();
34   fail_if (gst_rtsp_media_factory_is_shared (factory));
35   gst_rtsp_url_parse ("rtsp://localhost:8554/test", &url);
36
37   gst_rtsp_media_factory_set_launch (factory,
38       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
39
40   media = gst_rtsp_media_factory_construct (factory, url);
41   fail_unless (GST_IS_RTSP_MEDIA (media));
42
43   fail_unless (gst_rtsp_media_n_streams (media) == 1);
44
45   stream = gst_rtsp_media_get_stream (media, 0);
46   fail_unless (stream != NULL);
47
48   /* fails, need to be prepared */
49   str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
50   fail_unless (str == NULL);
51
52   fail_unless (gst_rtsp_range_parse ("npt=5.0-", &range) == GST_RTSP_OK);
53   /* fails, need to be prepared */
54   fail_if (gst_rtsp_media_seek (media, range));
55
56   fail_unless (gst_rtsp_media_prepare (media));
57
58   str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
59   fail_unless (g_str_equal (str, "npt=0-"));
60   g_free (str);
61
62   str = gst_rtsp_media_get_range_string (media, TRUE, GST_RTSP_RANGE_NPT);
63   fail_unless (g_str_equal (str, "npt=0-"));
64   g_free (str);
65
66   fail_unless (gst_rtsp_media_seek (media, range));
67
68   str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
69   fail_unless (g_str_equal (str, "npt=5-"));
70   g_free (str);
71
72   str = gst_rtsp_media_get_range_string (media, TRUE, GST_RTSP_RANGE_NPT);
73   fail_unless (g_str_equal (str, "npt=5-"));
74   g_free (str);
75
76   fail_unless (gst_rtsp_media_unprepare (media));
77
78   /* should fail again */
79   str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
80   fail_unless (str == NULL);
81   fail_if (gst_rtsp_media_seek (media, range));
82
83   gst_rtsp_range_free (range);
84   g_object_unref (media);
85
86   gst_rtsp_url_free (url);
87   g_object_unref (factory);
88 }
89
90 GST_END_TEST;
91
92 GST_START_TEST (test_media)
93 {
94   GstRTSPMedia *media;
95   GstElement *bin, *e1, *e2;
96
97   bin = gst_bin_new ("bin");
98   fail_if (bin == NULL);
99
100   e1 = gst_element_factory_make ("videotestsrc", NULL);
101   fail_if (e1 == NULL);
102
103   e2 = gst_element_factory_make ("rtpvrawpay", "pay0");
104   fail_if (e2 == NULL);
105   g_object_set (e2, "pt", 96, NULL);
106
107   gst_bin_add_many (GST_BIN_CAST (bin), e1, e2, NULL);
108   gst_element_link_many (e1, e2, NULL);
109
110   media = gst_rtsp_media_new (bin);
111   fail_unless (GST_IS_RTSP_MEDIA (media));
112   g_object_unref (media);
113 }
114
115 GST_END_TEST;
116
117 GST_START_TEST (test_media_prepare)
118 {
119   GstRTSPMediaFactory *factory;
120   GstRTSPMedia *media;
121   GstRTSPUrl *url;
122
123   /* test non-reusable media first */
124   factory = gst_rtsp_media_factory_new ();
125   fail_if (gst_rtsp_media_factory_is_shared (factory));
126   gst_rtsp_url_parse ("rtsp://localhost:8554/test", &url);
127
128   gst_rtsp_media_factory_set_launch (factory,
129       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
130
131   media = gst_rtsp_media_factory_construct (factory, url);
132   fail_unless (GST_IS_RTSP_MEDIA (media));
133   fail_unless (gst_rtsp_media_n_streams (media) == 1);
134
135   fail_unless (gst_rtsp_media_prepare (media));
136   fail_unless (gst_rtsp_media_unprepare (media));
137   fail_unless (gst_rtsp_media_n_streams (media) == 1);
138   fail_if (gst_rtsp_media_prepare (media));
139
140   g_object_unref (media);
141   gst_rtsp_url_free (url);
142   g_object_unref (factory);
143
144   /* test reusable media */
145   factory = gst_rtsp_media_factory_new ();
146   fail_if (gst_rtsp_media_factory_is_shared (factory));
147   gst_rtsp_url_parse ("rtsp://localhost:8554/test", &url);
148
149   gst_rtsp_media_factory_set_launch (factory,
150       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
151
152   media = gst_rtsp_media_factory_construct (factory, url);
153   fail_unless (GST_IS_RTSP_MEDIA (media));
154   fail_unless (gst_rtsp_media_n_streams (media) == 1);
155
156   g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
157
158   fail_unless (gst_rtsp_media_prepare (media));
159   fail_unless (gst_rtsp_media_unprepare (media));
160   fail_unless (gst_rtsp_media_n_streams (media) == 1);
161   fail_unless (gst_rtsp_media_prepare (media));
162   fail_unless (gst_rtsp_media_unprepare (media));
163
164   g_object_unref (media);
165   gst_rtsp_url_free (url);
166   g_object_unref (factory);
167 }
168
169 GST_END_TEST;
170
171 static void
172 on_notify_caps (GstPad * pad, GParamSpec * pspec, GstElement * pay)
173 {
174   GstCaps *caps;
175
176   g_object_get (pad, "caps", &caps, NULL);
177
178   GST_DEBUG ("notify %" GST_PTR_FORMAT, caps);
179
180   if (caps) {
181     g_signal_emit_by_name (pay, "pad-added", pad);
182     g_signal_emit_by_name (pay, "no-more-pads", NULL);
183   } else {
184     g_signal_emit_by_name (pay, "pad-removed", pad);
185   }
186 }
187
188 GST_START_TEST (test_media_dyn_prepare)
189 {
190   GstRTSPMedia *media;
191   GstElement *bin, *src, *pay;
192   GstElement *pipeline;
193   GstPad *srcpad;
194
195   bin = gst_bin_new ("bin");
196   fail_if (bin == NULL);
197
198   src = gst_element_factory_make ("videotestsrc", NULL);
199   fail_if (src == NULL);
200
201   pay = gst_element_factory_make ("rtpvrawpay", "dynpay0");
202   fail_if (pay == NULL);
203   g_object_set (pay, "pt", 96, NULL);
204
205   gst_bin_add_many (GST_BIN_CAST (bin), src, pay, NULL);
206   gst_element_link_many (src, pay, NULL);
207
208   media = gst_rtsp_media_new (bin);
209   fail_unless (GST_IS_RTSP_MEDIA (media));
210
211   g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
212
213   pipeline = gst_pipeline_new ("media-pipeline");
214   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
215
216   gst_rtsp_media_collect_streams (media);
217
218   srcpad = gst_element_get_static_pad (pay, "src");
219
220   g_signal_connect (srcpad, "notify::caps", (GCallback) on_notify_caps, pay);
221
222   fail_unless (gst_rtsp_media_n_streams (media) == 0);
223   fail_unless (gst_rtsp_media_prepare (media));
224   fail_unless (gst_rtsp_media_n_streams (media) == 1);
225   fail_unless (gst_rtsp_media_unprepare (media));
226   fail_unless (gst_rtsp_media_n_streams (media) == 0);
227
228   fail_unless (gst_rtsp_media_n_streams (media) == 0);
229   fail_unless (gst_rtsp_media_prepare (media));
230   fail_unless (gst_rtsp_media_n_streams (media) == 1);
231   fail_unless (gst_rtsp_media_unprepare (media));
232   fail_unless (gst_rtsp_media_n_streams (media) == 0);
233
234   gst_object_unref (srcpad);
235   g_object_unref (media);
236 }
237
238 GST_END_TEST;
239
240 GST_START_TEST (test_media_take_pipeline)
241 {
242   GstRTSPMediaFactory *factory;
243   GstRTSPMedia *media;
244   GstRTSPUrl *url;
245   GstElement *pipeline;
246
247   factory = gst_rtsp_media_factory_new ();
248   gst_rtsp_url_parse ("rtsp://localhost:8554/test", &url);
249   gst_rtsp_media_factory_set_launch (factory,
250       "( fakesrc ! text/plain ! rtpgstpay name=pay0 )");
251
252   media = gst_rtsp_media_factory_construct (factory, url);
253   fail_unless (GST_IS_RTSP_MEDIA (media));
254
255   pipeline = gst_pipeline_new ("media-pipeline");
256   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
257
258   g_object_unref (media);
259   gst_rtsp_url_free (url);
260   g_object_unref (factory);
261 }
262
263 GST_END_TEST;
264
265 static Suite *
266 rtspmedia_suite (void)
267 {
268   Suite *s = suite_create ("rtspmedia");
269   TCase *tc = tcase_create ("general");
270
271   suite_add_tcase (s, tc);
272   tcase_set_timeout (tc, 20);
273   tcase_add_test (tc, test_launch);
274   tcase_add_test (tc, test_media);
275   tcase_add_test (tc, test_media_prepare);
276   tcase_add_test (tc, test_media_dyn_prepare);
277   tcase_add_test (tc, test_media_take_pipeline);
278
279   return s;
280 }
281
282 GST_CHECK_MAIN (rtspmedia);