media: add suspend modes
[platform/upstream/gst-rtsp-server.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
99 GST_END_TEST;
100
101 GST_START_TEST (test_media)
102 {
103   GstRTSPMedia *media;
104   GstElement *bin, *e1, *e2;
105
106   bin = gst_bin_new ("bin");
107   fail_if (bin == NULL);
108
109   e1 = gst_element_factory_make ("videotestsrc", NULL);
110   fail_if (e1 == NULL);
111
112   e2 = gst_element_factory_make ("rtpvrawpay", "pay0");
113   fail_if (e2 == NULL);
114   g_object_set (e2, "pt", 96, NULL);
115
116   gst_bin_add_many (GST_BIN_CAST (bin), e1, e2, NULL);
117   gst_element_link_many (e1, e2, NULL);
118
119   media = gst_rtsp_media_new (bin);
120   fail_unless (GST_IS_RTSP_MEDIA (media));
121   g_object_unref (media);
122 }
123
124 GST_END_TEST;
125
126 GST_START_TEST (test_media_prepare)
127 {
128   GstRTSPMediaFactory *factory;
129   GstRTSPMedia *media;
130   GstRTSPUrl *url;
131   GstRTSPThreadPool *pool;
132   GstRTSPThread *thread;
133
134   pool = gst_rtsp_thread_pool_new ();
135
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);
141
142   gst_rtsp_media_factory_set_launch (factory,
143       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
144
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);
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_if (gst_rtsp_media_prepare (media, thread));
158
159   g_object_unref (media);
160   gst_rtsp_url_free (url);
161   g_object_unref (factory);
162
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);
168
169   gst_rtsp_media_factory_set_launch (factory,
170       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
171
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);
175
176   g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
177
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);
183
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));
188
189   g_object_unref (media);
190   gst_rtsp_url_free (url);
191   g_object_unref (factory);
192 }
193
194 GST_END_TEST;
195
196 static void
197 on_notify_caps (GstPad * pad, GParamSpec * pspec, GstElement * pay)
198 {
199   GstCaps *caps;
200
201   g_object_get (pad, "caps", &caps, NULL);
202
203   GST_DEBUG ("notify %" GST_PTR_FORMAT, caps);
204
205   if (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);
209   } else {
210     g_signal_emit_by_name (pay, "pad-removed", pad);
211   }
212 }
213
214 GST_START_TEST (test_media_dyn_prepare)
215 {
216   GstRTSPMedia *media;
217   GstElement *bin, *src, *pay;
218   GstElement *pipeline;
219   GstPad *srcpad;
220   GstRTSPThreadPool *pool;
221   GstRTSPThread *thread;
222
223   bin = gst_bin_new ("bin");
224   fail_if (bin == NULL);
225
226   src = gst_element_factory_make ("videotestsrc", NULL);
227   fail_if (src == NULL);
228
229   pay = gst_element_factory_make ("rtpvrawpay", "dynpay0");
230   fail_if (pay == NULL);
231   g_object_set (pay, "pt", 96, NULL);
232
233   gst_bin_add_many (GST_BIN_CAST (bin), src, pay, NULL);
234   gst_element_link_many (src, pay, NULL);
235
236   media = gst_rtsp_media_new (bin);
237   fail_unless (GST_IS_RTSP_MEDIA (media));
238
239   g_object_set (G_OBJECT (media), "reusable", TRUE, NULL);
240
241   pipeline = gst_pipeline_new ("media-pipeline");
242   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
243
244   gst_rtsp_media_collect_streams (media);
245
246   srcpad = gst_element_get_static_pad (pay, "src");
247
248   g_signal_connect (srcpad, "notify::caps", (GCallback) on_notify_caps, pay);
249
250   pool = gst_rtsp_thread_pool_new ();
251
252   fail_unless (gst_rtsp_media_n_streams (media) == 0);
253
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);
260
261   fail_unless (gst_rtsp_media_n_streams (media) == 0);
262
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);
269
270   gst_object_unref (srcpad);
271   g_object_unref (media);
272   g_object_unref (pool);
273 }
274
275 GST_END_TEST;
276
277 GST_START_TEST (test_media_take_pipeline)
278 {
279   GstRTSPMediaFactory *factory;
280   GstRTSPMedia *media;
281   GstRTSPUrl *url;
282   GstElement *pipeline;
283
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 )");
289
290   media = gst_rtsp_media_factory_construct (factory, url);
291   fail_unless (GST_IS_RTSP_MEDIA (media));
292
293   pipeline = gst_pipeline_new ("media-pipeline");
294   gst_rtsp_media_take_pipeline (media, GST_PIPELINE_CAST (pipeline));
295
296   g_object_unref (media);
297   gst_rtsp_url_free (url);
298   g_object_unref (factory);
299 }
300
301 GST_END_TEST;
302
303 GST_START_TEST (test_media_reset)
304 {
305   GstRTSPMediaFactory *factory;
306   GstRTSPMedia *media;
307   GstRTSPUrl *url;
308   GstRTSPThreadPool *pool;
309   GstRTSPThread *thread;
310
311   pool = gst_rtsp_thread_pool_new ();
312
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);
316
317   gst_rtsp_media_factory_set_launch (factory,
318       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
319
320   media = gst_rtsp_media_factory_construct (factory, url);
321   fail_unless (GST_IS_RTSP_MEDIA (media));
322
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);
329
330   media = gst_rtsp_media_factory_construct (factory, url);
331   fail_unless (GST_IS_RTSP_MEDIA (media));
332
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);
340
341   gst_rtsp_url_free (url);
342   g_object_unref (factory);
343 }
344
345 GST_END_TEST;
346
347 static Suite *
348 rtspmedia_suite (void)
349 {
350   Suite *s = suite_create ("rtspmedia");
351   TCase *tc = tcase_create ("general");
352
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);
361
362   return s;
363 }
364
365 GST_CHECK_MAIN (rtspmedia);