Debian Codegraph @

gstreamer/src/pad.ccg

raw
1 /* gstreamermm - a C++ wrapper for gstreamer
2 *
3 * Copyright 2008-2016 The gstreamermm Development Team
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
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 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <gst/gst.h>
21 #include <gstreamermm/buffer.h>
22 #include <gstreamermm/caps.h>
23 #include <gstreamermm/element.h>
24 #include <gstreamermm/event.h>
25 #include <gstreamermm/padtemplate.h>
26 #include <gstreamermm/iterator.h>
27 #include <gstreamermm/message.h>
28 #include <gstreamermm/bus.h>
29
30 _PINCLUDE(glibmm/private/object_p.h)
31 _PINCLUDE(gstreamermm/private/object_p.h)
32
33 namespace
34 {
35
36 extern "C"
37 {
38 static GstPadProbeReturn Pad_Probe_gstreamermm_callback(GstPad* pad, GstPadProbeInfo* probe_info, void* data)
39 {
40 Gst::Pad::SlotProbe* the_slot = static_cast<Gst::Pad::SlotProbe*>(data);
41 Glib::RefPtr<Gst::Pad> pad_wrapper = Glib::wrap(pad, true);
42 try
43 {
44 return static_cast<GstPadProbeReturn>((*the_slot)(pad_wrapper, Glib::wrap(probe_info, true)));
45 }
46 catch(...)
47 {
48 pad_wrapper->exception_handler();
49 }
50
51 return GST_PAD_PROBE_DROP;
52 }
53
54 static void Pad_Probe_gstreamermm_callback_disconnect(void* data)
55 {
56 Gst::Pad::SlotProbe* the_slot = static_cast<Gst::Pad::SlotProbe*>(data);
57
58 if(the_slot)
59 delete the_slot;
60 }
61
62 } // extern "C"
63
64 } // anonymous namespace
65
66 namespace Gst
67 {
68
69 Pad::Pad(PadDirection dir)
70 : _CONSTRUCT("name", nullptr, "direction", dir)
71 {}
72
73 Pad::Pad(const Glib::RefPtr<const Gst::PadTemplate>& templ,
74 const Glib::ustring& name)
75 : _CONSTRUCT("name", (name.empty() ? nullptr : name.c_str()),
76 "direction", (templ ? static_cast<GstPadDirection>(templ->get_direction())
77 : GST_PAD_UNKNOWN),
78 "template", Glib::unwrap(templ))
79 {
80 if(!templ)
81 gstreamermm_handle_error(
82 "Gst::Pad::Pad(const Glib::RefPtr<Gst::PadTemplate>&, "
83 "const Glib::ustring&): Failed to construct pad (name = " + name + ") "
84 "from null template.");
85 }
86
87 Pad::Pad(const Glib::RefPtr<const Gst::PadTemplate>& templ)
88 : _CONSTRUCT("name", nullptr,
89 "direction", (templ ? static_cast<GstPadDirection>(templ->get_direction())
90 : GST_PAD_UNKNOWN),
91 "template", Glib::unwrap(templ))
92 {
93 if(!templ)
94 gstreamermm_handle_error(
95 "Gst::Pad::Pad(const Glib::RefPtr<Gst::PadTemplate>&): Failed to "
96 "construct pad from null template.");
97 }
98
99 void Pad::exception_handler()
100 {
101 Glib::RefPtr<Element> parent = parent.cast_dynamic(get_parent());
102
103 if (!parent)
104 {
105 Glib::exception_handlers_invoke();
106 return;
107 }
108
109 Glib::RefPtr<Message> error_msg;
110
111 try
112 {
113 throw;
114 }
115 catch(const Glib::Error& error)
116 {
117 Glib::Error err = error;
118 error_msg = MessageError::create(parent, err, "glibmm exception");
119 }
120 catch(const std::exception& ex)
121 {
122 Glib::Error err(GST_CORE_ERROR, GST_CORE_ERROR_FAILED, ex.what());
123 error_msg = MessageError::create(parent, err, "standard exception");
124 }
125 catch(...)
126 {
127 Glib::Error err(GST_CORE_ERROR, GST_CORE_ERROR_FAILED, "");
128 error_msg = MessageError::create(parent, err, "unknown error type");
129 }
130
131 parent->post_message(std::move(error_msg));
132 }
133
134 gulong Pad::add_probe(PadProbeType mask, const SlotProbe& slot)
135 {
136 SlotProbe* slot_copy = new SlotProbe(slot);
137 return gst_pad_add_probe(gobj(), static_cast<GstPadProbeType>(mask), &Pad_Probe_gstreamermm_callback, slot_copy, &Pad_Probe_gstreamermm_callback_disconnect);
138 }
139
140 FlowReturn Pad::get_range(guint64 offset, guint size, Glib::RefPtr<Gst::Buffer>& buffer)
141 {
142 GstBuffer* c_buffer = nullptr;
143 GstFlowReturn result = gst_pad_get_range(gobj(), offset, size, &c_buffer);
144 buffer = Glib::wrap(c_buffer, false);
145 return FlowReturn(result);
146 }
147
148 FlowReturn Pad::pull_range(guint64 offset, guint size, Glib::RefPtr<Gst::Buffer>& buffer)
149 {
150 GstBuffer* c_buffer = nullptr;
151 GstFlowReturn result = gst_pad_pull_range(gobj(), offset, size, &c_buffer);
152 buffer = Glib::wrap(c_buffer, false);
153 return FlowReturn(result);
154 }
155
156 bool Pad::query_position(Format& format) const
157 {
158 return gst_pad_query_position(const_cast<GstPad*>(gobj()), (GstFormat)format, nullptr);
159 }
160
161 bool Pad::query_duration(Format& format) const
162 {
163 return gst_pad_query_duration(const_cast<GstPad*>(gobj()), (GstFormat)format, nullptr);
164 }
165
166 GstFlowReturn Pad::Pad_Chain_gstreamermm_callback(GstPad* pad, GstObject*, GstBuffer *buffer)
167 {
168 //FIXME: It's made like vfunc wrappers, but why there is no reffing of pad?
169 Gst::Pad *pad_wrapper = dynamic_cast<Gst::Pad*>
170 (static_cast<Glib::ObjectBase*>(
171 Glib::ObjectBase::_get_current_wrapper((GObject*)pad)
172 )
173 );
174 g_assert(pad);
175
176 try
177 {
178 Glib::RefPtr<Buffer> buffer_wrapped = Glib::wrap(buffer, false); //manage object
179
180 return static_cast<GstFlowReturn>(
181 pad_wrapper->slot_chain(Glib::wrap(pad, true), //take copy
182 buffer_wrapped
183 ));
184 }
185 catch(...)
186 {
187 pad_wrapper->exception_handler();
188 }
189
190 return GST_FLOW_ERROR;
191 }
192
193 gboolean Pad::Pad_Query_gstreamermm_callback(GstPad* pad, GstObject*, GstQuery* query)
194 {
195 Gst::Pad *pad_wrapper = dynamic_cast<Gst::Pad*>
196 (static_cast<Glib::ObjectBase*>(
197 Glib::ObjectBase::_get_current_wrapper((GObject*)pad)
198 )
199 );
200 g_assert(pad);
201
202 try
203 {
204 //We cannot make copy of query, since some elements fail to answer the query if it's refcount>1 (is not writtable)
205 Glib::RefPtr<Query> query_wrapped = Glib::wrap(query, false);
206
207 gboolean res = pad_wrapper->slot_query(Glib::wrap(pad, true), query_wrapped);
208
209 //we have to increase refcount, since freeing RefPtr will decrease it which would be inaccurate here, since the
210 //caller is responsible for managing the object (see "transfer none" on this parameter)
211 query_wrapped->reference();
212 return res;
213 }
214 catch(...)
215 {
216 pad_wrapper->exception_handler();
217 }
218
219 return false;
220 }
221
222 gboolean Pad::Pad_Event_gstreamermm_callback(GstPad* pad, GstObject*, GstEvent* event)
223 {
224 Gst::Pad *pad_wrapper = dynamic_cast<Gst::Pad*>
225 (static_cast<Glib::ObjectBase*>(
226 Glib::ObjectBase::_get_current_wrapper((GObject*)pad)
227 )
228 );
229 g_assert(pad);
230
231 try
232 {
233 //we don't make copy, since ownership is transfered to the callee ("transfer full")
234 Glib::RefPtr<Event> event_wrapped = Glib::wrap(event, false);
235
236 return pad_wrapper->slot_event(Glib::wrap(pad, true), event_wrapped);
237 }
238 catch(...)
239 {
240 pad_wrapper->exception_handler();
241 }
242
243 return false;
244 }
245
246 gboolean Pad::Pad_Activate_gstreamermm_callback(GstPad* pad, GstObject*)
247 {
248 Gst::Pad *pad_wrapper = dynamic_cast<Gst::Pad*>
249 (static_cast<Glib::ObjectBase*>(
250 Glib::ObjectBase::_get_current_wrapper((GObject*)pad)
251 )
252 );
253 g_assert(pad);
254
255 try
256 {
257 return pad_wrapper->slot_activate(Glib::wrap(pad, true));
258 }
259 catch(...)
260 {
261 pad_wrapper->exception_handler();
262 }
263
264 return false;
265 }
266
267 gboolean Pad::Pad_Activatemode_gstreamermm_callback(GstPad* pad, GstObject*, GstPadMode mode, gboolean active)
268 {
269 Gst::Pad *pad_wrapper = dynamic_cast<Gst::Pad*>
270 (static_cast<Glib::ObjectBase*>(
271 Glib::ObjectBase::_get_current_wrapper((GObject*)pad)
272 )
273 );
274 g_assert(pad);
275
276 try
277 {
278 return pad_wrapper->slot_activatemode(Glib::wrap(pad, true), static_cast<PadMode>(mode), active);
279 }
280 catch(...)
281 {
282 pad_wrapper->exception_handler();
283 }
284
285 return false;
286 }
287
288 GstFlowReturn Pad::Pad_Getrange_gstreamermm_callback(GstPad* pad, GstObject*, guint64 offset, guint length, GstBuffer **buffer)
289 {
290 Gst::Pad *pad_wrapper = dynamic_cast<Gst::Pad*>
291 (static_cast<Glib::ObjectBase*>(
292 Glib::ObjectBase::_get_current_wrapper((GObject*)pad)
293 )
294 );
295 g_assert(pad);
296 Glib::RefPtr<Buffer> buf = Glib::wrap(*buffer, false);
297 try
298 {
299 return static_cast<GstFlowReturn>(
300 pad_wrapper->slot_getrange(Glib::wrap(pad, true), offset, length, buf)
301 );
302 }
303 catch(...)
304 {
305 pad_wrapper->exception_handler();
306 }
307
308 return GST_FLOW_ERROR;
309 }
310
311 void Pad::set_chain_function(const SlotChain& slot)
312 {
313 slot_chain = slot;
314 gst_pad_set_chain_function(GST_PAD(gobj()), &Pad_Chain_gstreamermm_callback);
315 }
316
317 void Pad::set_query_function(const SlotQuery& slot)
318 {
319 slot_query = slot;
320 gst_pad_set_query_function(GST_PAD(gobj()), &Pad_Query_gstreamermm_callback);
321 }
322
323 void Pad::set_event_function(const SlotEvent& slot)
324 {
325 slot_event = slot;
326 gst_pad_set_event_function(GST_PAD(gobj()), &Pad_Event_gstreamermm_callback);
327 }
328
329 bool Pad::push_event(const Glib::RefPtr<Gst::Event>& event)
330 {
331 event->reference();
332 return gst_pad_push_event(gobj(), event->gobj());
333 }
334
335 void Pad::set_activate_function(const SlotActivate& slot)
336 {
337 slot_activate = slot;
338 gst_pad_set_activate_function(GST_PAD(gobj()), &Pad_Activate_gstreamermm_callback);
339 }
340
341 void Pad::set_activatemode_function(const SlotActivatemode& slot)
342 {
343 slot_activatemode= slot;
344 gst_pad_set_activatemode_function(GST_PAD(gobj()), &Pad_Activatemode_gstreamermm_callback);
345 }
346
347 void Pad::set_getrange_function(const SlotGetrange& slot)
348 {
349 slot_getrange = slot;
350 gst_pad_set_getrange_function(GST_PAD(gobj()), &Pad_Getrange_gstreamermm_callback);
351 }
352
353 bool Pad::is_ghost_pad() const
354 {
355 return GST_IS_GHOST_PAD(gobj());
356 }
357
358 bool Pad::is_proxy_pad() const
359 {
360 return GST_IS_PROXY_PAD(gobj());
361 }
362
363 bool Pad::send_event(const Glib::RefPtr<Gst::Event>& event)
364 {
365 event->reference();
366 return gst_pad_send_event(gobj(), event->gobj());
367 }
368
369
370
371 PadProbeInfo::PadProbeInfo()
372 : gobj_(g_try_new(GstPadProbeInfo, 1)),
373 take_ownership(true)
374 {
375 // Handle possible memory allocation failure.
376 if(!gobj_)
377 {
378 gstreamermm_handle_error(
379 "Failed to allocate a new Gst::PadProbeInfo.");
380 }
381 }
382
383 PadProbeInfo::PadProbeInfo(GstPadProbeInfo& castitem, bool take_ownership)
384 : gobj_(&castitem),
385 take_ownership(take_ownership)
386 {
387 }
388
389 PadProbeInfo::PadProbeInfo(GstPadProbeInfo* castitem, bool take_copy)
390 : take_ownership(take_copy)
391 {
392 if(take_copy)
393 {
394 gobj_ = (g_try_new(GstPadProbeInfo, 1));
395
396 if(!gobj_)
397 {
398 gstreamermm_handle_error(
399 "Failed to allocate a new Gst::PadProbeInfo.");
400 return;
401 }
402
403 if(castitem)
404 *gobj_ = *castitem;
405 }
406 else
407 {
408 gobj_ = castitem;
409 }
410 }
411
412 PadProbeInfo::PadProbeInfo(const PadProbeInfo& other)
413 : gobj_(g_try_new(GstPadProbeInfo, 1)),
414 take_ownership(true)
415 {
416 if(!gobj_)
417 {
418 gstreamermm_handle_error("Failed to allocate a new Gst::PadProbeInfo.");
419 return;
420 }
421
422 // I can do it, because there is no dynamic allocated memory in pad
423 *gobj_ = *other.gobj_;
424 }
425
426 PadProbeInfo::~PadProbeInfo()
427 {
428 if(take_ownership)
429 g_free(gobj_);
430 }
431
432 PadProbeType PadProbeInfo::get_type() const
433 {
434 return static_cast<PadProbeType>(GST_PAD_PROBE_INFO_TYPE(gobj()));
435 }
436
437 } //namespace Gst
438
439 namespace Glib
440 {
441 Gst::PadProbeInfo wrap(GstPadProbeInfo* probe_info, bool take_copy)
442 {
443 return Gst::PadProbeInfo(probe_info, take_copy);
444 }
445 }