media-test: Test for multiple dynamic payload
[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 #define FLAG_HAVE_CAPS GST_ELEMENT_FLAG_LAST
214 static void
215 on_notify_caps (GstPad * pad, GParamSpec * pspec, GstElement * pay)
216 {
217   GstCaps *caps;
218
219   g_object_get (pad, "caps", &caps, NULL);
220
221   GST_DEBUG ("notify %" GST_PTR_FORMAT, caps);
222
223   if (caps) {
224     if (!GST_OBJECT_FLAG_IS_SET (pay, FLAG_HAVE_CAPS)) {
225       g_signal_emit_by_name (pay, "pad-added", pad);
226       g_signal_emit_by_name (pay, "no-more-pads", NULL);
227       GST_OBJECT_FLAG_SET (pay, FLAG_HAVE_CAPS);
228     }
229     gst_caps_unref (caps);
230   } else {
231     if (GST_OBJECT_FLAG_IS_SET (pay, FLAG_HAVE_CAPS)) {
232       g_signal_emit_by_name (pay, "pad-removed", pad);
233       GST_OBJECT_FLAG_UNSET (pay, FLAG_HAVE_CAPS);
234     }
235   }
236 }
237
238 GST_START_TEST (test_media_dyn_prepare)
239 {
240   GstRTSPMedia *media;
241   GstElement *bin, *src, *pay;
242   GstElement *pipeline;
243   GstPad *srcpad;
244   GstRTSPThreadPool *pool;
245   GstRTSPThread *thread;
246
247   bin = gst_bin_new ("bin");
248   fail_if (bin == NULL);
249
250   src = gst_element_factory_make ("videotestsrc", NULL);
251   fail_if (src == NULL);
252
253   pay = gst_element_factory_make ("rtpvrawpay", "dynpay0");
254   fail_if (pay == NULL);
255   g_object_set (pay, "pt", 96, NULL);
256
257   gst_bin_add_many (GST_BIN_CAST (bin), src, pay, NULL);
258   gst_element_link_many (src, pay, NULL);
259
260   media = gst_rtsp_media_new (bin);
261   fail_unless (GST_IS_RTSP_MEDIA (media));
262
263   g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
264
265   pipeline = gst_pipeline_new ("media-pipeline");
266   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
267
268   gst_rtsp_media_collect_streams (media);
269
270   srcpad = gst_element_get_static_pad (pay, "src");
271
272   g_signal_connect (srcpad, "notify::caps", (GCallback) on_notify_caps, pay);
273
274   pool = gst_rtsp_thread_pool_new ();
275
276   fail_unless (gst_rtsp_media_n_streams (media) == 0);
277
278   thread = gst_rtsp_thread_pool_get_thread (pool,
279       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
280   fail_unless (gst_rtsp_media_prepare (media, thread));
281   fail_unless (gst_rtsp_media_n_streams (media) == 1);
282   fail_unless (gst_rtsp_media_unprepare (media));
283   fail_unless (gst_rtsp_media_n_streams (media) == 0);
284
285   fail_unless (gst_rtsp_media_n_streams (media) == 0);
286
287   thread = gst_rtsp_thread_pool_get_thread (pool,
288       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
289   fail_unless (gst_rtsp_media_prepare (media, thread));
290   fail_unless (gst_rtsp_media_n_streams (media) == 1);
291   fail_unless (gst_rtsp_media_unprepare (media));
292   fail_unless (gst_rtsp_media_n_streams (media) == 0);
293
294   gst_object_unref (srcpad);
295   g_object_unref (media);
296   g_object_unref (pool);
297
298   gst_rtsp_thread_pool_cleanup ();
299 }
300
301 GST_END_TEST;
302
303 GST_START_TEST (test_media_prepare_port_alloc_fail)
304 {
305   GstRTSPMediaFactory *factory;
306   GstRTSPMedia *media;
307   GstRTSPUrl *url;
308   GstRTSPThreadPool *pool;
309   GstRTSPThread *thread;
310   GstRTSPAddressPool *addrpool;
311
312   pool = gst_rtsp_thread_pool_new ();
313
314   factory = gst_rtsp_media_factory_new ();
315   fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
316           &url) == GST_RTSP_OK);
317
318   gst_rtsp_media_factory_set_launch (factory,
319       "( fakesrc is-live=true ! text/plain ! rtpgstpay name=pay0 )");
320
321   media = gst_rtsp_media_factory_construct (factory, url);
322   fail_unless (GST_IS_RTSP_MEDIA (media));
323
324   addrpool = gst_rtsp_address_pool_new ();
325   fail_unless (gst_rtsp_address_pool_add_range (addrpool, "192.168.1.1",
326           "192.168.1.1", 6000, 6001, 0));
327   gst_rtsp_media_set_address_pool (media, addrpool);
328
329   thread = gst_rtsp_thread_pool_get_thread (pool,
330       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
331   fail_if (gst_rtsp_media_prepare (media, thread));
332
333   g_object_unref (media);
334   g_object_unref (addrpool);
335   gst_rtsp_url_free (url);
336   g_object_unref (factory);
337   g_object_unref (pool);
338 }
339
340 GST_END_TEST;
341
342 GST_START_TEST (test_media_take_pipeline)
343 {
344   GstRTSPMediaFactory *factory;
345   GstRTSPMedia *media;
346   GstRTSPUrl *url;
347   GstElement *pipeline;
348
349   factory = gst_rtsp_media_factory_new ();
350   fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
351           &url) == GST_RTSP_OK);
352   gst_rtsp_media_factory_set_launch (factory,
353       "( fakesrc ! text/plain ! rtpgstpay name=pay0 )");
354
355   media = gst_rtsp_media_factory_construct (factory, url);
356   fail_unless (GST_IS_RTSP_MEDIA (media));
357
358   pipeline = gst_pipeline_new ("media-pipeline");
359   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
360
361   g_object_unref (media);
362   gst_rtsp_url_free (url);
363   g_object_unref (factory);
364 }
365
366 GST_END_TEST;
367
368 GST_START_TEST (test_media_reset)
369 {
370   GstRTSPMediaFactory *factory;
371   GstRTSPMedia *media;
372   GstRTSPUrl *url;
373   GstRTSPThreadPool *pool;
374   GstRTSPThread *thread;
375
376   pool = gst_rtsp_thread_pool_new ();
377
378   factory = gst_rtsp_media_factory_new ();
379   fail_if (gst_rtsp_media_factory_is_shared (factory));
380   gst_rtsp_url_parse ("rtsp://localhost:8554/test", &url);
381
382   gst_rtsp_media_factory_set_launch (factory,
383       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
384
385   media = gst_rtsp_media_factory_construct (factory, url);
386   fail_unless (GST_IS_RTSP_MEDIA (media));
387
388   thread = gst_rtsp_thread_pool_get_thread (pool,
389       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
390   fail_unless (gst_rtsp_media_prepare (media, thread));
391   fail_unless (gst_rtsp_media_suspend (media));
392   fail_unless (gst_rtsp_media_unprepare (media));
393   g_object_unref (media);
394
395   media = gst_rtsp_media_factory_construct (factory, url);
396   fail_unless (GST_IS_RTSP_MEDIA (media));
397
398   thread = gst_rtsp_thread_pool_get_thread (pool,
399       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
400   gst_rtsp_media_set_suspend_mode (media, GST_RTSP_SUSPEND_MODE_RESET);
401   fail_unless (gst_rtsp_media_prepare (media, thread));
402   fail_unless (gst_rtsp_media_suspend (media));
403   fail_unless (gst_rtsp_media_unprepare (media));
404   g_object_unref (media);
405
406   gst_rtsp_url_free (url);
407   g_object_unref (factory);
408   g_object_unref (pool);
409
410   gst_rtsp_thread_pool_cleanup ();
411 }
412
413 GST_END_TEST;
414
415 GST_START_TEST (test_media_multidyn_prepare)
416 {
417   GstRTSPMedia *media;
418   GstElement *bin, *src0, *pay0, *src1, *pay1;
419   GstElement *pipeline;
420   GstPad *srcpad0, *srcpad1;
421   GstRTSPThreadPool *pool;
422   GstRTSPThread *thread;
423
424   bin = gst_bin_new ("bin");
425   fail_if (bin == NULL);
426
427   src0 = gst_element_factory_make ("videotestsrc", NULL);
428   fail_if (src0 == NULL);
429
430   pay0 = gst_element_factory_make ("rtpvrawpay", "dynpay0");
431   fail_if (pay0 == NULL);
432   g_object_set (pay0, "pt", 96, NULL);
433
434   src1 = gst_element_factory_make ("videotestsrc", NULL);
435   fail_if (src1 == NULL);
436
437   pay1 = gst_element_factory_make ("rtpvrawpay", "dynpay1");
438   fail_if (pay1 == NULL);
439   g_object_set (pay1, "pt", 97, NULL);
440
441   gst_bin_add_many (GST_BIN_CAST (bin), src0, pay0, src1, pay1, NULL);
442   gst_element_link_many (src0, pay0, NULL);
443   gst_element_link_many (src1, pay1, NULL);
444
445   media = gst_rtsp_media_new (bin);
446   fail_unless (GST_IS_RTSP_MEDIA (media));
447
448   g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
449
450   pipeline = gst_pipeline_new ("media-pipeline");
451   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
452
453   gst_rtsp_media_collect_streams (media);
454
455   srcpad0 = gst_element_get_static_pad (pay0, "src");
456   srcpad1 = gst_element_get_static_pad (pay1, "src");
457
458   g_signal_connect (srcpad0, "notify::caps", (GCallback) on_notify_caps, pay0);
459   g_signal_connect (srcpad1, "notify::caps", (GCallback) on_notify_caps, pay1);
460
461   pool = gst_rtsp_thread_pool_new ();
462
463   fail_unless (gst_rtsp_media_n_streams (media) == 0);
464
465   thread = gst_rtsp_thread_pool_get_thread (pool,
466       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
467   fail_unless (gst_rtsp_media_prepare (media, thread));
468   fail_unless (gst_rtsp_media_n_streams (media) == 2);
469   fail_unless (gst_rtsp_media_unprepare (media));
470   fail_unless (gst_rtsp_media_n_streams (media) == 0);
471
472   fail_unless (gst_rtsp_media_n_streams (media) == 0);
473
474   thread = gst_rtsp_thread_pool_get_thread (pool,
475       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
476   fail_unless (gst_rtsp_media_prepare (media, thread));
477   fail_unless (gst_rtsp_media_n_streams (media) == 2);
478   fail_unless (gst_rtsp_media_unprepare (media));
479   fail_unless (gst_rtsp_media_n_streams (media) == 0);
480
481   gst_object_unref (srcpad0);
482   gst_object_unref (srcpad1);
483   g_object_unref (media);
484   g_object_unref (pool);
485
486   gst_rtsp_thread_pool_cleanup ();
487 }
488
489 GST_END_TEST;
490
491
492 static Suite *
493 rtspmedia_suite (void)
494 {
495   Suite *s = suite_create ("rtspmedia");
496   TCase *tc = tcase_create ("general");
497
498   suite_add_tcase (s, tc);
499   tcase_set_timeout (tc, 20);
500   tcase_add_test (tc, test_launch);
501   tcase_add_test (tc, test_media);
502   tcase_add_test (tc, test_media_prepare);
503   tcase_add_test (tc, test_media_dyn_prepare);
504   tcase_add_test (tc, test_media_prepare_port_alloc_fail);
505   tcase_add_test (tc, test_media_take_pipeline);
506   tcase_add_test (tc, test_media_reset);
507   tcase_add_test (tc, test_media_multidyn_prepare);
508
509   return s;
510 }
511
512 GST_CHECK_MAIN (rtspmedia);