1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QAUDIOOUTPUTPULSE_H
41 #define QAUDIOOUTPUTPULSE_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtCore/qfile.h>
55 #include <QtCore/qtimer.h>
56 #include <QtCore/qstring.h>
57 #include <QtCore/qstringlist.h>
58 #include <QtCore/qelapsedtimer.h>
59 #include <QtCore/qiodevice.h>
60 
61 #include "qaudio.h"
62 #include "qaudiodeviceinfo.h"
63 #include "qaudiosystem.h"
64 
65 #include <pulse/pulseaudio.h>
66 
67 QT_BEGIN_NAMESPACE
68 
69 class QPulseAudioOutput : public QAbstractAudioOutput
70 {
71     friend class PulseOutputPrivate;
72     Q_OBJECT
73 
74 public:
75     QPulseAudioOutput(const QByteArray &device);
76     ~QPulseAudioOutput();
77 
78     void start(QIODevice *device) override;
79     QIODevice *start() override;
80     void stop() override;
81     void reset() override;
82     void suspend() override;
83     void resume() override;
84     int bytesFree() const override;
85     int periodSize() const override;
86     void setBufferSize(int value) override;
87     int bufferSize() const override;
88     void setNotifyInterval(int milliSeconds) override;
89     int notifyInterval() const override;
90     qint64 processedUSecs() const override;
91     qint64 elapsedUSecs() const override;
92     QAudio::Error error() const override;
93     QAudio::State state() const override;
94     void setFormat(const QAudioFormat &format) override;
95     QAudioFormat format() const override;
96 
97     void setVolume(qreal volume) override;
98     qreal volume() const override;
99 
100     void setCategory(const QString &category) override;
101     QString category() const override;
102 
103 public:
104     void streamUnderflowCallback();
105 
106 private:
107     void setState(QAudio::State state);
108     void setError(QAudio::Error error);
109 
110     bool open();
111     void close();
112     qint64 write(const char *data, qint64 len);
113 
114 private Q_SLOTS:
115     void userFeed();
116     void onPulseContextFailed();
117 
118 private:
119     QByteArray m_device;
120     QByteArray m_streamName;
121     QAudioFormat m_format;
122     QAudio::Error m_errorState;
123     QAudio::State m_deviceState;
124     bool m_pullMode;
125     bool m_opened;
126     QIODevice *m_audioSource;
127     QTimer m_periodTimer;
128     int m_periodTime;
129     pa_stream *m_stream;
130     int m_notifyInterval;
131     int m_periodSize;
132     int m_bufferSize;
133     int m_maxBufferSize;
134     QElapsedTimer m_clockStamp;
135     qint64 m_totalTimeValue;
136     QTimer *m_tickTimer;
137     char *m_audioBuffer;
138     QElapsedTimer m_timeStamp;
139     qint64 m_elapsedTimeOffset;
140     bool m_resuming;
141     QString m_category;
142 
143     qreal m_volume;
144     pa_sample_spec m_spec;
145 };
146 
147 class PulseOutputPrivate : public QIODevice
148 {
149     friend class QPulseAudioOutput;
150     Q_OBJECT
151 
152 public:
153     PulseOutputPrivate(QPulseAudioOutput *audio);
~PulseOutputPrivate()154     virtual ~PulseOutputPrivate() {}
155 
156 protected:
157     qint64 readData(char *data, qint64 len) override;
158     qint64 writeData(const char *data, qint64 len) override;
159 
160 private:
161     QPulseAudioOutput *m_audioDevice;
162 };
163 
164 QT_END_NAMESPACE
165 
166 #endif
167