1 /* GStreamer unit test for wasapi plugin
2 *
3 * Copyright (C) 2021 Jakub Janků <janku.jakub.jj@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst.h>
26 #include <gst/check/gstcheck.h>
27
28 typedef struct
29 {
30 GMainLoop *loop;
31 GstElement *pipeline;
32 guint n_buffers;
33 guint restart_count;
34 GstState reuse_state;
35 } SrcReuseTestData;
36
37 static gboolean
src_reuse_bus_handler(GstBus * bus,GstMessage * message,SrcReuseTestData * data)38 src_reuse_bus_handler (GstBus * bus, GstMessage * message,
39 SrcReuseTestData * data)
40 {
41 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
42 GST_ERROR ("Got error message from pipeline");
43 g_main_loop_quit (data->loop);
44 }
45
46 return TRUE;
47 }
48
49 static void
start_pipeline(SrcReuseTestData * data)50 start_pipeline (SrcReuseTestData * data)
51 {
52 GstStateChangeReturn ret;
53
54 GST_INFO ("Start pipeline");
55 ret = gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
56 fail_unless (ret != GST_STATE_CHANGE_FAILURE);
57 }
58
59 static gboolean
restart_pipeline(SrcReuseTestData * data)60 restart_pipeline (SrcReuseTestData * data)
61 {
62 data->restart_count++;
63 start_pipeline (data);
64
65 return G_SOURCE_REMOVE;
66 }
67
68 static gboolean
handle_handoff(SrcReuseTestData * data)69 handle_handoff (SrcReuseTestData * data)
70 {
71 data->n_buffers++;
72
73 /* Restart every 10 packets */
74 if (data->n_buffers > 10) {
75 GstStateChangeReturn ret;
76 data->n_buffers = 0;
77
78 ret = gst_element_set_state (data->pipeline, data->reuse_state);
79 fail_unless (ret != GST_STATE_CHANGE_FAILURE);
80
81 if (data->restart_count < 2) {
82 GST_INFO ("Restart pipeline, current restart count %d",
83 data->restart_count);
84 g_timeout_add_seconds (1, (GSourceFunc) restart_pipeline, data);
85 } else {
86 GST_INFO ("Finish test");
87 g_main_loop_quit (data->loop);
88 }
89 }
90
91 return G_SOURCE_REMOVE;
92 }
93
94 static void
on_sink_handoff(GstElement * element,GstBuffer * buffer,GstPad * pad,SrcReuseTestData * data)95 on_sink_handoff (GstElement * element, GstBuffer * buffer, GstPad * pad,
96 SrcReuseTestData * data)
97 {
98 g_idle_add ((GSourceFunc) handle_handoff, data);
99 }
100
101 static void
wasapisrc_reuse(GstState reuse_state)102 wasapisrc_reuse (GstState reuse_state)
103 {
104 GstBus *bus;
105 GstElement *sink;
106 SrcReuseTestData data;
107
108 memset (&data, 0, sizeof (SrcReuseTestData));
109
110 data.loop = g_main_loop_new (NULL, FALSE);
111
112 data.pipeline = gst_parse_launch ("wasapisrc provide-clock=false ! queue ! "
113 "fakesink name=sink async=false", NULL);
114 fail_unless (data.pipeline != NULL);
115 data.reuse_state = reuse_state;
116
117 sink = gst_bin_get_by_name (GST_BIN (data.pipeline), "sink");
118 fail_unless (sink);
119
120 g_object_set (G_OBJECT (sink), "signal-handoffs", TRUE, NULL);
121 g_signal_connect (sink, "handoff", G_CALLBACK (on_sink_handoff), &data);
122
123 bus = gst_element_get_bus (GST_ELEMENT (data.pipeline));
124 fail_unless (bus != NULL);
125
126 gst_bus_add_watch (bus, (GstBusFunc) src_reuse_bus_handler, &data);
127 start_pipeline (&data);
128 g_main_loop_run (data.loop);
129
130 fail_unless (data.restart_count == 2);
131
132 gst_element_set_start_time (data.pipeline, GST_STATE_NULL);
133 gst_bus_remove_watch (bus);
134 gst_object_unref (bus);
135
136 gst_object_unref (data.pipeline);
137 g_main_loop_unref (data.loop);
138 }
139
140 /* https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/issues/1110 */
GST_START_TEST(test_wasapisrc_reuse_null)141 GST_START_TEST (test_wasapisrc_reuse_null)
142 {
143 wasapisrc_reuse (GST_STATE_NULL);
144 }
145
146 GST_END_TEST;
147
GST_START_TEST(test_wasapisrc_reuse_ready)148 GST_START_TEST (test_wasapisrc_reuse_ready)
149 {
150 wasapisrc_reuse (GST_STATE_READY);
151 }
152
153 GST_END_TEST;
154
155 typedef struct
156 {
157 GMainLoop *loop;
158 GstElement *pipe;
159 guint rem_st_changes;
160 GstState reuse_state;
161 } SinkPlayReadyTData;
162
163 static gboolean
sink_reuse_bus_watch_cb(GstBus * bus,GstMessage * message,gpointer user_data)164 sink_reuse_bus_watch_cb (GstBus * bus, GstMessage * message, gpointer user_data)
165 {
166 fail_unless (message->type != GST_MESSAGE_ERROR);
167
168 return G_SOURCE_CONTINUE;
169 }
170
171 static gboolean
state_timer_cb(gpointer user_data)172 state_timer_cb (gpointer user_data)
173 {
174 SinkPlayReadyTData *tdata = user_data;
175 GstState nxt_st = tdata->rem_st_changes % 2 == 1 ?
176 tdata->reuse_state : GST_STATE_PLAYING;
177
178 ASSERT_SET_STATE (tdata->pipe, nxt_st, GST_STATE_CHANGE_SUCCESS);
179 tdata->rem_st_changes--;
180
181 if (tdata->rem_st_changes == 0) {
182 g_main_loop_quit (tdata->loop);
183 return G_SOURCE_REMOVE;
184 }
185 return G_SOURCE_CONTINUE;
186 }
187
188 /* Test that the wasapisink can survive the state change
189 * from PLAYING to READY and then back to PLAYING */
190 static void
wasapisink_reuse(GstState reuse_state)191 wasapisink_reuse (GstState reuse_state)
192 {
193 SinkPlayReadyTData tdata;
194 GstBus *bus;
195
196 tdata.pipe = gst_parse_launch ("audiotestsrc ! wasapisink async=false", NULL);
197 fail_unless (tdata.pipe != NULL);
198 bus = gst_element_get_bus (tdata.pipe);
199 fail_unless (bus != NULL);
200 gst_bus_add_watch (bus, sink_reuse_bus_watch_cb, NULL);
201
202 tdata.reuse_state = reuse_state;
203
204 ASSERT_SET_STATE (tdata.pipe, GST_STATE_PLAYING, GST_STATE_CHANGE_SUCCESS);
205 tdata.rem_st_changes = 3; /* -> READY -> PLAYING -> QUIT */
206 g_timeout_add_seconds (1, state_timer_cb, &tdata);
207
208 tdata.loop = g_main_loop_new (NULL, FALSE);
209 g_main_loop_run (tdata.loop);
210
211 g_main_loop_unref (tdata.loop);
212 gst_bus_remove_watch (bus);
213 gst_object_unref (bus);
214 gst_object_unref (tdata.pipe);
215 }
216
GST_START_TEST(test_wasapisink_reuse_null)217 GST_START_TEST (test_wasapisink_reuse_null)
218 {
219 wasapisink_reuse (GST_STATE_NULL);
220 }
221
222 GST_END_TEST;
223
GST_START_TEST(test_wasapisink_reuse_ready)224 GST_START_TEST (test_wasapisink_reuse_ready)
225 {
226 wasapisink_reuse (GST_STATE_READY);
227 }
228
229 GST_END_TEST;
230
231 static gboolean
check_wasapi_element(gboolean is_src)232 check_wasapi_element (gboolean is_src)
233 {
234 gboolean ret = TRUE;
235 GstElement *elem;
236 const gchar *elem_name;
237
238 if (is_src)
239 elem_name = "wasapisrc";
240 else
241 elem_name = "wasapisink";
242
243 elem = gst_element_factory_make (elem_name, NULL);
244 if (!elem) {
245 GST_INFO ("%s is not available", elem_name);
246 return FALSE;
247 }
248
249 /* GST_STATE_READY is meaning that camera is available */
250 if (gst_element_set_state (elem, GST_STATE_READY) != GST_STATE_CHANGE_SUCCESS) {
251 GST_INFO ("cannot open device");
252 ret = FALSE;
253 }
254
255 gst_element_set_state (elem, GST_STATE_NULL);
256 gst_object_unref (elem);
257
258 return ret;
259 }
260
261 static Suite *
wasapi_suite(void)262 wasapi_suite (void)
263 {
264 Suite *s = suite_create ("wasapi");
265 TCase *tc_basic = tcase_create ("general");
266 gboolean have_src = FALSE;
267 gboolean have_sink = FALSE;
268
269 suite_add_tcase (s, tc_basic);
270
271 have_src = check_wasapi_element (TRUE);
272 have_sink = check_wasapi_element (FALSE);
273
274 if (!have_src && !have_sink) {
275 GST_INFO ("Skipping tests, wasapisrc/wasapisink are unavailable");
276 } else {
277 if (have_src) {
278 tcase_add_test (tc_basic, test_wasapisrc_reuse_null);
279 tcase_add_test (tc_basic, test_wasapisrc_reuse_ready);
280 }
281
282 if (have_sink) {
283 tcase_add_test (tc_basic, test_wasapisink_reuse_null);
284 tcase_add_test (tc_basic, test_wasapisink_reuse_ready);
285 }
286 }
287
288 return s;
289 }
290
291 GST_CHECK_MAIN (wasapi)
292