1 /* GstHarness - A test-harness for GStreamer testing
3 * Copyright (C) 2012-2015 Pexip <pexip.com>
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.
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.
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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 * @short_description: A test-harness for writing GStreamer unit tests
24 * @see_also: #GstTestClock,\
26 * #GstHarness is meant to make writing unit test for GStreamer much easier.
27 * It can be thought of as a way of treating a #GstElement as a black box,
28 * deterministically feeding it data, and controlling what data it outputs.
30 * The basic structure of #GstHarness is two "floating" #GstPads that connect
31 * to the harnessed #GstElement src and sink #GstPads like so:
34 * __________________________
35 * _____ | _____ _____ | _____
37 * | src |--+-| sink| Element | src |-+--| sink|
38 * |_____| | |_____| |_____| | |_____|
39 * |__________________________|
43 * With this, you can now simulate any environment the #GstElement might find
44 * itself in. By specifying the #GstCaps of the harness #GstPads, using
45 * functions like gst_harness_set_src_caps() or gst_harness_set_sink_caps_str(),
46 * you can test how the #GstElement interacts with different caps sets.
48 * Your harnessed #GstElement can of course also be a bin, and using
49 * gst_harness_new_parse() supporting standard gst-launch syntax, you can
50 * easily test a whole pipeline instead of just one element.
52 * You can then go on to push #GstBuffers and #GstEvents on to the srcpad,
53 * using functions like gst_harness_push() and gst_harness_push_event(), and
54 * then pull them out to examine them with gst_harness_pull() and
55 * gst_harness_pull_event().
58 * <title>A simple buffer-in buffer-out example</title>
59 * <programlisting language="c">
60 * #include <gst/gst.h>
61 * #include <gst/check/gstharness.h>
66 * // attach the harness to the src and sink pad of GstQueue
67 * h = gst_harness_new ("queue");
69 * // we must specify a caps before pushing buffers
70 * gst_harness_set_src_caps_str (h, "mycaps");
72 * // create a buffer of size 42
73 * in_buf = gst_harness_create_buffer (h, 42);
75 * // push the buffer into the queue
76 * gst_harness_push (h, in_buf);
78 * // pull the buffer from the queue
79 * out_buf = gst_harness_pull (h);
81 * // validate the buffer in is the same as buffer out
82 * fail_unless (in_buf == out_buf);
85 * gst_buffer_unref (out_buf);
86 * gst_harness_teardown (h);
91 * Another main feature of the #GstHarness is its integration with the
92 * #GstTestClock. Operating the #GstTestClock can be very challenging, but
93 * #GstHarness simplifies some of the most desired actions a lot, like wanting
94 * to manually advance the clock while at the same time releasing a #GstClockID
95 * that is waiting, with functions like gst_harness_crank_single_clock_wait().
97 * #GstHarness also supports sub-harnesses, as a way of generating and
98 * validating data. A sub-harness is another #GstHarness that is managed by
99 * the "parent" harness, and can either be created by using the standard
100 * gst_harness_new type functions directly on the (GstHarness *)->src_harness,
101 * or using the much more convenient gst_harness_add_src() or
102 * gst_harness_add_sink_parse(). If you have a decoder-element you want to test,
103 * (like vp8dec) it can be very useful to add a src-harness with both a
104 * src-element (videotestsrc) and an encoder (vp8enc) to feed the decoder data
105 * with different configurations, by simply doing:
108 * <programlisting language="c">
109 * GstHarness * h = gst_harness_new (h, "vp8dec");
110 * gst_harness_add_src_parse (h, "videotestsrc is-live=1 ! vp8enc", TRUE);
114 * and then feeding it data with:
117 * <programlisting language="c">
118 * gst_harness_push_from_src (h);
127 /* we have code with side effects in asserts, so make sure they are active */
128 #ifdef G_DISABLE_ASSERT
129 #error "GstHarness must be compiled with G_DISABLE_ASSERT undefined"
132 #include "gstharness.h"
138 static void gst_harness_stress_free (GstHarnessThread * t);
140 #define HARNESS_KEY "harness"
141 #define HARNESS_REF "harness-ref"
142 #define HARNESS_LOCK(h) g_mutex_lock (&(h)->priv->priv_mutex)
143 #define HARNESS_UNLOCK(h) g_mutex_unlock (&(h)->priv->priv_mutex)
145 static GstStaticPadTemplate hsrctemplate = GST_STATIC_PAD_TEMPLATE ("src",
148 GST_STATIC_CAPS_ANY);
149 static GstStaticPadTemplate hsinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
152 GST_STATIC_CAPS_ANY);
154 struct _GstHarnessPrivate
156 gchar *element_sinkpad_name;
157 gchar *element_srcpad_name;
163 GstPad *sink_forward_pad;
164 GstTestClock *testclock;
166 volatile gint recv_buffers;
167 volatile gint recv_events;
168 volatile gint recv_upstream_events;
170 GAsyncQueue *buffer_queue;
171 GAsyncQueue *src_event_queue;
172 GAsyncQueue *sink_event_queue;
174 GstClockTime latency_min;
175 GstClockTime latency_max;
176 gboolean has_clock_wait;
177 gboolean drop_buffers;
178 GstClockTime last_push_ts;
181 GstAllocator *allocator;
182 GstAllocationParams allocation_params;
183 GstAllocator *propose_allocator;
184 GstAllocationParams propose_allocation_params;
186 gboolean blocking_push_mode;
187 GCond blocking_push_cond;
188 GMutex blocking_push_mutex;
195 gst_harness_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
197 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
198 GstHarnessPrivate *priv = h->priv;
200 g_assert (h != NULL);
201 g_mutex_lock (&priv->blocking_push_mutex);
202 g_atomic_int_inc (&priv->recv_buffers);
204 if (priv->drop_buffers)
205 gst_buffer_unref (buffer);
207 g_async_queue_push (priv->buffer_queue, buffer);
209 if (priv->blocking_push_mode) {
210 g_cond_wait (&priv->blocking_push_cond, &priv->blocking_push_mutex);
212 g_mutex_unlock (&priv->blocking_push_mutex);
218 gst_harness_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
220 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
221 GstHarnessPrivate *priv = h->priv;
223 g_assert (h != NULL);
224 g_atomic_int_inc (&priv->recv_upstream_events);
225 g_async_queue_push (priv->src_event_queue, event);
230 gst_harness_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
232 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
233 GstHarnessPrivate *priv = h->priv;
237 g_assert (h != NULL);
239 g_atomic_int_inc (&priv->recv_events);
241 switch (GST_EVENT_TYPE (event)) {
242 case GST_EVENT_STREAM_START:
244 case GST_EVENT_SEGMENT:
253 if (priv->forwarding && forward && priv->sink_forward_pad) {
254 GstPad *fwdpad = gst_object_ref (priv->sink_forward_pad);
256 ret = gst_pad_push_event (fwdpad, event);
257 gst_object_unref (fwdpad);
260 g_async_queue_push (priv->sink_event_queue, event);
268 gst_harness_decide_allocation (GstHarness * h, GstCaps * caps)
270 GstHarnessPrivate *priv = h->priv;
272 GstAllocator *allocator;
273 GstAllocationParams params;
274 GstBufferPool *pool = NULL;
275 guint size, min, max;
277 query = gst_query_new_allocation (caps, FALSE);
278 gst_pad_peer_query (h->srcpad, query);
280 if (gst_query_get_n_allocation_params (query) > 0) {
281 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
284 gst_allocation_params_init (¶ms);
287 if (gst_query_get_n_allocation_pools (query) > 0) {
288 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
290 /* Most elements create their own pools if pool == NULL. Not sure if we
291 * want to do that in the harness since we may want to test the pool
292 * implementation of the elements. Not creating a pool will however ignore
293 * the returned size. */
295 pool = gst_buffer_pool_new ();
299 size = min = max = 0;
301 gst_query_unref (query);
304 GstStructure *config = gst_buffer_pool_get_config (pool);
305 gst_buffer_pool_config_set_params (config, caps, size, min, max);
306 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
307 gst_buffer_pool_set_config (pool, config);
310 if (pool != priv->pool) {
311 if (priv->pool != NULL)
312 gst_buffer_pool_set_active (priv->pool, FALSE);
314 gst_buffer_pool_set_active (pool, TRUE);
317 priv->allocation_params = params;
319 gst_object_unref (priv->allocator);
320 priv->allocator = allocator;
322 gst_object_unref (priv->pool);
327 gst_harness_negotiate (GstHarness * h)
331 caps = gst_pad_get_current_caps (h->srcpad);
333 gst_harness_decide_allocation (h, caps);
334 gst_caps_unref (caps);
336 GST_FIXME_OBJECT (h, "Cannot negotiate allocation because caps is not set");
341 gst_harness_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
343 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
344 GstHarnessPrivate *priv = h->priv;
346 g_assert (h != NULL);
348 // FIXME: forward all queries?
350 switch (GST_QUERY_TYPE (query)) {
351 case GST_QUERY_LATENCY:
352 gst_query_set_latency (query, TRUE, priv->latency_min, priv->latency_max);
356 GstCaps *caps, *filter = NULL;
358 if (priv->sink_caps) {
359 caps = gst_caps_ref (priv->sink_caps);
361 caps = gst_pad_get_pad_template_caps (pad);
364 gst_query_parse_caps (query, &filter);
365 if (filter != NULL) {
366 gst_caps_take (&caps,
367 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST));
370 gst_query_set_caps_result (query, caps);
371 gst_caps_unref (caps);
374 case GST_QUERY_ALLOCATION:
377 if (priv->forwarding && priv->sink_forward_pad != NULL) {
378 GstPad *peer = gst_pad_get_peer (priv->sink_forward_pad);
379 g_assert (peer != NULL);
381 res = gst_pad_query (peer, query);
382 gst_object_unref (peer);
389 gst_query_parse_allocation (query, &caps, &need_pool);
391 /* FIXME: Can this be removed? */
392 size = gst_query_get_n_allocation_params (query);
393 g_assert_cmpuint (0, ==, size);
394 gst_query_add_allocation_param (query,
395 priv->propose_allocator, &priv->propose_allocation_params);
397 GST_DEBUG_OBJECT (pad, "proposing allocation %" GST_PTR_FORMAT,
398 priv->propose_allocator);
404 res = gst_pad_query_default (pad, parent, query);
411 gst_harness_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
413 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
414 GstHarnessPrivate *priv = h->priv;
416 g_assert (h != NULL);
418 switch (GST_QUERY_TYPE (query)) {
419 case GST_QUERY_LATENCY:
420 gst_query_set_latency (query, TRUE, priv->latency_min, priv->latency_max);
424 GstCaps *caps, *filter = NULL;
426 if (priv->src_caps) {
427 caps = gst_caps_ref (priv->src_caps);
429 caps = gst_pad_get_pad_template_caps (pad);
432 gst_query_parse_caps (query, &filter);
433 if (filter != NULL) {
434 gst_caps_take (&caps,
435 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST));
438 gst_query_set_caps_result (query, caps);
439 gst_caps_unref (caps);
443 res = gst_pad_query_default (pad, parent, query);
449 gst_harness_element_ref (GstHarness * h)
453 GST_OBJECT_LOCK (h->element);
454 data = g_object_get_data (G_OBJECT (h->element), HARNESS_REF);
456 data = g_new0 (guint, 1);
458 g_object_set_data_full (G_OBJECT (h->element), HARNESS_REF, data, g_free);
462 GST_OBJECT_UNLOCK (h->element);
466 gst_harness_element_unref (GstHarness * h)
471 GST_OBJECT_LOCK (h->element);
472 data = g_object_get_data (G_OBJECT (h->element), HARNESS_REF);
473 g_assert (data != NULL);
476 GST_OBJECT_UNLOCK (h->element);
482 gst_harness_link_element_srcpad (GstHarness * h,
483 const gchar * element_srcpad_name)
485 GstHarnessPrivate *priv = h->priv;
486 GstPad *srcpad = gst_element_get_static_pad (h->element,
487 element_srcpad_name);
488 GstPadLinkReturn link;
490 srcpad = gst_element_get_request_pad (h->element, element_srcpad_name);
492 link = gst_pad_link (srcpad, h->sinkpad);
493 g_assert_cmpint (link, ==, GST_PAD_LINK_OK);
494 g_free (priv->element_srcpad_name);
495 priv->element_srcpad_name = gst_pad_get_name (srcpad);
497 gst_object_unref (srcpad);
501 gst_harness_link_element_sinkpad (GstHarness * h,
502 const gchar * element_sinkpad_name)
504 GstHarnessPrivate *priv = h->priv;
505 GstPad *sinkpad = gst_element_get_static_pad (h->element,
506 element_sinkpad_name);
507 GstPadLinkReturn link;
509 sinkpad = gst_element_get_request_pad (h->element, element_sinkpad_name);
511 link = gst_pad_link (h->srcpad, sinkpad);
512 g_assert_cmpint (link, ==, GST_PAD_LINK_OK);
513 g_free (priv->element_sinkpad_name);
514 priv->element_sinkpad_name = gst_pad_get_name (sinkpad);
516 gst_object_unref (sinkpad);
520 gst_harness_setup_src_pad (GstHarness * h,
521 GstStaticPadTemplate * src_tmpl, const gchar * element_sinkpad_name)
523 GstHarnessPrivate *priv = h->priv;
525 g_assert (h->srcpad == NULL);
527 priv->src_event_queue =
528 g_async_queue_new_full ((GDestroyNotify) gst_event_unref);
531 h->srcpad = gst_pad_new_from_static_template (src_tmpl, "src");
532 g_assert (h->srcpad);
533 g_object_set_data (G_OBJECT (h->srcpad), HARNESS_KEY, h);
535 gst_pad_set_query_function (h->srcpad, gst_harness_src_query);
536 gst_pad_set_event_function (h->srcpad, gst_harness_src_event);
538 gst_pad_set_active (h->srcpad, TRUE);
540 if (element_sinkpad_name)
541 gst_harness_link_element_sinkpad (h, element_sinkpad_name);
545 gst_harness_setup_sink_pad (GstHarness * h,
546 GstStaticPadTemplate * sink_tmpl, const gchar * element_srcpad_name)
548 GstHarnessPrivate *priv = h->priv;
549 g_assert (sink_tmpl);
550 g_assert (h->sinkpad == NULL);
552 priv->buffer_queue = g_async_queue_new_full (
553 (GDestroyNotify) gst_buffer_unref);
554 priv->sink_event_queue = g_async_queue_new_full (
555 (GDestroyNotify) gst_event_unref);
558 h->sinkpad = gst_pad_new_from_static_template (sink_tmpl, "sink");
559 g_assert (h->sinkpad);
560 g_object_set_data (G_OBJECT (h->sinkpad), HARNESS_KEY, h);
562 gst_pad_set_chain_function (h->sinkpad, gst_harness_chain);
563 gst_pad_set_query_function (h->sinkpad, gst_harness_sink_query);
564 gst_pad_set_event_function (h->sinkpad, gst_harness_sink_event);
566 gst_pad_set_active (h->sinkpad, TRUE);
568 if (element_srcpad_name)
569 gst_harness_link_element_srcpad (h, element_srcpad_name);
573 check_element_type (GstElement * element, gboolean * has_sinkpad,
574 gboolean * has_srcpad)
576 GstElementClass *element_class = GST_ELEMENT_GET_CLASS (element);
577 const GList *tmpl_list;
579 *has_srcpad = element->numsrcpads > 0;
580 *has_sinkpad = element->numsinkpads > 0;
582 tmpl_list = gst_element_class_get_pad_template_list (element_class);
585 GstPadTemplate *pad_tmpl = (GstPadTemplate *) tmpl_list->data;
586 tmpl_list = g_list_next (tmpl_list);
587 if (GST_PAD_TEMPLATE_DIRECTION (pad_tmpl) == GST_PAD_SRC)
589 if (GST_PAD_TEMPLATE_DIRECTION (pad_tmpl) == GST_PAD_SINK)
590 *has_sinkpad |= TRUE;
595 turn_async_and_sync_off (GstElement * element)
597 GObjectClass *class = G_OBJECT_GET_CLASS (element);
598 if (g_object_class_find_property (class, "async"))
599 g_object_set (element, "async", FALSE, NULL);
600 if (g_object_class_find_property (class, "sync"))
601 g_object_set (element, "sync", FALSE, NULL);
605 gst_pad_is_request_pad (GstPad * pad)
607 GstPadTemplate *temp;
612 temp = gst_pad_get_pad_template (pad);
615 is_request = GST_PAD_TEMPLATE_PRESENCE (temp) == GST_PAD_REQUEST;
616 gst_object_unref (temp);
621 * gst_harness_new_empty: (skip)
623 * Creates a new empty harness. Use gst_harness_add_element_full() to add
624 * an #GstElement to it.
628 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
634 gst_harness_new_empty (void)
637 GstHarnessPrivate *priv;
639 h = g_new0 (GstHarness, 1);
640 g_assert (h != NULL);
641 h->priv = g_new0 (GstHarnessPrivate, 1);
644 GST_DEBUG_OBJECT (h, "about to create new harness %p", h);
645 priv->last_push_ts = GST_CLOCK_TIME_NONE;
646 priv->latency_min = 0;
647 priv->latency_max = GST_CLOCK_TIME_NONE;
648 priv->drop_buffers = FALSE;
649 priv->testclock = GST_TEST_CLOCK_CAST (gst_test_clock_new ());
651 priv->propose_allocator = NULL;
652 gst_allocation_params_init (&priv->propose_allocation_params);
654 g_mutex_init (&priv->blocking_push_mutex);
655 g_cond_init (&priv->blocking_push_cond);
656 g_mutex_init (&priv->priv_mutex);
658 priv->stress = g_ptr_array_new_with_free_func (
659 (GDestroyNotify) gst_harness_stress_free);
661 /* we have forwarding on as a default */
662 gst_harness_set_forwarding (h, TRUE);
668 * gst_harness_add_element_full: (skip)
670 * @element: a #GstElement to add to the harness (transfer none)
671 * @hsrc: (allow-none): a #GstStaticPadTemplate describing the harness srcpad.
672 * %NULL will not create a harness srcpad.
673 * @element_sinkpad_name: (allow-none): a #gchar with the name of the element
674 * sinkpad that is then linked to the harness srcpad. Can be a static or request
675 * or a sometimes pad that has been added. %NULL will not get/request a sinkpad
676 * from the element. (Like if the element is a src.)
677 * @hsink: (allow-none): a #GstStaticPadTemplate describing the harness sinkpad.
678 * %NULL will not create a harness sinkpad.
679 * @element_srcpad_name: (allow-none): a #gchar with the name of the element
680 * srcpad that is then linked to the harness sinkpad, similar to the
681 * @element_sinkpad_name.
683 * Adds a #GstElement to an empty #GstHarness
690 gst_harness_add_element_full (GstHarness * h, GstElement * element,
691 GstStaticPadTemplate * hsrc, const gchar * element_sinkpad_name,
692 GstStaticPadTemplate * hsink, const gchar * element_srcpad_name)
694 GstClock *element_clock;
695 gboolean has_sinkpad, has_srcpad;
697 g_return_if_fail (element != NULL);
698 g_return_if_fail (h->element == NULL);
700 element_clock = GST_ELEMENT_CLOCK (element);
701 h->element = gst_object_ref (element);
702 check_element_type (element, &has_sinkpad, &has_srcpad);
704 /* setup the loose srcpad linked to the element sinkpad */
706 gst_harness_setup_src_pad (h, hsrc, element_sinkpad_name);
708 /* setup the loose sinkpad linked to the element srcpad */
710 gst_harness_setup_sink_pad (h, hsink, element_srcpad_name);
712 /* as a harness sink, we should not need sync and async */
713 if (has_sinkpad && !has_srcpad)
714 turn_async_and_sync_off (h->element);
716 if (h->srcpad != NULL) {
718 gchar *stream_id = g_strdup_printf ("%s-%p",
719 GST_OBJECT_NAME (h->element), h);
720 handled = gst_pad_push_event (h->srcpad,
721 gst_event_new_stream_start (stream_id));
726 /* if the element already has a testclock attached,
727 we replace our own with it, if no clock we attach the testclock */
729 if (GST_IS_TEST_CLOCK (element_clock)) {
730 gst_object_replace ((GstObject **) & h->priv->testclock,
731 (GstObject *) GST_ELEMENT_CLOCK (element));
734 gst_harness_use_testclock (h);
737 /* don't start sources, they start producing data! */
739 gst_harness_play (h);
741 gst_harness_element_ref (h);
743 GST_DEBUG_OBJECT (h, "added element to harness %p "
744 "with element_srcpad_name (%p, %s, %s) and element_sinkpad_name (%p, %s, %s)",
745 h, h->srcpad, GST_DEBUG_PAD_NAME (h->srcpad),
746 h->sinkpad, GST_DEBUG_PAD_NAME (h->sinkpad));
750 * gst_harness_new_full: (skip)
751 * @element: a #GstElement to attach the harness to (transfer none)
752 * @hsrc: (allow-none): a #GstStaticPadTemplate describing the harness srcpad.
753 * %NULL will not create a harness srcpad.
754 * @element_sinkpad_name: (allow-none): a #gchar with the name of the element
755 * sinkpad that is then linked to the harness srcpad. Can be a static or request
756 * or a sometimes pad that has been added. %NULL will not get/request a sinkpad
757 * from the element. (Like if the element is a src.)
758 * @hsink: (allow-none): a #GstStaticPadTemplate describing the harness sinkpad.
759 * %NULL will not create a harness sinkpad.
760 * @element_srcpad_name: (allow-none): a #gchar with the name of the element
761 * srcpad that is then linked to the harness sinkpad, similar to the
762 * @element_sinkpad_name.
764 * Creates a new harness.
768 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
774 gst_harness_new_full (GstElement * element,
775 GstStaticPadTemplate * hsrc, const gchar * element_sinkpad_name,
776 GstStaticPadTemplate * hsink, const gchar * element_srcpad_name)
779 h = gst_harness_new_empty ();
780 gst_harness_add_element_full (h, element,
781 hsrc, element_sinkpad_name, hsink, element_srcpad_name);
786 * gst_harness_new_with_element: (skip)
787 * @element: a #GstElement to attach the harness to (transfer none)
788 * @element_sinkpad_name: (allow-none): a #gchar with the name of the element
789 * sinkpad that is then linked to the harness srcpad. %NULL does not attach a
791 * @element_srcpad_name: (allow-none): a #gchar with the name of the element
792 * srcpad that is then linked to the harness sinkpad. %NULL does not attach a
795 * Creates a new harness. Works in the same way as gst_harness_new_full(), only
796 * that generic padtemplates are used for the harness src and sinkpads, which
797 * will be sufficient in most usecases.
801 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
807 gst_harness_new_with_element (GstElement * element,
808 const gchar * element_sinkpad_name, const gchar * element_srcpad_name)
810 return gst_harness_new_full (element,
811 &hsrctemplate, element_sinkpad_name, &hsinktemplate, element_srcpad_name);
815 * gst_harness_new_with_padnames: (skip)
816 * @element_name: a #gchar describing the #GstElement name
817 * @element_sinkpad_name: (allow-none): a #gchar with the name of the element
818 * sinkpad that is then linked to the harness srcpad. %NULL does not attach a
820 * @element_srcpad_name: (allow-none): a #gchar with the name of the element
821 * srcpad that is then linked to the harness sinkpad. %NULL does not attach a
824 * Creates a new harness. Works like gst_harness_new_with_element(),
825 * except you specify the factoryname of the #GstElement
829 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
835 gst_harness_new_with_padnames (const gchar * element_name,
836 const gchar * element_sinkpad_name, const gchar * element_srcpad_name)
839 GstElement *element = gst_element_factory_make (element_name, NULL);
840 g_assert (element != NULL);
842 h = gst_harness_new_with_element (element, element_sinkpad_name,
843 element_srcpad_name);
844 gst_object_unref (element);
849 * gst_harness_new_with_templates: (skip)
850 * @element_name: a #gchar describing the #GstElement name
851 * @hsrc: (allow-none): a #GstStaticPadTemplate describing the harness srcpad.
852 * %NULL will not create a harness srcpad.
853 * @hsink: (allow-none): a #GstStaticPadTemplate describing the harness sinkpad.
854 * %NULL will not create a harness sinkpad.
856 * Creates a new harness, like gst_harness_new_full(), except it
857 * assumes the #GstElement sinkpad is named "sink" and srcpad is named "src"
861 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
867 gst_harness_new_with_templates (const gchar * element_name,
868 GstStaticPadTemplate * hsrc, GstStaticPadTemplate * hsink)
871 GstElement *element = gst_element_factory_make (element_name, NULL);
872 g_assert (element != NULL);
874 h = gst_harness_new_full (element, hsrc, "sink", hsink, "src");
875 gst_object_unref (element);
880 * gst_harness_new: (skip)
881 * @element_name: a #gchar describing the #GstElement name
883 * Creates a new harness. Works like gst_harness_new_with_padnames(), except it
884 * assumes the #GstElement sinkpad is named "sink" and srcpad is named "src"
888 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
894 gst_harness_new (const gchar * element_name)
896 return gst_harness_new_with_padnames (element_name, "sink", "src");
900 * gst_harness_add_parse: (skip)
902 * @launchline: a #gchar describing a gst-launch type line
904 * Parses the @launchline and puts that in a #GstBin,
905 * and then attches the supplied #GstHarness to the bin.
912 gst_harness_add_parse (GstHarness * h, const gchar * launchline)
918 gboolean done = FALSE;
920 g_return_if_fail (launchline != NULL);
922 desc = g_strdup_printf ("bin.( %s )", launchline);
924 (GstBin *) gst_parse_launch_full (desc, NULL, GST_PARSE_FLAG_NONE, NULL);
927 if (G_UNLIKELY (bin == NULL))
930 /* find pads and ghost them if necessary */
931 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC)) != NULL) {
932 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("src", pad));
933 gst_object_unref (pad);
935 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SINK)) != NULL) {
936 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("sink", pad));
937 gst_object_unref (pad);
940 iter = gst_bin_iterate_sinks (bin);
942 GValue item = { 0, };
944 switch (gst_iterator_next (iter, &item)) {
945 case GST_ITERATOR_OK:
946 turn_async_and_sync_off (GST_ELEMENT (g_value_get_object (&item)));
947 g_value_reset (&item);
949 case GST_ITERATOR_DONE:
952 case GST_ITERATOR_RESYNC:
953 gst_iterator_resync (iter);
955 case GST_ITERATOR_ERROR:
956 gst_object_unref (bin);
957 gst_iterator_free (iter);
958 g_return_if_reached ();
962 gst_iterator_free (iter);
964 gst_harness_add_element_full (h, GST_ELEMENT_CAST (bin),
965 &hsrctemplate, "sink", &hsinktemplate, "src");
966 gst_object_unref (bin);
970 * gst_harness_new_parse: (skip)
971 * @launchline: a #gchar describing a gst-launch type line
973 * Creates a new harness, parsing the @launchline and putting that in a #GstBin,
974 * and then attches the harness to the bin.
978 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
984 gst_harness_new_parse (const gchar * launchline)
987 h = gst_harness_new_empty ();
988 gst_harness_add_parse (h, launchline);
993 * gst_harness_teardown:
996 * Tears down a @GstHarness, freeing all resources allocated using it.
1003 gst_harness_teardown (GstHarness * h)
1005 GstHarnessPrivate *priv = h->priv;
1007 if (priv->blocking_push_mode) {
1008 g_mutex_lock (&priv->blocking_push_mutex);
1009 priv->blocking_push_mode = FALSE;
1010 g_cond_signal (&priv->blocking_push_cond);
1011 g_mutex_unlock (&priv->blocking_push_mutex);
1014 if (h->src_harness) {
1015 gst_harness_teardown (h->src_harness);
1018 gst_object_replace ((GstObject **) & priv->sink_forward_pad, NULL);
1019 if (h->sink_harness) {
1020 gst_harness_teardown (h->sink_harness);
1024 gst_caps_unref (priv->src_caps);
1026 if (priv->sink_caps)
1027 gst_caps_unref (priv->sink_caps);
1030 if (gst_pad_is_request_pad (GST_PAD_PEER (h->srcpad)))
1031 gst_element_release_request_pad (h->element, GST_PAD_PEER (h->srcpad));
1032 g_free (priv->element_sinkpad_name);
1034 gst_pad_set_active (h->srcpad, FALSE);
1035 gst_object_unref (h->srcpad);
1037 g_async_queue_unref (priv->src_event_queue);
1041 if (gst_pad_is_request_pad (GST_PAD_PEER (h->sinkpad)))
1042 gst_element_release_request_pad (h->element, GST_PAD_PEER (h->sinkpad));
1043 g_free (priv->element_srcpad_name);
1045 gst_pad_set_active (h->sinkpad, FALSE);
1046 gst_object_unref (h->sinkpad);
1048 g_async_queue_unref (priv->buffer_queue);
1049 g_async_queue_unref (priv->sink_event_queue);
1052 gst_object_replace ((GstObject **) & priv->propose_allocator, NULL);
1053 gst_object_replace ((GstObject **) & priv->allocator, NULL);
1054 gst_object_replace ((GstObject **) & priv->pool, NULL);
1056 /* if we hold the last ref, set to NULL */
1057 if (gst_harness_element_unref (h) == 0) {
1058 gboolean state_change;
1059 GstState state, pending;
1060 state_change = gst_element_set_state (h->element, GST_STATE_NULL);
1061 g_assert (state_change == GST_STATE_CHANGE_SUCCESS);
1062 state_change = gst_element_get_state (h->element, &state, &pending, 0);
1063 g_assert (state_change == GST_STATE_CHANGE_SUCCESS);
1064 g_assert (state == GST_STATE_NULL);
1067 g_cond_clear (&priv->blocking_push_cond);
1068 g_mutex_clear (&priv->blocking_push_mutex);
1069 g_mutex_clear (&priv->priv_mutex);
1071 g_ptr_array_unref (priv->stress);
1073 gst_object_unref (h->element);
1075 gst_object_replace ((GstObject **) & priv->testclock, NULL);
1082 * gst_harness_add_element_src_pad:
1084 * @srcpad: a #GstPad to link to the harness sinkpad
1086 * Links the specifed #GstPad the @GstHarness sinkpad. This can be useful if
1087 * perhaps the srcpad did not exist at the time of creating the harness,
1088 * like a demuxer that provides a sometimes-pad after receiving data.
1095 gst_harness_add_element_src_pad (GstHarness * h, GstPad * srcpad)
1097 GstHarnessPrivate *priv = h->priv;
1098 GstPadLinkReturn link;
1099 if (h->sinkpad == NULL)
1100 gst_harness_setup_sink_pad (h, &hsinktemplate, NULL);
1101 link = gst_pad_link (srcpad, h->sinkpad);
1102 g_assert_cmpint (link, ==, GST_PAD_LINK_OK);
1103 g_free (priv->element_srcpad_name);
1104 priv->element_srcpad_name = gst_pad_get_name (srcpad);
1108 * gst_harness_add_element_sink_pad:
1110 * @sinkpad: a #GstPad to link to the harness srcpad
1112 * Links the specifed #GstPad the @GstHarness srcpad.
1119 gst_harness_add_element_sink_pad (GstHarness * h, GstPad * sinkpad)
1121 GstHarnessPrivate *priv = h->priv;
1122 GstPadLinkReturn link;
1123 if (h->srcpad == NULL)
1124 gst_harness_setup_src_pad (h, &hsrctemplate, NULL);
1125 link = gst_pad_link (h->srcpad, sinkpad);
1126 g_assert_cmpint (link, ==, GST_PAD_LINK_OK);
1127 g_free (priv->element_sinkpad_name);
1128 priv->element_sinkpad_name = gst_pad_get_name (sinkpad);
1132 * gst_harness_set_src_caps:
1134 * @caps: (transfer full): a #GstCaps to set on the harness srcpad
1136 * Sets the @GstHarness srcpad caps. This must be done before any buffers
1137 * can legally be pushed from the harness to the element.
1144 gst_harness_set_src_caps (GstHarness * h, GstCaps * caps)
1146 GstHarnessPrivate *priv = h->priv;
1150 handled = gst_pad_push_event (h->srcpad, gst_event_new_caps (caps));
1152 gst_caps_take (&priv->src_caps, caps);
1154 gst_segment_init (&segment, GST_FORMAT_TIME);
1155 handled = gst_pad_push_event (h->srcpad, gst_event_new_segment (&segment));
1159 * gst_harness_set_sink_caps:
1161 * @caps: (transfer full): a #GstCaps to set on the harness sinkpad
1163 * Sets the @GstHarness sinkpad caps.
1170 gst_harness_set_sink_caps (GstHarness * h, GstCaps * caps)
1172 GstHarnessPrivate *priv = h->priv;
1174 gst_caps_take (&priv->sink_caps, caps);
1175 gst_pad_push_event (h->sinkpad, gst_event_new_reconfigure ());
1179 * gst_harness_set_caps:
1181 * @in: (transfer full): a #GstCaps to set on the harness srcpad
1182 * @out: (transfer full): a #GstCaps to set on the harness sinkpad
1184 * Sets the @GstHarness srcpad and sinkpad caps.
1191 gst_harness_set_caps (GstHarness * h, GstCaps * in, GstCaps * out)
1193 gst_harness_set_sink_caps (h, out);
1194 gst_harness_set_src_caps (h, in);
1198 * gst_harness_set_src_caps_str:
1200 * @str: a @gchar describing a #GstCaps to set on the harness srcpad
1202 * Sets the @GstHarness srcpad caps using a string. This must be done before
1203 * any buffers can legally be pushed from the harness to the element.
1210 gst_harness_set_src_caps_str (GstHarness * h, const gchar * str)
1212 gst_harness_set_src_caps (h, gst_caps_from_string (str));
1216 * gst_harness_set_sink_caps_str:
1218 * @str: a @gchar describing a #GstCaps to set on the harness sinkpad
1220 * Sets the @GstHarness sinkpad caps using a string.
1227 gst_harness_set_sink_caps_str (GstHarness * h, const gchar * str)
1229 gst_harness_set_sink_caps (h, gst_caps_from_string (str));
1233 * gst_harness_set_caps_str:
1235 * @in: a @gchar describing a #GstCaps to set on the harness srcpad
1236 * @out: a @gchar describing a #GstCaps to set on the harness sinkpad
1238 * Sets the @GstHarness srcpad and sinkpad caps using strings.
1245 gst_harness_set_caps_str (GstHarness * h, const gchar * in, const gchar * out)
1247 gst_harness_set_sink_caps_str (h, out);
1248 gst_harness_set_src_caps_str (h, in);
1252 * gst_harness_use_systemclock:
1255 * Sets the system #GstClock on the @GstHarness #GstElement
1262 gst_harness_use_systemclock (GstHarness * h)
1264 GstClock *clock = gst_system_clock_obtain ();
1265 g_assert (clock != NULL);
1266 gst_element_set_clock (h->element, clock);
1267 gst_object_unref (clock);
1271 * gst_harness_use_testclock:
1274 * Sets the #GstTestClock on the #GstHarness #GstElement
1281 gst_harness_use_testclock (GstHarness * h)
1283 gst_element_set_clock (h->element, GST_CLOCK_CAST (h->priv->testclock));
1287 * gst_harness_get_testclock:
1290 * Get the #GstTestClock. Useful if specific operations on the testclock is
1295 * Returns: (transfer full): a #GstTestClock, or %NULL if the testclock is not
1301 gst_harness_get_testclock (GstHarness * h)
1303 return gst_object_ref (h->priv->testclock);
1307 * gst_harness_set_time:
1309 * @time: a #GstClockTime to advance the clock to
1311 * Advance the #GstTestClock to a specific time.
1315 * Returns: a @gboolean %TRUE if the time could be set. %FALSE if not.
1320 gst_harness_set_time (GstHarness * h, GstClockTime time)
1322 gst_test_clock_set_time (h->priv->testclock, time);
1327 * gst_harness_wait_for_clock_id_waits:
1329 * @waits: a #guint describing the numbers of #GstClockID registered with
1331 * @timeout: a #guint describing how many seconds to wait for @waits to be true
1333 * Waits for @timeout seconds until @waits number of #GstClockID waits is
1334 * registered with the #GstTestClock. Useful for writing deterministic tests,
1335 * where you want to make sure that an expected number of waits have been
1340 * Returns: a @gboolean %TRUE if the waits have been registered, %FALSE if not.
1341 * (Could be that it timed out waiting or that more waits then waits was found)
1346 gst_harness_wait_for_clock_id_waits (GstHarness * h, guint waits, guint timeout)
1348 GstTestClock *testclock = h->priv->testclock;
1352 start_time = g_get_monotonic_time ();
1353 while (gst_test_clock_peek_id_count (testclock) < waits) {
1356 g_usleep (G_USEC_PER_SEC / 1000);
1357 time_spent = g_get_monotonic_time () - start_time;
1358 if ((time_spent / G_USEC_PER_SEC) > timeout)
1362 ret = (waits == gst_test_clock_peek_id_count (testclock));
1368 * gst_harness_crank_single_clock_wait:
1371 * A "crank" consists of three steps:
1372 * 1: Wait for a #GstClockID to be registered with the #GstTestClock.
1373 * 2: Advance the #GstTestClock to the time the #GstClockID is waiting for.
1374 * 3: Release the #GstClockID wait.
1375 * Together, this provides an easy way to not have to think about the details
1376 * around clocks and time, but still being able to write deterministic tests
1377 * that are dependant on this. A "crank" can be though of as the notion of
1378 * manually driving the clock forward to its next logical step.
1382 * Returns: a @gboolean %TRUE if the "crank" was successful, %FALSE if not.
1387 gst_harness_crank_single_clock_wait (GstHarness * h)
1389 return gst_test_clock_crank (h->priv->testclock);
1393 * gst_harness_crank_multiple_clock_waits:
1395 * @waits: a #guint describing the number of #GstClockIDs to crank
1397 * Similar to gst_harness_crank_single_clock_wait(), this is the function to use
1398 * if your harnessed element(s) are using more then one gst_clock_id_wait.
1399 * Failing to do so can (and will) make it racy which #GstClockID you actually
1400 * are releasing, where as this function will process all the waits at the
1401 * same time, ensuring that one thread can't register another wait before
1402 * both are released.
1406 * Returns: a @gboolean %TRUE if the "crank" was successful, %FALSE if not.
1411 gst_harness_crank_multiple_clock_waits (GstHarness * h, guint waits)
1413 GstTestClock *testclock = h->priv->testclock;
1417 gst_test_clock_wait_for_multiple_pending_ids (testclock, waits, &pending);
1418 gst_harness_set_time (h, gst_test_clock_id_list_get_latest_time (pending));
1419 processed = gst_test_clock_process_id_list (testclock, pending);
1421 g_list_free_full (pending, gst_clock_id_unref);
1422 return processed == waits;
1429 * This will set the harnessed #GstElement to %GST_STATE_PLAYING.
1430 * #GstElements without a sink-#GstPad and with the %GST_ELEMENT_FLAG_SOURCE
1431 * flag set is concidered a src #GstElement
1432 * Non-src #GstElements (like sinks and filters) are automatically set to
1433 * playing by the #GstHarness, but src #GstElements are not to avoid them
1434 * starting to produce buffers.
1435 * Hence, for src #GstElement you must call gst_harness_play() explicitly.
1442 gst_harness_play (GstHarness * h)
1444 GstState state, pending;
1445 gboolean state_change;
1446 state_change = gst_element_set_state (h->element, GST_STATE_PLAYING);
1447 g_assert_cmpint (GST_STATE_CHANGE_SUCCESS, ==, state_change);
1448 state_change = gst_element_get_state (h->element, &state, &pending, 0);
1449 g_assert_cmpint (GST_STATE_CHANGE_SUCCESS, ==, state_change);
1450 g_assert_cmpint (GST_STATE_PLAYING, ==, state);
1454 * gst_harness_set_blocking_push_mode:
1457 * Setting this will make the harness block in the chain-function, and
1458 * then release when gst_harness_pull() or gst_harness_try_pull() is called.
1459 * Can be useful when wanting to control a src-element that is not implementing
1460 * gst_clock_id_wait() so it can't be controlled by the #GstTestClock, since
1461 * it otherwise would produce buffers as fast as possible.
1468 gst_harness_set_blocking_push_mode (GstHarness * h)
1470 GstHarnessPrivate *priv = h->priv;
1471 priv->blocking_push_mode = TRUE;
1475 * gst_harness_set_forwarding:
1477 * @forwarding: a #gboolean to enable/disable forwarding
1479 * As a convenience, a src-harness will forward %GST_EVENT_STREAM_START,
1480 * %GST_EVENT_CAPS and %GST_EVENT_SEGMENT to the main-harness if forwarding
1481 * is enabled, and forward any sticky-events from the main-harness to
1482 * the sink-harness. It will also forward the %GST_QUERY_ALLOCATION.
1484 * If forwarding is disabled, the user will have to either manually push
1485 * these events from the src-harness using gst_harness_src_push_event(), or
1486 * create and push them manually. While this will allow full control and
1487 * inspection of these events, for the most cases having forwarding enabled
1488 * will be sufficient when writing a test where the src-harness' main function
1489 * is providing data for the main-harness.
1491 * Forwarding is enabled by default.
1498 gst_harness_set_forwarding (GstHarness * h, gboolean forwarding)
1500 GstHarnessPrivate *priv = h->priv;
1501 priv->forwarding = forwarding;
1503 gst_harness_set_forwarding (h->src_harness, forwarding);
1504 if (h->sink_harness)
1505 gst_harness_set_forwarding (h->sink_harness, forwarding);
1509 gst_harness_set_forward_pad (GstHarness * h, GstPad * fwdpad)
1512 gst_object_replace ((GstObject **) & h->priv->sink_forward_pad,
1513 (GstObject *) fwdpad);
1518 * gst_harness_create_buffer:
1520 * @size: a #gsize specifying the size of the buffer
1522 * Allocates a buffer using a #GstBufferPool if present, or else using the
1523 * configured #GstAllocator and #GstAllocationParams
1527 * Returns: a #GstBuffer of size @size
1532 gst_harness_create_buffer (GstHarness * h, gsize size)
1534 GstHarnessPrivate *priv = h->priv;
1535 GstBuffer *ret = NULL;
1538 if (gst_pad_check_reconfigure (h->srcpad))
1539 gst_harness_negotiate (h);
1542 flow = gst_buffer_pool_acquire_buffer (priv->pool, &ret, NULL);
1543 g_assert_cmpint (flow, ==, GST_FLOW_OK);
1544 if (gst_buffer_get_size (ret) != size) {
1545 GST_DEBUG_OBJECT (h,
1546 "use fallback, pool is configured with a different size (%zu != %zu)",
1547 size, gst_buffer_get_size (ret));
1548 gst_buffer_unref (ret);
1555 gst_buffer_new_allocate (priv->allocator, size,
1556 &priv->allocation_params);
1558 g_assert (ret != NULL);
1565 * @buffer: a #GstBuffer to push
1567 * Pushes a #GstBuffer on the #GstHarness srcpad. The standard way of
1568 * interacting with an harnessed element.
1572 * Returns: a #GstFlowReturn with the result from the push
1577 gst_harness_push (GstHarness * h, GstBuffer * buffer)
1579 GstHarnessPrivate *priv = h->priv;
1580 g_assert (buffer != NULL);
1581 priv->last_push_ts = GST_BUFFER_TIMESTAMP (buffer);
1582 return gst_pad_push (h->srcpad, buffer);
1589 * Pulls a #GstBuffer from the #GAsyncQueue on the #GstHarness sinkpad. The pull
1590 * will timeout in 60 seconds. This is the standard way of getting a buffer
1591 * from a harnessed #GstElement.
1595 * Returns: a #GstBuffer or %NULL if timed out.
1600 gst_harness_pull (GstHarness * h)
1602 GstHarnessPrivate *priv = h->priv;
1603 GstBuffer *buf = (GstBuffer *) g_async_queue_timeout_pop (priv->buffer_queue,
1604 G_USEC_PER_SEC * 60);
1606 if (priv->blocking_push_mode) {
1607 g_mutex_lock (&priv->blocking_push_mutex);
1608 g_cond_signal (&priv->blocking_push_cond);
1609 g_mutex_unlock (&priv->blocking_push_mutex);
1616 * gst_harness_try_pull:
1619 * Pulls a #GstBuffer from the #GAsyncQueue on the #GstHarness sinkpad. Unlike
1620 * gst_harness_pull this will not wait for any buffers if not any are present,
1621 * and return %NULL straight away.
1625 * Returns: a #GstBuffer or %NULL if no buffers are present in the #GAsyncQueue
1630 gst_harness_try_pull (GstHarness * h)
1632 GstHarnessPrivate *priv = h->priv;
1633 GstBuffer *buf = (GstBuffer *) g_async_queue_try_pop (priv->buffer_queue);
1635 if (priv->blocking_push_mode) {
1636 g_mutex_lock (&priv->blocking_push_mutex);
1637 g_cond_signal (&priv->blocking_push_cond);
1638 g_mutex_unlock (&priv->blocking_push_mutex);
1645 * gst_harness_push_and_pull:
1647 * @buffer: a #GstBuffer to push
1649 * Basically a gst_harness_push and a gst_harness_pull in one line. Reflects
1650 * the fact that you often want to do exactly this in your test: Push one buffer
1651 * in, and inspect the outcome.
1655 * Returns: a #GstBuffer or %NULL if timed out.
1660 gst_harness_push_and_pull (GstHarness * h, GstBuffer * buffer)
1662 gst_harness_push (h, buffer);
1663 return gst_harness_pull (h);
1667 * gst_harness_buffers_received:
1670 * The total number of #GstBuffers that has arrived on the #GstHarness sinkpad.
1671 * This number includes buffers that have been dropped as well as buffers
1672 * that have already been pulled out.
1676 * Returns: a #guint number of buffers received
1681 gst_harness_buffers_received (GstHarness * h)
1683 GstHarnessPrivate *priv = h->priv;
1684 return g_atomic_int_get (&priv->recv_buffers);
1688 * gst_harness_buffers_in_queue:
1691 * The number of #GstBuffers currently in the #GstHarness sinkpad #GAsyncQueue
1695 * Returns: a #guint number of buffers in the queue
1700 gst_harness_buffers_in_queue (GstHarness * h)
1702 GstHarnessPrivate *priv = h->priv;
1703 return g_async_queue_length (priv->buffer_queue);
1707 * gst_harness_set_drop_buffers:
1709 * @drop_buffers: a #gboolean specifying to drop outgoing buffers or not
1711 * When set to %TRUE, instead of placing the buffers arriving from the harnessed
1712 * #GstElement inside the sinkpads #GAsyncQueue, they are instead unreffed.
1719 gst_harness_set_drop_buffers (GstHarness * h, gboolean drop_buffers)
1721 GstHarnessPrivate *priv = h->priv;
1722 priv->drop_buffers = drop_buffers;
1726 * gst_harness_dump_to_file:
1728 * @filename: a #gchar with a the name of a file
1730 * Allows you to dump the #GstBuffers the #GstHarness sinkpad #GAsyncQueue
1738 gst_harness_dump_to_file (GstHarness * h, const gchar * filename)
1740 GstHarnessPrivate *priv = h->priv;
1743 fd = fopen (filename, "wb");
1746 while ((buf = g_async_queue_try_pop (priv->buffer_queue))) {
1748 gst_buffer_map (buf, &info, GST_MAP_READ);
1749 fwrite (info.data, 1, info.size, fd);
1750 gst_buffer_unmap (buf, &info);
1751 gst_buffer_unref (buf);
1759 * gst_harness_get_last_pushed_timestamp:
1762 * Get the timestamp of the last #GstBuffer pushed on the #GstHarness srcpad,
1763 * typically with gst_harness_push or gst_harness_push_from_src.
1767 * Returns: a #GstClockTime with the timestamp or %GST_CLOCK_TIME_NONE if no
1768 * #GstBuffer has been pushed on the #GstHarness srcpad
1773 gst_harness_get_last_pushed_timestamp (GstHarness * h)
1775 GstHarnessPrivate *priv = h->priv;
1776 return priv->last_push_ts;
1780 * gst_harness_push_event:
1782 * @event: a #GstEvent to push
1784 * Pushes an #GstEvent on the #GstHarness srcpad.
1788 * Returns: a #gboolean with the result from the push
1793 gst_harness_push_event (GstHarness * h, GstEvent * event)
1795 return gst_pad_push_event (h->srcpad, event);
1799 * gst_harness_pull_event:
1802 * Pulls an #GstEvent from the #GAsyncQueue on the #GstHarness sinkpad.
1803 * Timeouts after 60 seconds similar to gst_harness_pull.
1807 * Returns: a #GstEvent or %NULL if timed out.
1812 gst_harness_pull_event (GstHarness * h)
1814 GstHarnessPrivate *priv = h->priv;
1815 return (GstEvent *) g_async_queue_timeout_pop (priv->sink_event_queue,
1816 G_USEC_PER_SEC * 60);
1820 * gst_harness_try_pull_event:
1823 * Pulls an #GstEvent from the #GAsyncQueue on the #GstHarness sinkpad.
1824 * See gst_harness_try_pull for details.
1828 * Returns: a #GstEvent or %NULL if no buffers are present in the #GAsyncQueue
1833 gst_harness_try_pull_event (GstHarness * h)
1835 GstHarnessPrivate *priv = h->priv;
1836 return (GstEvent *) g_async_queue_try_pop (priv->sink_event_queue);
1840 * gst_harness_events_received:
1843 * The total number of #GstEvents that has arrived on the #GstHarness sinkpad
1844 * This number includes events handled by the harness as well as events
1845 * that have already been pulled out.
1849 * Returns: a #guint number of events received
1854 gst_harness_events_received (GstHarness * h)
1856 GstHarnessPrivate *priv = h->priv;
1857 return g_atomic_int_get (&priv->recv_events);
1861 * gst_harness_events_in_queue:
1864 * The number of #GstEvents currently in the #GstHarness sinkpad #GAsyncQueue
1868 * Returns: a #guint number of events in the queue
1873 gst_harness_events_in_queue (GstHarness * h)
1875 GstHarnessPrivate *priv = h->priv;
1876 return g_async_queue_length (priv->sink_event_queue);
1880 * gst_harness_push_upstream_event:
1882 * @event: a #GstEvent to push
1884 * Pushes an #GstEvent on the #GstHarness sinkpad.
1888 * Returns: a #gboolean with the result from the push
1893 gst_harness_push_upstream_event (GstHarness * h, GstEvent * event)
1895 g_return_val_if_fail (event != NULL, FALSE);
1896 g_return_val_if_fail (GST_EVENT_IS_UPSTREAM (event), FALSE);
1898 return gst_pad_push_event (h->sinkpad, event);
1902 * gst_harness_pull_upstream_event:
1905 * Pulls an #GstEvent from the #GAsyncQueue on the #GstHarness srcpad.
1906 * Timeouts after 60 seconds similar to gst_harness_pull.
1910 * Returns: a #GstEvent or %NULL if timed out.
1915 gst_harness_pull_upstream_event (GstHarness * h)
1917 GstHarnessPrivate *priv = h->priv;
1918 return (GstEvent *) g_async_queue_timeout_pop (priv->src_event_queue,
1919 G_USEC_PER_SEC * 60);
1923 * gst_harness_try_pull_upstream_event:
1926 * Pulls an #GstEvent from the #GAsyncQueue on the #GstHarness srcpad.
1927 * See gst_harness_try_pull for details.
1931 * Returns: a #GstEvent or %NULL if no buffers are present in the #GAsyncQueue
1936 gst_harness_try_pull_upstream_event (GstHarness * h)
1938 GstHarnessPrivate *priv = h->priv;
1939 return (GstEvent *) g_async_queue_try_pop (priv->src_event_queue);
1943 * gst_harness_upstream_events_received:
1946 * The total number of #GstEvents that has arrived on the #GstHarness srcpad
1947 * This number includes events handled by the harness as well as events
1948 * that have already been pulled out.
1952 * Returns: a #guint number of events received
1957 gst_harness_upstream_events_received (GstHarness * h)
1959 GstHarnessPrivate *priv = h->priv;
1960 return g_atomic_int_get (&priv->recv_upstream_events);
1964 * gst_harness_upstream_events_in_queue:
1967 * The number of #GstEvents currently in the #GstHarness srcpad #GAsyncQueue
1971 * Returns: a #guint number of events in the queue
1976 gst_harness_upstream_events_in_queue (GstHarness * h)
1978 GstHarnessPrivate *priv = h->priv;
1979 return g_async_queue_length (priv->src_event_queue);
1983 * gst_harness_query_latency:
1986 * Get the min latency reported by any harnessed #GstElement.
1990 * Returns: a #GstClockTime with min latency
1995 gst_harness_query_latency (GstHarness * h)
1999 GstClockTime min = GST_CLOCK_TIME_NONE;
2002 query = gst_query_new_latency ();
2004 if (gst_pad_peer_query (h->sinkpad, query)) {
2005 gst_query_parse_latency (query, &is_live, &min, &max);
2007 gst_query_unref (query);
2013 * gst_harness_set_upstream_latency:
2015 * @latency: a #GstClockTime specifying the latency
2017 * Sets the min latency reported by #GstHarness when receiving a latency-query
2022 gst_harness_set_upstream_latency (GstHarness * h, GstClockTime latency)
2024 GstHarnessPrivate *priv = h->priv;
2025 priv->latency_min = latency;
2029 * gst_harness_get_allocator:
2031 * @allocator: (out) (allow-none) (transfer none): the #GstAllocator used
2032 * @params: (out) (allow-none) (transfer full): the #GstAllocationParams of
2035 * Gets the @allocator and its @params that has been decided to use after an
2043 gst_harness_get_allocator (GstHarness * h, GstAllocator ** allocator,
2044 GstAllocationParams * params)
2046 GstHarnessPrivate *priv = h->priv;
2048 *allocator = priv->allocator;
2050 *params = priv->allocation_params;
2055 * gst_harness_set_propose_allocator:
2057 * @allocator: (allow-none) (transfer full): a #GstAllocator
2058 * @params: (allow-none) (transfer none): a #GstAllocationParams
2060 * Sets the @allocator and @params to propose when receiving an allocation
2068 gst_harness_set_propose_allocator (GstHarness * h, GstAllocator * allocator,
2069 const GstAllocationParams * params)
2071 GstHarnessPrivate *priv = h->priv;
2073 priv->propose_allocator = allocator;
2075 priv->propose_allocation_params = *params;
2079 * gst_harness_add_src_harness:
2081 * @src_harness: (transfer full): a #GstHarness to be added as a src-harness.
2082 * @has_clock_wait: a #gboolean specifying if the #GstElement uses
2083 * gst_clock_wait_id internally.
2085 * A src-harness is a great way of providing the #GstHarness with data.
2086 * By adding a src-type #GstElement, it is then easy to use functions like
2087 * gst_harness_push_from_src or gst_harness_src_crank_and_push_many
2088 * to provide your harnessed element with input. The @has_clock_wait variable
2089 * is a greate way to control you src-element with, in that you can have it
2090 * produce a buffer for you by simply cranking the clock, and not have it
2091 * spin out of control producing buffers as fast as possible.
2093 * If a src-harness already exists it will be replaced.
2100 gst_harness_add_src_harness (GstHarness * h,
2101 GstHarness * src_harness, gboolean has_clock_wait)
2104 gst_harness_teardown (h->src_harness);
2105 h->src_harness = src_harness;
2106 gst_harness_set_forward_pad (h->src_harness, h->srcpad);
2107 h->src_harness->priv->has_clock_wait = has_clock_wait;
2108 gst_harness_set_forwarding (h->src_harness, h->priv->forwarding);
2112 * gst_harness_add_src:
2114 * @src_element_name: a #gchar with the name of a #GstElement
2115 * @has_clock_wait: a #gboolean specifying if the #GstElement uses
2116 * gst_clock_wait_id internally.
2118 * Similar to gst_harness_add_src_harness, this is a convenience to
2119 * directly create a src-harness using the @src_element_name name specified.
2126 gst_harness_add_src (GstHarness * h,
2127 const gchar * src_element_name, gboolean has_clock_wait)
2129 GstHarness *src_harness = gst_harness_new (src_element_name);
2130 gst_harness_add_src_harness (h, src_harness, has_clock_wait);
2134 * gst_harness_add_src_parse:
2136 * @launchline: a #gchar describing a gst-launch type line
2137 * @has_clock_wait: a #gboolean specifying if the #GstElement uses
2138 * gst_clock_wait_id internally.
2140 * Similar to gst_harness_add_src, this allows you to specify a launch-line,
2141 * which can be useful for both having more then one #GstElement acting as your
2142 * src (Like a src producing raw buffers, and then an encoder, providing encoded
2143 * data), but also by allowing you to set properties like "is-live" directly on
2151 gst_harness_add_src_parse (GstHarness * h,
2152 const gchar * launchline, gboolean has_clock_wait)
2154 GstHarness *src_harness = gst_harness_new_parse (launchline);
2155 gst_harness_add_src_harness (h, src_harness, has_clock_wait);
2159 * gst_harness_push_from_src:
2162 * Transfer data from the src-#GstHarness to the main-#GstHarness. It consists
2164 * 1: Make sure the src is started. (see: gst_harness_play)
2165 * 2: Crank the clock (see: gst_harness_crank_single_clock_wait)
2166 * 3: Pull a #GstBuffer from the src-#GstHarness (see: gst_harness_pull)
2167 * 4: Push the same #GstBuffer into the main-#GstHarness (see: gst_harness_push)
2171 * Returns: a #GstFlowReturn with the result of the push
2176 gst_harness_push_from_src (GstHarness * h)
2181 g_assert (h->src_harness);
2183 /* FIXME: this *is* the right time to start the src,
2184 but maybe a flag so we don't keep telling it to play? */
2185 gst_harness_play (h->src_harness);
2187 if (h->src_harness->priv->has_clock_wait) {
2188 crank = gst_harness_crank_single_clock_wait (h->src_harness);
2192 buf = gst_harness_pull (h->src_harness);
2193 g_assert (buf != NULL);
2194 return gst_harness_push (h, buf);
2198 * gst_harness_src_crank_and_push_many:
2200 * @cranks: a #gint with the number of calls to gst_harness_crank_single_clock_wait
2201 * @pushes: a #gint with the number of calls to gst_harness_push
2203 * Transfer data from the src-#GstHarness to the main-#GstHarness. Similar to
2204 * gst_harness_push_from_src, this variant allows you to specify how many cranks
2205 * and how many pushes to perform. This can be useful for both moving a lot
2206 * of data at the same time, as well as cases when one crank does not equal one
2207 * buffer to push and v.v.
2211 * Returns: a #GstFlowReturn with the result of the push
2216 gst_harness_src_crank_and_push_many (GstHarness * h, gint cranks, gint pushes)
2218 GstFlowReturn ret = GST_FLOW_OK;
2222 g_assert (h->src_harness);
2223 gst_harness_play (h->src_harness);
2225 for (i = 0; i < cranks; i++) {
2226 crank = gst_harness_crank_single_clock_wait (h->src_harness);
2230 for (i = 0; i < pushes; i++) {
2232 buf = gst_harness_pull (h->src_harness);
2233 g_assert (buf != NULL);
2234 ret = gst_harness_push (h, buf);
2235 if (ret != GST_FLOW_OK)
2243 * gst_harness_src_push_event:
2246 * Similar to what gst_harness_src_push does with #GstBuffers, this transfers
2247 * a #GstEvent from the src-#GstHarness to the main-#GstHarness. Note that
2248 * some #GstEvents are being transferred automagically. Look at sink_forward_pad
2253 * Returns: a #gboolean with the result of the push
2258 gst_harness_src_push_event (GstHarness * h)
2260 return gst_harness_push_event (h, gst_harness_pull_event (h->src_harness));
2265 forward_sticky_events (GstPad * pad, GstEvent ** ev, gpointer user_data)
2267 GstHarness *h = user_data;
2268 return gst_pad_push_event (h->priv->sink_forward_pad, gst_event_ref (*ev));
2272 * gst_harness_add_sink_harness:
2274 * @sink_harness: (transfer full): a #GstHarness to be added as a sink-harness.
2276 * Similar to gst_harness_add_src, this allows you to send the data coming out
2277 * of your harnessed #GstElement to a sink-element, allowing to test different
2278 * responses the element output might create in sink elements. An example might
2279 * be an existing sink providing some analytical data on the input it receives that
2280 * can be useful to your testing. If the goal is to test a sink-element itself,
2281 * this is better acheived using gst_harness_new directly on the sink.
2283 * If a sink-harness already exists it will be replaced.
2290 gst_harness_add_sink_harness (GstHarness * h, GstHarness * sink_harness)
2292 GstHarnessPrivate *priv = h->priv;
2294 if (h->sink_harness) {
2295 gst_harness_set_forward_pad (h, NULL);
2296 gst_harness_teardown (h->sink_harness);
2298 h->sink_harness = sink_harness;
2299 gst_harness_set_forward_pad (h, h->sink_harness->srcpad);
2300 if (priv->forwarding && h->sinkpad)
2301 gst_pad_sticky_events_foreach (h->sinkpad, forward_sticky_events, h);
2302 gst_harness_set_forwarding (h->sink_harness, priv->forwarding);
2306 * gst_harness_add_sink:
2308 * @sink_element_name: a #gchar with the name of a #GstElement
2310 * Similar to gst_harness_add_sink_harness, this is a convenience to
2311 * directly create a sink-harness using the @sink_element_name name specified.
2318 gst_harness_add_sink (GstHarness * h, const gchar * sink_element_name)
2320 GstHarness *sink_harness = gst_harness_new (sink_element_name);
2321 gst_harness_add_sink_harness (h, sink_harness);
2325 * gst_harness_add_sink_parse:
2327 * @launchline: a #gchar with the name of a #GstElement
2329 * Similar to gst_harness_add_sink, this allows you to specify a launch-line
2330 * instead of just an element name. See gst_harness_add_src_parse for details.
2337 gst_harness_add_sink_parse (GstHarness * h, const gchar * launchline)
2339 GstHarness *sink_harness = gst_harness_new_parse (launchline);
2340 gst_harness_add_sink_harness (h, sink_harness);
2344 * gst_harness_push_to_sink:
2347 * Transfer one #GstBuffer from the main-#GstHarness to the sink-#GstHarness.
2348 * See gst_harness_push_from_src for details.
2352 * Returns: a #GstFlowReturn with the result of the push
2357 gst_harness_push_to_sink (GstHarness * h)
2360 g_assert (h->sink_harness);
2361 buf = gst_harness_pull (h);
2362 g_assert (buf != NULL);
2363 return gst_harness_push (h->sink_harness, buf);
2367 * gst_harness_sink_push_many:
2369 * @pushes: a #gint with the number of calls to gst_harness_push_to_sink
2371 * Convenience that calls gst_harness_push_to_sink @pushes number of times.
2372 * Will abort the pushing if any one push fails.
2376 * Returns: a #GstFlowReturn with the result of the push
2381 gst_harness_sink_push_many (GstHarness * h, gint pushes)
2383 GstFlowReturn ret = GST_FLOW_OK;
2385 g_assert (h->sink_harness);
2386 for (i = 0; i < pushes; i++) {
2387 ret = gst_harness_push_to_sink (h);
2388 if (ret != GST_FLOW_OK)
2395 * gst_harness_find_element:
2397 * @element_name: a #gchar with a #GstElementFactory name
2399 * Most useful in conjunction with gst_harness_new_parse, this will scan the
2400 * #GstElements inside the #GstHarness, and check if any of them matches
2401 * @element_name. Typical usecase being that you need to access one of the
2402 * harnessed elements for properties and/or signals.
2406 * Returns: (transfer full) (allow-none): a #GstElement or %NULL if not found
2411 gst_harness_find_element (GstHarness * h, const gchar * element_name)
2413 gboolean done = FALSE;
2415 GValue data = G_VALUE_INIT;
2417 iter = gst_bin_iterate_elements (GST_BIN (h->element));
2421 switch (gst_iterator_next (iter, &data)) {
2422 case GST_ITERATOR_OK:
2424 GstElement *element = g_value_get_object (&data);
2425 GstPluginFeature *feature =
2426 GST_PLUGIN_FEATURE (gst_element_get_factory (element));
2427 if (!strcmp (element_name, gst_plugin_feature_get_name (feature))) {
2428 gst_iterator_free (iter);
2431 g_value_reset (&data);
2434 case GST_ITERATOR_RESYNC:
2435 gst_iterator_resync (iter);
2437 case GST_ITERATOR_ERROR:
2438 case GST_ITERATOR_DONE:
2443 gst_iterator_free (iter);
2451 * @element_name: a #gchar with a #GstElementFactory name
2452 * @first_property_name: a #gchar with the first property name
2453 * @...: value for the first property, followed optionally by more
2454 * name/value pairs, followed by %NULL
2456 * A convenience function to allows you to call g_object_set on a #GstElement
2457 * that are residing inside the #GstHarness, by using normal g_object_set
2465 gst_harness_set (GstHarness * h,
2466 const gchar * element_name, const gchar * first_property_name, ...)
2469 GstElement *element = gst_harness_find_element (h, element_name);
2470 va_start (var_args, first_property_name);
2471 g_object_set_valist (G_OBJECT (element), first_property_name, var_args);
2473 gst_object_unref (element);
2479 * @element_name: a #gchar with a #GstElementFactory name
2480 * @first_property_name: a #gchar with the first property name
2481 * @...: return location for the first property, followed optionally by more
2482 * name/return location pairs, followed by %NULL
2484 * A convenience function to allows you to call g_object_get on a #GstElement
2485 * that are residing inside the #GstHarness, by using normal g_object_get
2493 gst_harness_get (GstHarness * h,
2494 const gchar * element_name, const gchar * first_property_name, ...)
2497 GstElement *element = gst_harness_find_element (h, element_name);
2498 va_start (var_args, first_property_name);
2499 g_object_get_valist (G_OBJECT (element), first_property_name, var_args);
2501 gst_object_unref (element);
2505 * gst_harness_add_probe:
2507 * @element_name: a #gchar with a #GstElementFactory name
2508 * @pad_name: a #gchar with the name of the pad to attach the probe to
2509 * @mask: a #GstPadProbeType (see gst_pad_add_probe)
2510 * @callback: a #GstPadProbeCallback (see gst_pad_add_probe)
2511 * @user_data: a #gpointer (see gst_pad_add_probe)
2512 * @destroy_data: a #GDestroyNotify (see gst_pad_add_probe)
2514 * A convenience function to allows you to call gst_pad_add_probe on a
2515 * #GstPad of a #GstElement that are residing inside the #GstHarness,
2516 * by using normal gst_pad_add_probe syntax
2523 gst_harness_add_probe (GstHarness * h,
2524 const gchar * element_name, const gchar * pad_name, GstPadProbeType mask,
2525 GstPadProbeCallback callback, gpointer user_data,
2526 GDestroyNotify destroy_data)
2528 GstElement *element = gst_harness_find_element (h, element_name);
2529 GstPad *pad = gst_element_get_static_pad (element, pad_name);
2530 gst_pad_add_probe (pad, mask, callback, user_data, destroy_data);
2531 gst_object_unref (pad);
2532 gst_object_unref (element);
2535 /******************************************************************************/
2537 /******************************************************************************/
2538 struct _GstHarnessThread
2546 GDestroyNotify freefunc;
2556 } GstHarnessCustomThread;
2564 GstHarnessPrepareBufferFunc func;
2566 GDestroyNotify notify;
2567 } GstHarnessPushBufferThread;
2573 GstHarnessPrepareEventFunc func;
2575 GDestroyNotify notify;
2576 } GstHarnessPushEventThread;
2584 } GstHarnessPropThread;
2590 GstPadTemplate *templ;
2596 } GstHarnessReqPadThread;
2599 gst_harness_thread_init (GstHarnessThread * t, GDestroyNotify freefunc,
2600 GstHarness * h, gulong sleep)
2602 t->freefunc = freefunc;
2606 g_ptr_array_add (h->priv->stress, t);
2610 gst_harness_thread_free (GstHarnessThread * t)
2612 g_slice_free (GstHarnessThread, t);
2616 gst_harness_custom_thread_free (GstHarnessCustomThread * t)
2618 g_slice_free (GstHarnessCustomThread, t);
2622 gst_harness_push_buffer_thread_free (GstHarnessPushBufferThread * t)
2625 gst_caps_replace (&t->caps, NULL);
2626 if (t->notify != NULL)
2627 t->notify (t->data);
2628 g_slice_free (GstHarnessPushBufferThread, t);
2633 gst_harness_push_event_thread_free (GstHarnessPushEventThread * t)
2636 if (t->notify != NULL)
2637 t->notify (t->data);
2638 g_slice_free (GstHarnessPushEventThread, t);
2643 gst_harness_property_thread_free (GstHarnessPropThread * t)
2647 g_value_unset (&t->value);
2648 g_slice_free (GstHarnessPropThread, t);
2653 gst_harness_requestpad_release (GstPad * pad, GstElement * element)
2655 gst_element_release_request_pad (element, pad);
2656 gst_object_unref (pad);
2660 gst_harness_requestpad_release_pads (GstHarnessReqPadThread * rpt)
2662 g_slist_foreach (rpt->pads, (GFunc) gst_harness_requestpad_release,
2664 g_slist_free (rpt->pads);
2669 gst_harness_requestpad_thread_free (GstHarnessReqPadThread * t)
2672 gst_object_replace ((GstObject **) & t->templ, NULL);
2674 gst_caps_replace (&t->caps, NULL);
2676 gst_harness_requestpad_release_pads (t);
2677 g_slice_free (GstHarnessReqPadThread, t);
2681 #define GST_HARNESS_THREAD_START(ID, t) \
2682 (((GstHarnessThread *)t)->running = TRUE, \
2683 ((GstHarnessThread *)t)->thread = g_thread_new ( \
2684 "gst-harness-stress-"G_STRINGIFY(ID), \
2685 (GThreadFunc)gst_harness_stress_##ID##_func, t))
2686 #define GST_HARNESS_THREAD_END(t) \
2687 (t->running = FALSE, \
2688 GPOINTER_TO_UINT (g_thread_join (t->thread)))
2691 gst_harness_stress_free (GstHarnessThread * t)
2693 if (t != NULL && t->freefunc != NULL)
2698 gst_harness_stress_custom_func (GstHarnessThread * t)
2700 GstHarnessCustomThread *ct = (GstHarnessCustomThread *) t;
2703 if (ct->init != NULL)
2704 ct->init (ct, ct->data);
2706 while (t->running) {
2707 ct->callback (ct, ct->data);
2710 g_usleep (t->sleep);
2712 return GUINT_TO_POINTER (count);
2717 gst_harness_stress_statechange_func (GstHarnessThread * t)
2721 while (t->running) {
2722 GstClock *clock = gst_element_get_clock (t->h->element);
2724 gboolean done = FALSE;
2727 change = gst_element_set_state (t->h->element, GST_STATE_NULL);
2728 g_assert (change == GST_STATE_CHANGE_SUCCESS);
2731 it = gst_element_iterate_sink_pads (t->h->element);
2733 GValue item = G_VALUE_INIT;
2734 switch (gst_iterator_next (it, &item)) {
2735 case GST_ITERATOR_OK:
2737 GstPad *sinkpad = g_value_get_object (&item);
2738 GstPad *srcpad = gst_pad_get_peer (sinkpad);
2739 if (srcpad != NULL) {
2740 gst_pad_unlink (srcpad, sinkpad);
2741 gst_pad_link (srcpad, sinkpad);
2742 gst_object_unref (srcpad);
2744 g_value_reset (&item);
2747 case GST_ITERATOR_RESYNC:
2748 gst_iterator_resync (it);
2750 case GST_ITERATOR_ERROR:
2751 g_assert_not_reached ();
2752 case GST_ITERATOR_DONE:
2756 g_value_unset (&item);
2758 gst_iterator_free (it);
2760 if (clock != NULL) {
2761 gst_element_set_clock (t->h->element, clock);
2762 gst_object_unref (clock);
2764 change = gst_element_set_state (t->h->element, GST_STATE_PLAYING);
2765 g_assert (change == GST_STATE_CHANGE_SUCCESS);
2768 g_usleep (t->sleep);
2770 return GUINT_TO_POINTER (count);
2774 gst_harness_stress_buffer_func (GstHarnessThread * t)
2776 GstHarnessPushBufferThread *pt = (GstHarnessPushBufferThread *) t;
2781 /* Push stream start, caps and segment events */
2782 sid = g_strdup_printf ("%s-%p", GST_OBJECT_NAME (t->h->element), t->h);
2783 handled = gst_pad_push_event (t->h->srcpad, gst_event_new_stream_start (sid));
2786 handled = gst_pad_push_event (t->h->srcpad, gst_event_new_caps (pt->caps));
2788 handled = gst_pad_push_event (t->h->srcpad,
2789 gst_event_new_segment (&pt->segment));
2792 while (t->running) {
2793 gst_harness_push (t->h, pt->func (t->h, pt->data));
2796 g_usleep (t->sleep);
2798 return GUINT_TO_POINTER (count);
2802 gst_harness_stress_event_func (GstHarnessThread * t)
2804 GstHarnessPushEventThread *pet = (GstHarnessPushEventThread *) t;
2807 while (t->running) {
2808 gst_harness_push_event (t->h, pet->func (t->h, pet->data));
2811 g_usleep (t->sleep);
2813 return GUINT_TO_POINTER (count);
2817 gst_harness_stress_upstream_event_func (GstHarnessThread * t)
2819 GstHarnessPushEventThread *pet = (GstHarnessPushEventThread *) t;
2822 while (t->running) {
2823 gst_harness_push_upstream_event (t->h, pet->func (t->h, pet->data));
2826 g_usleep (t->sleep);
2828 return GUINT_TO_POINTER (count);
2832 gst_harness_stress_property_func (GstHarnessThread * t)
2834 GstHarnessPropThread *pt = (GstHarnessPropThread *) t;
2837 while (t->running) {
2838 GValue value = G_VALUE_INIT;
2840 g_object_set_property (G_OBJECT (t->h->element), pt->name, &pt->value);
2842 g_value_init (&value, G_VALUE_TYPE (&pt->value));
2843 g_object_get_property (G_OBJECT (t->h->element), pt->name, &value);
2844 g_value_reset (&value);
2847 g_usleep (t->sleep);
2849 return GUINT_TO_POINTER (count);
2853 gst_harness_stress_requestpad_func (GstHarnessThread * t)
2855 GstHarnessReqPadThread *rpt = (GstHarnessReqPadThread *) t;
2858 while (t->running) {
2862 gst_harness_requestpad_release_pads (rpt);
2866 reqpad = gst_element_request_pad (t->h->element,
2867 rpt->templ, rpt->name, rpt->caps);
2869 g_assert (reqpad != NULL);
2871 rpt->pads = g_slist_prepend (rpt->pads, reqpad);
2874 g_usleep (t->sleep);
2876 return GUINT_TO_POINTER (count);
2880 * gst_harness_stress_thread_stop:
2881 * @t: a #GstHarnessThread
2883 * Stop the running #GstHarnessThread
2890 gst_harness_stress_thread_stop (GstHarnessThread * t)
2894 g_return_val_if_fail (t != NULL, 0);
2896 ret = GST_HARNESS_THREAD_END (t);
2897 g_ptr_array_remove (t->h->priv->stress, t);
2902 * gst_harness_stress_custom_start: (skip)
2904 * @init: (allow-none): a #GFunc that is called initially and only once
2905 * @callback: a #GFunc that is called as often as possible
2906 * @data: a #gpointer with custom data to pass to the @callback function
2907 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
2908 * each call to the @callback
2910 * Start a custom stress-thread that will call your @callback for every
2911 * iteration allowing you to do something nasty.
2915 * Returns: a #GstHarnessThread
2920 gst_harness_stress_custom_start (GstHarness * h,
2921 GFunc init, GFunc callback, gpointer data, gulong sleep)
2923 GstHarnessCustomThread *t = g_slice_new0 (GstHarnessCustomThread);
2924 gst_harness_thread_init (&t->t,
2925 (GDestroyNotify) gst_harness_custom_thread_free, h, sleep);
2928 t->callback = callback;
2931 GST_HARNESS_THREAD_START (custom, t);
2936 * gst_harness_stress_statechange_start_full: (skip)
2938 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
2941 * Change the state of your harnessed #GstElement from NULL to PLAYING and
2942 * back again, only pausing for @sleep microseconds every time.
2946 * Returns: a #GstHarnessThread
2951 gst_harness_stress_statechange_start_full (GstHarness * h, gulong sleep)
2953 GstHarnessThread *t = g_slice_new0 (GstHarnessThread);
2954 gst_harness_thread_init (t,
2955 (GDestroyNotify) gst_harness_thread_free, h, sleep);
2956 GST_HARNESS_THREAD_START (statechange, t);
2961 gst_harness_ref_buffer (GstHarness * h, gpointer data)
2964 return gst_buffer_ref (GST_BUFFER_CAST (data));
2968 gst_harness_ref_event (GstHarness * h, gpointer data)
2971 return gst_event_ref (GST_EVENT_CAST (data));
2975 * gst_harness_stress_push_buffer_start_full: (skip)
2977 * @caps: a #GstCaps for the #GstBuffer
2978 * @segment: a #GstSegment
2979 * @buf: a #GstBuffer to push
2980 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
2981 * each call to gst_pad_push
2983 * Push a #GstBuffer in intervals of @sleep microseconds.
2987 * Returns: a #GstHarnessThread
2992 gst_harness_stress_push_buffer_start_full (GstHarness * h,
2993 GstCaps * caps, const GstSegment * segment, GstBuffer * buf, gulong sleep)
2995 return gst_harness_stress_push_buffer_with_cb_start_full (h, caps, segment,
2996 gst_harness_ref_buffer, gst_buffer_ref (buf),
2997 (GDestroyNotify) gst_buffer_unref, sleep);
3001 * gst_harness_stress_push_buffer_with_cb_start_full: (skip)
3003 * @caps: a #GstCaps for the #GstBuffer
3004 * @segment: a #GstSegment
3005 * @func: a #GstHarnessPrepareBufferFunc function called before every iteration
3006 * to prepare / create a #GstBuffer for pushing
3007 * @data: a #gpointer with data to the #GstHarnessPrepareBufferFunc function
3008 * @notify: a #GDestroyNotify that is called when thread is stopped
3009 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3010 * each call to gst_pad_push
3012 * Push a #GstBuffer returned by @func in intervals of @sleep microseconds.
3016 * Returns: a #GstHarnessThread
3021 gst_harness_stress_push_buffer_with_cb_start_full (GstHarness * h,
3022 GstCaps * caps, const GstSegment * segment,
3023 GstHarnessPrepareBufferFunc func, gpointer data, GDestroyNotify notify,
3026 GstHarnessPushBufferThread *t = g_slice_new0 (GstHarnessPushBufferThread);
3027 gst_harness_thread_init (&t->t,
3028 (GDestroyNotify) gst_harness_push_buffer_thread_free, h, sleep);
3030 gst_caps_replace (&t->caps, caps);
3031 t->segment = *segment;
3036 GST_HARNESS_THREAD_START (buffer, t);
3041 * gst_harness_stress_push_event_start_full: (skip)
3043 * @event: a #GstEvent to push
3044 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3045 * each gst_event_push with @event
3047 * Push the @event onto the harnessed #GstElement sinkpad in intervals of
3048 * @sleep microseconds
3052 * Returns: a #GstHarnessThread
3057 gst_harness_stress_push_event_start_full (GstHarness * h,
3058 GstEvent * event, gulong sleep)
3060 return gst_harness_stress_push_event_with_cb_start_full (h,
3061 gst_harness_ref_event, gst_event_ref (event),
3062 (GDestroyNotify) gst_event_unref, sleep);
3066 * gst_harness_stress_push_event_with_cb_start_full: (skip)
3068 * @func: a #GstHarnessPrepareEventFunc function called before every iteration
3069 * to prepare / create a #GstEvent for pushing
3070 * @data: a #gpointer with data to the #GstHarnessPrepareEventFunc function
3071 * @notify: a #GDestroyNotify that is called when thread is stopped
3072 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3073 * each call to gst_pad_push
3075 * Push a #GstEvent returned by @func onto the harnessed #GstElement sinkpad
3076 * in intervals of @sleep microseconds.
3080 * Returns: a #GstHarnessThread
3085 gst_harness_stress_push_event_with_cb_start_full (GstHarness * h,
3086 GstHarnessPrepareEventFunc func, gpointer data, GDestroyNotify notify,
3089 GstHarnessPushEventThread *t = g_slice_new0 (GstHarnessPushEventThread);
3090 gst_harness_thread_init (&t->t,
3091 (GDestroyNotify) gst_harness_push_event_thread_free, h, sleep);
3097 GST_HARNESS_THREAD_START (event, t);
3102 * gst_harness_stress_push_upstream_event_start_full: (skip)
3104 * @event: a #GstEvent to push
3105 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3106 * each gst_event_push with @event
3108 * Push the @event onto the harnessed #GstElement srcpad in intervals of
3109 * @sleep microseconds.
3113 * Returns: a #GstHarnessThread
3118 gst_harness_stress_push_upstream_event_start_full (GstHarness * h,
3119 GstEvent * event, gulong sleep)
3121 return gst_harness_stress_push_upstream_event_with_cb_start_full (h,
3122 gst_harness_ref_event, gst_event_ref (event),
3123 (GDestroyNotify) gst_event_unref, sleep);
3127 * gst_harness_stress_push_upstream_event_with_cb_start_full: (skip)
3129 * @func: a #GstHarnessPrepareEventFunc function called before every iteration
3130 * to prepare / create a #GstEvent for pushing
3131 * @data: a #gpointer with data to the #GstHarnessPrepareEventFunc function
3132 * @notify: a #GDestroyNotify that is called when thread is stopped
3133 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3134 * each call to gst_pad_push
3136 * Push a #GstEvent returned by @func onto the harnessed #GstElement srcpad
3137 * in intervals of @sleep microseconds.
3141 * Returns: a #GstHarnessThread
3146 gst_harness_stress_push_upstream_event_with_cb_start_full (GstHarness * h,
3147 GstHarnessPrepareEventFunc func, gpointer data, GDestroyNotify notify,
3150 GstHarnessPushEventThread *t = g_slice_new0 (GstHarnessPushEventThread);
3151 gst_harness_thread_init (&t->t,
3152 (GDestroyNotify) gst_harness_push_event_thread_free, h, sleep);
3158 GST_HARNESS_THREAD_START (upstream_event, t);
3163 * gst_harness_stress_property_start_full: (skip)
3165 * @name: a #gchar specifying a property name
3166 * @value: a #GValue to set the property to
3167 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3168 * each g_object_set with @name and @value
3170 * Call g_object_set with @name and @value in intervals of @sleep microseconds
3174 * Returns: a #GstHarnessThread
3179 gst_harness_stress_property_start_full (GstHarness * h,
3180 const gchar * name, const GValue * value, gulong sleep)
3182 GstHarnessPropThread *t = g_slice_new0 (GstHarnessPropThread);
3183 gst_harness_thread_init (&t->t,
3184 (GDestroyNotify) gst_harness_property_thread_free, h, sleep);
3186 t->name = g_strdup (name);
3187 g_value_init (&t->value, G_VALUE_TYPE (value));
3188 g_value_copy (value, &t->value);
3190 GST_HARNESS_THREAD_START (property, t);
3195 * gst_harness_stress_requestpad_start_full: (skip)
3197 * @templ: a #GstPadTemplate
3200 * @release: a #gboolean
3201 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3202 * each gst_element_request_pad
3204 * Call gst_element_request_pad in intervals of @sleep microseconds
3208 * Returns: a #GstHarnessThread
3213 gst_harness_stress_requestpad_start_full (GstHarness * h,
3214 GstPadTemplate * templ, const gchar * name, GstCaps * caps,
3215 gboolean release, gulong sleep)
3217 GstHarnessReqPadThread *t = g_slice_new0 (GstHarnessReqPadThread);
3218 gst_harness_thread_init (&t->t,
3219 (GDestroyNotify) gst_harness_requestpad_thread_free, h, sleep);
3221 t->templ = gst_object_ref (templ);
3222 t->name = g_strdup (name);
3223 gst_caps_replace (&t->caps, caps);
3224 t->release = release;
3226 GST_HARNESS_THREAD_START (requestpad, t);