initial import
[vuplus_webkit] / 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(QTKIT)
39 #include "QTKitFullScreenVideoHandler.h"
40 #endif
41
42 #if USE(QT_MULTIMEDIA)
43 #include <QGraphicsVideoItem>
44 #include <QMediaPlayer>
45 #endif
46 #include <QWidget>
47
48 namespace WebCore {
49
50 #if USE(GSTREAMER)
51 GStreamerFullScreenVideoHandler::GStreamerFullScreenVideoHandler()
52     : m_videoElement(0)
53     , m_fullScreenWidget(0)
54 {
55 }
56
57 void GStreamerFullScreenVideoHandler::setVideoElement(HTMLVideoElement* element)
58 {
59     m_videoElement = element;
60 }
61
62 void GStreamerFullScreenVideoHandler::enterFullScreen()
63 {
64     if (m_videoElement->platformMedia().type != WebCore::PlatformMedia::GStreamerGWorldType)
65         return;
66
67     GStreamerGWorld* gstreamerGWorld = m_videoElement->platformMedia().media.gstreamerGWorld;
68
69     if (!gstreamerGWorld->enterFullscreen())
70         return;
71
72     m_fullScreenWidget = reinterpret_cast<FullScreenVideoWindow*>(gstreamerGWorld->platformVideoWindow()->window());
73     m_fullScreenWidget->setVideoElement(m_videoElement);
74     connect(m_fullScreenWidget, SIGNAL(closed()), this, SLOT(windowClosed()));
75     m_fullScreenWidget->showFullScreen();
76 }
77
78 void GStreamerFullScreenVideoHandler::windowClosed()
79 {
80     m_videoElement->exitFullscreen();
81 }
82
83 void GStreamerFullScreenVideoHandler::exitFullScreen()
84 {
85     if (m_videoElement->platformMedia().type == WebCore::PlatformMedia::GStreamerGWorldType)
86         m_videoElement->platformMedia().media.gstreamerGWorld->exitFullscreen();
87
88     m_fullScreenWidget->setVideoElement(0);
89     m_fullScreenWidget->close();
90 }
91 #endif
92
93 #if USE(QT_MULTIMEDIA)
94 bool DefaultFullScreenVideoHandler::s_shouldForceFullScreenVideoPlayback = false;
95
96 DefaultFullScreenVideoHandler::DefaultFullScreenVideoHandler()
97     : QWebFullScreenVideoHandler()
98     , m_fullScreenWidget(new FullScreenVideoWidget)
99 {
100     connect(m_fullScreenWidget, SIGNAL(didExitFullScreen()), this, SIGNAL(fullScreenClosed()));
101     m_fullScreenWidget->hide();
102
103     m_fullScreenWidget->close();
104 }
105
106 DefaultFullScreenVideoHandler::~DefaultFullScreenVideoHandler()
107 {
108     delete m_fullScreenWidget;
109 }
110
111 bool DefaultFullScreenVideoHandler::requiresFullScreenForVideoPlayback() const
112 {
113     static bool initialized = false;
114     if (!initialized) {
115         QByteArray forceFullScreen = qgetenv("QT_WEBKIT_FORCE_FULLSCREEN_VIDEO");
116         if (!forceFullScreen.isEmpty())
117             s_shouldForceFullScreenVideoPlayback = true;
118
119         initialized = true;
120     }
121
122     return s_shouldForceFullScreenVideoPlayback;
123 }
124
125 void DefaultFullScreenVideoHandler::enterFullScreen(QMediaPlayer* player)
126 {
127     m_fullScreenWidget->show(player);
128 }
129
130 void DefaultFullScreenVideoHandler::exitFullScreen()
131 {
132     m_fullScreenWidget->close();
133 }
134 #endif
135
136 FullScreenVideoQt::FullScreenVideoQt(ChromeClientQt* chromeClient)
137     : m_chromeClient(chromeClient)
138     , m_videoElement(0)
139 {
140     Q_ASSERT(m_chromeClient);
141
142 #if USE(QT_MULTIMEDIA)
143     m_FullScreenVideoHandler = m_chromeClient->m_platformPlugin.createFullScreenVideoHandler().leakPtr();
144     if (!m_FullScreenVideoHandler)
145         m_FullScreenVideoHandler = new DefaultFullScreenVideoHandler;
146
147     if (m_FullScreenVideoHandler)
148         connect(m_FullScreenVideoHandler, SIGNAL(fullScreenClosed()), this, SLOT(aboutToClose()));
149 #endif
150
151 #if USE(GSTREAMER)
152     m_FullScreenVideoHandlerGStreamer = new GStreamerFullScreenVideoHandler;
153 #endif
154
155 #if USE(QTKIT)
156     m_FullScreenVideoHandlerQTKit = new QTKitFullScreenVideoHandler;
157 #endif
158 }
159
160 FullScreenVideoQt::~FullScreenVideoQt()
161 {
162 #if USE(QT_MULTIMEDIA)
163     delete m_FullScreenVideoHandler;
164 #endif
165 #if USE(GSTREAMER)
166     delete m_FullScreenVideoHandlerGStreamer;
167 #endif
168 #if USE(QTKIT)
169     delete m_FullScreenVideoHandlerQTKit;
170 #endif
171 }
172
173 void FullScreenVideoQt::enterFullScreenForNode(Node* node)
174 {
175     Q_ASSERT(node);
176     m_videoElement = static_cast<HTMLVideoElement*>(node);
177
178 #if USE(QT_MULTIMEDIA)
179     Q_ASSERT(m_FullScreenVideoHandler);
180     HTMLVideoElement* videoElement = static_cast<HTMLVideoElement*>(node);
181     PlatformMedia platformMedia = videoElement->platformMedia();
182
183     ASSERT(platformMedia.type == PlatformMedia::QtMediaPlayerType);
184     if (platformMedia.type != PlatformMedia::QtMediaPlayerType)
185         return;
186
187     if (!m_FullScreenVideoHandler)
188         return;
189
190     MediaPlayerPrivateQt* mediaPlayerQt = mediaPlayer();
191     mediaPlayerQt->removeVideoItem();
192     m_FullScreenVideoHandler->enterFullScreen(mediaPlayerQt->mediaPlayer());
193 #endif
194
195 #if USE(GSTREAMER)
196     m_FullScreenVideoHandlerGStreamer->setVideoElement(m_videoElement);
197     m_FullScreenVideoHandlerGStreamer->enterFullScreen();
198 #endif
199
200 #if USE(QTKIT)
201     m_FullScreenVideoHandlerQTKit->enterFullScreen(m_videoElement);
202 #endif
203 }
204
205 void FullScreenVideoQt::exitFullScreenForNode(Node* node)
206 {
207     Q_ASSERT(node);
208
209 #if USE(QT_MULTIMEDIA)
210     HTMLVideoElement* videoElement = static_cast<HTMLVideoElement*>(node);
211     PlatformMedia platformMedia = videoElement->platformMedia();
212
213     ASSERT(platformMedia.type == PlatformMedia::QtMediaPlayerType);
214     if (platformMedia.type != PlatformMedia::QtMediaPlayerType)
215         return;
216
217     Q_ASSERT(m_FullScreenVideoHandler);
218
219     if (!m_FullScreenVideoHandler)
220         return;
221
222     m_FullScreenVideoHandler->exitFullScreen();
223     MediaPlayerPrivateQt* mediaPlayerQt = mediaPlayer();
224     mediaPlayerQt->restoreVideoItem();
225 #endif
226 #if USE(GSTREAMER)
227     m_FullScreenVideoHandlerGStreamer->exitFullScreen();
228 #endif
229
230 #if USE(QTKIT)
231     m_FullScreenVideoHandlerQTKit->exitFullScreen();
232 #endif
233
234 }
235
236 void FullScreenVideoQt::aboutToClose()
237 {
238     Q_ASSERT(m_videoElement);
239     m_videoElement->exitFullscreen();
240 }
241
242 #if USE(QT_MULTIMEDIA)
243 MediaPlayerPrivateQt* FullScreenVideoQt::mediaPlayer()
244 {
245     Q_ASSERT(m_videoElement);
246     PlatformMedia platformMedia = m_videoElement->platformMedia();
247     return static_cast<MediaPlayerPrivateQt*>(platformMedia.media.qtMediaPlayer);
248 }
249 #endif
250
251 bool FullScreenVideoQt::requiresFullScreenForVideoPlayback()
252 {
253 #if USE(QT_MULTIMEDIA)
254     return m_FullScreenVideoHandler ? m_FullScreenVideoHandler->requiresFullScreenForVideoPlayback() : false;
255 #else
256     return false;
257 #endif
258 }
259
260 bool FullScreenVideoQt::isValid() const
261 {
262 #if USE(QT_MULTIMEDIA)
263     return m_FullScreenVideoHandler;
264 #endif
265 #if USE(GSTREAMER)
266     return m_FullScreenVideoHandlerGStreamer;
267 #elif USE(QTKIT)
268     return m_FullScreenVideoHandlerQTKit;
269 #else
270     return 0;
271 #endif
272 }
273
274 }
275