| 1 | // Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QMutexLocker> |
| 5 | #include <QtMultimedia/QVideoFrame> |
| 6 | |
| 7 | #include "videoplayer.h" |
| 8 | |
| 9 | TextureWidget::TextureWidget(QWidget *parent) |
| 10 | : QOpenGLWidget(parent) |
| 11 | , m_texture(QOpenGLTexture::Target2D) |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | // Main thread |
| 16 | void TextureWidget::initializeGL() |
| 17 | { |
| 18 | initializeOpenGLFunctions(); |
| 19 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
| 20 | |
| 21 | if (!m_shader.addShaderFromSourceCode(QOpenGLShader::Vertex, |
| 22 | "#version 330\n" |
| 23 | "out vec2 coords;\n" |
| 24 | "const vec2 positions[6] = vec2[] (" |
| 25 | " vec2(-1.0, 1.0)," |
| 26 | " vec2(-1.0, -1.0)," |
| 27 | " vec2(1.0, 1.0)," |
| 28 | " vec2(1.0, 1.0)," |
| 29 | " vec2(-1.0, -1.0)," |
| 30 | " vec2(1.0, -1.0));\n" |
| 31 | "const vec2 texCoords[6] = vec2[] (" |
| 32 | " vec2(0.0, 0.0)," |
| 33 | " vec2(0.0, 1.0)," |
| 34 | " vec2(1.0, 0.0)," |
| 35 | " vec2(1.0, 0.0)," |
| 36 | " vec2(0.0, 1.0)," |
| 37 | " vec2(1.0, 1.0));\n" |
| 38 | "void main() {\n" |
| 39 | " coords = texCoords[gl_VertexID];\n" |
| 40 | " gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0);\n" |
| 41 | "}")) |
| 42 | qDebug() << "Failed to load vertex shader" << m_shader.log(); |
| 43 | if (!m_shader.addShaderFromSourceCode(QOpenGLShader::Fragment, |
| 44 | "#version 330\n" |
| 45 | "in vec2 coords;\n" |
| 46 | "uniform sampler2D video_texture;\n" |
| 47 | "out vec4 fragColor;\n" |
| 48 | "void main() {\n" |
| 49 | " fragColor = texture(video_texture, coords);\n" |
| 50 | "}")) |
| 51 | qDebug() << "Failed to load fragment shader" << m_shader.log(); |
| 52 | if (!m_shader.link()) |
| 53 | qDebug() << "Failed to link shaders" << m_shader.log(); |
| 54 | |
| 55 | qDebug() << Q_FUNC_INFO << context()->shareContext(); |
| 56 | |
| 57 | m_vao.create(); |
| 58 | } |
| 59 | |
| 60 | // Main thread |
| 61 | void TextureWidget::paintGL() |
| 62 | { |
| 63 | glViewport(0, 0, width(), height()); |
| 64 | |
| 65 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
| 66 | |
| 67 | if (!m_texture.isCreated()) |
| 68 | return; |
| 69 | |
| 70 | m_shader.bind(); |
| 71 | |
| 72 | m_texture.bind(0); |
| 73 | m_shader.setUniformValue("video_texture", 0); |
| 74 | |
| 75 | m_vao.bind(); |
| 76 | glDrawArrays(GL_TRIANGLES, 0, 6); |
| 77 | |
| 78 | m_vao.release(); |
| 79 | m_shader.release(); |
| 80 | } |
| 81 | |
| 82 | void TextureWidget::setVideoFrame(const QVideoFrame &frame) |
| 83 | { |
| 84 | QVideoFrame f = frame; |
| 85 | |
| 86 | // Map frame |
| 87 | if (!f.map(QAbstractVideoBuffer::ReadOnly)) |
| 88 | return; |
| 89 | |
| 90 | makeCurrent(); |
| 91 | |
| 92 | // Create or recreate texture |
| 93 | if (m_texture.width() != f.width() || m_texture.height() != f.height()) { |
| 94 | if (m_texture.isCreated()) |
| 95 | m_texture.destroy(); |
| 96 | |
| 97 | m_texture.setSize(f.width(), f.height()); |
| 98 | m_texture.setFormat(QOpenGLTexture::RGBA32F); |
| 99 | m_texture.setWrapMode(QOpenGLTexture::ClampToBorder); |
| 100 | m_texture.setMinificationFilter(QOpenGLTexture::Nearest); |
| 101 | m_texture.setMagnificationFilter(QOpenGLTexture::Nearest); |
| 102 | m_texture.allocateStorage(); |
| 103 | |
| 104 | m_texture.create(); |
| 105 | emit textureIdChanged(m_texture.textureId()); |
| 106 | } |
| 107 | |
| 108 | const QVideoFrame::PixelFormat pFormat = f.pixelFormat(); |
| 109 | if (pFormat == QVideoFrame::Format_RGB32) { |
| 110 | m_texture.setData(QOpenGLTexture::RGBA, QOpenGLTexture::UInt8, f.bits()); |
| 111 | } |
| 112 | |
| 113 | doneCurrent(); |
| 114 | |
| 115 | // Request display udpate |
| 116 | QOpenGLWidget::update(); |
| 117 | |
| 118 | // Unmap |
| 119 | f.unmap(); |
| 120 | } |
| 121 | |
| 122 | QList<QVideoFrame::PixelFormat> GLVideoSurface::supportedPixelFormats(QAbstractVideoBuffer::HandleType type) const |
| 123 | { |
| 124 | if (type == QAbstractVideoBuffer::NoHandle) |
| 125 | return { |
| 126 | QVideoFrame::Format_RGB32, |
| 127 | QVideoFrame::Format_ARGB32, |
| 128 | QVideoFrame::Format_BGR32, |
| 129 | QVideoFrame::Format_BGRA32 |
| 130 | }; |
| 131 | return {}; |
| 132 | } |
| 133 | |
| 134 | // Video player thread |
| 135 | bool GLVideoSurface::present(const QVideoFrame &frame) |
| 136 | { |
| 137 | emit onNewFrame(frame); |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | VideoPlayer::VideoPlayer(TextureWidget *textureWidget) |
| 142 | : QObject(textureWidget) |
| 143 | , m_player(new QMediaPlayer(nullptr, QMediaPlayer::VideoSurface)) |
| 144 | , m_surface(new GLVideoSurface()) |
| 145 | { |
| 146 | m_player->setMedia(QUrl("https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4")); |
| 147 | |
| 148 | // Tell player to render on GLVideoSurface |
| 149 | m_player->setVideoOutput(m_surface.get()); |
| 150 | |
| 151 | // Display errors |
| 152 | QObject::connect(m_player.get(), QOverload<QMediaPlayer::Error>::of(&QMediaPlayer::error), |
| 153 | m_player.get(), [this] (QMediaPlayer::Error e) { |
| 154 | qDebug() << Q_FUNC_INFO << e << m_player->errorString(); |
| 155 | }); |
| 156 | |
| 157 | // Repeat video indefinitely |
| 158 | QObject::connect(m_player.get(), &QMediaPlayer::stateChanged, m_player.get(), [this] (QMediaPlayer::State state) { |
| 159 | if (state == QMediaPlayer::StoppedState) |
| 160 | m_player->play(); |
| 161 | }); |
| 162 | |
| 163 | QObject::connect(m_surface.get(), &GLVideoSurface::onNewFrame, |
| 164 | textureWidget, &TextureWidget::setVideoFrame, Qt::DirectConnection); |
| 165 | |
| 166 | // Start playing |
| 167 | m_player->play(); |
| 168 | } |
| 169 | |
| 170 | VideoPlayer::~VideoPlayer() |
| 171 | { |
| 172 | } |