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//
5// W A R N I N G
6// -------------
7//
8// This file is not part of the Qt API. It exists for the convenience
9// of other Qt classes. This header file may change from version to
10// version without notice, or even be removed.
11//
12// We mean it.
13//
14
15#ifndef QAUDIOINPUTPULSE_H
16#define QAUDIOINPUTPULSE_H
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 <private/qaudiosystem_p.h>
28
29#include <pulse/pulseaudio.h>
30
31QT_BEGIN_NAMESPACE
32
33class PulseInputPrivate;
34
35class QPulseAudioSource : public QPlatformAudioSource
36{
37 Q_OBJECT
38
39public:
40 QPulseAudioSource(const QByteArray &device, QObject *parent);
41 ~QPulseAudioSource();
42
43 qint64 read(char *data, qint64 len);
44
45 void start(QIODevice *device) override;
46 QIODevice *start() override;
47 void stop() override;
48 void reset() override;
49 void suspend() override;
50 void resume() override;
51 qsizetype bytesReady() const override;
52 void setBufferSize(qsizetype value) override;
53 qsizetype bufferSize() const override;
54 qint64 processedUSecs() const override;
55 QAudio::Error error() const override;
56 QAudio::State state() const override;
57 void setFormat(const QAudioFormat &format) override;
58 QAudioFormat format() const override;
59
60 void setVolume(qreal volume) override;
61 qreal volume() const override;
62
63 qint64 m_totalTimeValue;
64 QIODevice *m_audioSource;
65 QAudioFormat m_format;
66 QAudio::Error m_errorState;
67 QAudio::State m_deviceState;
68 qreal m_volume;
69
70private slots:
71 void userFeed();
72 bool deviceReady();
73 void onPulseContextFailed();
74
75private:
76 void setState(QAudio::State state);
77 void setError(QAudio::Error error);
78
79 void applyVolume(const void *src, void *dest, int len);
80
81 int checkBytesReady();
82 bool open();
83 void close();
84
85 bool m_pullMode;
86 bool m_opened;
87 int m_bytesAvailable;
88 int m_bufferSize;
89 int m_periodSize;
90 unsigned int m_periodTime;
91 QTimer *m_timer;
92 qint64 m_elapsedTimeOffset;
93 pa_stream *m_stream;
94 QByteArray m_streamName;
95 QByteArray m_device;
96 QByteArray m_tempBuffer;
97 pa_sample_spec m_spec;
98};
99
100class PulseInputPrivate : public QIODevice
101{
102 Q_OBJECT
103public:
104 PulseInputPrivate(QPulseAudioSource *audio);
105 ~PulseInputPrivate() override = default;
106
107 qint64 readData(char *data, qint64 len) override;
108 qint64 writeData(const char *data, qint64 len) override;
109
110 void trigger();
111
112private:
113 QPulseAudioSource *m_audioDevice;
114};
115
116QT_END_NAMESPACE
117
118#endif
119

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