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);
153 GstStructure *params;
157 propose_meta_clear (ProposeMeta * meta)
160 gst_structure_free (meta->params);
163 struct _GstHarnessPrivate
165 gchar *element_sinkpad_name;
166 gchar *element_srcpad_name;
172 GstPad *sink_forward_pad;
173 GstTestClock *testclock;
175 volatile gint recv_buffers;
176 volatile gint recv_events;
177 volatile gint recv_upstream_events;
179 GAsyncQueue *buffer_queue;
180 GAsyncQueue *src_event_queue;
181 GAsyncQueue *sink_event_queue;
183 GstClockTime latency_min;
184 GstClockTime latency_max;
185 gboolean has_clock_wait;
186 gboolean drop_buffers;
187 GstClockTime last_push_ts;
190 GstAllocator *allocator;
191 GstAllocationParams allocation_params;
192 GstAllocator *propose_allocator;
193 GstAllocationParams propose_allocation_params;
195 GArray *propose_allocation_metas;
197 gboolean blocking_push_mode;
198 GCond blocking_push_cond;
199 GMutex blocking_push_mutex;
206 gst_harness_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
208 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
209 GstHarnessPrivate *priv = h->priv;
211 g_assert (h != NULL);
212 g_mutex_lock (&priv->blocking_push_mutex);
213 g_atomic_int_inc (&priv->recv_buffers);
215 if (priv->drop_buffers)
216 gst_buffer_unref (buffer);
218 g_async_queue_push (priv->buffer_queue, buffer);
220 if (priv->blocking_push_mode) {
221 g_cond_wait (&priv->blocking_push_cond, &priv->blocking_push_mutex);
223 g_mutex_unlock (&priv->blocking_push_mutex);
229 gst_harness_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
231 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
232 GstHarnessPrivate *priv = h->priv;
234 g_assert (h != NULL);
235 g_atomic_int_inc (&priv->recv_upstream_events);
236 g_async_queue_push (priv->src_event_queue, event);
241 gst_harness_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
243 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
244 GstHarnessPrivate *priv = h->priv;
248 g_assert (h != NULL);
250 g_atomic_int_inc (&priv->recv_events);
252 switch (GST_EVENT_TYPE (event)) {
253 case GST_EVENT_STREAM_START:
255 case GST_EVENT_SEGMENT:
264 if (priv->forwarding && forward && priv->sink_forward_pad) {
265 GstPad *fwdpad = gst_object_ref (priv->sink_forward_pad);
267 ret = gst_pad_push_event (fwdpad, event);
268 gst_object_unref (fwdpad);
271 g_async_queue_push (priv->sink_event_queue, event);
279 gst_harness_decide_allocation (GstHarness * h, GstCaps * caps)
281 GstHarnessPrivate *priv = h->priv;
283 GstAllocator *allocator;
284 GstAllocationParams params;
285 GstBufferPool *pool = NULL;
286 guint size, min, max;
288 query = gst_query_new_allocation (caps, FALSE);
289 gst_pad_peer_query (h->srcpad, query);
291 if (gst_query_get_n_allocation_params (query) > 0) {
292 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
295 gst_allocation_params_init (¶ms);
298 if (gst_query_get_n_allocation_pools (query) > 0) {
299 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
301 /* Most elements create their own pools if pool == NULL. Not sure if we
302 * want to do that in the harness since we may want to test the pool
303 * implementation of the elements. Not creating a pool will however ignore
304 * the returned size. */
306 pool = gst_buffer_pool_new ();
310 size = min = max = 0;
312 gst_query_unref (query);
315 GstStructure *config = gst_buffer_pool_get_config (pool);
316 gst_buffer_pool_config_set_params (config, caps, size, min, max);
317 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
318 gst_buffer_pool_set_config (pool, config);
321 if (pool != priv->pool) {
322 if (priv->pool != NULL)
323 gst_buffer_pool_set_active (priv->pool, FALSE);
325 gst_buffer_pool_set_active (pool, TRUE);
328 priv->allocation_params = params;
330 gst_object_unref (priv->allocator);
331 priv->allocator = allocator;
333 gst_object_unref (priv->pool);
338 gst_harness_negotiate (GstHarness * h)
342 caps = gst_pad_get_current_caps (h->srcpad);
344 gst_harness_decide_allocation (h, caps);
345 gst_caps_unref (caps);
347 GST_FIXME_OBJECT (h, "Cannot negotiate allocation because caps is not set");
352 gst_harness_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
354 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
355 GstHarnessPrivate *priv = h->priv;
357 g_assert (h != NULL);
359 // FIXME: forward all queries?
361 switch (GST_QUERY_TYPE (query)) {
362 case GST_QUERY_LATENCY:
363 gst_query_set_latency (query, TRUE, priv->latency_min, priv->latency_max);
367 GstCaps *caps, *filter = NULL;
369 if (priv->sink_caps) {
370 caps = gst_caps_ref (priv->sink_caps);
372 caps = gst_pad_get_pad_template_caps (pad);
375 gst_query_parse_caps (query, &filter);
376 if (filter != NULL) {
377 gst_caps_take (&caps,
378 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST));
381 gst_query_set_caps_result (query, caps);
382 gst_caps_unref (caps);
385 case GST_QUERY_ALLOCATION:
388 if (priv->forwarding && priv->sink_forward_pad != NULL) {
389 GstPad *peer = gst_pad_get_peer (priv->sink_forward_pad);
390 g_assert (peer != NULL);
392 res = gst_pad_query (peer, query);
393 gst_object_unref (peer);
400 gst_query_parse_allocation (query, &caps, &need_pool);
402 /* FIXME: Can this be removed? */
403 size = gst_query_get_n_allocation_params (query);
404 g_assert_cmpuint (0, ==, size);
405 gst_query_add_allocation_param (query,
406 priv->propose_allocator, &priv->propose_allocation_params);
408 if (priv->propose_allocation_metas) {
410 for (i = 0; i < priv->propose_allocation_metas->len; i++) {
412 &g_array_index (priv->propose_allocation_metas, ProposeMeta, i);
413 gst_query_add_allocation_meta (query, meta->api, meta->params);
417 GST_DEBUG_OBJECT (pad, "proposing allocation %" GST_PTR_FORMAT,
418 priv->propose_allocator);
424 res = gst_pad_query_default (pad, parent, query);
431 gst_harness_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
433 GstHarness *h = g_object_get_data (G_OBJECT (pad), HARNESS_KEY);
434 GstHarnessPrivate *priv = h->priv;
436 g_assert (h != NULL);
438 switch (GST_QUERY_TYPE (query)) {
439 case GST_QUERY_LATENCY:
440 gst_query_set_latency (query, TRUE, priv->latency_min, priv->latency_max);
444 GstCaps *caps, *filter = NULL;
446 if (priv->src_caps) {
447 caps = gst_caps_ref (priv->src_caps);
449 caps = gst_pad_get_pad_template_caps (pad);
452 gst_query_parse_caps (query, &filter);
453 if (filter != NULL) {
454 gst_caps_take (&caps,
455 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST));
458 gst_query_set_caps_result (query, caps);
459 gst_caps_unref (caps);
463 res = gst_pad_query_default (pad, parent, query);
469 gst_harness_element_ref (GstHarness * h)
473 GST_OBJECT_LOCK (h->element);
474 data = g_object_get_data (G_OBJECT (h->element), HARNESS_REF);
476 data = g_new0 (guint, 1);
478 g_object_set_data_full (G_OBJECT (h->element), HARNESS_REF, data, g_free);
482 GST_OBJECT_UNLOCK (h->element);
486 gst_harness_element_unref (GstHarness * h)
491 GST_OBJECT_LOCK (h->element);
492 data = g_object_get_data (G_OBJECT (h->element), HARNESS_REF);
493 g_assert (data != NULL);
496 GST_OBJECT_UNLOCK (h->element);
502 gst_harness_link_element_srcpad (GstHarness * h,
503 const gchar * element_srcpad_name)
505 GstHarnessPrivate *priv = h->priv;
506 GstPad *srcpad = gst_element_get_static_pad (h->element,
507 element_srcpad_name);
508 GstPadLinkReturn link;
510 srcpad = gst_element_get_request_pad (h->element, element_srcpad_name);
512 link = gst_pad_link (srcpad, h->sinkpad);
513 g_assert_cmpint (link, ==, GST_PAD_LINK_OK);
514 g_free (priv->element_srcpad_name);
515 priv->element_srcpad_name = gst_pad_get_name (srcpad);
517 gst_object_unref (srcpad);
521 gst_harness_link_element_sinkpad (GstHarness * h,
522 const gchar * element_sinkpad_name)
524 GstHarnessPrivate *priv = h->priv;
525 GstPad *sinkpad = gst_element_get_static_pad (h->element,
526 element_sinkpad_name);
527 GstPadLinkReturn link;
529 sinkpad = gst_element_get_request_pad (h->element, element_sinkpad_name);
531 link = gst_pad_link (h->srcpad, sinkpad);
532 g_assert_cmpint (link, ==, GST_PAD_LINK_OK);
533 g_free (priv->element_sinkpad_name);
534 priv->element_sinkpad_name = gst_pad_get_name (sinkpad);
536 gst_object_unref (sinkpad);
540 gst_harness_setup_src_pad (GstHarness * h,
541 GstStaticPadTemplate * src_tmpl, const gchar * element_sinkpad_name)
543 GstHarnessPrivate *priv = h->priv;
545 g_assert (h->srcpad == NULL);
547 priv->src_event_queue =
548 g_async_queue_new_full ((GDestroyNotify) gst_event_unref);
551 h->srcpad = gst_pad_new_from_static_template (src_tmpl, "src");
552 g_assert (h->srcpad);
553 g_object_set_data (G_OBJECT (h->srcpad), HARNESS_KEY, h);
555 gst_pad_set_query_function (h->srcpad, gst_harness_src_query);
556 gst_pad_set_event_function (h->srcpad, gst_harness_src_event);
558 gst_pad_set_active (h->srcpad, TRUE);
560 if (element_sinkpad_name)
561 gst_harness_link_element_sinkpad (h, element_sinkpad_name);
565 gst_harness_setup_sink_pad (GstHarness * h,
566 GstStaticPadTemplate * sink_tmpl, const gchar * element_srcpad_name)
568 GstHarnessPrivate *priv = h->priv;
569 g_assert (sink_tmpl);
570 g_assert (h->sinkpad == NULL);
572 priv->buffer_queue = g_async_queue_new_full (
573 (GDestroyNotify) gst_buffer_unref);
574 priv->sink_event_queue = g_async_queue_new_full (
575 (GDestroyNotify) gst_event_unref);
578 h->sinkpad = gst_pad_new_from_static_template (sink_tmpl, "sink");
579 g_assert (h->sinkpad);
580 g_object_set_data (G_OBJECT (h->sinkpad), HARNESS_KEY, h);
582 gst_pad_set_chain_function (h->sinkpad, gst_harness_chain);
583 gst_pad_set_query_function (h->sinkpad, gst_harness_sink_query);
584 gst_pad_set_event_function (h->sinkpad, gst_harness_sink_event);
586 gst_pad_set_active (h->sinkpad, TRUE);
588 if (element_srcpad_name)
589 gst_harness_link_element_srcpad (h, element_srcpad_name);
593 check_element_type (GstElement * element, gboolean * has_sinkpad,
594 gboolean * has_srcpad)
596 GstElementClass *element_class = GST_ELEMENT_GET_CLASS (element);
597 const GList *tmpl_list;
599 *has_srcpad = element->numsrcpads > 0;
600 *has_sinkpad = element->numsinkpads > 0;
602 tmpl_list = gst_element_class_get_pad_template_list (element_class);
605 GstPadTemplate *pad_tmpl = (GstPadTemplate *) tmpl_list->data;
606 tmpl_list = g_list_next (tmpl_list);
607 if (GST_PAD_TEMPLATE_DIRECTION (pad_tmpl) == GST_PAD_SRC)
609 if (GST_PAD_TEMPLATE_DIRECTION (pad_tmpl) == GST_PAD_SINK)
610 *has_sinkpad |= TRUE;
615 turn_async_and_sync_off (GstElement * element)
617 GObjectClass *class = G_OBJECT_GET_CLASS (element);
618 if (g_object_class_find_property (class, "async"))
619 g_object_set (element, "async", FALSE, NULL);
620 if (g_object_class_find_property (class, "sync"))
621 g_object_set (element, "sync", FALSE, NULL);
625 gst_pad_is_request_pad (GstPad * pad)
627 GstPadTemplate *temp;
632 temp = gst_pad_get_pad_template (pad);
635 is_request = GST_PAD_TEMPLATE_PRESENCE (temp) == GST_PAD_REQUEST;
636 gst_object_unref (temp);
641 * gst_harness_new_empty: (skip)
643 * Creates a new empty harness. Use gst_harness_add_element_full() to add
644 * an #GstElement to it.
648 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
654 gst_harness_new_empty (void)
657 GstHarnessPrivate *priv;
659 h = g_new0 (GstHarness, 1);
660 g_assert (h != NULL);
661 h->priv = g_new0 (GstHarnessPrivate, 1);
664 GST_DEBUG_OBJECT (h, "about to create new harness %p", h);
665 priv->last_push_ts = GST_CLOCK_TIME_NONE;
666 priv->latency_min = 0;
667 priv->latency_max = GST_CLOCK_TIME_NONE;
668 priv->drop_buffers = FALSE;
669 priv->testclock = GST_TEST_CLOCK_CAST (gst_test_clock_new ());
671 priv->propose_allocator = NULL;
672 gst_allocation_params_init (&priv->propose_allocation_params);
674 g_mutex_init (&priv->blocking_push_mutex);
675 g_cond_init (&priv->blocking_push_cond);
676 g_mutex_init (&priv->priv_mutex);
678 priv->stress = g_ptr_array_new_with_free_func (
679 (GDestroyNotify) gst_harness_stress_free);
681 /* we have forwarding on as a default */
682 gst_harness_set_forwarding (h, TRUE);
688 * gst_harness_add_element_full: (skip)
690 * @element: a #GstElement to add to the harness (transfer none)
691 * @hsrc: (allow-none): a #GstStaticPadTemplate describing the harness srcpad.
692 * %NULL will not create a harness srcpad.
693 * @element_sinkpad_name: (allow-none): a #gchar with the name of the element
694 * sinkpad that is then linked to the harness srcpad. Can be a static or request
695 * or a sometimes pad that has been added. %NULL will not get/request a sinkpad
696 * from the element. (Like if the element is a src.)
697 * @hsink: (allow-none): a #GstStaticPadTemplate describing the harness sinkpad.
698 * %NULL will not create a harness sinkpad.
699 * @element_srcpad_name: (allow-none): a #gchar with the name of the element
700 * srcpad that is then linked to the harness sinkpad, similar to the
701 * @element_sinkpad_name.
703 * Adds a #GstElement to an empty #GstHarness
710 gst_harness_add_element_full (GstHarness * h, GstElement * element,
711 GstStaticPadTemplate * hsrc, const gchar * element_sinkpad_name,
712 GstStaticPadTemplate * hsink, const gchar * element_srcpad_name)
714 GstClock *element_clock;
715 gboolean has_sinkpad, has_srcpad;
717 g_return_if_fail (element != NULL);
718 g_return_if_fail (h->element == NULL);
720 element_clock = GST_ELEMENT_CLOCK (element);
721 h->element = gst_object_ref (element);
722 check_element_type (element, &has_sinkpad, &has_srcpad);
724 /* setup the loose srcpad linked to the element sinkpad */
726 gst_harness_setup_src_pad (h, hsrc, element_sinkpad_name);
728 /* setup the loose sinkpad linked to the element srcpad */
730 gst_harness_setup_sink_pad (h, hsink, element_srcpad_name);
732 /* as a harness sink, we should not need sync and async */
733 if (has_sinkpad && !has_srcpad)
734 turn_async_and_sync_off (h->element);
736 if (h->srcpad != NULL) {
738 gchar *stream_id = g_strdup_printf ("%s-%p",
739 GST_OBJECT_NAME (h->element), h);
740 handled = gst_pad_push_event (h->srcpad,
741 gst_event_new_stream_start (stream_id));
746 /* if the element already has a testclock attached,
747 we replace our own with it, if no clock we attach the testclock */
749 if (GST_IS_TEST_CLOCK (element_clock)) {
750 gst_object_replace ((GstObject **) & h->priv->testclock,
751 (GstObject *) GST_ELEMENT_CLOCK (element));
754 gst_harness_use_testclock (h);
757 /* don't start sources, they start producing data! */
759 gst_harness_play (h);
761 gst_harness_element_ref (h);
763 GST_DEBUG_OBJECT (h, "added element to harness %p "
764 "with element_srcpad_name (%p, %s, %s) and element_sinkpad_name (%p, %s, %s)",
765 h, h->srcpad, GST_DEBUG_PAD_NAME (h->srcpad),
766 h->sinkpad, GST_DEBUG_PAD_NAME (h->sinkpad));
770 * gst_harness_new_full: (skip)
771 * @element: a #GstElement to attach the harness to (transfer none)
772 * @hsrc: (allow-none): a #GstStaticPadTemplate describing the harness srcpad.
773 * %NULL will not create a harness srcpad.
774 * @element_sinkpad_name: (allow-none): a #gchar with the name of the element
775 * sinkpad that is then linked to the harness srcpad. Can be a static or request
776 * or a sometimes pad that has been added. %NULL will not get/request a sinkpad
777 * from the element. (Like if the element is a src.)
778 * @hsink: (allow-none): a #GstStaticPadTemplate describing the harness sinkpad.
779 * %NULL will not create a harness sinkpad.
780 * @element_srcpad_name: (allow-none): a #gchar with the name of the element
781 * srcpad that is then linked to the harness sinkpad, similar to the
782 * @element_sinkpad_name.
784 * Creates a new harness.
788 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
794 gst_harness_new_full (GstElement * element,
795 GstStaticPadTemplate * hsrc, const gchar * element_sinkpad_name,
796 GstStaticPadTemplate * hsink, const gchar * element_srcpad_name)
799 h = gst_harness_new_empty ();
800 gst_harness_add_element_full (h, element,
801 hsrc, element_sinkpad_name, hsink, element_srcpad_name);
806 * gst_harness_new_with_element: (skip)
807 * @element: a #GstElement to attach the harness to (transfer none)
808 * @element_sinkpad_name: (allow-none): a #gchar with the name of the element
809 * sinkpad that is then linked to the harness srcpad. %NULL does not attach a
811 * @element_srcpad_name: (allow-none): a #gchar with the name of the element
812 * srcpad that is then linked to the harness sinkpad. %NULL does not attach a
815 * Creates a new harness. Works in the same way as gst_harness_new_full(), only
816 * that generic padtemplates are used for the harness src and sinkpads, which
817 * will be sufficient in most usecases.
821 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
827 gst_harness_new_with_element (GstElement * element,
828 const gchar * element_sinkpad_name, const gchar * element_srcpad_name)
830 return gst_harness_new_full (element,
831 &hsrctemplate, element_sinkpad_name, &hsinktemplate, element_srcpad_name);
835 * gst_harness_new_with_padnames: (skip)
836 * @element_name: a #gchar describing the #GstElement name
837 * @element_sinkpad_name: (allow-none): a #gchar with the name of the element
838 * sinkpad that is then linked to the harness srcpad. %NULL does not attach a
840 * @element_srcpad_name: (allow-none): a #gchar with the name of the element
841 * srcpad that is then linked to the harness sinkpad. %NULL does not attach a
844 * Creates a new harness. Works like gst_harness_new_with_element(),
845 * except you specify the factoryname of the #GstElement
849 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
855 gst_harness_new_with_padnames (const gchar * element_name,
856 const gchar * element_sinkpad_name, const gchar * element_srcpad_name)
859 GstElement *element = gst_element_factory_make (element_name, NULL);
860 g_assert (element != NULL);
862 h = gst_harness_new_with_element (element, element_sinkpad_name,
863 element_srcpad_name);
864 gst_object_unref (element);
869 * gst_harness_new_with_templates: (skip)
870 * @element_name: a #gchar describing the #GstElement name
871 * @hsrc: (allow-none): a #GstStaticPadTemplate describing the harness srcpad.
872 * %NULL will not create a harness srcpad.
873 * @hsink: (allow-none): a #GstStaticPadTemplate describing the harness sinkpad.
874 * %NULL will not create a harness sinkpad.
876 * Creates a new harness, like gst_harness_new_full(), except it
877 * assumes the #GstElement sinkpad is named "sink" and srcpad is named "src"
881 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
887 gst_harness_new_with_templates (const gchar * element_name,
888 GstStaticPadTemplate * hsrc, GstStaticPadTemplate * hsink)
891 GstElement *element = gst_element_factory_make (element_name, NULL);
892 g_assert (element != NULL);
894 h = gst_harness_new_full (element, hsrc, "sink", hsink, "src");
895 gst_object_unref (element);
900 * gst_harness_new: (skip)
901 * @element_name: a #gchar describing the #GstElement name
903 * Creates a new harness. Works like gst_harness_new_with_padnames(), except it
904 * assumes the #GstElement sinkpad is named "sink" and srcpad is named "src"
908 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
914 gst_harness_new (const gchar * element_name)
916 return gst_harness_new_with_padnames (element_name, "sink", "src");
920 * gst_harness_add_parse: (skip)
922 * @launchline: a #gchar describing a gst-launch type line
924 * Parses the @launchline and puts that in a #GstBin,
925 * and then attches the supplied #GstHarness to the bin.
932 gst_harness_add_parse (GstHarness * h, const gchar * launchline)
938 gboolean done = FALSE;
939 GError *error = NULL;
941 g_return_if_fail (launchline != NULL);
943 desc = g_strdup_printf ("bin.( %s )", launchline);
945 (GstBin *) gst_parse_launch_full (desc, NULL, GST_PARSE_FLAG_FATAL_ERRORS,
948 if (G_UNLIKELY (error != NULL)) {
949 g_error ("Unable to create pipeline '%s': %s", desc, error->message);
953 /* find pads and ghost them if necessary */
954 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC)) != NULL) {
955 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("src", pad));
956 gst_object_unref (pad);
958 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SINK)) != NULL) {
959 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("sink", pad));
960 gst_object_unref (pad);
963 iter = gst_bin_iterate_sinks (bin);
965 GValue item = { 0, };
967 switch (gst_iterator_next (iter, &item)) {
968 case GST_ITERATOR_OK:
969 turn_async_and_sync_off (GST_ELEMENT (g_value_get_object (&item)));
970 g_value_reset (&item);
972 case GST_ITERATOR_DONE:
975 case GST_ITERATOR_RESYNC:
976 gst_iterator_resync (iter);
978 case GST_ITERATOR_ERROR:
979 gst_object_unref (bin);
980 gst_iterator_free (iter);
981 g_return_if_reached ();
985 gst_iterator_free (iter);
987 gst_harness_add_element_full (h, GST_ELEMENT_CAST (bin),
988 &hsrctemplate, "sink", &hsinktemplate, "src");
989 gst_object_unref (bin);
993 * gst_harness_new_parse: (skip)
994 * @launchline: a #gchar describing a gst-launch type line
996 * Creates a new harness, parsing the @launchline and putting that in a #GstBin,
997 * and then attches the harness to the bin.
1001 * Returns: (transfer full): a #GstHarness, or %NULL if the harness could
1007 gst_harness_new_parse (const gchar * launchline)
1010 h = gst_harness_new_empty ();
1011 gst_harness_add_parse (h, launchline);
1016 * gst_harness_teardown:
1019 * Tears down a @GstHarness, freeing all resources allocated using it.
1026 gst_harness_teardown (GstHarness * h)
1028 GstHarnessPrivate *priv = h->priv;
1030 if (priv->blocking_push_mode) {
1031 g_mutex_lock (&priv->blocking_push_mutex);
1032 priv->blocking_push_mode = FALSE;
1033 g_cond_signal (&priv->blocking_push_cond);
1034 g_mutex_unlock (&priv->blocking_push_mutex);
1037 if (h->src_harness) {
1038 gst_harness_teardown (h->src_harness);
1041 gst_object_replace ((GstObject **) & priv->sink_forward_pad, NULL);
1042 if (h->sink_harness) {
1043 gst_harness_teardown (h->sink_harness);
1047 gst_caps_unref (priv->src_caps);
1049 if (priv->sink_caps)
1050 gst_caps_unref (priv->sink_caps);
1053 if (gst_pad_is_request_pad (GST_PAD_PEER (h->srcpad)))
1054 gst_element_release_request_pad (h->element, GST_PAD_PEER (h->srcpad));
1055 g_free (priv->element_sinkpad_name);
1057 gst_pad_set_active (h->srcpad, FALSE);
1058 gst_object_unref (h->srcpad);
1060 g_async_queue_unref (priv->src_event_queue);
1064 if (gst_pad_is_request_pad (GST_PAD_PEER (h->sinkpad)))
1065 gst_element_release_request_pad (h->element, GST_PAD_PEER (h->sinkpad));
1066 g_free (priv->element_srcpad_name);
1068 gst_pad_set_active (h->sinkpad, FALSE);
1069 gst_object_unref (h->sinkpad);
1071 g_async_queue_unref (priv->buffer_queue);
1072 g_async_queue_unref (priv->sink_event_queue);
1075 gst_object_replace ((GstObject **) & priv->propose_allocator, NULL);
1076 gst_object_replace ((GstObject **) & priv->allocator, NULL);
1077 gst_object_replace ((GstObject **) & priv->pool, NULL);
1079 if (priv->propose_allocation_metas)
1080 g_array_unref (priv->propose_allocation_metas);
1082 /* if we hold the last ref, set to NULL */
1083 if (gst_harness_element_unref (h) == 0) {
1084 gboolean state_change;
1085 GstState state, pending;
1086 state_change = gst_element_set_state (h->element, GST_STATE_NULL);
1087 g_assert (state_change == GST_STATE_CHANGE_SUCCESS);
1088 state_change = gst_element_get_state (h->element, &state, &pending, 0);
1089 g_assert (state_change == GST_STATE_CHANGE_SUCCESS);
1090 g_assert (state == GST_STATE_NULL);
1093 g_cond_clear (&priv->blocking_push_cond);
1094 g_mutex_clear (&priv->blocking_push_mutex);
1095 g_mutex_clear (&priv->priv_mutex);
1097 g_ptr_array_unref (priv->stress);
1099 gst_object_unref (h->element);
1101 gst_object_replace ((GstObject **) & priv->testclock, NULL);
1108 * gst_harness_add_element_src_pad:
1110 * @srcpad: a #GstPad to link to the harness sinkpad
1112 * Links the specified #GstPad the @GstHarness sinkpad. This can be useful if
1113 * perhaps the srcpad did not exist at the time of creating the harness,
1114 * like a demuxer that provides a sometimes-pad after receiving data.
1121 gst_harness_add_element_src_pad (GstHarness * h, GstPad * srcpad)
1123 GstHarnessPrivate *priv = h->priv;
1124 GstPadLinkReturn link;
1125 if (h->sinkpad == NULL)
1126 gst_harness_setup_sink_pad (h, &hsinktemplate, NULL);
1127 link = gst_pad_link (srcpad, h->sinkpad);
1128 g_assert_cmpint (link, ==, GST_PAD_LINK_OK);
1129 g_free (priv->element_srcpad_name);
1130 priv->element_srcpad_name = gst_pad_get_name (srcpad);
1134 * gst_harness_add_element_sink_pad:
1136 * @sinkpad: a #GstPad to link to the harness srcpad
1138 * Links the specified #GstPad the @GstHarness srcpad.
1145 gst_harness_add_element_sink_pad (GstHarness * h, GstPad * sinkpad)
1147 GstHarnessPrivate *priv = h->priv;
1148 GstPadLinkReturn link;
1149 if (h->srcpad == NULL)
1150 gst_harness_setup_src_pad (h, &hsrctemplate, NULL);
1151 link = gst_pad_link (h->srcpad, sinkpad);
1152 g_assert_cmpint (link, ==, GST_PAD_LINK_OK);
1153 g_free (priv->element_sinkpad_name);
1154 priv->element_sinkpad_name = gst_pad_get_name (sinkpad);
1158 * gst_harness_set_src_caps:
1160 * @caps: (transfer full): a #GstCaps to set on the harness srcpad
1162 * Sets the @GstHarness srcpad caps. This must be done before any buffers
1163 * can legally be pushed from the harness to the element.
1170 gst_harness_set_src_caps (GstHarness * h, GstCaps * caps)
1172 GstHarnessPrivate *priv = h->priv;
1176 handled = gst_pad_push_event (h->srcpad, gst_event_new_caps (caps));
1178 gst_caps_take (&priv->src_caps, caps);
1180 gst_segment_init (&segment, GST_FORMAT_TIME);
1181 handled = gst_pad_push_event (h->srcpad, gst_event_new_segment (&segment));
1185 * gst_harness_set_sink_caps:
1187 * @caps: (transfer full): a #GstCaps to set on the harness sinkpad
1189 * Sets the @GstHarness sinkpad caps.
1196 gst_harness_set_sink_caps (GstHarness * h, GstCaps * caps)
1198 GstHarnessPrivate *priv = h->priv;
1200 gst_caps_take (&priv->sink_caps, caps);
1201 gst_pad_push_event (h->sinkpad, gst_event_new_reconfigure ());
1205 * gst_harness_set_caps:
1207 * @in: (transfer full): a #GstCaps to set on the harness srcpad
1208 * @out: (transfer full): a #GstCaps to set on the harness sinkpad
1210 * Sets the @GstHarness srcpad and sinkpad caps.
1217 gst_harness_set_caps (GstHarness * h, GstCaps * in, GstCaps * out)
1219 gst_harness_set_sink_caps (h, out);
1220 gst_harness_set_src_caps (h, in);
1224 * gst_harness_set_src_caps_str:
1226 * @str: a @gchar describing a #GstCaps to set on the harness srcpad
1228 * Sets the @GstHarness srcpad caps using a string. This must be done before
1229 * any buffers can legally be pushed from the harness to the element.
1236 gst_harness_set_src_caps_str (GstHarness * h, const gchar * str)
1238 gst_harness_set_src_caps (h, gst_caps_from_string (str));
1242 * gst_harness_set_sink_caps_str:
1244 * @str: a @gchar describing a #GstCaps to set on the harness sinkpad
1246 * Sets the @GstHarness sinkpad caps using a string.
1253 gst_harness_set_sink_caps_str (GstHarness * h, const gchar * str)
1255 gst_harness_set_sink_caps (h, gst_caps_from_string (str));
1259 * gst_harness_set_caps_str:
1261 * @in: a @gchar describing a #GstCaps to set on the harness srcpad
1262 * @out: a @gchar describing a #GstCaps to set on the harness sinkpad
1264 * Sets the @GstHarness srcpad and sinkpad caps using strings.
1271 gst_harness_set_caps_str (GstHarness * h, const gchar * in, const gchar * out)
1273 gst_harness_set_sink_caps_str (h, out);
1274 gst_harness_set_src_caps_str (h, in);
1278 * gst_harness_use_systemclock:
1281 * Sets the system #GstClock on the @GstHarness #GstElement
1288 gst_harness_use_systemclock (GstHarness * h)
1290 GstClock *clock = gst_system_clock_obtain ();
1291 g_assert (clock != NULL);
1292 gst_element_set_clock (h->element, clock);
1293 gst_object_unref (clock);
1297 * gst_harness_use_testclock:
1300 * Sets the #GstTestClock on the #GstHarness #GstElement
1307 gst_harness_use_testclock (GstHarness * h)
1309 gst_element_set_clock (h->element, GST_CLOCK_CAST (h->priv->testclock));
1313 * gst_harness_get_testclock:
1316 * Get the #GstTestClock. Useful if specific operations on the testclock is
1321 * Returns: (transfer full): a #GstTestClock, or %NULL if the testclock is not
1327 gst_harness_get_testclock (GstHarness * h)
1329 return gst_object_ref (h->priv->testclock);
1333 * gst_harness_set_time:
1335 * @time: a #GstClockTime to advance the clock to
1337 * Advance the #GstTestClock to a specific time.
1341 * Returns: a @gboolean %TRUE if the time could be set. %FALSE if not.
1346 gst_harness_set_time (GstHarness * h, GstClockTime time)
1348 gst_test_clock_set_time (h->priv->testclock, time);
1353 * gst_harness_wait_for_clock_id_waits:
1355 * @waits: a #guint describing the numbers of #GstClockID registered with
1357 * @timeout: a #guint describing how many seconds to wait for @waits to be true
1359 * Waits for @timeout seconds until @waits number of #GstClockID waits is
1360 * registered with the #GstTestClock. Useful for writing deterministic tests,
1361 * where you want to make sure that an expected number of waits have been
1366 * Returns: a @gboolean %TRUE if the waits have been registered, %FALSE if not.
1367 * (Could be that it timed out waiting or that more waits than waits was found)
1372 gst_harness_wait_for_clock_id_waits (GstHarness * h, guint waits, guint timeout)
1374 return gst_test_clock_timed_wait_for_multiple_pending_ids (h->priv->testclock,
1375 waits, timeout * 1000, NULL);
1379 * gst_harness_crank_single_clock_wait:
1382 * A "crank" consists of three steps:
1383 * 1: Wait for a #GstClockID to be registered with the #GstTestClock.
1384 * 2: Advance the #GstTestClock to the time the #GstClockID is waiting for.
1385 * 3: Release the #GstClockID wait.
1386 * Together, this provides an easy way to not have to think about the details
1387 * around clocks and time, but still being able to write deterministic tests
1388 * that are dependent on this. A "crank" can be though of as the notion of
1389 * manually driving the clock forward to its next logical step.
1393 * Returns: a @gboolean %TRUE if the "crank" was successful, %FALSE if not.
1398 gst_harness_crank_single_clock_wait (GstHarness * h)
1400 return gst_test_clock_crank (h->priv->testclock);
1404 * gst_harness_crank_multiple_clock_waits:
1406 * @waits: a #guint describing the number of #GstClockIDs to crank
1408 * Similar to gst_harness_crank_single_clock_wait(), this is the function to use
1409 * if your harnessed element(s) are using more then one gst_clock_id_wait.
1410 * Failing to do so can (and will) make it racy which #GstClockID you actually
1411 * are releasing, where as this function will process all the waits at the
1412 * same time, ensuring that one thread can't register another wait before
1413 * both are released.
1417 * Returns: a @gboolean %TRUE if the "crank" was successful, %FALSE if not.
1422 gst_harness_crank_multiple_clock_waits (GstHarness * h, guint waits)
1424 GstTestClock *testclock = h->priv->testclock;
1428 gst_test_clock_wait_for_multiple_pending_ids (testclock, waits, &pending);
1429 gst_harness_set_time (h, gst_test_clock_id_list_get_latest_time (pending));
1430 processed = gst_test_clock_process_id_list (testclock, pending);
1432 g_list_free_full (pending, gst_clock_id_unref);
1433 return processed == waits;
1440 * This will set the harnessed #GstElement to %GST_STATE_PLAYING.
1441 * #GstElements without a sink-#GstPad and with the %GST_ELEMENT_FLAG_SOURCE
1442 * flag set is considered a src #GstElement
1443 * Non-src #GstElements (like sinks and filters) are automatically set to
1444 * playing by the #GstHarness, but src #GstElements are not to avoid them
1445 * starting to produce buffers.
1446 * Hence, for src #GstElement you must call gst_harness_play() explicitly.
1453 gst_harness_play (GstHarness * h)
1455 GstState state, pending;
1456 gboolean state_change;
1457 state_change = gst_element_set_state (h->element, GST_STATE_PLAYING);
1458 g_assert_cmpint (GST_STATE_CHANGE_SUCCESS, ==, state_change);
1459 state_change = gst_element_get_state (h->element, &state, &pending, 0);
1460 g_assert_cmpint (GST_STATE_CHANGE_SUCCESS, ==, state_change);
1461 g_assert_cmpint (GST_STATE_PLAYING, ==, state);
1465 * gst_harness_set_blocking_push_mode:
1468 * Setting this will make the harness block in the chain-function, and
1469 * then release when gst_harness_pull() or gst_harness_try_pull() is called.
1470 * Can be useful when wanting to control a src-element that is not implementing
1471 * gst_clock_id_wait() so it can't be controlled by the #GstTestClock, since
1472 * it otherwise would produce buffers as fast as possible.
1479 gst_harness_set_blocking_push_mode (GstHarness * h)
1481 GstHarnessPrivate *priv = h->priv;
1482 priv->blocking_push_mode = TRUE;
1486 * gst_harness_set_forwarding:
1488 * @forwarding: a #gboolean to enable/disable forwarding
1490 * As a convenience, a src-harness will forward %GST_EVENT_STREAM_START,
1491 * %GST_EVENT_CAPS and %GST_EVENT_SEGMENT to the main-harness if forwarding
1492 * is enabled, and forward any sticky-events from the main-harness to
1493 * the sink-harness. It will also forward the %GST_QUERY_ALLOCATION.
1495 * If forwarding is disabled, the user will have to either manually push
1496 * these events from the src-harness using gst_harness_src_push_event(), or
1497 * create and push them manually. While this will allow full control and
1498 * inspection of these events, for the most cases having forwarding enabled
1499 * will be sufficient when writing a test where the src-harness' main function
1500 * is providing data for the main-harness.
1502 * Forwarding is enabled by default.
1509 gst_harness_set_forwarding (GstHarness * h, gboolean forwarding)
1511 GstHarnessPrivate *priv = h->priv;
1512 priv->forwarding = forwarding;
1514 gst_harness_set_forwarding (h->src_harness, forwarding);
1515 if (h->sink_harness)
1516 gst_harness_set_forwarding (h->sink_harness, forwarding);
1520 gst_harness_set_forward_pad (GstHarness * h, GstPad * fwdpad)
1523 gst_object_replace ((GstObject **) & h->priv->sink_forward_pad,
1524 (GstObject *) fwdpad);
1529 * gst_harness_create_buffer:
1531 * @size: a #gsize specifying the size of the buffer
1533 * Allocates a buffer using a #GstBufferPool if present, or else using the
1534 * configured #GstAllocator and #GstAllocationParams
1538 * Returns: a #GstBuffer of size @size
1543 gst_harness_create_buffer (GstHarness * h, gsize size)
1545 GstHarnessPrivate *priv = h->priv;
1546 GstBuffer *ret = NULL;
1549 if (gst_pad_check_reconfigure (h->srcpad))
1550 gst_harness_negotiate (h);
1553 flow = gst_buffer_pool_acquire_buffer (priv->pool, &ret, NULL);
1554 g_assert_cmpint (flow, ==, GST_FLOW_OK);
1555 if (gst_buffer_get_size (ret) != size) {
1556 GST_DEBUG_OBJECT (h,
1557 "use fallback, pool is configured with a different size (%zu != %zu)",
1558 size, gst_buffer_get_size (ret));
1559 gst_buffer_unref (ret);
1566 gst_buffer_new_allocate (priv->allocator, size,
1567 &priv->allocation_params);
1569 g_assert (ret != NULL);
1576 * @buffer: (transfer full): a #GstBuffer to push
1578 * Pushes a #GstBuffer on the #GstHarness srcpad. The standard way of
1579 * interacting with an harnessed element.
1583 * Returns: a #GstFlowReturn with the result from the push
1588 gst_harness_push (GstHarness * h, GstBuffer * buffer)
1590 GstHarnessPrivate *priv = h->priv;
1591 g_assert (buffer != NULL);
1592 priv->last_push_ts = GST_BUFFER_TIMESTAMP (buffer);
1593 return gst_pad_push (h->srcpad, buffer);
1600 * Pulls a #GstBuffer from the #GAsyncQueue on the #GstHarness sinkpad. The pull
1601 * will timeout in 60 seconds. This is the standard way of getting a buffer
1602 * from a harnessed #GstElement.
1606 * Returns: (transfer full): a #GstBuffer or %NULL if timed out.
1611 gst_harness_pull (GstHarness * h)
1613 GstHarnessPrivate *priv = h->priv;
1614 GstBuffer *buf = (GstBuffer *) g_async_queue_timeout_pop (priv->buffer_queue,
1615 G_USEC_PER_SEC * 60);
1617 if (priv->blocking_push_mode) {
1618 g_mutex_lock (&priv->blocking_push_mutex);
1619 g_cond_signal (&priv->blocking_push_cond);
1620 g_mutex_unlock (&priv->blocking_push_mutex);
1627 * gst_harness_try_pull:
1630 * Pulls a #GstBuffer from the #GAsyncQueue on the #GstHarness sinkpad. Unlike
1631 * gst_harness_pull this will not wait for any buffers if not any are present,
1632 * and return %NULL straight away.
1636 * Returns: (transfer full): a #GstBuffer or %NULL if no buffers are present in the #GAsyncQueue
1641 gst_harness_try_pull (GstHarness * h)
1643 GstHarnessPrivate *priv = h->priv;
1644 GstBuffer *buf = (GstBuffer *) g_async_queue_try_pop (priv->buffer_queue);
1646 if (priv->blocking_push_mode) {
1647 g_mutex_lock (&priv->blocking_push_mutex);
1648 g_cond_signal (&priv->blocking_push_cond);
1649 g_mutex_unlock (&priv->blocking_push_mutex);
1656 * gst_harness_push_and_pull:
1658 * @buffer: (transfer full): a #GstBuffer to push
1660 * Basically a gst_harness_push and a gst_harness_pull in one line. Reflects
1661 * the fact that you often want to do exactly this in your test: Push one buffer
1662 * in, and inspect the outcome.
1666 * Returns: (transfer full): a #GstBuffer or %NULL if timed out.
1671 gst_harness_push_and_pull (GstHarness * h, GstBuffer * buffer)
1673 gst_harness_push (h, buffer);
1674 return gst_harness_pull (h);
1678 * gst_harness_buffers_received:
1681 * The total number of #GstBuffers that has arrived on the #GstHarness sinkpad.
1682 * This number includes buffers that have been dropped as well as buffers
1683 * that have already been pulled out.
1687 * Returns: a #guint number of buffers received
1692 gst_harness_buffers_received (GstHarness * h)
1694 GstHarnessPrivate *priv = h->priv;
1695 return g_atomic_int_get (&priv->recv_buffers);
1699 * gst_harness_buffers_in_queue:
1702 * The number of #GstBuffers currently in the #GstHarness sinkpad #GAsyncQueue
1706 * Returns: a #guint number of buffers in the queue
1711 gst_harness_buffers_in_queue (GstHarness * h)
1713 GstHarnessPrivate *priv = h->priv;
1714 return g_async_queue_length (priv->buffer_queue);
1718 * gst_harness_set_drop_buffers:
1720 * @drop_buffers: a #gboolean specifying to drop outgoing buffers or not
1722 * When set to %TRUE, instead of placing the buffers arriving from the harnessed
1723 * #GstElement inside the sinkpads #GAsyncQueue, they are instead unreffed.
1730 gst_harness_set_drop_buffers (GstHarness * h, gboolean drop_buffers)
1732 GstHarnessPrivate *priv = h->priv;
1733 priv->drop_buffers = drop_buffers;
1737 * gst_harness_take_all_data_as_buffer:
1740 * Pulls all pending data from the harness and returns it as a single buffer.
1742 * Returns: (transfer full): the data as a buffer. Unref with gst_buffer_unref()
1743 * when no longer needed.
1748 gst_harness_take_all_data_as_buffer (GstHarness * h)
1750 GstHarnessPrivate *priv;
1751 GstBuffer *ret, *buf;
1753 g_return_val_if_fail (h != NULL, NULL);
1757 g_async_queue_lock (priv->buffer_queue);
1759 ret = g_async_queue_try_pop_unlocked (priv->buffer_queue);
1762 ret = gst_buffer_new ();
1764 /* buffer appending isn't very efficient for larger numbers of buffers
1765 * or lots of memories, but this function is not performance critical and
1766 * we can still improve it if and when the need arises. For now KISS. */
1767 while ((buf = g_async_queue_try_pop_unlocked (priv->buffer_queue)))
1768 ret = gst_buffer_append (ret, buf);
1771 g_async_queue_unlock (priv->buffer_queue);
1777 * gst_harness_take_all_data: (skip)
1779 * @size: (out): the size of the data in bytes
1781 * Pulls all pending data from the harness and returns it as a single
1784 * Returns: (transfer full): a pointer to the data, newly allocated. Free
1785 * with g_free() when no longer needed. Will return %NULL if there is no
1791 gst_harness_take_all_data (GstHarness * h, gsize * size)
1794 guint8 *data = NULL;
1796 g_return_val_if_fail (h != NULL, NULL);
1797 g_return_val_if_fail (size != NULL, NULL);
1799 buf = gst_harness_take_all_data_as_buffer (h);
1800 gst_buffer_extract_dup (buf, 0, -1, (gpointer *) & data, size);
1801 gst_buffer_unref (buf);
1806 * gst_harness_take_all_data_as_bytes: (rename-to gst_harness_take_all_data)
1809 * Pulls all pending data from the harness and returns it as a single #GBytes.
1811 * Returns: (transfer full): a pointer to the data, newly allocated. Free
1812 * with g_free() when no longer needed.
1817 gst_harness_take_all_data_as_bytes (GstHarness * h)
1822 g_return_val_if_fail (h != NULL, NULL);
1824 data = gst_harness_take_all_data (h, &size);
1825 return g_bytes_new_take (data, size);
1830 * gst_harness_dump_to_file:
1832 * @filename: a #gchar with a the name of a file
1834 * Allows you to dump the #GstBuffers the #GstHarness sinkpad #GAsyncQueue
1842 gst_harness_dump_to_file (GstHarness * h, const gchar * filename)
1848 data = gst_harness_take_all_data (h, &size);
1849 if (!g_file_set_contents (filename, data ? data : "", size, &err)) {
1850 g_error ("GstHarness: Failed to write data to file: %s", err->message);
1851 g_clear_error (&err);
1857 * gst_harness_get_last_pushed_timestamp:
1860 * Get the timestamp of the last #GstBuffer pushed on the #GstHarness srcpad,
1861 * typically with gst_harness_push or gst_harness_push_from_src.
1865 * Returns: a #GstClockTime with the timestamp or %GST_CLOCK_TIME_NONE if no
1866 * #GstBuffer has been pushed on the #GstHarness srcpad
1871 gst_harness_get_last_pushed_timestamp (GstHarness * h)
1873 GstHarnessPrivate *priv = h->priv;
1874 return priv->last_push_ts;
1878 * gst_harness_push_event:
1880 * @event: a #GstEvent to push
1882 * Pushes an #GstEvent on the #GstHarness srcpad.
1886 * Returns: a #gboolean with the result from the push
1891 gst_harness_push_event (GstHarness * h, GstEvent * event)
1893 return gst_pad_push_event (h->srcpad, event);
1897 * gst_harness_pull_event:
1900 * Pulls an #GstEvent from the #GAsyncQueue on the #GstHarness sinkpad.
1901 * Timeouts after 60 seconds similar to gst_harness_pull.
1905 * Returns: a #GstEvent or %NULL if timed out.
1910 gst_harness_pull_event (GstHarness * h)
1912 GstHarnessPrivate *priv = h->priv;
1913 return (GstEvent *) g_async_queue_timeout_pop (priv->sink_event_queue,
1914 G_USEC_PER_SEC * 60);
1918 * gst_harness_try_pull_event:
1921 * Pulls an #GstEvent from the #GAsyncQueue on the #GstHarness sinkpad.
1922 * See gst_harness_try_pull for details.
1926 * Returns: a #GstEvent or %NULL if no buffers are present in the #GAsyncQueue
1931 gst_harness_try_pull_event (GstHarness * h)
1933 GstHarnessPrivate *priv = h->priv;
1934 return (GstEvent *) g_async_queue_try_pop (priv->sink_event_queue);
1938 * gst_harness_events_received:
1941 * The total number of #GstEvents that has arrived on the #GstHarness sinkpad
1942 * This number includes events handled by the harness as well as events
1943 * that have already been pulled out.
1947 * Returns: a #guint number of events received
1952 gst_harness_events_received (GstHarness * h)
1954 GstHarnessPrivate *priv = h->priv;
1955 return g_atomic_int_get (&priv->recv_events);
1959 * gst_harness_events_in_queue:
1962 * The number of #GstEvents currently in the #GstHarness sinkpad #GAsyncQueue
1966 * Returns: a #guint number of events in the queue
1971 gst_harness_events_in_queue (GstHarness * h)
1973 GstHarnessPrivate *priv = h->priv;
1974 return g_async_queue_length (priv->sink_event_queue);
1978 * gst_harness_push_upstream_event:
1980 * @event: a #GstEvent to push
1982 * Pushes an #GstEvent on the #GstHarness sinkpad.
1986 * Returns: a #gboolean with the result from the push
1991 gst_harness_push_upstream_event (GstHarness * h, GstEvent * event)
1993 g_return_val_if_fail (event != NULL, FALSE);
1994 g_return_val_if_fail (GST_EVENT_IS_UPSTREAM (event), FALSE);
1996 return gst_pad_push_event (h->sinkpad, event);
2000 * gst_harness_pull_upstream_event:
2003 * Pulls an #GstEvent from the #GAsyncQueue on the #GstHarness srcpad.
2004 * Timeouts after 60 seconds similar to gst_harness_pull.
2008 * Returns: a #GstEvent or %NULL if timed out.
2013 gst_harness_pull_upstream_event (GstHarness * h)
2015 GstHarnessPrivate *priv = h->priv;
2016 return (GstEvent *) g_async_queue_timeout_pop (priv->src_event_queue,
2017 G_USEC_PER_SEC * 60);
2021 * gst_harness_try_pull_upstream_event:
2024 * Pulls an #GstEvent from the #GAsyncQueue on the #GstHarness srcpad.
2025 * See gst_harness_try_pull for details.
2029 * Returns: a #GstEvent or %NULL if no buffers are present in the #GAsyncQueue
2034 gst_harness_try_pull_upstream_event (GstHarness * h)
2036 GstHarnessPrivate *priv = h->priv;
2037 return (GstEvent *) g_async_queue_try_pop (priv->src_event_queue);
2041 * gst_harness_upstream_events_received:
2044 * The total number of #GstEvents that has arrived on the #GstHarness srcpad
2045 * This number includes events handled by the harness as well as events
2046 * that have already been pulled out.
2050 * Returns: a #guint number of events received
2055 gst_harness_upstream_events_received (GstHarness * h)
2057 GstHarnessPrivate *priv = h->priv;
2058 return g_atomic_int_get (&priv->recv_upstream_events);
2062 * gst_harness_upstream_events_in_queue:
2065 * The number of #GstEvents currently in the #GstHarness srcpad #GAsyncQueue
2069 * Returns: a #guint number of events in the queue
2074 gst_harness_upstream_events_in_queue (GstHarness * h)
2076 GstHarnessPrivate *priv = h->priv;
2077 return g_async_queue_length (priv->src_event_queue);
2081 * gst_harness_query_latency:
2084 * Get the min latency reported by any harnessed #GstElement.
2088 * Returns: a #GstClockTime with min latency
2093 gst_harness_query_latency (GstHarness * h)
2097 GstClockTime min = GST_CLOCK_TIME_NONE;
2100 query = gst_query_new_latency ();
2102 if (gst_pad_peer_query (h->sinkpad, query)) {
2103 gst_query_parse_latency (query, &is_live, &min, &max);
2105 gst_query_unref (query);
2111 * gst_harness_set_upstream_latency:
2113 * @latency: a #GstClockTime specifying the latency
2115 * Sets the min latency reported by #GstHarness when receiving a latency-query
2120 gst_harness_set_upstream_latency (GstHarness * h, GstClockTime latency)
2122 GstHarnessPrivate *priv = h->priv;
2123 priv->latency_min = latency;
2127 * gst_harness_get_allocator:
2129 * @allocator: (out) (allow-none) (transfer none): the #GstAllocator used
2130 * @params: (out) (allow-none) (transfer full): the #GstAllocationParams of
2133 * Gets the @allocator and its @params that has been decided to use after an
2141 gst_harness_get_allocator (GstHarness * h, GstAllocator ** allocator,
2142 GstAllocationParams * params)
2144 GstHarnessPrivate *priv = h->priv;
2146 *allocator = priv->allocator;
2148 *params = priv->allocation_params;
2153 * gst_harness_set_propose_allocator:
2155 * @allocator: (allow-none) (transfer full): a #GstAllocator
2156 * @params: (allow-none) (transfer none): a #GstAllocationParams
2158 * Sets the @allocator and @params to propose when receiving an allocation
2166 gst_harness_set_propose_allocator (GstHarness * h, GstAllocator * allocator,
2167 const GstAllocationParams * params)
2169 GstHarnessPrivate *priv = h->priv;
2171 priv->propose_allocator = allocator;
2173 priv->propose_allocation_params = *params;
2177 * gst_harness_add_propose_allocation_meta:
2179 * @api: a metadata API
2180 * @params: (allow-none) (transfer none): API specific parameters
2182 * Add api with params as one of the supported metadata API to propose when
2183 * receiving an allocation query.
2190 gst_harness_add_propose_allocation_meta (GstHarness * h, GType api,
2191 const GstStructure * params)
2193 GstHarnessPrivate *priv = h->priv;
2197 meta.params = params ? gst_structure_copy (params) : NULL;
2199 if (!priv->propose_allocation_metas) {
2200 priv->propose_allocation_metas =
2201 g_array_new (FALSE, FALSE, sizeof (ProposeMeta));
2202 g_array_set_clear_func (priv->propose_allocation_metas,
2203 (GDestroyNotify) propose_meta_clear);
2205 g_array_append_val (priv->propose_allocation_metas, meta);
2209 * gst_harness_add_src_harness:
2211 * @src_harness: (transfer full): a #GstHarness to be added as a src-harness.
2212 * @has_clock_wait: a #gboolean specifying if the #GstElement uses
2213 * gst_clock_wait_id internally.
2215 * A src-harness is a great way of providing the #GstHarness with data.
2216 * By adding a src-type #GstElement, it is then easy to use functions like
2217 * gst_harness_push_from_src or gst_harness_src_crank_and_push_many
2218 * to provide your harnessed element with input. The @has_clock_wait variable
2219 * is a great way to control you src-element with, in that you can have it
2220 * produce a buffer for you by simply cranking the clock, and not have it
2221 * spin out of control producing buffers as fast as possible.
2223 * If a src-harness already exists it will be replaced.
2230 gst_harness_add_src_harness (GstHarness * h,
2231 GstHarness * src_harness, gboolean has_clock_wait)
2234 gst_harness_teardown (h->src_harness);
2235 h->src_harness = src_harness;
2236 gst_harness_set_forward_pad (h->src_harness, h->srcpad);
2237 h->src_harness->priv->has_clock_wait = has_clock_wait;
2238 gst_harness_set_forwarding (h->src_harness, h->priv->forwarding);
2242 * gst_harness_add_src:
2244 * @src_element_name: a #gchar with the name of a #GstElement
2245 * @has_clock_wait: a #gboolean specifying if the #GstElement uses
2246 * gst_clock_wait_id internally.
2248 * Similar to gst_harness_add_src_harness, this is a convenience to
2249 * directly create a src-harness using the @src_element_name name specified.
2256 gst_harness_add_src (GstHarness * h,
2257 const gchar * src_element_name, gboolean has_clock_wait)
2259 GstHarness *src_harness = gst_harness_new (src_element_name);
2260 gst_harness_add_src_harness (h, src_harness, has_clock_wait);
2264 * gst_harness_add_src_parse:
2266 * @launchline: a #gchar describing a gst-launch type line
2267 * @has_clock_wait: a #gboolean specifying if the #GstElement uses
2268 * gst_clock_wait_id internally.
2270 * Similar to gst_harness_add_src, this allows you to specify a launch-line,
2271 * which can be useful for both having more then one #GstElement acting as your
2272 * src (Like a src producing raw buffers, and then an encoder, providing encoded
2273 * data), but also by allowing you to set properties like "is-live" directly on
2281 gst_harness_add_src_parse (GstHarness * h,
2282 const gchar * launchline, gboolean has_clock_wait)
2284 GstHarness *src_harness = gst_harness_new_parse (launchline);
2285 gst_harness_add_src_harness (h, src_harness, has_clock_wait);
2289 * gst_harness_push_from_src:
2292 * Transfer data from the src-#GstHarness to the main-#GstHarness. It consists
2294 * 1: Make sure the src is started. (see: gst_harness_play)
2295 * 2: Crank the clock (see: gst_harness_crank_single_clock_wait)
2296 * 3: Pull a #GstBuffer from the src-#GstHarness (see: gst_harness_pull)
2297 * 4: Push the same #GstBuffer into the main-#GstHarness (see: gst_harness_push)
2301 * Returns: a #GstFlowReturn with the result of the push
2306 gst_harness_push_from_src (GstHarness * h)
2311 g_assert (h->src_harness);
2313 /* FIXME: this *is* the right time to start the src,
2314 but maybe a flag so we don't keep telling it to play? */
2315 gst_harness_play (h->src_harness);
2317 if (h->src_harness->priv->has_clock_wait) {
2318 crank = gst_harness_crank_single_clock_wait (h->src_harness);
2322 buf = gst_harness_pull (h->src_harness);
2323 g_assert (buf != NULL);
2324 return gst_harness_push (h, buf);
2328 * gst_harness_src_crank_and_push_many:
2330 * @cranks: a #gint with the number of calls to gst_harness_crank_single_clock_wait
2331 * @pushes: a #gint with the number of calls to gst_harness_push
2333 * Transfer data from the src-#GstHarness to the main-#GstHarness. Similar to
2334 * gst_harness_push_from_src, this variant allows you to specify how many cranks
2335 * and how many pushes to perform. This can be useful for both moving a lot
2336 * of data at the same time, as well as cases when one crank does not equal one
2337 * buffer to push and v.v.
2341 * Returns: a #GstFlowReturn with the result of the push
2346 gst_harness_src_crank_and_push_many (GstHarness * h, gint cranks, gint pushes)
2348 GstFlowReturn ret = GST_FLOW_OK;
2352 g_assert (h->src_harness);
2353 gst_harness_play (h->src_harness);
2355 for (i = 0; i < cranks; i++) {
2356 crank = gst_harness_crank_single_clock_wait (h->src_harness);
2360 for (i = 0; i < pushes; i++) {
2362 buf = gst_harness_pull (h->src_harness);
2363 g_assert (buf != NULL);
2364 ret = gst_harness_push (h, buf);
2365 if (ret != GST_FLOW_OK)
2373 * gst_harness_src_push_event:
2376 * Similar to what gst_harness_src_push does with #GstBuffers, this transfers
2377 * a #GstEvent from the src-#GstHarness to the main-#GstHarness. Note that
2378 * some #GstEvents are being transferred automagically. Look at sink_forward_pad
2383 * Returns: a #gboolean with the result of the push
2388 gst_harness_src_push_event (GstHarness * h)
2390 return gst_harness_push_event (h, gst_harness_pull_event (h->src_harness));
2395 forward_sticky_events (GstPad * pad, GstEvent ** ev, gpointer user_data)
2397 GstHarness *h = user_data;
2398 return gst_pad_push_event (h->priv->sink_forward_pad, gst_event_ref (*ev));
2402 * gst_harness_add_sink_harness:
2404 * @sink_harness: (transfer full): a #GstHarness to be added as a sink-harness.
2406 * Similar to gst_harness_add_src, this allows you to send the data coming out
2407 * of your harnessed #GstElement to a sink-element, allowing to test different
2408 * responses the element output might create in sink elements. An example might
2409 * be an existing sink providing some analytical data on the input it receives that
2410 * can be useful to your testing. If the goal is to test a sink-element itself,
2411 * this is better achieved using gst_harness_new directly on the sink.
2413 * If a sink-harness already exists it will be replaced.
2420 gst_harness_add_sink_harness (GstHarness * h, GstHarness * sink_harness)
2422 GstHarnessPrivate *priv = h->priv;
2424 if (h->sink_harness) {
2425 gst_harness_set_forward_pad (h, NULL);
2426 gst_harness_teardown (h->sink_harness);
2428 h->sink_harness = sink_harness;
2429 gst_harness_set_forward_pad (h, h->sink_harness->srcpad);
2430 if (priv->forwarding && h->sinkpad)
2431 gst_pad_sticky_events_foreach (h->sinkpad, forward_sticky_events, h);
2432 gst_harness_set_forwarding (h->sink_harness, priv->forwarding);
2436 * gst_harness_add_sink:
2438 * @sink_element_name: a #gchar with the name of a #GstElement
2440 * Similar to gst_harness_add_sink_harness, this is a convenience to
2441 * directly create a sink-harness using the @sink_element_name name specified.
2448 gst_harness_add_sink (GstHarness * h, const gchar * sink_element_name)
2450 GstHarness *sink_harness = gst_harness_new (sink_element_name);
2451 gst_harness_add_sink_harness (h, sink_harness);
2455 * gst_harness_add_sink_parse:
2457 * @launchline: a #gchar with the name of a #GstElement
2459 * Similar to gst_harness_add_sink, this allows you to specify a launch-line
2460 * instead of just an element name. See gst_harness_add_src_parse for details.
2467 gst_harness_add_sink_parse (GstHarness * h, const gchar * launchline)
2469 GstHarness *sink_harness = gst_harness_new_parse (launchline);
2470 gst_harness_add_sink_harness (h, sink_harness);
2474 * gst_harness_push_to_sink:
2477 * Transfer one #GstBuffer from the main-#GstHarness to the sink-#GstHarness.
2478 * See gst_harness_push_from_src for details.
2482 * Returns: a #GstFlowReturn with the result of the push
2487 gst_harness_push_to_sink (GstHarness * h)
2490 g_assert (h->sink_harness);
2491 buf = gst_harness_pull (h);
2492 g_assert (buf != NULL);
2493 return gst_harness_push (h->sink_harness, buf);
2497 * gst_harness_sink_push_many:
2499 * @pushes: a #gint with the number of calls to gst_harness_push_to_sink
2501 * Convenience that calls gst_harness_push_to_sink @pushes number of times.
2502 * Will abort the pushing if any one push fails.
2506 * Returns: a #GstFlowReturn with the result of the push
2511 gst_harness_sink_push_many (GstHarness * h, gint pushes)
2513 GstFlowReturn ret = GST_FLOW_OK;
2515 g_assert (h->sink_harness);
2516 for (i = 0; i < pushes; i++) {
2517 ret = gst_harness_push_to_sink (h);
2518 if (ret != GST_FLOW_OK)
2525 * gst_harness_find_element:
2527 * @element_name: a #gchar with a #GstElementFactory name
2529 * Most useful in conjunction with gst_harness_new_parse, this will scan the
2530 * #GstElements inside the #GstHarness, and check if any of them matches
2531 * @element_name. Typical usecase being that you need to access one of the
2532 * harnessed elements for properties and/or signals.
2536 * Returns: (transfer full) (allow-none): a #GstElement or %NULL if not found
2541 gst_harness_find_element (GstHarness * h, const gchar * element_name)
2543 gboolean done = FALSE;
2545 GValue data = G_VALUE_INIT;
2547 if (!GST_IS_BIN (h->element)) {
2548 GstPluginFeature *feature;
2550 g_return_val_if_fail (GST_IS_ELEMENT (h->element), NULL);
2552 feature = GST_PLUGIN_FEATURE (gst_element_get_factory (h->element));
2553 if (!strcmp (element_name, gst_plugin_feature_get_name (feature)))
2554 return gst_object_ref (h->element);
2559 iter = gst_bin_iterate_elements (GST_BIN (h->element));
2563 switch (gst_iterator_next (iter, &data)) {
2564 case GST_ITERATOR_OK:
2566 GstElement *element = g_value_get_object (&data);
2567 GstPluginFeature *feature =
2568 GST_PLUGIN_FEATURE (gst_element_get_factory (element));
2569 if (!strcmp (element_name, gst_plugin_feature_get_name (feature))) {
2570 gst_iterator_free (iter);
2573 g_value_reset (&data);
2576 case GST_ITERATOR_RESYNC:
2577 gst_iterator_resync (iter);
2579 case GST_ITERATOR_ERROR:
2580 case GST_ITERATOR_DONE:
2585 gst_iterator_free (iter);
2593 * @element_name: a #gchar with a #GstElementFactory name
2594 * @first_property_name: a #gchar with the first property name
2595 * @...: value for the first property, followed optionally by more
2596 * name/value pairs, followed by %NULL
2598 * A convenience function to allows you to call g_object_set on a #GstElement
2599 * that are residing inside the #GstHarness, by using normal g_object_set
2607 gst_harness_set (GstHarness * h,
2608 const gchar * element_name, const gchar * first_property_name, ...)
2611 GstElement *element = gst_harness_find_element (h, element_name);
2612 va_start (var_args, first_property_name);
2613 g_object_set_valist (G_OBJECT (element), first_property_name, var_args);
2615 gst_object_unref (element);
2621 * @element_name: a #gchar with a #GstElementFactory name
2622 * @first_property_name: a #gchar with the first property name
2623 * @...: return location for the first property, followed optionally by more
2624 * name/return location pairs, followed by %NULL
2626 * A convenience function to allows you to call g_object_get on a #GstElement
2627 * that are residing inside the #GstHarness, by using normal g_object_get
2635 gst_harness_get (GstHarness * h,
2636 const gchar * element_name, const gchar * first_property_name, ...)
2639 GstElement *element = gst_harness_find_element (h, element_name);
2640 va_start (var_args, first_property_name);
2641 g_object_get_valist (G_OBJECT (element), first_property_name, var_args);
2643 gst_object_unref (element);
2647 * gst_harness_add_probe:
2649 * @element_name: a #gchar with a #GstElementFactory name
2650 * @pad_name: a #gchar with the name of the pad to attach the probe to
2651 * @mask: a #GstPadProbeType (see gst_pad_add_probe)
2652 * @callback: a #GstPadProbeCallback (see gst_pad_add_probe)
2653 * @user_data: a #gpointer (see gst_pad_add_probe)
2654 * @destroy_data: a #GDestroyNotify (see gst_pad_add_probe)
2656 * A convenience function to allows you to call gst_pad_add_probe on a
2657 * #GstPad of a #GstElement that are residing inside the #GstHarness,
2658 * by using normal gst_pad_add_probe syntax
2665 gst_harness_add_probe (GstHarness * h,
2666 const gchar * element_name, const gchar * pad_name, GstPadProbeType mask,
2667 GstPadProbeCallback callback, gpointer user_data,
2668 GDestroyNotify destroy_data)
2670 GstElement *element = gst_harness_find_element (h, element_name);
2671 GstPad *pad = gst_element_get_static_pad (element, pad_name);
2672 gst_pad_add_probe (pad, mask, callback, user_data, destroy_data);
2673 gst_object_unref (pad);
2674 gst_object_unref (element);
2677 /******************************************************************************/
2679 /******************************************************************************/
2680 struct _GstHarnessThread
2688 GDestroyNotify freefunc;
2698 } GstHarnessCustomThread;
2706 GstHarnessPrepareBufferFunc func;
2708 GDestroyNotify notify;
2709 } GstHarnessPushBufferThread;
2715 GstHarnessPrepareEventFunc func;
2717 GDestroyNotify notify;
2718 } GstHarnessPushEventThread;
2726 } GstHarnessPropThread;
2732 GstPadTemplate *templ;
2738 } GstHarnessReqPadThread;
2741 gst_harness_thread_init (GstHarnessThread * t, GDestroyNotify freefunc,
2742 GstHarness * h, gulong sleep)
2744 t->freefunc = freefunc;
2748 g_ptr_array_add (h->priv->stress, t);
2752 gst_harness_thread_free (GstHarnessThread * t)
2754 g_slice_free (GstHarnessThread, t);
2758 gst_harness_custom_thread_free (GstHarnessCustomThread * t)
2760 g_slice_free (GstHarnessCustomThread, t);
2764 gst_harness_push_buffer_thread_free (GstHarnessPushBufferThread * t)
2767 gst_caps_replace (&t->caps, NULL);
2768 if (t->notify != NULL)
2769 t->notify (t->data);
2770 g_slice_free (GstHarnessPushBufferThread, t);
2775 gst_harness_push_event_thread_free (GstHarnessPushEventThread * t)
2778 if (t->notify != NULL)
2779 t->notify (t->data);
2780 g_slice_free (GstHarnessPushEventThread, t);
2785 gst_harness_property_thread_free (GstHarnessPropThread * t)
2789 g_value_unset (&t->value);
2790 g_slice_free (GstHarnessPropThread, t);
2795 gst_harness_requestpad_release (GstPad * pad, GstElement * element)
2797 gst_element_release_request_pad (element, pad);
2798 gst_object_unref (pad);
2802 gst_harness_requestpad_release_pads (GstHarnessReqPadThread * rpt)
2804 g_slist_foreach (rpt->pads, (GFunc) gst_harness_requestpad_release,
2806 g_slist_free (rpt->pads);
2811 gst_harness_requestpad_thread_free (GstHarnessReqPadThread * t)
2814 gst_object_replace ((GstObject **) & t->templ, NULL);
2816 gst_caps_replace (&t->caps, NULL);
2818 gst_harness_requestpad_release_pads (t);
2819 g_slice_free (GstHarnessReqPadThread, t);
2823 #define GST_HARNESS_THREAD_START(ID, t) \
2824 (((GstHarnessThread *)t)->running = TRUE, \
2825 ((GstHarnessThread *)t)->thread = g_thread_new ( \
2826 "gst-harness-stress-"G_STRINGIFY(ID), \
2827 (GThreadFunc)gst_harness_stress_##ID##_func, t))
2828 #define GST_HARNESS_THREAD_END(t) \
2829 (t->running = FALSE, \
2830 GPOINTER_TO_UINT (g_thread_join (t->thread)))
2833 gst_harness_stress_free (GstHarnessThread * t)
2835 if (t != NULL && t->freefunc != NULL)
2840 gst_harness_stress_custom_func (GstHarnessThread * t)
2842 GstHarnessCustomThread *ct = (GstHarnessCustomThread *) t;
2845 if (ct->init != NULL)
2846 ct->init (ct, ct->data);
2848 while (t->running) {
2849 ct->callback (ct, ct->data);
2852 g_usleep (t->sleep);
2854 return GUINT_TO_POINTER (count);
2859 gst_harness_stress_statechange_func (GstHarnessThread * t)
2863 while (t->running) {
2864 GstClock *clock = gst_element_get_clock (t->h->element);
2866 gboolean done = FALSE;
2869 change = gst_element_set_state (t->h->element, GST_STATE_NULL);
2870 g_assert (change == GST_STATE_CHANGE_SUCCESS);
2873 it = gst_element_iterate_sink_pads (t->h->element);
2875 GValue item = G_VALUE_INIT;
2876 switch (gst_iterator_next (it, &item)) {
2877 case GST_ITERATOR_OK:
2879 GstPad *sinkpad = g_value_get_object (&item);
2880 GstPad *srcpad = gst_pad_get_peer (sinkpad);
2881 if (srcpad != NULL) {
2882 gst_pad_unlink (srcpad, sinkpad);
2883 gst_pad_link (srcpad, sinkpad);
2884 gst_object_unref (srcpad);
2886 g_value_reset (&item);
2889 case GST_ITERATOR_RESYNC:
2890 gst_iterator_resync (it);
2892 case GST_ITERATOR_ERROR:
2893 g_assert_not_reached ();
2894 case GST_ITERATOR_DONE:
2898 g_value_unset (&item);
2900 gst_iterator_free (it);
2902 if (clock != NULL) {
2903 gst_element_set_clock (t->h->element, clock);
2904 gst_object_unref (clock);
2906 change = gst_element_set_state (t->h->element, GST_STATE_PLAYING);
2907 g_assert (change == GST_STATE_CHANGE_SUCCESS);
2910 g_usleep (t->sleep);
2912 return GUINT_TO_POINTER (count);
2916 gst_harness_stress_buffer_func (GstHarnessThread * t)
2918 GstHarnessPushBufferThread *pt = (GstHarnessPushBufferThread *) t;
2923 /* Push stream start, caps and segment events */
2924 sid = g_strdup_printf ("%s-%p", GST_OBJECT_NAME (t->h->element), t->h);
2925 handled = gst_pad_push_event (t->h->srcpad, gst_event_new_stream_start (sid));
2928 handled = gst_pad_push_event (t->h->srcpad, gst_event_new_caps (pt->caps));
2930 handled = gst_pad_push_event (t->h->srcpad,
2931 gst_event_new_segment (&pt->segment));
2934 while (t->running) {
2935 gst_harness_push (t->h, pt->func (t->h, pt->data));
2938 g_usleep (t->sleep);
2940 return GUINT_TO_POINTER (count);
2944 gst_harness_stress_event_func (GstHarnessThread * t)
2946 GstHarnessPushEventThread *pet = (GstHarnessPushEventThread *) t;
2949 while (t->running) {
2950 gst_harness_push_event (t->h, pet->func (t->h, pet->data));
2953 g_usleep (t->sleep);
2955 return GUINT_TO_POINTER (count);
2959 gst_harness_stress_upstream_event_func (GstHarnessThread * t)
2961 GstHarnessPushEventThread *pet = (GstHarnessPushEventThread *) t;
2964 while (t->running) {
2965 gst_harness_push_upstream_event (t->h, pet->func (t->h, pet->data));
2968 g_usleep (t->sleep);
2970 return GUINT_TO_POINTER (count);
2974 gst_harness_stress_property_func (GstHarnessThread * t)
2976 GstHarnessPropThread *pt = (GstHarnessPropThread *) t;
2979 while (t->running) {
2980 GValue value = G_VALUE_INIT;
2982 g_object_set_property (G_OBJECT (t->h->element), pt->name, &pt->value);
2984 g_value_init (&value, G_VALUE_TYPE (&pt->value));
2985 g_object_get_property (G_OBJECT (t->h->element), pt->name, &value);
2986 g_value_reset (&value);
2989 g_usleep (t->sleep);
2991 return GUINT_TO_POINTER (count);
2995 gst_harness_stress_requestpad_func (GstHarnessThread * t)
2997 GstHarnessReqPadThread *rpt = (GstHarnessReqPadThread *) t;
3000 while (t->running) {
3004 gst_harness_requestpad_release_pads (rpt);
3008 reqpad = gst_element_request_pad (t->h->element,
3009 rpt->templ, rpt->name, rpt->caps);
3011 g_assert (reqpad != NULL);
3013 rpt->pads = g_slist_prepend (rpt->pads, reqpad);
3016 g_usleep (t->sleep);
3018 return GUINT_TO_POINTER (count);
3022 * gst_harness_stress_thread_stop:
3023 * @t: a #GstHarnessThread
3025 * Stop the running #GstHarnessThread
3032 gst_harness_stress_thread_stop (GstHarnessThread * t)
3036 g_return_val_if_fail (t != NULL, 0);
3038 ret = GST_HARNESS_THREAD_END (t);
3039 g_ptr_array_remove (t->h->priv->stress, t);
3044 * gst_harness_stress_custom_start: (skip)
3046 * @init: (allow-none): a #GFunc that is called initially and only once
3047 * @callback: a #GFunc that is called as often as possible
3048 * @data: a #gpointer with custom data to pass to the @callback function
3049 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3050 * each call to the @callback
3052 * Start a custom stress-thread that will call your @callback for every
3053 * iteration allowing you to do something nasty.
3057 * Returns: a #GstHarnessThread
3062 gst_harness_stress_custom_start (GstHarness * h,
3063 GFunc init, GFunc callback, gpointer data, gulong sleep)
3065 GstHarnessCustomThread *t = g_slice_new0 (GstHarnessCustomThread);
3066 gst_harness_thread_init (&t->t,
3067 (GDestroyNotify) gst_harness_custom_thread_free, h, sleep);
3070 t->callback = callback;
3073 GST_HARNESS_THREAD_START (custom, t);
3078 * gst_harness_stress_statechange_start_full: (skip)
3080 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3083 * Change the state of your harnessed #GstElement from NULL to PLAYING and
3084 * back again, only pausing for @sleep microseconds every time.
3088 * Returns: a #GstHarnessThread
3093 gst_harness_stress_statechange_start_full (GstHarness * h, gulong sleep)
3095 GstHarnessThread *t = g_slice_new0 (GstHarnessThread);
3096 gst_harness_thread_init (t,
3097 (GDestroyNotify) gst_harness_thread_free, h, sleep);
3098 GST_HARNESS_THREAD_START (statechange, t);
3103 gst_harness_ref_buffer (GstHarness * h, gpointer data)
3106 return gst_buffer_ref (GST_BUFFER_CAST (data));
3110 gst_harness_ref_event (GstHarness * h, gpointer data)
3113 return gst_event_ref (GST_EVENT_CAST (data));
3117 * gst_harness_stress_push_buffer_start_full: (skip)
3119 * @caps: a #GstCaps for the #GstBuffer
3120 * @segment: a #GstSegment
3121 * @buf: a #GstBuffer to push
3122 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3123 * each call to gst_pad_push
3125 * Push a #GstBuffer in intervals of @sleep microseconds.
3129 * Returns: a #GstHarnessThread
3134 gst_harness_stress_push_buffer_start_full (GstHarness * h,
3135 GstCaps * caps, const GstSegment * segment, GstBuffer * buf, gulong sleep)
3137 return gst_harness_stress_push_buffer_with_cb_start_full (h, caps, segment,
3138 gst_harness_ref_buffer, gst_buffer_ref (buf),
3139 (GDestroyNotify) gst_buffer_unref, sleep);
3143 * gst_harness_stress_push_buffer_with_cb_start_full: (skip)
3145 * @caps: a #GstCaps for the #GstBuffer
3146 * @segment: a #GstSegment
3147 * @func: a #GstHarnessPrepareBufferFunc function called before every iteration
3148 * to prepare / create a #GstBuffer for pushing
3149 * @data: a #gpointer with data to the #GstHarnessPrepareBufferFunc function
3150 * @notify: a #GDestroyNotify that is called when thread is stopped
3151 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3152 * each call to gst_pad_push
3154 * Push a #GstBuffer returned by @func in intervals of @sleep microseconds.
3158 * Returns: a #GstHarnessThread
3163 gst_harness_stress_push_buffer_with_cb_start_full (GstHarness * h,
3164 GstCaps * caps, const GstSegment * segment,
3165 GstHarnessPrepareBufferFunc func, gpointer data, GDestroyNotify notify,
3168 GstHarnessPushBufferThread *t = g_slice_new0 (GstHarnessPushBufferThread);
3169 gst_harness_thread_init (&t->t,
3170 (GDestroyNotify) gst_harness_push_buffer_thread_free, h, sleep);
3172 gst_caps_replace (&t->caps, caps);
3173 t->segment = *segment;
3178 GST_HARNESS_THREAD_START (buffer, t);
3183 * gst_harness_stress_push_event_start_full: (skip)
3185 * @event: a #GstEvent to push
3186 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3187 * each gst_event_push with @event
3189 * Push the @event onto the harnessed #GstElement sinkpad in intervals of
3190 * @sleep microseconds
3194 * Returns: a #GstHarnessThread
3199 gst_harness_stress_push_event_start_full (GstHarness * h,
3200 GstEvent * event, gulong sleep)
3202 return gst_harness_stress_push_event_with_cb_start_full (h,
3203 gst_harness_ref_event, gst_event_ref (event),
3204 (GDestroyNotify) gst_event_unref, sleep);
3208 * gst_harness_stress_push_event_with_cb_start_full: (skip)
3210 * @func: a #GstHarnessPrepareEventFunc function called before every iteration
3211 * to prepare / create a #GstEvent for pushing
3212 * @data: a #gpointer with data to the #GstHarnessPrepareEventFunc function
3213 * @notify: a #GDestroyNotify that is called when thread is stopped
3214 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3215 * each call to gst_pad_push
3217 * Push a #GstEvent returned by @func onto the harnessed #GstElement sinkpad
3218 * in intervals of @sleep microseconds.
3222 * Returns: a #GstHarnessThread
3227 gst_harness_stress_push_event_with_cb_start_full (GstHarness * h,
3228 GstHarnessPrepareEventFunc func, gpointer data, GDestroyNotify notify,
3231 GstHarnessPushEventThread *t = g_slice_new0 (GstHarnessPushEventThread);
3232 gst_harness_thread_init (&t->t,
3233 (GDestroyNotify) gst_harness_push_event_thread_free, h, sleep);
3239 GST_HARNESS_THREAD_START (event, t);
3244 * gst_harness_stress_push_upstream_event_start_full: (skip)
3246 * @event: a #GstEvent to push
3247 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3248 * each gst_event_push with @event
3250 * Push the @event onto the harnessed #GstElement srcpad in intervals of
3251 * @sleep microseconds.
3255 * Returns: a #GstHarnessThread
3260 gst_harness_stress_push_upstream_event_start_full (GstHarness * h,
3261 GstEvent * event, gulong sleep)
3263 return gst_harness_stress_push_upstream_event_with_cb_start_full (h,
3264 gst_harness_ref_event, gst_event_ref (event),
3265 (GDestroyNotify) gst_event_unref, sleep);
3269 * gst_harness_stress_push_upstream_event_with_cb_start_full: (skip)
3271 * @func: a #GstHarnessPrepareEventFunc function called before every iteration
3272 * to prepare / create a #GstEvent for pushing
3273 * @data: a #gpointer with data to the #GstHarnessPrepareEventFunc function
3274 * @notify: a #GDestroyNotify that is called when thread is stopped
3275 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3276 * each call to gst_pad_push
3278 * Push a #GstEvent returned by @func onto the harnessed #GstElement srcpad
3279 * in intervals of @sleep microseconds.
3283 * Returns: a #GstHarnessThread
3288 gst_harness_stress_push_upstream_event_with_cb_start_full (GstHarness * h,
3289 GstHarnessPrepareEventFunc func, gpointer data, GDestroyNotify notify,
3292 GstHarnessPushEventThread *t = g_slice_new0 (GstHarnessPushEventThread);
3293 gst_harness_thread_init (&t->t,
3294 (GDestroyNotify) gst_harness_push_event_thread_free, h, sleep);
3300 GST_HARNESS_THREAD_START (upstream_event, t);
3305 * gst_harness_stress_property_start_full: (skip)
3307 * @name: a #gchar specifying a property name
3308 * @value: a #GValue to set the property to
3309 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3310 * each g_object_set with @name and @value
3312 * Call g_object_set with @name and @value in intervals of @sleep microseconds
3316 * Returns: a #GstHarnessThread
3321 gst_harness_stress_property_start_full (GstHarness * h,
3322 const gchar * name, const GValue * value, gulong sleep)
3324 GstHarnessPropThread *t = g_slice_new0 (GstHarnessPropThread);
3325 gst_harness_thread_init (&t->t,
3326 (GDestroyNotify) gst_harness_property_thread_free, h, sleep);
3328 t->name = g_strdup (name);
3329 g_value_init (&t->value, G_VALUE_TYPE (value));
3330 g_value_copy (value, &t->value);
3332 GST_HARNESS_THREAD_START (property, t);
3337 * gst_harness_stress_requestpad_start_full: (skip)
3339 * @templ: a #GstPadTemplate
3342 * @release: a #gboolean
3343 * @sleep: a #gulong specifying how long to sleep in (microseconds) for
3344 * each gst_element_request_pad
3346 * Call gst_element_request_pad in intervals of @sleep microseconds
3350 * Returns: a #GstHarnessThread
3355 gst_harness_stress_requestpad_start_full (GstHarness * h,
3356 GstPadTemplate * templ, const gchar * name, GstCaps * caps,
3357 gboolean release, gulong sleep)
3359 GstHarnessReqPadThread *t = g_slice_new0 (GstHarnessReqPadThread);
3360 gst_harness_thread_init (&t->t,
3361 (GDestroyNotify) gst_harness_requestpad_thread_free, h, sleep);
3363 t->templ = gst_object_ref (templ);
3364 t->name = g_strdup (name);
3365 gst_caps_replace (&t->caps, caps);
3366 t->release = release;
3368 GST_HARNESS_THREAD_START (requestpad, t);