2 * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
20 #include <gst/check/gstcheck.h>
22 #include <rtsp-media-factory.h>
24 GST_START_TEST (test_launch)
26 GstRTSPMediaFactory *factory;
29 GstRTSPStream *stream;
30 GstRTSPTimeRange *range;
32 GstRTSPThreadPool *pool;
33 GstRTSPThread *thread;
35 factory = gst_rtsp_media_factory_new ();
36 fail_if (gst_rtsp_media_factory_is_shared (factory));
37 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
38 &url) == GST_RTSP_OK);
40 gst_rtsp_media_factory_set_launch (factory,
41 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
43 media = gst_rtsp_media_factory_construct (factory, url);
44 fail_unless (GST_IS_RTSP_MEDIA (media));
46 fail_unless (gst_rtsp_media_n_streams (media) == 1);
48 stream = gst_rtsp_media_get_stream (media, 0);
49 fail_unless (stream != NULL);
51 /* fails, need to be prepared */
52 str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
53 fail_unless (str == NULL);
55 fail_unless (gst_rtsp_range_parse ("npt=5.0-", &range) == GST_RTSP_OK);
56 /* fails, need to be prepared */
57 fail_if (gst_rtsp_media_seek (media, range));
59 pool = gst_rtsp_thread_pool_new ();
60 thread = gst_rtsp_thread_pool_get_thread (pool,
61 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
63 fail_unless (gst_rtsp_media_prepare (media, thread));
65 str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
66 fail_unless (g_str_equal (str, "npt=0-"));
69 str = gst_rtsp_media_get_range_string (media, TRUE, GST_RTSP_RANGE_NPT);
70 fail_unless (g_str_equal (str, "npt=0-"));
73 fail_unless (gst_rtsp_media_seek (media, range));
75 str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
76 fail_unless (g_str_equal (str, "npt=5-"));
79 str = gst_rtsp_media_get_range_string (media, TRUE, GST_RTSP_RANGE_NPT);
80 fail_unless (g_str_equal (str, "npt=5-"));
83 fail_unless (gst_rtsp_media_unprepare (media));
85 /* should fail again */
86 str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
87 fail_unless (str == NULL);
88 fail_if (gst_rtsp_media_seek (media, range));
90 gst_rtsp_range_free (range);
91 g_object_unref (media);
93 gst_rtsp_url_free (url);
94 g_object_unref (factory);
96 g_object_unref (pool);
98 gst_rtsp_thread_pool_cleanup ();
103 GST_START_TEST (test_media)
106 GstElement *bin, *e1, *e2;
108 bin = gst_bin_new ("bin");
109 fail_if (bin == NULL);
111 e1 = gst_element_factory_make ("videotestsrc", NULL);
112 fail_if (e1 == NULL);
114 e2 = gst_element_factory_make ("rtpvrawpay", "pay0");
115 fail_if (e2 == NULL);
116 g_object_set (e2, "pt", 96, NULL);
118 gst_bin_add_many (GST_BIN_CAST (bin), e1, e2, NULL);
119 gst_element_link_many (e1, e2, NULL);
121 media = gst_rtsp_media_new (bin);
122 fail_unless (GST_IS_RTSP_MEDIA (media));
123 g_object_unref (media);
129 test_prepare_reusable (GstRTSPThreadPool * pool, const gchar * launch_line)
131 GstRTSPMediaFactory *factory;
134 GstRTSPThread *thread;
136 factory = gst_rtsp_media_factory_new ();
137 fail_if (gst_rtsp_media_factory_is_shared (factory));
138 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
139 &url) == GST_RTSP_OK);
141 gst_rtsp_media_factory_set_launch (factory, launch_line);
143 media = gst_rtsp_media_factory_construct (factory, url);
144 fail_unless (GST_IS_RTSP_MEDIA (media));
145 fail_unless (gst_rtsp_media_n_streams (media) == 1);
147 g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
149 thread = gst_rtsp_thread_pool_get_thread (pool,
150 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
151 fail_unless (gst_rtsp_media_prepare (media, thread));
152 fail_unless (gst_rtsp_media_unprepare (media));
153 fail_unless (gst_rtsp_media_n_streams (media) == 1);
155 thread = gst_rtsp_thread_pool_get_thread (pool,
156 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
157 fail_unless (gst_rtsp_media_prepare (media, thread));
158 fail_unless (gst_rtsp_media_unprepare (media));
160 g_object_unref (media);
161 gst_rtsp_url_free (url);
162 g_object_unref (factory);
165 GST_START_TEST (test_media_prepare)
167 GstRTSPMediaFactory *factory;
170 GstRTSPThreadPool *pool;
171 GstRTSPThread *thread;
173 pool = gst_rtsp_thread_pool_new ();
175 /* test non-reusable media first */
176 factory = gst_rtsp_media_factory_new ();
177 fail_if (gst_rtsp_media_factory_is_shared (factory));
178 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
179 &url) == GST_RTSP_OK);
181 gst_rtsp_media_factory_set_launch (factory,
182 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
184 media = gst_rtsp_media_factory_construct (factory, url);
185 fail_unless (GST_IS_RTSP_MEDIA (media));
186 fail_unless (gst_rtsp_media_n_streams (media) == 1);
188 thread = gst_rtsp_thread_pool_get_thread (pool,
189 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
190 fail_unless (gst_rtsp_media_prepare (media, thread));
191 fail_unless (gst_rtsp_media_unprepare (media));
192 fail_unless (gst_rtsp_media_n_streams (media) == 1);
194 thread = gst_rtsp_thread_pool_get_thread (pool,
195 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
196 fail_if (gst_rtsp_media_prepare (media, thread));
198 g_object_unref (media);
199 gst_rtsp_url_free (url);
200 g_object_unref (factory);
202 /* test reusable media */
203 test_prepare_reusable (pool, "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
204 test_prepare_reusable (pool,
205 "( videotestsrc is-live=true ! rtpvrawpay pt=96 name=pay0 )");
207 g_object_unref (pool);
208 gst_rtsp_thread_stop (thread);
209 gst_rtsp_thread_pool_cleanup ();
215 on_notify_caps (GstPad * pad, GParamSpec * pspec, GstElement * pay)
219 g_object_get (pad, "caps", &caps, NULL);
221 GST_DEBUG ("notify %" GST_PTR_FORMAT, caps);
224 g_signal_emit_by_name (pay, "pad-added", pad);
225 g_signal_emit_by_name (pay, "no-more-pads", NULL);
226 gst_caps_unref (caps);
228 g_signal_emit_by_name (pay, "pad-removed", pad);
232 GST_START_TEST (test_media_dyn_prepare)
235 GstElement *bin, *src, *pay;
236 GstElement *pipeline;
238 GstRTSPThreadPool *pool;
239 GstRTSPThread *thread;
241 bin = gst_bin_new ("bin");
242 fail_if (bin == NULL);
244 src = gst_element_factory_make ("videotestsrc", NULL);
245 fail_if (src == NULL);
247 pay = gst_element_factory_make ("rtpvrawpay", "dynpay0");
248 fail_if (pay == NULL);
249 g_object_set (pay, "pt", 96, NULL);
251 gst_bin_add_many (GST_BIN_CAST (bin), src, pay, NULL);
252 gst_element_link_many (src, pay, NULL);
254 media = gst_rtsp_media_new (bin);
255 fail_unless (GST_IS_RTSP_MEDIA (media));
257 g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
259 pipeline = gst_pipeline_new ("media-pipeline");
260 gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
262 gst_rtsp_media_collect_streams (media);
264 srcpad = gst_element_get_static_pad (pay, "src");
266 g_signal_connect (srcpad, "notify::caps", (GCallback) on_notify_caps, pay);
268 pool = gst_rtsp_thread_pool_new ();
270 fail_unless (gst_rtsp_media_n_streams (media) == 0);
272 thread = gst_rtsp_thread_pool_get_thread (pool,
273 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
274 fail_unless (gst_rtsp_media_prepare (media, thread));
275 fail_unless (gst_rtsp_media_n_streams (media) == 1);
276 fail_unless (gst_rtsp_media_unprepare (media));
277 fail_unless (gst_rtsp_media_n_streams (media) == 0);
279 fail_unless (gst_rtsp_media_n_streams (media) == 0);
281 thread = gst_rtsp_thread_pool_get_thread (pool,
282 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
283 fail_unless (gst_rtsp_media_prepare (media, thread));
284 fail_unless (gst_rtsp_media_n_streams (media) == 1);
285 fail_unless (gst_rtsp_media_unprepare (media));
286 fail_unless (gst_rtsp_media_n_streams (media) == 0);
288 gst_object_unref (srcpad);
289 g_object_unref (media);
290 g_object_unref (pool);
292 gst_rtsp_thread_pool_cleanup ();
297 GST_START_TEST (test_media_take_pipeline)
299 GstRTSPMediaFactory *factory;
302 GstElement *pipeline;
304 factory = gst_rtsp_media_factory_new ();
305 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
306 &url) == GST_RTSP_OK);
307 gst_rtsp_media_factory_set_launch (factory,
308 "( fakesrc ! text/plain ! rtpgstpay name=pay0 )");
310 media = gst_rtsp_media_factory_construct (factory, url);
311 fail_unless (GST_IS_RTSP_MEDIA (media));
313 pipeline = gst_pipeline_new ("media-pipeline");
314 gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
316 g_object_unref (media);
317 gst_rtsp_url_free (url);
318 g_object_unref (factory);
323 GST_START_TEST (test_media_reset)
325 GstRTSPMediaFactory *factory;
328 GstRTSPThreadPool *pool;
329 GstRTSPThread *thread;
331 pool = gst_rtsp_thread_pool_new ();
333 factory = gst_rtsp_media_factory_new ();
334 fail_if (gst_rtsp_media_factory_is_shared (factory));
335 gst_rtsp_url_parse ("rtsp://localhost:8554/test", &url);
337 gst_rtsp_media_factory_set_launch (factory,
338 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
340 media = gst_rtsp_media_factory_construct (factory, url);
341 fail_unless (GST_IS_RTSP_MEDIA (media));
343 thread = gst_rtsp_thread_pool_get_thread (pool,
344 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
345 fail_unless (gst_rtsp_media_prepare (media, thread));
346 fail_unless (gst_rtsp_media_suspend (media));
347 fail_unless (gst_rtsp_media_unprepare (media));
348 g_object_unref (media);
350 media = gst_rtsp_media_factory_construct (factory, url);
351 fail_unless (GST_IS_RTSP_MEDIA (media));
353 thread = gst_rtsp_thread_pool_get_thread (pool,
354 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
355 gst_rtsp_media_set_suspend_mode (media, GST_RTSP_SUSPEND_MODE_RESET);
356 fail_unless (gst_rtsp_media_prepare (media, thread));
357 fail_unless (gst_rtsp_media_suspend (media));
358 fail_unless (gst_rtsp_media_unprepare (media));
359 g_object_unref (media);
361 gst_rtsp_url_free (url);
362 g_object_unref (factory);
363 g_object_unref (pool);
365 gst_rtsp_thread_pool_cleanup ();
371 rtspmedia_suite (void)
373 Suite *s = suite_create ("rtspmedia");
374 TCase *tc = tcase_create ("general");
376 suite_add_tcase (s, tc);
377 tcase_set_timeout (tc, 20);
378 tcase_add_test (tc, test_launch);
379 tcase_add_test (tc, test_media);
380 tcase_add_test (tc, test_media_prepare);
381 tcase_add_test (tc, test_media_dyn_prepare);
382 tcase_add_test (tc, test_media_take_pipeline);
383 tcase_add_test (tc, test_media_reset);
388 GST_CHECK_MAIN (rtspmedia);