1
/* This file is part of the KDE project.
3
Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
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.
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.
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/>.
18
#include "videowidget.h"
19
#include "x11renderer.h"
25
#include "mediaobject.h"
27
#include <QtGui/QPalette>
28
#include <QtGui/QApplication>
29
#include <QtGui/QPainter>
32
#include <gst/interfaces/xoverlay.h>
33
#include <gst/interfaces/propertyprobe.h>
42
class OverlayWidget : public QWidget
45
OverlayWidget(VideoWidget *videoWidget, X11Renderer *renderer) :
47
m_videoWidget(videoWidget),
48
m_renderer(renderer) { }
49
void paintEvent(QPaintEvent *) {
50
Phonon::State state = m_videoWidget->root() ? m_videoWidget->root()->state() : Phonon::LoadingState;
51
if (state == Phonon::PlayingState || state == Phonon::PausedState) {
52
m_renderer->windowExposed();
54
QPainter painter(this);
55
painter.fillRect(m_videoWidget->rect(), m_videoWidget->palette().background());
59
VideoWidget *m_videoWidget;
60
X11Renderer *m_renderer;
63
X11Renderer::X11Renderer(VideoWidget *videoWidget)
64
: AbstractRenderer(videoWidget)
66
m_renderWidget = new OverlayWidget(videoWidget, this);
67
videoWidget->backend()->logMessage("Creating X11 overlay renderer");
69
palette.setColor(QPalette::Background, Qt::black);
70
m_videoWidget->setPalette(palette);
71
m_videoWidget->setAutoFillBackground(true);
72
m_renderWidget->setMouseTracking(true);
73
m_videoSink = createVideoSink();
74
aspectRatioChanged(videoWidget->aspectRatio());
78
X11Renderer::~X11Renderer()
80
m_renderWidget->setAttribute(Qt::WA_PaintOnScreen, false);
81
m_renderWidget->setAttribute(Qt::WA_NoSystemBackground, false);
82
delete m_renderWidget;
85
GstElement* X11Renderer::createVideoSink()
87
GstElement *videoSink = gst_element_factory_make ("xvimagesink", NULL);
89
// Check if the xv sink is usable
90
if (gst_element_set_state(videoSink, GST_STATE_READY) != GST_STATE_CHANGE_SUCCESS) {
91
gst_object_unref(GST_OBJECT(videoSink));
94
// Note that this should not really be necessary as these are
95
// default values, though under certain conditions values are retained
96
// even between application instances. (reproducible on 0.10.16/Gutsy)
97
g_object_set(G_OBJECT(videoSink), "brightness", 0, (const char*)NULL);
98
g_object_set(G_OBJECT(videoSink), "contrast", 0, (const char*)NULL);
99
g_object_set(G_OBJECT(videoSink), "hue", 0, (const char*)NULL);
100
g_object_set(G_OBJECT(videoSink), "saturation", 0, (const char*)NULL);
105
videoSink = gst_element_factory_make ("ximagesink", NULL);
107
gst_object_ref (GST_OBJECT (videoSink)); //Take ownership
108
gst_object_sink (GST_OBJECT (videoSink));
113
void X11Renderer::handleMediaNodeEvent(const MediaNodeEvent *event)
115
switch (event->type()) {
116
case MediaNodeEvent::SourceChanged:
117
setOverlay(); // We need to do this whenever the pipeline is reset
118
break; // otherwise the videosink will open in its own window
125
void X11Renderer::aspectRatioChanged(Phonon::VideoWidget::AspectRatio)
127
if (m_renderWidget) {
128
m_renderWidget->setGeometry(m_videoWidget->calculateDrawFrameRect());
132
void X11Renderer::scaleModeChanged(Phonon::VideoWidget::ScaleMode)
134
if (m_renderWidget) {
135
m_renderWidget->setGeometry(m_videoWidget->calculateDrawFrameRect());
139
void X11Renderer::movieSizeChanged(const QSize &movieSize)
143
if (m_renderWidget) {
144
m_renderWidget->setGeometry(m_videoWidget->calculateDrawFrameRect());
148
bool X11Renderer::eventFilter(QEvent *e)
150
if (e->type() == QEvent::Show) {
151
// Setting these values ensures smooth resizing since it
152
// will prevent the system from clearing the background
153
m_renderWidget->setAttribute(Qt::WA_NoSystemBackground, true);
154
m_renderWidget->setAttribute(Qt::WA_PaintOnScreen, true);
156
} else if (e->type() == QEvent::Resize) {
157
// This is a workaround for missing background repaints
158
// when reducing window size
159
m_renderWidget->setGeometry(m_videoWidget->calculateDrawFrameRect());
165
void X11Renderer::handlePaint(QPaintEvent *)
167
QPainter painter(m_videoWidget);
168
painter.fillRect(m_videoWidget->rect(), m_videoWidget->palette().background());
171
void X11Renderer::setOverlay()
173
if (m_videoSink && GST_IS_X_OVERLAY(m_videoSink)) {
174
WId windowId = m_renderWidget->winId();
175
// Even if we have created a winId at this point, other X applications
176
// need to be aware of it.
177
QApplication::syncX();
178
gst_x_overlay_set_xwindow_id ( GST_X_OVERLAY(m_videoSink) , windowId );
184
void X11Renderer::windowExposed()
186
QApplication::syncX();
187
if (m_videoSink && GST_IS_X_OVERLAY(m_videoSink))
188
gst_x_overlay_expose(GST_X_OVERLAY(m_videoSink));
192
} //namespace Phonon::Gstreamer