diff --git a/src/audio.cpp b/src/audio.cpp index e926e013..d182db14 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -135,10 +135,16 @@ namespace audio { return; } + auto init_failure_fg = util::fail_guard([&shutdown_event]() { + BOOST_LOG(error) << "Unable to initialize audio capture. The stream will not have audio."sv; + + // Wait for shutdown to be signalled if we fail init. + // This allows streaming to continue without audio. + shutdown_event->view(); + }); + auto &control = ref->control; if (!control) { - shutdown_event->view(); - return; } @@ -181,6 +187,15 @@ namespace audio { } } + auto frame_size = config.packetDuration * stream->sampleRate / 1000; + auto mic = control->microphone(stream->mapping, stream->channelCount, stream->sampleRate, frame_size); + if (!mic) { + return; + } + + // Audio is initialized, so we don't want to print the failure message + init_failure_fg.disable(); + // Capture takes place on this thread platf::adjust_thread_priority(platf::thread_priority_e::critical); @@ -194,16 +209,8 @@ namespace audio { shutdown_event->view(); }); - auto frame_size = config.packetDuration * stream->sampleRate / 1000; int samples_per_frame = frame_size * stream->channelCount; - auto mic = control->microphone(stream->mapping, stream->channelCount, stream->sampleRate, frame_size); - if (!mic) { - BOOST_LOG(error) << "Couldn't create audio input"sv; - - return; - } - while (!shutdown_event->peek()) { std::vector sample_buffer; sample_buffer.resize(samples_per_frame); diff --git a/src/platform/windows/audio.cpp b/src/platform/windows/audio.cpp index cc31c0c5..7da6b9a7 100644 --- a/src/platform/windows/audio.cpp +++ b/src/platform/windows/audio.cpp @@ -666,7 +666,13 @@ namespace platf::audio { for (int x = 0; x < (int) ERole_enum_count; ++x) { auto status = policy->SetDefaultEndpoint(wstring_device_id->c_str(), (ERole) x); if (status) { - BOOST_LOG(warning) << "Couldn't set ["sv << sink << "] to role ["sv << x << ']'; + // Depending on the format of the string, we could get either of these errors + if (status == HRESULT_FROM_WIN32(ERROR_NOT_FOUND) || status == E_INVALIDARG) { + BOOST_LOG(warning) << "Audio sink not found: "sv << sink; + } + else { + BOOST_LOG(warning) << "Couldn't set ["sv << sink << "] to role ["sv << x << "]: 0x"sv << util::hex(status).to_string_view(); + } ++failure; }