Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QAudioDecoder>
#include <QAudioFormat>
#include <QTimer>
#include <QThread>
#include <QEventLoop>
#include "3rdParty/QtWavFile/WavFile.h"
#include "Common/Qt/StringToolsQt.h"
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -144,39 +143,39 @@ std::tuple<const char*, size_t> AudioFileLoader::load_full_audio(){
return convert_raw_wav_samples();
}

// Use QAudioDecoder to decode compressed audio file:
m_audio_decoder_worker = static_cast<AudioDecoderWorker*>(
GlobalThreadPools::qt_event_threadpool().add_object([this]{
return std::make_unique<AudioDecoderWorker>(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();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review: This exec() has no timeout (the old code's exec() didn't either). If the backend ever fails to deliver errored/finished (the kind of QThread/backend stall this rewrite is aimed at), the calling thread hangs forever — load_audio_template() blocks the GUI thread via qt_worker_threadpool().run_and_wait(). probe_decoded_native_format() guards its equivalent loop with a 3 s single-shot QTimer (AudioInputFile.cpp), and DecodedNativePlaybackTest with 15 s. Consider a generous timeout here that emits errored-style handling (or at least breaks out and reports the stall).

}
}

// 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();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review: After clear(), the fallthrough returns m_raw_buffer.data(), which is only nullptr when the vector holds no capacity. On a repeated load_full_audio() call against the same loader (capacity retained from the previous decode) the returned pointer is non-null with size 0, which contradicts the header contract ("If loading fails, the returned pointer is nullptr") and makes load_audio_template() build a 1-window silent template instead of returning empty. Suggest return std::make_tuple<const char*, size_t>(nullptr, 0); here, matching the wav error paths above.

}
return std::make_tuple<const char*, size_t>(m_raw_buffer.data(), m_raw_buffer.size());
}

Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand All @@ -179,7 +181,7 @@ public slots:

std::vector<char>& m_decoded_buffer;

bool m_start_succeeded = true;
bool m_start_succeeded = false;
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <QAudioDecoder>
#include <QEventLoop>
#include <QTimer>
#include "Common/Cpp/Filesystem/Filesystem.h"
#include "Common/Cpp/TestRunners/UnitTest.h"
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "CommonFramework/GlobalAutoPaths.h"
Expand Down Expand Up @@ -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;
}
};



}

Expand All @@ -490,6 +582,8 @@ void add_tests_AudioFileLoader(UnitTestDatabase& database){
database.add<LoadFullRateMismatchTest>();
database.add<DecodedNativeProbeTest>();
database.add<DecodedNativePlaybackTest>();
database.add<LoadFullMp3Test>();
database.add<Mp3MissingFileTest>();
}


Expand Down
Loading