2 * Tests and examples of GstHarness
4 * Copyright (C) 2015 Havard Graff <havard@pexip.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
25 #include <gst/check/gstcheck.h>
26 #include <gst/check/gstharness.h>
29 create_destroy_element_harness (gpointer data, gpointer user_data)
31 GstElement *element = user_data;
32 GstHarness *h = gst_harness_new_with_element (element, NULL, NULL);
33 gst_harness_teardown (h);
36 GST_START_TEST (test_harness_element_ref)
38 GstHarness *h = gst_harness_new ("identity");
39 GstHarnessThread *threads[100];
42 for (i = 0; i < G_N_ELEMENTS (threads); i++)
43 threads[i] = gst_harness_stress_custom_start (h, NULL,
44 create_destroy_element_harness, h->element, 0);
45 for (i = 0; i < G_N_ELEMENTS (threads); i++)
46 gst_harness_stress_thread_stop (threads[i]);
48 fail_unless_equals_int (G_OBJECT (h->element)->ref_count, 1);
50 gst_harness_teardown (h);
56 GST_START_TEST (test_src_harness)
58 GstHarness *h = gst_harness_new ("identity");
60 /* add a fakesrc that syncs to the clock and a
61 capsfilter that adds some caps to it */
62 gst_harness_add_src_parse (h,
63 "fakesrc sync=1 ! capsfilter caps=\"mycaps\"", TRUE);
65 /* this cranks the clock and transfers the resulting buffer
66 from the src-harness into the identity element */
67 gst_harness_push_from_src (h);
69 /* verify that identity outputs a buffer by pulling and unreffing */
70 gst_buffer_unref (gst_harness_pull (h));
72 gst_harness_teardown (h);
77 GST_START_TEST (test_src_harness_no_forwarding)
79 GstHarness *h = gst_harness_new ("identity");
81 /* turn of forwarding of necessary events */
82 gst_harness_set_forwarding (h, FALSE);
84 /* add a fakesrc that syncs to the clock and a
85 capsfilter that adds some caps to it */
86 gst_harness_add_src_parse (h,
87 "fakesrc sync=1 ! capsfilter caps=\"mycaps\"", TRUE);
89 /* start the fakesrc to produce the first events */
90 gst_harness_play (h->src_harness);
92 /* transfer STREAM_START event */
93 gst_harness_src_push_event (h);
95 /* crank the clock to produce the CAPS and SEGMENT events */
96 gst_harness_crank_single_clock_wait (h->src_harness);
98 /* transfer CAPS event */
99 gst_harness_src_push_event (h);
101 /* transfer SEGMENT event */
102 gst_harness_src_push_event (h);
104 /* now transfer the buffer produced by exploiting
105 the ability to say 0 cranks but 1 push */
106 gst_harness_src_crank_and_push_many (h, 0, 1);
108 /* and verify that the identity element outputs it */
109 gst_buffer_unref (gst_harness_pull (h));
111 gst_harness_teardown (h);
116 GST_START_TEST (test_add_sink_harness_without_sinkpad)
118 GstHarness *h = gst_harness_new ("fakesink");
120 gst_harness_add_sink (h, "fakesink");
122 gst_harness_teardown (h);
128 create_new_stream_start_event (GstHarness * h, gpointer data)
130 guint *counter = data;
131 gchar *stream_id = g_strdup_printf ("streamid/%d", *counter);
132 GstEvent *event = gst_event_new_stream_start (stream_id);
139 push_query (gpointer data, gpointer user_data)
141 GstHarness *h = user_data;
142 GstCaps *caps = gst_caps_new_empty_simple ("mycaps");
143 GstQuery *query = gst_query_new_allocation (caps, FALSE);
144 gst_caps_unref (caps);
145 gst_pad_peer_query (h->srcpad, query);
146 gst_query_unref (query);
149 GST_START_TEST (test_forward_event_and_query_to_sink_harness_while_teardown)
151 GstHarness *h = gst_harness_new ("identity");
153 GstHarnessThread *e_thread = gst_harness_stress_push_event_with_cb_start (h,
154 create_new_stream_start_event, &counter, NULL);
155 GstHarnessThread *q_thread = gst_harness_stress_custom_start (h, NULL,
157 gdouble duration = 1.0;
158 GTimer *timer = g_timer_new ();
160 while (g_timer_elapsed (timer, NULL) < duration) {
161 gst_harness_add_sink (h, "fakesink");
165 g_timer_destroy (timer);
166 gst_harness_stress_thread_stop (q_thread);
167 gst_harness_stress_thread_stop (e_thread);
168 gst_harness_teardown (h);
174 harness_new_and_fill_with_data (void)
176 GstHarness *h = gst_harness_new_parse ("fakesrc num-buffers=5 "
177 "filltype=pattern-span sizetype=fixed sizemin=10 sizemax=10");
178 gboolean have_eos = FALSE;
180 gst_harness_play (h);
183 GstEvent *e = gst_harness_pull_event (h);
184 have_eos = GST_EVENT_TYPE (e) == GST_EVENT_EOS;
191 GST_START_TEST (test_get_all_data)
202 for (i = 0; i < G_N_ELEMENTS (expected); ++i)
205 h = harness_new_and_fill_with_data ();
206 buf = gst_harness_take_all_data_as_buffer (h);
207 fail_unless (buf != NULL);
208 fail_unless_equals_int (gst_buffer_get_size (buf), 5 * 10);
209 fail_unless (gst_buffer_memcmp (buf, 0, expected, 5 * 10) == 0);
210 gst_buffer_unref (buf);
211 /* There should be nothing left now. We should still get a non-NULL buffer */
212 buf = gst_harness_take_all_data_as_buffer (h);
213 fail_unless (buf != NULL);
214 fail_unless_equals_int (gst_buffer_get_size (buf), 0);
215 gst_buffer_unref (buf);
216 gst_harness_teardown (h);
218 h = harness_new_and_fill_with_data ();
219 bytes = gst_harness_take_all_data_as_bytes (h);
220 fail_unless (bytes != NULL);
221 cdata = g_bytes_get_data (bytes, &size);
222 fail_unless_equals_int (size, 5 * 10);
223 fail_unless (memcmp (cdata, expected, 50) == 0);
224 g_bytes_unref (bytes);
225 /* There should be nothing left now. We should still get a non-NULL bytes */
226 bytes = gst_harness_take_all_data_as_bytes (h);
227 fail_unless (bytes != NULL);
228 cdata = g_bytes_get_data (bytes, &size);
229 fail_unless (cdata == NULL);
230 fail_unless_equals_int (size, 0);
231 g_bytes_unref (bytes);
232 gst_harness_teardown (h);
234 h = harness_new_and_fill_with_data ();
235 data = gst_harness_take_all_data (h, &size);
236 fail_unless (data != NULL);
237 fail_unless_equals_int (size, 5 * 10);
238 fail_unless (memcmp (data, expected, 50) == 0);
240 /* There should be nothing left now. */
241 data = gst_harness_take_all_data (h, &size);
242 fail_unless (data == NULL);
243 fail_unless_equals_int (size, 0);
244 gst_harness_teardown (h);
250 gst_harness_suite (void)
252 Suite *s = suite_create ("GstHarness");
253 TCase *tc_chain = tcase_create ("harness");
255 suite_add_tcase (s, tc_chain);
257 tcase_add_test (tc_chain, test_harness_element_ref);
258 tcase_add_test (tc_chain, test_src_harness);
259 tcase_add_test (tc_chain, test_src_harness_no_forwarding);
260 tcase_add_test (tc_chain, test_add_sink_harness_without_sinkpad);
262 tcase_add_test (tc_chain,
263 test_forward_event_and_query_to_sink_harness_while_teardown);
264 tcase_add_test (tc_chain, test_get_all_data);
269 GST_CHECK_MAIN (gst_harness);