Initial release including wifi display based on gst-rtsp-server-1.4.1
[platform/upstream/gstreamer.git] / tests / check / gst / sessionpool.c
1 /* GStreamer
2  * Copyright (C) 2014 Sebastian Rasmussen <sebras@hotmail.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 #include <rtsp-session-pool.h>
22
23 typedef struct
24 {
25   GstRTSPSession *sessions[3];
26   GstRTSPFilterResult response[3];
27 } Responses;
28
29 static GstRTSPFilterResult
30 filter_func (GstRTSPSessionPool * pool, GstRTSPSession * session,
31     gpointer user_data)
32 {
33   Responses *responses = (Responses *) user_data;
34   gint i;
35
36   for (i = 0; i < 3; i++)
37     if (session == responses->sessions[i])
38       return responses->response[i];
39
40   return GST_RTSP_FILTER_KEEP;
41 }
42
43 GST_START_TEST (test_pool)
44 {
45   GstRTSPSessionPool *pool;
46   GstRTSPSession *session1, *session2, *session3;
47   GstRTSPSession *compare;
48   gchar *session1id, *session2id, *session3id;
49   GList *list;
50   guint maxsessions;
51   GSource *source;
52   guint sourceid;
53
54   pool = gst_rtsp_session_pool_new ();
55   fail_unless_equals_int (gst_rtsp_session_pool_get_n_sessions (pool), 0);
56   fail_unless_equals_int (gst_rtsp_session_pool_get_max_sessions (pool), 0);
57
58   gst_rtsp_session_pool_set_max_sessions (pool, 3);
59   fail_unless_equals_int (gst_rtsp_session_pool_get_max_sessions (pool), 3);
60
61   session1 = gst_rtsp_session_pool_create (pool);
62   fail_unless (GST_IS_RTSP_SESSION (session1));
63   fail_unless_equals_int (gst_rtsp_session_pool_get_n_sessions (pool), 1);
64   fail_unless_equals_int (gst_rtsp_session_pool_get_max_sessions (pool), 3);
65   session1id = g_strdup (gst_rtsp_session_get_sessionid (session1));
66
67   session2 = gst_rtsp_session_pool_create (pool);
68   fail_unless (GST_IS_RTSP_SESSION (session2));
69   fail_unless_equals_int (gst_rtsp_session_pool_get_n_sessions (pool), 2);
70   fail_unless_equals_int (gst_rtsp_session_pool_get_max_sessions (pool), 3);
71   session2id = g_strdup (gst_rtsp_session_get_sessionid (session2));
72
73   session3 = gst_rtsp_session_pool_create (pool);
74   fail_unless (GST_IS_RTSP_SESSION (session3));
75   fail_unless_equals_int (gst_rtsp_session_pool_get_n_sessions (pool), 3);
76   fail_unless_equals_int (gst_rtsp_session_pool_get_max_sessions (pool), 3);
77   session3id = g_strdup (gst_rtsp_session_get_sessionid (session3));
78
79   fail_if (GST_IS_RTSP_SESSION (gst_rtsp_session_pool_create (pool)));
80
81   compare = gst_rtsp_session_pool_find (pool, session1id);
82   fail_unless (compare == session1);
83   g_object_unref (compare);
84   compare = gst_rtsp_session_pool_find (pool, session2id);
85   fail_unless (compare == session2);
86   g_object_unref (compare);
87   compare = gst_rtsp_session_pool_find (pool, session3id);
88   fail_unless (compare == session3);
89   g_object_unref (compare);
90   fail_unless (gst_rtsp_session_pool_find (pool, "") == NULL);
91
92   fail_unless (gst_rtsp_session_pool_remove (pool, session2));
93   g_object_unref (session2);
94   fail_unless_equals_int (gst_rtsp_session_pool_get_n_sessions (pool), 2);
95   fail_unless_equals_int (gst_rtsp_session_pool_get_max_sessions (pool), 3);
96
97   gst_rtsp_session_pool_set_max_sessions (pool, 2);
98   fail_unless_equals_int (gst_rtsp_session_pool_get_n_sessions (pool), 2);
99   fail_unless_equals_int (gst_rtsp_session_pool_get_max_sessions (pool), 2);
100
101   session2 = gst_rtsp_session_pool_create (pool);
102   fail_if (GST_IS_RTSP_SESSION (session2));
103
104   {
105     list = gst_rtsp_session_pool_filter (pool, NULL, NULL);
106     fail_unless_equals_int (g_list_length (list), 2);
107     fail_unless (g_list_find (list, session1) != NULL);
108     fail_unless (g_list_find (list, session3) != NULL);
109     g_list_free_full (list, (GDestroyNotify) g_object_unref);
110   }
111
112   {
113     Responses responses = {
114       {session1, session2, session3},
115       {GST_RTSP_FILTER_KEEP, GST_RTSP_FILTER_KEEP, GST_RTSP_FILTER_KEEP},
116     };
117
118     list = gst_rtsp_session_pool_filter (pool, filter_func, &responses);
119     fail_unless (list == NULL);
120   }
121
122   {
123     Responses responses = {
124       {session1, session2, session3},
125       {GST_RTSP_FILTER_REF, GST_RTSP_FILTER_KEEP, GST_RTSP_FILTER_KEEP},
126     };
127
128     list = gst_rtsp_session_pool_filter (pool, filter_func, &responses);
129     fail_unless_equals_int (g_list_length (list), 1);
130     fail_unless (g_list_nth_data (list, 0) == session1);
131     g_list_free_full (list, (GDestroyNotify) g_object_unref);
132   }
133
134   {
135     Responses responses = {
136       {session1, session2, session3},
137       {GST_RTSP_FILTER_KEEP, GST_RTSP_FILTER_KEEP, GST_RTSP_FILTER_REMOVE},
138     };
139
140     list = gst_rtsp_session_pool_filter (pool, filter_func, &responses);
141     fail_unless_equals_int (g_list_length (list), 0);
142     g_list_free (list);
143   }
144
145   compare = gst_rtsp_session_pool_find (pool, session1id);
146   fail_unless (compare == session1);
147   g_object_unref (compare);
148   fail_unless (gst_rtsp_session_pool_find (pool, session2id) == NULL);
149   fail_unless (gst_rtsp_session_pool_find (pool, session3id) == NULL);
150
151   g_object_get (pool, "max-sessions", &maxsessions, NULL);
152   fail_unless_equals_int (maxsessions, 2);
153
154   g_object_set (pool, "max-sessions", 3, NULL);
155   g_object_get (pool, "max-sessions", &maxsessions, NULL);
156   fail_unless_equals_int (maxsessions, 3);
157
158   fail_unless_equals_int (gst_rtsp_session_pool_cleanup (pool), 0);
159
160   gst_rtsp_session_set_timeout (session1, 1);
161
162   source = gst_rtsp_session_pool_create_watch (pool);
163   fail_unless (source != NULL);
164
165   sourceid = g_source_attach (source, NULL);
166   fail_unless (sourceid != 0);
167
168   while (!g_main_context_iteration (NULL, TRUE));
169
170   g_source_unref (source);
171
172   g_object_unref (session1);
173   g_object_unref (session3);
174
175   g_free (session1id);
176   g_free (session2id);
177   g_free (session3id);
178
179   g_object_unref (pool);
180 }
181
182 GST_END_TEST;
183
184 static Suite *
185 rtspsessionpool_suite (void)
186 {
187   Suite *s = suite_create ("rtspsessionpool");
188   TCase *tc = tcase_create ("general");
189
190   suite_add_tcase (s, tc);
191   tcase_set_timeout (tc, 15);
192   tcase_add_test (tc, test_pool);
193
194   return s;
195 }
196
197 GST_CHECK_MAIN (rtspsessionpool);