strawberry-audio-player-win.../src/engine/pulsedevicefinder.cpp

149 lines
3.7 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2014, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
#include <pulse/context.h>
#include <pulse/def.h>
2018-02-27 18:06:05 +01:00
#include <pulse/introspect.h>
#include <pulse/mainloop.h>
#include <QtGlobal>
#include <QVariant>
#include <QString>
2018-02-27 18:06:05 +01:00
#include "core/logging.h"
#include "pulsedevicefinder.h"
2023-06-01 19:31:19 +02:00
#include "enginedevice.h"
2018-02-27 18:06:05 +01:00
2024-04-09 23:20:26 +02:00
PulseDeviceFinder::PulseDeviceFinder() : DeviceFinder(QStringLiteral("pulseaudio"), { "pulseaudio", "pulse", "pulsesink" }), mainloop_(nullptr), context_(nullptr) {}
2018-02-27 18:06:05 +01:00
2020-10-17 17:29:09 +02:00
bool PulseDeviceFinder::Initialize() {
2018-02-27 18:06:05 +01:00
mainloop_ = pa_mainloop_new();
if (!mainloop_) {
qLog(Warning) << "Failed to create pulseaudio mainloop";
return false;
}
return Reconnect();
}
bool PulseDeviceFinder::Reconnect() {
if (context_) {
pa_context_disconnect(context_);
pa_context_unref(context_);
}
context_ = pa_context_new(pa_mainloop_get_api(mainloop_), "Strawberry device finder");
if (!context_) {
qLog(Warning) << "Failed to create pulseaudio context";
return false;
}
if (pa_context_connect(context_, nullptr, PA_CONTEXT_NOFLAGS, nullptr) < 0) {
qLog(Warning) << "Failed to connect pulseaudio context";
return false;
}
// Wait for the context to be connected.
forever {
const pa_context_state state = pa_context_get_state(context_);
if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED) {
qLog(Warning) << "Connection to pulseaudio failed";
return false;
}
if (state == PA_CONTEXT_READY) {
return true;
}
pa_mainloop_iterate(mainloop_, true, nullptr);
}
}
2023-06-01 19:31:19 +02:00
EngineDeviceList PulseDeviceFinder::ListDevices() {
2018-02-27 18:06:05 +01:00
if (!context_ || pa_context_get_state(context_) != PA_CONTEXT_READY) {
2023-06-01 19:31:19 +02:00
return EngineDeviceList();
2018-02-27 18:06:05 +01:00
}
retry:
ListDevicesState state;
pa_context_get_sink_info_list(context_, &PulseDeviceFinder::GetSinkInfoCallback, &state);
forever {
if (state.finished) {
return state.devices;
}
switch (pa_context_get_state(context_)) {
2022-03-22 21:09:05 +01:00
case PA_CONTEXT_READY:
break;
case PA_CONTEXT_FAILED:
case PA_CONTEXT_TERMINATED:
// Maybe pulseaudio died. Try reconnecting.
if (Reconnect()) {
goto retry;
}
return state.devices;
default:
return state.devices;
2018-02-27 18:06:05 +01:00
}
pa_mainloop_iterate(mainloop_, true, nullptr);
}
}
void PulseDeviceFinder::GetSinkInfoCallback(pa_context *c, const pa_sink_info *info, int eol, void *state_voidptr) {
2019-09-15 20:27:32 +02:00
Q_UNUSED(c);
2018-02-27 18:06:05 +01:00
ListDevicesState *state = reinterpret_cast<ListDevicesState*>(state_voidptr);
2019-09-15 20:27:32 +02:00
if (!state) return;
2018-02-27 18:06:05 +01:00
if (info) {
2023-06-01 19:31:19 +02:00
EngineDevice device;
device.description = QString::fromUtf8(info->description);
device.value = QString::fromUtf8(info->name);
device.iconname = device.GuessIconName();
2018-02-27 18:06:05 +01:00
2023-06-01 19:31:19 +02:00
state->devices.append(device);
2018-02-27 18:06:05 +01:00
}
2021-06-22 13:54:58 +02:00
if (eol > 0) {
2018-02-27 18:06:05 +01:00
state->finished = true;
}
}
PulseDeviceFinder::~PulseDeviceFinder() {
if (context_) {
pa_context_disconnect(context_);
pa_context_unref(context_);
}
if (mainloop_) {
pa_mainloop_free(mainloop_);
}
}