2 * Copyright (C) 2016,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.
19 #include <QtCore/QDebug>
22 : m_init(false), m_ml(nullptr), m_mlapi(nullptr), m_ctx(nullptr)
32 void PaClient::close()
35 pa_threaded_mainloop_stop(m_ml);
36 pa_threaded_mainloop_free(m_ml);
40 static void set_sink_volume_cb(pa_context *c, int success, void *data __attribute__((unused)))
43 qWarning() << "PaClient: set sink volume: " <<
44 pa_strerror(pa_context_errno(c));
47 static void set_source_volume_cb(pa_context *c, int success, void *data __attribute__((unused)))
50 qWarning() << "PaClient: set source volume: " <<
51 pa_strerror(pa_context_errno(c));
54 void PaClient::setVolume(uint32_t type, uint32_t index, uint32_t channel, uint32_t volume)
57 pa_context *c = context();
58 CState *cstate = NULL;
60 foreach (cstate, m_cstatelist)
61 if ((cstate->index == index) && (cstate->type == type))
64 cstate->cvolume.values[channel] = volume;
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));
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));
81 pa_operation_unref(o);
85 void get_source_list_cb(pa_context *c,
86 const pa_source_info *i,
92 PaClient *self = reinterpret_cast<PaClient*>(data);
95 qWarning() << "PaClient: get source list: " <<
96 pa_strerror(pa_context_errno(c));
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]);
112 void get_sink_list_cb(pa_context *c,
113 const pa_sink_info *i,
118 PaClient *self = reinterpret_cast<PaClient*>(data);
121 qWarning() << "PaClient: get sink list: " <<
122 pa_strerror(pa_context_errno(c));
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]);
137 void context_state_cb(pa_context *c, void *data)
140 PaClient *self = reinterpret_cast<PaClient*>(data);
142 switch (pa_context_get_state(c)) {
143 case PA_CONTEXT_CONNECTING:
144 case PA_CONTEXT_AUTHORIZING:
145 case PA_CONTEXT_SETTING_NAME:
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));
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));
161 case PA_CONTEXT_TERMINATED:
165 case PA_CONTEXT_FAILED:
167 qCritical() << "PaClient: connection failed: " <<
168 pa_strerror(pa_context_errno(c));
174 void PaClient::init()
176 m_ml = pa_threaded_mainloop_new();
178 qCritical("PaClient: failed to create mainloop");
182 pa_threaded_mainloop_set_name(m_ml, "PaClient mainloop");
184 m_mlapi = pa_threaded_mainloop_get_api(m_ml);
188 m_ctx = pa_context_new(m_mlapi, "Mixer");
190 qCritical("PaClient: failed to create context");
191 pa_threaded_mainloop_free(m_ml);
194 pa_context_set_state_callback(m_ctx, context_state_cb, this);
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);
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);
215 void PaClient::addOneControlState(int type, int index, const pa_cvolume *cvolume)
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];
227 m_cstatelist.append(cstate);