initial import
[vuplus_webkit] / Source / WebKit / qt / symbian / platformplugin / HTML5VideoPlugin.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 "HTML5VideoPlugin.h"
22
23 #include <QtPlugin>
24
25 HTML5FullScreenVideoHandler::HTML5FullScreenVideoHandler()
26     : m_videoWidget(0)
27     , m_mediaPlayer(0)
28     , m_fullScreen(false)
29     , m_savedOrientation(CAknAppUi::EAppUiOrientationUnspecified)
30 {
31 }
32
33 void HTML5FullScreenVideoHandler::enterFullScreen(QMediaPlayer *player)
34 {
35     if (!player)
36         return;
37
38     m_videoWidget = new HTML5VideoWidget();
39     if (!m_videoWidget)
40         return;
41
42     m_videoWidget->setDuration(player->duration() / 1000);
43
44     CAknAppUi* appUi = dynamic_cast<CAknAppUi*>(CEikonEnv::Static()->AppUi());
45     if (appUi) {
46         m_savedOrientation = appUi->Orientation();
47         appUi->SetOrientationL(CAknAppUi::EAppUiOrientationLandscape);
48     }
49
50     m_mediaPlayer = player;
51     connect(m_mediaPlayer, SIGNAL(positionChanged(qint64)), m_videoWidget, SLOT(onPositionChanged(qint64)));
52     connect(m_mediaPlayer, SIGNAL(durationChanged(qint64)), m_videoWidget, SLOT(setDuration(qint64)));
53     connect(m_mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(onPlayerStateChanged(QMediaPlayer::State)));
54     connect(m_mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(onPlayerError(QMediaPlayer::Error)));
55     connect(m_mediaPlayer, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(onMediaStatusChanged(QMediaPlayer::MediaStatus)));
56     connect(m_videoWidget, SIGNAL(positionChangedByUser(qint64)), m_mediaPlayer, SLOT(setPosition(qint64)));
57     connect(m_videoWidget, SIGNAL(closeClicked()), this, SIGNAL(fullScreenClosed()));
58     connect(m_videoWidget, SIGNAL(muted(bool)), m_mediaPlayer, SLOT(setMuted(bool)));
59     connect(m_videoWidget, SIGNAL(volumeChanged(int)), m_mediaPlayer, SLOT(setVolume(int)));
60     connect(m_videoWidget, SIGNAL(pauseClicked()), m_mediaPlayer, SLOT(pause()));
61     connect(m_videoWidget, SIGNAL(playClicked()), m_mediaPlayer, SLOT(play()));
62
63     m_mediaPlayer->setVideoOutput(m_videoWidget);
64
65     m_videoWidget->setVolume(m_mediaPlayer->volume());
66     m_videoWidget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
67     m_videoWidget->showFullScreen();
68     m_fullScreen = true;
69
70     // Handle current Media Status and Media Player error.
71     onMediaStatusChanged(m_mediaPlayer->mediaStatus());
72     onPlayerError(m_mediaPlayer->error());
73 }
74
75 void HTML5FullScreenVideoHandler::exitFullScreen()
76 {
77     m_videoWidget->hide();
78     m_mediaPlayer->disconnect(m_videoWidget);
79     m_mediaPlayer->disconnect(this);
80     delete m_videoWidget;
81     m_videoWidget = 0;
82     if (m_mediaPlayer->state() == QMediaPlayer::PlayingState)
83         m_mediaPlayer->pause();
84     m_fullScreen = false;
85
86     CAknAppUi* appUi = dynamic_cast<CAknAppUi*>(CEikonEnv::Static()->AppUi());
87     if (appUi) 
88         appUi->SetOrientationL(m_savedOrientation);
89 }
90
91 void HTML5FullScreenVideoHandler::onPlayerError(QMediaPlayer::Error error)
92 {
93     switch (error) {
94     case QMediaPlayer::NoError:
95         break;
96     default:
97         m_videoWidget->onPlayerError();
98     }
99 }
100
101 void HTML5FullScreenVideoHandler::onPlayerStateChanged(QMediaPlayer::State state)
102 {
103     if (state == QMediaPlayer::StoppedState)
104         m_videoWidget->onPlayerStopped();
105 }
106
107 void HTML5FullScreenVideoHandler::onMediaStatusChanged(QMediaPlayer::MediaStatus status)
108 {
109     switch (status) {
110     case QMediaPlayer::EndOfMedia:
111         m_videoWidget->onEndOfMedia();
112         break;
113     case QMediaPlayer::StalledMedia:
114     case QMediaPlayer::LoadingMedia:
115     case QMediaPlayer::BufferingMedia:
116         m_videoWidget->onBufferingMedia();
117         break;
118     case QMediaPlayer::LoadedMedia:
119     case QMediaPlayer::BufferedMedia:
120         m_videoWidget->onBufferedMedia();
121         break;
122     default:
123         break;
124     }
125     return;
126 }
127
128 bool HTML5VideoPlugin::supportsExtension(Extension extension) const
129 {
130     switch (extension) {
131     case FullScreenVideoPlayer:
132         return true;
133     default:
134         return false;
135     }
136 }
137
138 QObject* HTML5VideoPlugin::createExtension(Extension extension) const
139 {
140     HTML5FullScreenVideoHandler* videoHandler = 0;
141     switch (extension) {
142     case FullScreenVideoPlayer:
143         videoHandler = new HTML5FullScreenVideoHandler;
144         break;
145     default:
146         break;
147     }
148     return videoHandler;
149 }