Skip to content
Open
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
11 changes: 8 additions & 3 deletions src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ static QMediaFormat recordingFormat(bool audio) {
return f;
}
bool Writer::supported(bool audio) { return recordingFormat(audio).isSupported(QMediaFormat::Encode); }
bool Writer::startingEncoder() const { return !m_encoderStartup.hasExpired(2000); }
bool Writer::start(const QString &path, const QVideoFrameFormat &format,
double fps, const QAudioFormat &audioFormat, qint64 now) {
m_clock.start(now);
Expand All @@ -277,6 +278,7 @@ bool Writer::start(const QString &path, const QVideoFrameFormat &format,
m_recorder.setQuality(QMediaRecorder::VeryHighQuality);
m_recorder.setAudioBitRate(192000);
m_recorder.setOutputLocation(QUrl::fromLocalFile(path));
m_encoderStartup.start();
m_recorder.record();
return !m_failed;
}
Expand Down Expand Up @@ -314,8 +316,10 @@ void Writer::videoAt(QVideoFrame frame, qint64 position, bool fromCamera) {
frame.setEndTime(qRound64((frameIndex + 1) * 1000000.0 / m_fps));
frame.setStreamFrameRate(m_fps);
m_lastVideo = timestamp;
// Bounded native-frame references; never build an unbounded 4K frame queue.
if (m_frames.size() >= 6) { fail(tr("Video encoding cannot keep up at this camera's maximum resolution, so the take was stopped.")); return; }
// Opening the hardware encoder can take a second before it consumes frames.
// Keep a bounded startup backlog, then restore the normal throughput limit.
const int limit=startingEncoder()?60:6;
if (m_frames.size() >= limit) { fail(tr("Video encoding cannot keep up at this camera's maximum resolution, so the take was stopped.")); return; }
m_frames.enqueue(frame);
drain();
}
Expand Down Expand Up @@ -358,7 +362,8 @@ void Writer::appendAudio(const QByteArray &data) {
QAudioBuffer buffer(data, m_audioFormat, m_audioFormat.durationForFrames(m_audioFramesWritten));
m_audioFramesWritten += buffer.frameCount();
m_queuedAudioUs += buffer.duration();
if (m_queuedAudioUs > 500000) { fail(tr("Audio encoding cannot keep up, so the take was stopped.")); return; }
const qint64 limit=startingEncoder()?2000000:500000;
if (m_queuedAudioUs > limit) { fail(tr("Audio encoding cannot keep up, so the take was stopped.")); return; }
m_buffers.enqueue(buffer);
drain();
}
Expand Down
3 changes: 3 additions & 0 deletions src/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QVideoFrame>
#include <QQueue>
#include <QTimer>
#include <QElapsedTimer>
#include <optional>

// Maps a monotonic capture clock onto a take with the paused intervals removed.
Expand Down Expand Up @@ -48,6 +49,7 @@ class Writer : public QObject {
void finished();
void failed(const QString &message);
private:
bool startingEncoder() const;
void drain();
void fail(const QString &message);
void appendAudio(const QByteArray &data);
Expand All @@ -63,6 +65,7 @@ class Writer : public QObject {
QVideoFrame m_tailFrame;
QQueue<QAudioBuffer> m_buffers;
QTimer m_flushTimeout;
QElapsedTimer m_encoderStartup;
TakeClock m_clock;
qint64 m_lastVideo = -1, m_lastIndex = -1;
qint64 m_frameDuration = 33333;
Expand Down
35 changes: 34 additions & 1 deletion tests/backend_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ class FakePicker : public FilePicker {
// A camera frame as Qt's V4L2 backend delivers MJPEG: one plane of JPEG bytes.
class JpegBuffer : public QAbstractVideoBuffer {
public:
JpegBuffer(QByteArray jpeg, QSize size) : m_jpeg(std::move(jpeg)), m_format(size, QVideoFrameFormat::Format_Jpeg) {}
JpegBuffer(QByteArray jpeg, QSize size, unsigned long delay = 0)
: m_jpeg(std::move(jpeg)), m_format(size, QVideoFrameFormat::Format_Jpeg), m_delay(delay) {}
QVideoFrameFormat format() const override { return m_format; }
MapData map(QVideoFrame::MapMode) override {
if(m_delay) { const auto delay=m_delay; m_delay=0; QThread::msleep(delay); }
MapData result; result.planeCount=1;
result.data[0]=reinterpret_cast<uchar*>(m_jpeg.data());
result.bytesPerLine[0]=result.dataSize[0]=int(m_jpeg.size());
Expand All @@ -44,6 +46,7 @@ class JpegBuffer : public QAbstractVideoBuffer {
private:
QByteArray m_jpeg;
QVideoFrameFormat m_format;
unsigned long m_delay;
};

class Tests : public QObject {
Expand Down Expand Up @@ -320,6 +323,36 @@ private slots:
QVERIFY(QFile::copy(directory.filePath("take.mp4"),fixture.filePath("fixture.mp4")));
}
}
void encoderStartup_data() {
QTest::addColumn<int>("delayedFrame");
QTest::newRow("startup")<<0;
QTest::newRow("already-recording")<<60;
}
void encoderStartup() {
QFETCH(int,delayedFrame);
QTemporaryDir directory;
Writer writer; QSignalSpy ended(&writer,&Writer::finished), errors(&writer,&Writer::failed);
QAudioFormat audio; audio.setSampleRate(48000); audio.setChannelCount(1); audio.setSampleFormat(QAudioFormat::Int16);
QImage image(320,240,QImage::Format_RGB32); image.fill(Qt::red);
QByteArray jpeg; QBuffer buffer(&jpeg); buffer.open(QIODevice::WriteOnly); QVERIFY(image.save(&buffer,"JPEG"));
QVideoFrameFormat format(image.size(),QVideoFrameFormat::Format_Jpeg);
QVERIFY(writer.start(directory.filePath("take.mp4"),format,30,audio,0));
// Delay real frame mapping on Qt's encoder thread, without replacing the encoder.
// Startup must retain the whole take, but a later stall must still trip backpressure.
for(int i=0;i<90 && errors.isEmpty();++i) {
writer.video(QVideoFrame(std::make_unique<JpegBuffer>(jpeg,image.size(),i==delayedFrame?1000:0)),i*1000000LL/30);
writer.audio(QByteArray(1600*2,0),i*1000000LL/30);
QTest::qWait(35);
}
writer.finish(); QTRY_VERIFY_WITH_TIMEOUT(ended.count()>0,15000);
if(delayedFrame) { QVERIFY(!errors.isEmpty()); return; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Unrelated errors pass the test

The already-recording case passes on any Writer::failed signal, even if recording fails before frame 60 reaches the encoder. That can hide an unrelated failure instead of checking that the later stall still stops recording. Assert that recording stays error-free until the delayed frame, then check for the expected backlog error.

QVERIFY2(errors.isEmpty(),errors.isEmpty()?"":qPrintable(errors.first().first().toString()));
auto p=media::probe(directory.filePath("take.mp4")); QVERIFY2(p.ok,qPrintable(p.error));
QVERIFY(p.audio); QVERIFY(std::abs(p.duration-3)<.1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Shortened audio passes the test

The startup case checks all 90 video frames, but checks audio only through p.audio and the overall file duration. media::probe takes that duration from the file, not the audio stream, so shortened audio can still pass alongside three seconds of video. Check the audio stream duration or decoded sample count as well.

QProcess frames; frames.start("ffprobe",{"-v","error","-select_streams","v","-show_entries","stream=nb_frames","-of","csv=p=0",directory.filePath("take.mp4")});
QVERIFY(frames.waitForFinished()); QCOMPARE(frames.exitCode(),0);
QCOMPARE(frames.readAllStandardOutput().trimmed().toInt(),90);
}
void recoveryAndSave() {
const auto directory=leaveTake("00000000-0000-0000-0000-000000000001",fixture.filePath("fixture.mp4"));
QVERIFY(!directory.isEmpty());
Expand Down