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 QAUDIOOUTPUTGSTREAMER_H
5#define QAUDIOOUTPUTGSTREAMER_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#include <QtCore/private/qringbuffer_p.h>
25
26#include "qaudio.h"
27#include "qaudiodevice.h"
28#include <private/qaudiosystem_p.h>
29#include <private/qmultimediautils_p.h>
30
31#include <qgst_p.h>
32#include <qgstpipeline_p.h>
33
34QT_BEGIN_NAMESPACE
35
36class QGstAppSrc;
37
38class QGStreamerAudioSink
39 : public QPlatformAudioSink,
40 public QGstreamerBusMessageFilter
41{
42 friend class GStreamerOutputPrivate;
43 Q_OBJECT
44
45public:
46 static QMaybe<QPlatformAudioSink *> create(const QAudioDevice &device, QObject *parent);
47 ~QGStreamerAudioSink();
48
49 void start(QIODevice *device) override;
50 QIODevice *start() override;
51 void stop() override;
52 void reset() override;
53 void suspend() override;
54 void resume() override;
55 qsizetype bytesFree() const override;
56 void setBufferSize(qsizetype value) override;
57 qsizetype bufferSize() const override;
58 qint64 processedUSecs() const override;
59 QAudio::Error error() const override;
60 QAudio::State state() const override;
61 void setFormat(const QAudioFormat &format) override;
62 QAudioFormat format() const override;
63
64 void setVolume(qreal volume) override;
65 qreal volume() const override;
66
67private Q_SLOTS:
68 void bytesProcessedByAppSrc(int bytes);
69 void needData();
70
71private:
72 QGStreamerAudioSink(const QAudioDevice &device, QGstAppSrc *appsrc, QGstElement audioconvert,
73 QGstElement volume, QObject *parent);
74
75 void setState(QAudio::State state);
76 void setError(QAudio::Error error);
77
78 bool processBusMessage(const QGstreamerMessage &message) override;
79
80 bool open();
81 void close();
82 qint64 write(const char *data, qint64 len);
83
84private:
85 QByteArray m_device;
86 QAudioFormat m_format;
87 QAudio::Error m_errorState = QAudio::NoError;
88 QAudio::State m_deviceState = QAudio::StoppedState;
89 QAudio::State m_suspendedInState = QAudio::SuspendedState;
90 bool m_pullMode = true;
91 bool m_opened = false;
92 QIODevice *m_audioSource = nullptr;
93 int m_bufferSize = 0;
94 qint64 m_bytesProcessed = 0;
95 QElapsedTimer m_timeStamp;
96 qreal m_volume = 1.;
97 QByteArray pushData;
98
99 QGstPipeline gstPipeline;
100 QGstElement gstOutput;
101 QGstElement gstVolume;
102 QGstElement gstAppSrc;
103 QGstAppSrc *m_appSrc = nullptr;
104};
105
106class GStreamerOutputPrivate : public QIODevice
107{
108 friend class QGStreamerAudioSink;
109 Q_OBJECT
110
111public:
112 GStreamerOutputPrivate(QGStreamerAudioSink *audio);
113 virtual ~GStreamerOutputPrivate() {}
114
115protected:
116 qint64 readData(char *data, qint64 len) override;
117 qint64 writeData(const char *data, qint64 len) override;
118
119private:
120 QGStreamerAudioSink *m_audioDevice;
121};
122
123QT_END_NAMESPACE
124
125#endif
126

source code of qtmultimedia/src/plugins/multimedia/gstreamer/audio/qgstreameraudiosink_p.h