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_media_seek)
26 GstRTSPMediaFactory *factory;
29 GstRTSPStream *stream;
30 GstRTSPTimeRange *range;
32 GstRTSPThreadPool *pool;
33 GstRTSPThread *thread;
34 GstRTSPTransport *transport;
36 factory = gst_rtsp_media_factory_new ();
37 fail_if (gst_rtsp_media_factory_is_shared (factory));
38 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
39 &url) == GST_RTSP_OK);
41 gst_rtsp_media_factory_set_launch (factory,
42 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
44 media = gst_rtsp_media_factory_construct (factory, url);
45 fail_unless (GST_IS_RTSP_MEDIA (media));
47 fail_unless (gst_rtsp_media_n_streams (media) == 1);
49 stream = gst_rtsp_media_get_stream (media, 0);
50 fail_unless (stream != NULL);
52 pool = gst_rtsp_thread_pool_new ();
53 thread = gst_rtsp_thread_pool_get_thread (pool,
54 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
56 fail_unless (gst_rtsp_media_prepare (media, thread));
58 /* define transport */
59 fail_unless (gst_rtsp_transport_new (&transport) == GST_RTSP_OK);
60 transport->lower_transport = GST_RTSP_LOWER_TRANS_TCP;
62 fail_unless (gst_rtsp_stream_complete_stream (stream, transport));
64 fail_unless (gst_rtsp_transport_free (transport) == GST_RTSP_OK);
65 fail_unless (gst_rtsp_range_parse ("npt=5.0-", &range) == GST_RTSP_OK);
67 /* the media is seekable now */
68 fail_unless (gst_rtsp_media_seek (media, range));
70 str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
71 fail_unless (g_str_equal (str, "npt=5-"));
73 gst_rtsp_range_free (range);
76 fail_unless (gst_rtsp_media_unprepare (media));
77 g_object_unref (media);
79 gst_rtsp_url_free (url);
80 g_object_unref (factory);
82 g_object_unref (pool);
84 gst_rtsp_thread_pool_cleanup ();
90 GST_START_TEST (test_media_seek_no_sinks)
92 GstRTSPMediaFactory *factory;
95 GstRTSPStream *stream;
96 GstRTSPTimeRange *range;
98 GstRTSPThreadPool *pool;
99 GstRTSPThread *thread;
101 factory = gst_rtsp_media_factory_new ();
102 fail_if (gst_rtsp_media_factory_is_shared (factory));
103 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
104 &url) == GST_RTSP_OK);
106 gst_rtsp_media_factory_set_launch (factory,
107 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
109 media = gst_rtsp_media_factory_construct (factory, url);
110 fail_unless (GST_IS_RTSP_MEDIA (media));
112 fail_unless (gst_rtsp_media_n_streams (media) == 1);
114 stream = gst_rtsp_media_get_stream (media, 0);
115 fail_unless (stream != NULL);
117 /* fails, need to be prepared */
118 str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
119 fail_unless (str == NULL);
121 fail_unless (gst_rtsp_range_parse ("npt=5.0-", &range) == GST_RTSP_OK);
122 /* fails, need to be prepared */
123 fail_if (gst_rtsp_media_seek (media, range));
125 pool = gst_rtsp_thread_pool_new ();
126 thread = gst_rtsp_thread_pool_get_thread (pool,
127 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
129 fail_unless (gst_rtsp_media_prepare (media, thread));
131 str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
132 fail_unless (g_str_equal (str, "npt=0-"));
135 str = gst_rtsp_media_get_range_string (media, TRUE, GST_RTSP_RANGE_NPT);
136 fail_unless (g_str_equal (str, "npt=0-"));
139 /* fails, need to be prepared and contain sink elements */
140 fail_if (gst_rtsp_media_seek (media, range));
142 fail_unless (gst_rtsp_media_unprepare (media));
144 /* should fail again */
145 str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
146 fail_unless (str == NULL);
147 fail_if (gst_rtsp_media_seek (media, range));
149 gst_rtsp_range_free (range);
150 g_object_unref (media);
152 gst_rtsp_url_free (url);
153 g_object_unref (factory);
155 g_object_unref (pool);
157 gst_rtsp_thread_pool_cleanup ();
162 GST_START_TEST (test_media)
165 GstElement *bin, *e1, *e2;
167 bin = gst_bin_new ("bin");
168 fail_if (bin == NULL);
170 e1 = gst_element_factory_make ("videotestsrc", NULL);
171 fail_if (e1 == NULL);
173 e2 = gst_element_factory_make ("rtpvrawpay", "pay0");
174 fail_if (e2 == NULL);
175 g_object_set (e2, "pt", 96, NULL);
177 gst_bin_add_many (GST_BIN_CAST (bin), e1, e2, NULL);
178 gst_element_link_many (e1, e2, NULL);
180 media = gst_rtsp_media_new (bin);
181 fail_unless (GST_IS_RTSP_MEDIA (media));
182 g_object_unref (media);
188 test_prepare_reusable (const gchar * launch_line)
190 GstRTSPMediaFactory *factory;
193 GstRTSPThread *thread;
194 GstRTSPThreadPool *pool;
196 factory = gst_rtsp_media_factory_new ();
197 fail_if (gst_rtsp_media_factory_is_shared (factory));
198 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
199 &url) == GST_RTSP_OK);
201 gst_rtsp_media_factory_set_launch (factory, launch_line);
203 media = gst_rtsp_media_factory_construct (factory, url);
204 fail_unless (GST_IS_RTSP_MEDIA (media));
205 fail_unless (gst_rtsp_media_n_streams (media) == 1);
207 g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
209 pool = gst_rtsp_thread_pool_new ();
210 thread = gst_rtsp_thread_pool_get_thread (pool,
211 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
212 fail_unless (gst_rtsp_media_prepare (media, thread));
213 fail_unless (gst_rtsp_media_unprepare (media));
214 fail_unless (gst_rtsp_media_n_streams (media) == 1);
216 thread = gst_rtsp_thread_pool_get_thread (pool,
217 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
218 fail_unless (gst_rtsp_media_prepare (media, thread));
219 fail_unless (gst_rtsp_media_unprepare (media));
221 g_object_unref (media);
222 gst_rtsp_url_free (url);
223 g_object_unref (factory);
226 GST_START_TEST (test_media_reusable)
229 /* test reusable media */
230 test_prepare_reusable ("( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
231 test_prepare_reusable (
232 "( videotestsrc is-live=true ! rtpvrawpay pt=96 name=pay0 )");
237 GST_START_TEST (test_media_prepare)
239 GstRTSPMediaFactory *factory;
242 GstRTSPThreadPool *pool;
243 GstRTSPThread *thread;
245 pool = gst_rtsp_thread_pool_new ();
247 /* test non-reusable media first */
248 factory = gst_rtsp_media_factory_new ();
249 fail_if (gst_rtsp_media_factory_is_shared (factory));
250 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
251 &url) == GST_RTSP_OK);
253 gst_rtsp_media_factory_set_launch (factory,
254 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
256 media = gst_rtsp_media_factory_construct (factory, url);
257 fail_unless (GST_IS_RTSP_MEDIA (media));
258 fail_unless (gst_rtsp_media_n_streams (media) == 1);
260 thread = gst_rtsp_thread_pool_get_thread (pool,
261 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
262 fail_unless (gst_rtsp_media_prepare (media, thread));
263 fail_unless (gst_rtsp_media_unprepare (media));
264 fail_unless (gst_rtsp_media_n_streams (media) == 1);
266 thread = gst_rtsp_thread_pool_get_thread (pool,
267 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
268 fail_if (gst_rtsp_media_prepare (media, thread));
270 g_object_unref (media);
271 gst_rtsp_url_free (url);
272 g_object_unref (factory);
274 g_object_unref (pool);
275 gst_rtsp_thread_pool_cleanup ();
280 #define FLAG_HAVE_CAPS GST_ELEMENT_FLAG_LAST
282 on_notify_caps (GstPad * pad, GParamSpec * pspec, GstElement * pay)
286 g_object_get (pad, "caps", &caps, NULL);
288 GST_DEBUG ("notify %" GST_PTR_FORMAT, caps);
291 if (!GST_OBJECT_FLAG_IS_SET (pay, FLAG_HAVE_CAPS)) {
292 g_signal_emit_by_name (pay, "pad-added", pad);
293 g_signal_emit_by_name (pay, "no-more-pads", NULL);
294 GST_OBJECT_FLAG_SET (pay, FLAG_HAVE_CAPS);
296 gst_caps_unref (caps);
298 if (GST_OBJECT_FLAG_IS_SET (pay, FLAG_HAVE_CAPS)) {
299 g_signal_emit_by_name (pay, "pad-removed", pad);
300 GST_OBJECT_FLAG_UNSET (pay, FLAG_HAVE_CAPS);
305 GST_START_TEST (test_media_dyn_prepare)
308 GstElement *bin, *src, *pay;
309 GstElement *pipeline;
311 GstRTSPThreadPool *pool;
312 GstRTSPThread *thread;
314 bin = gst_bin_new ("bin");
315 fail_if (bin == NULL);
317 src = gst_element_factory_make ("videotestsrc", NULL);
318 fail_if (src == NULL);
320 pay = gst_element_factory_make ("rtpvrawpay", "dynpay0");
321 fail_if (pay == NULL);
322 g_object_set (pay, "pt", 96, NULL);
324 gst_bin_add_many (GST_BIN_CAST (bin), src, pay, NULL);
325 gst_element_link_many (src, pay, NULL);
327 media = gst_rtsp_media_new (bin);
328 fail_unless (GST_IS_RTSP_MEDIA (media));
330 g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
332 pipeline = gst_pipeline_new ("media-pipeline");
333 gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
335 gst_rtsp_media_collect_streams (media);
337 srcpad = gst_element_get_static_pad (pay, "src");
339 g_signal_connect (srcpad, "notify::caps", (GCallback) on_notify_caps, pay);
341 pool = gst_rtsp_thread_pool_new ();
343 fail_unless (gst_rtsp_media_n_streams (media) == 0);
345 thread = gst_rtsp_thread_pool_get_thread (pool,
346 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
347 fail_unless (gst_rtsp_media_prepare (media, thread));
348 fail_unless (gst_rtsp_media_n_streams (media) == 1);
349 fail_unless (gst_rtsp_media_unprepare (media));
350 fail_unless (gst_rtsp_media_n_streams (media) == 0);
352 thread = gst_rtsp_thread_pool_get_thread (pool,
353 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
354 fail_unless (gst_rtsp_media_prepare (media, thread));
355 fail_unless (gst_rtsp_media_n_streams (media) == 1);
356 fail_unless (gst_rtsp_media_unprepare (media));
357 fail_unless (gst_rtsp_media_n_streams (media) == 0);
359 gst_object_unref (srcpad);
360 g_object_unref (media);
361 g_object_unref (pool);
363 gst_rtsp_thread_pool_cleanup ();
368 GST_START_TEST (test_media_take_pipeline)
370 GstRTSPMediaFactory *factory;
373 GstElement *pipeline;
375 factory = gst_rtsp_media_factory_new ();
376 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
377 &url) == GST_RTSP_OK);
378 gst_rtsp_media_factory_set_launch (factory,
379 "( fakesrc ! text/plain ! rtpgstpay name=pay0 )");
381 media = gst_rtsp_media_factory_construct (factory, url);
382 fail_unless (GST_IS_RTSP_MEDIA (media));
384 pipeline = gst_pipeline_new ("media-pipeline");
385 gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
387 g_object_unref (media);
388 gst_rtsp_url_free (url);
389 g_object_unref (factory);
394 GST_START_TEST (test_media_reset)
396 GstRTSPMediaFactory *factory;
399 GstRTSPThreadPool *pool;
400 GstRTSPThread *thread;
402 pool = gst_rtsp_thread_pool_new ();
404 factory = gst_rtsp_media_factory_new ();
405 fail_if (gst_rtsp_media_factory_is_shared (factory));
406 gst_rtsp_url_parse ("rtsp://localhost:8554/test", &url);
408 gst_rtsp_media_factory_set_launch (factory,
409 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
411 media = gst_rtsp_media_factory_construct (factory, url);
412 fail_unless (GST_IS_RTSP_MEDIA (media));
414 thread = gst_rtsp_thread_pool_get_thread (pool,
415 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
416 fail_unless (gst_rtsp_media_prepare (media, thread));
417 fail_unless (gst_rtsp_media_suspend (media));
418 fail_unless (gst_rtsp_media_unprepare (media));
419 g_object_unref (media);
421 media = gst_rtsp_media_factory_construct (factory, url);
422 fail_unless (GST_IS_RTSP_MEDIA (media));
424 thread = gst_rtsp_thread_pool_get_thread (pool,
425 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
426 gst_rtsp_media_set_suspend_mode (media, GST_RTSP_SUSPEND_MODE_RESET);
427 fail_unless (gst_rtsp_media_prepare (media, thread));
428 fail_unless (gst_rtsp_media_suspend (media));
429 fail_unless (gst_rtsp_media_unprepare (media));
430 g_object_unref (media);
432 gst_rtsp_url_free (url);
433 g_object_unref (factory);
434 g_object_unref (pool);
436 gst_rtsp_thread_pool_cleanup ();
441 GST_START_TEST (test_media_multidyn_prepare)
444 GstElement *bin, *src0, *pay0, *src1, *pay1;
445 GstElement *pipeline;
446 GstPad *srcpad0, *srcpad1;
447 GstRTSPThreadPool *pool;
448 GstRTSPThread *thread;
450 bin = gst_bin_new ("bin");
451 fail_if (bin == NULL);
453 src0 = gst_element_factory_make ("videotestsrc", NULL);
454 fail_if (src0 == NULL);
456 pay0 = gst_element_factory_make ("rtpvrawpay", "dynpay0");
457 fail_if (pay0 == NULL);
458 g_object_set (pay0, "pt", 96, NULL);
460 src1 = gst_element_factory_make ("videotestsrc", NULL);
461 fail_if (src1 == NULL);
463 pay1 = gst_element_factory_make ("rtpvrawpay", "dynpay1");
464 fail_if (pay1 == NULL);
465 g_object_set (pay1, "pt", 97, NULL);
467 gst_bin_add_many (GST_BIN_CAST (bin), src0, pay0, src1, pay1, NULL);
468 gst_element_link_many (src0, pay0, NULL);
469 gst_element_link_many (src1, pay1, NULL);
471 media = gst_rtsp_media_new (bin);
472 fail_unless (GST_IS_RTSP_MEDIA (media));
474 g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
476 pipeline = gst_pipeline_new ("media-pipeline");
477 gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
479 gst_rtsp_media_collect_streams (media);
481 srcpad0 = gst_element_get_static_pad (pay0, "src");
482 srcpad1 = gst_element_get_static_pad (pay1, "src");
484 g_signal_connect (srcpad0, "notify::caps", (GCallback) on_notify_caps, pay0);
485 g_signal_connect (srcpad1, "notify::caps", (GCallback) on_notify_caps, pay1);
487 pool = gst_rtsp_thread_pool_new ();
489 fail_unless (gst_rtsp_media_n_streams (media) == 0);
491 thread = gst_rtsp_thread_pool_get_thread (pool,
492 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
493 fail_unless (gst_rtsp_media_prepare (media, thread));
494 fail_unless (gst_rtsp_media_n_streams (media) == 2);
495 fail_unless (gst_rtsp_media_unprepare (media));
496 fail_unless (gst_rtsp_media_n_streams (media) == 0);
498 thread = gst_rtsp_thread_pool_get_thread (pool,
499 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
500 fail_unless (gst_rtsp_media_prepare (media, thread));
501 fail_unless (gst_rtsp_media_n_streams (media) == 2);
502 fail_unless (gst_rtsp_media_unprepare (media));
503 fail_unless (gst_rtsp_media_n_streams (media) == 0);
505 gst_object_unref (srcpad0);
506 gst_object_unref (srcpad1);
507 g_object_unref (media);
508 g_object_unref (pool);
510 gst_rtsp_thread_pool_cleanup ();
517 rtspmedia_suite (void)
519 Suite *s = suite_create ("rtspmedia");
520 TCase *tc = tcase_create ("general");
522 suite_add_tcase (s, tc);
523 tcase_set_timeout (tc, 20);
524 tcase_add_test (tc, test_media_seek);
525 tcase_add_test (tc, test_media_seek_no_sinks);
526 tcase_add_test (tc, test_media);
527 tcase_add_test (tc, test_media_prepare);
528 tcase_add_test (tc, test_media_reusable);
529 tcase_add_test (tc, test_media_dyn_prepare);
530 tcase_add_test (tc, test_media_take_pipeline);
531 tcase_add_test (tc, test_media_reset);
532 tcase_add_test (tc, test_media_multidyn_prepare);
537 GST_CHECK_MAIN (rtspmedia);