1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QAUDIOOUTPUTPULSE_H |
5 | #define QAUDIOOUTPUTPULSE_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtCore/qfile.h> |
19 | #include <QtCore/qtimer.h> |
20 | #include <QtCore/qstring.h> |
21 | #include <QtCore/qstringlist.h> |
22 | #include <QtCore/qelapsedtimer.h> |
23 | #include <QtCore/qiodevice.h> |
24 | |
25 | #include "qaudio.h" |
26 | #include "qaudiodevice.h" |
27 | #include "pulseaudio/qpulsehelpers_p.h" |
28 | |
29 | #include <private/qaudiosystem_p.h> |
30 | #include <private/qaudiostatemachine_p.h> |
31 | #include <pulse/pulseaudio.h> |
32 | |
33 | QT_BEGIN_NAMESPACE |
34 | |
35 | class QPulseAudioSink : public QPlatformAudioSink |
36 | { |
37 | friend class PulseOutputPrivate; |
38 | Q_OBJECT |
39 | |
40 | public: |
41 | QPulseAudioSink(const QByteArray &device, QObject *parent); |
42 | ~QPulseAudioSink() override; |
43 | |
44 | void start(QIODevice *device) override; |
45 | QIODevice *start() override; |
46 | void stop() override; |
47 | void reset() override; |
48 | void suspend() override; |
49 | void resume() override; |
50 | qsizetype bytesFree() const override; |
51 | void setBufferSize(qsizetype value) override; |
52 | qsizetype bufferSize() const override; |
53 | qint64 processedUSecs() const override; |
54 | QAudio::Error error() const override; |
55 | QAudio::State state() const override; |
56 | void setFormat(const QAudioFormat &format); |
57 | QAudioFormat format() const override; |
58 | |
59 | void setVolume(qreal volume) override; |
60 | qreal volume() const override; |
61 | |
62 | void streamUnderflowCallback(); |
63 | void streamDrainedCallback(); |
64 | |
65 | protected: |
66 | void timerEvent(QTimerEvent *event) override; |
67 | |
68 | private: |
69 | void startPulling(); |
70 | void stopTimer(); |
71 | |
72 | bool open(); |
73 | void close(); |
74 | qint64 write(const char *data, qint64 len); |
75 | |
76 | private Q_SLOTS: |
77 | void userFeed(); |
78 | void onPulseContextFailed(); |
79 | |
80 | private: |
81 | using PAOperationHandle = QPulseAudioInternal::PAOperationHandle; |
82 | using PAStreamHandle = QPulseAudioInternal::PAStreamHandle; |
83 | PAOperationHandle exchangeDrainOperation(pa_operation *newOperation); |
84 | |
85 | qsizetype defaultBufferSize() const; |
86 | |
87 | pa_sample_spec m_spec = {}; |
88 | // calculate timing manually, as pulseaudio doesn't give us good enough data |
89 | mutable timeval lastTimingInfo = {}; |
90 | |
91 | mutable QList<qint64> latencyList; // last latency values |
92 | |
93 | QByteArray m_device; |
94 | QByteArray m_streamName; |
95 | QAudioFormat m_format; |
96 | QBasicTimer m_tickTimer; |
97 | |
98 | QIODevice *m_audioSource = nullptr; |
99 | PAStreamHandle m_stream; |
100 | std::vector<char> m_audioBuffer; |
101 | |
102 | qint64 m_totalTimeValue = 0; |
103 | qint64 m_elapsedTimeOffset = 0; |
104 | mutable qint64 averageLatency = 0; // average latency |
105 | mutable qint64 lastProcessedUSecs = 0; |
106 | qreal m_volume = 1.0; |
107 | |
108 | std::atomic<pa_operation *> m_drainOperation = nullptr; |
109 | qsizetype m_bufferSize = 0; |
110 | std::optional<qsizetype> m_userBufferSize = std::nullopt; |
111 | int m_pullingPeriodSize = 0; |
112 | int m_pullingPeriodTime = 0; |
113 | bool m_pullMode = true; |
114 | bool m_opened = false; |
115 | |
116 | QAudioStateMachine m_stateMachine; |
117 | }; |
118 | |
119 | class PulseOutputPrivate : public QIODevice |
120 | { |
121 | friend class QPulseAudioSink; |
122 | Q_OBJECT |
123 | |
124 | public: |
125 | PulseOutputPrivate(QPulseAudioSink *audio); |
126 | ~PulseOutputPrivate() override = default; |
127 | |
128 | protected: |
129 | qint64 readData(char *data, qint64 len) override; |
130 | qint64 writeData(const char *data, qint64 len) override; |
131 | |
132 | private: |
133 | QPulseAudioSink *m_audioDevice; |
134 | }; |
135 | |
136 | QT_END_NAMESPACE |
137 | |
138 | #endif |
139 | |