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_pool_cleanup ();
214 on_notify_caps (GstPad * pad, GParamSpec * pspec, GstElement * pay)
218 g_object_get (pad, "caps", &caps, NULL);
220 GST_DEBUG ("notify %" GST_PTR_FORMAT, caps);
223 g_signal_emit_by_name (pay, "pad-added", pad);
224 g_signal_emit_by_name (pay, "no-more-pads", NULL);
225 gst_caps_unref (caps);
227 g_signal_emit_by_name (pay, "pad-removed", pad);
231 GST_START_TEST (test_media_dyn_prepare)
234 GstElement *bin, *src, *pay;
235 GstElement *pipeline;
237 GstRTSPThreadPool *pool;
238 GstRTSPThread *thread;
240 bin = gst_bin_new ("bin");
241 fail_if (bin == NULL);
243 src = gst_element_factory_make ("videotestsrc", NULL);
244 fail_if (src == NULL);
246 pay = gst_element_factory_make ("rtpvrawpay", "dynpay0");
247 fail_if (pay == NULL);
248 g_object_set (pay, "pt", 96, NULL);
250 gst_bin_add_many (GST_BIN_CAST (bin), src, pay, NULL);
251 gst_element_link_many (src, pay, NULL);
253 media = gst_rtsp_media_new (bin);
254 fail_unless (GST_IS_RTSP_MEDIA (media));
256 g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
258 pipeline = gst_pipeline_new ("media-pipeline");
259 gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
261 gst_rtsp_media_collect_streams (media);
263 srcpad = gst_element_get_static_pad (pay, "src");
265 g_signal_connect (srcpad, "notify::caps", (GCallback) on_notify_caps, pay);
267 pool = gst_rtsp_thread_pool_new ();
269 fail_unless (gst_rtsp_media_n_streams (media) == 0);
271 thread = gst_rtsp_thread_pool_get_thread (pool,
272 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
273 fail_unless (gst_rtsp_media_prepare (media, thread));
274 fail_unless (gst_rtsp_media_n_streams (media) == 1);
275 fail_unless (gst_rtsp_media_unprepare (media));
276 fail_unless (gst_rtsp_media_n_streams (media) == 0);
278 fail_unless (gst_rtsp_media_n_streams (media) == 0);
280 thread = gst_rtsp_thread_pool_get_thread (pool,
281 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
282 fail_unless (gst_rtsp_media_prepare (media, thread));
283 fail_unless (gst_rtsp_media_n_streams (media) == 1);
284 fail_unless (gst_rtsp_media_unprepare (media));
285 fail_unless (gst_rtsp_media_n_streams (media) == 0);
287 gst_object_unref (srcpad);
288 g_object_unref (media);
289 g_object_unref (pool);
291 gst_rtsp_thread_pool_cleanup ();
296 GST_START_TEST (test_media_prepare_port_alloc_fail)
298 GstRTSPMediaFactory *factory;
301 GstRTSPThreadPool *pool;
302 GstRTSPThread *thread;
303 GstRTSPAddressPool *addrpool;
305 pool = gst_rtsp_thread_pool_new ();
307 factory = gst_rtsp_media_factory_new ();
308 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
309 &url) == GST_RTSP_OK);
311 gst_rtsp_media_factory_set_launch (factory,
312 "( fakesrc is-live=true ! text/plain ! rtpgstpay name=pay0 )");
314 media = gst_rtsp_media_factory_construct (factory, url);
315 fail_unless (GST_IS_RTSP_MEDIA (media));
317 addrpool = gst_rtsp_address_pool_new ();
318 fail_unless (gst_rtsp_address_pool_add_range (addrpool, "192.168.1.1",
319 "192.168.1.1", 6000, 6001, 0));
320 gst_rtsp_media_set_address_pool (media, addrpool);
322 thread = gst_rtsp_thread_pool_get_thread (pool,
323 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
324 fail_if (gst_rtsp_media_prepare (media, thread));
326 g_object_unref (media);
327 g_object_unref (addrpool);
328 gst_rtsp_url_free (url);
329 g_object_unref (factory);
330 g_object_unref (pool);
335 GST_START_TEST (test_media_take_pipeline)
337 GstRTSPMediaFactory *factory;
340 GstElement *pipeline;
342 factory = gst_rtsp_media_factory_new ();
343 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
344 &url) == GST_RTSP_OK);
345 gst_rtsp_media_factory_set_launch (factory,
346 "( fakesrc ! text/plain ! rtpgstpay name=pay0 )");
348 media = gst_rtsp_media_factory_construct (factory, url);
349 fail_unless (GST_IS_RTSP_MEDIA (media));
351 pipeline = gst_pipeline_new ("media-pipeline");
352 gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
354 g_object_unref (media);
355 gst_rtsp_url_free (url);
356 g_object_unref (factory);
361 GST_START_TEST (test_media_reset)
363 GstRTSPMediaFactory *factory;
366 GstRTSPThreadPool *pool;
367 GstRTSPThread *thread;
369 pool = gst_rtsp_thread_pool_new ();
371 factory = gst_rtsp_media_factory_new ();
372 fail_if (gst_rtsp_media_factory_is_shared (factory));
373 gst_rtsp_url_parse ("rtsp://localhost:8554/test", &url);
375 gst_rtsp_media_factory_set_launch (factory,
376 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
378 media = gst_rtsp_media_factory_construct (factory, url);
379 fail_unless (GST_IS_RTSP_MEDIA (media));
381 thread = gst_rtsp_thread_pool_get_thread (pool,
382 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
383 fail_unless (gst_rtsp_media_prepare (media, thread));
384 fail_unless (gst_rtsp_media_suspend (media));
385 fail_unless (gst_rtsp_media_unprepare (media));
386 g_object_unref (media);
388 media = gst_rtsp_media_factory_construct (factory, url);
389 fail_unless (GST_IS_RTSP_MEDIA (media));
391 thread = gst_rtsp_thread_pool_get_thread (pool,
392 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
393 gst_rtsp_media_set_suspend_mode (media, GST_RTSP_SUSPEND_MODE_RESET);
394 fail_unless (gst_rtsp_media_prepare (media, thread));
395 fail_unless (gst_rtsp_media_suspend (media));
396 fail_unless (gst_rtsp_media_unprepare (media));
397 g_object_unref (media);
399 gst_rtsp_url_free (url);
400 g_object_unref (factory);
401 g_object_unref (pool);
403 gst_rtsp_thread_pool_cleanup ();
409 rtspmedia_suite (void)
411 Suite *s = suite_create ("rtspmedia");
412 TCase *tc = tcase_create ("general");
414 suite_add_tcase (s, tc);
415 tcase_set_timeout (tc, 20);
416 tcase_add_test (tc, test_launch);
417 tcase_add_test (tc, test_media);
418 tcase_add_test (tc, test_media_prepare);
419 tcase_add_test (tc, test_media_dyn_prepare);
420 tcase_add_test (tc, test_media_prepare_port_alloc_fail);
421 tcase_add_test (tc, test_media_take_pipeline);
422 tcase_add_test (tc, test_media_reset);
427 GST_CHECK_MAIN (rtspmedia);