| 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/audio/audio.h> |
| 21 |
#include <gst/audio/gstaudioringbuffer.h> |
| 22 |
#include <gstreamermm/caps.h> |
| 23 |
#include <gstreamermm/object.h> |
| 24 |
#include <gstreamermm/format.h> |
| 25 |
#include <gstreamermm/clock.h> |
| 26 |
#include <glibmm/arrayhandle.h> |
| 27 |
#include <memory> |
| 28 |
|
| 29 |
_DEFS(gstreamermm,gst) |
| 30 |
|
| 31 |
namespace Gst |
| 32 |
{ |
| 33 |
|
| 34 |
_WRAP_ENUM(AudioChannelPosition, GstAudioChannelPosition) |
| 35 |
_WRAP_ENUM(AudioRingBufferState, GstAudioRingBufferState) |
| 36 |
_WRAP_ENUM(AudioRingBufferFormatType, GstAudioRingBufferFormatType) |
| 37 |
|
| 38 |
/** A class containing the format specification of a Gst::AudioRingBuffer. |
| 39 |
* Gst::AudioRingBufferSpec contains the format specification of a |
| 40 |
* Gst::AudioRingBuffer. The "in" members should be specified by the caller of |
| 41 |
* the acquire() method while the "in/out" members may be |
| 42 |
* set by the caller but are also modifiable by |
| 43 |
* acquire(). The "out" members are generated as a |
| 44 |
* result of the call to acquire(). |
| 45 |
* @see acquire(). |
| 46 |
*/ |
| 47 |
class AudioRingBufferSpec |
| 48 |
{ |
| 49 |
_CLASS_GENERIC(AudioRingBufferSpec, GstAudioRingBufferSpec) |
| 50 |
public: |
| 51 |
/** Default constructor. |
| 52 |
* @throw std::runtime_error if memory is unavailable for the new |
| 53 |
* Gst::AudioRingBufferSpec. |
| 54 |
*/ |
| 55 |
AudioRingBufferSpec(); |
| 56 |
|
| 57 |
/** Fully construct a Gst::AudioRingBufferSpec. Only the "(in)" parameters |
| 58 |
* are required. The "(in/out)" parameters are optional and may be modified |
| 59 |
* by the call to the Gst::AudioRingBuffer::acquire() method. |
| 60 |
* @param caps The caps of the buffer (in). |
| 61 |
* @param type The sample type (in/out). |
| 62 |
* @param latency_time The latency in microseconds (in/out). |
| 63 |
* @param buffer_time The total buffer size in microseconds (in/out). |
| 64 |
* @param segsize The size of one segment in bytes (in/out). |
| 65 |
* @param segtotal The total number of segments (in/out). |
| 66 |
* @param seglatency Number of segments queued in the lower level device, |
| 67 |
* defaults to @a segtotal in the C API (in/out). |
| 68 |
* |
| 69 |
* @throw std::runtime_error if memory is unavailable for the new |
| 70 |
* Gst::AudioRingBufferSpec. |
| 71 |
*/ |
| 72 |
AudioRingBufferSpec(const Glib::RefPtr<Gst::Caps>& caps, |
| 73 |
Gst::AudioRingBufferFormatType type = Gst::AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, |
| 74 |
guint64 latency_time = 0, |
| 75 |
guint64 buffer_time = 0, |
| 76 |
int segsize = 0, int segtotal = 0, int seglatency = 0); |
| 77 |
|
| 78 |
/// Construct a Gst::AudioRingBufferSpec from a GstAudioRingBufferSpec. |
| 79 |
explicit AudioRingBufferSpec(GstAudioRingBufferSpec& castitem, |
| 80 |
bool take_ownership = false); |
| 81 |
|
| 82 |
/** Copy constructor. |
| 83 |
* |
| 84 |
* @throw std::runtime_error if memory is unavailable for the new |
| 85 |
* Gst::AudioRingBufferSpec. |
| 86 |
*/ |
| 87 |
AudioRingBufferSpec(const AudioRingBufferSpec& other); |
| 88 |
|
| 89 |
/// Assignment operator. |
| 90 |
AudioRingBufferSpec& operator=(const AudioRingBufferSpec& other); |
| 91 |
|
| 92 |
/// Destructor. |
| 93 |
virtual ~AudioRingBufferSpec(); |
| 94 |
|
| 95 |
void swap(AudioRingBufferSpec& other); |
| 96 |
|
| 97 |
/// Gets the underlying gobject. |
| 98 |
GstAudioRingBufferSpec* gobj() { return m_spec; }; |
| 99 |
|
| 100 |
/// Gets the underlying gobject. |
| 101 |
const GstAudioRingBufferSpec* gobj() const { return m_spec; }; |
| 102 |
|
| 103 |
/** Get the caps of the buffer (in). */ |
| 104 |
_MEMBER_GET_GOBJECT(caps, caps, Gst::Caps, GstCaps*) |
| 105 |
|
| 106 |
/** Set the caps of the buffer (in). */ |
| 107 |
_MEMBER_SET_GOBJECT(caps, caps, Gst::Caps, GstCaps*) |
| 108 |
|
| 109 |
/** Get the sample type (in/out). |
| 110 |
*/ |
| 111 |
_MEMBER_GET(type, type, Gst::AudioRingBufferFormatType, GstAudioRingBufferFormatType) |
| 112 |
|
| 113 |
/** Set the sample type (in/out). |
| 114 |
*/ |
| 115 |
_MEMBER_SET(type, type, Gst::AudioRingBufferFormatType, GstAudioRingBufferFormatType) |
| 116 |
|
| 117 |
/** Get the latency in microseconds (in/out). |
| 118 |
*/ |
| 119 |
_MEMBER_GET(latency_time, latency_time, guint64, guint64) |
| 120 |
|
| 121 |
/** Set the latency in microseconds (in/out). |
| 122 |
*/ |
| 123 |
_MEMBER_SET(latency_time, latency_time, guint64, guint64) |
| 124 |
|
| 125 |
/** Get the total buffer size in microseconds (in/out). |
| 126 |
*/ |
| 127 |
_MEMBER_GET(buffer_time, buffer_time, guint64, guint64) |
| 128 |
|
| 129 |
/** Set the total buffer size in microseconds (in/out). |
| 130 |
*/ |
| 131 |
_MEMBER_SET(buffer_time, buffer_time, guint64, guint64) |
| 132 |
|
| 133 |
/** Get the size of one segment in bytes (in/out). |
| 134 |
*/ |
| 135 |
_MEMBER_GET(segsize, segsize, int, gint) |
| 136 |
|
| 137 |
/** Set the size of one segment in bytes (in/out). |
| 138 |
*/ |
| 139 |
_MEMBER_SET(segsize, segsize, int, gint) |
| 140 |
|
| 141 |
/** Get the total number of segments (in/out). |
| 142 |
*/ |
| 143 |
_MEMBER_GET(segtotal, segtotal, int, gint) |
| 144 |
|
| 145 |
/** Set the total number of segments (in/out). |
| 146 |
*/ |
| 147 |
_MEMBER_SET(segtotal, segtotal, int, gint) |
| 148 |
|
| 149 |
/** Get the number of segments queued in the lower level device, defaults to |
| 150 |
* segtotal (in/out). |
| 151 |
*/ |
| 152 |
_MEMBER_GET(seglatency, seglatency, int, gint) |
| 153 |
|
| 154 |
/** Set the number of segments queued in the lower level device, defaults to |
| 155 |
* segtotal (in/out). |
| 156 |
*/ |
| 157 |
_MEMBER_SET(seglatency, seglatency, int, gint) |
| 158 |
|
| 159 |
#m4 _CONVERSION(`guint8*', `Glib::ArrayHandle<guint8>', `Glib::ArrayHandle<guint8>($3, GSTREAMERMM_RING_BUFFER_SPEC_SILENCE_SAMPLE, Glib::OWNERSHIP_NONE)') |
| 160 |
|
| 161 |
|
| 162 |
protected: |
| 163 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 164 |
GstAudioRingBufferSpec* m_spec; |
| 165 |
// Tells whether the m_spec member should be freed upon destruction. |
| 166 |
bool take_ownership; |
| 167 |
#endif /* DOXYGEN_SHOULD_SKIP_THIS */ |
| 168 |
}; |
| 169 |
|
| 170 |
/** A base class for audio audioringbuffer implementations. |
| 171 |
* This object is the base class for audio ringbuffers used by the base audio |
| 172 |
* source and sink classes. |
| 173 |
* |
| 174 |
* The audioringbuffer abstracts a circular buffer of data. One reader and one |
| 175 |
* writer can operate on the data from different threads in a lockfree manner. |
| 176 |
* The base class is sufficiently flexible to be used as an abstraction for |
| 177 |
* DMA based audioringbuffer as well as a pure software implementations. |
| 178 |
* |
| 179 |
* Last reviewed on 2016-04-23 (1.8.0). |
| 180 |
* @ingroup GstBaseClasses |
| 181 |
*/ |
| 182 |
class AudioRingBuffer : public Gst::Object |
| 183 |
{ |
| 184 |
_CLASS_GOBJECT(AudioRingBuffer, GstAudioRingBuffer, GST_AUDIO_RING_BUFFER, Gst::Object, GstObject) |
| 185 |
|
| 186 |
public: |
| 187 |
/** For example, |
| 188 |
* bool on_fill(const Glib::RefPtr<Gst::AudioRingBuffer>& rbuf, |
| 189 |
* const std::vector<guint8>& data, guint len);. |
| 190 |
* This slot is set with set_fill_slot() and is called to fill the memory at |
| 191 |
* data with len bytes of samples. |
| 192 |
*/ |
| 193 |
typedef sigc::slot<void, const Glib::ArrayHandle<guint8>&, guint> SlotFill; |
| 194 |
|
| 195 |
//TODO: _MEMBER_GET(cond, cond, Glib::Cond, GCond*) |
| 196 |
|
| 197 |
/** Sets the given fill slot on the buffer. The slot will be called every |
| 198 |
* time a segment has been written to a device. |
| 199 |
* |
| 200 |
* MT safe. |
| 201 |
* |
| 202 |
* @param slot The fill slot to set. |
| 203 |
*/ |
| 204 |
void set_fill_slot(const SlotFill& slot); |
| 205 |
_IGNORE(gst_audio_ring_buffer_set_callback) |
| 206 |
|
| 207 |
_WRAP_METHOD(bool acquire(Gst::AudioRingBufferSpec& spec), gst_audio_ring_buffer_acquire) |
| 208 |
_WRAP_METHOD(bool release(), gst_audio_ring_buffer_release) |
| 209 |
_WRAP_METHOD(bool is_acquired() const, gst_audio_ring_buffer_is_acquired) |
| 210 |
_WRAP_METHOD(bool activate(bool active), gst_audio_ring_buffer_activate) |
| 211 |
_WRAP_METHOD(bool is_active() const, gst_audio_ring_buffer_is_active) |
| 212 |
_WRAP_METHOD(bool start(), gst_audio_ring_buffer_start) |
| 213 |
_WRAP_METHOD(bool pause(), gst_audio_ring_buffer_pause) |
| 214 |
_WRAP_METHOD(bool stop(), gst_audio_ring_buffer_stop) |
| 215 |
_WRAP_METHOD(guint get_delay() const, gst_audio_ring_buffer_delay) |
| 216 |
_WRAP_METHOD(guint64 get_samples_done() const, gst_audio_ring_buffer_samples_done) |
| 217 |
_WRAP_METHOD(void set_sample(guint64 sample), gst_audio_ring_buffer_set_sample) |
| 218 |
_WRAP_METHOD(void set_channel_position(const Gst::AudioChannelPosition& position), gst_audio_ring_buffer_set_channel_positions) |
| 219 |
_WRAP_METHOD(gboolean is_flushing(), gst_audio_ring_buffer_is_flushing) |
| 220 |
_WRAP_METHOD(void set_timestamp(gint readseg, ClockTime timestamp), gst_audio_ring_buffer_set_timestamp) |
| 221 |
|
| 222 |
#m4 _CONVERSION(`const std::vector<guint8>&', `guint8*', `Glib::ArrayHandler<guint8>::vector_to_array($3).data()') |
| 223 |
|
| 224 |
_WRAP_METHOD(guint commit(guint64& sample, const std::vector<guint8>& data, int in_samples, int out_samples, int& accum), gst_audio_ring_buffer_commit) |
| 225 |
|
| 226 |
_WRAP_METHOD(bool convert(Gst::Format src_fmt, gint64 src_val, Gst::Format dest_fmt, gint64& dest_val) const, gst_audio_ring_buffer_convert) |
| 227 |
|
| 228 |
_WRAP_METHOD_DOCS_ONLY(gst_audio_ring_buffer_prepare_read) |
| 229 |
bool prepare_read(int& segment, std::vector<guint8>& readptr, int& len); |
| 230 |
|
| 231 |
_WRAP_METHOD(guint read(guint64 sample, const std::vector<guint8>& data, guint len, Gst::ClockTime& timestamp), gst_audio_ring_buffer_read) |
| 232 |
|
| 233 |
_WRAP_METHOD(void clear(int segment), gst_audio_ring_buffer_clear) |
| 234 |
_WRAP_METHOD(void clear_all(), gst_audio_ring_buffer_clear_all) |
| 235 |
_WRAP_METHOD(void advance(guint advance), gst_audio_ring_buffer_advance) |
| 236 |
_WRAP_METHOD(bool close_device(), gst_audio_ring_buffer_close_device) |
| 237 |
_WRAP_METHOD(bool open_device(), gst_audio_ring_buffer_open_device) |
| 238 |
_WRAP_METHOD(bool device_is_open() const, gst_audio_ring_buffer_device_is_open) |
| 239 |
_WRAP_METHOD(void set_may_start(bool allowed), gst_audio_ring_buffer_may_start) |
| 240 |
_WRAP_METHOD(static bool parse_caps(Gst::AudioRingBufferSpec&, const Glib::RefPtr<Gst::Caps>& caps), gst_audio_ring_buffer_parse_caps) |
| 241 |
_WRAP_METHOD(void set_flushing(bool flushing), gst_audio_ring_buffer_set_flushing) |
| 242 |
|
| 243 |
/** Virtual function to open the device. Don't set any params or allocate |
| 244 |
* anything. |
| 245 |
*/ |
| 246 |
_WRAP_VFUNC(bool open_device(), "open_device") |
| 247 |
|
| 248 |
/** Virtual function to allocate the resources for the ring buffer using the |
| 249 |
* given spec. |
| 250 |
*/ |
| 251 |
virtual bool acquire_vfunc(Gst::AudioRingBufferSpec& spec); |
| 252 |
|
| 253 |
/** Virtual function to free resources of the ring buffer. |
| 254 |
*/ |
| 255 |
_WRAP_VFUNC(bool release(), "release") |
| 256 |
|
| 257 |
/** Virtual function to close the device. |
| 258 |
*/ |
| 259 |
_WRAP_VFUNC(bool close_device(), "close_device") |
| 260 |
|
| 261 |
/** Virtual function to start processing of samples. |
| 262 |
*/ |
| 263 |
_WRAP_VFUNC(bool start(), "start") |
| 264 |
|
| 265 |
/** Virtual function to pause processing of samples. |
| 266 |
*/ |
| 267 |
_WRAP_VFUNC(bool pause(), "pause") |
| 268 |
|
| 269 |
/** Virtual function to resume processing of samples after pause. |
| 270 |
*/ |
| 271 |
_WRAP_VFUNC(bool resume(), "resume") |
| 272 |
|
| 273 |
/** Virtual function to stop processing of samples. |
| 274 |
*/ |
| 275 |
_WRAP_VFUNC(bool stop(), "stop") |
| 276 |
|
| 277 |
/** Virtual function to get number of samples queued in device. |
| 278 |
*/ |
| 279 |
_WRAP_VFUNC(guint delay(), "delay") |
| 280 |
|
| 281 |
/** Virtual function to activate the thread that starts pulling and |
| 282 |
* monitoring the consumed segments in the device. Since 0.10.22. |
| 283 |
*/ |
| 284 |
_WRAP_VFUNC(bool activate(bool active), "activate") |
| 285 |
|
| 286 |
/** Virtual function to write samples into the ring buffer. |
| 287 |
*/ |
| 288 |
virtual guint commit_vfunc(guint64& sample, const std::vector<guint8>& data, |
| 289 |
int in_samples, int out_samples, int& accum); |
| 290 |
|
| 291 |
/** Virtual function to clear the entire audioringbuffer Since 0.10.24. |
| 292 |
*/ |
| 293 |
_WRAP_VFUNC(void clear_all(), "clear_all") |
| 294 |
|
| 295 |
protected: |
| 296 |
#m4begin |
| 297 |
_PUSH(SECTION_PCC_CLASS_INIT_VFUNCS) |
| 298 |
klass->acquire = &acquire_vfunc_callback; |
| 299 |
klass->commit = &commit_vfunc_callback; |
| 300 |
_SECTION(SECTION_PH_VFUNCS) |
| 301 |
static gboolean acquire_vfunc_callback(GstAudioRingBuffer* self, GstAudioRingBufferSpec* spec); |
| 302 |
static guint commit_vfunc_callback(GstAudioRingBuffer* self, guint64* sample, guchar* data, gint in_samples, gint out_samples, gint* accum); |
| 303 |
_POP() |
| 304 |
#m4end |
| 305 |
|
| 306 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 307 |
private: |
| 308 |
// todo this slot should be moved in move constructor, but for now it's |
| 309 |
// impossible to provide custom move constructor |
| 310 |
// (see https://bugzilla.gnome.org/show_bug.cgi?id=756593). |
| 311 |
// However, task should be managed by RefPtr class, so move constructor |
| 312 |
// and move assignment operator will never be called. |
| 313 |
std::unique_ptr<SlotFill> m_slot; |
| 314 |
#endif |
| 315 |
}; |
| 316 |
|
| 317 |
} // namespace Gst |