From f5fd5539fa8a5bdc1ec4fa269fb9d26ae151b548 Mon Sep 17 00:00:00 2001 From: Alexander Yee Date: Sun, 15 Dec 2024 11:01:21 -0800 Subject: [PATCH] Don't crash if audio sink suddenly goes invalid. --- .../CommonFramework/AudioPipeline/IO/AudioSink.cpp | 14 +++++++++++++- .../CommonFramework/Recording/StreamRecorder.cpp | 6 ++++++ .../VideoPipeline/Backends/CameraWidgetQt6.5.cpp | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSink.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSink.cpp index d6bb5f4a0..a91522fcf 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSink.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSink.cpp @@ -57,6 +57,16 @@ class AudioOutputDevice : public AudioFloatToStream, private StreamListener{ , m_logger(logger) , m_sink(device, format) { + m_sink.connect( + &m_sink, &NativeAudioSink::stateChanged, + &m_sink, [this](QAudio::State state){ + if (state == QAudio::State::StoppedState){ + m_io_device = nullptr; + m_logger.log("AudioOutputDevice has prematurely stopped.", COLOR_RED); + } + } + ); + m_io_device = m_sink.start(); m_sink.setVolume(convertAudioVolumeFromSlider(volume)); add_listener(*this); @@ -74,7 +84,9 @@ class AudioOutputDevice : public AudioFloatToStream, private StreamListener{ virtual void on_objects(const void* data, size_t objects) override{ auto scope_check = m_sanitizer.check_scope(); - m_io_device->write((const char*)data, objects * object_size); + if (m_io_device != nullptr){ + m_io_device->write((const char*)data, objects * object_size); + } } private: diff --git a/SerialPrograms/Source/CommonFramework/Recording/StreamRecorder.cpp b/SerialPrograms/Source/CommonFramework/Recording/StreamRecorder.cpp index 74fa36e2b..4c401eb8d 100644 --- a/SerialPrograms/Source/CommonFramework/Recording/StreamRecorder.cpp +++ b/SerialPrograms/Source/CommonFramework/Recording/StreamRecorder.cpp @@ -116,6 +116,7 @@ bool StreamRecording::stop_and_save(const std::string& filename){ void StreamRecording::push_samples(WallClock timestamp, const float* data, size_t frames){ +#if 1 WallClock now = current_time(); if (m_audio_samples_per_frame == 0 || now < m_start_time){ return; @@ -150,8 +151,10 @@ void StreamRecording::push_samples(WallClock timestamp, const float* data, size_ data, frames * m_audio_samples_per_frame ); m_cv.notify_all(); +#endif } void StreamRecording::push_frame(std::shared_ptr frame){ +#if 1 WallClock now = current_time(); if (!m_has_video || now < m_start_time){ return; @@ -190,6 +193,7 @@ void StreamRecording::push_frame(std::shared_ptr frame){ m_buffered_frames.emplace_back(std::move(frame)); m_last_frame_time = frame_time; m_cv.notify_all(); +#endif } @@ -313,6 +317,7 @@ void StreamRecording::internal_run(){ // cout << "Bit Rate = " << (int)recorder.videoBitRate() << endl; +#if 1 // cout << "starting recording" << endl; m_recorder->record(); @@ -380,6 +385,7 @@ void StreamRecording::internal_run(){ m_recorder->stop(); // cout << "recorder.stop()" << endl; +#endif while (m_recorder->recorderState() != QMediaRecorder::StoppedState){ diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.5.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.5.cpp index 03f386376..abfc28417 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.5.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.5.cpp @@ -9,6 +9,7 @@ #include #include +//#include #include #include #include @@ -286,6 +287,8 @@ void CameraSession::connect_video_sink(QVideoSink* sink){ } } +// cout << QThread::currentThread()->isMainThread() << endl; + }, Qt::DirectConnection );