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 QAUDIOINPUTGSTREAMER_H |
16 | #define QAUDIOINPUTGSTREAMER_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 | #include <QtCore/qmutex.h> |
25 | #include <QtCore/qatomic.h> |
26 | #include <QtCore/private/qringbuffer_p.h> |
27 | |
28 | #include "qaudio.h" |
29 | #include "qaudiodevice.h" |
30 | #include <private/qaudiosystem_p.h> |
31 | |
32 | #include <qgstutils_p.h> |
33 | #include <qgstpipeline_p.h> |
34 | #include <gst/app/gstappsink.h> |
35 | |
36 | QT_BEGIN_NAMESPACE |
37 | |
38 | class GStreamerInputPrivate; |
39 | |
40 | class QGStreamerAudioSource |
41 | : public QPlatformAudioSource |
42 | { |
43 | Q_OBJECT |
44 | friend class GStreamerInputPrivate; |
45 | public: |
46 | QGStreamerAudioSource(const QAudioDevice &device, QObject *parent); |
47 | ~QGStreamerAudioSource(); |
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 bytesReady() 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 | |
67 | private Q_SLOTS: |
68 | void newDataAvailable(GstSample *sample); |
69 | |
70 | private: |
71 | void setState(QAudio::State state); |
72 | void setError(QAudio::Error error); |
73 | |
74 | QGstElement createAppSink(); |
75 | static GstFlowReturn new_sample(GstAppSink *, gpointer user_data); |
76 | static void eos(GstAppSink *, gpointer user_data); |
77 | |
78 | bool open(); |
79 | void close(); |
80 | |
81 | static gboolean busMessage(GstBus *bus, GstMessage *msg, gpointer user_data); |
82 | |
83 | QAudioDevice m_info; |
84 | qint64 m_bytesWritten = 0; |
85 | QIODevice *m_audioSink = nullptr; |
86 | QAudioFormat m_format; |
87 | QAudio::Error m_errorState = QAudio::NoError; |
88 | QAudio::State m_deviceState = QAudio::StoppedState; |
89 | qreal m_volume = 1.; |
90 | |
91 | QRingBuffer m_buffer; |
92 | QAtomicInteger<bool> m_pullMode = true; |
93 | bool m_opened = false; |
94 | int m_bufferSize = 0; |
95 | qint64 m_elapsedTimeOffset = 0; |
96 | QElapsedTimer m_timeStamp; |
97 | QByteArray m_device; |
98 | QByteArray m_tempBuffer; |
99 | |
100 | QGstElement gstInput; |
101 | QGstPipeline gstPipeline; |
102 | QGstElement gstVolume; |
103 | QGstElement gstAppSink; |
104 | }; |
105 | |
106 | class GStreamerInputPrivate : public QIODevice |
107 | { |
108 | Q_OBJECT |
109 | public: |
110 | GStreamerInputPrivate(QGStreamerAudioSource *audio); |
111 | ~GStreamerInputPrivate() {}; |
112 | |
113 | qint64 readData(char *data, qint64 len) override; |
114 | qint64 writeData(const char *data, qint64 len) override; |
115 | qint64 bytesAvailable() const override; |
116 | bool isSequential() const override { return true; } |
117 | private: |
118 | QGStreamerAudioSource *m_audioDevice; |
119 | }; |
120 | |
121 | QT_END_NAMESPACE |
122 | |
123 | #endif |
124 | |