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 #define MASTER_CONTROL "Master Playback"
24 MasterVolume::MasterVolume(QObject* parent)
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&)));
34 void MasterVolume::open(const QUrl& url)
40 qint32 MasterVolume::getVolume() const
45 void MasterVolume::setVolume(qint32 volume)
47 if (m_volume != volume)
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);
58 void MasterVolume::onClientConnected()
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)
74 arg.insert("event", "volume_changed");
75 m_client.call("audiomixer", "subscribe", arg);
79 void MasterVolume::onClientDisconnected()
81 qDebug() << "MasterVolume::onClientDisconnected!";
82 QTimer::singleShot(1000, this, SLOT(TryOpen()));
85 void MasterVolume::onClientError(QAbstractSocket::SocketError se)
87 qDebug() << "MasterVolume::onClientError: " << se;
90 void MasterVolume::onClientEventReceived(QString name, const QJsonValue& data)
92 qDebug() << "MasterVolume::onClientEventReceived[" << name << "]: " << data;
93 if (name == "audiomixer/volume_changed")
95 QString ctlName = data.toObject()["control"].toString();
97 if (ctlName != MASTER_CONTROL)
100 int volume = data.toObject()["value"].toDouble() * 100;
101 volume = qBound(0, volume, 100);
102 if (m_volume != volume)
105 emit VolumeChanged();
110 void MasterVolume::TryOpen()
112 m_client.open(m_url);