43beaefeba60cf46a3d1d2e0ae657c74038998a7
[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 #define MASTER_CONTROL "Master Playback"
23
24 MasterVolume::MasterVolume(QObject* parent)
25         : QObject(parent)
26         , m_volume{50}
27 {
28         connect(&m_client, SIGNAL(connected()), this, SLOT(onClientConnected()));
29         connect(&m_client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
30         connect(&m_client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onClientError(QAbstractSocket::SocketError)));
31         connect(&m_client, SIGNAL(eventReceived(QString, const QJsonValue&)), this, SLOT(onClientEventReceived(QString, const QJsonValue&)));
32 }
33
34 void MasterVolume::open(const QUrl& url)
35 {
36         m_url = url;
37         TryOpen();
38 }
39
40 qint32 MasterVolume::getVolume() const
41 {
42         return m_volume;
43 }
44
45 void MasterVolume::setVolume(qint32 volume)
46 {
47         if (m_volume != volume)
48         {
49                 m_volume = volume;
50                 QJsonObject arg;
51                 arg.insert("control", MASTER_CONTROL);
52                 double v = (double) volume / 100.0;
53                 arg.insert("value", v);
54                 m_client.call("audiomixer", "volume", arg);
55         }
56 }
57
58 void MasterVolume::onClientConnected()
59 {
60         QJsonObject arg;
61         arg.insert("control", MASTER_CONTROL);
62         m_client.call("audiomixer", "volume", arg, [this](bool r, const QJsonValue& v) {
63                 if (r && v.isObject()) {
64                         int volume = v.toObject()["response"].toObject()["volume"].toDouble() * 100;
65                         volume = qBound(0, volume, 100);
66                         if (m_volume != volume)
67                         {
68                                 m_volume = volume;
69                                 emit VolumeChanged();
70                         }
71                 }
72
73                 QJsonObject arg;
74                 arg.insert("event", "volume_changed");
75                 m_client.call("audiomixer", "subscribe", arg);
76         });
77 }
78
79 void MasterVolume::onClientDisconnected()
80 {
81         qDebug() << "MasterVolume::onClientDisconnected!";
82         QTimer::singleShot(1000, this, SLOT(TryOpen()));
83 }
84
85 void MasterVolume::onClientError(QAbstractSocket::SocketError se)
86 {
87         qDebug() << "MasterVolume::onClientError: " << se;
88 }
89
90 void MasterVolume::onClientEventReceived(QString name, const QJsonValue& data)
91 {
92         qDebug() << "MasterVolume::onClientEventReceived[" << name << "]: " << data;
93         if (name == "audiomixer/volume_changed")
94         {
95                 QString ctlName = data.toObject()["control"].toString();
96
97                 if (ctlName != MASTER_CONTROL)
98                         return;
99
100                 int volume = data.toObject()["value"].toDouble() * 100;
101                 volume = qBound(0, volume, 100);
102                 if (m_volume != volume)
103                 {
104                         m_volume = volume;
105                         emit VolumeChanged();
106                 }
107         }
108 }
109
110 void MasterVolume::TryOpen()
111 {
112         m_client.open(m_url);
113 }