Debian Codegraph @

gstreamer/src/audioringbuffer.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 <gstreamermm/handle_error.h>
21 _PINCLUDE(gstreamermm/private/object_p.h)
22
23 namespace Gst
24 {
25
26 AudioRingBufferSpec::AudioRingBufferSpec()
27 : m_spec(g_try_new(GstAudioRingBufferSpec, 1)),
28 take_ownership(true)
29 {
30 // Handle possible memory allocation failure.
31 if(!m_spec)
32 {
33 gstreamermm_handle_error(
34 "Failed to allocate a new Gst::AudioRingBufferSpec.");
35 return;
36 }
37 }
38
39 AudioRingBufferSpec::AudioRingBufferSpec(const Glib::RefPtr<Gst::Caps>& caps,
40 Gst::AudioRingBufferFormatType type,
41 guint64 latency_time, guint64 buffer_time, int segsize, int segtotal,
42 int seglatency)
43 : m_spec(g_try_new(GstAudioRingBufferSpec, 1)),
44 take_ownership(true)
45 {
46 // Handle possible memory allocation failure.
47 if(!m_spec)
48 {
49 gstreamermm_handle_error("Failed to allocate a new Gst::AudioRingBufferSpec.");
50 return;
51 }
52
53 GstAudioRingBufferSpec* spec = gobj();
54
55 spec->caps = Glib::unwrap(caps);
56
57 // Keep a copy of the caps (it will be unreferenced in the destructor).
58 if (caps)
59 caps->reference();
60
61 spec->type = static_cast<GstAudioRingBufferFormatType>(type);
62 spec->latency_time = latency_time;
63 spec->buffer_time = buffer_time;
64 spec->segsize = segsize;
65 spec->segtotal = segtotal;
66 spec->seglatency = seglatency;
67 }
68
69
70 AudioRingBufferSpec::AudioRingBufferSpec(GstAudioRingBufferSpec& castitem, bool take_ownership)
71 : m_spec(&castitem),
72 take_ownership(take_ownership)
73 {
74 // Keep a copy of the caps (it will be unreferenced in the destructor).
75 if(castitem.caps)
76 gst_caps_ref(castitem.caps);
77 }
78
79 AudioRingBufferSpec::AudioRingBufferSpec(const AudioRingBufferSpec& other)
80 : m_spec(g_try_new(GstAudioRingBufferSpec, 1)),
81 take_ownership(true)
82 {
83 // Handle possible memory allocation failure.
84 if(!m_spec)
85 {
86 gstreamermm_handle_error("Failed to allocate a new Gst::AudioRingBufferSpec.");
87 return;
88 }
89
90 m_spec->caps = other.m_spec->caps;
91
92 // Keep a copy of the caps (it will be unreferenced in the destructor).
93 if(m_spec->caps)
94 gst_caps_ref(m_spec->caps);
95
96 m_spec->type = other.m_spec->type;
97 m_spec->latency_time = other.m_spec->latency_time;
98 m_spec->buffer_time = other.m_spec->buffer_time;
99 m_spec->segsize = other.m_spec->segsize;
100 m_spec->segtotal = other.m_spec->segtotal;
101 m_spec->seglatency = other.m_spec->seglatency;
102 }
103
104 AudioRingBufferSpec& AudioRingBufferSpec::operator=(const AudioRingBufferSpec& other)
105 {
106 AudioRingBufferSpec temp(other);
107 swap(temp);
108 return *this;
109 }
110
111 AudioRingBufferSpec::~AudioRingBufferSpec()
112 {
113 // Unref existing reference to caps.
114 if(m_spec->caps)
115 gst_caps_unref(m_spec->caps);
116
117 // Release the underlying m_spec if it is owned.
118 if(take_ownership)
119 g_free(m_spec);
120 }
121
122 void AudioRingBufferSpec::swap(AudioRingBufferSpec& other)
123 {
124 GstAudioRingBufferSpec* spec_temp = m_spec;
125 m_spec = other.m_spec;
126 other.m_spec = spec_temp;
127
128 const bool take_temp = take_ownership;
129 take_ownership = other.take_ownership;
130 other.take_ownership = take_temp;
131 }
132
133 bool AudioRingBuffer::prepare_read(int& segment, std::vector<guint8>& readptr,
134 int& len)
135 {
136 guint8* c_readptr = nullptr;
137
138 const bool result = static_cast<bool>(gst_audio_ring_buffer_prepare_read(gobj(), &segment, &c_readptr, &len));
139 readptr.assign(c_readptr, c_readptr + len);
140
141 if (c_readptr)
142 g_free(c_readptr);
143
144 return result;
145 }
146
147 gboolean AudioRingBuffer_Class::acquire_vfunc_callback(GstAudioRingBuffer* self, GstAudioRingBufferSpec* spec)
148 {
149 Glib::ObjectBase *const obj_base = static_cast<Glib::ObjectBase*>(
150 Glib::ObjectBase::_get_current_wrapper((GObject*)self));
151
152 // Non-gtkmmproc-generated custom classes implicitly call the default
153 // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
154 // generated classes can use this optimisation, which avoids the unnecessary
155 // parameter conversions if there is no possibility of the virtual function
156 // being overridden:
157 if(obj_base && obj_base->is_derived_())
158 {
159 CppObjectType *const obj = dynamic_cast<CppObjectType* const>(obj_base);
160 if(obj) // This can be NULL during destruction.
161 {
162 try // Trap C++ exceptions which would normally be lost because this is a C callback.
163 {
164 Gst::AudioRingBufferSpec cpp_spec(*spec);
165
166 // Call the virtual member method, which derived classes might override.
167 return static_cast<int>(obj->acquire_vfunc(cpp_spec));
168
169 }
170 catch(...)
171 {
172 Glib::exception_handlers_invoke();
173 }
174 }
175 }
176
177 BaseClassType *const base = static_cast<BaseClassType*>(
178 g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)) // Get the parent class of the object class (The original underlying C class).
179 );
180
181 // Call the original underlying C function:
182 if(base && base->acquire)
183 return (*base->acquire)(self, spec);
184
185
186 typedef gboolean RType;
187 return RType();
188 }
189 bool Gst::AudioRingBuffer::acquire_vfunc(Gst::AudioRingBufferSpec& spec)
190 {
191 BaseClassType *const base = static_cast<BaseClassType*>(
192 g_type_class_peek_parent(G_OBJECT_GET_CLASS(gobject_)) // Get the parent class of the object class (The original underlying C class).
193 );
194
195 if(base && base->acquire)
196 return static_cast<bool>((*base->acquire)(gobj(), spec.gobj()));
197
198 typedef bool RType;
199 return RType();
200 }
201 guint AudioRingBuffer_Class::commit_vfunc_callback(GstAudioRingBuffer* self, guint64* sample, guchar* data, gint in_samples, gint out_samples, gint* accum)
202 {
203 Glib::ObjectBase *const obj_base = static_cast<Glib::ObjectBase*>(
204 Glib::ObjectBase::_get_current_wrapper((GObject*)self));
205
206 // Non-gtkmmproc-generated custom classes implicitly call the default
207 // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
208 // generated classes can use this optimisation, which avoids the unnecessary
209 // parameter conversions if there is no possibility of the virtual function
210 // being overridden:
211 if(obj_base && obj_base->is_derived_())
212 {
213 CppObjectType *const obj = dynamic_cast<CppObjectType* const>(obj_base);
214 if(obj) // This can be NULL during destruction.
215 {
216 try // Trap C++ exceptions which would normally be lost because this is a C callback.
217 {
218 // Declare the ArrayHandle for the C++ vfunc.
219 Glib::ArrayHandle<guchar> cpp_data(data, self->size, Glib::OWNERSHIP_NONE);
220 // Call the virtual member method, which derived classes might override.
221 return obj->commit_vfunc(*(sample), Glib::ArrayHandler<guint8>::array_to_vector(data, self->size, Glib::OWNERSHIP_NONE), in_samples, out_samples, *(accum));
222 }
223 catch(...)
224 {
225 Glib::exception_handlers_invoke();
226 }
227 }
228 }
229
230 BaseClassType *const base = static_cast<BaseClassType*>(
231 g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)) // Get the parent class of the object class (The original underlying C class).
232 );
233
234 // Call the original underlying C function:
235 if(base && base->commit)
236 return (*base->commit)(self, sample, data, in_samples, out_samples, accum);
237
238
239 typedef guint RType;
240 return RType();
241 }
242 guint Gst::AudioRingBuffer::commit_vfunc(guint64& sample, const std::vector<guint8>& data, int in_samples, int out_samples, int& accum)
243 {
244 BaseClassType *const base = static_cast<BaseClassType*>(
245 g_type_class_peek_parent(G_OBJECT_GET_CLASS(gobject_)) // Get the parent class of the object class (The original underlying C class).
246 );
247
248 if(base && base->commit)
249 return (*base->commit)(gobj(),&sample,Glib::ArrayHandler<guint8>::vector_to_array(data).data(),in_samples,out_samples,&accum);
250
251 typedef guint RType;
252 return RType();
253 }
254
255 } // namespace Gst