2011-05-25 Alexis Menard <alexis.menard@openbossa.org>
[WebKit-https.git] / Source / WebKit / qt / WebCoreSupport / FullScreenVideoQt.cpp
1 /*
2  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
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 GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20
21 #include "config.h"
22 #include "FullScreenVideoQt.h"
23
24 #include "ChromeClientQt.h"
25 #if USE(QT_MULTIMEDIA)
26 #include "FullScreenVideoWidget.h"
27 #include "MediaPlayerPrivateQt.h"
28 #endif
29 #include "HTMLNames.h"
30 #include "HTMLVideoElement.h"
31 #include "Node.h"
32
33 #if USE(GSTREAMER)
34 #include "GStreamerGWorld.h"
35 #include "PlatformVideoWindowPrivate.h"
36 #endif
37
38 #if USE(QT_MULTIMEDIA)
39 #include <QGraphicsVideoItem>
40 #include <QMediaPlayer>
41 #endif
42 #include <QWidget>
43
44 namespace WebCore {
45
46 #if USE(GSTREAMER)
47 GStreamerFullScreenVideoHandler::GStreamerFullScreenVideoHandler()
48     : m_videoElement(0)
49     , m_fullScreenWidget(0)
50 {
51 }
52
53 void GStreamerFullScreenVideoHandler::setVideoElement(HTMLVideoElement* element)
54 {
55     m_videoElement = element;
56 }
57
58 void GStreamerFullScreenVideoHandler::enterFullScreen()
59 {
60     if (m_videoElement->platformMedia().type != WebCore::PlatformMedia::GStreamerGWorldType)
61         return;
62
63     GStreamerGWorld* gstreamerGWorld = m_videoElement->platformMedia().media.gstreamerGWorld;
64
65     if (!gstreamerGWorld->enterFullscreen())
66         return;
67
68     m_fullScreenWidget = reinterpret_cast<FullScreenVideoWindow*>(gstreamerGWorld->platformVideoWindow()->window());
69     m_fullScreenWidget->setVideoElement(m_videoElement);
70     connect(m_fullScreenWidget, SIGNAL(closed()), this, SLOT(windowClosed()));
71     m_fullScreenWidget->showFullScreen();
72 }
73
74 void GStreamerFullScreenVideoHandler::windowClosed()
75 {
76     m_videoElement->exitFullscreen();
77 }
78
79 void GStreamerFullScreenVideoHandler::exitFullScreen()
80 {
81     if (m_videoElement->platformMedia().type == WebCore::PlatformMedia::GStreamerGWorldType)
82         m_videoElement->platformMedia().media.gstreamerGWorld->exitFullscreen();
83
84     m_fullScreenWidget->setVideoElement(0);
85     m_fullScreenWidget->close();
86 }
87 #endif
88
89 #if USE(QT_MULTIMEDIA)
90 bool DefaultFullScreenVideoHandler::s_shouldForceFullScreenVideoPlayback = false;
91
92 DefaultFullScreenVideoHandler::DefaultFullScreenVideoHandler()
93     : QWebFullScreenVideoHandler()
94     , m_fullScreenWidget(new FullScreenVideoWidget)
95 {
96     connect(m_fullScreenWidget, SIGNAL(didExitFullScreen()), this, SIGNAL(fullScreenClosed()));
97     m_fullScreenWidget->hide();
98
99     m_fullScreenWidget->close();
100 }
101
102 DefaultFullScreenVideoHandler::~DefaultFullScreenVideoHandler()
103 {
104     delete m_fullScreenWidget;
105 }
106
107 bool DefaultFullScreenVideoHandler::requiresFullScreenForVideoPlayback() const
108 {
109     static bool initialized = false;
110     if (!initialized) {
111         QByteArray forceFullScreen = qgetenv("QT_WEBKIT_FORCE_FULLSCREEN_VIDEO");
112         if (!forceFullScreen.isEmpty())
113             s_shouldForceFullScreenVideoPlayback = true;
114
115         initialized = true;
116     }
117
118     return s_shouldForceFullScreenVideoPlayback;
119 }
120
121 void DefaultFullScreenVideoHandler::enterFullScreen(QMediaPlayer* player)
122 {
123     m_fullScreenWidget->show(player);
124 }
125
126 void DefaultFullScreenVideoHandler::exitFullScreen()
127 {
128     m_fullScreenWidget->close();
129 }
130 #endif
131
132 FullScreenVideoQt::FullScreenVideoQt(ChromeClientQt* chromeClient)
133     : m_chromeClient(chromeClient)
134     , m_videoElement(0)
135 {
136     Q_ASSERT(m_chromeClient);
137
138 #if USE(QT_MULTIMEDIA)
139     m_FullScreenVideoHandler = m_chromeClient->m_platformPlugin.createFullScreenVideoHandler().leakPtr();
140     if (!m_FullScreenVideoHandler)
141         m_FullScreenVideoHandler = new DefaultFullScreenVideoHandler;
142
143     if (m_FullScreenVideoHandler)
144         connect(m_FullScreenVideoHandler, SIGNAL(fullScreenClosed()), this, SLOT(aboutToClose()));
145 #endif
146
147 #if USE(GSTREAMER)
148     m_FullScreenVideoHandlerGStreamer = new GStreamerFullScreenVideoHandler;
149 #endif
150 }
151
152 FullScreenVideoQt::~FullScreenVideoQt()
153 {
154 #if USE(QT_MULTIMEDIA)
155     delete m_FullScreenVideoHandler;
156 #endif
157 #if USE(GSTREAMER)
158     delete m_FullScreenVideoHandlerGStreamer;
159 #endif
160 }
161
162 void FullScreenVideoQt::enterFullScreenForNode(Node* node)
163 {
164     Q_ASSERT(node);
165     m_videoElement = static_cast<HTMLVideoElement*>(node);
166
167 #if USE(QT_MULTIMEDIA)
168     Q_ASSERT(m_FullScreenVideoHandler);
169     HTMLVideoElement* videoElement = static_cast<HTMLVideoElement*>(node);
170     PlatformMedia platformMedia = videoElement->platformMedia();
171
172     ASSERT(platformMedia.type == PlatformMedia::QtMediaPlayerType);
173     if (platformMedia.type != PlatformMedia::QtMediaPlayerType)
174         return;
175
176     if (!m_FullScreenVideoHandler)
177         return;
178
179     MediaPlayerPrivateQt* mediaPlayerQt = mediaPlayer();
180     mediaPlayerQt->removeVideoItem();
181     m_FullScreenVideoHandler->enterFullScreen(mediaPlayerQt->mediaPlayer());
182 #endif
183
184 #if USE(GSTREAMER)
185     m_FullScreenVideoHandlerGStreamer->setVideoElement(m_videoElement);
186     m_FullScreenVideoHandlerGStreamer->enterFullScreen();
187 #endif
188 }
189
190 void FullScreenVideoQt::exitFullScreenForNode(Node* node)
191 {
192     Q_ASSERT(node);
193
194 #if USE(QT_MULTIMEDIA)
195     HTMLVideoElement* videoElement = static_cast<HTMLVideoElement*>(node);
196     PlatformMedia platformMedia = videoElement->platformMedia();
197
198     ASSERT(platformMedia.type == PlatformMedia::QtMediaPlayerType);
199     if (platformMedia.type != PlatformMedia::QtMediaPlayerType)
200         return;
201
202     Q_ASSERT(m_FullScreenVideoHandler);
203
204     if (!m_FullScreenVideoHandler)
205         return;
206
207     m_FullScreenVideoHandler->exitFullScreen();
208     MediaPlayerPrivateQt* mediaPlayerQt = mediaPlayer();
209     mediaPlayerQt->restoreVideoItem();
210 #endif
211 #if USE(GSTREAMER)
212     m_FullScreenVideoHandlerGStreamer->exitFullScreen();
213 #endif
214 }
215
216 void FullScreenVideoQt::aboutToClose()
217 {
218     Q_ASSERT(m_videoElement);
219     m_videoElement->exitFullscreen();
220 }
221
222 #if USE(QT_MULTIMEDIA)
223 MediaPlayerPrivateQt* FullScreenVideoQt::mediaPlayer()
224 {
225     Q_ASSERT(m_videoElement);
226     PlatformMedia platformMedia = m_videoElement->platformMedia();
227     return static_cast<MediaPlayerPrivateQt*>(platformMedia.media.qtMediaPlayer);
228 }
229 #endif
230
231 bool FullScreenVideoQt::requiresFullScreenForVideoPlayback()
232 {
233 #if USE(QT_MULTIMEDIA)
234     return m_FullScreenVideoHandler ? m_FullScreenVideoHandler->requiresFullScreenForVideoPlayback() : false;
235 #endif
236 #if USE(GSTREAMER)
237     return false;
238 #else
239     return false;
240 #endif
241 }
242
243 bool FullScreenVideoQt::isValid() const
244 {
245 #if USE(QT_MULTIMEDIA)
246     return m_FullScreenVideoHandler;
247 #endif
248 #if USE(GSTREAMER)
249     return m_FullScreenVideoHandlerGStreamer;
250 #else
251     return 0;
252 #endif
253 }
254
255 }
256