1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/*************************************************************************
Record-PulseAudio.h - device for audio recording via PulesAudio
-------------------
begin : Sun Okt 20 2013
copyright : (C) 2014 by Joerg-Christian Boehme
email : joerg@chaosdorf.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef KWAVE_RECORD_PULSEAUDIO_H
#define KWAVE_RECORD_PULSEAUDIO_H
#include "config.h"
#ifdef HAVE_PULSEAUDIO_SUPPORT
#include <poll.h>
#include <pulse/context.h>
#include <pulse/error.h>
#include <pulse/introspect.h>
#include <pulse/mainloop.h>
#include <pulse/proplist.h>
#include <pulse/sample.h>
#include <pulse/stream.h>
#include <QList>
#include <QMutex>
#include <QString>
#include <QStringList>
#include <QWaitCondition>
#include "libkwave/Runnable.h"
#include "libkwave/SampleFormat.h"
#include "libkwave/WorkerThread.h"
#include "RecordDevice.h"
namespace Kwave
{
class RecordPulseAudio: public Kwave::RecordDevice, public Kwave::Runnable
{
public:
/** Constructor */
RecordPulseAudio();
/** Destructor */
~RecordPulseAudio() override;
/**
* Open the record device.
* @param dev path of the record device
* @retval QString() if successful
* @retval QString::number(ENODEV) if device not found
* @retval QString::number(EBUSY) if device is busy
* @retval QString(...) device specific error message
* (already translated)
*/
QString open(const QString& dev) override;
/** Returns the current endianness (big/little) */
Kwave::byte_order_t endianness() override;
/** Returns the current sample format (signed/unsigned) */
Kwave::SampleFormat::Format sampleFormat() override;
/**
* Try to set a new sample format (signed/unsigned)
* @param new_format the identifier for the new format
* @return zero on success, negative error code if failed
* @see class SampleFormat
*/
virtual int setSampleFormat(Kwave::SampleFormat::Format new_format)
override;
/**
* Gets a list of supported sample formats.
* @note this depends on the current setting of the compression!
*/
virtual QList<Kwave::SampleFormat::Format> detectSampleFormats()
override;
/**
* Returns the current resolution in bits per sample or a negative
* error code if failed
*/
int bitsPerSample() override;
/**
* Set the resolution in bits per sample
* @param new_bits resolution [bits/sample]
* @return zero on success, negative error code if failed
*/
int setBitsPerSample(unsigned int new_bits) override;
/**
* Detect a list of supported bits per sample.
* @note this depends on the compression type
* @return a list of bits per sample, empty if failed
*/
QList< unsigned int > supportedBits() override;
/** Returns the current compression type (0==none) */
Kwave::Compression::Type compression() override;
/**
* Try to set a new compression type.
* @param new_compression the identifier of the new compression
* @return zero on success, negative error code if failed
* @see class Compression
*/
virtual int setCompression(Kwave::Compression::Type new_compression)
override;
/**
* Gets a list of supported compression types. If no compression is
* supported, the list might be empty.
*/
virtual QList<Kwave::Compression::Type> detectCompressions()
override;
/** Returns the current sample rate of the device */
double sampleRate() override;
/**
* Try to set a new sample rate.
* @param new_rate the sample rate to be set [samples/second], can
* be modified and rounded up/down to the nearest supported
* sample rate if the underlying driver supports that.
* @return zero on success, negative error code if failed
*/
int setSampleRate(double& new_rate) override;
/** get a list of supported sample rates */
QList< double > detectSampleRates() override;
/** Returns the current number of tracks */
int tracks() override;
/**
* Try to set a new number of tracks.
* @note the device must be open
* @param tracks the number of tracks to be set, can be modified and
* decreased to the next supported number of tracks if the
* underlying driver supports that.
* @return zero on success, negative error code if failed
*/
int setTracks(unsigned int& tracks) override;
/**
* Detect the minimum and maximum number of tracks.
* If the detection fails, minimum and maximum are set to zero.
* @param min receives the lowest supported number of tracks
* @param max receives the highest supported number of tracks
* @return zero or positive number if ok,
* negative error number if failed
*/
virtual int detectTracks(unsigned int& min, unsigned int& max)
override;
/** Close the device */
int close() override;
/**
* Read the raw audio data from the record device.
* @param buffer array of bytes to receive the audio data
* might be resized for alignment
* @param offset offset in bytes within the buffer
* @return number of bytes read, zero or negative if failed
*/
virtual int read(QByteArray& buffer, unsigned int offset)
override;
/** return a string list with supported device names */
QStringList supportedDevices() override;
/**
* our own poll function, for timeout support
* @internal
*/
int mainloopPoll(struct pollfd *ufds, unsigned long int nfds,
int timeout);
protected:
/** re-implementation of the threaded mainloop of PulseAudio */
void run_wrapper(const QVariant ¶ms) override;
private:
/**
* Walk through the list of all known formats and collect the
* ones that are supported into "m_supported_formats".
* @param device name of the device
*/
void detectSupportedFormats(const QString &device);
/**
* called from pulse audio to inform about state changes of the
* server context.
*
* @param c pulse server context
* @param userdata user data, pointer to a RecordPulseAudio object
*/
static void pa_context_notify_cb(pa_context *c, void *userdata);
/**
* called from pulse audio to inform about state changes of the
* server context.
*
* @param c pulse server context
* @param info pointer to a source info object
* @param eol if negative: error occurred, zero: more data follows,
* positive: end of info, done.
* @param userdata pointer to a RecordPulseAudio object
*/
static void pa_source_info_cb(pa_context *c, const pa_source_info *info,
int eol, void *userdata);
/**
* called from pulse audio to inform about state changes of a
* stream.
*
* @param p pulse audio stream
* @param userdata user data, pointer to a RecordPulseAudio object
*/
static void pa_stream_state_cb(pa_stream *p, void *userdata);
/**
* called from pulse audio after data has been read
*
* @param p pulse audio stream
* @param nbytes number of read bytes, maybe (unused)
* @param userdata user data, pointer to a RecordPulseAudio object
*/
static void pa_read_cb(pa_stream *p, size_t nbytes, void *userdata);
/**
* Callback for pulse sink info.
*
* @param c pulse server context
* @param info pointer to a source info object
* @param eol if negative: error occurred, zero: more data follows,
* positive: end of info, done.
*/
void notifySourceInfo(pa_context *c, const pa_source_info *info,
int eol);
/**
* Callback for pulse audio context state changes
*
* @param c pulse server context
*/
void notifyContext(pa_context *c);
/**
* Callback for pulse stream state changes
*
* @param stream pulse audio stream
*/
void notifyStreamState(pa_stream *stream);
/**
* Callback after reading data.
*
* @param stream pulse audio stream
* @param nbytes number of read bytes, maybe (unused)
*/
void notifyRead(pa_stream *stream, size_t nbytes);
/**
* Try to connect to the PulseAudio server and create a valid context
*/
bool connectToServer();
/**
* Disconnect from the PulseAudio server and clean up
*/
void disconnectFromServer();
/** scan all PulseAudio source, re-creates m_device_list */
void scanDevices();
/**
* create a PulseAudio device format (enum) from parameters.
* @param compression the compression type
* @see Compression
* @param bits the number of bits per sample, related
* to the decoded stream
* @param sample_format the sample format
* (signed or unsigned)
* @return the best matching format within the list of known formats,
* or PA_SAMPLE_INVALID if no match was found
*/
pa_sample_format_t mode2format(int compression, int bits,
Kwave::SampleFormat::Format sample_format);
/**
* Initialize the PulseAudio device with current parameters and
* prepare it for recording.
* @param buffer_size buffer size
* @return zero on success or negative error code
* -EINVAL or -EIO
*/
int initialize(uint32_t buffer_size);
private:
/** relevant information about a PulseAudio source */
typedef struct
{
QString m_name; /**< internal name of the source */
QString m_description; /**< verbose name of the source */
QString m_driver; /**< internal driver name */
quint32 m_card; /**< index of the card */
pa_sample_spec m_sample_spec; /**< accepted sample format */
} source_info_t;
private:
/** worker thread, running the event loop */
Kwave::WorkerThread m_mainloop_thread;
/** lock for the main loop */
QMutex m_mainloop_lock;
/** wait condition for mainloopWait/mainloopSignal */
QWaitCondition m_mainloop_signal;
/** sample format (signed int, unsigned int, float, ... */
Kwave::SampleFormat::Format m_sample_format;
/** number of tracks [0...N-1] */
quint8 m_tracks;
/** sample rate */
double m_rate;
/** compression mode */
Kwave::Compression::Type m_compression;
/** resolution [bits per sample] */
unsigned int m_bits_per_sample;
/**
* list of supported formats of the current device, indices in
* the global list of known formats.
* Only valid after a successful call to "open()",
* otherwise empty
*/
QList<pa_sample_format_t> m_supported_formats;
/** true if initialize() has been successfully been run */
bool m_initialized;
/** pulse: property list of the context */
pa_proplist *m_pa_proplist;
/** pulse: main loop */
pa_mainloop *m_pa_mainloop;
/** pulse: context of the connection to the server */
pa_context *m_pa_context;
/** pulse: playback stream */
pa_stream *m_pa_stream;
/** pulse: device */
QString m_pa_device;
/** record plugin name */
QString m_name;
/** encoded name of the sink */
QString m_device;
/**
* list of available devices
* key=full encoded name of the sink, data=info about the sink
*/
QMap<QString, source_info_t> m_device_list;
};
}
#endif /* HAVE_PULSEAUDIO_SUPPORT */
#endif // KWAVE_RECORD_PULSEAUDIO_H
|