2 * Copyright (C) 2017 Konsulko Group
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "mastervolume.h"
18 #include <QJsonObject>
22 MasterVolume::MasterVolume(QObject* parent)
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&)));
32 void MasterVolume::open(const QUrl& url)
38 qint32 MasterVolume::getVolume() const
43 void MasterVolume::setVolume(qint32 volume)
45 if (m_volume != volume)
49 arg.insert("control", "Master");
50 arg.insert("value", volume);
51 m_client.call("audiomixer", "volume", arg);
55 void MasterVolume::onClientConnected()
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)
71 arg.insert("event", "volume_changed");
72 m_client.call("audiomixer", "subscribe", arg);
76 void MasterVolume::onClientDisconnected()
78 qDebug() << "MasterVolume::onClientDisconnected!";
79 QTimer::singleShot(1000, this, SLOT(TryOpen()));
82 void MasterVolume::onClientError(QAbstractSocket::SocketError se)
84 qDebug() << "MasterVolume::onClientError: " << se;
87 void MasterVolume::onClientEventReceived(QString name, const QJsonValue& data)
89 qDebug() << "MasterVolume::onClientEventReceived[" << name << "]: " << data;
90 if (name == "audiomixer/volume_changed")
92 QString ctlName = data.toObject()["control"].toString();
94 if (ctlName != "Master")
97 int volume = data.toObject()["value"].toDouble() * 100;
98 volume = qBound(0, volume, 100);
99 if (m_volume != volume)
102 emit VolumeChanged();
107 void MasterVolume::TryOpen()
109 m_client.open(m_url);