Initial release including wifi display based on gst-rtsp-server-1.4.1
[platform/upstream/gstreamer.git] / tests / check / gst / sessionmedia.c
1 /* GStreamer
2  * Copyright (C) 2013 Branko Subasic <branko.subasic@axis.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 #include <rtsp-session-media.h>
24
25 #define TEST_PATH "rtsp://localhost:8554/test"
26 #define SETUP_URL1 TEST_PATH "/stream=0"
27 #define SETUP_URL2 TEST_PATH "/stream=1"
28
29 GST_START_TEST (test_setup_url)
30 {
31   GstRTSPMediaFactory *factory;
32   GstRTSPMedia *media;
33   GstRTSPUrl *url, *setup_url;
34   GstRTSPStream *stream;
35   GstRTSPThreadPool *pool;
36   GstRTSPThread *thread;
37   GstRTSPSessionMedia *sm;
38   GstRTSPStreamTransport *trans;
39   GstRTSPTransport *ct;
40   gint match_len;
41   gchar *url_str, *url_str2;
42
43   factory = gst_rtsp_media_factory_new ();
44   fail_if (gst_rtsp_media_factory_is_shared (factory));
45   fail_unless (gst_rtsp_url_parse (TEST_PATH, &url) == GST_RTSP_OK);
46
47   gst_rtsp_media_factory_set_launch (factory,
48       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
49
50   media = gst_rtsp_media_factory_construct (factory, url);
51   fail_unless (GST_IS_RTSP_MEDIA (media));
52
53   fail_unless (gst_rtsp_media_n_streams (media) == 1);
54
55   stream = gst_rtsp_media_get_stream (media, 0);
56   fail_unless (GST_IS_RTSP_STREAM (stream));
57
58   pool = gst_rtsp_thread_pool_new ();
59   thread = gst_rtsp_thread_pool_get_thread (pool,
60       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
61
62   fail_unless (gst_rtsp_media_prepare (media, thread));
63
64   /* create session media and make sure it matches test path
65    * note that gst_rtsp_session_media_new takes ownership of the media
66    * thus no need to unref it at the bottom of function */
67   sm = gst_rtsp_session_media_new (TEST_PATH, media);
68   fail_unless (GST_IS_RTSP_SESSION_MEDIA (sm));
69   fail_unless (gst_rtsp_session_media_matches (sm, TEST_PATH, &match_len));
70   fail_unless (match_len == strlen (TEST_PATH));
71   fail_unless (gst_rtsp_session_media_get_media (sm) == media);
72
73   /* make a transport for the stream */
74   gst_rtsp_transport_new (&ct);
75   trans = gst_rtsp_session_media_set_transport (sm, stream, ct);
76   fail_unless (gst_rtsp_session_media_get_transport (sm, 0) == trans);
77
78   /* make sure there's no setup url stored initially */
79   fail_unless (gst_rtsp_stream_transport_get_url (trans) == NULL);
80
81   /* now store a setup url and make sure it can be retrieved and that it's correct */
82   fail_unless (gst_rtsp_url_parse (SETUP_URL1, &setup_url) == GST_RTSP_OK);
83   gst_rtsp_stream_transport_set_url (trans, setup_url);
84
85   url_str = gst_rtsp_url_get_request_uri (setup_url);
86   url_str2 =
87       gst_rtsp_url_get_request_uri (gst_rtsp_stream_transport_get_url (trans));
88   fail_if (g_strcmp0 (url_str, url_str2) != 0);
89   g_free (url_str);
90   g_free (url_str2);
91
92   /* check that it's ok to try to store the same url again */
93   gst_rtsp_stream_transport_set_url (trans, setup_url);
94
95   fail_unless (gst_rtsp_media_unprepare (media));
96
97   gst_rtsp_url_free (setup_url);
98   gst_rtsp_url_free (url);
99
100   g_object_unref (sm);
101
102   g_object_unref (factory);
103   g_object_unref (pool);
104 }
105
106 GST_END_TEST;
107
108 GST_START_TEST (test_rtsp_state)
109 {
110   GstRTSPMediaFactory *factory;
111   GstRTSPMedia *media;
112   GstRTSPUrl *url;
113   GstRTSPStream *stream;
114   GstRTSPThreadPool *pool;
115   GstRTSPThread *thread;
116   GstRTSPSessionMedia *sm;
117
118   factory = gst_rtsp_media_factory_new ();
119   fail_if (gst_rtsp_media_factory_is_shared (factory));
120   fail_unless (gst_rtsp_url_parse (TEST_PATH, &url) == GST_RTSP_OK);
121
122   gst_rtsp_media_factory_set_launch (factory,
123       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
124
125   media = gst_rtsp_media_factory_construct (factory, url);
126   fail_unless (GST_IS_RTSP_MEDIA (media));
127
128   fail_unless (gst_rtsp_media_n_streams (media) == 1);
129
130   stream = gst_rtsp_media_get_stream (media, 0);
131   fail_unless (GST_IS_RTSP_STREAM (stream));
132
133   pool = gst_rtsp_thread_pool_new ();
134   thread = gst_rtsp_thread_pool_get_thread (pool,
135       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
136
137   fail_unless (gst_rtsp_media_prepare (media, thread));
138
139   sm = gst_rtsp_session_media_new (TEST_PATH, media);
140   fail_unless (GST_IS_RTSP_SESSION_MEDIA (sm));
141   fail_unless_equals_int (gst_rtsp_session_media_get_rtsp_state (sm),
142       GST_RTSP_STATE_INIT);
143
144   gst_rtsp_session_media_set_rtsp_state (sm, GST_RTSP_STATE_READY);
145   fail_unless_equals_int (gst_rtsp_session_media_get_rtsp_state (sm),
146       GST_RTSP_STATE_READY);
147
148   gst_rtsp_session_media_set_rtsp_state (sm, GST_RTSP_STATE_SEEKING);
149   fail_unless_equals_int (gst_rtsp_session_media_get_rtsp_state (sm),
150       GST_RTSP_STATE_SEEKING);
151
152   gst_rtsp_session_media_set_rtsp_state (sm, GST_RTSP_STATE_PLAYING);
153   fail_unless_equals_int (gst_rtsp_session_media_get_rtsp_state (sm),
154       GST_RTSP_STATE_PLAYING);
155
156   gst_rtsp_session_media_set_rtsp_state (sm, GST_RTSP_STATE_RECORDING);
157   fail_unless_equals_int (gst_rtsp_session_media_get_rtsp_state (sm),
158       GST_RTSP_STATE_RECORDING);
159
160   fail_unless (gst_rtsp_media_unprepare (media));
161
162   gst_rtsp_url_free (url);
163
164   g_object_unref (sm);
165
166   g_object_unref (factory);
167   g_object_unref (pool);
168 }
169
170 GST_END_TEST;
171
172 GST_START_TEST (test_transports)
173 {
174   GstRTSPMediaFactory *factory;
175   GstRTSPMedia *media;
176   GstRTSPUrl *url;
177   GstRTSPStream *stream1, *stream2;
178   GstRTSPThreadPool *pool;
179   GstRTSPThread *thread;
180   GstRTSPSessionMedia *sm;
181   GstRTSPStreamTransport *trans;
182   GstRTSPTransport *ct1, *ct2, *ct3, *ct4;
183   gint match_len;
184
185   factory = gst_rtsp_media_factory_new ();
186   fail_if (gst_rtsp_media_factory_is_shared (factory));
187   fail_unless (gst_rtsp_url_parse (TEST_PATH, &url) == GST_RTSP_OK);
188
189   gst_rtsp_media_factory_set_launch (factory,
190       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 audiotestsrc ! rtpgstpay pt=97 name=pay1 )");
191
192   media = gst_rtsp_media_factory_construct (factory, url);
193   fail_unless (GST_IS_RTSP_MEDIA (media));
194
195   fail_unless (gst_rtsp_media_n_streams (media) == 2);
196
197   stream1 = gst_rtsp_media_get_stream (media, 0);
198   fail_unless (GST_IS_RTSP_STREAM (stream1));
199
200   stream2 = gst_rtsp_media_get_stream (media, 1);
201   fail_unless (GST_IS_RTSP_STREAM (stream2));
202
203   pool = gst_rtsp_thread_pool_new ();
204   thread = gst_rtsp_thread_pool_get_thread (pool,
205       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
206
207   fail_unless (gst_rtsp_media_prepare (media, thread));
208
209   sm = gst_rtsp_session_media_new (TEST_PATH, media);
210   fail_unless (GST_IS_RTSP_SESSION_MEDIA (sm));
211   fail_unless (gst_rtsp_session_media_matches (sm, TEST_PATH, &match_len));
212   fail_unless (match_len == strlen (TEST_PATH));
213
214   gst_rtsp_transport_new (&ct1);
215   trans = gst_rtsp_session_media_set_transport (sm, stream1, ct1);
216   fail_unless (gst_rtsp_session_media_get_transport (sm, 0) == trans);
217
218   gst_rtsp_transport_new (&ct2);
219   trans = gst_rtsp_session_media_set_transport (sm, stream1, ct2);
220   fail_unless (gst_rtsp_session_media_get_transport (sm, 0) == trans);
221
222   gst_rtsp_transport_new (&ct3);
223   trans = gst_rtsp_session_media_set_transport (sm, stream2, ct3);
224   fail_unless (gst_rtsp_session_media_get_transport (sm, 1) == trans);
225
226   gst_rtsp_transport_new (&ct4);
227   trans = gst_rtsp_session_media_set_transport (sm, stream2, ct4);
228   fail_unless (gst_rtsp_session_media_get_transport (sm, 1) == trans);
229
230   fail_unless (gst_rtsp_media_unprepare (media));
231
232   gst_rtsp_url_free (url);
233
234   g_object_unref (sm);
235
236   g_object_unref (factory);
237   g_object_unref (pool);
238 }
239
240 GST_END_TEST;
241
242 GST_START_TEST (test_time_and_rtpinfo)
243 {
244   GstRTSPMediaFactory *factory;
245   GstRTSPMedia *media;
246   GstRTSPUrl *url;
247   GstRTSPStream *stream1, *stream2;
248   GstRTSPThreadPool *pool;
249   GstRTSPThread *thread;
250   GstRTSPSessionMedia *sm;
251   GstClockTime base_time;
252   gchar *rtpinfo;
253   GstRTSPTransport *ct1;
254   GstRTSPStreamTransport *trans;
255   GstRTSPUrl *setup_url;
256   gchar **streaminfo;
257
258   factory = gst_rtsp_media_factory_new ();
259   fail_if (gst_rtsp_media_factory_is_shared (factory));
260   fail_unless (gst_rtsp_url_parse (TEST_PATH, &url) == GST_RTSP_OK);
261
262   gst_rtsp_media_factory_set_launch (factory,
263       "( videotestsrc do-timestamp=true timestamp-offset=0 ! rtpvrawpay pt=96 name=pay0 "
264       "audiotestsrc do-timestamp=true timestamp-offset=1000000000 ! rtpgstpay pt=97 name=pay1 )");
265
266   media = gst_rtsp_media_factory_construct (factory, url);
267   fail_unless (GST_IS_RTSP_MEDIA (media));
268
269   fail_unless (gst_rtsp_media_n_streams (media) == 2);
270
271   stream1 = gst_rtsp_media_get_stream (media, 0);
272   fail_unless (GST_IS_RTSP_STREAM (stream1));
273
274   stream2 = gst_rtsp_media_get_stream (media, 1);
275   fail_unless (GST_IS_RTSP_STREAM (stream2));
276
277   pool = gst_rtsp_thread_pool_new ();
278   thread = gst_rtsp_thread_pool_get_thread (pool,
279       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
280
281   fail_unless (gst_rtsp_media_prepare (media, thread));
282
283   sm = gst_rtsp_session_media_new (TEST_PATH, media);
284   fail_unless (GST_IS_RTSP_SESSION_MEDIA (sm));
285
286   base_time = gst_rtsp_session_media_get_base_time (sm);
287   fail_unless_equals_int64 (base_time, 0);
288
289   rtpinfo = gst_rtsp_session_media_get_rtpinfo (sm);
290   fail_unless (rtpinfo == NULL);
291
292   gst_rtsp_transport_new (&ct1);
293   trans = gst_rtsp_session_media_set_transport (sm, stream1, ct1);
294   fail_unless (gst_rtsp_session_media_get_transport (sm, 0) == trans);
295   fail_unless (gst_rtsp_url_parse (SETUP_URL1, &setup_url) == GST_RTSP_OK);
296   gst_rtsp_stream_transport_set_url (trans, setup_url);
297
298   base_time = gst_rtsp_session_media_get_base_time (sm);
299   fail_unless_equals_int64 (base_time, 0);
300
301   rtpinfo = gst_rtsp_session_media_get_rtpinfo (sm);
302   streaminfo = g_strsplit (rtpinfo, ",", 1);
303   g_free (rtpinfo);
304
305   fail_unless (g_strstr_len (streaminfo[0], -1, "url=") != NULL);
306   fail_unless (g_strstr_len (streaminfo[0], -1, "seq=") != NULL);
307   fail_unless (g_strstr_len (streaminfo[0], -1, "rtptime=") != NULL);
308   fail_unless (g_strstr_len (streaminfo[0], -1, SETUP_URL1) != NULL);
309
310   g_strfreev (streaminfo);
311
312   fail_unless (gst_rtsp_media_unprepare (media));
313
314   rtpinfo = gst_rtsp_session_media_get_rtpinfo (sm);
315   fail_unless (rtpinfo == NULL);
316
317   gst_rtsp_url_free (setup_url);
318   gst_rtsp_url_free (url);
319
320   g_object_unref (sm);
321
322   g_object_unref (factory);
323   g_object_unref (pool);
324 }
325
326 GST_END_TEST;
327
328 GST_START_TEST (test_allocate_channels)
329 {
330   GstRTSPMediaFactory *factory;
331   GstRTSPMedia *media;
332   GstRTSPUrl *url;
333   GstRTSPStream *stream;
334   GstRTSPThreadPool *pool;
335   GstRTSPThread *thread;
336   GstRTSPSessionMedia *sm;
337   GstRTSPRange range;
338
339   factory = gst_rtsp_media_factory_new ();
340   fail_if (gst_rtsp_media_factory_is_shared (factory));
341   fail_unless (gst_rtsp_url_parse (TEST_PATH, &url) == GST_RTSP_OK);
342
343   gst_rtsp_media_factory_set_launch (factory,
344       "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
345
346   media = gst_rtsp_media_factory_construct (factory, url);
347   fail_unless (GST_IS_RTSP_MEDIA (media));
348
349   fail_unless (gst_rtsp_media_n_streams (media) == 1);
350
351   stream = gst_rtsp_media_get_stream (media, 0);
352   fail_unless (GST_IS_RTSP_STREAM (stream));
353
354   pool = gst_rtsp_thread_pool_new ();
355   thread = gst_rtsp_thread_pool_get_thread (pool,
356       GST_RTSP_THREAD_TYPE_MEDIA, NULL);
357
358   fail_unless (gst_rtsp_media_prepare (media, thread));
359
360   sm = gst_rtsp_session_media_new (TEST_PATH, media);
361   fail_unless (GST_IS_RTSP_SESSION_MEDIA (sm));
362
363   fail_unless (gst_rtsp_session_media_alloc_channels (sm, &range));
364   fail_unless_equals_int (range.min, 0);
365   fail_unless_equals_int (range.max, 1);
366
367   fail_unless (gst_rtsp_session_media_alloc_channels (sm, &range));
368   fail_unless_equals_int (range.min, 2);
369   fail_unless_equals_int (range.max, 3);
370
371   fail_unless (gst_rtsp_media_unprepare (media));
372
373   gst_rtsp_url_free (url);
374
375   g_object_unref (sm);
376
377   g_object_unref (factory);
378   g_object_unref (pool);
379 }
380
381 GST_END_TEST;
382 static Suite *
383 rtspsessionmedia_suite (void)
384 {
385   Suite *s = suite_create ("rtspsessionmedia");
386   TCase *tc = tcase_create ("general");
387
388   suite_add_tcase (s, tc);
389   tcase_set_timeout (tc, 20);
390   tcase_add_test (tc, test_setup_url);
391   tcase_add_test (tc, test_rtsp_state);
392   tcase_add_test (tc, test_transports);
393   tcase_add_test (tc, test_time_and_rtpinfo);
394   tcase_add_test (tc, test_allocate_channels);
395
396   return s;
397 }
398
399 GST_CHECK_MAIN (rtspsessionmedia);