diff --git a/src/writer.cpp b/src/writer.cpp index aa02c59..6fa6d6c 100644 --- a/src/writer.cpp +++ b/src/writer.cpp @@ -290,14 +290,20 @@ void Writer::video(QVideoFrame frame, qint64 capturedAt) { if (m_stopping || m_failed || !frame.isValid()) return; const auto time = m_clock.position(capturedAt); if (!time) return; - videoAt(frame, *time); + videoAt(frame, *time, true); } -void Writer::videoAt(QVideoFrame frame, qint64 position) { +void Writer::videoAt(QVideoFrame frame, qint64 position, bool fromCamera) { // Quantize to the output rate instead of rejecting short callback intervals: // real webcams deliver jittery batches, which must not halve the frame rate. - const qint64 frameIndex = qRound64(position * m_fps / 1000000.0); + qint64 frameIndex = qRound64(position * m_fps / 1000000.0); + // When the camera's phase sits near the midpoint between output frames, its + // jitter rounds consecutive frames onto one slot. Take the next slot instead, + // provided that is at most three quarters of a frame early. + if (fromCamera && frameIndex <= m_lastIndex && (m_lastIndex + 1) * 1000000.0 / m_fps - position <= 0.75 * 1000000.0 / m_fps) + frameIndex = m_lastIndex + 1; const qint64 timestamp = qRound64(frameIndex * 1000000.0 / m_fps); if (timestamp <= m_lastVideo) return; + m_lastIndex = frameIndex; m_tailFrame = frame; if (Yuv420FrameBuffer::converts(frame.pixelFormat())) frame = QVideoFrame(std::make_unique(frame)); diff --git a/src/writer.h b/src/writer.h index 9838276..961f327 100644 --- a/src/writer.h +++ b/src/writer.h @@ -52,7 +52,8 @@ class Writer : public QObject { void fail(const QString &message); void appendAudio(const QByteArray &data); void padAudioTo(qint64 time); - void videoAt(QVideoFrame frame, qint64 position); + // fromCamera lets a jittered frame take the next free slot; the repeated tail frame must not. + void videoAt(QVideoFrame frame, qint64 position, bool fromCamera = false); QMediaCaptureSession m_session; QMediaRecorder m_recorder; QVideoFrameInput *m_video = nullptr; @@ -63,7 +64,7 @@ class Writer : public QObject { QQueue m_buffers; QTimer m_flushTimeout; TakeClock m_clock; - qint64 m_lastVideo = -1; + qint64 m_lastVideo = -1, m_lastIndex = -1; qint64 m_frameDuration = 33333; double m_fps = 30; qint64 m_queuedAudioUs = 0; diff --git a/tests/backend_tests.cpp b/tests/backend_tests.cpp index 67cca8e..2de6fc1 100644 --- a/tests/backend_tests.cpp +++ b/tests/backend_tests.cpp @@ -446,6 +446,45 @@ private slots: QCOMPARE(backend.clips().size(),1); backend.closeTake(); } + void jitteryCameraKeepsEveryFrame() { + // A 30 fps camera whose frames fall midway between output slots, with + // the alternating 28/36 ms intervals real webcams show. + QTemporaryDir directory; + const auto path=directory.filePath("jitter.mp4"); + Writer writer; QSignalSpy ended(&writer,&Writer::finished), errors(&writer,&Writer::failed); + QVERIFY(writer.start(path,QVideoFrameFormat(QSize(320,240),QVideoFrameFormat::Format_BGRA8888),30,QAudioFormat(),0)); + QImage image(320,240,QImage::Format_RGB32); image.fill(Qt::gray); + qint64 captured=15000; + for(int i=0;i<60;++i) { + writer.video(QVideoFrame(image),captured); + const qint64 step=(i%3==2)?28000:36000; + captured+=step; + QTest::qWait(int(step/1000)); + } + writer.finish(); QTRY_VERIFY_WITH_TIMEOUT(!ended.isEmpty(),15000); + QVERIFY2(errors.isEmpty(),errors.isEmpty()?"":qPrintable(errors.first().first().toString())); + QProcess streams; streams.start("ffprobe",{"-v","error","-select_streams","v:0","-show_entries","stream=nb_frames","-of","csv=p=0",path}); + QVERIFY(streams.waitForFinished()); + QCOMPARE(streams.readAllStandardOutput().trimmed().toInt(),60); + } + void stoppingAddsNoFrameAfterTheTake() { + // Finishing repeats the last camera frame to fill the take's final slot. + // That repeat must never move into a later slot, past where the take ends. + QTemporaryDir directory; + const auto path=directory.filePath("tail.mp4"); + Writer writer; QSignalSpy ended(&writer,&Writer::finished), errors(&writer,&Writer::failed); + QVERIFY(writer.start(path,QVideoFrameFormat(QSize(320,240),QVideoFrameFormat::Format_BGRA8888),30,QAudioFormat(),0)); + QImage image(320,240,QImage::Format_RGB32); image.fill(Qt::gray); + // The first frame opens the encoder; give it that moment before the rest arrive. + writer.video(QVideoFrame(image),0); QTest::qWait(300); + for(int i=1;i<29;++i) { writer.video(QVideoFrame(image),qRound64(i*1000000.0/30)); QTest::qWait(33); } + writer.video(QVideoFrame(image),974000); QTest::qWait(33); + writer.pause(1009000); writer.finish(); QTRY_VERIFY_WITH_TIMEOUT(!ended.isEmpty(),15000); + QVERIFY2(errors.isEmpty(),errors.isEmpty()?"":qPrintable(errors.first().first().toString())); + QProcess streams; streams.start("ffprobe",{"-v","error","-select_streams","v:0","-count_frames","-show_entries","stream=nb_read_frames","-of","csv=p=0",path}); + QVERIFY(streams.waitForFinished()); + QCOMPARE(streams.readAllStandardOutput().trimmed().toInt(),30); + } void jpegFramesRecordAs420() { // Webcams send their largest modes as 4:2:2 MJPEG. Recording them as // RGB made Qt pick a 12-bit 4:4:4 target that NVENC rejects, leaving