KPipewire

mediamonitor.h
1/*
2 SPDX-FileCopyrightText: 2023 Fushan Wen <qydwhotmail@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <QAbstractListModel>
10#include <QQmlParserStatus>
11#include <QSharedPointer>
12#include <QTimer>
13#include <qqmlregistration.h>
14
15#include <pipewire/pipewire.h>
16
17class PipeWireCore;
18
19namespace MediaRole
20{
21Q_NAMESPACE
22QML_ELEMENT
23// Match PW_KEY_MEDIA_ROLE
24enum Role : int {
25 Unknown = -1,
26 Movie,
27 Music,
28 Camera,
29 Screen,
30 Communication,
31 Game,
32 Notification,
33 DSP,
34 Production,
35 Accessibility,
36 Test,
37 Last = Test,
38};
39Q_ENUM_NS(Role)
40}
41
42namespace NodeState
43{
44Q_NAMESPACE
45QML_ELEMENT
46// Match enum pw_node_state
47enum State : int {
48 Error = -1, /**< error state */
49 Creating = 0, /**< the node is being created */
50 Suspended = 1, /**< the node is suspended, the device might be closed */
51 Idle = 2, /**< the node is running but there is no active port */
52 Running = 3, /**< the node is running */
53};
54Q_ENUM_NS(State)
55}
56
57class MediaMonitor : public QAbstractListModel, public QQmlParserStatus
58{
61 QML_ELEMENT
62
63 /**
64 * Role for media streams. Only media streams with their @p PW_KEY_MEDIA_ROLE matching @p role will be monitored.
65 * @default MediaRole::Unknown
66 */
67 Q_PROPERTY(MediaRole::Role role READ role WRITE setRole NOTIFY roleChanged)
68
69 /**
70 * Whether this monitor is able to detect apps using Pipewire for media access
71 */
72 Q_PROPERTY(bool detectionAvailable READ detectionAvailable NOTIFY detectionAvailableChanged)
73
74 /**
75 * Total count of media streams with the given role on the system
76 */
77 Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
78
79 /**
80 * The number of media streams that are currently used by applications
81 */
82 Q_PROPERTY(int runningCount READ runningCount NOTIFY runningCountChanged)
83
84 /**
85 * The number of media streams that are in idle state.
86 */
87 Q_PROPERTY(int idleCount READ idleCount NOTIFY idleCountChanged)
88
89public:
90 enum Role {
91 StateRole = Qt::UserRole + 1,
92 ObjectSerialRole,
93 };
94 Q_ENUM(Role);
95
96 explicit MediaMonitor(QObject *parent = nullptr);
97 ~MediaMonitor() override;
98
99 QVariant data(const QModelIndex &index, int role) const override;
100 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
101 QHash<int, QByteArray> roleNames() const override;
102
103 MediaRole::Role role() const;
104 void setRole(MediaRole::Role newRole);
105
106 bool detectionAvailable() const;
107 int runningCount() const;
108 int idleCount() const;
109
111 void roleChanged();
112 void detectionAvailableChanged();
113 void countChanged();
114 void runningCountChanged();
115 void idleCountChanged();
116
117private Q_SLOTS:
118 void connectToCore();
119 void onPipeBroken();
120
121private:
122 struct ProxyDeleter {
123 void operator()(pw_proxy *proxy) const
124 {
125 MediaMonitor::onProxyDestroy(pw_proxy_get_user_data(proxy));
126 pw_proxy_destroy(proxy);
127 }
128 };
129
130 static void onRegistryEventGlobal(void *data, uint32_t id, uint32_t permissions, const char *type, uint32_t version, const spa_dict *props);
131 static void onRegistryEventGlobalRemove(void *data, uint32_t id);
132 static void onProxyDestroy(void *data);
133 static void onNodeEventInfo(void *data, const pw_node_info *info);
134
135 static void readProps(const spa_dict *props, pw_proxy *proxy, bool emitSignal);
136
137 void classBegin() override;
138 void componentComplete() override;
139
140 void disconnectFromCore();
141 void reconnectOnIdle();
142 void updateState();
143
144 static pw_registry_events s_pwRegistryEvents;
145 static pw_proxy_events s_pwProxyEvents;
146 static pw_node_events s_pwNodeEvents;
147
148 bool m_componentReady = false;
149 MediaRole::Role m_role = MediaRole::Unknown;
150 bool m_detectionAvailable = false;
151 int m_runningCount = 0;
152 int m_idleCount = 0;
153
155 pw_registry *m_registry = nullptr;
156 spa_hook m_registryListener;
157 std::vector<std::unique_ptr<pw_proxy, ProxyDeleter>> m_nodeList;
158 QTimer m_reconnectTimer;
159
160 bool m_inDestructor = false;
161};
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
Q_INTERFACES(...)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
UserRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:17 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.