File indexing completed on 2024-04-14 15:40:19

0001 /*
0002     SPDX-FileCopyrightText: 2020 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QObject>
0010 #include <QPointer>
0011 #include <QQmlParserStatus>
0012 
0013 struct pa_stream;
0014 
0015 namespace QPulseAudio
0016 {
0017 class VolumeObject;
0018 
0019 /**
0020  * This class provides a way to see the "peak" volume currently playing of any VolumeObject
0021  */
0022 class VolumeMonitor : public QObject
0023 {
0024     Q_OBJECT
0025     /**
0026      * Object to monitor the volume of
0027      * This is the "PulseObject" role of any SinkInput, Sink or Output model
0028      * Setting to null will stop streaming
0029      */
0030     Q_PROPERTY(QPulseAudio::VolumeObject *target READ target WRITE setTarget NOTIFY targetChanged)
0031     /**
0032      * The peak output for the volume at any given moment
0033      * Value is normalised between 0 and 1
0034      */
0035     Q_PROPERTY(qreal volume MEMBER m_volume NOTIFY volumeChanged)
0036 
0037     /**
0038      * Whether monitoring is available
0039      */
0040     Q_PROPERTY(bool available READ isAvailable NOTIFY availableChanged)
0041 
0042 public:
0043     VolumeMonitor(QObject *parent = nullptr);
0044     ~VolumeMonitor();
0045 
0046     bool isAvailable() const;
0047 
0048     VolumeObject *target() const;
0049     void setTarget(VolumeObject *target);
0050 
0051 Q_SIGNALS:
0052     void volumeChanged();
0053     void targetChanged();
0054     void availableChanged();
0055 
0056 private:
0057     void createStream();
0058     void updateVolume(qreal volume);
0059     static void read_callback(pa_stream *s, size_t length, void *userdata);
0060     static void suspended_callback(pa_stream *s, void *userdata);
0061 
0062     VolumeObject *m_target = nullptr;
0063     pa_stream *m_stream = nullptr;
0064 
0065     qreal m_volume = 0;
0066 };
0067 
0068 }