e23fa3740b13e5318336fb7b3a317e452b360d5f
[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_stop (thread);
209   gst_rtsp_thread_pool_cleanup ();
210 }
211
212 GST_END_TEST;
213
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     g_signal_emit_by_name (pay, "pad-added", pad);
225     g_signal_emit_by_name (pay, "no-more-pads", NULL);
226     gst_caps_unref (caps);
227   } else {
228     g_signal_emit_by_name (pay, "pad-removed", pad);
229   }
230 }
231
232 GST_START_TEST (test_media_dyn_prepare)
233 {
234   GstRTSPMedia *media;
235   GstElement *bin, *src, *pay;
236   GstElement *pipeline;
237   GstPad *srcpad;
238   GstRTSPThreadPool *pool;
239   GstRTSPThread *thread;
240
241   bin = gst_bin_new ("bin");
242   fail_if (bin == NULL);
243
244   src = gst_element_factory_make ("videotestsrc", NULL);
245   fail_if (src == NULL);
246
247   pay = gst_element_factory_make ("rtpvrawpay", "dynpay0");
248   fail_if (pay == NULL);
249   g_object_set (pay, "pt", 96, NULL);
250
251   gst_bin_add_many (GST_BIN_CAST (bin), src, pay, NULL);
252   gst_element_link_many (src, pay, NULL);
253
254   media = gst_rtsp_media_new (bin);
255   fail_unless (GST_IS_RTSP_MEDIA (media));
256
257   g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
258
259   pipeline = gst_pipeline_new ("media-pipeline");
260   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
261
262   gst_rtsp_media_collect_streams (media);
263
264   srcpad = gst_element_get_static_pad (pay, "src");
265
266   g_signal_connect (srcpad, "notify::caps", (GCallback) on_notify_caps, pay);
267
268   pool = gst_rtsp_thread_pool_new ();
269
270   fail_unless (gst_rtsp_media_n_streams (media) == 0);
271
272   thread = gst_rtsp_thread_pool_get_thread (pool,
273       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
274   fail_unless (gst_rtsp_media_prepare (media, thread));
275   fail_unless (gst_rtsp_media_n_streams (media) == 1);
276   fail_unless (gst_rtsp_media_unprepare (media));
277   fail_unless (gst_rtsp_media_n_streams (media) == 0);
278
279   fail_unless (gst_rtsp_media_n_streams (media) == 0);
280
281   thread = gst_rtsp_thread_pool_get_thread (pool,
282       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
283   fail_unless (gst_rtsp_media_prepare (media, thread));
284   fail_unless (gst_rtsp_media_n_streams (media) == 1);
285   fail_unless (gst_rtsp_media_unprepare (media));
286   fail_unless (gst_rtsp_media_n_streams (media) == 0);
287
288   gst_object_unref (srcpad);
289   g_object_unref (media);
290   g_object_unref (pool);
291
292   gst_rtsp_thread_pool_cleanup ();
293 }
294
295 GST_END_TEST;
296
297 GST_START_TEST (test_media_take_pipeline)
298 {
299   GstRTSPMediaFactory *factory;
300   GstRTSPMedia *media;
301   GstRTSPUrl *url;
302   GstElement *pipeline;
303
304   factory = gst_rtsp_media_factory_new ();
305   fail_unless (gst_rtsp_url_parse ("rtsp://localhost:8554/test",
306           &url) == GST_RTSP_OK);
307   gst_rtsp_media_factory_set_launch (factory,
308       "( fakesrc ! text/plain ! rtpgstpay name=pay0 )");
309
310   media = gst_rtsp_media_factory_construct (factory, url);
311   fail_unless (GST_IS_RTSP_MEDIA (media));
312
313   pipeline = gst_pipeline_new ("media-pipeline");
314   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
315
316   g_object_unref (media);
317   gst_rtsp_url_free (url);
318   g_object_unref (factory);
319 }
320
321 GST_END_TEST;
322
323 GST_START_TEST (test_media_reset)
324 {
325   GstRTSPMediaFactory *factory;
326   GstRTSPMedia *media;
327   GstRTSPUrl *url;
328   GstRTSPThreadPool *pool;
329   GstRTSPThread *thread;
330
331   pool = gst_rtsp_thread_pool_new ();
332
333   factory = gst_rtsp_media_factory_new ();
334   fail_if (gst_rtsp_media_factory_is_shared (factory));
335   gst_rtsp_url_parse ("rtsp://localhost:8554/test", &url);
336
337   gst_rtsp_media_factory_set_launch (factory,
338       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
339
340   media = gst_rtsp_media_factory_construct (factory, url);
341   fail_unless (GST_IS_RTSP_MEDIA (media));
342
343   thread = gst_rtsp_thread_pool_get_thread (pool,
344       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
345   fail_unless (gst_rtsp_media_prepare (media, thread));
346   fail_unless (gst_rtsp_media_suspend (media));
347   fail_unless (gst_rtsp_media_unprepare (media));
348   g_object_unref (media);
349
350   media = gst_rtsp_media_factory_construct (factory, url);
351   fail_unless (GST_IS_RTSP_MEDIA (media));
352
353   thread = gst_rtsp_thread_pool_get_thread (pool,
354       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
355   gst_rtsp_media_set_suspend_mode (media, GST_RTSP_SUSPEND_MODE_RESET);
356   fail_unless (gst_rtsp_media_prepare (media, thread));
357   fail_unless (gst_rtsp_media_suspend (media));
358   fail_unless (gst_rtsp_media_unprepare (media));
359   g_object_unref (media);
360
361   gst_rtsp_url_free (url);
362   g_object_unref (factory);
363   g_object_unref (pool);
364
365   gst_rtsp_thread_pool_cleanup ();
366 }
367
368 GST_END_TEST;
369
370 static Suite *
371 rtspmedia_suite (void)
372 {
373   Suite *s = suite_create ("rtspmedia");
374   TCase *tc = tcase_create ("general");
375
376   suite_add_tcase (s, tc);
377   tcase_set_timeout (tc, 20);
378   tcase_add_test (tc, test_launch);
379   tcase_add_test (tc, test_media);
380   tcase_add_test (tc, test_media_prepare);
381   tcase_add_test (tc, test_media_dyn_prepare);
382   tcase_add_test (tc, test_media_take_pipeline);
383   tcase_add_test (tc, test_media_reset);
384
385   return s;
386 }
387
388 GST_CHECK_MAIN (rtspmedia);