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(); |
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) override; |
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 | PAOperationUPtr exchangeDrainOperation(pa_operation *newOperation); |
81 | |
82 | private: |
83 | qsizetype defaultBufferSize() const; |
84 | |
85 | pa_sample_spec m_spec = {}; |
86 | // calculate timing manually, as pulseaudio doesn't give us good enough data |
87 | mutable timeval lastTimingInfo = {}; |
88 | |
89 | mutable QList<qint64> latencyList; // last latency values |
90 | |
91 | QByteArray m_device; |
92 | QByteArray m_streamName; |
93 | QAudioFormat m_format; |
94 | QBasicTimer m_tickTimer; |
95 | |
96 | QIODevice *m_audioSource = nullptr; |
97 | pa_stream *m_stream = nullptr; |
98 | std::vector<char> m_audioBuffer; |
99 | |
100 | qint64 m_totalTimeValue = 0; |
101 | qint64 m_elapsedTimeOffset = 0; |
102 | mutable qint64 averageLatency = 0; // average latency |
103 | mutable qint64 lastProcessedUSecs = 0; |
104 | qreal m_volume = 1.0; |
105 | |
106 | std::atomic<pa_operation *> m_drainOperation = nullptr; |
107 | qsizetype m_bufferSize = 0; |
108 | std::optional<qsizetype> m_userBufferSize = std::nullopt; |
109 | int m_pullingPeriodSize = 0; |
110 | int m_pullingPeriodTime = 0; |
111 | bool m_pullMode = true; |
112 | bool m_opened = false; |
113 | |
114 | QAudioStateMachine m_stateMachine; |
115 | }; |
116 | |
117 | class PulseOutputPrivate : public QIODevice |
118 | { |
119 | friend class QPulseAudioSink; |
120 | Q_OBJECT |
121 | |
122 | public: |
123 | PulseOutputPrivate(QPulseAudioSink *audio); |
124 | virtual ~PulseOutputPrivate() {} |
125 | |
126 | protected: |
127 | qint64 readData(char *data, qint64 len) override; |
128 | qint64 writeData(const char *data, qint64 len) override; |
129 | |
130 | private: |
131 | QPulseAudioSink *m_audioDevice; |
132 | }; |
133 | |
134 | QT_END_NAMESPACE |
135 | |
136 | #endif |
137 | |