mastervolume: port to use the pipewire audiomixer service
[apps/homescreen.git] / homescreen / src / mastervolume.cpp
1 /*
2  * Copyright (C) 2017 Konsulko Group
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "mastervolume.h"
18 #include <QJsonObject>
19 #include <QTimer>
20 #include <QtDebug>
21
22 MasterVolume::MasterVolume(QObject* parent)
23         : QObject(parent)
24         , m_volume{50}
25 {
26         connect(&m_client, SIGNAL(connected()), this, SLOT(onClientConnected()));
27         connect(&m_client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
28         connect(&m_client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onClientError(QAbstractSocket::SocketError)));
29         connect(&m_client, SIGNAL(eventReceived(QString, const QJsonValue&)), this, SLOT(onClientEventReceived(QString, const QJsonValue&)));
30 }
31
32 void MasterVolume::open(const QUrl& url)
33 {
34         m_url = url;
35         TryOpen();
36 }
37
38 qint32 MasterVolume::getVolume() const
39 {
40         return m_volume;
41 }
42
43 void MasterVolume::setVolume(qint32 volume)
44 {
45         if (m_volume != volume)
46         {
47                 m_volume = volume;
48                 QJsonObject arg;
49                 arg.insert("control", "Master");
50                 arg.insert("value", volume);
51                 m_client.call("audiomixer", "volume", arg);
52         }
53 }
54
55 void MasterVolume::onClientConnected()
56 {
57         QJsonObject arg;
58         arg.insert("control", "Master");
59         m_client.call("audiomixer", "volume", arg, [this](bool r, const QJsonValue& v) {
60                 if (r && v.isObject()) {
61                         int volume = v.toObject()["response"].toObject()["volume"].toDouble() * 100;
62                         volume = qBound(0, volume, 100);
63                         if (m_volume != volume)
64                         {
65                                 m_volume = volume;
66                                 emit VolumeChanged();
67                         }
68                 }
69
70                 QJsonObject arg;
71                 arg.insert("event", "volume_changed");
72                 m_client.call("audiomixer", "subscribe", arg);
73         });
74 }
75
76 void MasterVolume::onClientDisconnected()
77 {
78         qDebug() << "MasterVolume::onClientDisconnected!";
79         QTimer::singleShot(1000, this, SLOT(TryOpen()));
80 }
81
82 void MasterVolume::onClientError(QAbstractSocket::SocketError se)
83 {
84         qDebug() << "MasterVolume::onClientError: " << se;
85 }
86
87 void MasterVolume::onClientEventReceived(QString name, const QJsonValue& data)
88 {
89         qDebug() << "MasterVolume::onClientEventReceived[" << name << "]: " << data;
90         if (name == "audiomixer/volume_changed")
91         {
92                 QString ctlName = data.toObject()["control"].toString();
93
94                 if (ctlName != "Master")
95                         return;
96
97                 int volume = data.toObject()["value"].toDouble() * 100;
98                 volume = qBound(0, volume, 100);
99                 if (m_volume != volume)
100                 {
101                         m_volume = volume;
102                         emit VolumeChanged();
103                 }
104         }
105 }
106
107 void MasterVolume::TryOpen()
108 {
109         m_client.open(m_url);
110 }