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
12 changes: 9 additions & 3 deletions src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Yuv420FrameBuffer>(frame));
Expand Down
5 changes: 3 additions & 2 deletions src/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -63,7 +64,7 @@ class Writer : public QObject {
QQueue<QAudioBuffer> 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;
Expand Down
39 changes: 39 additions & 0 deletions tests/backend_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading