Dynamically reconfigure pipeline in PLAY based on transports
[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_media_seek)
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   GstRTSPTransport *transport;
35
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);
40
41   gst_rtsp_media_factory_set_launch (factory,
42       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
43
44   media = gst_rtsp_media_factory_construct (factory, url);
45   fail_unless (GST_IS_RTSP_MEDIA (media));
46
47   fail_unless (gst_rtsp_media_n_streams (media) == 1);
48
49   stream = gst_rtsp_media_get_stream (media, 0);
50   fail_unless (stream != NULL);
51
52   pool = gst_rtsp_thread_pool_new ();
53   thread = gst_rtsp_thread_pool_get_thread (pool,
54       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
55
56   fail_unless (gst_rtsp_media_prepare (media, thread));
57
58   /* define transport */
59   fail_unless (gst_rtsp_transport_new (&transport) == GST_RTSP_OK);
60   transport->lower_transport = GST_RTSP_LOWER_TRANS_TCP;
61
62   fail_unless (gst_rtsp_stream_complete_stream (stream, transport));
63
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);
66
67   /* the media is seekable now */
68   fail_unless (gst_rtsp_media_seek (media, range));
69
70   str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
71   fail_unless (g_str_equal (str, "npt=5-"));
72
73   gst_rtsp_range_free (range);
74   g_free (str);
75
76   fail_unless (gst_rtsp_media_unprepare (media));
77   g_object_unref (media);
78
79   gst_rtsp_url_free (url);
80   g_object_unref (factory);
81
82   g_object_unref (pool);
83
84   gst_rtsp_thread_pool_cleanup ();
85 }
86
87 GST_END_TEST;
88
89
90 GST_START_TEST (test_media_seek_no_sinks)
91 {
92   GstRTSPMediaFactory *factory;
93   GstRTSPMedia *media;
94   GstRTSPUrl *url;
95   GstRTSPStream *stream;
96   GstRTSPTimeRange *range;
97   gchar *str;
98   GstRTSPThreadPool *pool;
99   GstRTSPThread *thread;
100
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);
105
106   gst_rtsp_media_factory_set_launch (factory,
107       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
108
109   media = gst_rtsp_media_factory_construct (factory, url);
110   fail_unless (GST_IS_RTSP_MEDIA (media));
111
112   fail_unless (gst_rtsp_media_n_streams (media) == 1);
113
114   stream = gst_rtsp_media_get_stream (media, 0);
115   fail_unless (stream != NULL);
116
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);
120
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));
124
125   pool = gst_rtsp_thread_pool_new ();
126   thread = gst_rtsp_thread_pool_get_thread (pool,
127       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
128
129   fail_unless (gst_rtsp_media_prepare (media, thread));
130
131   str = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
132   fail_unless (g_str_equal (str, "npt=0-"));
133   g_free (str);
134
135   str = gst_rtsp_media_get_range_string (media, TRUE, GST_RTSP_RANGE_NPT);
136   fail_unless (g_str_equal (str, "npt=0-"));
137   g_free (str);
138
139   /* fails, need to be prepared and contain sink elements */
140   fail_if (gst_rtsp_media_seek (media, range));
141
142   fail_unless (gst_rtsp_media_unprepare (media));
143
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));
148
149   gst_rtsp_range_free (range);
150   g_object_unref (media);
151
152   gst_rtsp_url_free (url);
153   g_object_unref (factory);
154
155   g_object_unref (pool);
156
157   gst_rtsp_thread_pool_cleanup ();
158 }
159
160 GST_END_TEST;
161
162 GST_START_TEST (test_media)
163 {
164   GstRTSPMedia *media;
165   GstElement *bin, *e1, *e2;
166
167   bin = gst_bin_new ("bin");
168   fail_if (bin == NULL);
169
170   e1 = gst_element_factory_make ("videotestsrc", NULL);
171   fail_if (e1 == NULL);
172
173   e2 = gst_element_factory_make ("rtpvrawpay", "pay0");
174   fail_if (e2 == NULL);
175   g_object_set (e2, "pt", 96, NULL);
176
177   gst_bin_add_many (GST_BIN_CAST (bin), e1, e2, NULL);
178   gst_element_link_many (e1, e2, NULL);
179
180   media = gst_rtsp_media_new (bin);
181   fail_unless (GST_IS_RTSP_MEDIA (media));
182   g_object_unref (media);
183 }
184
185 GST_END_TEST;
186
187 static void
188 test_prepare_reusable (const gchar * launch_line)
189 {
190   GstRTSPMediaFactory *factory;
191   GstRTSPMedia *media;
192   GstRTSPUrl *url;
193   GstRTSPThread *thread;
194   GstRTSPThreadPool *pool;
195
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);
200
201   gst_rtsp_media_factory_set_launch (factory, launch_line);
202
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);
206
207   g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
208
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);
215
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));
220
221   g_object_unref (media);
222   gst_rtsp_url_free (url);
223   g_object_unref (factory);
224 }
225
226 GST_START_TEST (test_media_reusable)
227 {
228
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 )");
233 }
234
235 GST_END_TEST;
236
237 GST_START_TEST (test_media_prepare)
238 {
239   GstRTSPMediaFactory *factory;
240   GstRTSPMedia *media;
241   GstRTSPUrl *url;
242   GstRTSPThreadPool *pool;
243   GstRTSPThread *thread;
244
245   pool = gst_rtsp_thread_pool_new ();
246
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);
252
253   gst_rtsp_media_factory_set_launch (factory,
254       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
255
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);
259
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);
265
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));
269
270   g_object_unref (media);
271   gst_rtsp_url_free (url);
272   g_object_unref (factory);
273
274   g_object_unref (pool);
275   gst_rtsp_thread_pool_cleanup ();
276 }
277
278 GST_END_TEST;
279
280 #define FLAG_HAVE_CAPS GST_ELEMENT_FLAG_LAST
281 static void
282 on_notify_caps (GstPad * pad, GParamSpec * pspec, GstElement * pay)
283 {
284   GstCaps *caps;
285
286   g_object_get (pad, "caps", &caps, NULL);
287
288   GST_DEBUG ("notify %" GST_PTR_FORMAT, caps);
289
290   if (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);
295     }
296     gst_caps_unref (caps);
297   } else {
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);
301     }
302   }
303 }
304
305 GST_START_TEST (test_media_dyn_prepare)
306 {
307   GstRTSPMedia *media;
308   GstElement *bin, *src, *pay;
309   GstElement *pipeline;
310   GstPad *srcpad;
311   GstRTSPThreadPool *pool;
312   GstRTSPThread *thread;
313
314   bin = gst_bin_new ("bin");
315   fail_if (bin == NULL);
316
317   src = gst_element_factory_make ("videotestsrc", NULL);
318   fail_if (src == NULL);
319
320   pay = gst_element_factory_make ("rtpvrawpay", "dynpay0");
321   fail_if (pay == NULL);
322   g_object_set (pay, "pt", 96, NULL);
323
324   gst_bin_add_many (GST_BIN_CAST (bin), src, pay, NULL);
325   gst_element_link_many (src, pay, NULL);
326
327   media = gst_rtsp_media_new (bin);
328   fail_unless (GST_IS_RTSP_MEDIA (media));
329
330   g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
331
332   pipeline = gst_pipeline_new ("media-pipeline");
333   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
334
335   gst_rtsp_media_collect_streams (media);
336
337   srcpad = gst_element_get_static_pad (pay, "src");
338
339   g_signal_connect (srcpad, "notify::caps", (GCallback) on_notify_caps, pay);
340
341   pool = gst_rtsp_thread_pool_new ();
342
343   fail_unless (gst_rtsp_media_n_streams (media) == 0);
344
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);
351
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);
358
359   gst_object_unref (srcpad);
360   g_object_unref (media);
361   g_object_unref (pool);
362
363   gst_rtsp_thread_pool_cleanup ();
364 }
365
366 GST_END_TEST;
367
368 GST_START_TEST (test_media_take_pipeline)
369 {
370   GstRTSPMediaFactory *factory;
371   GstRTSPMedia *media;
372   GstRTSPUrl *url;
373   GstElement *pipeline;
374
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 )");
380
381   media = gst_rtsp_media_factory_construct (factory, url);
382   fail_unless (GST_IS_RTSP_MEDIA (media));
383
384   pipeline = gst_pipeline_new ("media-pipeline");
385   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
386
387   g_object_unref (media);
388   gst_rtsp_url_free (url);
389   g_object_unref (factory);
390 }
391
392 GST_END_TEST;
393
394 GST_START_TEST (test_media_reset)
395 {
396   GstRTSPMediaFactory *factory;
397   GstRTSPMedia *media;
398   GstRTSPUrl *url;
399   GstRTSPThreadPool *pool;
400   GstRTSPThread *thread;
401
402   pool = gst_rtsp_thread_pool_new ();
403
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);
407
408   gst_rtsp_media_factory_set_launch (factory,
409       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
410
411   media = gst_rtsp_media_factory_construct (factory, url);
412   fail_unless (GST_IS_RTSP_MEDIA (media));
413
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);
420
421   media = gst_rtsp_media_factory_construct (factory, url);
422   fail_unless (GST_IS_RTSP_MEDIA (media));
423
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);
431
432   gst_rtsp_url_free (url);
433   g_object_unref (factory);
434   g_object_unref (pool);
435
436   gst_rtsp_thread_pool_cleanup ();
437 }
438
439 GST_END_TEST;
440
441 GST_START_TEST (test_media_multidyn_prepare)
442 {
443   GstRTSPMedia *media;
444   GstElement *bin, *src0, *pay0, *src1, *pay1;
445   GstElement *pipeline;
446   GstPad *srcpad0, *srcpad1;
447   GstRTSPThreadPool *pool;
448   GstRTSPThread *thread;
449
450   bin = gst_bin_new ("bin");
451   fail_if (bin == NULL);
452
453   src0 = gst_element_factory_make ("videotestsrc", NULL);
454   fail_if (src0 == NULL);
455
456   pay0 = gst_element_factory_make ("rtpvrawpay", "dynpay0");
457   fail_if (pay0 == NULL);
458   g_object_set (pay0, "pt", 96, NULL);
459
460   src1 = gst_element_factory_make ("videotestsrc", NULL);
461   fail_if (src1 == NULL);
462
463   pay1 = gst_element_factory_make ("rtpvrawpay", "dynpay1");
464   fail_if (pay1 == NULL);
465   g_object_set (pay1, "pt", 97, NULL);
466
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);
470
471   media = gst_rtsp_media_new (bin);
472   fail_unless (GST_IS_RTSP_MEDIA (media));
473
474   g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
475
476   pipeline = gst_pipeline_new ("media-pipeline");
477   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
478
479   gst_rtsp_media_collect_streams (media);
480
481   srcpad0 = gst_element_get_static_pad (pay0, "src");
482   srcpad1 = gst_element_get_static_pad (pay1, "src");
483
484   g_signal_connect (srcpad0, "notify::caps", (GCallback) on_notify_caps, pay0);
485   g_signal_connect (srcpad1, "notify::caps", (GCallback) on_notify_caps, pay1);
486
487   pool = gst_rtsp_thread_pool_new ();
488
489   fail_unless (gst_rtsp_media_n_streams (media) == 0);
490
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);
497
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);
504
505   gst_object_unref (srcpad0);
506   gst_object_unref (srcpad1);
507   g_object_unref (media);
508   g_object_unref (pool);
509
510   gst_rtsp_thread_pool_cleanup ();
511 }
512
513 GST_END_TEST;
514
515
516 static Suite *
517 rtspmedia_suite (void)
518 {
519   Suite *s = suite_create ("rtspmedia");
520   TCase *tc = tcase_create ("general");
521
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);
533
534   return s;
535 }
536
537 GST_CHECK_MAIN (rtspmedia);