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
4 changes: 4 additions & 0 deletions src/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ void Backend::receiveVideo(QVideoFrame frame) {
auto format=frame.surfaceFormat(); format.setColorSpace(m_colorSpace);
frame=media::withFormat(frame,format);
}
// uvcvideo flags a frame the camera cut short, but Qt passes it on regardless,
// and it decodes as a coloured grid. Skip it, and the recording holds the frame
// before. A camera that sends nothing else still times out as stalled.
if(!media::completeFrame(frame)) return;
m_lastVideoAt=now();
if(frame.size()!=m_cameraFormat.resolution()) {
sourceFailed(tr("The camera did not provide its maximum resolution. Choose another source or Retry.")); return;
Expand Down
16 changes: 16 additions & 0 deletions src/mediautils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ QString media::chooseSource(const QStringList &connected, const QString &preferr
if (connected.isEmpty() || connected.contains(remembered)) return remembered;
return connected.contains(preferred) ? preferred : connected.first();
}
bool media::completeJpeg(const uchar *data, qsizetype size) {
if (size < 4 || data[0] != 0xFF || data[1] != 0xD8) return false;
// Some cameras pad the buffer past EOI, so look for it near the end rather than at it.
// Up to 64 bytes of padding: EOI may start 66 bytes before the end.
for (qsizetype i = size - 2; i >= std::max<qsizetype>(2, size - 66); --i)
if (data[i] == 0xFF && data[i + 1] == 0xD9) return true;
return false;
}
bool media::completeFrame(const QVideoFrame &frame) {
if (frame.pixelFormat() != QVideoFrameFormat::Format_Jpeg) return true;
QVideoFrame mapped(frame);
if (!mapped.map(QVideoFrame::ReadOnly)) return false;
const bool complete = completeJpeg(mapped.bits(0), mapped.mappedBytes(0));
mapped.unmap();
return complete;
}
void media::useFfmpeg() {
qputenv("QT_MEDIA_BACKEND", "ffmpeg");
// Loading the backend tries every hardware API before returning, about two
Expand Down
4 changes: 4 additions & 0 deletions src/mediautils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ QVideoFrameFormat::ColorSpace colorSpace(int v4l2Colorspace, int v4l2Encoding);
QVideoFrameFormat::ColorSpace cameraColorSpace(const QByteArray &device);
// Shares the source's image planes and capture times under a new format; later metadata changes stay independent.
QVideoFrame withFormat(const QVideoFrame &source, const QVideoFrameFormat &format);
// A whole JPEG: starts with SOI and ends with EOI, allowing a little padding after it.
bool completeJpeg(const uchar *data, qsizetype size);
// False for an MJPEG frame the camera cut short; other frames always pass.
bool completeFrame(const QVideoFrame &frame);
// Chooses Qt's FFmpeg backend, which takes custom timestamped frames, before anything loads it.
void useFfmpeg();
double targetFps(double minimum, double maximum);
Expand Down
19 changes: 19 additions & 0 deletions tests/backend_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ private slots:
// A camera whose every mode is slower than 15 fps still records at its largest size.
QCOMPARE(media::bestFormat({{{1280,720},10,10,0}, {{640,480},10,12,0}}), 0);
}
void truncatedJpegIsSkipped() {
// An MJPEG frame the camera cut short has no end-of-image marker.
QTemporaryDir directory;
const auto path=directory.filePath("frame.jpg");
QProcess encode; encode.start("ffmpeg",{"-v","error","-f","lavfi","-i","color=red:s=320x240","-frames:v","1",path});
QVERIFY(encode.waitForFinished(20000)); QCOMPARE(encode.exitCode(),0);
QFile file(path); QVERIFY(file.open(QIODevice::ReadOnly));
const auto jpeg=file.readAll();
auto complete=[](const QByteArray &bytes) { return media::completeJpeg(reinterpret_cast<const uchar*>(bytes.constData()),bytes.size()); };
QVERIFY(complete(jpeg));
QVERIFY(complete(jpeg+QByteArray(64,'\0')));
QVERIFY(!complete(jpeg+QByteArray(65,'\0')));
QVERIFY(!complete(jpeg.left(jpeg.size()*2/3)));
QVERIFY(!complete(QByteArray("\xFF\xD9",2)));
const auto frame=[](const QByteArray &bytes) { return QVideoFrame(std::make_unique<JpegBuffer>(bytes,QSize(320,240))); };
QVERIFY(media::completeFrame(frame(jpeg)));
QVERIFY(!media::completeFrame(frame(jpeg.left(jpeg.size()/2))));
QImage image(4,4,QImage::Format_RGB32); QVERIFY(media::completeFrame(QVideoFrame(image)));
}
void cameraColorSpace() {
// Cam Link 4K: sRGB colorspace, explicit BT.709 encoding.
QCOMPARE(media::colorSpace(V4L2_COLORSPACE_SRGB,V4L2_YCBCR_ENC_709),QVideoFrameFormat::ColorSpace_BT709);
Expand Down
Loading