1/*
2 *  Copyright (C) 2013 Igalia S.L
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#include "config.h"
21
22#if ENABLE(VIDEO) && USE(GSTREAMER) && USE(NATIVE_FULLSCREEN_VIDEO)
23
24#include "FullscreenVideoControllerGStreamer.h"
25
26#if PLATFORM(GTK)
27#include "FullscreenVideoControllerGtk.h"
28#endif
29
30#include "GStreamerGWorld.h"
31#include "MediaPlayer.h"
32#include "MediaPlayerPrivateGStreamerBase.h"
33#include <gst/gst.h>
34#include <wtf/text/CString.h>
35
36#define VOLUME_UP_OFFSET 0.05 // 5%
37#define VOLUME_DOWN_OFFSET 0.05 // 5%
38
39namespace WebCore {
40
41void playerVolumeChangedCallback(GObject *element, GParamSpec *pspec, FullscreenVideoControllerGStreamer* controller)
42{
43    controller->volumeChanged();
44}
45
46void playerMuteChangedCallback(GObject *element, GParamSpec *pspec, FullscreenVideoControllerGStreamer* controller)
47{
48    controller->muteChanged();
49}
50
51PassOwnPtr<FullscreenVideoControllerGStreamer> FullscreenVideoControllerGStreamer::create(MediaPlayerPrivateGStreamerBase* player)
52{
53#if PLATFORM(GTK)
54   return adoptPtr(new FullscreenVideoControllerGtk(player));
55#else
56   return nullptr;
57#endif
58}
59
60FullscreenVideoControllerGStreamer::FullscreenVideoControllerGStreamer(MediaPlayerPrivateGStreamerBase* player)
61    : m_player(player)
62    , m_client(player->mediaPlayer()->mediaPlayerClient())
63    , m_gstreamerGWorld(player->platformMedia().media.gstreamerGWorld)
64    , m_playerVolumeSignalHandler(0)
65    , m_playerMuteSignalHandler(0)
66{
67}
68
69FullscreenVideoControllerGStreamer::~FullscreenVideoControllerGStreamer()
70{
71    exitFullscreen();
72}
73
74void FullscreenVideoControllerGStreamer::enterFullscreen()
75{
76    if (!m_gstreamerGWorld)
77        return;
78
79    if (!m_gstreamerGWorld->enterFullscreen())
80        return;
81
82    initializeWindow();
83
84    GstElement* pipeline = m_gstreamerGWorld->pipeline();
85    m_playerVolumeSignalHandler = g_signal_connect(pipeline, "notify::volume", G_CALLBACK(playerVolumeChangedCallback), this);
86    m_playerMuteSignalHandler = g_signal_connect(pipeline, "notify::mute", G_CALLBACK(playerMuteChangedCallback), this);
87}
88
89void FullscreenVideoControllerGStreamer::exitFullscreen()
90{
91    destroyWindow();
92
93    GstElement* pipeline = m_gstreamerGWorld->pipeline();
94    if (m_playerVolumeSignalHandler) {
95        g_signal_handler_disconnect(pipeline, m_playerVolumeSignalHandler);
96        m_playerVolumeSignalHandler = 0;
97    }
98
99    if (m_playerMuteSignalHandler) {
100        g_signal_handler_disconnect(pipeline, m_playerMuteSignalHandler);
101        m_playerMuteSignalHandler = 0;
102    }
103
104    m_gstreamerGWorld->exitFullscreen();
105}
106
107void FullscreenVideoControllerGStreamer::exitOnUserRequest()
108{
109    m_client->mediaPlayerExitFullscreen();
110}
111
112void FullscreenVideoControllerGStreamer::togglePlay()
113{
114    if (m_client->mediaPlayerIsPaused())
115        m_client->mediaPlayerPlay();
116    else
117        m_client->mediaPlayerPause();
118    playStateChanged();
119}
120
121void FullscreenVideoControllerGStreamer::increaseVolume()
122{
123    setVolume(m_player->volume() + VOLUME_UP_OFFSET);
124}
125
126void FullscreenVideoControllerGStreamer::decreaseVolume()
127{
128    setVolume(m_player->volume() - VOLUME_DOWN_OFFSET);
129}
130
131void FullscreenVideoControllerGStreamer::setVolume(float volume)
132{
133    volume = CLAMP(volume, 0.0, 1.0);
134    m_player->setVolume(volume);
135}
136
137String FullscreenVideoControllerGStreamer::timeToString(float time)
138{
139    if (!std::isfinite(time))
140        time = 0;
141    int seconds = fabsf(time);
142    int hours = seconds / (60 * 60);
143    int minutes = (seconds / 60) % 60;
144    seconds %= 60;
145
146    if (hours) {
147        if (hours > 9)
148            return String::format("%s%02d:%02d:%02d", (time < 0 ? "-" : ""), hours, minutes, seconds);
149        return String::format("%s%01d:%02d:%02d", (time < 0 ? "-" : ""), hours, minutes, seconds);
150    }
151
152    return String::format("%s%02d:%02d", (time < 0 ? "-" : ""), minutes, seconds);
153}
154
155} // namespace WebCore
156#endif
157