mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-08-04 16:22:52 +08:00
fix: Fixes a crash when no audio device is connected
This commit is contained in:
committed by
Lars Toenning
parent
87528d9086
commit
d8ce95e3f1
@@ -96,6 +96,21 @@ namespace swift::core::afv::audio
|
||||
inputFormat.setSampleFormat(QAudioFormat::Int16);
|
||||
|
||||
QAudioDevice selectedDevice = getLowestLatencyDevice(inputDevice, inputFormat);
|
||||
|
||||
if (selectedDevice.isNull())
|
||||
{
|
||||
CLogMessage(this).error(u"No suitable audio input device found for '%1'. Required format: %2")
|
||||
<< inputDevice.getName() << toQString(inputFormat);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!selectedDevice.isFormatSupported(inputFormat))
|
||||
{
|
||||
CLogMessage(this).error(u"Selected device '%1' does not support required format: %2")
|
||||
<< selectedDevice.description() << toQString(inputFormat);
|
||||
return;
|
||||
}
|
||||
|
||||
m_inputFormat = inputFormat;
|
||||
m_audioInput.reset(new QAudioSource(selectedDevice, m_inputFormat));
|
||||
if (!m_audioInputBuffer) { m_audioInputBuffer = new CAudioInputBuffer(this); }
|
||||
|
||||
@@ -73,7 +73,50 @@ namespace swift::sound
|
||||
{
|
||||
const QAudioDevice defDevice =
|
||||
device.isInputDevice() ? QMediaDevices::defaultAudioInput() : QMediaDevices::defaultAudioOutput();
|
||||
Q_ASSERT_X(defDevice.isFormatSupported(format), Q_FUNC_INFO, "Device does not support format");
|
||||
|
||||
if (defDevice.isNull())
|
||||
{
|
||||
// No default device available
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!defDevice.isFormatSupported(format))
|
||||
{
|
||||
// Try to find a supported format
|
||||
QAudioFormat adjustedFormat = defDevice.preferredFormat();
|
||||
|
||||
// Try to keep as many original settings as possible
|
||||
adjustedFormat.setSampleRate(format.sampleRate());
|
||||
adjustedFormat.setChannelCount(format.channelCount());
|
||||
adjustedFormat.setSampleFormat(format.sampleFormat());
|
||||
|
||||
if (defDevice.isFormatSupported(adjustedFormat))
|
||||
{
|
||||
format = adjustedFormat;
|
||||
return defDevice;
|
||||
}
|
||||
|
||||
// Fallback: Try with preferred format's sample rate but keep other settings
|
||||
adjustedFormat = defDevice.preferredFormat();
|
||||
adjustedFormat.setChannelCount(format.channelCount());
|
||||
adjustedFormat.setSampleFormat(format.sampleFormat());
|
||||
|
||||
if (defDevice.isFormatSupported(adjustedFormat))
|
||||
{
|
||||
format = adjustedFormat;
|
||||
return defDevice;
|
||||
}
|
||||
|
||||
// Last resort: Use the device's preferred format entirely
|
||||
if (defDevice.isFormatSupported(defDevice.preferredFormat()))
|
||||
{
|
||||
format = defDevice.preferredFormat();
|
||||
return defDevice;
|
||||
}
|
||||
|
||||
// Device doesn't support any reasonable format
|
||||
return {};
|
||||
}
|
||||
return defDevice;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user