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"
21 #define MASTER_CONTROL "Master Playback"
23 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&)));
36 void MasterVolume::open(const QUrl& url)
41 qint32 MasterVolume::getVolume() const
46 void MasterVolume::setVolume(qint32 volume)
48 if (m_volume != volume)
53 arg.insert("control", MASTER_CONTROL);
54 double v = (double) volume / 100.0;
55 arg.insert("value", v);
56 m_client.call("audiomixer", "volume", arg);
63 void MasterVolume::onClientConnected()
67 arg.insert("control", MASTER_CONTROL);
68 m_client.call("audiomixer", "volume", arg, [this](bool r, const QJsonValue& v) {
69 if (r && v.isObject()) {
70 int volume = v.toObject()["response"].toObject()["volume"].toDouble() * 100;
71 volume = qBound(0, volume, 100);
72 if (m_volume != volume)
80 arg.insert("event", "volume_changed");
81 m_client.call("audiomixer", "subscribe", arg);
85 void MasterVolume::onClientDisconnected()
87 qDebug() << "MasterVolume::onClientDisconnected!";
88 QTimer::singleShot(1000, this, SLOT(TryOpen()));
91 void MasterVolume::onClientError(QAbstractSocket::SocketError se)
93 qDebug() << "MasterVolume::onClientError: " << se;
96 void MasterVolume::onClientEventReceived(QString name, const QJsonValue& data)
98 qDebug() << "MasterVolume::onClientEventReceived[" << name << "]: " << data;
99 if (name == "audiomixer/volume_changed")
101 QString ctlName = data.toObject()["control"].toString();
103 if (ctlName != MASTER_CONTROL)
106 int volume = data.toObject()["value"].toDouble() * 100;
107 volume = qBound(0, volume, 100);
108 if (m_volume != volume)
111 emit VolumeChanged();