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
33QT_BEGIN_NAMESPACE
34
35class QPulseAudioSink : public QPlatformAudioSink
36{
37 friend class PulseOutputPrivate;
38 Q_OBJECT
39
40public:
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
65protected:
66 void timerEvent(QTimerEvent *event) override;
67
68private:
69 void startReading();
70
71 bool open();
72 void close();
73 qint64 write(const char *data, qint64 len);
74
75private Q_SLOTS:
76 void userFeed();
77 void onPulseContextFailed();
78
79 PAOperationUPtr exchangeDrainOperation(pa_operation *newOperation);
80
81private:
82 pa_sample_spec m_spec = {};
83 // calculate timing manually, as pulseaudio doesn't give us good enough data
84 mutable timeval lastTimingInfo = {};
85
86 mutable QList<qint64> latencyList; // last latency values
87
88 QByteArray m_device;
89 QByteArray m_streamName;
90 QAudioFormat m_format;
91 QBasicTimer m_tickTimer;
92
93 QIODevice *m_audioSource = nullptr;
94 pa_stream *m_stream = nullptr;
95 std::vector<char> m_audioBuffer;
96
97 qint64 m_totalTimeValue = 0;
98 qint64 m_elapsedTimeOffset = 0;
99 mutable qint64 averageLatency = 0; // average latency
100 mutable qint64 lastProcessedUSecs = 0;
101 qreal m_volume = 1.0;
102
103 std::atomic<pa_operation *> m_drainOperation = nullptr;
104 int m_periodSize = 0;
105 int m_bufferSize = 0;
106 int m_periodTime = 0;
107 bool m_pullMode = true;
108 bool m_opened = false;
109 bool m_resuming = false;
110
111 QAudioStateMachine m_stateMachine;
112};
113
114class PulseOutputPrivate : public QIODevice
115{
116 friend class QPulseAudioSink;
117 Q_OBJECT
118
119public:
120 PulseOutputPrivate(QPulseAudioSink *audio);
121 virtual ~PulseOutputPrivate() {}
122
123protected:
124 qint64 readData(char *data, qint64 len) override;
125 qint64 writeData(const char *data, qint64 len) override;
126
127private:
128 QPulseAudioSink *m_audioDevice;
129};
130
131QT_END_NAMESPACE
132
133#endif
134

source code of qtmultimedia/src/multimedia/pulseaudio/qpulseaudiosink_p.h