| 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 QPULSEAUDIOSOURCE_P_H |
| 16 | #define QPULSEAUDIOSOURCE_P_H |
| 17 | |
| 18 | #include <QtCore/qtclasshelpermacros.h> |
| 19 | |
| 20 | #include <QtMultimedia/qaudio.h> |
| 21 | #include <QtMultimedia/qaudiodevice.h> |
| 22 | #include <QtMultimedia/private/qaudio_platform_implementation_support_p.h> |
| 23 | #include <QtMultimedia/private/qaudiosystem_p.h> |
| 24 | #include <QtMultimedia/private/qpulsehelpers_p.h> |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | namespace QPulseAudioInternal { |
| 29 | |
| 30 | class QPulseAudioSource; |
| 31 | using namespace QtMultimediaPrivate; |
| 32 | |
| 33 | struct QPulseAudioSourceStream final : QPlatformAudioSourceStream |
| 34 | { |
| 35 | using SourceType = QPulseAudioSource; |
| 36 | |
| 37 | QPulseAudioSourceStream(QAudioDevice, const QAudioFormat &, |
| 38 | std::optional<qsizetype> ringbufferSize, QPulseAudioSource *parent, |
| 39 | float volume, std::optional<int32_t> hardwareBufferSize); |
| 40 | Q_DISABLE_COPY_MOVE(QPulseAudioSourceStream) |
| 41 | ~QPulseAudioSourceStream(); |
| 42 | |
| 43 | using QPlatformAudioSourceStream::bytesReady; |
| 44 | using QPlatformAudioSourceStream::deviceIsRingbufferReader; |
| 45 | using QPlatformAudioSourceStream::processedDuration; |
| 46 | using QPlatformAudioSourceStream::ringbufferSizeInBytes; |
| 47 | using QPlatformAudioSourceStream::setVolume; |
| 48 | |
| 49 | bool start(QIODevice *device); |
| 50 | bool start(AudioCallback &&); |
| 51 | QIODevice *start(); |
| 52 | void stop(ShutdownPolicy); |
| 53 | void suspend(); |
| 54 | void resume(); |
| 55 | bool open() const; |
| 56 | |
| 57 | void updateStreamIdle(bool idle) override; |
| 58 | |
| 59 | private: |
| 60 | enum class StreamType : uint8_t |
| 61 | { |
| 62 | Ringbuffer, |
| 63 | Callback, |
| 64 | }; |
| 65 | |
| 66 | bool startStream(StreamType); |
| 67 | void installCallbacks(StreamType); |
| 68 | void uninstallCallbacks(); |
| 69 | |
| 70 | QPulseAudioSource *m_parent; |
| 71 | PAStreamHandle m_stream; |
| 72 | std::optional<AudioCallback> m_audioCallback; |
| 73 | |
| 74 | // PulseAudio callbacks |
| 75 | void underflowCallback() { } |
| 76 | void overflowCallback() { } |
| 77 | void stateCallback() { } |
| 78 | void readCallbackRingbuffer(size_t bytesToRead); |
| 79 | void readCallbackAudioCallback(size_t bytesToRead); |
| 80 | void latencyUpdateCallback() { } |
| 81 | }; |
| 82 | |
| 83 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 84 | |
| 85 | class QPulseAudioSource final |
| 86 | : public QPlatformAudioSourceImplementationWithCallback<QPulseAudioSourceStream, |
| 87 | QPulseAudioSource> |
| 88 | { |
| 89 | using BaseClass = QPlatformAudioSourceImplementationWithCallback<QPulseAudioSourceStream, |
| 90 | QPulseAudioSource>; |
| 91 | |
| 92 | public: |
| 93 | QPulseAudioSource(QAudioDevice, const QAudioFormat &, QObject *parent); |
| 94 | |
| 95 | void start(QIODevice *device) override; |
| 96 | void start(AudioCallback &&) override; |
| 97 | QIODevice *start() override; |
| 98 | |
| 99 | private: |
| 100 | bool validatePulseaudio(); |
| 101 | }; |
| 102 | |
| 103 | } // namespace QPulseAudioInternal |
| 104 | |
| 105 | QT_END_NAMESPACE |
| 106 | |
| 107 | #endif // QPULSEAUDIOSOURCE_P_H |
| 108 | |