d85a4316733b917d365aae8e6a4839cb22bf3922
[apps/mixer.git] / app / mixer.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016,2017 Konsulko Group
4  * Copyright (C) 2018 IoT.bzh
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <QJsonArray>
20 #include <QJsonObject>
21 #include <QTimer>
22 #include <QtDebug>
23 #include "mixer.hpp"
24
25 Mixer::Mixer(QObject* parent)
26         : QObject(parent)
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 Mixer::open(const QUrl& url)
35 {
36         m_url = url;
37         TryOpen();
38 }
39
40 QList<QObject*> Mixer::roles() const
41 {
42         return m_roles;
43 }
44
45 void Mixer::getRoleVolume(AudioRole* role)
46 {
47         if (role == nullptr) return;
48
49         QJsonObject arg;
50         arg.insert("action", "volume");
51         arg.insert("value", QJsonValue("+0")); // FIXME: Hack to get volume: ask for a relative change with a delta of zero
52
53         m_client.call("ahl-4a", role->Name().toLocal8Bit().data(), arg, [role](bool r, const QJsonValue& v) {
54                 if (r && v.isObject())
55                 {
56                         qDebug() << role->Name() << " Volume changed: " << v;
57                         int newVolume = v.toObject()["response"].toObject()["volnew"].toInt();
58                         role->setValue(newVolume);
59                 }
60         });
61 }
62
63 void Mixer::setRoleVolume(AudioRole* role)
64 {
65         if (role == nullptr) return;
66         role->BeginUpdate();
67
68         QJsonObject arg;
69         arg.insert("action", "volume");
70         arg.insert("value", QJsonValue(role->Value()));
71         m_client.call("ahl-4a", role->Name().toLocal8Bit().data(), arg, [role](bool r, const QJsonValue& v) {
72                 // Nothing to do, events will update sliders
73                 role->EndUpdate();
74         });
75 }
76
77 void Mixer::onClientConnected()
78 {
79         // Subscribe to 4a events
80         m_client.call("ahl-4a", "subscribe", QJsonValue(), [this](bool r, const QJsonValue& val) {
81                 if (r) qDebug() << "Mixer::onClientConnected - subscribed to 4a events!";
82                 else qCritical () << "Mixer::onClientConnected - Failed to subscribe to 4a events!";
83         });
84
85         // Call HAL to populate list
86         m_client.call("ahl-4a", "get_roles", QJsonValue(), [this](bool r, const QJsonValue& val) {
87                 if (r)
88                 {
89                         for(QObject* role : m_roles) delete role;
90                         m_roles.clear();
91
92                         QJsonArray cards = val.toObject()["response"].toArray();
93                         foreach (const QJsonValue& card, cards)
94                         {
95                                 AudioRole* ar = new AudioRole(card.toString(), 0);
96                                 getRoleVolume(reinterpret_cast<AudioRole*>(ar));
97                                 connect(ar, SIGNAL(ValueChanged()), this, SLOT(onRoleValueChanged()));
98                                 m_roles.append(ar);
99                                 qDebug() << "Mixer::onClientConnected - added this HAL: " << card.toString();
100                         }
101                         emit rolesChanged();
102                 }
103         });
104
105 }
106
107 void Mixer::onClientDisconnected()
108 {
109         qDebug() << "Mixer::onClientDisconnected!";
110         QTimer::singleShot(1000, this, SLOT(TryOpen()));
111 }
112
113 void Mixer::onClientError(QAbstractSocket::SocketError se)
114 {
115         qDebug() << "Mixer::onClientError: " << se;
116 }
117
118 void Mixer::onClientEventReceived(QString eventName, const QJsonValue& data)
119 {
120         qDebug() << "Mixer::onClientEventReceived[" << eventName << "]: " << data;
121         if (eventName == "ahl-4a/volume_changed")
122         {
123                 QString role = data.toObject()["role"].toString();
124                 int volume = data.toObject()["volume"].toInt();
125                 for(QObject* o : m_roles)
126                 {
127                         AudioRole* ar = reinterpret_cast<AudioRole*>(o);
128                         if (ar && ar->Name() == role)
129                         {
130                                 ar->setValue(volume);
131                         }
132                 }
133         }
134 }
135
136 void Mixer::onRoleValueChanged()
137 {
138         AudioRole* role = reinterpret_cast<AudioRole*>(QObject::sender());
139         if (role == nullptr) return;
140         setRoleVolume(role);
141 }
142
143 void Mixer::TryOpen()
144 {
145         qDebug() << "Mixer::TryOpen: " << m_url;
146         m_client.open(m_url);
147 }