f2a58cedd4aa3fedb0234014359b4577dd01b33b
[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   GstRTSPThreadPool *pool;
33   GstRTSPThread *thread;
34
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);
39
40   gst_rtsp_media_factory_set_launch (factory,
41       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
42
43   media = gst_rtsp_media_factory_construct (factory, url);
44   fail_unless (GST_IS_RTSP_MEDIA (media));
45
46   fail_unless (gst_rtsp_media_n_streams (media) == 1);
47
48   stream = gst_rtsp_media_get_stream (media, 0);
49   fail_unless (stream != NULL);
50
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);
54
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));
58
59   pool = gst_rtsp_thread_pool_new ();
60   thread = gst_rtsp_thread_pool_get_thread (pool,
61       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
62
63   fail_unless (gst_rtsp_media_prepare (media, thread));
64
65   str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
66   fail_unless (g_str_equal (str, "npt=0-"));
67   g_free (str);
68
69   str = gst_rtsp_media_get_range_string (media, TRUE, GST_RTSP_RANGE_NPT);
70   fail_unless (g_str_equal (str, "npt=0-"));
71   g_free (str);
72
73   fail_unless (gst_rtsp_media_seek (media, range));
74
75   str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
76   fail_unless (g_str_equal (str, "npt=5-"));
77   g_free (str);
78
79   str = gst_rtsp_media_get_range_string (media, TRUE, GST_RTSP_RANGE_NPT);
80   fail_unless (g_str_equal (str, "npt=5-"));
81   g_free (str);
82
83   fail_unless (gst_rtsp_media_unprepare (media));
84
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));
89
90   gst_rtsp_range_free (range);
91   g_object_unref (media);
92
93   gst_rtsp_url_free (url);
94   g_object_unref (factory);
95
96   g_object_unref (pool);
97
98   gst_rtsp_thread_pool_cleanup ();
99 }
100
101 GST_END_TEST;
102
103 GST_START_TEST (test_media)
104 {
105   GstRTSPMedia *media;
106   GstElement *bin, *e1, *e2;
107
108   bin = gst_bin_new ("bin");
109   fail_if (bin == NULL);
110
111   e1 = gst_element_factory_make ("videotestsrc", NULL);
112   fail_if (e1 == NULL);
113
114   e2 = gst_element_factory_make ("rtpvrawpay", "pay0");
115   fail_if (e2 == NULL);
116   g_object_set (e2, "pt", 96, NULL);
117
118   gst_bin_add_many (GST_BIN_CAST (bin), e1, e2, NULL);
119   gst_element_link_many (e1, e2, NULL);
120
121   media = gst_rtsp_media_new (bin);
122   fail_unless (GST_IS_RTSP_MEDIA (media));
123   g_object_unref (media);
124 }
125
126 GST_END_TEST;
127
128 static void
129 test_prepare_reusable (GstRTSPThreadPool * pool, const gchar * launch_line)
130 {
131   GstRTSPMediaFactory *factory;
132   GstRTSPMedia *media;
133   GstRTSPUrl *url;
134   GstRTSPThread *thread;
135
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);
140
141   gst_rtsp_media_factory_set_launch (factory, launch_line);
142
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);
146
147   g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
148
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);
154
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));
159
160   g_object_unref (media);
161   gst_rtsp_url_free (url);
162   g_object_unref (factory);
163 }
164
165 GST_START_TEST (test_media_prepare)
166 {
167   GstRTSPMediaFactory *factory;
168   GstRTSPMedia *media;
169   GstRTSPUrl *url;
170   GstRTSPThreadPool *pool;
171   GstRTSPThread *thread;
172
173   pool = gst_rtsp_thread_pool_new ();
174
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);
180
181   gst_rtsp_media_factory_set_launch (factory,
182       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
183
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);
187
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);
193
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));
197
198   g_object_unref (media);
199   gst_rtsp_url_free (url);
200   g_object_unref (factory);
201
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 )");
206
207   g_object_unref (pool);
208   gst_rtsp_thread_pool_cleanup ();
209 }
210
211 GST_END_TEST;
212
213 static void
214 on_notify_caps (GstPad * pad, GParamSpec * pspec, GstElement * pay)
215 {
216   GstCaps *caps;
217
218   g_object_get (pad, "caps", &caps, NULL);
219
220   GST_DEBUG ("notify %" GST_PTR_FORMAT, caps);
221
222   if (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);
226   } else {
227     g_signal_emit_by_name (pay, "pad-removed", pad);
228   }
229 }
230
231 GST_START_TEST (test_media_dyn_prepare)
232 {
233   GstRTSPMedia *media;
234   GstElement *bin, *src, *pay;
235   GstElement *pipeline;
236   GstPad *srcpad;
237   GstRTSPThreadPool *pool;
238   GstRTSPThread *thread;
239
240   bin = gst_bin_new ("bin");
241   fail_if (bin == NULL);
242
243   src = gst_element_factory_make ("videotestsrc", NULL);
244   fail_if (src == NULL);
245
246   pay = gst_element_factory_make ("rtpvrawpay", "dynpay0");
247   fail_if (pay == NULL);
248   g_object_set (pay, "pt", 96, NULL);
249
250   gst_bin_add_many (GST_BIN_CAST (bin), src, pay, NULL);
251   gst_element_link_many (src, pay, NULL);
252
253   media = gst_rtsp_media_new (bin);
254   fail_unless (GST_IS_RTSP_MEDIA (media));
255
256   g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
257
258   pipeline = gst_pipeline_new ("media-pipeline");
259   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
260
261   gst_rtsp_media_collect_streams (media);
262
263   srcpad = gst_element_get_static_pad (pay, "src");
264
265   g_signal_connect (srcpad, "notify::caps", (GCallback) on_notify_caps, pay);
266
267   pool = gst_rtsp_thread_pool_new ();
268
269   fail_unless (gst_rtsp_media_n_streams (media) == 0);
270
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);
277
278   fail_unless (gst_rtsp_media_n_streams (media) == 0);
279
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);
286
287   gst_object_unref (srcpad);
288   g_object_unref (media);
289   g_object_unref (pool);
290
291   gst_rtsp_thread_pool_cleanup ();
292 }
293
294 GST_END_TEST;
295
296 GST_START_TEST (test_media_prepare_port_alloc_fail)
297 {
298   GstRTSPMediaFactory *factory;
299   GstRTSPMedia *media;
300   GstRTSPUrl *url;
301   GstRTSPThreadPool *pool;
302   GstRTSPThread *thread;
303   GstRTSPAddressPool *addrpool;
304
305   pool = gst_rtsp_thread_pool_new ();
306
307   factory = gst_rtsp_media_factory_new ();
308   fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
309           &url) == GST_RTSP_OK);
310
311   gst_rtsp_media_factory_set_launch (factory,
312       "( fakesrc is-live=true ! text/plain ! rtpgstpay name=pay0 )");
313
314   media = gst_rtsp_media_factory_construct (factory, url);
315   fail_unless (GST_IS_RTSP_MEDIA (media));
316
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);
321
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));
325
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);
331 }
332
333 GST_END_TEST;
334
335 GST_START_TEST (test_media_take_pipeline)
336 {
337   GstRTSPMediaFactory *factory;
338   GstRTSPMedia *media;
339   GstRTSPUrl *url;
340   GstElement *pipeline;
341
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 )");
347
348   media = gst_rtsp_media_factory_construct (factory, url);
349   fail_unless (GST_IS_RTSP_MEDIA (media));
350
351   pipeline = gst_pipeline_new ("media-pipeline");
352   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
353
354   g_object_unref (media);
355   gst_rtsp_url_free (url);
356   g_object_unref (factory);
357 }
358
359 GST_END_TEST;
360
361 GST_START_TEST (test_media_reset)
362 {
363   GstRTSPMediaFactory *factory;
364   GstRTSPMedia *media;
365   GstRTSPUrl *url;
366   GstRTSPThreadPool *pool;
367   GstRTSPThread *thread;
368
369   pool = gst_rtsp_thread_pool_new ();
370
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);
374
375   gst_rtsp_media_factory_set_launch (factory,
376       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
377
378   media = gst_rtsp_media_factory_construct (factory, url);
379   fail_unless (GST_IS_RTSP_MEDIA (media));
380
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);
387
388   media = gst_rtsp_media_factory_construct (factory, url);
389   fail_unless (GST_IS_RTSP_MEDIA (media));
390
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);
398
399   gst_rtsp_url_free (url);
400   g_object_unref (factory);
401   g_object_unref (pool);
402
403   gst_rtsp_thread_pool_cleanup ();
404 }
405
406 GST_END_TEST;
407
408 static Suite *
409 rtspmedia_suite (void)
410 {
411   Suite *s = suite_create ("rtspmedia");
412   TCase *tc = tcase_create ("general");
413
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);
423
424   return s;
425 }
426
427 GST_CHECK_MAIN (rtspmedia);