ALSA: improve handling of default device.

- "default" is a valid argument for snd_pcm_open(), let ALSA handle that
case rather than using the first device in the list.
- Don't add "default" in the list of available devices if there is
already one.

Change-Id: Icd41aa6677923a79faf6c90d0627eedd8700b91b
Reviewed-by: Christian Stromme <christian.stromme@qt.io>
This commit is contained in:
Yoann Lopes
2016-05-03 16:01:45 +02:00
parent d76d3184b7
commit 5473621bde
4 changed files with 56 additions and 109 deletions

View File

@@ -137,35 +137,18 @@ QList<QAudioFormat::SampleType> QAlsaAudioDeviceInfo::supportedSampleTypes()
bool QAlsaAudioDeviceInfo::open() bool QAlsaAudioDeviceInfo::open()
{ {
int err = 0; int err = 0;
QString dev = device; QString dev;
QList<QByteArray> devices = availableDevices(mode);
if(dev.compare(QLatin1String("default")) == 0) { if (!availableDevices(mode).contains(device.toLocal8Bit()))
#if SND_LIB_VERSION >= 0x1000e // 1.0.14
if (devices.size() > 0)
dev = QLatin1String(devices.first().constData());
else
return false; return false;
#else
dev = QLatin1String("hw:0,0"); #if SND_LIB_VERSION < 0x1000e // 1.0.14
if (device.compare(QLatin1String("default")) != 0)
dev = deviceFromCardName(device);
else
#endif #endif
} else {
#if SND_LIB_VERSION >= 0x1000e // 1.0.14
dev = device; dev = device;
#else
int idx = 0;
char *name;
QString shortName = device.mid(device.indexOf(QLatin1String("="),0)+1);
while (snd_card_get_name(idx,&name) == 0) {
if(dev.contains(QLatin1String(name)))
break;
idx++;
}
dev = QString(QLatin1String("hw:%1,0")).arg(idx);
#endif
}
if(mode == QAudio::AudioOutput) { if(mode == QAudio::AudioOutput) {
err=snd_pcm_open( &handle,dev.toLocal8Bit().constData(),SND_PCM_STREAM_PLAYBACK,0); err=snd_pcm_open( &handle,dev.toLocal8Bit().constData(),SND_PCM_STREAM_PLAYBACK,0);
} else { } else {
@@ -194,30 +177,12 @@ bool QAlsaAudioDeviceInfo::testSettings(const QAudioFormat& format) const
snd_pcm_hw_params_t *params; snd_pcm_hw_params_t *params;
QString dev; QString dev;
#if SND_LIB_VERSION >= 0x1000e // 1.0.14 #if SND_LIB_VERSION < 0x1000e // 1.0.14
dev = device; if (device.compare(QLatin1String("default")) != 0)
if (dev.compare(QLatin1String("default")) == 0) { dev = deviceFromCardName(device);
QList<QByteArray> devices = availableDevices(QAudio::AudioOutput); else
if (!devices.isEmpty())
dev = QLatin1String(devices.first().constData());
}
#else
if (dev.compare(QLatin1String("default")) == 0) {
dev = QLatin1String("hw:0,0");
} else {
int idx = 0;
char *name;
QString shortName = device.mid(device.indexOf(QLatin1String("="),0)+1);
while(snd_card_get_name(idx,&name) == 0) {
if(shortName.compare(QLatin1String(name)) == 0)
break;
idx++;
}
dev = QString(QLatin1String("hw:%1,0")).arg(idx);
}
#endif #endif
dev = device;
snd_pcm_stream_t stream = mode == QAudio::AudioOutput snd_pcm_stream_t stream = mode == QAudio::AudioOutput
? SND_PCM_STREAM_PLAYBACK : SND_PCM_STREAM_CAPTURE; ? SND_PCM_STREAM_PLAYBACK : SND_PCM_STREAM_CAPTURE;
@@ -333,9 +298,11 @@ void QAlsaAudioDeviceInfo::updateLists()
QList<QByteArray> QAlsaAudioDeviceInfo::availableDevices(QAudio::Mode mode) QList<QByteArray> QAlsaAudioDeviceInfo::availableDevices(QAudio::Mode mode)
{ {
QList<QByteArray> devices; QList<QByteArray> devices;
QByteArray filter; bool hasDefault = false;
#if SND_LIB_VERSION >= 0x1000e // 1.0.14 #if SND_LIB_VERSION >= 0x1000e // 1.0.14
QByteArray filter;
// Create a list of all current audio devices that support mode // Create a list of all current audio devices that support mode
void **hints, **n; void **hints, **n;
char *name, *descr, *io; char *name, *descr, *io;
@@ -359,12 +326,9 @@ QList<QByteArray> QAlsaAudioDeviceInfo::availableDevices(QAudio::Mode mode)
io = snd_device_name_get_hint(*n, "IOID"); io = snd_device_name_get_hint(*n, "IOID");
if ((descr != NULL) && ((io == NULL) || (io == filter))) { if ((descr != NULL) && ((io == NULL) || (io == filter))) {
QString deviceName = QLatin1String(name); devices.append(name);
QString deviceDescription = QLatin1String(descr); if (strcmp(name, "default") == 0)
if (deviceDescription.contains(QLatin1String("Default Audio Device"))) hasDefault = true;
devices.prepend(deviceName.toLocal8Bit().constData());
else
devices.append(deviceName.toLocal8Bit().constData());
} }
free(descr); free(descr);
@@ -380,12 +344,14 @@ QList<QByteArray> QAlsaAudioDeviceInfo::availableDevices(QAudio::Mode mode)
while(snd_card_get_name(idx,&name) == 0) { while(snd_card_get_name(idx,&name) == 0) {
devices.append(name); devices.append(name);
if (strcmp(name, "default") == 0)
hasDefault = true;
idx++; idx++;
} }
#endif #endif
if (devices.size() > 0) if (!hasDefault && devices.size() > 0)
devices.append("default"); devices.prepend("default");
return devices; return devices;
} }
@@ -448,4 +414,20 @@ void QAlsaAudioDeviceInfo::checkSurround()
snd_device_name_free_hint(hints); snd_device_name_free_hint(hints);
} }
QString QAlsaAudioDeviceInfo::deviceFromCardName(const QString &card)
{
int idx = 0;
char *name;
QStringRef shortName = card.midRef(card.indexOf(QLatin1String("="), 0) + 1);
while (snd_card_get_name(idx, &name) == 0) {
if (shortName.compare(QLatin1String(name)) == 0)
break;
idx++;
}
return QString(QLatin1String("hw:%1,0")).arg(idx);
}
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@@ -85,6 +85,7 @@ public:
static QByteArray defaultInputDevice(); static QByteArray defaultInputDevice();
static QByteArray defaultOutputDevice(); static QByteArray defaultOutputDevice();
static QList<QByteArray> availableDevices(QAudio::Mode); static QList<QByteArray> availableDevices(QAudio::Mode);
static QString deviceFromCardName(const QString &card);
private: private:
bool open(); bool open();

View File

@@ -300,34 +300,16 @@ bool QAlsaAudioInput::open()
} }
QString dev = QString(QLatin1String(m_device.constData())); if (!QAlsaAudioDeviceInfo::availableDevices(QAudio::AudioOutput).contains(m_device))
QList<QByteArray> devices = QAlsaAudioDeviceInfo::availableDevices(QAudio::AudioInput);
if(dev.compare(QLatin1String("default")) == 0) {
#if SND_LIB_VERSION >= 0x1000e // 1.0.14
if (devices.size() > 0)
dev = QLatin1String(devices.first());
else
return false; return false;
#else
dev = QLatin1String("hw:0,0");
#endif
} else {
#if SND_LIB_VERSION >= 0x1000e // 1.0.14
dev = QLatin1String(m_device);
#else
int idx = 0;
char *name;
QString shortName = QLatin1String(m_device.mid(m_device.indexOf('=',0)+1).constData()); QString dev;
#if SND_LIB_VERSION < 0x1000e // 1.0.14
while(snd_card_get_name(idx,&name) == 0) { if (m_device != "default")
if(qstrncmp(shortName.toLocal8Bit().constData(),name,shortName.length()) == 0) dev = QAlsaAudioDeviceInfo::deviceFromCardName(m_device);
break; else
idx++;
}
dev = QString(QLatin1String("hw:%1,0")).arg(idx);
#endif #endif
} dev = m_device;
// Step 1: try and open the device // Step 1: try and open the device
while((count < 5) && (err < 0)) { while((count < 5) && (err < 0)) {

View File

@@ -303,34 +303,16 @@ bool QAlsaAudioOutput::open()
return false; return false;
} }
QString dev = QString(QLatin1String(m_device.constData())); if (!QAlsaAudioDeviceInfo::availableDevices(QAudio::AudioOutput).contains(m_device))
QList<QByteArray> devices = QAlsaAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
if(dev.compare(QLatin1String("default")) == 0) {
#if SND_LIB_VERSION >= 0x1000e // 1.0.14
if (devices.size() > 0)
dev = QLatin1String(devices.first());
else
return false; return false;
#else
dev = QLatin1String("hw:0,0");
#endif
} else {
#if SND_LIB_VERSION >= 0x1000e // 1.0.14
dev = QLatin1String(m_device);
#else
int idx = 0;
char *name;
QString shortName = QLatin1String(m_device.mid(m_device.indexOf('=',0)+1).constData()); QString dev;
#if SND_LIB_VERSION < 0x1000e // 1.0.14
while (snd_card_get_name(idx,&name) == 0) { if (m_device != "default")
if(qstrncmp(shortName.toLocal8Bit().constData(),name,shortName.length()) == 0) dev = QAlsaAudioDeviceInfo::deviceFromCardName(m_device);
break; else
idx++;
}
dev = QString(QLatin1String("hw:%1,0")).arg(idx);
#endif #endif
} dev = m_device;
// Step 1: try and open the device // Step 1: try and open the device
while((count < 5) && (err < 0)) { while((count < 5) && (err < 0)) {