Rewrite PulseAudio backend into a threaded class
[apps/mixer.git] / app / paclient.cpp
1 /*
2  * Copyright (C) 2016,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 "paclient.h"
18
19 #include <QtCore/QDebug>
20
21 PaClient::PaClient()
22         : m_init(false), m_ml(nullptr), m_mlapi(nullptr), m_ctx(nullptr)
23 {
24 }
25
26 PaClient::~PaClient()
27 {
28         if (m_init)
29                 close();
30 }
31
32 void PaClient::close()
33 {
34         if (!m_init) return;
35         pa_threaded_mainloop_stop(m_ml);
36         pa_threaded_mainloop_free(m_ml);
37         m_init = false;
38 }
39
40 static void set_sink_volume_cb(pa_context *c, int success, void *data __attribute__((unused)))
41 {
42         if (!success)
43                 qWarning() << "PaClient: set sink volume: " <<
44                         pa_strerror(pa_context_errno(c));
45 }
46
47 static void set_source_volume_cb(pa_context *c, int success, void *data __attribute__((unused)))
48 {
49         if (!success)
50                 qWarning() << "PaClient: set source volume: " <<
51                         pa_strerror(pa_context_errno(c));
52 }
53
54 void PaClient::setVolume(uint32_t type, uint32_t index, uint32_t channel, uint32_t volume)
55 {
56         pa_operation *o;
57         pa_context *c = context();
58         CState *cstate = NULL;
59
60         foreach (cstate, m_cstatelist)
61                 if ((cstate->index == index) && (cstate->type == type))
62                         break;
63
64         cstate->cvolume.values[channel] = volume;
65
66         if (type == C_SINK) {
67                 if (!(o = pa_context_set_sink_volume_by_index(c, index, &cstate->cvolume, set_sink_volume_cb, NULL))) {
68                         qWarning() << "PaClient: set sink #" << index <<
69                                 " channel #" << channel <<
70                                 " volume: " << pa_strerror(pa_context_errno(c));
71                         return;
72                 }
73                 pa_operation_unref(o);
74         } else if (type == C_SOURCE) {
75                 if (!(o = pa_context_set_source_volume_by_index(c, index, &cstate->cvolume, set_source_volume_cb, NULL))) {
76                         qWarning() << "PaClient: set source #" << index <<
77                                 " channel #" << channel <<
78                                 " volume: " << pa_strerror(pa_context_errno(c));
79                         return;
80                 }
81                 pa_operation_unref(o);
82         }
83 }
84
85 void get_source_list_cb(pa_context *c,
86                 const pa_source_info *i,
87                 int eol,
88                 void *data)
89 {
90         int chan;
91
92         PaClient *self = reinterpret_cast<PaClient*>(data);
93
94         if (eol < 0) {
95                 qWarning() << "PaClient: get source list: " <<
96                         pa_strerror(pa_context_errno(c));
97
98                 self->close();
99                 return;
100         }
101
102         if (!eol) {
103                 self->addOneControlState(C_SOURCE, i->index, &i->volume);
104                 for (chan = 0; chan < i->channel_map.channels; chan++) {
105                         emit self->controlAdded(i->index, QString(i->description), C_SOURCE, chan,
106                                                 channel_position_string[i->channel_map.map[chan]],
107                                                 i->volume.values[chan]);
108                 }
109         }
110 }
111
112 void get_sink_list_cb(pa_context *c,
113                 const pa_sink_info *i,
114                 int eol,
115                 void *data)
116 {
117         int chan;
118         PaClient *self = reinterpret_cast<PaClient*>(data);
119
120         if(eol < 0) {
121                 qWarning() << "PaClient: get sink list: " <<
122                         pa_strerror(pa_context_errno(c));
123                 self->close();
124                 return;
125         }
126
127         if(!eol) {
128                 self->addOneControlState(C_SINK, i->index, &i->volume);
129                 for (chan = 0; chan < i->channel_map.channels; chan++) {
130                         emit self->controlAdded(i->index, QString(i->description), C_SINK, chan,
131                                                  channel_position_string[i->channel_map.map[chan]],
132                                                  i->volume.values[chan]);
133                 }
134         }
135 }
136
137 void context_state_cb(pa_context *c, void *data)
138 {
139         pa_operation *o;
140         PaClient *self = reinterpret_cast<PaClient*>(data);
141
142         switch (pa_context_get_state(c)) {
143                 case PA_CONTEXT_CONNECTING:
144                 case PA_CONTEXT_AUTHORIZING:
145                 case PA_CONTEXT_SETTING_NAME:
146                         break;
147                 case PA_CONTEXT_READY:
148                         // Fetch the controls of interest
149                         if (!(o = pa_context_get_source_info_list(c, get_source_list_cb, data))) {
150                                 qWarning() << "PaClient: get source info list: " <<
151                                         pa_strerror(pa_context_errno(c));
152                                 return;
153                         }
154                         pa_operation_unref(o);
155                         if (!(o = pa_context_get_sink_info_list(c, &get_sink_list_cb, data))) {
156                                 qWarning() << "PaClient: get sink info list: " <<
157                                         pa_strerror(pa_context_errno(c));
158                                 return;
159                         }
160                         break;
161                 case PA_CONTEXT_TERMINATED:
162                         self->close();
163                         break;
164
165                 case PA_CONTEXT_FAILED:
166                 default:
167                         qCritical() << "PaClient: connection failed: " <<
168                                 pa_strerror(pa_context_errno(c));
169                         self->close();
170                         break;
171         }
172 }
173
174 void PaClient::init()
175 {
176         m_ml = pa_threaded_mainloop_new();
177         if (!m_ml) {
178                 qCritical("PaClient: failed to create mainloop");
179                 return;
180         }
181
182         pa_threaded_mainloop_set_name(m_ml, "PaClient mainloop");
183
184         m_mlapi = pa_threaded_mainloop_get_api(m_ml);
185
186         lock();
187
188         m_ctx = pa_context_new(m_mlapi, "Mixer");
189         if (!m_ctx) {
190                 qCritical("PaClient: failed to create context");
191                 pa_threaded_mainloop_free(m_ml);
192                 return;
193         }
194         pa_context_set_state_callback(m_ctx, context_state_cb, this);
195
196         if (pa_context_connect(m_ctx, 0, (pa_context_flags_t)0, 0) < 0) {
197                 qCritical("PaClient: failed to connect");
198                 pa_context_unref(m_ctx);
199                 pa_threaded_mainloop_free(m_ml);
200                 return;
201         }
202
203         if (pa_threaded_mainloop_start(m_ml) != 0) {
204                 qCritical("PaClient: failed to start mainloop");
205                 pa_context_unref(m_ctx);
206                 pa_threaded_mainloop_free(m_ml);
207                 return;
208         }
209
210         unlock();
211
212         m_init = true;
213 }
214
215 void PaClient::addOneControlState(int type, int index, const pa_cvolume *cvolume)
216 {
217         int i;
218         CState *cstate;
219
220         cstate = new CState;
221         cstate->type = type;
222         cstate->index = index;
223         cstate->cvolume.channels = cvolume->channels;
224         for (i = 0; i < cvolume->channels; i++)
225                 cstate->cvolume.values[i] = cvolume->values[i];
226
227         m_cstatelist.append(cstate);
228 }