ccca43c7d3a13726ffb033969d89cfad361cd0b3
[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 #include "FullScreenVideoWidget.h"
26 #include "HTMLNames.h"
27 #include "HTMLVideoElement.h"
28 #include "MediaPlayerPrivateQt.h"
29 #include "Node.h"
30
31
32 #include <QGraphicsVideoItem>
33 #include <QMediaPlayer>
34
35 namespace WebCore {
36
37 bool DefaultFullScreenVideoHandler::s_shouldForceFullScreenVideoPlayback = false;
38
39 DefaultFullScreenVideoHandler::DefaultFullScreenVideoHandler()
40     : QWebFullScreenVideoHandler()
41     , m_fullScreenWidget(new FullScreenVideoWidget)
42 {
43     connect(m_fullScreenWidget, SIGNAL(didExitFullScreen()), this, SIGNAL(fullScreenClosed()));
44     m_fullScreenWidget->hide();
45 }
46
47 DefaultFullScreenVideoHandler::~DefaultFullScreenVideoHandler()
48 {
49     delete m_fullScreenWidget;
50 }
51
52 bool DefaultFullScreenVideoHandler::requiresFullScreenForVideoPlayback() const
53 {
54     static bool initialized = false;
55     if (!initialized) {
56         QByteArray forceFullScreen = qgetenv("QT_WEBKIT_FORCE_FULLSCREEN_VIDEO");
57         if (!forceFullScreen.isEmpty())
58             s_shouldForceFullScreenVideoPlayback = true;
59
60         initialized = true;
61     }
62
63     return s_shouldForceFullScreenVideoPlayback;
64 }
65
66 void DefaultFullScreenVideoHandler::enterFullScreen(QMediaPlayer* player)
67 {
68     m_fullScreenWidget->show(player);
69 }
70
71 void DefaultFullScreenVideoHandler::exitFullScreen()
72 {
73     m_fullScreenWidget->close();
74 }
75
76 FullScreenVideoQt::FullScreenVideoQt(ChromeClientQt* chromeClient)
77     : m_chromeClient(chromeClient)
78     , m_videoElement(0)
79 {
80     Q_ASSERT(m_chromeClient);
81     m_FullScreenVideoHandler = m_chromeClient->m_platformPlugin.createFullScreenVideoHandler();
82
83     if (!m_FullScreenVideoHandler)
84         m_FullScreenVideoHandler = new DefaultFullScreenVideoHandler;
85
86     if (m_FullScreenVideoHandler)
87         connect(m_FullScreenVideoHandler, SIGNAL(fullScreenClosed()), this, SLOT(aboutToClose()));
88 }
89
90 FullScreenVideoQt::~FullScreenVideoQt()
91 {
92     delete m_FullScreenVideoHandler;
93 }
94
95 void FullScreenVideoQt::enterFullScreenForNode(Node* node)
96 {
97     Q_ASSERT(node);
98     Q_ASSERT(m_FullScreenVideoHandler);
99
100     if (!m_FullScreenVideoHandler)
101         return;
102
103     MediaPlayerPrivateQt* mediaPlayerQt = mediaPlayerForNode(node);
104     mediaPlayerQt->removeVideoItem();
105     m_FullScreenVideoHandler->enterFullScreen(mediaPlayerQt->mediaPlayer());
106 }
107
108 void FullScreenVideoQt::exitFullScreenForNode(Node* node)
109 {
110     Q_ASSERT(node);
111     Q_ASSERT(m_FullScreenVideoHandler);
112
113     if (!m_FullScreenVideoHandler)
114         return;
115
116     m_FullScreenVideoHandler->exitFullScreen();
117     MediaPlayerPrivateQt* mediaPlayerQt = mediaPlayerForNode(node);
118     mediaPlayerQt->restoreVideoItem();
119 }
120
121 void FullScreenVideoQt::aboutToClose()
122 {
123     Q_ASSERT(m_videoElement);
124     m_videoElement->exitFullscreen();
125 }
126
127 MediaPlayerPrivateQt* FullScreenVideoQt::mediaPlayer()
128 {
129     Q_ASSERT(m_videoElement);
130     PlatformMedia platformMedia = m_videoElement->platformMedia();
131     return static_cast<MediaPlayerPrivateQt*>(platformMedia.media.qtMediaPlayer);
132 }
133
134 MediaPlayerPrivateQt* FullScreenVideoQt::mediaPlayerForNode(Node* node)
135 {
136     Q_ASSERT(node);
137     if (node)
138         m_videoElement = static_cast<HTMLVideoElement*>(node);
139     return mediaPlayer();
140 }
141
142 bool FullScreenVideoQt::requiresFullScreenForVideoPlayback()
143 {
144     return m_FullScreenVideoHandler ? m_FullScreenVideoHandler->requiresFullScreenForVideoPlayback() : false;
145 }
146
147 }
148