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.
24 * @short_description: A test-harness for writing GStreamer unit tests
25 * @see_also: #GstTestClock,\
27 * #GstHarness is meant to make writing unit test for GStreamer much easier.
28 * It can be thought of as a way of treating a #GstElement as a black box,
29 * deterministically feeding it data, and controlling what data it outputs.
31 * The basic structure of #GstHarness is two "floating" #GstPads that connect
32 * to the harnessed #GstElement src and sink #GstPads like so:
35 * __________________________
36 * _____ | _____ _____ | _____
38 * | src |--+-| sink| Element | src |-+--| sink|
39 * |_____| | |_____| |_____| | |_____|
40 * |__________________________|
44 * With this, you can now simulate any environment the #GstElement might find
45 * itself in. By specifying the #GstCaps of the harness #GstPads, using
46 * functions like gst_harness_set_src_caps() or gst_harness_set_sink_caps_str(),
47 * you can test how the #GstElement interacts with different caps sets.
49 * Your harnessed #GstElement can of course also be a bin, and using
50 * gst_harness_new_parse() supporting standard gst-launch syntax, you can
51 * easily test a whole pipeline instead of just one element.
53 * You can then go on to push #GstBuffers and #GstEvents on to the srcpad,
54 * using functions like gst_harness_push() and gst_harness_push_event(), and
55 * then pull them out to examine them with gst_harness_pull() and
56 * gst_harness_pull_event().
58 * ## A simple buffer-in buffer-out example
60 * |[<!-- language="C" -->
61 * #include <gst/gst.h>
62 * #include <gst/check/gstharness.h>
67 * // attach the harness to the src and sink pad of GstQueue
68 * h = gst_harness_new ("queue");
70 * // we must specify a caps before pushing buffers
71 * gst_harness_set_src_caps_str (h, "mycaps");
73 * // create a buffer of size 42
74 * in_buf = gst_harness_create_buffer (h, 42);
76 * // push the buffer into the queue
77 * gst_harness_push (h, in_buf);
79 * // pull the buffer from the queue
80 * out_buf = gst_harness_pull (h);
82 * // validate the buffer in is the same as buffer out
83 * fail_unless (in_buf == out_buf);
86 * gst_buffer_unref (out_buf);
87 * 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:
107 * |[<!-- language="C" -->
108 * GstHarness * h = gst_harness_new (h, "vp8dec");
109 * gst_harness_add_src_parse (h, "videotestsrc is-live=1 ! vp8enc", TRUE);
112 * and then feeding it data with:
114 * |[<!-- language="C" -->
115 * gst_harness_push_from_src (h);
123 /* we have code with side effects in asserts, so make sure they are active */
124 #ifdef G_DISABLE_ASSERT
125 #error "GstHarness must be compiled with G_DISABLE_ASSERT undefined"
128 #include "gstharness.h"
134 static void gst_harness_stress_free (GstHarnessThread * t);
136 #define HARNESS_KEY "harness"
137 #define HARNESS_REF "harness-ref"
138 #define HARNESS_LOCK(h) g_mutex_lock (&(h)->priv->priv_mutex)
139 #define HARNESS_UNLOCK(h) g_mutex_unlock (&(h)->priv->priv_mutex)
141 static GstStaticPadTemplate hsrctemplate = GST_STATIC_PAD_TEMPLATE ("src",
144 GST_STATIC_CAPS_ANY);
145 static GstStaticPadTemplate hsinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
148 GST_STATIC_CAPS_ANY);
150 struct _GstHarnessPrivate
152 gchar *element_sinkpad_name;
153 gchar *element_srcpad_name;
159 GstPad *sink_forward_pad;
160 GstTestClock *testclock;
162 volatile gint recv_buffers;
163 volatile gint recv_events;
164 volatile gint recv_upstream_events;
166 GAsyncQueue *buffer_queue;
167 GAsyncQueue *src_event_queue;
168 GAsyncQueue *sink_event_queue;
170 GstClockTime latency_min;
171 GstClockTime latency_max;
172 gboolean has_clock_wait;
173 gboolean drop_buffers;
174 GstClockTime last_push_ts;
177 GstAllocator *allocator;
178 GstAllocationParams allocation_params;
179 GstAllocator *propose_allocator;
180 GstAllocationParams propose_allocation_params;
182 gboolean blocking_push_mode;
183 GCond blocking_push_cond;
184 GMutex blocking_push_mutex;
191 gst_harness_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
193 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
194 GstHarnessPrivate *priv = h->priv;
196 g_assert (h != NULL);
197 g_mutex_lock (&priv->blocking_push_mutex);
198 g_atomic_int_inc (&priv->recv_buffers);
200 if (priv->drop_buffers)
201 gst_buffer_unref (buffer);
203 g_async_queue_push (priv->buffer_queue, buffer);
205 if (priv->blocking_push_mode) {
206 g_cond_wait (&priv->blocking_push_cond, &priv->blocking_push_mutex);
208 g_mutex_unlock (&priv->blocking_push_mutex);
214 gst_harness_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
216 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
217 GstHarnessPrivate *priv = h->priv;
219 g_assert (h != NULL);
220 g_atomic_int_inc (&priv->recv_upstream_events);
221 g_async_queue_push (priv->src_event_queue, event);
226 gst_harness_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
228 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
229 GstHarnessPrivate *priv = h->priv;
233 g_assert (h != NULL);
235 g_atomic_int_inc (&priv->recv_events);
237 switch (GST_EVENT_TYPE (event)) {
238 case GST_EVENT_STREAM_START:
240 case GST_EVENT_SEGMENT:
249 if (priv->forwarding && forward && priv->sink_forward_pad) {
250 GstPad *fwdpad = gst_object_ref (priv->sink_forward_pad);
252 ret = gst_pad_push_event (fwdpad, event);
253 gst_object_unref (fwdpad);
256 g_async_queue_push (priv->sink_event_queue, event);
264 gst_harness_decide_allocation (GstHarness * h, GstCaps * caps)
266 GstHarnessPrivate *priv = h->priv;
268 GstAllocator *allocator;
269 GstAllocationParams params;
270 GstBufferPool *pool = NULL;
271 guint size, min, max;
273 query = gst_query_new_allocation (caps, FALSE);
274 gst_pad_peer_query (h->srcpad, query);
276 if (gst_query_get_n_allocation_params (query) > 0) {
277 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
280 gst_allocation_params_init (¶ms);
283 if (gst_query_get_n_allocation_pools (query) > 0) {
284 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
286 /* Most elements create their own pools if pool == NULL. Not sure if we
287 * want to do that in the harness since we may want to test the pool
288 * implementation of the elements. Not creating a pool will however ignore
289 * the returned size. */
291 pool = gst_buffer_pool_new ();
295 size = min = max = 0;
297 gst_query_unref (query);
300 GstStructure *config = gst_buffer_pool_get_config (pool);
301 gst_buffer_pool_config_set_params (config, caps, size, min, max);
302 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
303 gst_buffer_pool_set_config (pool, config);
306 if (pool != priv->pool) {
307 if (priv->pool != NULL)
308 gst_buffer_pool_set_active (priv->pool, FALSE);
310 gst_buffer_pool_set_active (pool, TRUE);
313 priv->allocation_params = params;
315 gst_object_unref (priv->allocator);
316 priv->allocator = allocator;
318 gst_object_unref (priv->pool);
323 gst_harness_negotiate (GstHarness * h)
327 caps = gst_pad_get_current_caps (h->srcpad);
329 gst_harness_decide_allocation (h, caps);
330 gst_caps_unref (caps);
332 GST_FIXME_OBJECT (h, "Cannot negotiate allocation because caps is not set");
337 gst_harness_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
339 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
340 GstHarnessPrivate *priv = h->priv;
342 g_assert (h != NULL);
344 // FIXME: forward all queries?
346 switch (GST_QUERY_TYPE (query)) {
347 case GST_QUERY_LATENCY:
348 gst_query_set_latency (query, TRUE, priv->latency_min, priv->latency_max);
352 GstCaps *caps, *filter = NULL;
354 if (priv->sink_caps) {
355 caps = gst_caps_ref (priv->sink_caps);
357 caps = gst_pad_get_pad_template_caps (pad);
360 gst_query_parse_caps (query, &filter);
361 if (filter != NULL) {
362 gst_caps_take (&caps,
363 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST));
366 gst_query_set_caps_result (query, caps);
367 gst_caps_unref (caps);
370 case GST_QUERY_ALLOCATION:
373 if (priv->forwarding && priv->sink_forward_pad != NULL) {
374 GstPad *peer = gst_pad_get_peer (priv->sink_forward_pad);
375 g_assert (peer != NULL);
377 res = gst_pad_query (peer, query);
378 gst_object_unref (peer);
385 gst_query_parse_allocation (query, &caps, &need_pool);
387 /* FIXME: Can this be removed? */
388 size = gst_query_get_n_allocation_params (query);
389 g_assert_cmpuint (0, ==, size);
390 gst_query_add_allocation_param (query,
391 priv->propose_allocator, &priv->propose_allocation_params);
393 GST_DEBUG_OBJECT (pad, "proposing allocation %" GST_PTR_FORMAT,
394 priv->propose_allocator);
400 res = gst_pad_query_default (pad, parent, query);
407 gst_harness_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
409 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
410 GstHarnessPrivate *priv = h->priv;
412 g_assert (h != NULL);
414 switch (GST_QUERY_TYPE (query)) {
415 case GST_QUERY_LATENCY:
416 gst_query_set_latency (query, TRUE, priv->latency_min, priv->latency_max);
420 GstCaps *caps, *filter = NULL;
422 if (priv->src_caps) {
423 caps = gst_caps_ref (priv->src_caps);
425 caps = gst_pad_get_pad_template_caps (pad);
428 gst_query_parse_caps (query, &filter);
429 if (filter != NULL) {
430 gst_caps_take (&caps,
431 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST));
434 gst_query_set_caps_result (query, caps);
435 gst_caps_unref (caps);
439 res = gst_pad_query_default (pad, parent, query);
445 gst_harness_element_ref (GstHarness * h)
449 GST_OBJECT_LOCK (h->element);
450 data = g_object_get_data (G_OBJECT (h->element), HARNESS_REF);
452 data = g_new0 (guint, 1);
454 g_object_set_data_full (G_OBJECT (h->element), HARNESS_REF, data, g_free);
458 GST_OBJECT_UNLOCK (h->element);
462 gst_harness_element_unref (GstHarness * h)
467 GST_OBJECT_LOCK (h->element);
468 data = g_object_get_data (G_OBJECT (h->element), HARNESS_REF);
469 g_assert (data != NULL);
472 GST_OBJECT_UNLOCK (h->element);
478 gst_harness_link_element_srcpad (GstHarness * h,
479 const gchar * element_srcpad_name)
481 GstHarnessPrivate *priv = h->priv;
482 GstPad *srcpad = gst_element_get_static_pad (h->element,
483 element_srcpad_name);
484 GstPadLinkReturn link;
486 srcpad = gst_element_get_request_pad (h->element, element_srcpad_name);
488 link = gst_pad_link (srcpad, h->sinkpad);
489 g_assert_cmpint (link, ==, GST_PAD_LINK_OK);
490 g_free (priv->element_srcpad_name);
491 priv->element_srcpad_name = gst_pad_get_name (srcpad);
493 gst_object_unref (srcpad);
497 gst_harness_link_element_sinkpad (GstHarness * h,
498 const gchar * element_sinkpad_name)
500 GstHarnessPrivate *priv = h->priv;
501 GstPad *sinkpad = gst_element_get_static_pad (h->element,
502 element_sinkpad_name);
503 GstPadLinkReturn link;
505 sinkpad = gst_element_get_request_pad (h->element, element_sinkpad_name);
507 link = gst_pad_link (h->srcpad, sinkpad);
508 g_assert_cmpint (link, ==, GST_PAD_LINK_OK);
509 g_free (priv->element_sinkpad_name);
510 priv->element_sinkpad_name = gst_pad_get_name (sinkpad);
512 gst_object_unref (sinkpad);
516 gst_harness_setup_src_pad (GstHarness * h,
517 GstStaticPadTemplate * src_tmpl, const gchar * element_sinkpad_name)
519 GstHarnessPrivate *priv = h->priv;
521 g_assert (h->srcpad == NULL);
523 priv->src_event_queue =
524 g_async_queue_new_full ((GDestroyNotify) gst_event_unref);
527 h->srcpad = gst_pad_new_from_static_template (src_tmpl, "src");
528 g_assert (h->srcpad);
529 g_object_set_data (G_OBJECT (h->srcpad), HARNESS_KEY, h);
531 gst_pad_set_query_function (h->srcpad, gst_harness_src_query);
532 gst_pad_set_event_function (h->srcpad, gst_harness_src_event);
534 gst_pad_set_active (h->srcpad, TRUE);
536 if (element_sinkpad_name)
537 gst_harness_link_element_sinkpad (h, element_sinkpad_name);
541 gst_harness_setup_sink_pad (GstHarness * h,
542 GstStaticPadTemplate * sink_tmpl, const gchar * element_srcpad_name)
544 GstHarnessPrivate *priv = h->priv;
545 g_assert (sink_tmpl);
546 g_assert (h->sinkpad == NULL);
548 priv->buffer_queue = g_async_queue_new_full (
549 (GDestroyNotify) gst_buffer_unref);
550 priv->sink_event_queue = g_async_queue_new_full (
551 (GDestroyNotify) gst_event_unref);
554 h->sinkpad = gst_pad_new_from_static_template (sink_tmpl, "sink");
555 g_assert (h->sinkpad);
556 g_object_set_data (G_OBJECT (h->sinkpad), HARNESS_KEY, h);
558 gst_pad_set_chain_function (h->sinkpad, gst_harness_chain);
559 gst_pad_set_query_function (h->sinkpad, gst_harness_sink_query);
560 gst_pad_set_event_function (h->sinkpad, gst_harness_sink_event);
562 gst_pad_set_active (h->sinkpad, TRUE);
564 if (element_srcpad_name)
565 gst_harness_link_element_srcpad (h, element_srcpad_name);
569 check_element_type (GstElement * element, gboolean * has_sinkpad,
570 gboolean * has_srcpad)
572 GstElementClass *element_class = GST_ELEMENT_GET_CLASS (element);
573 const GList *tmpl_list;
575 *has_srcpad = element->numsrcpads > 0;
576 *has_sinkpad = element->numsinkpads > 0;
578 tmpl_list = gst_element_class_get_pad_template_list (element_class);
581 GstPadTemplate *pad_tmpl = (GstPadTemplate *) tmpl_list->data;
582 tmpl_list = g_list_next (tmpl_list);
583 if (GST_PAD_TEMPLATE_DIRECTION (pad_tmpl) == GST_PAD_SRC)
585 if (GST_PAD_TEMPLATE_DIRECTION (pad_tmpl) == GST_PAD_SINK)
586 *has_sinkpad |= TRUE;
591 turn_async_and_sync_off (GstElement * element)
593 GObjectClass *class = G_OBJECT_GET_CLASS (element);
594 if (g_object_class_find_property (class, "async"))
595 g_object_set (element, "async", FALSE, NULL);
596 if (g_object_class_find_property (class, "sync"))
597 g_object_set (element, "sync", FALSE, NULL);
601 gst_pad_is_request_pad (GstPad * pad)
603 GstPadTemplate *temp;
608 temp = gst_pad_get_pad_template (pad);
611 is_request = GST_PAD_TEMPLATE_PRESENCE (temp) == GST_PAD_REQUEST;
612 gst_object_unref (temp);
617 * gst_harness_new_empty: (skip)
619 * Creates a new empty harness. Use gst_harness_add_element_full() to add
620 * an #GstElement to it.
624 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
630 gst_harness_new_empty (void)
633 GstHarnessPrivate *priv;
635 h = g_new0 (GstHarness, 1);
636 g_assert (h != NULL);
637 h->priv = g_new0 (GstHarnessPrivate, 1);
640 GST_DEBUG_OBJECT (h, "about to create new harness %p", h);
641 priv->last_push_ts = GST_CLOCK_TIME_NONE;
642 priv->latency_min = 0;
643 priv->latency_max = GST_CLOCK_TIME_NONE;
644 priv->drop_buffers = FALSE;
645 priv->testclock = GST_TEST_CLOCK_CAST (gst_test_clock_new ());
647 priv->propose_allocator = NULL;
648 gst_allocation_params_init (&priv->propose_allocation_params);
650 g_mutex_init (&priv->blocking_push_mutex);
651 g_cond_init (&priv->blocking_push_cond);
652 g_mutex_init (&priv->priv_mutex);
654 priv->stress = g_ptr_array_new_with_free_func (
655 (GDestroyNotify) gst_harness_stress_free);
657 /* we have forwarding on as a default */
658 gst_harness_set_forwarding (h, TRUE);
664 * gst_harness_add_element_full: (skip)
666 * @element: a #GstElement to add to the harness (transfer none)
667 * @hsrc: (allow-none): a #GstStaticPadTemplate describing the harness srcpad.
668 * %NULL will not create a harness srcpad.
669 * @element_sinkpad_name: (allow-none): a #gchar with the name of the element
670 * sinkpad that is then linked to the harness srcpad. Can be a static or request
671 * or a sometimes pad that has been added. %NULL will not get/request a sinkpad
672 * from the element. (Like if the element is a src.)
673 * @hsink: (allow-none): a #GstStaticPadTemplate describing the harness sinkpad.
674 * %NULL will not create a harness sinkpad.
675 * @element_srcpad_name: (allow-none): a #gchar with the name of the element
676 * srcpad that is then linked to the harness sinkpad, similar to the
677 * @element_sinkpad_name.
679 * Adds a #GstElement to an empty #GstHarness
686 gst_harness_add_element_full (GstHarness * h, GstElement * element,
687 GstStaticPadTemplate * hsrc, const gchar * element_sinkpad_name,
688 GstStaticPadTemplate * hsink, const gchar * element_srcpad_name)
690 GstClock *element_clock;
691 gboolean has_sinkpad, has_srcpad;
693 g_return_if_fail (element != NULL);
694 g_return_if_fail (h->element == NULL);
696 element_clock = GST_ELEMENT_CLOCK (element);
697 h->element = gst_object_ref (element);
698 check_element_type (element, &has_sinkpad, &has_srcpad);
700 /* setup the loose srcpad linked to the element sinkpad */
702 gst_harness_setup_src_pad (h, hsrc, element_sinkpad_name);
704 /* setup the loose sinkpad linked to the element srcpad */
706 gst_harness_setup_sink_pad (h, hsink, element_srcpad_name);
708 /* as a harness sink, we should not need sync and async */
709 if (has_sinkpad && !has_srcpad)
710 turn_async_and_sync_off (h->element);
712 if (h->srcpad != NULL) {
714 gchar *stream_id = g_strdup_printf ("%s-%p",
715 GST_OBJECT_NAME (h->element), h);
716 handled = gst_pad_push_event (h->srcpad,
717 gst_event_new_stream_start (stream_id));
722 /* if the element already has a testclock attached,
723 we replace our own with it, if no clock we attach the testclock */
725 if (GST_IS_TEST_CLOCK (element_clock)) {
726 gst_object_replace ((GstObject **) & h->priv->testclock,
727 (GstObject *) GST_ELEMENT_CLOCK (element));
730 gst_harness_use_testclock (h);
733 /* don't start sources, they start producing data! */
735 gst_harness_play (h);
737 gst_harness_element_ref (h);
739 GST_DEBUG_OBJECT (h, "added element to harness %p "
740 "with element_srcpad_name (%p, %s, %s) and element_sinkpad_name (%p, %s, %s)",
741 h, h->srcpad, GST_DEBUG_PAD_NAME (h->srcpad),
742 h->sinkpad, GST_DEBUG_PAD_NAME (h->sinkpad));
746 * gst_harness_new_full: (skip)
747 * @element: a #GstElement to attach the harness to (transfer none)
748 * @hsrc: (allow-none): a #GstStaticPadTemplate describing the harness srcpad.
749 * %NULL will not create a harness srcpad.
750 * @element_sinkpad_name: (allow-none): a #gchar with the name of the element
751 * sinkpad that is then linked to the harness srcpad. Can be a static or request
752 * or a sometimes pad that has been added. %NULL will not get/request a sinkpad
753 * from the element. (Like if the element is a src.)
754 * @hsink: (allow-none): a #GstStaticPadTemplate describing the harness sinkpad.
755 * %NULL will not create a harness sinkpad.
756 * @element_srcpad_name: (allow-none): a #gchar with the name of the element
757 * srcpad that is then linked to the harness sinkpad, similar to the
758 * @element_sinkpad_name.
760 * Creates a new harness.
764 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
770 gst_harness_new_full (GstElement * element,
771 GstStaticPadTemplate * hsrc, const gchar * element_sinkpad_name,
772 GstStaticPadTemplate * hsink, const gchar * element_srcpad_name)
775 h = gst_harness_new_empty ();
776 gst_harness_add_element_full (h, element,
777 hsrc, element_sinkpad_name, hsink, element_srcpad_name);
782 * gst_harness_new_with_element: (skip)
783 * @element: a #GstElement to attach the harness to (transfer none)
784 * @element_sinkpad_name: (allow-none): a #gchar with the name of the element
785 * sinkpad that is then linked to the harness srcpad. %NULL does not attach a
787 * @element_srcpad_name: (allow-none): a #gchar with the name of the element
788 * srcpad that is then linked to the harness sinkpad. %NULL does not attach a
791 * Creates a new harness. Works in the same way as gst_harness_new_full(), only
792 * that generic padtemplates are used for the harness src and sinkpads, which
793 * will be sufficient in most usecases.
797 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
803 gst_harness_new_with_element (GstElement * element,
804 const gchar * element_sinkpad_name, const gchar * element_srcpad_name)
806 return gst_harness_new_full (element,
807 &hsrctemplate, element_sinkpad_name, &hsinktemplate, element_srcpad_name);
811 * gst_harness_new_with_padnames: (skip)
812 * @element_name: a #gchar describing the #GstElement name
813 * @element_sinkpad_name: (allow-none): a #gchar with the name of the element
814 * sinkpad that is then linked to the harness srcpad. %NULL does not attach a
816 * @element_srcpad_name: (allow-none): a #gchar with the name of the element
817 * srcpad that is then linked to the harness sinkpad. %NULL does not attach a
820 * Creates a new harness. Works like gst_harness_new_with_element(),
821 * except you specify the factoryname of the #GstElement
825 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
831 gst_harness_new_with_padnames (const gchar * element_name,
832 const gchar * element_sinkpad_name, const gchar * element_srcpad_name)
835 GstElement *element = gst_element_factory_make (element_name, NULL);
836 g_assert (element != NULL);
838 h = gst_harness_new_with_element (element, element_sinkpad_name,
839 element_srcpad_name);
840 gst_object_unref (element);
845 * gst_harness_new_with_templates: (skip)
846 * @element_name: a #gchar describing the #GstElement name
847 * @hsrc: (allow-none): a #GstStaticPadTemplate describing the harness srcpad.
848 * %NULL will not create a harness srcpad.
849 * @hsink: (allow-none): a #GstStaticPadTemplate describing the harness sinkpad.
850 * %NULL will not create a harness sinkpad.
852 * Creates a new harness, like gst_harness_new_full(), except it
853 * assumes the #GstElement sinkpad is named "sink" and srcpad is named "src"
857 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
863 gst_harness_new_with_templates (const gchar * element_name,
864 GstStaticPadTemplate * hsrc, GstStaticPadTemplate * hsink)
867 GstElement *element = gst_element_factory_make (element_name, NULL);
868 g_assert (element != NULL);
870 h = gst_harness_new_full (element, hsrc, "sink", hsink, "src");
871 gst_object_unref (element);
876 * gst_harness_new: (skip)
877 * @element_name: a #gchar describing the #GstElement name
879 * Creates a new harness. Works like gst_harness_new_with_padnames(), except it
880 * assumes the #GstElement sinkpad is named "sink" and srcpad is named "src"
884 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
890 gst_harness_new (const gchar * element_name)
892 return gst_harness_new_with_padnames (element_name, "sink", "src");
896 * gst_harness_add_parse: (skip)
898 * @launchline: a #gchar describing a gst-launch type line
900 * Parses the @launchline and puts that in a #GstBin,
901 * and then attches the supplied #GstHarness to the bin.
908 gst_harness_add_parse (GstHarness * h, const gchar * launchline)
914 gboolean done = FALSE;
916 g_return_if_fail (launchline != NULL);
918 desc = g_strdup_printf ("bin.( %s )", launchline);
920 (GstBin *) gst_parse_launch_full (desc, NULL, GST_PARSE_FLAG_NONE, NULL);
923 if (G_UNLIKELY (bin == NULL))
926 /* find pads and ghost them if necessary */
927 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC)) != NULL) {
928 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("src", pad));
929 gst_object_unref (pad);
931 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SINK)) != NULL) {
932 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("sink", pad));
933 gst_object_unref (pad);
936 iter = gst_bin_iterate_sinks (bin);
938 GValue item = { 0, };
940 switch (gst_iterator_next (iter, &item)) {
941 case GST_ITERATOR_OK:
942 turn_async_and_sync_off (GST_ELEMENT (g_value_get_object (&item)));
943 g_value_reset (&item);
945 case GST_ITERATOR_DONE:
948 case GST_ITERATOR_RESYNC:
949 gst_iterator_resync (iter);
951 case GST_ITERATOR_ERROR:
952 gst_object_unref (bin);
953 gst_iterator_free (iter);
954 g_return_if_reached ();
958 gst_iterator_free (iter);
960 gst_harness_add_element_full (h, GST_ELEMENT_CAST (bin),
961 &hsrctemplate, "sink", &hsinktemplate, "src");
962 gst_object_unref (bin);
966 * gst_harness_new_parse: (skip)
967 * @launchline: a #gchar describing a gst-launch type line
969 * Creates a new harness, parsing the @launchline and putting that in a #GstBin,
970 * and then attches the harness to the bin.
974 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
980 gst_harness_new_parse (const gchar * launchline)
983 h = gst_harness_new_empty ();
984 gst_harness_add_parse (h, launchline);
989 * gst_harness_teardown:
992 * Tears down a @GstHarness, freeing all resources allocated using it.
999 gst_harness_teardown (GstHarness * h)
1001 GstHarnessPrivate *priv = h->priv;
1003 if (priv->blocking_push_mode) {
1004 g_mutex_lock (&priv->blocking_push_mutex);
1005 priv->blocking_push_mode = FALSE;
1006 g_cond_signal (&priv->blocking_push_cond);
1007 g_mutex_unlock (&priv->blocking_push_mutex);
1010 if (h->src_harness) {
1011 gst_harness_teardown (h->src_harness);
1014 gst_object_replace ((GstObject **) & priv->sink_forward_pad, NULL);
1015 if (h->sink_harness) {
1016 gst_harness_teardown (h->sink_harness);
1020 gst_caps_unref (priv->src_caps);
1022 if (priv->sink_caps)
1023 gst_caps_unref (priv->sink_caps);
1026 if (gst_pad_is_request_pad (GST_PAD_PEER (h->srcpad)))
1027 gst_element_release_request_pad (h->element, GST_PAD_PEER (h->srcpad));
1028 g_free (priv->element_sinkpad_name);
1030 gst_pad_set_active (h->srcpad, FALSE);
1031 gst_object_unref (h->srcpad);
1033 g_async_queue_unref (priv->src_event_queue);
1037 if (gst_pad_is_request_pad (GST_PAD_PEER (h->sinkpad)))
1038 gst_element_release_request_pad (h->element, GST_PAD_PEER (h->sinkpad));
1039 g_free (priv->element_srcpad_name);
1041 gst_pad_set_active (h->sinkpad, FALSE);
1042 gst_object_unref (h->sinkpad);
1044 g_async_queue_unref (priv->buffer_queue);
1045 g_async_queue_unref (priv->sink_event_queue);
1048 gst_object_replace ((GstObject **) & priv->propose_allocator, NULL);
1049 gst_object_replace ((GstObject **) & priv->allocator, NULL);
1050 gst_object_replace ((GstObject **) & priv->pool, NULL);
1052 /* if we hold the last ref, set to NULL */
1053 if (gst_harness_element_unref (h) == 0) {
1054 gboolean state_change;
1055 GstState state, pending;
1056 state_change = gst_element_set_state (h->element, GST_STATE_NULL);
1057 g_assert (state_change == GST_STATE_CHANGE_SUCCESS);
1058 state_change = gst_element_get_state (h->element, &state, &pending, 0);
1059 g_assert (state_change == GST_STATE_CHANGE_SUCCESS);
1060 g_assert (state == GST_STATE_NULL);
1063 g_cond_clear (&priv->blocking_push_cond);
1064 g_mutex_clear (&priv->blocking_push_mutex);
1065 g_mutex_clear (&priv->priv_mutex);
1067 g_ptr_array_unref (priv->stress);
1069 gst_object_unref (h->element);
1071 gst_object_replace ((GstObject **) & priv->testclock, NULL);
1078 * gst_harness_add_element_src_pad:
1080 * @srcpad: a #GstPad to link to the harness sinkpad
1082 * Links the specifed #GstPad the @GstHarness sinkpad. This can be useful if
1083 * perhaps the srcpad did not exist at the time of creating the harness,
1084 * like a demuxer that provides a sometimes-pad after receiving data.
1091 gst_harness_add_element_src_pad (GstHarness * h, GstPad * srcpad)
1093 GstHarnessPrivate *priv = h->priv;
1094 GstPadLinkReturn link;
1095 if (h->sinkpad == NULL)
1096 gst_harness_setup_sink_pad (h, &hsinktemplate, NULL);
1097 link = gst_pad_link (srcpad, h->sinkpad);
1098 g_assert_cmpint (link, ==, GST_PAD_LINK_OK);
1099 g_free (priv->element_srcpad_name);
1100 priv->element_srcpad_name = gst_pad_get_name (srcpad);
1104 * gst_harness_add_element_sink_pad:
1106 * @sinkpad: a #GstPad to link to the harness srcpad
1108 * Links the specifed #GstPad the @GstHarness srcpad.
1115 gst_harness_add_element_sink_pad (GstHarness * h, GstPad * sinkpad)
1117 GstHarnessPrivate *priv = h->priv;
1118 GstPadLinkReturn link;
1119 if (h->srcpad == NULL)
1120 gst_harness_setup_src_pad (h, &hsrctemplate, NULL);
1121 link = gst_pad_link (h->srcpad, sinkpad);
1122 g_assert_cmpint (link, ==, GST_PAD_LINK_OK);
1123 g_free (priv->element_sinkpad_name);
1124 priv->element_sinkpad_name = gst_pad_get_name (sinkpad);
1128 * gst_harness_set_src_caps:
1130 * @caps: (transfer full): a #GstCaps to set on the harness srcpad
1132 * Sets the @GstHarness srcpad caps. This must be done before any buffers
1133 * can legally be pushed from the harness to the element.
1140 gst_harness_set_src_caps (GstHarness * h, GstCaps * caps)
1142 GstHarnessPrivate *priv = h->priv;
1146 handled = gst_pad_push_event (h->srcpad, gst_event_new_caps (caps));
1148 gst_caps_take (&priv->src_caps, caps);
1150 gst_segment_init (&segment, GST_FORMAT_TIME);
1151 handled = gst_pad_push_event (h->srcpad, gst_event_new_segment (&segment));
1155 * gst_harness_set_sink_caps:
1157 * @caps: (transfer full): a #GstCaps to set on the harness sinkpad
1159 * Sets the @GstHarness sinkpad caps.
1166 gst_harness_set_sink_caps (GstHarness * h, GstCaps * caps)
1168 GstHarnessPrivate *priv = h->priv;
1170 gst_caps_take (&priv->sink_caps, caps);
1171 gst_pad_push_event (h->sinkpad, gst_event_new_reconfigure ());
1175 * gst_harness_set_caps:
1177 * @in: (transfer full): a #GstCaps to set on the harness srcpad
1178 * @out: (transfer full): a #GstCaps to set on the harness sinkpad
1180 * Sets the @GstHarness srcpad and sinkpad caps.
1187 gst_harness_set_caps (GstHarness * h, GstCaps * in, GstCaps * out)
1189 gst_harness_set_sink_caps (h, out);
1190 gst_harness_set_src_caps (h, in);
1194 * gst_harness_set_src_caps_str:
1196 * @str: a @gchar describing a #GstCaps to set on the harness srcpad
1198 * Sets the @GstHarness srcpad caps using a string. This must be done before
1199 * any buffers can legally be pushed from the harness to the element.
1206 gst_harness_set_src_caps_str (GstHarness * h, const gchar * str)
1208 gst_harness_set_src_caps (h, gst_caps_from_string (str));
1212 * gst_harness_set_sink_caps_str:
1214 * @str: a @gchar describing a #GstCaps to set on the harness sinkpad
1216 * Sets the @GstHarness sinkpad caps using a string.
1223 gst_harness_set_sink_caps_str (GstHarness * h, const gchar * str)
1225 gst_harness_set_sink_caps (h, gst_caps_from_string (str));
1229 * gst_harness_set_caps_str:
1231 * @in: a @gchar describing a #GstCaps to set on the harness srcpad
1232 * @out: a @gchar describing a #GstCaps to set on the harness sinkpad
1234 * Sets the @GstHarness srcpad and sinkpad caps using strings.
1241 gst_harness_set_caps_str (GstHarness * h, const gchar * in, const gchar * out)
1243 gst_harness_set_sink_caps_str (h, out);
1244 gst_harness_set_src_caps_str (h, in);
1248 * gst_harness_use_systemclock:
1251 * Sets the system #GstClock on the @GstHarness #GstElement
1258 gst_harness_use_systemclock (GstHarness * h)
1260 GstClock *clock = gst_system_clock_obtain ();
1261 g_assert (clock != NULL);
1262 gst_element_set_clock (h->element, clock);
1263 gst_object_unref (clock);
1267 * gst_harness_use_testclock:
1270 * Sets the #GstTestClock on the #GstHarness #GstElement
1277 gst_harness_use_testclock (GstHarness * h)
1279 gst_element_set_clock (h->element, GST_CLOCK_CAST (h->priv->testclock));
1283 * gst_harness_get_testclock:
1286 * Get the #GstTestClock. Useful if specific operations on the testclock is
1291 * Returns: (transfer full): a #GstTestClock, or %NULL if the testclock is not
1297 gst_harness_get_testclock (GstHarness * h)
1299 return gst_object_ref (h->priv->testclock);
1303 * gst_harness_set_time:
1305 * @time: a #GstClockTime to advance the clock to
1307 * Advance the #GstTestClock to a specific time.
1311 * Returns: a @gboolean %TRUE if the time could be set. %FALSE if not.
1316 gst_harness_set_time (GstHarness * h, GstClockTime time)
1318 gst_test_clock_set_time (h->priv->testclock, time);
1323 * gst_harness_wait_for_clock_id_waits:
1325 * @waits: a #guint describing the numbers of #GstClockID registered with
1327 * @timeout: a #guint describing how many seconds to wait for @waits to be true
1329 * Waits for @timeout seconds until @waits number of #GstClockID waits is
1330 * registered with the #GstTestClock. Useful for writing deterministic tests,
1331 * where you want to make sure that an expected number of waits have been
1336 * Returns: a @gboolean %TRUE if the waits have been registered, %FALSE if not.
1337 * (Could be that it timed out waiting or that more waits then waits was found)
1342 gst_harness_wait_for_clock_id_waits (GstHarness * h, guint waits, guint timeout)
1344 GstTestClock *testclock = h->priv->testclock;
1348 start_time = g_get_monotonic_time ();
1349 while (gst_test_clock_peek_id_count (testclock) < waits) {
1352 g_usleep (G_USEC_PER_SEC / 1000);
1353 time_spent = g_get_monotonic_time () - start_time;
1354 if ((time_spent / G_USEC_PER_SEC) > timeout)
1358 ret = (waits == gst_test_clock_peek_id_count (testclock));
1364 * gst_harness_crank_single_clock_wait:
1367 * A "crank" consists of three steps:
1368 * 1: Wait for a #GstClockID to be registered with the #GstTestClock.
1369 * 2: Advance the #GstTestClock to the time the #GstClockID is waiting for.
1370 * 3: Release the #GstClockID wait.
1371 * Together, this provides an easy way to not have to think about the details
1372 * around clocks and time, but still being able to write deterministic tests
1373 * that are dependant on this. A "crank" can be though of as the notion of
1374 * manually driving the clock forward to its next logical step.
1378 * Returns: a @gboolean %TRUE if the "crank" was successful, %FALSE if not.
1383 gst_harness_crank_single_clock_wait (GstHarness * h)
1385 return gst_test_clock_crank (h->priv->testclock);
1389 * gst_harness_crank_multiple_clock_waits:
1391 * @waits: a #guint describing the number of #GstClockIDs to crank
1393 * Similar to gst_harness_crank_single_clock_wait(), this is the function to use
1394 * if your harnessed element(s) are using more then one gst_clock_id_wait.
1395 * Failing to do so can (and will) make it racy which #GstClockID you actually
1396 * are releasing, where as this function will process all the waits at the
1397 * same time, ensuring that one thread can't register another wait before
1398 * both are released.
1402 * Returns: a @gboolean %TRUE if the "crank" was successful, %FALSE if not.
1407 gst_harness_crank_multiple_clock_waits (GstHarness * h, guint waits)
1409 GstTestClock *testclock = h->priv->testclock;
1413 gst_test_clock_wait_for_multiple_pending_ids (testclock, waits, &pending);
1414 gst_harness_set_time (h, gst_test_clock_id_list_get_latest_time (pending));
1415 processed = gst_test_clock_process_id_list (testclock, pending);
1417 g_list_free_full (pending, gst_clock_id_unref);
1418 return processed == waits;
1425 * This will set the harnessed #GstElement to %GST_STATE_PLAYING.
1426 * #GstElements without a sink-#GstPad and with the %GST_ELEMENT_FLAG_SOURCE
1427 * flag set is concidered a src #GstElement
1428 * Non-src #GstElements (like sinks and filters) are automatically set to
1429 * playing by the #GstHarness, but src #GstElements are not to avoid them
1430 * starting to produce buffers.
1431 * Hence, for src #GstElement you must call gst_harness_play() explicitly.
1438 gst_harness_play (GstHarness * h)
1440 GstState state, pending;
1441 gboolean state_change;
1442 state_change = gst_element_set_state (h->element, GST_STATE_PLAYING);
1443 g_assert_cmpint (GST_STATE_CHANGE_SUCCESS, ==, state_change);
1444 state_change = gst_element_get_state (h->element, &state, &pending, 0);
1445 g_assert_cmpint (GST_STATE_CHANGE_SUCCESS, ==, state_change);
1446 g_assert_cmpint (GST_STATE_PLAYING, ==, state);
1450 * gst_harness_set_blocking_push_mode:
1453 * Setting this will make the harness block in the chain-function, and
1454 * then release when gst_harness_pull() or gst_harness_try_pull() is called.
1455 * Can be useful when wanting to control a src-element that is not implementing
1456 * gst_clock_id_wait() so it can't be controlled by the #GstTestClock, since
1457 * it otherwise would produce buffers as fast as possible.
1464 gst_harness_set_blocking_push_mode (GstHarness * h)
1466 GstHarnessPrivate *priv = h->priv;
1467 priv->blocking_push_mode = TRUE;
1471 * gst_harness_set_forwarding:
1473 * @forwarding: a #gboolean to enable/disable forwarding
1475 * As a convenience, a src-harness will forward %GST_EVENT_STREAM_START,
1476 * %GST_EVENT_CAPS and %GST_EVENT_SEGMENT to the main-harness if forwarding
1477 * is enabled, and forward any sticky-events from the main-harness to
1478 * the sink-harness. It will also forward the %GST_QUERY_ALLOCATION.
1480 * If forwarding is disabled, the user will have to either manually push
1481 * these events from the src-harness using gst_harness_src_push_event(), or
1482 * create and push them manually. While this will allow full control and
1483 * inspection of these events, for the most cases having forwarding enabled
1484 * will be sufficient when writing a test where the src-harness' main function
1485 * is providing data for the main-harness.
1487 * Forwarding is enabled by default.
1494 gst_harness_set_forwarding (GstHarness * h, gboolean forwarding)
1496 GstHarnessPrivate *priv = h->priv;
1497 priv->forwarding = forwarding;
1499 gst_harness_set_forwarding (h->src_harness, forwarding);
1500 if (h->sink_harness)
1501 gst_harness_set_forwarding (h->sink_harness, forwarding);
1505 gst_harness_set_forward_pad (GstHarness * h, GstPad * fwdpad)
1508 gst_object_replace ((GstObject **) & h->priv->sink_forward_pad,
1509 (GstObject *) fwdpad);
1514 * gst_harness_create_buffer:
1516 * @size: a #gsize specifying the size of the buffer
1518 * Allocates a buffer using a #GstBufferPool if present, or else using the
1519 * configured #GstAllocator and #GstAllocationParams
1523 * Returns: a #GstBuffer of size @size
1528 gst_harness_create_buffer (GstHarness * h, gsize size)
1530 GstHarnessPrivate *priv = h->priv;
1531 GstBuffer *ret = NULL;
1534 if (gst_pad_check_reconfigure (h->srcpad))
1535 gst_harness_negotiate (h);
1538 flow = gst_buffer_pool_acquire_buffer (priv->pool, &ret, NULL);
1539 g_assert_cmpint (flow, ==, GST_FLOW_OK);
1540 if (gst_buffer_get_size (ret) != size) {
1541 GST_DEBUG_OBJECT (h,
1542 "use fallback, pool is configured with a different size (%zu != %zu)",
1543 size, gst_buffer_get_size (ret));
1544 gst_buffer_unref (ret);
1551 gst_buffer_new_allocate (priv->allocator, size,
1552 &priv->allocation_params);
1554 g_assert (ret != NULL);
1561 * @buffer: a #GstBuffer to push
1563 * Pushes a #GstBuffer on the #GstHarness srcpad. The standard way of
1564 * interacting with an harnessed element.
1568 * Returns: a #GstFlowReturn with the result from the push
1573 gst_harness_push (GstHarness * h, GstBuffer * buffer)
1575 GstHarnessPrivate *priv = h->priv;
1576 g_assert (buffer != NULL);
1577 priv->last_push_ts = GST_BUFFER_TIMESTAMP (buffer);
1578 return gst_pad_push (h->srcpad, buffer);
1585 * Pulls a #GstBuffer from the #GAsyncQueue on the #GstHarness sinkpad. The pull
1586 * will timeout in 60 seconds. This is the standard way of getting a buffer
1587 * from a harnessed #GstElement.
1591 * Returns: a #GstBuffer or %NULL if timed out.
1596 gst_harness_pull (GstHarness * h)
1598 GstHarnessPrivate *priv = h->priv;
1599 GstBuffer *buf = (GstBuffer *) g_async_queue_timeout_pop (priv->buffer_queue,
1600 G_USEC_PER_SEC * 60);
1602 if (priv->blocking_push_mode) {
1603 g_mutex_lock (&priv->blocking_push_mutex);
1604 g_cond_signal (&priv->blocking_push_cond);
1605 g_mutex_unlock (&priv->blocking_push_mutex);
1612 * gst_harness_try_pull:
1615 * Pulls a #GstBuffer from the #GAsyncQueue on the #GstHarness sinkpad. Unlike
1616 * gst_harness_pull this will not wait for any buffers if not any are present,
1617 * and return %NULL straight away.
1621 * Returns: a #GstBuffer or %NULL if no buffers are present in the #GAsyncQueue
1626 gst_harness_try_pull (GstHarness * h)
1628 GstHarnessPrivate *priv = h->priv;
1629 GstBuffer *buf = (GstBuffer *) g_async_queue_try_pop (priv->buffer_queue);
1631 if (priv->blocking_push_mode) {
1632 g_mutex_lock (&priv->blocking_push_mutex);
1633 g_cond_signal (&priv->blocking_push_cond);
1634 g_mutex_unlock (&priv->blocking_push_mutex);
1641 * gst_harness_push_and_pull:
1643 * @buffer: a #GstBuffer to push
1645 * Basically a gst_harness_push and a gst_harness_pull in one line. Reflects
1646 * the fact that you often want to do exactly this in your test: Push one buffer
1647 * in, and inspect the outcome.
1651 * Returns: a #GstBuffer or %NULL if timed out.
1656 gst_harness_push_and_pull (GstHarness * h, GstBuffer * buffer)
1658 gst_harness_push (h, buffer);
1659 return gst_harness_pull (h);
1663 * gst_harness_buffers_received:
1666 * The total number of #GstBuffers that has arrived on the #GstHarness sinkpad.
1667 * This number includes buffers that have been dropped as well as buffers
1668 * that have already been pulled out.
1672 * Returns: a #guint number of buffers received
1677 gst_harness_buffers_received (GstHarness * h)
1679 GstHarnessPrivate *priv = h->priv;
1680 return g_atomic_int_get (&priv->recv_buffers);
1684 * gst_harness_buffers_in_queue:
1687 * The number of #GstBuffers currently in the #GstHarness sinkpad #GAsyncQueue
1691 * Returns: a #guint number of buffers in the queue
1696 gst_harness_buffers_in_queue (GstHarness * h)
1698 GstHarnessPrivate *priv = h->priv;
1699 return g_async_queue_length (priv->buffer_queue);
1703 * gst_harness_set_drop_buffers:
1705 * @drop_buffers: a #gboolean specifying to drop outgoing buffers or not
1707 * When set to %TRUE, instead of placing the buffers arriving from the harnessed
1708 * #GstElement inside the sinkpads #GAsyncQueue, they are instead unreffed.
1715 gst_harness_set_drop_buffers (GstHarness * h, gboolean drop_buffers)
1717 GstHarnessPrivate *priv = h->priv;
1718 priv->drop_buffers = drop_buffers;
1722 * gst_harness_dump_to_file:
1724 * @filename: a #gchar with a the name of a file
1726 * Allows you to dump the #GstBuffers the #GstHarness sinkpad #GAsyncQueue
1734 gst_harness_dump_to_file (GstHarness * h, const gchar * filename)
1736 GstHarnessPrivate *priv = h->priv;
1739 fd = fopen (filename, "wb");
1742 while ((buf = g_async_queue_try_pop (priv->buffer_queue))) {
1744 if (gst_buffer_map (buf, &info, GST_MAP_READ)) {
1745 fwrite (info.data, 1, info.size, fd);
1746 gst_buffer_unmap (buf, &info);
1748 GST_ERROR ("failed to map buffer %p", buf);
1750 gst_buffer_unref (buf);
1758 * gst_harness_get_last_pushed_timestamp:
1761 * Get the timestamp of the last #GstBuffer pushed on the #GstHarness srcpad,
1762 * typically with gst_harness_push or gst_harness_push_from_src.
1766 * Returns: a #GstClockTime with the timestamp or %GST_CLOCK_TIME_NONE if no
1767 * #GstBuffer has been pushed on the #GstHarness srcpad
1772 gst_harness_get_last_pushed_timestamp (GstHarness * h)
1774 GstHarnessPrivate *priv = h->priv;
1775 return priv->last_push_ts;
1779 * gst_harness_push_event:
1781 * @event: a #GstEvent to push
1783 * Pushes an #GstEvent on the #GstHarness srcpad.
1787 * Returns: a #gboolean with the result from the push
1792 gst_harness_push_event (GstHarness * h, GstEvent * event)
1794 return gst_pad_push_event (h->srcpad, event);
1798 * gst_harness_pull_event:
1801 * Pulls an #GstEvent from the #GAsyncQueue on the #GstHarness sinkpad.
1802 * Timeouts after 60 seconds similar to gst_harness_pull.
1806 * Returns: a #GstEvent or %NULL if timed out.
1811 gst_harness_pull_event (GstHarness * h)
1813 GstHarnessPrivate *priv = h->priv;
1814 return (GstEvent *) g_async_queue_timeout_pop (priv->sink_event_queue,
1815 G_USEC_PER_SEC * 60);
1819 * gst_harness_try_pull_event:
1822 * Pulls an #GstEvent from the #GAsyncQueue on the #GstHarness sinkpad.
1823 * See gst_harness_try_pull for details.
1827 * Returns: a #GstEvent or %NULL if no buffers are present in the #GAsyncQueue
1832 gst_harness_try_pull_event (GstHarness * h)
1834 GstHarnessPrivate *priv = h->priv;
1835 return (GstEvent *) g_async_queue_try_pop (priv->sink_event_queue);
1839 * gst_harness_events_received:
1842 * The total number of #GstEvents that has arrived on the #GstHarness sinkpad
1843 * This number includes events handled by the harness as well as events
1844 * that have already been pulled out.
1848 * Returns: a #guint number of events received
1853 gst_harness_events_received (GstHarness * h)
1855 GstHarnessPrivate *priv = h->priv;
1856 return g_atomic_int_get (&priv->recv_events);
1860 * gst_harness_events_in_queue:
1863 * The number of #GstEvents currently in the #GstHarness sinkpad #GAsyncQueue
1867 * Returns: a #guint number of events in the queue
1872 gst_harness_events_in_queue (GstHarness * h)
1874 GstHarnessPrivate *priv = h->priv;
1875 return g_async_queue_length (priv->sink_event_queue);
1879 * gst_harness_push_upstream_event:
1881 * @event: a #GstEvent to push
1883 * Pushes an #GstEvent on the #GstHarness sinkpad.
1887 * Returns: a #gboolean with the result from the push
1892 gst_harness_push_upstream_event (GstHarness * h, GstEvent * event)
1894 g_return_val_if_fail (event != NULL, FALSE);
1895 g_return_val_if_fail (GST_EVENT_IS_UPSTREAM (event), FALSE);
1897 return gst_pad_push_event (h->sinkpad, event);
1901 * gst_harness_pull_upstream_event:
1904 * Pulls an #GstEvent from the #GAsyncQueue on the #GstHarness srcpad.
1905 * Timeouts after 60 seconds similar to gst_harness_pull.
1909 * Returns: a #GstEvent or %NULL if timed out.
1914 gst_harness_pull_upstream_event (GstHarness * h)
1916 GstHarnessPrivate *priv = h->priv;
1917 return (GstEvent *) g_async_queue_timeout_pop (priv->src_event_queue,
1918 G_USEC_PER_SEC * 60);
1922 * gst_harness_try_pull_upstream_event:
1925 * Pulls an #GstEvent from the #GAsyncQueue on the #GstHarness srcpad.
1926 * See gst_harness_try_pull for details.
1930 * Returns: a #GstEvent or %NULL if no buffers are present in the #GAsyncQueue
1935 gst_harness_try_pull_upstream_event (GstHarness * h)
1937 GstHarnessPrivate *priv = h->priv;
1938 return (GstEvent *) g_async_queue_try_pop (priv->src_event_queue);
1942 * gst_harness_upstream_events_received:
1945 * The total number of #GstEvents that has arrived on the #GstHarness srcpad
1946 * This number includes events handled by the harness as well as events
1947 * that have already been pulled out.
1951 * Returns: a #guint number of events received
1956 gst_harness_upstream_events_received (GstHarness * h)
1958 GstHarnessPrivate *priv = h->priv;
1959 return g_atomic_int_get (&priv->recv_upstream_events);
1963 * gst_harness_upstream_events_in_queue:
1966 * The number of #GstEvents currently in the #GstHarness srcpad #GAsyncQueue
1970 * Returns: a #guint number of events in the queue
1975 gst_harness_upstream_events_in_queue (GstHarness * h)
1977 GstHarnessPrivate *priv = h->priv;
1978 return g_async_queue_length (priv->src_event_queue);
1982 * gst_harness_query_latency:
1985 * Get the min latency reported by any harnessed #GstElement.
1989 * Returns: a #GstClockTime with min latency
1994 gst_harness_query_latency (GstHarness * h)
1998 GstClockTime min = GST_CLOCK_TIME_NONE;
2001 query = gst_query_new_latency ();
2003 if (gst_pad_peer_query (h->sinkpad, query)) {
2004 gst_query_parse_latency (query, &is_live, &min, &max);
2006 gst_query_unref (query);
2012 * gst_harness_set_upstream_latency:
2014 * @latency: a #GstClockTime specifying the latency
2016 * Sets the min latency reported by #GstHarness when receiving a latency-query
2021 gst_harness_set_upstream_latency (GstHarness * h, GstClockTime latency)
2023 GstHarnessPrivate *priv = h->priv;
2024 priv->latency_min = latency;
2028 * gst_harness_get_allocator:
2030 * @allocator: (out) (allow-none) (transfer none): the #GstAllocator used
2031 * @params: (out) (allow-none) (transfer full): the #GstAllocationParams of
2034 * Gets the @allocator and its @params that has been decided to use after an
2042 gst_harness_get_allocator (GstHarness * h, GstAllocator ** allocator,
2043 GstAllocationParams * params)
2045 GstHarnessPrivate *priv = h->priv;
2047 *allocator = priv->allocator;
2049 *params = priv->allocation_params;
2054 * gst_harness_set_propose_allocator:
2056 * @allocator: (allow-none) (transfer full): a #GstAllocator
2057 * @params: (allow-none) (transfer none): a #GstAllocationParams
2059 * Sets the @allocator and @params to propose when receiving an allocation
2067 gst_harness_set_propose_allocator (GstHarness * h, GstAllocator * allocator,
2068 const GstAllocationParams * params)
2070 GstHarnessPrivate *priv = h->priv;
2072 priv->propose_allocator = allocator;
2074 priv->propose_allocation_params = *params;
2078 * gst_harness_add_src_harness:
2080 * @src_harness: (transfer full): a #GstHarness to be added as a src-harness.
2081 * @has_clock_wait: a #gboolean specifying if the #GstElement uses
2082 * gst_clock_wait_id internally.
2084 * A src-harness is a great way of providing the #GstHarness with data.
2085 * By adding a src-type #GstElement, it is then easy to use functions like
2086 * gst_harness_push_from_src or gst_harness_src_crank_and_push_many
2087 * to provide your harnessed element with input. The @has_clock_wait variable
2088 * is a greate way to control you src-element with, in that you can have it
2089 * produce a buffer for you by simply cranking the clock, and not have it
2090 * spin out of control producing buffers as fast as possible.
2092 * If a src-harness already exists it will be replaced.
2099 gst_harness_add_src_harness (GstHarness * h,
2100 GstHarness * src_harness, gboolean has_clock_wait)
2103 gst_harness_teardown (h->src_harness);
2104 h->src_harness = src_harness;
2105 gst_harness_set_forward_pad (h->src_harness, h->srcpad);
2106 h->src_harness->priv->has_clock_wait = has_clock_wait;
2107 gst_harness_set_forwarding (h->src_harness, h->priv->forwarding);
2111 * gst_harness_add_src:
2113 * @src_element_name: a #gchar with the name of a #GstElement
2114 * @has_clock_wait: a #gboolean specifying if the #GstElement uses
2115 * gst_clock_wait_id internally.
2117 * Similar to gst_harness_add_src_harness, this is a convenience to
2118 * directly create a src-harness using the @src_element_name name specified.
2125 gst_harness_add_src (GstHarness * h,
2126 const gchar * src_element_name, gboolean has_clock_wait)
2128 GstHarness *src_harness = gst_harness_new (src_element_name);
2129 gst_harness_add_src_harness (h, src_harness, has_clock_wait);
2133 * gst_harness_add_src_parse:
2135 * @launchline: a #gchar describing a gst-launch type line
2136 * @has_clock_wait: a #gboolean specifying if the #GstElement uses
2137 * gst_clock_wait_id internally.
2139 * Similar to gst_harness_add_src, this allows you to specify a launch-line,
2140 * which can be useful for both having more then one #GstElement acting as your
2141 * src (Like a src producing raw buffers, and then an encoder, providing encoded
2142 * data), but also by allowing you to set properties like "is-live" directly on
2150 gst_harness_add_src_parse (GstHarness * h,
2151 const gchar * launchline, gboolean has_clock_wait)
2153 GstHarness *src_harness = gst_harness_new_parse (launchline);
2154 gst_harness_add_src_harness (h, src_harness, has_clock_wait);
2158 * gst_harness_push_from_src:
2161 * Transfer data from the src-#GstHarness to the main-#GstHarness. It consists
2163 * 1: Make sure the src is started. (see: gst_harness_play)
2164 * 2: Crank the clock (see: gst_harness_crank_single_clock_wait)
2165 * 3: Pull a #GstBuffer from the src-#GstHarness (see: gst_harness_pull)
2166 * 4: Push the same #GstBuffer into the main-#GstHarness (see: gst_harness_push)
2170 * Returns: a #GstFlowReturn with the result of the push
2175 gst_harness_push_from_src (GstHarness * h)
2180 g_assert (h->src_harness);
2182 /* FIXME: this *is* the right time to start the src,
2183 but maybe a flag so we don't keep telling it to play? */
2184 gst_harness_play (h->src_harness);
2186 if (h->src_harness->priv->has_clock_wait) {
2187 crank = gst_harness_crank_single_clock_wait (h->src_harness);
2191 buf = gst_harness_pull (h->src_harness);
2192 g_assert (buf != NULL);
2193 return gst_harness_push (h, buf);
2197 * gst_harness_src_crank_and_push_many:
2199 * @cranks: a #gint with the number of calls to gst_harness_crank_single_clock_wait
2200 * @pushes: a #gint with the number of calls to gst_harness_push
2202 * Transfer data from the src-#GstHarness to the main-#GstHarness. Similar to
2203 * gst_harness_push_from_src, this variant allows you to specify how many cranks
2204 * and how many pushes to perform. This can be useful for both moving a lot
2205 * of data at the same time, as well as cases when one crank does not equal one
2206 * buffer to push and v.v.
2210 * Returns: a #GstFlowReturn with the result of the push
2215 gst_harness_src_crank_and_push_many (GstHarness * h, gint cranks, gint pushes)
2217 GstFlowReturn ret = GST_FLOW_OK;
2221 g_assert (h->src_harness);
2222 gst_harness_play (h->src_harness);
2224 for (i = 0; i < cranks; i++) {
2225 crank = gst_harness_crank_single_clock_wait (h->src_harness);
2229 for (i = 0; i < pushes; i++) {
2231 buf = gst_harness_pull (h->src_harness);
2232 g_assert (buf != NULL);
2233 ret = gst_harness_push (h, buf);
2234 if (ret != GST_FLOW_OK)
2242 * gst_harness_src_push_event:
2245 * Similar to what gst_harness_src_push does with #GstBuffers, this transfers
2246 * a #GstEvent from the src-#GstHarness to the main-#GstHarness. Note that
2247 * some #GstEvents are being transferred automagically. Look at sink_forward_pad
2252 * Returns: a #gboolean with the result of the push
2257 gst_harness_src_push_event (GstHarness * h)
2259 return gst_harness_push_event (h, gst_harness_pull_event (h->src_harness));
2264 forward_sticky_events (GstPad * pad, GstEvent ** ev, gpointer user_data)
2266 GstHarness *h = user_data;
2267 return gst_pad_push_event (h->priv->sink_forward_pad, gst_event_ref (*ev));
2271 * gst_harness_add_sink_harness:
2273 * @sink_harness: (transfer full): a #GstHarness to be added as a sink-harness.
2275 * Similar to gst_harness_add_src, this allows you to send the data coming out
2276 * of your harnessed #GstElement to a sink-element, allowing to test different
2277 * responses the element output might create in sink elements. An example might
2278 * be an existing sink providing some analytical data on the input it receives that
2279 * can be useful to your testing. If the goal is to test a sink-element itself,
2280 * this is better acheived using gst_harness_new directly on the sink.
2282 * If a sink-harness already exists it will be replaced.
2289 gst_harness_add_sink_harness (GstHarness * h, GstHarness * sink_harness)
2291 GstHarnessPrivate *priv = h->priv;
2293 if (h->sink_harness) {
2294 gst_harness_set_forward_pad (h, NULL);
2295 gst_harness_teardown (h->sink_harness);
2297 h->sink_harness = sink_harness;
2298 gst_harness_set_forward_pad (h, h->sink_harness->srcpad);
2299 if (priv->forwarding && h->sinkpad)
2300 gst_pad_sticky_events_foreach (h->sinkpad, forward_sticky_events, h);
2301 gst_harness_set_forwarding (h->sink_harness, priv->forwarding);
2305 * gst_harness_add_sink:
2307 * @sink_element_name: a #gchar with the name of a #GstElement
2309 * Similar to gst_harness_add_sink_harness, this is a convenience to
2310 * directly create a sink-harness using the @sink_element_name name specified.
2317 gst_harness_add_sink (GstHarness * h, const gchar * sink_element_name)
2319 GstHarness *sink_harness = gst_harness_new (sink_element_name);
2320 gst_harness_add_sink_harness (h, sink_harness);
2324 * gst_harness_add_sink_parse:
2326 * @launchline: a #gchar with the name of a #GstElement
2328 * Similar to gst_harness_add_sink, this allows you to specify a launch-line
2329 * instead of just an element name. See gst_harness_add_src_parse for details.
2336 gst_harness_add_sink_parse (GstHarness * h, const gchar * launchline)
2338 GstHarness *sink_harness = gst_harness_new_parse (launchline);
2339 gst_harness_add_sink_harness (h, sink_harness);
2343 * gst_harness_push_to_sink:
2346 * Transfer one #GstBuffer from the main-#GstHarness to the sink-#GstHarness.
2347 * See gst_harness_push_from_src for details.
2351 * Returns: a #GstFlowReturn with the result of the push
2356 gst_harness_push_to_sink (GstHarness * h)
2359 g_assert (h->sink_harness);
2360 buf = gst_harness_pull (h);
2361 g_assert (buf != NULL);
2362 return gst_harness_push (h->sink_harness, buf);
2366 * gst_harness_sink_push_many:
2368 * @pushes: a #gint with the number of calls to gst_harness_push_to_sink
2370 * Convenience that calls gst_harness_push_to_sink @pushes number of times.
2371 * Will abort the pushing if any one push fails.
2375 * Returns: a #GstFlowReturn with the result of the push
2380 gst_harness_sink_push_many (GstHarness * h, gint pushes)
2382 GstFlowReturn ret = GST_FLOW_OK;
2384 g_assert (h->sink_harness);
2385 for (i = 0; i < pushes; i++) {
2386 ret = gst_harness_push_to_sink (h);
2387 if (ret != GST_FLOW_OK)
2394 * gst_harness_find_element:
2396 * @element_name: a #gchar with a #GstElementFactory name
2398 * Most useful in conjunction with gst_harness_new_parse, this will scan the
2399 * #GstElements inside the #GstHarness, and check if any of them matches
2400 * @element_name. Typical usecase being that you need to access one of the
2401 * harnessed elements for properties and/or signals.
2405 * Returns: (transfer full) (allow-none): a #GstElement or %NULL if not found
2410 gst_harness_find_element (GstHarness * h, const gchar * element_name)
2412 gboolean done = FALSE;
2414 GValue data = G_VALUE_INIT;
2416 iter = gst_bin_iterate_elements (GST_BIN (h->element));
2420 switch (gst_iterator_next (iter, &data)) {
2421 case GST_ITERATOR_OK:
2423 GstElement *element = g_value_get_object (&data);
2424 GstPluginFeature *feature =
2425 GST_PLUGIN_FEATURE (gst_element_get_factory (element));
2426 if (!strcmp (element_name, gst_plugin_feature_get_name (feature))) {
2427 gst_iterator_free (iter);
2430 g_value_reset (&data);
2433 case GST_ITERATOR_RESYNC:
2434 gst_iterator_resync (iter);
2436 case GST_ITERATOR_ERROR:
2437 case GST_ITERATOR_DONE:
2442 gst_iterator_free (iter);
2450 * @element_name: a #gchar with a #GstElementFactory name
2451 * @first_property_name: a #gchar with the first property name
2452 * @...: value for the first property, followed optionally by more
2453 * name/value pairs, followed by %NULL
2455 * A convenience function to allows you to call g_object_set on a #GstElement
2456 * that are residing inside the #GstHarness, by using normal g_object_set
2464 gst_harness_set (GstHarness * h,
2465 const gchar * element_name, const gchar * first_property_name, ...)
2468 GstElement *element = gst_harness_find_element (h, element_name);
2469 va_start (var_args, first_property_name);
2470 g_object_set_valist (G_OBJECT (element), first_property_name, var_args);
2472 gst_object_unref (element);
2478 * @element_name: a #gchar with a #GstElementFactory name
2479 * @first_property_name: a #gchar with the first property name
2480 * @...: return location for the first property, followed optionally by more
2481 * name/return location pairs, followed by %NULL
2483 * A convenience function to allows you to call g_object_get on a #GstElement
2484 * that are residing inside the #GstHarness, by using normal g_object_get
2492 gst_harness_get (GstHarness * h,
2493 const gchar * element_name, const gchar * first_property_name, ...)
2496 GstElement *element = gst_harness_find_element (h, element_name);
2497 va_start (var_args, first_property_name);
2498 g_object_get_valist (G_OBJECT (element), first_property_name, var_args);
2500 gst_object_unref (element);
2504 * gst_harness_add_probe:
2506 * @element_name: a #gchar with a #GstElementFactory name
2507 * @pad_name: a #gchar with the name of the pad to attach the probe to
2508 * @mask: a #GstPadProbeType (see gst_pad_add_probe)
2509 * @callback: a #GstPadProbeCallback (see gst_pad_add_probe)
2510 * @user_data: a #gpointer (see gst_pad_add_probe)
2511 * @destroy_data: a #GDestroyNotify (see gst_pad_add_probe)
2513 * A convenience function to allows you to call gst_pad_add_probe on a
2514 * #GstPad of a #GstElement that are residing inside the #GstHarness,
2515 * by using normal gst_pad_add_probe syntax
2522 gst_harness_add_probe (GstHarness * h,
2523 const gchar * element_name, const gchar * pad_name, GstPadProbeType mask,
2524 GstPadProbeCallback callback, gpointer user_data,
2525 GDestroyNotify destroy_data)
2527 GstElement *element = gst_harness_find_element (h, element_name);
2528 GstPad *pad = gst_element_get_static_pad (element, pad_name);
2529 gst_pad_add_probe (pad, mask, callback, user_data, destroy_data);
2530 gst_object_unref (pad);
2531 gst_object_unref (element);
2534 /******************************************************************************/
2536 /******************************************************************************/
2537 struct _GstHarnessThread
2545 GDestroyNotify freefunc;
2555 } GstHarnessCustomThread;
2563 GstHarnessPrepareBufferFunc func;
2565 GDestroyNotify notify;
2566 } GstHarnessPushBufferThread;
2572 GstHarnessPrepareEventFunc func;
2574 GDestroyNotify notify;
2575 } GstHarnessPushEventThread;
2583 } GstHarnessPropThread;
2589 GstPadTemplate *templ;
2595 } GstHarnessReqPadThread;
2598 gst_harness_thread_init (GstHarnessThread * t, GDestroyNotify freefunc,
2599 GstHarness * h, gulong sleep)
2601 t->freefunc = freefunc;
2605 g_ptr_array_add (h->priv->stress, t);
2609 gst_harness_thread_free (GstHarnessThread * t)
2611 g_slice_free (GstHarnessThread, t);
2615 gst_harness_custom_thread_free (GstHarnessCustomThread * t)
2617 g_slice_free (GstHarnessCustomThread, t);
2621 gst_harness_push_buffer_thread_free (GstHarnessPushBufferThread * t)
2624 gst_caps_replace (&t->caps, NULL);
2625 if (t->notify != NULL)
2626 t->notify (t->data);
2627 g_slice_free (GstHarnessPushBufferThread, t);
2632 gst_harness_push_event_thread_free (GstHarnessPushEventThread * t)
2635 if (t->notify != NULL)
2636 t->notify (t->data);
2637 g_slice_free (GstHarnessPushEventThread, t);
2642 gst_harness_property_thread_free (GstHarnessPropThread * t)
2646 g_value_unset (&t->value);
2647 g_slice_free (GstHarnessPropThread, t);
2652 gst_harness_requestpad_release (GstPad * pad, GstElement * element)
2654 gst_element_release_request_pad (element, pad);
2655 gst_object_unref (pad);
2659 gst_harness_requestpad_release_pads (GstHarnessReqPadThread * rpt)
2661 g_slist_foreach (rpt->pads, (GFunc) gst_harness_requestpad_release,
2663 g_slist_free (rpt->pads);
2668 gst_harness_requestpad_thread_free (GstHarnessReqPadThread * t)
2671 gst_object_replace ((GstObject **) & t->templ, NULL);
2673 gst_caps_replace (&t->caps, NULL);
2675 gst_harness_requestpad_release_pads (t);
2676 g_slice_free (GstHarnessReqPadThread, t);
2680 #define GST_HARNESS_THREAD_START(ID, t) \
2681 (((GstHarnessThread *)t)->running = TRUE, \
2682 ((GstHarnessThread *)t)->thread = g_thread_new ( \
2683 "gst-harness-stress-"G_STRINGIFY(ID), \
2684 (GThreadFunc)gst_harness_stress_##ID##_func, t))
2685 #define GST_HARNESS_THREAD_END(t) \
2686 (t->running = FALSE, \
2687 GPOINTER_TO_UINT (g_thread_join (t->thread)))
2690 gst_harness_stress_free (GstHarnessThread * t)
2692 if (t != NULL && t->freefunc != NULL)
2697 gst_harness_stress_custom_func (GstHarnessThread * t)
2699 GstHarnessCustomThread *ct = (GstHarnessCustomThread *) t;
2702 if (ct->init != NULL)
2703 ct->init (ct, ct->data);
2705 while (t->running) {
2706 ct->callback (ct, ct->data);
2709 g_usleep (t->sleep);
2711 return GUINT_TO_POINTER (count);
2716 gst_harness_stress_statechange_func (GstHarnessThread * t)
2720 while (t->running) {
2721 GstClock *clock = gst_element_get_clock (t->h->element);
2723 gboolean done = FALSE;
2726 change = gst_element_set_state (t->h->element, GST_STATE_NULL);
2727 g_assert (change == GST_STATE_CHANGE_SUCCESS);
2730 it = gst_element_iterate_sink_pads (t->h->element);
2732 GValue item = G_VALUE_INIT;
2733 switch (gst_iterator_next (it, &item)) {
2734 case GST_ITERATOR_OK:
2736 GstPad *sinkpad = g_value_get_object (&item);
2737 GstPad *srcpad = gst_pad_get_peer (sinkpad);
2738 if (srcpad != NULL) {
2739 gst_pad_unlink (srcpad, sinkpad);
2740 gst_pad_link (srcpad, sinkpad);
2741 gst_object_unref (srcpad);
2743 g_value_reset (&item);
2746 case GST_ITERATOR_RESYNC:
2747 gst_iterator_resync (it);
2749 case GST_ITERATOR_ERROR:
2750 g_assert_not_reached ();
2751 case GST_ITERATOR_DONE:
2755 g_value_unset (&item);
2757 gst_iterator_free (it);
2759 if (clock != NULL) {
2760 gst_element_set_clock (t->h->element, clock);
2761 gst_object_unref (clock);
2763 change = gst_element_set_state (t->h->element, GST_STATE_PLAYING);
2764 g_assert (change == GST_STATE_CHANGE_SUCCESS);
2767 g_usleep (t->sleep);
2769 return GUINT_TO_POINTER (count);
2773 gst_harness_stress_buffer_func (GstHarnessThread * t)
2775 GstHarnessPushBufferThread *pt = (GstHarnessPushBufferThread *) t;
2780 /* Push stream start, caps and segment events */
2781 sid = g_strdup_printf ("%s-%p", GST_OBJECT_NAME (t->h->element), t->h);
2782 handled = gst_pad_push_event (t->h->srcpad, gst_event_new_stream_start (sid));
2785 handled = gst_pad_push_event (t->h->srcpad, gst_event_new_caps (pt->caps));
2787 handled = gst_pad_push_event (t->h->srcpad,
2788 gst_event_new_segment (&pt->segment));
2791 while (t->running) {
2792 gst_harness_push (t->h, pt->func (t->h, pt->data));
2795 g_usleep (t->sleep);
2797 return GUINT_TO_POINTER (count);
2801 gst_harness_stress_event_func (GstHarnessThread * t)
2803 GstHarnessPushEventThread *pet = (GstHarnessPushEventThread *) t;
2806 while (t->running) {
2807 gst_harness_push_event (t->h, pet->func (t->h, pet->data));
2810 g_usleep (t->sleep);
2812 return GUINT_TO_POINTER (count);
2816 gst_harness_stress_upstream_event_func (GstHarnessThread * t)
2818 GstHarnessPushEventThread *pet = (GstHarnessPushEventThread *) t;
2821 while (t->running) {
2822 gst_harness_push_upstream_event (t->h, pet->func (t->h, pet->data));
2825 g_usleep (t->sleep);
2827 return GUINT_TO_POINTER (count);
2831 gst_harness_stress_property_func (GstHarnessThread * t)
2833 GstHarnessPropThread *pt = (GstHarnessPropThread *) t;
2836 while (t->running) {
2837 GValue value = G_VALUE_INIT;
2839 g_object_set_property (G_OBJECT (t->h->element), pt->name, &pt->value);
2841 g_value_init (&value, G_VALUE_TYPE (&pt->value));
2842 g_object_get_property (G_OBJECT (t->h->element), pt->name, &value);
2843 g_value_reset (&value);
2846 g_usleep (t->sleep);
2848 return GUINT_TO_POINTER (count);
2852 gst_harness_stress_requestpad_func (GstHarnessThread * t)
2854 GstHarnessReqPadThread *rpt = (GstHarnessReqPadThread *) t;
2857 while (t->running) {
2861 gst_harness_requestpad_release_pads (rpt);
2865 reqpad = gst_element_request_pad (t->h->element,
2866 rpt->templ, rpt->name, rpt->caps);
2868 g_assert (reqpad != NULL);
2870 rpt->pads = g_slist_prepend (rpt->pads, reqpad);
2873 g_usleep (t->sleep);
2875 return GUINT_TO_POINTER (count);
2879 * gst_harness_stress_thread_stop:
2880 * @t: a #GstHarnessThread
2882 * Stop the running #GstHarnessThread
2889 gst_harness_stress_thread_stop (GstHarnessThread * t)
2893 g_return_val_if_fail (t != NULL, 0);
2895 ret = GST_HARNESS_THREAD_END (t);
2896 g_ptr_array_remove (t->h->priv->stress, t);
2901 * gst_harness_stress_custom_start: (skip)
2903 * @init: (allow-none): a #GFunc that is called initially and only once
2904 * @callback: a #GFunc that is called as often as possible
2905 * @data: a #gpointer with custom data to pass to the @callback function
2906 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
2907 * each call to the @callback
2909 * Start a custom stress-thread that will call your @callback for every
2910 * iteration allowing you to do something nasty.
2914 * Returns: a #GstHarnessThread
2919 gst_harness_stress_custom_start (GstHarness * h,
2920 GFunc init, GFunc callback, gpointer data, gulong sleep)
2922 GstHarnessCustomThread *t = g_slice_new0 (GstHarnessCustomThread);
2923 gst_harness_thread_init (&t->t,
2924 (GDestroyNotify) gst_harness_custom_thread_free, h, sleep);
2927 t->callback = callback;
2930 GST_HARNESS_THREAD_START (custom, t);
2935 * gst_harness_stress_statechange_start_full: (skip)
2937 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
2940 * Change the state of your harnessed #GstElement from NULL to PLAYING and
2941 * back again, only pausing for @sleep microseconds every time.
2945 * Returns: a #GstHarnessThread
2950 gst_harness_stress_statechange_start_full (GstHarness * h, gulong sleep)
2952 GstHarnessThread *t = g_slice_new0 (GstHarnessThread);
2953 gst_harness_thread_init (t,
2954 (GDestroyNotify) gst_harness_thread_free, h, sleep);
2955 GST_HARNESS_THREAD_START (statechange, t);
2960 gst_harness_ref_buffer (GstHarness * h, gpointer data)
2963 return gst_buffer_ref (GST_BUFFER_CAST (data));
2967 gst_harness_ref_event (GstHarness * h, gpointer data)
2970 return gst_event_ref (GST_EVENT_CAST (data));
2974 * gst_harness_stress_push_buffer_start_full: (skip)
2976 * @caps: a #GstCaps for the #GstBuffer
2977 * @segment: a #GstSegment
2978 * @buf: a #GstBuffer to push
2979 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
2980 * each call to gst_pad_push
2982 * Push a #GstBuffer in intervals of @sleep microseconds.
2986 * Returns: a #GstHarnessThread
2991 gst_harness_stress_push_buffer_start_full (GstHarness * h,
2992 GstCaps * caps, const GstSegment * segment, GstBuffer * buf, gulong sleep)
2994 return gst_harness_stress_push_buffer_with_cb_start_full (h, caps, segment,
2995 gst_harness_ref_buffer, gst_buffer_ref (buf),
2996 (GDestroyNotify) gst_buffer_unref, sleep);
3000 * gst_harness_stress_push_buffer_with_cb_start_full: (skip)
3002 * @caps: a #GstCaps for the #GstBuffer
3003 * @segment: a #GstSegment
3004 * @func: a #GstHarnessPrepareBufferFunc function called before every iteration
3005 * to prepare / create a #GstBuffer for pushing
3006 * @data: a #gpointer with data to the #GstHarnessPrepareBufferFunc function
3007 * @notify: a #GDestroyNotify that is called when thread is stopped
3008 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3009 * each call to gst_pad_push
3011 * Push a #GstBuffer returned by @func in intervals of @sleep microseconds.
3015 * Returns: a #GstHarnessThread
3020 gst_harness_stress_push_buffer_with_cb_start_full (GstHarness * h,
3021 GstCaps * caps, const GstSegment * segment,
3022 GstHarnessPrepareBufferFunc func, gpointer data, GDestroyNotify notify,
3025 GstHarnessPushBufferThread *t = g_slice_new0 (GstHarnessPushBufferThread);
3026 gst_harness_thread_init (&t->t,
3027 (GDestroyNotify) gst_harness_push_buffer_thread_free, h, sleep);
3029 gst_caps_replace (&t->caps, caps);
3030 t->segment = *segment;
3035 GST_HARNESS_THREAD_START (buffer, t);
3040 * gst_harness_stress_push_event_start_full: (skip)
3042 * @event: a #GstEvent to push
3043 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3044 * each gst_event_push with @event
3046 * Push the @event onto the harnessed #GstElement sinkpad in intervals of
3047 * @sleep microseconds
3051 * Returns: a #GstHarnessThread
3056 gst_harness_stress_push_event_start_full (GstHarness * h,
3057 GstEvent * event, gulong sleep)
3059 return gst_harness_stress_push_event_with_cb_start_full (h,
3060 gst_harness_ref_event, gst_event_ref (event),
3061 (GDestroyNotify) gst_event_unref, sleep);
3065 * gst_harness_stress_push_event_with_cb_start_full: (skip)
3067 * @func: a #GstHarnessPrepareEventFunc function called before every iteration
3068 * to prepare / create a #GstEvent for pushing
3069 * @data: a #gpointer with data to the #GstHarnessPrepareEventFunc function
3070 * @notify: a #GDestroyNotify that is called when thread is stopped
3071 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3072 * each call to gst_pad_push
3074 * Push a #GstEvent returned by @func onto the harnessed #GstElement sinkpad
3075 * in intervals of @sleep microseconds.
3079 * Returns: a #GstHarnessThread
3084 gst_harness_stress_push_event_with_cb_start_full (GstHarness * h,
3085 GstHarnessPrepareEventFunc func, gpointer data, GDestroyNotify notify,
3088 GstHarnessPushEventThread *t = g_slice_new0 (GstHarnessPushEventThread);
3089 gst_harness_thread_init (&t->t,
3090 (GDestroyNotify) gst_harness_push_event_thread_free, h, sleep);
3096 GST_HARNESS_THREAD_START (event, t);
3101 * gst_harness_stress_push_upstream_event_start_full: (skip)
3103 * @event: a #GstEvent to push
3104 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3105 * each gst_event_push with @event
3107 * Push the @event onto the harnessed #GstElement srcpad in intervals of
3108 * @sleep microseconds.
3112 * Returns: a #GstHarnessThread
3117 gst_harness_stress_push_upstream_event_start_full (GstHarness * h,
3118 GstEvent * event, gulong sleep)
3120 return gst_harness_stress_push_upstream_event_with_cb_start_full (h,
3121 gst_harness_ref_event, gst_event_ref (event),
3122 (GDestroyNotify) gst_event_unref, sleep);
3126 * gst_harness_stress_push_upstream_event_with_cb_start_full: (skip)
3128 * @func: a #GstHarnessPrepareEventFunc function called before every iteration
3129 * to prepare / create a #GstEvent for pushing
3130 * @data: a #gpointer with data to the #GstHarnessPrepareEventFunc function
3131 * @notify: a #GDestroyNotify that is called when thread is stopped
3132 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3133 * each call to gst_pad_push
3135 * Push a #GstEvent returned by @func onto the harnessed #GstElement srcpad
3136 * in intervals of @sleep microseconds.
3140 * Returns: a #GstHarnessThread
3145 gst_harness_stress_push_upstream_event_with_cb_start_full (GstHarness * h,
3146 GstHarnessPrepareEventFunc func, gpointer data, GDestroyNotify notify,
3149 GstHarnessPushEventThread *t = g_slice_new0 (GstHarnessPushEventThread);
3150 gst_harness_thread_init (&t->t,
3151 (GDestroyNotify) gst_harness_push_event_thread_free, h, sleep);
3157 GST_HARNESS_THREAD_START (upstream_event, t);
3162 * gst_harness_stress_property_start_full: (skip)
3164 * @name: a #gchar specifying a property name
3165 * @value: a #GValue to set the property to
3166 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3167 * each g_object_set with @name and @value
3169 * Call g_object_set with @name and @value in intervals of @sleep microseconds
3173 * Returns: a #GstHarnessThread
3178 gst_harness_stress_property_start_full (GstHarness * h,
3179 const gchar * name, const GValue * value, gulong sleep)
3181 GstHarnessPropThread *t = g_slice_new0 (GstHarnessPropThread);
3182 gst_harness_thread_init (&t->t,
3183 (GDestroyNotify) gst_harness_property_thread_free, h, sleep);
3185 t->name = g_strdup (name);
3186 g_value_init (&t->value, G_VALUE_TYPE (value));
3187 g_value_copy (value, &t->value);
3189 GST_HARNESS_THREAD_START (property, t);
3194 * gst_harness_stress_requestpad_start_full: (skip)
3196 * @templ: a #GstPadTemplate
3199 * @release: a #gboolean
3200 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3201 * each gst_element_request_pad
3203 * Call gst_element_request_pad in intervals of @sleep microseconds
3207 * Returns: a #GstHarnessThread
3212 gst_harness_stress_requestpad_start_full (GstHarness * h,
3213 GstPadTemplate * templ, const gchar * name, GstCaps * caps,
3214 gboolean release, gulong sleep)
3216 GstHarnessReqPadThread *t = g_slice_new0 (GstHarnessReqPadThread);
3217 gst_harness_thread_init (&t->t,
3218 (GDestroyNotify) gst_harness_requestpad_thread_free, h, sleep);
3220 t->templ = gst_object_ref (templ);
3221 t->name = g_strdup (name);
3222 gst_caps_replace (&t->caps, caps);
3223 t->release = release;
3225 GST_HARNESS_THREAD_START (requestpad, t);