1 | /* GStreamer |
---|
2 | * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu> |
---|
3 | * 2000 Wim Taymans <wim.taymans@chello.be> |
---|
4 | * |
---|
5 | * gstprobe.h: Header for GstProbe object |
---|
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 | */ |
---|
22 | |
---|
23 | #include "gst_private.h" |
---|
24 | #include "gstprobe.h" |
---|
25 | |
---|
26 | static GstProbe * |
---|
27 | _gst_probe_copy (const GstProbe * src) |
---|
28 | { |
---|
29 | return gst_probe_new (src->single_shot, src->callback, src->user_data); |
---|
30 | } |
---|
31 | |
---|
32 | GType |
---|
33 | gst_probe_get_type (void) |
---|
34 | { |
---|
35 | static GType gst_probe_type = 0; |
---|
36 | |
---|
37 | if (!gst_probe_type) { |
---|
38 | gst_probe_type = g_boxed_type_register_static ("GstProbe", |
---|
39 | (GBoxedCopyFunc) _gst_probe_copy, (GBoxedFreeFunc) gst_probe_destroy); |
---|
40 | } |
---|
41 | |
---|
42 | return gst_probe_type; |
---|
43 | |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * gst_probe_new: |
---|
48 | * @single_shot: TRUE if a single shot probe is required |
---|
49 | * @callback: the function to call when the probe is triggered |
---|
50 | * @user_data: data passed to the callback function |
---|
51 | * |
---|
52 | * Create a new probe with the specified parameters |
---|
53 | * |
---|
54 | * Returns: a new #GstProbe. |
---|
55 | */ |
---|
56 | GstProbe * |
---|
57 | gst_probe_new (gboolean single_shot, |
---|
58 | GstProbeCallback callback, gpointer user_data) |
---|
59 | { |
---|
60 | GstProbe *probe; |
---|
61 | |
---|
62 | g_return_val_if_fail (callback, NULL); |
---|
63 | |
---|
64 | probe = g_new0 (GstProbe, 1); |
---|
65 | |
---|
66 | probe->single_shot = single_shot; |
---|
67 | probe->callback = callback; |
---|
68 | probe->user_data = user_data; |
---|
69 | |
---|
70 | GST_CAT_DEBUG (GST_CAT_PROBE, "created probe %p", probe); |
---|
71 | |
---|
72 | return probe; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * gst_probe_destroy: |
---|
77 | * @probe: The probe to destroy |
---|
78 | * |
---|
79 | * Free the memory associated with the probe. |
---|
80 | */ |
---|
81 | void |
---|
82 | gst_probe_destroy (GstProbe * probe) |
---|
83 | { |
---|
84 | g_return_if_fail (probe); |
---|
85 | |
---|
86 | #ifdef USE_POISONING |
---|
87 | memset (probe, 0xff, sizeof (*probe)); |
---|
88 | #endif |
---|
89 | |
---|
90 | g_free (probe); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * gst_probe_perform: |
---|
95 | * @probe: The probe to trigger |
---|
96 | * @data: the GstData that triggered the probe. |
---|
97 | * |
---|
98 | * Perform the callback associated with the given probe. |
---|
99 | * |
---|
100 | * Returns: the result of the probe callback function. |
---|
101 | */ |
---|
102 | gboolean |
---|
103 | gst_probe_perform (GstProbe * probe, GstData ** data) |
---|
104 | { |
---|
105 | gboolean res = TRUE; |
---|
106 | |
---|
107 | g_return_val_if_fail (probe, res); |
---|
108 | |
---|
109 | GST_CAT_DEBUG (GST_CAT_PROBE, "performing probe %p", probe); |
---|
110 | |
---|
111 | if (probe->callback) |
---|
112 | res = probe->callback (probe, data, probe->user_data); |
---|
113 | |
---|
114 | return res; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * gst_probe_dispatcher_new: |
---|
119 | * |
---|
120 | * Create a new probe dispatcher |
---|
121 | * |
---|
122 | * Returns: a new probe dispatcher. |
---|
123 | */ |
---|
124 | GstProbeDispatcher * |
---|
125 | gst_probe_dispatcher_new (void) |
---|
126 | { |
---|
127 | GstProbeDispatcher *disp; |
---|
128 | |
---|
129 | disp = g_new0 (GstProbeDispatcher, 1); |
---|
130 | |
---|
131 | gst_probe_dispatcher_init (disp); |
---|
132 | |
---|
133 | return disp; |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * gst_probe_dispatcher_destroy: |
---|
138 | * @disp: the dispatcher to destroy |
---|
139 | * |
---|
140 | * Free the memory allocated by the probe dispatcher. All pending |
---|
141 | * probes are removed first. |
---|
142 | */ |
---|
143 | void |
---|
144 | gst_probe_dispatcher_destroy (GstProbeDispatcher * disp) |
---|
145 | { |
---|
146 | g_return_if_fail (disp); |
---|
147 | |
---|
148 | #ifdef USE_POISONING |
---|
149 | memset (disp, 0xff, sizeof (*disp)); |
---|
150 | #endif |
---|
151 | |
---|
152 | /* FIXME, free pending probes */ |
---|
153 | g_free (disp); |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * gst_probe_dispatcher_init: |
---|
158 | * @disp: the dispatcher to initialize |
---|
159 | * |
---|
160 | * Initialize the dispatcher. Useful for statically allocated probe |
---|
161 | * dispatchers. |
---|
162 | */ |
---|
163 | void |
---|
164 | gst_probe_dispatcher_init (GstProbeDispatcher * disp) |
---|
165 | { |
---|
166 | g_return_if_fail (disp); |
---|
167 | |
---|
168 | disp->active = TRUE; |
---|
169 | disp->probes = NULL; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * gst_probe_dispatcher_set_active: |
---|
174 | * @disp: the dispatcher to activate |
---|
175 | * @active: boolean to indicate activation or deactivation |
---|
176 | * |
---|
177 | * Activate or deactivate the given dispatcher |
---|
178 | * dispatchers. |
---|
179 | */ |
---|
180 | void |
---|
181 | gst_probe_dispatcher_set_active (GstProbeDispatcher * disp, gboolean active) |
---|
182 | { |
---|
183 | g_return_if_fail (disp); |
---|
184 | |
---|
185 | disp->active = active; |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * gst_probe_dispatcher_add_probe: |
---|
190 | * @disp: the dispatcher to add the probe to |
---|
191 | * @probe: the probe to add to the dispatcher |
---|
192 | * |
---|
193 | * Adds the given probe to the dispatcher. |
---|
194 | */ |
---|
195 | void |
---|
196 | gst_probe_dispatcher_add_probe (GstProbeDispatcher * disp, GstProbe * probe) |
---|
197 | { |
---|
198 | g_return_if_fail (disp); |
---|
199 | g_return_if_fail (probe); |
---|
200 | |
---|
201 | GST_CAT_DEBUG (GST_CAT_PROBE, "adding probe %p to dispatcher %p", probe, |
---|
202 | disp); |
---|
203 | |
---|
204 | disp->probes = g_slist_prepend (disp->probes, probe); |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * gst_probe_dispatcher_remove_probe: |
---|
209 | * @disp: the dispatcher to remove the probe from |
---|
210 | * @probe: the probe to remove from the dispatcher |
---|
211 | * |
---|
212 | * Removes the given probe from the dispatcher. |
---|
213 | */ |
---|
214 | void |
---|
215 | gst_probe_dispatcher_remove_probe (GstProbeDispatcher * disp, GstProbe * probe) |
---|
216 | { |
---|
217 | g_return_if_fail (disp); |
---|
218 | g_return_if_fail (probe); |
---|
219 | |
---|
220 | GST_CAT_DEBUG (GST_CAT_PROBE, "removing probe %p from dispatcher %p", |
---|
221 | probe, disp); |
---|
222 | |
---|
223 | disp->probes = g_slist_remove (disp->probes, probe); |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * gst_probe_dispatcher_dispatch: |
---|
228 | * @disp: the dispatcher to dispatch |
---|
229 | * @data: the data that triggered the dispatch |
---|
230 | * |
---|
231 | * Trigger all registered probes on the given dispatcher. |
---|
232 | * |
---|
233 | * Returns: TRUE if all callbacks returned TRUE. |
---|
234 | */ |
---|
235 | gboolean |
---|
236 | gst_probe_dispatcher_dispatch (GstProbeDispatcher * disp, GstData ** data) |
---|
237 | { |
---|
238 | GSList *walk; |
---|
239 | gboolean res = TRUE; |
---|
240 | |
---|
241 | g_return_val_if_fail (disp, res); |
---|
242 | |
---|
243 | GST_CAT_DEBUG (GST_CAT_PROBE, "dispatching data %p on dispatcher %p", |
---|
244 | *data, disp); |
---|
245 | |
---|
246 | walk = disp->probes; |
---|
247 | while (walk) { |
---|
248 | GstProbe *probe = (GstProbe *) walk->data; |
---|
249 | |
---|
250 | walk = g_slist_next (walk); |
---|
251 | |
---|
252 | res &= gst_probe_perform (probe, data); |
---|
253 | /* it might have disappeared in the callback */ |
---|
254 | if (disp->active && |
---|
255 | g_slist_find (disp->probes, probe) && probe->single_shot) { |
---|
256 | disp->probes = g_slist_remove (disp->probes, probe); |
---|
257 | |
---|
258 | gst_probe_destroy (probe); |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | return res; |
---|
263 | } |
---|