diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.cpp index 4a4ee9a587..45a5f28366 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "3rdParty/QtWavFile/WavFile.h" #include "Common/Qt/StringToolsQt.h" @@ -106,9 +107,7 @@ bool AudioFileLoader::start(){ connect(m_timer, &QTimer::timeout, this, &AudioFileLoader::send_decoded_buffer_on_timer); }); - emit m_audio_decoder_worker->start(); - - return m_audio_decoder_worker->start_succeeded(); + return m_audio_decoder_worker->start(); } void AudioFileLoader::build_timer(){ @@ -144,39 +143,39 @@ std::tuple AudioFileLoader::load_full_audio(){ return convert_raw_wav_samples(); } - // Use QAudioDecoder to decode compressed audio file: - m_audio_decoder_worker = static_cast( - GlobalThreadPools::qt_event_threadpool().add_object([this]{ - return std::make_unique(nullptr, m_filename, m_audio_format, m_raw_buffer); - }) - ); -// m_audio_decoder_worker = new AudioDecoderWorker(nullptr, m_filename, m_audio_format, m_raw_buffer); -// m_audio_decoder_worker->moveToThread(&m_audioDecoderThread); - - connect(this, &AudioFileLoader::run_audio_decoder_async, m_audio_decoder_worker, &AudioDecoderWorker::start); -// connect(&m_audioDecoderThread, &QThread::finished, m_audio_decoder_worker, &QObject::deleteLater); - - QEventLoop loop; - connect(m_audio_decoder_worker, &AudioDecoderWorker::errored, &loop, &QEventLoop::quit); - connect(m_audio_decoder_worker, &AudioDecoderWorker::finished, &loop, &QEventLoop::quit); - - // m_audioDecoderThread takes care of m_audio_decoder_worker via finished -> deleteLater connection. - // So we don't hold the pointer m_audio_decoder_worker anymore. - m_audio_decoder_worker = nullptr; - -// m_audioDecoderThread.start(); - emit run_audio_decoder_async(); + // Use QAudioDecoder to decode compressed audio file on the current thread. + bool errored = false; + { + AudioDecoderWorker worker(nullptr, m_filename, m_audio_format, m_raw_buffer); + + QEventLoop loop; + bool done = false; + connect(&worker, &AudioDecoderWorker::errored, &loop, [&done, &errored, &loop]{ + done = true; + errored = true; + loop.quit(); + }); + connect(&worker, &AudioDecoderWorker::finished, &loop, [&done, &loop]{ + done = true; + loop.quit(); + }); + + // The decoder can fail inside `start()`: Qt reports a missing file as + // an error right away, we check `done` after `start()`. + worker.start(); + if (!done){ + loop.exec(); + } + } - // Block the current thread until the m_audio_decoder_worker errored or finished. - loop.exec(); if (m_raw_buffer.empty()){ std::cout << "Error: audio decoder produced no audio data for " << m_filename << " (the decoder could not find usable audio in the file)." << std::endl; } - -// m_audioDecoderThread.quit(); -// m_audioDecoderThread.wait(); - GlobalThreadPools::qt_event_threadpool().remove_object(m_audio_decoder_worker); + else if (errored){ + std::cout << "Error: error decoding audio file " << m_filename << "." << std::endl; + m_raw_buffer.clear(); + } return std::make_tuple(m_raw_buffer.data(), m_raw_buffer.size()); } @@ -354,10 +353,23 @@ void AudioDecoderWorker::internal_start(){ m_audio_decoder->start(); std::cout << "Start audio decoder." << std::endl; - m_start_succeeded = true; + // Qt can report an error inside start() itself, e.g. for a missing file. + m_start_succeeded = m_audio_decoder->error() == QAudioDecoder::NoError; return; } +bool AudioDecoderWorker::start(){ + // internal_start() must run on the worker's thread (queried via `this->thread()`) + if (QThread::currentThread() == thread()){ + internal_start(); + }else{ + // Called from any another thread + // `QMetaObject::invokeMethod()` blocks until it has run there + QMetaObject::invokeMethod(this, &AudioDecoderWorker::internal_start, Qt::BlockingQueuedConnection); + } + return m_start_succeeded; +} + AudioDecoderWorker::~AudioDecoderWorker(){ stop(); } diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.h index ce1236fee6..ce715e8164 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.h +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.h @@ -60,6 +60,8 @@ class AudioFileLoader: public QObject{ // Send the sample buffers at the desired speed determined by sample rate in `audio_format` passed // to the constructor. // Retrieve sample buffer by connecting to signal `buffer_ready` from the same thread. + // Returns false if the file can't be opened or decoded from the start, e.g. a missing + // file. For a compressed file, this briefly waits for the decoder to start on its thread. // See comments of the class full more details. bool start(); @@ -79,9 +81,6 @@ class AudioFileLoader: public QObject{ // When finished sending decoded audio frames after `start()` called void finished(); - // Used privately to launch audio decoder woker in a separate thread. - void run_audio_decoder_async(); - private: void build_timer(); @@ -144,11 +143,11 @@ class AudioDecoderWorker: public QObject{ ); virtual ~AudioDecoderWorker(); - void start(){ - QMetaObject::invokeMethod(this, &AudioDecoderWorker::internal_start); - } - - bool start_succeeded(){ return m_start_succeeded; } + // Start decoding on the worker's thread. Returns whether the decoder + // started: false if it failed right away, e.g. for a missing file. + // This function works both on `AudioDecoderWorker`'s own Qt thread, and on + // another Qt thread. + bool start(); void stop(){ QMetaObject::invokeMethod(this, &AudioDecoderWorker::internal_stop); @@ -160,6 +159,9 @@ class AudioDecoderWorker: public QObject{ void finished(); public slots: + // Creates an QAudioDecoder to start decoding the file. + // Every Qt object (including this `AudioDecoderWorker` class) belongs to one thread, queried by `this->thread()`. + // Qt requires a child to be created on its parent’s thread. So internal_start() must run on the worker’s thread. void internal_start(); void internal_stop(); @@ -179,7 +181,7 @@ public slots: std::vector& m_decoded_buffer; - bool m_start_succeeded = true; + bool m_start_succeeded = false; }; diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader_Tests.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader_Tests.cpp index 9e1d108ea3..a82f412938 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader_Tests.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader_Tests.cpp @@ -19,6 +19,7 @@ #include #include #include +#include "Common/Cpp/Filesystem/Filesystem.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/GlobalAutoPaths.h" @@ -476,6 +477,97 @@ class DecodedNativePlaybackTest : public UnitTest{ }; +// +// load_full_audio() on the mp3 fixture, with the kind of target that +// load_audio_template() uses for the mp3 templates (mono float): the decode +// path must return about 2 s of audio. +// +class LoadFullMp3Test : public UnitTest{ +public: + LoadFullMp3Test() + : UnitTest("AudioFileLoader - LoadFull_Mp3") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + const std::string fixture = decoded_probe_fixture(); + if (!Filesystem::exists(fixture)){ + return "Missing mp3 fixture: " + fixture + "."; + } + if (!QAudioDecoder(nullptr).isSupported()){ + return UnitTestResult( + UnitTestResult::SKIPPED, + "No audio decode backend (Qt Multimedia media plugin missing or failing to load)." + ); + } + + // load_full_audio() does not resample, so the target is at the + // fixture's own 44100 Hz. + QAudioFormat target; + target.setSampleFormat(QAudioFormat::SampleFormat::Float); + target.setSampleRate(44100); + target.setChannelCount(1); + + AudioFileLoader loader(nullptr, fixture, target); + const auto ret = loader.load_full_audio(); + const size_t frame_count = std::get<1>(ret) / sizeof(float); + if (frame_count > 88200 + 4000 || frame_count < 88200 - 4000){ + return "Expected about 88200 frames (2 s at 44100 Hz), got " + + std::to_string(frame_count) + "."; + } + + return true; + } +}; + + +// +// A missing mp3 fails right away on both decode paths. Qt reports a missing +// file as an error inside QAudioDecoder::start(), before start() returns: +// - load_full_audio() returns no data. It must not wait in its event loop +// for a decoder signal that already fired, or it would never return. +// - start() returns false. It used to read the start result before the +// decoder's thread had set it, so it always returned true. +// +class Mp3MissingFileTest : public UnitTest{ +public: + Mp3MissingFileTest() + : UnitTest("AudioFileLoader - Mp3_MissingFile") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + if (!QAudioDecoder(nullptr).isSupported()){ + return UnitTestResult( + UnitTestResult::SKIPPED, + "No audio decode backend (Qt Multimedia media plugin missing or failing to load)." + ); + } + + const std::string missing = "/nonexistent/pa_audio_file_loader_missing.mp3"; + QAudioFormat format; + format.setSampleFormat(QAudioFormat::SampleFormat::Float); + format.setSampleRate(44100); + format.setChannelCount(1); + + { + AudioFileLoader loader(nullptr, missing, format); + const auto ret = loader.load_full_audio(); + if (std::get<1>(ret) != 0){ + return "load_full_audio() should return no data for a missing mp3, got " + + std::to_string(std::get<1>(ret)) + " bytes."; + } + } + { + AudioFileLoader loader(nullptr, missing, format); + if (loader.start()){ + return "start() should fail for a missing mp3."; + } + } + + return true; + } +}; + + } @@ -490,6 +582,8 @@ void add_tests_AudioFileLoader(UnitTestDatabase& database){ database.add(); database.add(); database.add(); + database.add(); + database.add(); }