1 /*
2 * Strawberry Music Player
3 * This file was part of Clementine.
4 * Copyright 2014, David Sansome <me@davidsansome.com>
5 *
6 * Strawberry is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Strawberry is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include "config.h"
22
23 #include <pulse/context.h>
24 #include <pulse/def.h>
25 #include <pulse/introspect.h>
26 #include <pulse/mainloop.h>
27 #include <pulse/proplist.h>
28
29 #include <QtGlobal>
30 #include <QVariant>
31 #include <QString>
32
33 #include "core/logging.h"
34 #include "devicefinder.h"
35 #include "pulsedevicefinder.h"
36
PulseDeviceFinder()37 PulseDeviceFinder::PulseDeviceFinder() : DeviceFinder( "pulseaudio", { "pulseaudio", "pulse", "pulsesink" }), mainloop_(nullptr), context_(nullptr) {}
38
Initialize()39 bool PulseDeviceFinder::Initialize() {
40
41 mainloop_ = pa_mainloop_new();
42 if (!mainloop_) {
43 qLog(Warning) << "Failed to create pulseaudio mainloop";
44 return false;
45 }
46
47 return Reconnect();
48 }
49
Reconnect()50 bool PulseDeviceFinder::Reconnect() {
51
52 if (context_) {
53 pa_context_disconnect(context_);
54 pa_context_unref(context_);
55 }
56
57 context_ = pa_context_new(pa_mainloop_get_api(mainloop_), "Strawberry device finder");
58 if (!context_) {
59 qLog(Warning) << "Failed to create pulseaudio context";
60 return false;
61 }
62
63 if (pa_context_connect(context_, nullptr, PA_CONTEXT_NOFLAGS, nullptr) < 0) {
64 qLog(Warning) << "Failed to connect pulseaudio context";
65 return false;
66 }
67
68 // Wait for the context to be connected.
69 forever {
70 const pa_context_state state = pa_context_get_state(context_);
71 if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED) {
72 qLog(Warning) << "Connection to pulseaudio failed";
73 return false;
74 }
75
76 if (state == PA_CONTEXT_READY) {
77 return true;
78 }
79
80 pa_mainloop_iterate(mainloop_, true, nullptr);
81 }
82 }
83
ListDevices()84 QList<DeviceFinder::Device> PulseDeviceFinder::ListDevices() {
85
86 if (!context_ || pa_context_get_state(context_) != PA_CONTEXT_READY) {
87 return QList<Device>();
88 }
89
90 retry:
91 ListDevicesState state;
92 pa_context_get_sink_info_list(context_, &PulseDeviceFinder::GetSinkInfoCallback, &state);
93
94 forever {
95 if (state.finished) {
96 return state.devices;
97 }
98
99 switch (pa_context_get_state(context_)) {
100 case PA_CONTEXT_READY:
101 break;
102 case PA_CONTEXT_FAILED:
103 case PA_CONTEXT_TERMINATED:
104 // Maybe pulseaudio died. Try reconnecting.
105 if (Reconnect()) {
106 goto retry;
107 }
108 return state.devices;
109 default:
110 return state.devices;
111 }
112
113 pa_mainloop_iterate(mainloop_, true, nullptr);
114 }
115 }
116
GetSinkInfoCallback(pa_context * c,const pa_sink_info * info,int eol,void * state_voidptr)117 void PulseDeviceFinder::GetSinkInfoCallback(pa_context *c, const pa_sink_info *info, int eol, void *state_voidptr) {
118
119 Q_UNUSED(c);
120
121 ListDevicesState *state = reinterpret_cast<ListDevicesState*>(state_voidptr);
122 if (!state) return;
123
124 if (info) {
125 Device dev;
126 dev.description = QString::fromUtf8(info->description);
127 dev.value = QString::fromUtf8(info->name);
128 dev.iconname = GuessIconName(dev.description);
129
130 state->devices.append(dev);
131 }
132
133 if (eol > 0) {
134 state->finished = true;
135 }
136 }
137
~PulseDeviceFinder()138 PulseDeviceFinder::~PulseDeviceFinder() {
139
140 if (context_) {
141 pa_context_disconnect(context_);
142 pa_context_unref(context_);
143 }
144
145 if (mainloop_) {
146 pa_mainloop_free(mainloop_);
147 }
148 }
149
150