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