Skip to content

Instantly share code, notes, and snippets.

@JC3
Last active June 22, 2021 19:15
Show Gist options
  • Save JC3/a7bab65acbd7659d1e57103d2b0021ba to your computer and use it in GitHub Desktop.
Save JC3/a7bab65acbd7659d1e57103d2b0021ba to your computer and use it in GitHub Desktop.
VideoProbeSurface for when your QVideoProbe isn't working.
#ifndef VIDEOPROBESURFACE_H
#define VIDEOPROBESURFACE_H
#include <QAbstractVideoSurface>
#include <QVideoSurfaceFormat>
/* A bit of a hack, but...
*
* Example:
*
* QMediaPlayer *player = ...;
*
* QVideoWidget *widget = ...;
*
* VideoProbeSurface *probe = new VideoProbeSurface(...);
* probe->setFormatSource(widget->videoSurface());
* connect(probe, &VideoProbeSurface::videoFrameProbed, ...);
*
* player->setVideoOutput({ widget->videoSurface(), probe });
*/
class VideoProbeSurface : public QAbstractVideoSurface {
Q_OBJECT
public:
VideoProbeSurface (QObject *parent = nullptr)
: QAbstractVideoSurface(parent)
, formatSource_(nullptr)
{
}
void setFormatSource (QAbstractVideoSurface *source) {
formatSource_ = source;
}
QList<QVideoFrame::PixelFormat> supportedPixelFormats (QAbstractVideoBuffer::HandleType type) const override {
return formatSource_ ? formatSource_->supportedPixelFormats(type)
: QList<QVideoFrame::PixelFormat>();
}
QVideoSurfaceFormat nearestFormat (const QVideoSurfaceFormat &format) const override {
return formatSource_ ? formatSource_->nearestFormat(format)
: QAbstractVideoSurface::nearestFormat(format);
}
bool present (const QVideoFrame &frame) override {
emit videoFrameProbed(frame);
return true;
}
signals:
void videoFrameProbed (const QVideoFrame &frame);
private:
QAbstractVideoSurface *formatSource_;
};
#endif // VIDEOPROBESURFACE_H
@JC3
Copy link
Author

JC3 commented Jun 22, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment