2011-04-06 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();
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     Q_ASSERT(m_FullScreenVideoHandler);
166
167     m_videoElement = static_cast<HTMLVideoElement*>(node);
168
169 #if USE(QT_MULTIMEDIA)
170     HTMLVideoElement* videoElement = static_cast<HTMLVideoElement*>(node);
171     PlatformMedia platformMedia = videoElement->platformMedia();
172
173     ASSERT(platformMedia.type == PlatformMedia::QtMediaPlayerType);
174     if (platformMedia.type != PlatformMedia::QtMediaPlayerType)
175         return;
176
177     if (!m_FullScreenVideoHandler)
178         return;
179
180     MediaPlayerPrivateQt* mediaPlayerQt = mediaPlayer();
181     mediaPlayerQt->removeVideoItem();
182     m_FullScreenVideoHandler->enterFullScreen(mediaPlayerQt->mediaPlayer());
183 #endif
184
185 #if USE(GSTREAMER)
186     m_FullScreenVideoHandlerGStreamer->setVideoElement(m_videoElement);
187     m_FullScreenVideoHandlerGStreamer->enterFullScreen();
188 #endif
189 }
190
191 void FullScreenVideoQt::exitFullScreenForNode(Node* node)
192 {
193     Q_ASSERT(node);
194
195 #if USE(QT_MULTIMEDIA)
196     HTMLVideoElement* videoElement = static_cast<HTMLVideoElement*>(node);
197     PlatformMedia platformMedia = videoElement->platformMedia();
198
199     ASSERT(platformMedia.type == PlatformMedia::QtMediaPlayerType);
200     if (platformMedia.type != PlatformMedia::QtMediaPlayerType)
201         return;
202
203     Q_ASSERT(m_FullScreenVideoHandler);
204
205     if (!m_FullScreenVideoHandler)
206         return;
207
208     m_FullScreenVideoHandler->exitFullScreen();
209     MediaPlayerPrivateQt* mediaPlayerQt = mediaPlayer();
210     mediaPlayerQt->restoreVideoItem();
211 #endif
212 #if USE(GSTREAMER)
213     m_FullScreenVideoHandlerGStreamer->exitFullScreen();
214 #endif
215 }
216
217 void FullScreenVideoQt::aboutToClose()
218 {
219     Q_ASSERT(m_videoElement);
220     m_videoElement->exitFullscreen();
221 }
222
223 #if USE(QT_MULTIMEDIA)
224 MediaPlayerPrivateQt* FullScreenVideoQt::mediaPlayer()
225 {
226     Q_ASSERT(m_videoElement);
227     PlatformMedia platformMedia = m_videoElement->platformMedia();
228     return static_cast<MediaPlayerPrivateQt*>(platformMedia.media.qtMediaPlayer);
229 }
230 #endif
231
232 bool FullScreenVideoQt::requiresFullScreenForVideoPlayback()
233 {
234 #if USE(QT_MULTIMEDIA)
235     return m_FullScreenVideoHandler ? m_FullScreenVideoHandler->requiresFullScreenForVideoPlayback() : false;
236 #endif
237 #if USE(GSTREAMER)
238     return false;
239 #endif
240 }
241
242 bool FullScreenVideoQt::isValid() const
243 {
244 #if USE(QT_MULTIMEDIA)
245     return m_FullScreenVideoHandler;
246 #endif
247 #if USE(GSTREAMER)
248     return m_FullScreenVideoHandlerGStreamer;
249 #endif
250 }
251
252 }
253