| 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | // |
| 3 | // Copyright (C) 2021, Dmitry Shusharin <[email protected]> |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // a) Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // |
| 11 | // b) Redistributions in binary form must reproduce the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer in |
| 13 | // the documentation and/or other materials provided with the distribution. |
| 14 | // |
| 15 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 17 | // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 19 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 25 | // THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | |
| 27 | #include <QQuickWindow> |
| 28 | #include <QQmlEngine> |
| 29 | #include <QRunnable> |
| 30 | |
| 31 | #include <gst/gst.h> |
| 32 | #include <gst/gl/gl.h> |
| 33 | |
| 34 | #include "videoitem.h" |
| 35 | |
| 36 | static void registerMetatypes() |
| 37 | { |
| 38 | qmlRegisterType<VideoItem>("ACME.VideoItem", 1, 0, "VideoItem"); |
| 39 | qRegisterMetaType<VideoItem::State>("VideoItem::State"); |
| 40 | } |
| 41 | Q_CONSTRUCTOR_FUNCTION(registerMetatypes) |
| 42 | |
| 43 | static const QStringList patterns = { |
| 44 | "smpte", |
| 45 | "snow", |
| 46 | "black", |
| 47 | "white", |
| 48 | "red", |
| 49 | "green", |
| 50 | "blue", |
| 51 | "checkers-1", |
| 52 | "checkers-2", |
| 53 | "checkers-4", |
| 54 | "checkers-8", |
| 55 | "circular", |
| 56 | "blink", |
| 57 | "smpte75", |
| 58 | "zone-plate", |
| 59 | "gamut", |
| 60 | "chroma-zone-plate", |
| 61 | "solid-color", |
| 62 | "ball", |
| 63 | "smpte100", |
| 64 | "bar", |
| 65 | "pinwheel", |
| 66 | "spokes", |
| 67 | "gradient", |
| 68 | "colors" |
| 69 | }; |
| 70 | |
| 71 | struct VideoItemPrivate { |
| 72 | explicit VideoItemPrivate(VideoItem *owner) : own(owner) { } |
| 73 | |
| 74 | VideoItem *own { nullptr }; |
| 75 | |
| 76 | GstElement *pipeline { nullptr }; |
| 77 | GstElement *src { nullptr }; |
| 78 | GstElement *sink { nullptr }; |
| 79 | |
| 80 | GstPad *renderPad { nullptr }; |
| 81 | GstBus *bus { nullptr }; |
| 82 | |
| 83 | VideoItem::State state { VideoItem::STATE_VOID_PENDING }; |
| 84 | |
| 85 | QString pattern {}; |
| 86 | QRect rect { 0, 0, 0, 0 }; |
| 87 | QSize resolution { 0, 0 }; |
| 88 | |
| 89 | /* TODO: make q-properties? */ |
| 90 | quint64 timeout { 3000 }; /* 3s timeout */ |
| 91 | }; |
| 92 | |
| 93 | struct RenderJob : public QRunnable { |
| 94 | using Callable = std::function<void()>; |
| 95 | |
| 96 | explicit RenderJob(Callable c) : _c(c) { } |
| 97 | |
| 98 | void run() { _c(); } |
| 99 | |
| 100 | private: |
| 101 | Callable _c; |
| 102 | }; |
| 103 | |
| 104 | namespace { |
| 105 | |
| 106 | GstBusSyncReply messageHandler(GstBus * /*bus*/, GstMessage *msg, gpointer userData) |
| 107 | { |
| 108 | auto priv = static_cast<VideoItemPrivate *>(userData); |
| 109 | |
| 110 | switch (GST_MESSAGE_TYPE(msg)) { |
| 111 | case GST_MESSAGE_ERROR: { |
| 112 | GError *error { nullptr }; |
| 113 | QString str { "GStreamer error: " }; |
| 114 | |
| 115 | gst_message_parse_error(msg, &error, nullptr); |
| 116 | str.append(error->message); |
| 117 | g_error_free(error); |
| 118 | |
| 119 | emit priv->own->errorOccurred(str); |
| 120 | qWarning() << str; |
| 121 | } break; |
| 122 | |
| 123 | case GST_MESSAGE_STATE_CHANGED: { |
| 124 | if (GST_MESSAGE_SRC(msg) == GST_OBJECT(priv->pipeline)) { |
| 125 | GstState oldState { GST_STATE_NULL }, newState { GST_STATE_NULL }; |
| 126 | |
| 127 | gst_message_parse_state_changed(msg, &oldState, &newState, nullptr); |
| 128 | priv->own->setState(static_cast<VideoItem::State>(newState)); |
| 129 | } |
| 130 | } break; |
| 131 | |
| 132 | case GST_MESSAGE_HAVE_CONTEXT: { |
| 133 | GstContext *context { nullptr }; |
| 134 | |
| 135 | gst_message_parse_have_context(msg, &context); |
| 136 | |
| 137 | if (gst_context_has_context_type(context, GST_GL_DISPLAY_CONTEXT_TYPE)) |
| 138 | gst_element_set_context(priv->pipeline, context); |
| 139 | |
| 140 | if (context) |
| 141 | gst_context_unref(context); |
| 142 | |
| 143 | gst_message_unref(msg); |
| 144 | |
| 145 | return GST_BUS_DROP; |
| 146 | } break; |
| 147 | |
| 148 | default: |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | return GST_BUS_PASS; |
| 153 | } |
| 154 | |
| 155 | } // end namespace |
| 156 | |
| 157 | VideoItem::VideoItem(QQuickItem *parent) |
| 158 | : QQuickItem(parent), _priv(new VideoItemPrivate(this)) |
| 159 | { |
| 160 | connect(this, &VideoItem::rectChanged, this, &VideoItem::updateRect); |
| 161 | connect(this, &VideoItem::stateChanged, this, &VideoItem::updateRect); |
| 162 | |
| 163 | // gst init pipeline |
| 164 | _priv->pipeline = gst_pipeline_new(nullptr); |
| 165 | _priv->src = gst_element_factory_make("videotestsrc", nullptr); |
| 166 | _priv->sink = gst_element_factory_make("glsinkbin", nullptr); |
| 167 | |
| 168 | GstElement *fakesink = gst_element_factory_make("fakesink", nullptr); |
| 169 | |
| 170 | Q_ASSERT(_priv->pipeline && _priv->src && _priv->sink); |
| 171 | Q_ASSERT(fakesink); |
| 172 | |
| 173 | g_object_set(_priv->sink, "sink", fakesink, nullptr); |
| 174 | |
| 175 | gst_bin_add_many(GST_BIN(_priv->pipeline), _priv->src, _priv->sink, nullptr); |
| 176 | gst_element_link_many (_priv->src, _priv->sink, nullptr); |
| 177 | |
| 178 | // add watch |
| 179 | _priv->bus = gst_pipeline_get_bus(GST_PIPELINE(_priv->pipeline)); |
| 180 | gst_bus_set_sync_handler(_priv->bus, messageHandler, _priv.data(), nullptr); |
| 181 | |
| 182 | gst_element_set_state(_priv->pipeline, GST_STATE_READY); |
| 183 | gst_element_get_state(_priv->pipeline, nullptr, nullptr, _priv->timeout * GST_MSECOND); |
| 184 | } |
| 185 | |
| 186 | VideoItem::~VideoItem() |
| 187 | { |
| 188 | gst_bus_set_sync_handler(_priv->bus, nullptr, nullptr, nullptr); // stop handling messages |
| 189 | |
| 190 | gst_element_set_state(_priv->pipeline, GST_STATE_NULL); |
| 191 | |
| 192 | gst_object_unref(_priv->pipeline); |
| 193 | gst_object_unref(_priv->bus); |
| 194 | } |
| 195 | |
| 196 | bool VideoItem::hasVideo() const |
| 197 | { |
| 198 | return _priv->renderPad && (state() & STATE_PLAYING); |
| 199 | } |
| 200 | |
| 201 | QString VideoItem::source() const |
| 202 | { |
| 203 | return _priv->pattern; |
| 204 | } |
| 205 | |
| 206 | void VideoItem::setSource(const QString &source) |
| 207 | { |
| 208 | if (_priv->pattern == source) |
| 209 | return; |
| 210 | |
| 211 | _priv->pattern = source; |
| 212 | |
| 213 | stop(); |
| 214 | |
| 215 | if (!_priv->pattern.isEmpty()) { |
| 216 | |
| 217 | auto it = std::find (patterns.begin(), patterns.end(), source); |
| 218 | |
| 219 | if (it != patterns.end()) { |
| 220 | g_object_set(_priv->src, "pattern", std::distance(patterns.begin(), it), nullptr); |
| 221 | play(); |
| 222 | } |
| 223 | } |
| 224 | emit sourceChanged(_priv->pattern); |
| 225 | } |
| 226 | |
| 227 | void VideoItem::play() |
| 228 | { |
| 229 | if (_priv->state > STATE_NULL) { |
| 230 | const auto status = gst_element_set_state(_priv->pipeline, GST_STATE_PLAYING); |
| 231 | |
| 232 | if (status == GST_STATE_CHANGE_FAILURE) |
| 233 | qWarning() << "GStreamer error: unable to start playback"; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void VideoItem::stop() |
| 238 | { |
| 239 | if (_priv->state > STATE_NULL) { |
| 240 | const auto status = gst_element_set_state(_priv->pipeline, GST_STATE_READY); |
| 241 | |
| 242 | if (status == GST_STATE_CHANGE_FAILURE) |
| 243 | qWarning() << "GStreamer error: unable to stop playback"; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void VideoItem::componentComplete() |
| 248 | { |
| 249 | QQuickItem::componentComplete(); |
| 250 | |
| 251 | QQuickItem *videoItem = findChild<QQuickItem *>("videoItem"); |
| 252 | Q_ASSERT(videoItem); // should not fail: check VideoItem.qml |
| 253 | |
| 254 | // needed for proper OpenGL context setup for GStreamer elements (QtQuick renderer) |
| 255 | auto setRenderer = [=](QQuickWindow *window) { |
| 256 | if (window) { |
| 257 | GstElement *glsink = gst_element_factory_make("qmlglsink", nullptr); |
| 258 | Q_ASSERT(glsink); |
| 259 | gst_object_ref_sink (glsink); |
| 260 | |
| 261 | GstState current {GST_STATE_NULL}, pending {GST_STATE_NULL}, target {GST_STATE_NULL}; |
| 262 | auto status = gst_element_get_state(_priv->pipeline, ¤t, &pending, 0); |
| 263 | |
| 264 | switch (status) { |
| 265 | case GST_STATE_CHANGE_FAILURE: { |
| 266 | qWarning() << "GStreamer error: while setting renderer: pending state change failure"; |
| 267 | return; |
| 268 | } |
| 269 | case GST_STATE_CHANGE_SUCCESS: |
| 270 | Q_FALLTHROUGH(); |
| 271 | case GST_STATE_CHANGE_NO_PREROLL: { |
| 272 | target = current; |
| 273 | break; |
| 274 | } |
| 275 | case GST_STATE_CHANGE_ASYNC: { |
| 276 | target = pending; |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | gst_element_set_state(_priv->pipeline, GST_STATE_NULL); |
| 282 | |
| 283 | window->scheduleRenderJob(new RenderJob([=] { |
| 284 | g_object_set(glsink, "widget", videoItem, nullptr); |
| 285 | _priv->renderPad = gst_element_get_static_pad(glsink, "sink"); |
| 286 | g_object_set(_priv->sink, "sink", glsink, nullptr); |
| 287 | gst_element_set_state(_priv->pipeline, target); |
| 288 | gst_object_unref (glsink); |
| 289 | }), |
| 290 | QQuickWindow::BeforeSynchronizingStage); |
| 291 | } |
| 292 | }; |
| 293 | |
| 294 | setRenderer(window()); |
| 295 | connect(this, &QQuickItem::windowChanged, this, setRenderer); |
| 296 | } |
| 297 | |
| 298 | void VideoItem::releaseResources() |
| 299 | { |
| 300 | GstElement *sink { nullptr }; |
| 301 | |
| 302 | gst_element_set_state(_priv->pipeline, GST_STATE_NULL); |
| 303 | g_object_get(_priv->sink, "sink", &sink, nullptr); |
| 304 | |
| 305 | if (sink && _priv->renderPad) { |
| 306 | g_object_set(sink, "widget", nullptr, nullptr); |
| 307 | } |
| 308 | |
| 309 | gst_clear_object (&_priv->renderPad); |
| 310 | gst_clear_object (&sink); |
| 311 | } |
| 312 | |
| 313 | void VideoItem::updateRect() |
| 314 | { |
| 315 | // WARNING: don't touch this |
| 316 | if (!_priv->renderPad || _priv->state < STATE_PLAYING) { |
| 317 | setRect(QRect(0, 0, 0, 0)); |
| 318 | setResolution(QSize(0, 0)); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | // update size |
| 323 | GstCaps *caps = gst_pad_get_current_caps(_priv->renderPad); |
| 324 | GstStructure *caps_struct = gst_caps_get_structure(caps, 0); |
| 325 | |
| 326 | gint picWidth { 0 }, picHeight { 0 }; |
| 327 | gst_structure_get_int(caps_struct, "width", &picWidth); |
| 328 | gst_structure_get_int(caps_struct, "height", &picHeight); |
| 329 | |
| 330 | qreal winWidth { this->width() }, winHeight { this->height() }; |
| 331 | |
| 332 | float picScaleRatio = picWidth * 1.0f / picHeight; |
| 333 | float wndScaleRatio = winWidth / winHeight; |
| 334 | |
| 335 | if (picScaleRatio >= wndScaleRatio) { |
| 336 | float span = winHeight - winWidth * picHeight / picWidth; |
| 337 | setRect(QRect(0, span / 2, winWidth, winHeight - span)); |
| 338 | } else { |
| 339 | float span = winWidth - winHeight * picWidth / picHeight; |
| 340 | setRect(QRect(span / 2, 0, winWidth - span, winHeight)); |
| 341 | } |
| 342 | setResolution(QSize(picWidth, picHeight)); |
| 343 | gst_clear_caps(&caps); |
| 344 | } |
| 345 | |
| 346 | VideoItem::State VideoItem::state() const |
| 347 | { |
| 348 | return _priv->state; |
| 349 | } |
| 350 | |
| 351 | void VideoItem::setState(VideoItem::State state) |
| 352 | { |
| 353 | if (_priv->state == state) |
| 354 | return; |
| 355 | |
| 356 | _priv->state = state; |
| 357 | |
| 358 | emit hasVideoChanged(_priv->state & STATE_PLAYING); |
| 359 | emit stateChanged(_priv->state); |
| 360 | } |
| 361 | |
| 362 | QRect VideoItem::rect() const |
| 363 | { |
| 364 | return _priv->rect; |
| 365 | } |
| 366 | |
| 367 | void VideoItem::setRect(const QRect &rect) |
| 368 | { |
| 369 | if (_priv->rect == rect) |
| 370 | return; |
| 371 | |
| 372 | _priv->rect = rect; |
| 373 | emit rectChanged(_priv->rect); |
| 374 | } |
| 375 | |
| 376 | QSize VideoItem::resolution() const |
| 377 | { |
| 378 | return _priv->resolution; |
| 379 | } |
| 380 | |
| 381 | void VideoItem::setResolution(const QSize &size) |
| 382 | { |
| 383 | if (_priv->resolution == size) |
| 384 | return; |
| 385 | |
| 386 | _priv->resolution = size; |
| 387 | emit resolutionChanged(_priv->resolution); |
| 388 | } |