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);
101 GST_START_TEST (test_media)
104 GstElement *bin, *e1, *e2;
106 bin = gst_bin_new ("bin");
107 fail_if (bin == NULL);
109 e1 = gst_element_factory_make ("videotestsrc", NULL);
110 fail_if (e1 == NULL);
112 e2 = gst_element_factory_make ("rtpvrawpay", "pay0");
113 fail_if (e2 == NULL);
114 g_object_set (e2, "pt", 96, NULL);
116 gst_bin_add_many (GST_BIN_CAST (bin), e1, e2, NULL);
117 gst_element_link_many (e1, e2, NULL);
119 media = gst_rtsp_media_new (bin);
120 fail_unless (GST_IS_RTSP_MEDIA (media));
121 g_object_unref (media);
126 GST_START_TEST (test_media_prepare)
128 GstRTSPMediaFactory *factory;
131 GstRTSPThreadPool *pool;
132 GstRTSPThread *thread;
134 pool = gst_rtsp_thread_pool_new ();
136 /* test non-reusable media first */
137 factory = gst_rtsp_media_factory_new ();
138 fail_if (gst_rtsp_media_factory_is_shared (factory));
139 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
140 &url) == GST_RTSP_OK);
142 gst_rtsp_media_factory_set_launch (factory,
143 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
145 media = gst_rtsp_media_factory_construct (factory, url);
146 fail_unless (GST_IS_RTSP_MEDIA (media));
147 fail_unless (gst_rtsp_media_n_streams (media) == 1);
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_if (gst_rtsp_media_prepare (media, thread));
159 g_object_unref (media);
160 gst_rtsp_url_free (url);
161 g_object_unref (factory);
163 /* test reusable media */
164 factory = gst_rtsp_media_factory_new ();
165 fail_if (gst_rtsp_media_factory_is_shared (factory));
166 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
167 &url) == GST_RTSP_OK);
169 gst_rtsp_media_factory_set_launch (factory,
170 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
172 media = gst_rtsp_media_factory_construct (factory, url);
173 fail_unless (GST_IS_RTSP_MEDIA (media));
174 fail_unless (gst_rtsp_media_n_streams (media) == 1);
176 g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
178 thread = gst_rtsp_thread_pool_get_thread (pool,
179 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
180 fail_unless (gst_rtsp_media_prepare (media, thread));
181 fail_unless (gst_rtsp_media_unprepare (media));
182 fail_unless (gst_rtsp_media_n_streams (media) == 1);
184 thread = gst_rtsp_thread_pool_get_thread (pool,
185 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
186 fail_unless (gst_rtsp_media_prepare (media, thread));
187 fail_unless (gst_rtsp_media_unprepare (media));
189 g_object_unref (media);
190 gst_rtsp_url_free (url);
191 g_object_unref (factory);
197 on_notify_caps (GstPad * pad, GParamSpec * pspec, GstElement * pay)
201 g_object_get (pad, "caps", &caps, NULL);
203 GST_DEBUG ("notify %" GST_PTR_FORMAT, caps);
206 g_signal_emit_by_name (pay, "pad-added", pad);
207 g_signal_emit_by_name (pay, "no-more-pads", NULL);
208 gst_caps_unref (caps);
210 g_signal_emit_by_name (pay, "pad-removed", pad);
214 GST_START_TEST (test_media_dyn_prepare)
217 GstElement *bin, *src, *pay;
218 GstElement *pipeline;
220 GstRTSPThreadPool *pool;
221 GstRTSPThread *thread;
223 bin = gst_bin_new ("bin");
224 fail_if (bin == NULL);
226 src = gst_element_factory_make ("videotestsrc", NULL);
227 fail_if (src == NULL);
229 pay = gst_element_factory_make ("rtpvrawpay", "dynpay0");
230 fail_if (pay == NULL);
231 g_object_set (pay, "pt", 96, NULL);
233 gst_bin_add_many (GST_BIN_CAST (bin), src, pay, NULL);
234 gst_element_link_many (src, pay, NULL);
236 media = gst_rtsp_media_new (bin);
237 fail_unless (GST_IS_RTSP_MEDIA (media));
239 g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
241 pipeline = gst_pipeline_new ("media-pipeline");
242 gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
244 gst_rtsp_media_collect_streams (media);
246 srcpad = gst_element_get_static_pad (pay, "src");
248 g_signal_connect (srcpad, "notify::caps", (GCallback) on_notify_caps, pay);
250 pool = gst_rtsp_thread_pool_new ();
252 fail_unless (gst_rtsp_media_n_streams (media) == 0);
254 thread = gst_rtsp_thread_pool_get_thread (pool,
255 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
256 fail_unless (gst_rtsp_media_prepare (media, thread));
257 fail_unless (gst_rtsp_media_n_streams (media) == 1);
258 fail_unless (gst_rtsp_media_unprepare (media));
259 fail_unless (gst_rtsp_media_n_streams (media) == 0);
261 fail_unless (gst_rtsp_media_n_streams (media) == 0);
263 thread = gst_rtsp_thread_pool_get_thread (pool,
264 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
265 fail_unless (gst_rtsp_media_prepare (media, thread));
266 fail_unless (gst_rtsp_media_n_streams (media) == 1);
267 fail_unless (gst_rtsp_media_unprepare (media));
268 fail_unless (gst_rtsp_media_n_streams (media) == 0);
270 gst_object_unref (srcpad);
271 g_object_unref (media);
272 g_object_unref (pool);
277 GST_START_TEST (test_media_take_pipeline)
279 GstRTSPMediaFactory *factory;
282 GstElement *pipeline;
284 factory = gst_rtsp_media_factory_new ();
285 fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
286 &url) == GST_RTSP_OK);
287 gst_rtsp_media_factory_set_launch (factory,
288 "( fakesrc ! text/plain ! rtpgstpay name=pay0 )");
290 media = gst_rtsp_media_factory_construct (factory, url);
291 fail_unless (GST_IS_RTSP_MEDIA (media));
293 pipeline = gst_pipeline_new ("media-pipeline");
294 gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
296 g_object_unref (media);
297 gst_rtsp_url_free (url);
298 g_object_unref (factory);
303 GST_START_TEST (test_media_reset)
305 GstRTSPMediaFactory *factory;
308 GstRTSPThreadPool *pool;
309 GstRTSPThread *thread;
311 pool = gst_rtsp_thread_pool_new ();
313 factory = gst_rtsp_media_factory_new ();
314 fail_if (gst_rtsp_media_factory_is_shared (factory));
315 gst_rtsp_url_parse ("rtsp://localhost:8554/test", &url);
317 gst_rtsp_media_factory_set_launch (factory,
318 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
320 media = gst_rtsp_media_factory_construct (factory, url);
321 fail_unless (GST_IS_RTSP_MEDIA (media));
323 thread = gst_rtsp_thread_pool_get_thread (pool,
324 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
325 fail_unless (gst_rtsp_media_prepare (media, thread));
326 fail_unless (gst_rtsp_media_suspend (media));
327 fail_unless (gst_rtsp_media_unprepare (media));
328 g_object_unref (media);
330 media = gst_rtsp_media_factory_construct (factory, url);
331 fail_unless (GST_IS_RTSP_MEDIA (media));
333 thread = gst_rtsp_thread_pool_get_thread (pool,
334 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
335 gst_rtsp_media_set_suspend_mode (media, GST_RTSP_SUSPEND_MODE_RESET);
336 fail_unless (gst_rtsp_media_prepare (media, thread));
337 fail_unless (gst_rtsp_media_suspend (media));
338 fail_unless (gst_rtsp_media_unprepare (media));
339 g_object_unref (media);
341 gst_rtsp_url_free (url);
342 g_object_unref (factory);
348 rtspmedia_suite (void)
350 Suite *s = suite_create ("rtspmedia");
351 TCase *tc = tcase_create ("general");
353 suite_add_tcase (s, tc);
354 tcase_set_timeout (tc, 20);
355 tcase_add_test (tc, test_launch);
356 tcase_add_test (tc, test_media);
357 tcase_add_test (tc, test_media_prepare);
358 tcase_add_test (tc, test_media_dyn_prepare);
359 tcase_add_test (tc, test_media_take_pipeline);
360 tcase_add_test (tc, test_media_reset);
365 GST_CHECK_MAIN (rtspmedia);