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.
22 #include <gst/check/gstcheck.h>
23 #include <gst/check/gstharness.h>
26 create_destroy_element_harness (gpointer data, gpointer user_data)
28 GstElement *element = user_data;
29 GstHarness *h = gst_harness_new_with_element (element, NULL, NULL);
30 gst_harness_teardown (h);
33 GST_START_TEST (test_harness_element_ref)
35 GstHarness *h = gst_harness_new ("identity");
36 GstHarnessThread *threads[100];
39 for (i = 0; i < G_N_ELEMENTS (threads); i++)
40 threads[i] = gst_harness_stress_custom_start (h, NULL,
41 create_destroy_element_harness, h->element, 0);
42 for (i = 0; i < G_N_ELEMENTS (threads); i++)
43 gst_harness_stress_thread_stop (threads[i]);
45 fail_unless_equals_int (G_OBJECT (h->element)->ref_count, 1);
47 gst_harness_teardown (h);
53 GST_START_TEST (test_src_harness)
55 GstHarness *h = gst_harness_new ("identity");
57 /* add a fakesrc that syncs to the clock and a
58 capsfilter that adds some caps to it */
59 gst_harness_add_src_parse (h,
60 "fakesrc sync=1 ! capsfilter caps=\"mycaps\"", TRUE);
62 /* this cranks the clock and transfers the resulting buffer
63 from the src-harness into the identity element */
64 gst_harness_push_from_src (h);
66 /* verify that identity outputs a buffer by pulling and unreffing */
67 gst_buffer_unref (gst_harness_pull (h));
69 gst_harness_teardown (h);
74 GST_START_TEST (test_src_harness_no_forwarding)
76 GstHarness *h = gst_harness_new ("identity");
78 /* turn of forwarding of necessary events */
79 gst_harness_set_forwarding (h, FALSE);
81 /* add a fakesrc that syncs to the clock and a
82 capsfilter that adds some caps to it */
83 gst_harness_add_src_parse (h,
84 "fakesrc sync=1 ! capsfilter caps=\"mycaps\"", TRUE);
86 /* start the fakesrc to produce the first events */
87 gst_harness_play (h->src_harness);
89 /* transfer STREAM_START event */
90 gst_harness_src_push_event (h);
92 /* crank the clock to produce the CAPS and SEGMENT events */
93 gst_harness_crank_single_clock_wait (h->src_harness);
95 /* transfer CAPS event */
96 gst_harness_src_push_event (h);
98 /* transfer SEGMENT event */
99 gst_harness_src_push_event (h);
101 /* now transfer the buffer produced by exploiting
102 the ability to say 0 cranks but 1 push */
103 gst_harness_src_crank_and_push_many (h, 0, 1);
105 /* and verify that the identity element outputs it */
106 gst_buffer_unref (gst_harness_pull (h));
108 gst_harness_teardown (h);
113 GST_START_TEST (test_add_sink_harness_without_sinkpad)
115 GstHarness *h = gst_harness_new ("fakesink");
117 gst_harness_add_sink (h, "fakesink");
119 gst_harness_teardown (h);
125 create_new_stream_start_event (GstHarness * h, gpointer data)
127 guint *counter = data;
128 gchar *stream_id = g_strdup_printf ("streamid/%d", *counter);
129 GstEvent *event = gst_event_new_stream_start (stream_id);
136 push_query (gpointer data, gpointer user_data)
138 GstHarness *h = user_data;
139 GstCaps *caps = gst_caps_new_empty_simple ("mycaps");
140 GstQuery *query = gst_query_new_allocation (caps, FALSE);
141 gst_caps_unref (caps);
142 gst_pad_peer_query (h->srcpad, query);
143 gst_query_unref (query);
146 GST_START_TEST (test_forward_event_and_query_to_sink_harness_while_teardown)
148 GstHarness *h = gst_harness_new ("identity");
150 GstHarnessThread *e_thread = gst_harness_stress_push_event_with_cb_start (h,
151 create_new_stream_start_event, &counter, NULL);
152 GstHarnessThread *q_thread = gst_harness_stress_custom_start (h, NULL,
154 gdouble duration = 1.0;
155 GTimer *timer = g_timer_new ();
157 while (g_timer_elapsed (timer, NULL) < duration) {
158 gst_harness_add_sink (h, "fakesink");
162 g_timer_destroy (timer);
163 gst_harness_stress_thread_stop (q_thread);
164 gst_harness_stress_thread_stop (e_thread);
165 gst_harness_teardown (h);
171 gst_harness_suite (void)
173 Suite *s = suite_create ("GstHarness");
174 TCase *tc_chain = tcase_create ("harness");
176 suite_add_tcase (s, tc_chain);
178 tcase_add_test (tc_chain, test_harness_element_ref);
179 tcase_add_test (tc_chain, test_src_harness);
180 tcase_add_test (tc_chain, test_src_harness_no_forwarding);
181 tcase_add_test (tc_chain, test_add_sink_harness_without_sinkpad);
183 tcase_add_test (tc_chain,
184 test_forward_event_and_query_to_sink_harness_while_teardown);
189 GST_CHECK_MAIN (gst_harness);