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

111 lines
3.3 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
2021-03-20 21:14:47 +01:00
* Copyright 2017-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* 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/>.
*
*/
#include "config.h"
2020-06-14 23:54:18 +02:00
#include <cstdio>
#include <cerrno>
2018-02-27 18:06:05 +01:00
#include <alsa/asoundlib.h>
#include <QString>
#include <QScopeGuard>
2018-02-27 18:06:05 +01:00
#include <core/logging.h>
#include "alsadevicefinder.h"
2023-06-01 19:31:19 +02:00
#include "enginedevice.h"
AlsaDeviceFinder::AlsaDeviceFinder() : DeviceFinder(QStringLiteral("alsa"), { QStringLiteral("alsa"), QStringLiteral("alsasink") }) {}
2018-02-27 18:06:05 +01:00
2023-06-01 19:31:19 +02:00
EngineDeviceList AlsaDeviceFinder::ListDevices() {
2018-02-27 18:06:05 +01:00
2023-06-01 19:31:19 +02:00
EngineDeviceList devices;
2018-02-27 18:06:05 +01:00
snd_pcm_stream_name(SND_PCM_STREAM_PLAYBACK);
2018-06-12 00:26:08 +02:00
int card = -1;
2020-10-17 17:29:09 +02:00
snd_ctl_card_info_t *cardinfo = nullptr;
2018-06-12 00:26:08 +02:00
snd_ctl_card_info_alloca(&cardinfo);
2018-02-27 18:06:05 +01:00
while (true) {
2019-04-08 18:46:11 +02:00
int result = snd_card_next(&card);
2018-02-27 18:06:05 +01:00
if (result < 0) {
qLog(Error) << "Unable to get soundcard:" << snd_strerror(result);
2018-06-07 19:38:40 +02:00
break;
2018-02-27 18:06:05 +01:00
}
2018-06-07 19:38:40 +02:00
if (card < 0) break;
2018-02-27 18:06:05 +01:00
2018-06-12 00:26:08 +02:00
char str[32];
2018-06-17 15:07:11 +02:00
snprintf(str, sizeof(str) - 1, "hw:%d", card);
2020-10-17 17:29:09 +02:00
snd_ctl_t *handle = nullptr;
2018-06-12 00:26:08 +02:00
result = snd_ctl_open(&handle, str, 0);
2018-02-27 18:06:05 +01:00
if (result < 0) {
qLog(Error) << "Unable to open soundcard" << card << ":" << snd_strerror(result);
2018-02-27 18:06:05 +01:00
continue;
}
const QScopeGuard snd_ctl_handle_close = qScopeGuard([&handle]() { snd_ctl_close(handle); });
2018-06-17 15:07:11 +02:00
2018-02-27 18:06:05 +01:00
result = snd_ctl_card_info(handle, cardinfo);
if (result < 0) {
qLog(Error) << "Control hardware failure for card" << card << ":" << snd_strerror(result);
2018-02-27 18:06:05 +01:00
continue;
}
2018-06-12 00:26:08 +02:00
int dev = -1;
2020-10-17 17:29:09 +02:00
snd_pcm_info_t *pcminfo = nullptr;
2018-06-12 00:26:08 +02:00
snd_pcm_info_alloca(&pcminfo);
2018-02-27 18:06:05 +01:00
while (true) {
result = snd_ctl_pcm_next_device(handle, &dev);
if (result < 0) {
2021-06-21 16:08:53 +02:00
qLog(Error) << "Unable to get PCM for card" << card << ":" << snd_strerror(result);
continue;
2018-02-27 18:06:05 +01:00
}
if (dev < 0) break;
snd_pcm_info_set_device(pcminfo, dev);
snd_pcm_info_set_subdevice(pcminfo, 0);
snd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_PLAYBACK);
2018-02-27 18:06:05 +01:00
result = snd_ctl_pcm_info(handle, pcminfo);
if (result < 0) {
if (result != -ENOENT) qLog(Error) << "Unable to get control digital audio info for card" << card << ":" << snd_strerror(result);
2018-02-27 18:06:05 +01:00
continue;
}
2023-06-01 19:31:19 +02:00
EngineDevice device;
device.description = QStringLiteral("%1 %2").arg(QString::fromUtf8(snd_ctl_card_info_get_name(cardinfo)), QString::fromUtf8(snd_pcm_info_get_name(pcminfo)));
2023-06-01 19:31:19 +02:00
device.iconname = device.GuessIconName();
device.card = card;
device.device = dev;
2024-04-09 23:20:26 +02:00
device.value = QStringLiteral("hw:%1,%2").arg(card).arg(dev);
2023-06-01 19:31:19 +02:00
devices.append(device);
2024-04-09 23:20:26 +02:00
device.value = QStringLiteral("plughw:%1,%2").arg(card).arg(dev);
2023-06-01 19:31:19 +02:00
devices.append(device);
2018-02-27 18:06:05 +01:00
}
}
snd_config_update_free_global();
2023-06-01 19:31:19 +02:00
return devices;
2018-02-27 18:06:05 +01:00
}