b84d0448b9cf67cfbd912d77933bec8da55b7720
1 /* GStreamer
2 *
3 * unit testing helper lib
4 *
5 * Copyright (C) 2006 Andy Wingo <wingo at pobox.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
23 /**
24 * SECTION:gstcheckbufferstraw
25 * @short_description: Buffer interception code for GStreamer unit tests
26 *
27 * These macros and functions are for internal use of the unit tests found
28 * inside the 'check' directories of various GStreamer packages.
29 */
31 #include "gstbufferstraw.h"
33 static GCond *cond = NULL;
34 static GMutex *lock = NULL;
35 static GstBuffer *buf = NULL;
36 static gulong id;
38 /* called for every buffer. Waits until the global "buf" variable is unset,
39 * then sets it to the buffer received, and signals. */
40 static gboolean
41 buffer_probe (GstPad * pad, GstBuffer * buffer, gpointer unused)
42 {
43 g_mutex_lock (lock);
45 while (buf != NULL)
46 g_cond_wait (cond, lock);
48 /* increase the refcount because we store it globally for others to use */
49 buf = gst_buffer_ref (buffer);
51 g_cond_signal (cond);
53 g_mutex_unlock (lock);
55 return TRUE;
56 }
58 /**
59 * gst_buffer_straw_start_pipeline:
60 * @bin: the pipeline to run
61 * @pad: a pad on an element in @bin
62 *
63 * Sets up a pipeline for buffer sucking. This will allow you to call
64 * gst_buffer_straw_get_buffer() to access buffers as they pass over @pad.
65 *
66 * This function is normally used in unit tests that want to verify that a
67 * particular element is outputting correct buffers. For example, you would make
68 * a pipeline via gst_parse_launch(), pull out the pad you want to monitor, then
69 * call gst_buffer_straw_get_buffer() to get the buffers that pass through @pad.
70 * The pipeline will block until you have sucked off the buffers.
71 *
72 * This function will set the state of @bin to PLAYING; to clean up, be sure to
73 * call gst_buffer_straw_stop_pipeline().
74 *
75 * Note that you may not start two buffer straws at the same time. This function
76 * is intended for unit tests, not general API use. In fact it calls fail_if
77 * from libcheck, so you cannot use it outside unit tests.
78 */
79 void
80 gst_buffer_straw_start_pipeline (GstElement * bin, GstPad * pad)
81 {
82 GstStateChangeReturn ret;
84 id = gst_pad_add_buffer_probe (pad, G_CALLBACK (buffer_probe), NULL);
86 cond = g_cond_new ();
87 lock = g_mutex_new ();
89 ret = gst_element_set_state (bin, GST_STATE_PLAYING);
90 fail_if (ret == GST_STATE_CHANGE_FAILURE, "Could not start test pipeline");
91 if (ret == GST_STATE_CHANGE_ASYNC) {
92 ret = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
93 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "Could not start test pipeline");
94 }
95 }
97 /**
98 * gst_buffer_straw_get_buffer:
99 * @bin: the pipeline previously started via gst_buffer_straw_start_pipeline()
100 * @pad: the pad previously passed to gst_buffer_straw_start_pipeline()
101 *
102 * Get one buffer from @pad. Implemented via buffer probes. This function will
103 * block until the pipeline passes a buffer over @pad, so for robust behavior
104 * in unit tests, you need to use check's timeout to fail out in the case that a
105 * buffer never arrives.
106 *
107 * You must have previously called gst_buffer_straw_start_pipeline() on
108 * @pipeline and @pad.
109 */
110 GstBuffer *
111 gst_buffer_straw_get_buffer (GstElement * bin, GstPad * pad)
112 {
113 GstBuffer *ret;
115 g_mutex_lock (lock);
117 while (buf == NULL)
118 g_cond_wait (cond, lock);
120 ret = buf;
121 buf = NULL;
123 g_cond_signal (cond);
125 g_mutex_unlock (lock);
127 return ret;
128 }
130 /**
131 * gst_buffer_straw_stop_pipeline:
132 * @bin: the pipeline previously started via gst_buffer_straw_start_pipeline()
133 * @pad: the pad previously passed to gst_buffer_straw_start_pipeline()
134 *
135 * Set @bin to #GST_STATE_NULL and release resource allocated in
136 * gst_buffer_straw_start_pipeline().
137 *
138 * You must have previously called gst_buffer_straw_start_pipeline() on
139 * @pipeline and @pad.
140 */
141 void
142 gst_buffer_straw_stop_pipeline (GstElement * bin, GstPad * pad)
143 {
144 GstStateChangeReturn ret;
146 g_mutex_lock (lock);
147 if (buf)
148 gst_buffer_unref (buf);
149 buf = NULL;
150 gst_pad_remove_buffer_probe (pad, (guint) id);
151 id = 0;
152 g_cond_signal (cond);
153 g_mutex_unlock (lock);
155 ret = gst_element_set_state (bin, GST_STATE_NULL);
156 fail_if (ret == GST_STATE_CHANGE_FAILURE, "Could not stop test pipeline");
157 if (ret == GST_STATE_CHANGE_ASYNC) {
158 ret = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
159 fail_if (ret != GST_STATE_CHANGE_SUCCESS, "Could not stop test pipeline");
160 }
162 g_mutex_lock (lock);
163 if (buf)
164 gst_buffer_unref (buf);
165 buf = NULL;
166 g_mutex_unlock (lock);
168 g_mutex_free (lock);
169 g_cond_free (cond);
171 lock = NULL;
172 cond = NULL;
173 }