| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <QtGui/QOpenGLFunctions> |
| 5 |
|
| 6 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 7 |
#include <QtOpenGLWidgets/QOpenGLWidget> |
| 8 |
#else |
| 9 |
#include <QtWidgets/QOpenGLWidget> |
| 10 |
#endif |
| 11 |
|
| 12 |
#include <QOpenGLVertexArrayObject> |
| 13 |
#include <QOpenGLBuffer> |
| 14 |
#include <QOpenGLTexture> |
| 15 |
#include <QOpenGLShaderProgram> |
| 16 |
|
| 17 |
#include <QtMultimedia/QAbstractVideoSurface> |
| 18 |
#include <QtMultimedia/QMediaPlayer> |
| 19 |
|
| 20 |
#include <memory> |
| 21 |
|
| 22 |
class TextureWidget : public QOpenGLWidget, private QOpenGLFunctions |
| 23 |
{ |
| 24 |
Q_OBJECT |
| 25 |
Q_PROPERTY(int textureId READ textureId NOTIFY textureIdChanged) |
| 26 |
public: |
| 27 |
TextureWidget(QWidget *parent = nullptr); |
| 28 |
|
| 29 |
int textureId() { return m_texture.textureId(); } |
| 30 |
|
| 31 |
private: |
| 32 |
|
| 33 |
void initializeGL() override; |
| 34 |
|
| 35 |
|
| 36 |
void paintGL() override; |
| 37 |
|
| 38 |
public Q_SLOTS: |
| 39 |
|
| 40 |
void setVideoFrame(const QVideoFrame &frame); |
| 41 |
|
| 42 |
Q_SIGNALS: |
| 43 |
void textureIdChanged(int textureId); |
| 44 |
|
| 45 |
private: |
| 46 |
QOpenGLVertexArrayObject m_vao; |
| 47 |
QOpenGLShaderProgram m_shader; |
| 48 |
QOpenGLTexture m_texture; |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
class GLVideoSurface : public QAbstractVideoSurface |
| 53 |
{ |
| 54 |
Q_OBJECT |
| 55 |
public: |
| 56 |
QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType type) const override; |
| 57 |
|
| 58 |
|
| 59 |
bool present(const QVideoFrame &frame) override; |
| 60 |
|
| 61 |
Q_SIGNALS: |
| 62 |
void onNewFrame(const QVideoFrame &frame); |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
class VideoPlayer : public QObject |
| 67 |
{ |
| 68 |
Q_OBJECT |
| 69 |
public: |
| 70 |
VideoPlayer(TextureWidget *textureWidget); |
| 71 |
~VideoPlayer(); |
| 72 |
|
| 73 |
private: |
| 74 |
TextureWidget *m_textureWidget; |
| 75 |
std::unique_ptr<QMediaPlayer> m_player; |
| 76 |
std::unique_ptr<GLVideoSurface> m_surface; |
| 77 |
}; |