File: PlayBack-PulseAudio.h

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (304 lines) | stat: -rw-r--r-- 9,979 bytes parent folder | download
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
/***************************************************************************
  PlayBack-PulseAudio.h  -  playback device for PulseAudio
                             -------------------
    begin                : Tue Sep 29 2009
    copyright            : (C) 2009 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.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 PLAY_BACK_PULSE_AUDIO_H
#define PLAY_BACK_PULSE_AUDIO_H

#include "config.h"
#ifdef HAVE_PULSEAUDIO_SUPPORT

#include <poll.h>

#include <pulse/context.h>
#include <pulse/error.h>
#include <pulse/gccmacro.h>
#include <pulse/introspect.h>
#include <pulse/mainloop.h>
#include <pulse/proplist.h>
#include <pulse/stream.h>

#include <QList>
#include <QMap>
#include <QMutex>
#include <QString>
#include <QWaitCondition>

#include "libkwave/FileInfo.h"
#include "libkwave/PlayBackDevice.h"
#include "libkwave/Runnable.h"
#include "libkwave/SampleArray.h"
#include "libkwave/WorkerThread.h"

namespace Kwave
{
    class PlayBackPulseAudio: public Kwave::PlayBackDevice,
                              public Kwave::Runnable
    {
    public:

        /**
         * Constructor
         * @param info the current FileInfo with metadata
         */
        explicit PlayBackPulseAudio(const Kwave::FileInfo &info);

        /** Destructor */
        ~PlayBackPulseAudio() override;

        /**
         * Opens the device for playback.
         * @see PlayBackDevice::open
         */
        virtual QString open(const QString &device, double rate,
                             unsigned int channels, unsigned int bits,
                             unsigned int bufbase) override;

        /**
         * Writes an array of samples to the output device.
         * @see PlayBackDevice::write
         */
        int write(const Kwave::SampleArray &samples) override;

        /**
         * Closes the output device.
         * @see PlayBackDevice::close
         */
        int close() override;

        /** return a string list with supported device names */
        QStringList supportedDevices() override;

        /** return a string suitable for a "File Open..." dialog */
        QString fileFilter() override;

        /**
         * returns a list of supported bits per sample resolutions
         * of a given device.
         *
         * @param device filename of the device
         * @return list of supported bits per sample, or empty on errors
         */
        virtual QList<unsigned int> supportedBits(const QString &device)
            override;

        /**
         * Detect the minimum and maximum number of channels.
         * If the detection fails, minimum and maximum are set to zero.
         *
         * @param device filename of the device
         * @param min receives the lowest supported number of channels
         * @param max receives the highest supported number of channels
         * @return zero or positive number if ok,
         *         negative error number if failed
         */
        virtual int detectChannels(const QString &device,
                                   unsigned int &min, unsigned int &max)
            override;

        /**
         * our own poll function, for timeout support
         * @internal
         */
        int mainloopPoll(struct pollfd *ufds, unsigned long int nfds,
                         int timeout);

    protected:

        /** Writes the output buffer to the device */
        int flush();

        /** re-implementation of the threaded mainloop of PulseAudio */
        void run_wrapper(const QVariant &params) override;

    private:

        /**
         * called from pulse audio to inform about state changes of the
         * server context.
         *
         * @param c pulse server context
         * @param data user data, pointer to a PlayBackPulseAudio object
         */
        static void pa_context_notify_cb(pa_context *c, void *data);

        /**
         * called from pulse audio to inform about state changes of the
         * server context.
         *
         * @param c pulse server context
         * @param info pointer to a sink info object
         * @param eol if negative: error occurred, zero: more data follows,
         *            positive: end of info, done.
         * @param userdata pointer to a PlayBackPulseAudio object
         */
        static void pa_sink_info_cb(pa_context *c, const pa_sink_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 PlayBackPulseAudio object
         */
        static void pa_stream_state_cb(pa_stream *p, void *userdata);

        /**
         * called from pulse audio after data has been written
         *
         * @param p pulse audio stream
         * @param nbytes number of written bytes, maybe (unused)
         * @param userdata user data, pointer to a PlayBackPulseAudio object
         */
        static void pa_write_cb(pa_stream *p, size_t nbytes, void *userdata);

        /**
         * called from pulse audio after data has been written
         *
         * @param s pulse audio stream
         * @param success indicates success (unused)
         * @param userdata user data, pointer to a PlayBackPulseAudio object
         */
        static void pa_stream_success_cb(pa_stream *s, int success,
                                         void *userdata);

        /**
         * Callback for pulse audio context state changes
         *
         * @param c pulse server context
         */
        void notifyContext(pa_context *c);

        /**
         * Callback for pulse sink info
         *
         * @param c pulse server context
         * @param info pointer to a sink info object
         * @param eol if negative: error occurred, zero: more data follows,
         *            positive: end of info, done.
         */
        void notifySinkInfo(pa_context *c, const pa_sink_info *info, int eol);

        /**
         * Callback for pulse stream state changes
         *
         * @param stream pulse audio stream
         */
        void notifyStreamState(pa_stream *stream);

        /**
         * Callback after writing data.
         *
         * @param stream pulse audio stream
         * @param nbytes number of written bytes, maybe (unused)
         */
        void notifyWrite(pa_stream *stream, size_t nbytes);

        /**
         * Callback after successful stream operations.
         *
         * @param stream pulse audio stream
         * @param success (unused)
         */
        void notifySuccess(pa_stream *stream, int success);

        /**
         * 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 sinks, re-creates m_device_list */
        void scanDevices();

    private:

        /** relevant information about a PulseAudio sink */
        typedef struct
        {
            QString m_name;               /**< internal name of the sink  */
            QString m_description;        /**< verbose name of the sink   */
            QString m_driver;             /**< internal driver name       */
            quint32  m_card;              /**< index of the card or -1    */
            pa_sample_spec m_sample_spec; /**< accepted sample format     */
        } sink_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;

        /** file info, for meta info like title, author, name etc. */
        Kwave::FileInfo m_info;

        /** sample rate used when opening the device */
        double m_rate;

        /** number of bytes per sample x nr of channels */
        unsigned int m_bytes_per_sample;

        /** buffer with raw device data */
        void *m_buffer;

        /** buffer size in bytes */
        size_t m_buffer_size;

        /** number of bytes in the buffer */
        size_t m_buffer_used;

        /**
         * exponent of the buffer size,
         * buffer size should be (1 << m_bufbase)
         */
        unsigned int m_bufbase;

        /** 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;

        /**
         * list of available devices
         * key=full encoded name of the sink, data=info about the sink
         */
        QMap<QString, sink_info_t> m_device_list;

    };
}

#endif /* HAVE_PULSEAUDIO_SUPPORT */

#endif /* PLAY_BACK_PULSE_AUDIO_H */

//***************************************************************************
//***************************************************************************