| 1 | /*  This file is part of the KDE project.
 | 
|---|
| 2 | 
 | 
|---|
| 3 |     Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 | 
|---|
| 4 | 
 | 
|---|
| 5 |     This library is free software: you can redistribute it and/or modify
 | 
|---|
| 6 |     it under the terms of the GNU Lesser General Public License as published by
 | 
|---|
| 7 |     the Free Software Foundation, either version 2.1 or 3 of the License.
 | 
|---|
| 8 | 
 | 
|---|
| 9 |     This library is distributed in the hope that it will be useful,
 | 
|---|
| 10 |     but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 11 |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 12 |     GNU Lesser General Public License for more details.
 | 
|---|
| 13 | 
 | 
|---|
| 14 |     You should have received a copy of the GNU Lesser General Public License
 | 
|---|
| 15 |     along with this library.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 16 | */
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "videowidget.h"
 | 
|---|
| 19 | #include <QtCore/QEvent>
 | 
|---|
| 20 | #include <QtGui/QResizeEvent>
 | 
|---|
| 21 | #include <QtGui/QPalette>
 | 
|---|
| 22 | #include <QtGui/QImage>
 | 
|---|
| 23 | #include <QtGui/QPainter>
 | 
|---|
| 24 | #include <QtGui/QBoxLayout>
 | 
|---|
| 25 | #include <QApplication>
 | 
|---|
| 26 | #include <gst/gst.h>
 | 
|---|
| 27 | #include <gst/interfaces/propertyprobe.h>
 | 
|---|
| 28 | #include "mediaobject.h"
 | 
|---|
| 29 | #include "message.h"
 | 
|---|
| 30 | #include "common.h"
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #include "glrenderer.h"
 | 
|---|
| 33 | #include "widgetrenderer.h"
 | 
|---|
| 34 | #include "x11renderer.h"
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #ifndef QT_NO_PHONON_VIDEO
 | 
|---|
| 37 | QT_BEGIN_NAMESPACE
 | 
|---|
| 38 | 
 | 
|---|
| 39 | namespace Phonon
 | 
|---|
| 40 | {
 | 
|---|
| 41 | namespace Gstreamer
 | 
|---|
| 42 | {
 | 
|---|
| 43 | 
 | 
|---|
| 44 | VideoWidget::VideoWidget(Backend *backend, QWidget *parent) :
 | 
|---|
| 45 |     QWidget(parent),
 | 
|---|
| 46 |     MediaNode(backend, VideoSink),
 | 
|---|
| 47 |     m_videoBin(0),
 | 
|---|
| 48 |     m_renderer(0),
 | 
|---|
| 49 |     m_aspectRatio(Phonon::VideoWidget::AspectRatioAuto),
 | 
|---|
| 50 |     m_brightness(0.0),
 | 
|---|
| 51 |     m_hue(0.0),
 | 
|---|
| 52 |     m_contrast(0.0),
 | 
|---|
| 53 |     m_saturation(0.0),
 | 
|---|
| 54 |     m_scaleMode(Phonon::VideoWidget::FitInView),
 | 
|---|
| 55 |     m_videoBalance(0),
 | 
|---|
| 56 |     m_colorspace(0),
 | 
|---|
| 57 |     m_videoplug(0)
 | 
|---|
| 58 | {
 | 
|---|
| 59 |     setupVideoBin();
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | VideoWidget::~VideoWidget()
 | 
|---|
| 63 | {
 | 
|---|
| 64 |     if (m_videoBin) {
 | 
|---|
| 65 |         gst_element_set_state (m_videoBin, GST_STATE_NULL);
 | 
|---|
| 66 |         gst_object_unref (m_videoBin);
 | 
|---|
| 67 |     }
 | 
|---|
| 68 | 
 | 
|---|
| 69 |     if (m_renderer)
 | 
|---|
| 70 |         delete m_renderer;
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | 
 | 
|---|
| 74 | void VideoWidget::setupVideoBin()
 | 
|---|
| 75 | {
 | 
|---|
| 76 | 
 | 
|---|
| 77 |     m_renderer = m_backend->deviceManager()->createVideoRenderer(this);
 | 
|---|
| 78 |     GstElement *videoSink = m_renderer->videoSink();
 | 
|---|
| 79 | 
 | 
|---|
| 80 |     m_videoBin = gst_bin_new (NULL);
 | 
|---|
| 81 |     Q_ASSERT(m_videoBin);
 | 
|---|
| 82 |     gst_object_ref (GST_OBJECT (m_videoBin)); //Take ownership
 | 
|---|
| 83 |     gst_object_sink (GST_OBJECT (m_videoBin));
 | 
|---|
| 84 | 
 | 
|---|
| 85 |     //The videoplug element is the final element before the pluggable videosink
 | 
|---|
| 86 |     m_videoplug = gst_element_factory_make ("identity", NULL);
 | 
|---|
| 87 | 
 | 
|---|
| 88 |     //Colorspace ensures that the output of the stream matches the input format accepted by our video sink
 | 
|---|
| 89 |     m_colorspace = gst_element_factory_make ("ffmpegcolorspace", NULL);
 | 
|---|
| 90 | 
 | 
|---|
| 91 |     //Video scale is used to prepare the correct aspect ratio and scale.
 | 
|---|
| 92 |     GstElement *videoScale = gst_element_factory_make ("videoscale", NULL);
 | 
|---|
| 93 | 
 | 
|---|
| 94 |     //We need a queue to support the tee from parent node
 | 
|---|
| 95 |     GstElement *queue = gst_element_factory_make ("queue", NULL);
 | 
|---|
| 96 | 
 | 
|---|
| 97 |     if (queue && m_videoBin && videoScale && m_colorspace && videoSink && m_videoplug) {
 | 
|---|
| 98 |         //Ensure that the bare essentials are prepared
 | 
|---|
| 99 |         gst_bin_add_many (GST_BIN (m_videoBin), queue, m_colorspace, m_videoplug, videoScale, videoSink, (const char*)NULL);
 | 
|---|
| 100 |         bool success = false;
 | 
|---|
| 101 |         //Video balance controls color/sat/hue in the YUV colorspace
 | 
|---|
| 102 |         m_videoBalance = gst_element_factory_make ("videobalance", NULL);
 | 
|---|
| 103 |         if (m_videoBalance) {
 | 
|---|
| 104 |             // For video balance to work we have to first ensure that the video is in YUV colorspace,
 | 
|---|
| 105 |             // then hand it off to the videobalance filter before finally converting it back to RGB.
 | 
|---|
| 106 |             // Hence we nede a videoFilter to convert the colorspace before and after videobalance
 | 
|---|
| 107 |             GstElement *m_colorspace2 = gst_element_factory_make ("ffmpegcolorspace", NULL);
 | 
|---|
| 108 |             gst_bin_add_many(GST_BIN(m_videoBin), m_videoBalance, m_colorspace2, (const char*)NULL);
 | 
|---|
| 109 |             success = gst_element_link_many(queue, m_colorspace, m_videoBalance, m_colorspace2, videoScale, m_videoplug, videoSink, (const char*)NULL);
 | 
|---|
| 110 |         } else {
 | 
|---|
| 111 |             //If video balance is not available, just connect to sink directly
 | 
|---|
| 112 |             success = gst_element_link_many(queue, m_colorspace, videoScale, m_videoplug, videoSink, (const char*)NULL);
 | 
|---|
| 113 |         }
 | 
|---|
| 114 | 
 | 
|---|
| 115 |         if (success) {
 | 
|---|
| 116 |             GstPad *videopad = gst_element_get_pad (queue, "sink");
 | 
|---|
| 117 |             gst_element_add_pad (m_videoBin, gst_ghost_pad_new ("sink", videopad));
 | 
|---|
| 118 |             gst_object_unref (videopad);
 | 
|---|
| 119 |             QWidget *parentWidget = qobject_cast<QWidget*>(parent());
 | 
|---|
| 120 |             if (parentWidget)
 | 
|---|
| 121 |                 parentWidget->winId();  // Due to some existing issues with alien in 4.4,
 | 
|---|
| 122 |                                         //  we must currently force the creation of a parent widget.
 | 
|---|
| 123 |             m_isValid = true; //initialization ok, accept input
 | 
|---|
| 124 |         }
 | 
|---|
| 125 |     }
 | 
|---|
| 126 | }
 | 
|---|
| 127 | 
 | 
|---|
| 128 | void VideoWidget::paintEvent(QPaintEvent *event)
 | 
|---|
| 129 | {
 | 
|---|
| 130 |     Q_ASSERT(m_renderer);
 | 
|---|
| 131 |     m_renderer->handlePaint(event);
 | 
|---|
| 132 | }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | void VideoWidget::setVisible(bool val) {
 | 
|---|
| 135 |     Q_ASSERT(m_renderer);
 | 
|---|
| 136 | 
 | 
|---|
| 137 |     // Disable overlays for graphics view
 | 
|---|
| 138 |     if (root() && window() && window()->testAttribute(Qt::WA_DontShowOnScreen) && !m_renderer->paintsOnWidget()) {
 | 
|---|
| 139 |         m_backend->logMessage(QString("Widget rendering forced"), Backend::Info, this);
 | 
|---|
| 140 |         GstElement *videoSink = m_renderer->videoSink();
 | 
|---|
| 141 |         Q_ASSERT(videoSink);
 | 
|---|
| 142 | 
 | 
|---|
| 143 |         gst_element_set_state (videoSink, GST_STATE_NULL);
 | 
|---|
| 144 |         gst_bin_remove(GST_BIN(m_videoBin), videoSink);
 | 
|---|
| 145 |         delete m_renderer;
 | 
|---|
| 146 |         m_renderer = 0;
 | 
|---|
| 147 | 
 | 
|---|
| 148 |         // Use widgetRenderer as a fallback
 | 
|---|
| 149 |         m_renderer = new WidgetRenderer(this);
 | 
|---|
| 150 |         videoSink = m_renderer->videoSink();
 | 
|---|
| 151 |         gst_bin_add(GST_BIN(m_videoBin), videoSink);
 | 
|---|
| 152 |         gst_element_link(m_videoplug, videoSink);
 | 
|---|
| 153 |         gst_element_set_state (videoSink, GST_STATE_PAUSED);
 | 
|---|
| 154 | 
 | 
|---|
| 155 |         // Request return to current state
 | 
|---|
| 156 |         root()->invalidateGraph();
 | 
|---|
| 157 |         root()->setState(root()->state());
 | 
|---|
| 158 |     }
 | 
|---|
| 159 |     QWidget::setVisible(val);    
 | 
|---|
| 160 | }
 | 
|---|
| 161 | 
 | 
|---|
| 162 | bool VideoWidget::event(QEvent *event)
 | 
|---|
| 163 | {
 | 
|---|
| 164 |     if (m_renderer && m_renderer->eventFilter(event))
 | 
|---|
| 165 |         return true;
 | 
|---|
| 166 |     return QWidget::event(event);
 | 
|---|
| 167 | }
 | 
|---|
| 168 | 
 | 
|---|
| 169 | Phonon::VideoWidget::AspectRatio VideoWidget::aspectRatio() const
 | 
|---|
| 170 | {
 | 
|---|
| 171 |     return m_aspectRatio;
 | 
|---|
| 172 | }
 | 
|---|
| 173 | 
 | 
|---|
| 174 | QSize VideoWidget::sizeHint() const
 | 
|---|
| 175 | {
 | 
|---|
| 176 |     if (!m_movieSize.isEmpty())
 | 
|---|
| 177 |         return m_movieSize;
 | 
|---|
| 178 |     else
 | 
|---|
| 179 |         return QSize(640, 480);
 | 
|---|
| 180 | }
 | 
|---|
| 181 | 
 | 
|---|
| 182 | void VideoWidget::setAspectRatio(Phonon::VideoWidget::AspectRatio aspectRatio)
 | 
|---|
| 183 | {
 | 
|---|
| 184 |     m_aspectRatio = aspectRatio;
 | 
|---|
| 185 |     if (m_renderer)
 | 
|---|
| 186 |         m_renderer->aspectRatioChanged(aspectRatio);
 | 
|---|
| 187 | }
 | 
|---|
| 188 | 
 | 
|---|
| 189 | Phonon::VideoWidget::ScaleMode VideoWidget::scaleMode() const
 | 
|---|
| 190 | {
 | 
|---|
| 191 |     return m_scaleMode;
 | 
|---|
| 192 | }
 | 
|---|
| 193 | 
 | 
|---|
| 194 | QRect VideoWidget::scaleToAspect(QRect srcRect, int w, int h) const
 | 
|---|
| 195 | {
 | 
|---|
| 196 |     float width = srcRect.width();
 | 
|---|
| 197 |     float height = srcRect.width() * (float(h) / float(w));
 | 
|---|
| 198 |     if (height > srcRect.height()) {
 | 
|---|
| 199 |         height = srcRect.height();
 | 
|---|
| 200 |         width = srcRect.height() * (float(w) / float(h));
 | 
|---|
| 201 |     }
 | 
|---|
| 202 |     return QRect(0, 0, (int)width, (int)height);
 | 
|---|
| 203 | }
 | 
|---|
| 204 | 
 | 
|---|
| 205 | /***
 | 
|---|
| 206 |  * Calculates the actual rectangle the movie will be presented with
 | 
|---|
| 207 |  **/
 | 
|---|
| 208 | QRect VideoWidget::calculateDrawFrameRect() const
 | 
|---|
| 209 | {
 | 
|---|
| 210 |     QRect widgetRect = rect();
 | 
|---|
| 211 |     QRect drawFrameRect;
 | 
|---|
| 212 |     // Set m_drawFrameRect to be the size of the smallest possible
 | 
|---|
| 213 |     // rect conforming to the aspect and containing the whole frame:
 | 
|---|
| 214 |     switch (aspectRatio()) {
 | 
|---|
| 215 | 
 | 
|---|
| 216 |     case Phonon::VideoWidget::AspectRatioWidget:
 | 
|---|
| 217 |         drawFrameRect = widgetRect;
 | 
|---|
| 218 |         // No more calculations needed.
 | 
|---|
| 219 |         return drawFrameRect;
 | 
|---|
| 220 | 
 | 
|---|
| 221 |     case Phonon::VideoWidget::AspectRatio4_3:
 | 
|---|
| 222 |         drawFrameRect = scaleToAspect(widgetRect, 4, 3);
 | 
|---|
| 223 |         break;
 | 
|---|
| 224 | 
 | 
|---|
| 225 |     case Phonon::VideoWidget::AspectRatio16_9:
 | 
|---|
| 226 |         drawFrameRect = scaleToAspect(widgetRect, 16, 9);
 | 
|---|
| 227 |         break;
 | 
|---|
| 228 | 
 | 
|---|
| 229 |     case Phonon::VideoWidget::AspectRatioAuto:
 | 
|---|
| 230 |     default:
 | 
|---|
| 231 |         drawFrameRect = QRect(0, 0, movieSize().width(), movieSize().height());
 | 
|---|
| 232 |         break;
 | 
|---|
| 233 |     }
 | 
|---|
| 234 | 
 | 
|---|
| 235 |     // Scale m_drawFrameRect to fill the widget
 | 
|---|
| 236 |     // without breaking aspect:
 | 
|---|
| 237 |     float widgetWidth = widgetRect.width();
 | 
|---|
| 238 |     float widgetHeight = widgetRect.height();
 | 
|---|
| 239 |     float frameWidth = widgetWidth;
 | 
|---|
| 240 |     float frameHeight = drawFrameRect.height() * float(widgetWidth) / float(drawFrameRect.width());
 | 
|---|
| 241 | 
 | 
|---|
| 242 |     switch (scaleMode()) {
 | 
|---|
| 243 |     case Phonon::VideoWidget::ScaleAndCrop:
 | 
|---|
| 244 |         if (frameHeight < widgetHeight) {
 | 
|---|
| 245 |             frameWidth *= float(widgetHeight) / float(frameHeight);
 | 
|---|
| 246 |             frameHeight = widgetHeight;
 | 
|---|
| 247 |         }
 | 
|---|
| 248 |         break;
 | 
|---|
| 249 |     case Phonon::VideoWidget::FitInView:
 | 
|---|
| 250 |     default:
 | 
|---|
| 251 |         if (frameHeight > widgetHeight) {
 | 
|---|
| 252 |             frameWidth *= float(widgetHeight) / float(frameHeight);
 | 
|---|
| 253 |             frameHeight = widgetHeight;
 | 
|---|
| 254 |         }
 | 
|---|
| 255 |         break;
 | 
|---|
| 256 |     }
 | 
|---|
| 257 |     drawFrameRect.setSize(QSize(int(frameWidth), int(frameHeight)));
 | 
|---|
| 258 |     drawFrameRect.moveTo(int((widgetWidth - frameWidth) / 2.0f),
 | 
|---|
| 259 |                            int((widgetHeight - frameHeight) / 2.0f));
 | 
|---|
| 260 |     return drawFrameRect;
 | 
|---|
| 261 | }
 | 
|---|
| 262 | 
 | 
|---|
| 263 | void VideoWidget::setScaleMode(Phonon::VideoWidget::ScaleMode scaleMode)
 | 
|---|
| 264 | {
 | 
|---|
| 265 |     m_scaleMode = scaleMode;
 | 
|---|
| 266 |     if (m_renderer)
 | 
|---|
| 267 |         m_renderer->scaleModeChanged(scaleMode);
 | 
|---|
| 268 | }
 | 
|---|
| 269 | 
 | 
|---|
| 270 | qreal VideoWidget::brightness() const
 | 
|---|
| 271 | {
 | 
|---|
| 272 |     return m_brightness;
 | 
|---|
| 273 | }
 | 
|---|
| 274 | 
 | 
|---|
| 275 | qreal clampedValue(qreal val)
 | 
|---|
| 276 | {
 | 
|---|
| 277 |     if (val > 1.0 )
 | 
|---|
| 278 |         return 1.0;
 | 
|---|
| 279 |     else if (val < -1.0)
 | 
|---|
| 280 |         return -1.0;
 | 
|---|
| 281 |     else return val;
 | 
|---|
| 282 | }
 | 
|---|
| 283 | 
 | 
|---|
| 284 | void VideoWidget::setBrightness(qreal newValue)
 | 
|---|
| 285 | {
 | 
|---|
| 286 |     newValue = clampedValue(newValue);
 | 
|---|
| 287 | 
 | 
|---|
| 288 |     if (newValue == m_brightness)
 | 
|---|
| 289 |         return;
 | 
|---|
| 290 | 
 | 
|---|
| 291 |     m_brightness = newValue;
 | 
|---|
| 292 | 
 | 
|---|
| 293 |     if (m_videoBalance)
 | 
|---|
| 294 |         g_object_set(G_OBJECT(m_videoBalance), "brightness", newValue, (const char*)NULL); //gstreamer range is [-1, 1]
 | 
|---|
| 295 | 
 | 
|---|
| 296 | }
 | 
|---|
| 297 | 
 | 
|---|
| 298 | qreal VideoWidget::contrast() const
 | 
|---|
| 299 | {
 | 
|---|
| 300 |     return m_contrast;
 | 
|---|
| 301 | }
 | 
|---|
| 302 | 
 | 
|---|
| 303 | void VideoWidget::setContrast(qreal newValue)
 | 
|---|
| 304 | {
 | 
|---|
| 305 |     newValue = clampedValue(newValue);
 | 
|---|
| 306 | 
 | 
|---|
| 307 |     if (newValue == m_contrast)
 | 
|---|
| 308 |         return;
 | 
|---|
| 309 | 
 | 
|---|
| 310 |     m_contrast = newValue;
 | 
|---|
| 311 | 
 | 
|---|
| 312 |     if (m_videoBalance)
 | 
|---|
| 313 |         g_object_set(G_OBJECT(m_videoBalance), "contrast", (newValue + 1.0), (const char*)NULL); //gstreamer range is [0-2]
 | 
|---|
| 314 | }
 | 
|---|
| 315 | 
 | 
|---|
| 316 | qreal VideoWidget::hue() const
 | 
|---|
| 317 | {
 | 
|---|
| 318 |     return m_hue;
 | 
|---|
| 319 | }
 | 
|---|
| 320 | 
 | 
|---|
| 321 | void VideoWidget::setHue(qreal newValue)
 | 
|---|
| 322 | {
 | 
|---|
| 323 |     if (newValue == m_hue)
 | 
|---|
| 324 |         return;
 | 
|---|
| 325 | 
 | 
|---|
| 326 |     newValue = clampedValue(newValue);
 | 
|---|
| 327 | 
 | 
|---|
| 328 |     m_hue = newValue;
 | 
|---|
| 329 | 
 | 
|---|
| 330 |     if (m_videoBalance)
 | 
|---|
| 331 |         g_object_set(G_OBJECT(m_videoBalance), "hue", newValue, (const char*)NULL); //gstreamer range is [-1, 1]
 | 
|---|
| 332 | }
 | 
|---|
| 333 | 
 | 
|---|
| 334 | qreal VideoWidget::saturation() const
 | 
|---|
| 335 | {
 | 
|---|
| 336 |     return m_saturation;
 | 
|---|
| 337 | }
 | 
|---|
| 338 | 
 | 
|---|
| 339 | void VideoWidget::setSaturation(qreal newValue)
 | 
|---|
| 340 | {
 | 
|---|
| 341 |     newValue = clampedValue(newValue);
 | 
|---|
| 342 | 
 | 
|---|
| 343 |     if (newValue == m_saturation)
 | 
|---|
| 344 |         return;
 | 
|---|
| 345 | 
 | 
|---|
| 346 |     m_saturation = newValue;
 | 
|---|
| 347 | 
 | 
|---|
| 348 |     if (m_videoBalance)
 | 
|---|
| 349 |         g_object_set(G_OBJECT(m_videoBalance), "saturation", newValue + 1.0, (const char*)NULL); //gstreamer range is [0, 2]
 | 
|---|
| 350 | }
 | 
|---|
| 351 | 
 | 
|---|
| 352 | 
 | 
|---|
| 353 | void VideoWidget::setMovieSize(const QSize &size)
 | 
|---|
| 354 | {
 | 
|---|
| 355 |     m_backend->logMessage(QString("New video size %0 x %1").arg(size.width()).arg(size.height()), Backend::Info);
 | 
|---|
| 356 |     if (size == m_movieSize)
 | 
|---|
| 357 |         return;
 | 
|---|
| 358 |     m_movieSize = size;
 | 
|---|
| 359 |     widget()->updateGeometry();
 | 
|---|
| 360 |     widget()->update();
 | 
|---|
| 361 | 
 | 
|---|
| 362 |     if (m_renderer)
 | 
|---|
| 363 |         m_renderer->movieSizeChanged(m_movieSize);
 | 
|---|
| 364 | }
 | 
|---|
| 365 | 
 | 
|---|
| 366 | void VideoWidget::mediaNodeEvent(const MediaNodeEvent *event)
 | 
|---|
| 367 | {
 | 
|---|
| 368 |     switch (event->type()) {
 | 
|---|
| 369 |     case MediaNodeEvent::VideoSizeChanged: {
 | 
|---|
| 370 |             const QSize *size = static_cast<const QSize*>(event->data());
 | 
|---|
| 371 |             setMovieSize(*size);
 | 
|---|
| 372 |         }
 | 
|---|
| 373 |         break;
 | 
|---|
| 374 |     default:
 | 
|---|
| 375 |         break;
 | 
|---|
| 376 |     }
 | 
|---|
| 377 | 
 | 
|---|
| 378 |     // Forward events to renderer
 | 
|---|
| 379 |     if (m_renderer)
 | 
|---|
| 380 |         m_renderer->handleMediaNodeEvent(event);
 | 
|---|
| 381 | }
 | 
|---|
| 382 | 
 | 
|---|
| 383 | }
 | 
|---|
| 384 | } //namespace Phonon::Gstreamer
 | 
|---|
| 385 | 
 | 
|---|
| 386 | QT_END_NAMESPACE
 | 
|---|
| 387 | #endif //QT_NO_PHONON_VIDEO
 | 
|---|
| 388 | 
 | 
|---|
| 389 | #include "moc_videowidget.cpp"
 | 
|---|