harness: add gst_harness_take_all_data() + _take_all_data_as_{bytes,buffer}()
[platform/upstream/gstreamer.git] / tests / check / libs / gstharness.c
1 /*
2  * Tests and examples of GstHarness
3  *
4  * Copyright (C) 2015 Havard Graff <havard@pexip.com>
5  *
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.
10  *
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.
15  *
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.
20  */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/check/gstcheck.h>
26 #include <gst/check/gstharness.h>
27
28 static void
29 create_destroy_element_harness (gpointer data, gpointer user_data)
30 {
31   GstElement *element = user_data;
32   GstHarness *h = gst_harness_new_with_element (element, NULL, NULL);
33   gst_harness_teardown (h);
34 }
35
36 GST_START_TEST (test_harness_element_ref)
37 {
38   GstHarness *h = gst_harness_new ("identity");
39   GstHarnessThread *threads[100];
40   gint i;
41
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]);
47
48   fail_unless_equals_int (G_OBJECT (h->element)->ref_count, 1);
49
50   gst_harness_teardown (h);
51 }
52
53 GST_END_TEST;
54
55
56 GST_START_TEST (test_src_harness)
57 {
58   GstHarness *h = gst_harness_new ("identity");
59
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);
64
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);
68
69   /* verify that identity outputs a buffer by pulling and unreffing */
70   gst_buffer_unref (gst_harness_pull (h));
71
72   gst_harness_teardown (h);
73 }
74
75 GST_END_TEST;
76
77 GST_START_TEST (test_src_harness_no_forwarding)
78 {
79   GstHarness *h = gst_harness_new ("identity");
80
81   /* turn of forwarding of necessary events */
82   gst_harness_set_forwarding (h, FALSE);
83
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);
88
89   /* start the fakesrc to produce the first events */
90   gst_harness_play (h->src_harness);
91
92   /* transfer STREAM_START event */
93   gst_harness_src_push_event (h);
94
95   /* crank the clock to produce the CAPS and SEGMENT events */
96   gst_harness_crank_single_clock_wait (h->src_harness);
97
98   /* transfer CAPS event */
99   gst_harness_src_push_event (h);
100
101   /* transfer SEGMENT event */
102   gst_harness_src_push_event (h);
103
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);
107
108   /* and verify that the identity element outputs it */
109   gst_buffer_unref (gst_harness_pull (h));
110
111   gst_harness_teardown (h);
112 }
113
114 GST_END_TEST;
115
116 GST_START_TEST (test_add_sink_harness_without_sinkpad)
117 {
118   GstHarness *h = gst_harness_new ("fakesink");
119
120   gst_harness_add_sink (h, "fakesink");
121
122   gst_harness_teardown (h);
123 }
124
125 GST_END_TEST;
126
127 static GstEvent *
128 create_new_stream_start_event (GstHarness * h, gpointer data)
129 {
130   guint *counter = data;
131   gchar *stream_id = g_strdup_printf ("streamid/%d", *counter);
132   GstEvent *event = gst_event_new_stream_start (stream_id);
133   g_free (stream_id);
134   (*counter)++;
135   return event;
136 }
137
138 static void
139 push_query (gpointer data, gpointer user_data)
140 {
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);
147 }
148
149 GST_START_TEST (test_forward_event_and_query_to_sink_harness_while_teardown)
150 {
151   GstHarness *h = gst_harness_new ("identity");
152   guint counter = 0;
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,
156       push_query, h, 0);
157   gdouble duration = 1.0;
158   GTimer *timer = g_timer_new ();
159
160   while (g_timer_elapsed (timer, NULL) < duration) {
161     gst_harness_add_sink (h, "fakesink");
162     g_thread_yield ();
163   }
164
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);
169 }
170
171 GST_END_TEST;
172
173 static GstHarness *
174 harness_new_and_fill_with_data (void)
175 {
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;
179
180   gst_harness_play (h);
181
182   do {
183     GstEvent *e = gst_harness_pull_event (h);
184     have_eos = GST_EVENT_TYPE (e) == GST_EVENT_EOS;
185     gst_event_unref (e);
186   } while (!have_eos);
187
188   return h;
189 }
190
191 GST_START_TEST (test_get_all_data)
192 {
193   guint8 expected[50];
194   const guint8 *cdata;
195   GstHarness *h;
196   GstBuffer *buf;
197   GBytes *bytes;
198   guint8 *data;
199   gsize size;
200   guint i;
201
202   for (i = 0; i < G_N_ELEMENTS (expected); ++i)
203     expected[i] = i;
204
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);
217
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);
233
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);
239   g_free (data);
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);
245 }
246
247 GST_END_TEST;
248
249 static Suite *
250 gst_harness_suite (void)
251 {
252   Suite *s = suite_create ("GstHarness");
253   TCase *tc_chain = tcase_create ("harness");
254
255   suite_add_tcase (s, tc_chain);
256
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);
261
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);
265
266   return s;
267 }
268
269 GST_CHECK_MAIN (gst_harness);