diff --git a/src/writer.cpp b/src/writer.cpp index 6fa6d6c..caf6673 100644 --- a/src/writer.cpp +++ b/src/writer.cpp @@ -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); @@ -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; } @@ -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(); } @@ -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(); } diff --git a/src/writer.h b/src/writer.h index 961f327..bee650b 100644 --- a/src/writer.h +++ b/src/writer.h @@ -7,6 +7,7 @@ #include #include #include +#include #include // Maps a monotonic capture clock onto a take with the paused intervals removed. @@ -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); @@ -63,6 +65,7 @@ class Writer : public QObject { QVideoFrame m_tailFrame; QQueue m_buffers; QTimer m_flushTimeout; + QElapsedTimer m_encoderStartup; TakeClock m_clock; qint64 m_lastVideo = -1, m_lastIndex = -1; qint64 m_frameDuration = 33333; diff --git a/tests/backend_tests.cpp b/tests/backend_tests.cpp index 78f145e..5775a69 100644 --- a/tests/backend_tests.cpp +++ b/tests/backend_tests.cpp @@ -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(m_jpeg.data()); result.bytesPerLine[0]=result.dataSize[0]=int(m_jpeg.size()); @@ -44,6 +46,7 @@ class JpegBuffer : public QAbstractVideoBuffer { private: QByteArray m_jpeg; QVideoFrameFormat m_format; + unsigned long m_delay; }; class Tests : public QObject { @@ -320,6 +323,36 @@ private slots: QVERIFY(QFile::copy(directory.filePath("take.mp4"),fixture.filePath("fixture.mp4"))); } } + void encoderStartup_data() { + QTest::addColumn("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(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; } + 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); + 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());