2 * Copyright (C) 2013 Branko Subasic <branko.subasic@axis.com>
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.
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.
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.
20 #include <gst/check/gstcheck.h>
22 #include <rtsp-media-factory.h>
23 #include <rtsp-session-media.h>
25 #define TEST_PATH "rtsp://localhost:8554/test"
26 #define SETUP_URL1 TEST_PATH "/stream=0"
27 #define SETUP_URL2 TEST_PATH "/stream=1"
29 GST_START_TEST (test_setup_url)
31 GstRTSPMediaFactory *factory;
33 GstRTSPUrl *url, *setup_url;
34 GstRTSPStream *stream;
35 GstRTSPThreadPool *pool;
36 GstRTSPThread *thread;
37 GstRTSPSessionMedia *sm;
38 GstRTSPStreamTransport *trans;
41 gchar *url_str, *url_str2;
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);
47 gst_rtsp_media_factory_set_launch (factory,
48 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
50 media = gst_rtsp_media_factory_construct (factory, url);
51 fail_unless (GST_IS_RTSP_MEDIA (media));
53 fail_unless (gst_rtsp_media_n_streams (media) == 1);
55 stream = gst_rtsp_media_get_stream (media, 0);
56 fail_unless (GST_IS_RTSP_STREAM (stream));
58 pool = gst_rtsp_thread_pool_new ();
59 thread = gst_rtsp_thread_pool_get_thread (pool,
60 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
62 fail_unless (gst_rtsp_media_prepare (media, thread));
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);
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);
78 /* make sure there's no setup url stored initially */
79 fail_unless (gst_rtsp_stream_transport_get_url (trans) == NULL);
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);
85 url_str = gst_rtsp_url_get_request_uri (setup_url);
87 gst_rtsp_url_get_request_uri (gst_rtsp_stream_transport_get_url (trans));
88 fail_if (g_strcmp0 (url_str, url_str2) != 0);
92 /* check that it's ok to try to store the same url again */
93 gst_rtsp_stream_transport_set_url (trans, setup_url);
95 fail_unless (gst_rtsp_media_unprepare (media));
97 gst_rtsp_url_free (setup_url);
98 gst_rtsp_url_free (url);
102 g_object_unref (factory);
103 g_object_unref (pool);
108 GST_START_TEST (test_rtsp_state)
110 GstRTSPMediaFactory *factory;
113 GstRTSPStream *stream;
114 GstRTSPThreadPool *pool;
115 GstRTSPThread *thread;
116 GstRTSPSessionMedia *sm;
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);
122 gst_rtsp_media_factory_set_launch (factory,
123 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
125 media = gst_rtsp_media_factory_construct (factory, url);
126 fail_unless (GST_IS_RTSP_MEDIA (media));
128 fail_unless (gst_rtsp_media_n_streams (media) == 1);
130 stream = gst_rtsp_media_get_stream (media, 0);
131 fail_unless (GST_IS_RTSP_STREAM (stream));
133 pool = gst_rtsp_thread_pool_new ();
134 thread = gst_rtsp_thread_pool_get_thread (pool,
135 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
137 fail_unless (gst_rtsp_media_prepare (media, thread));
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);
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);
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);
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);
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);
160 fail_unless (gst_rtsp_media_unprepare (media));
162 gst_rtsp_url_free (url);
166 g_object_unref (factory);
167 g_object_unref (pool);
172 GST_START_TEST (test_transports)
174 GstRTSPMediaFactory *factory;
177 GstRTSPStream *stream1, *stream2;
178 GstRTSPThreadPool *pool;
179 GstRTSPThread *thread;
180 GstRTSPSessionMedia *sm;
181 GstRTSPStreamTransport *trans;
182 GstRTSPTransport *ct1, *ct2, *ct3, *ct4;
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);
189 gst_rtsp_media_factory_set_launch (factory,
190 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 audiotestsrc ! rtpgstpay pt=97 name=pay1 )");
192 media = gst_rtsp_media_factory_construct (factory, url);
193 fail_unless (GST_IS_RTSP_MEDIA (media));
195 fail_unless (gst_rtsp_media_n_streams (media) == 2);
197 stream1 = gst_rtsp_media_get_stream (media, 0);
198 fail_unless (GST_IS_RTSP_STREAM (stream1));
200 stream2 = gst_rtsp_media_get_stream (media, 1);
201 fail_unless (GST_IS_RTSP_STREAM (stream2));
203 pool = gst_rtsp_thread_pool_new ();
204 thread = gst_rtsp_thread_pool_get_thread (pool,
205 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
207 fail_unless (gst_rtsp_media_prepare (media, thread));
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));
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);
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);
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);
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);
230 fail_unless (gst_rtsp_media_unprepare (media));
232 gst_rtsp_url_free (url);
236 g_object_unref (factory);
237 g_object_unref (pool);
242 GST_START_TEST (test_time_and_rtpinfo)
244 GstRTSPMediaFactory *factory;
247 GstRTSPStream *stream1, *stream2;
248 GstRTSPThreadPool *pool;
249 GstRTSPThread *thread;
250 GstRTSPSessionMedia *sm;
251 GstClockTime base_time;
253 GstRTSPTransport *ct1;
254 GstRTSPStreamTransport *trans;
255 GstRTSPUrl *setup_url;
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);
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 )");
266 media = gst_rtsp_media_factory_construct (factory, url);
267 fail_unless (GST_IS_RTSP_MEDIA (media));
269 fail_unless (gst_rtsp_media_n_streams (media) == 2);
271 stream1 = gst_rtsp_media_get_stream (media, 0);
272 fail_unless (GST_IS_RTSP_STREAM (stream1));
274 stream2 = gst_rtsp_media_get_stream (media, 1);
275 fail_unless (GST_IS_RTSP_STREAM (stream2));
277 pool = gst_rtsp_thread_pool_new ();
278 thread = gst_rtsp_thread_pool_get_thread (pool,
279 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
281 fail_unless (gst_rtsp_media_prepare (media, thread));
283 sm = gst_rtsp_session_media_new (TEST_PATH, media);
284 fail_unless (GST_IS_RTSP_SESSION_MEDIA (sm));
286 base_time = gst_rtsp_session_media_get_base_time (sm);
287 fail_unless_equals_int64 (base_time, 0);
289 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sm);
290 fail_unless (rtpinfo == NULL);
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);
298 base_time = gst_rtsp_session_media_get_base_time (sm);
299 fail_unless_equals_int64 (base_time, 0);
301 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sm);
302 streaminfo = g_strsplit (rtpinfo, ",", 1);
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);
310 g_strfreev (streaminfo);
312 fail_unless (gst_rtsp_media_unprepare (media));
314 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sm);
315 fail_unless (rtpinfo == NULL);
317 gst_rtsp_url_free (setup_url);
318 gst_rtsp_url_free (url);
322 g_object_unref (factory);
323 g_object_unref (pool);
328 GST_START_TEST (test_allocate_channels)
330 GstRTSPMediaFactory *factory;
333 GstRTSPStream *stream;
334 GstRTSPThreadPool *pool;
335 GstRTSPThread *thread;
336 GstRTSPSessionMedia *sm;
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);
343 gst_rtsp_media_factory_set_launch (factory,
344 "( videotestsrc ! rtpvrawpay pt=96 name=pay0 )");
346 media = gst_rtsp_media_factory_construct (factory, url);
347 fail_unless (GST_IS_RTSP_MEDIA (media));
349 fail_unless (gst_rtsp_media_n_streams (media) == 1);
351 stream = gst_rtsp_media_get_stream (media, 0);
352 fail_unless (GST_IS_RTSP_STREAM (stream));
354 pool = gst_rtsp_thread_pool_new ();
355 thread = gst_rtsp_thread_pool_get_thread (pool,
356 GST_RTSP_THREAD_TYPE_MEDIA, NULL);
358 fail_unless (gst_rtsp_media_prepare (media, thread));
360 sm = gst_rtsp_session_media_new (TEST_PATH, media);
361 fail_unless (GST_IS_RTSP_SESSION_MEDIA (sm));
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);
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);
371 fail_unless (gst_rtsp_media_unprepare (media));
373 gst_rtsp_url_free (url);
377 g_object_unref (factory);
378 g_object_unref (pool);
383 rtspsessionmedia_suite (void)
385 Suite *s = suite_create ("rtspsessionmedia");
386 TCase *tc = tcase_create ("general");
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);
399 GST_CHECK_MAIN (rtspsessionmedia);