Add optimized fullscreen mode.
[WebKit-https.git] / Source / WebCore / Modules / mediacontrols / MediaControlsHost.cpp
1 /*
2  * Copyright (C) 2013, 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27
28 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
29
30 #include "MediaControlsHost.h"
31
32 #include "CaptionUserPreferences.h"
33 #include "Element.h"
34 #include "HTMLMediaElement.h"
35 #include "Logging.h"
36 #include "MediaControlElements.h"
37 #include "Page.h"
38 #include "PageGroup.h"
39 #include "TextTrack.h"
40 #include "TextTrackList.h"
41 #include <runtime/JSCJSValueInlines.h>
42
43 namespace WebCore {
44
45 const AtomicString& MediaControlsHost::automaticKeyword()
46 {
47     DEPRECATED_DEFINE_STATIC_LOCAL(const AtomicString, automatic, ("automatic", AtomicString::ConstructFromLiteral));
48     return automatic;
49 }
50
51 const AtomicString& MediaControlsHost::forcedOnlyKeyword()
52 {
53     DEPRECATED_DEFINE_STATIC_LOCAL(const AtomicString, forcedOn, ("forced-only", AtomicString::ConstructFromLiteral));
54     return forcedOn;
55 }
56
57 const AtomicString& MediaControlsHost::alwaysOnKeyword()
58 {
59     DEPRECATED_DEFINE_STATIC_LOCAL(const AtomicString, alwaysOn, ("always-on", AtomicString::ConstructFromLiteral));
60     return alwaysOn;
61 }
62
63 PassRefPtr<MediaControlsHost> MediaControlsHost::create(HTMLMediaElement* mediaElement)
64 {
65     return adoptRef(new MediaControlsHost(mediaElement));
66 }
67
68 MediaControlsHost::MediaControlsHost(HTMLMediaElement* mediaElement)
69     : m_mediaElement(mediaElement)
70 {
71     ASSERT(mediaElement);
72 }
73
74 MediaControlsHost::~MediaControlsHost()
75 {
76 }
77
78 Vector<RefPtr<TextTrack>> MediaControlsHost::sortedTrackListForMenu(TextTrackList* trackList)
79 {
80     if (!trackList)
81         return Vector<RefPtr<TextTrack>>();
82
83     Page* page = m_mediaElement->document().page();
84     if (!page)
85         return Vector<RefPtr<TextTrack>>();
86
87     CaptionUserPreferences* captionPreferences = page->group().captionPreferences();
88     return captionPreferences->sortedTrackListForMenu(trackList);
89 }
90
91 Vector<RefPtr<AudioTrack>> MediaControlsHost::sortedTrackListForMenu(AudioTrackList* trackList)
92 {
93     if (!trackList)
94         return Vector<RefPtr<AudioTrack>>();
95
96     Page* page = m_mediaElement->document().page();
97     if (!page)
98         return Vector<RefPtr<AudioTrack>>();
99
100     CaptionUserPreferences* captionPreferences = page->group().captionPreferences();
101     return captionPreferences->sortedTrackListForMenu(trackList);
102 }
103
104 String MediaControlsHost::displayNameForTrack(TextTrack* track)
105 {
106     if (!track)
107         return emptyString();
108
109     Page* page = m_mediaElement->document().page();
110     if (!page)
111         return emptyString();
112
113     CaptionUserPreferences* captionPreferences = page->group().captionPreferences();
114     return captionPreferences->displayNameForTrack(track);
115 }
116
117 String MediaControlsHost::displayNameForTrack(AudioTrack* track)
118 {
119     if (!track)
120         return emptyString();
121
122     Page* page = m_mediaElement->document().page();
123     if (!page)
124         return emptyString();
125
126     CaptionUserPreferences* captionPreferences = page->group().captionPreferences();
127     return captionPreferences->displayNameForTrack(track);
128 }
129
130 TextTrack* MediaControlsHost::captionMenuOffItem()
131 {
132     return TextTrack::captionMenuOffItem();
133 }
134
135 TextTrack* MediaControlsHost::captionMenuAutomaticItem()
136 {
137     return TextTrack::captionMenuAutomaticItem();
138 }
139
140 AtomicString MediaControlsHost::captionDisplayMode()
141 {
142     Page* page = m_mediaElement->document().page();
143     if (!page)
144         return emptyAtom;
145
146     switch (page->group().captionPreferences()->captionDisplayMode()) {
147     case CaptionUserPreferences::Automatic:
148         return automaticKeyword();
149     case CaptionUserPreferences::ForcedOnly:
150         return forcedOnlyKeyword();
151     case CaptionUserPreferences::AlwaysOn:
152         return alwaysOnKeyword();
153     default:
154         ASSERT_NOT_REACHED();
155         return emptyAtom;
156     }
157 }
158
159 void MediaControlsHost::setSelectedTextTrack(TextTrack* track)
160 {
161     m_mediaElement->setSelectedTextTrack(track);
162 }
163
164 Element* MediaControlsHost::textTrackContainer()
165 {
166     if (!m_textTrackContainer) {
167         m_textTrackContainer = MediaControlTextTrackContainerElement::create(m_mediaElement->document());
168         m_textTrackContainer->setMediaController(m_mediaElement);
169     }
170     return m_textTrackContainer.get();
171 }
172
173 void MediaControlsHost::updateTextTrackContainer()
174 {
175     if (m_textTrackContainer)
176         m_textTrackContainer->updateDisplay();
177 }
178
179 void MediaControlsHost::enteredFullscreen()
180 {
181     if (m_textTrackContainer)
182         m_textTrackContainer->enteredFullscreen();
183 }
184
185 void MediaControlsHost::exitedFullscreen()
186 {
187     if (m_textTrackContainer)
188         m_textTrackContainer->exitedFullscreen();
189 }
190     
191 void MediaControlsHost::enterFullscreenOptimized()
192 {
193 #if ENABLE(OPTIMIZED_FULLSCREEN)
194     m_mediaElement->enterFullscreenOptimized();
195 #endif
196 }
197
198 void MediaControlsHost::updateCaptionDisplaySizes()
199 {
200     if (m_textTrackContainer)
201         m_textTrackContainer->updateSizes(true);
202 }
203     
204 bool MediaControlsHost::mediaPlaybackAllowsInline() const
205 {
206     return !m_mediaElement->mediaSession().requiresFullscreenForVideoPlayback(*m_mediaElement);
207 }
208
209 bool MediaControlsHost::supportsFullscreen()
210 {
211     return m_mediaElement->supportsFullscreen();
212 }
213
214 bool MediaControlsHost::userGestureRequired() const
215 {
216     return !m_mediaElement->mediaSession().playbackPermitted(*m_mediaElement);
217 }
218
219 String MediaControlsHost::externalDeviceDisplayName() const
220 {
221 #if ENABLE(IOS_AIRPLAY)
222     MediaPlayer* player = m_mediaElement->player();
223     if (!player) {
224         LOG(Media, "MediaControlsHost::externalDeviceDisplayName - returning \"\" because player is NULL");
225         return emptyString();
226     }
227     
228     String name = player->wirelessPlaybackTargetName();
229     LOG(Media, "MediaControlsHost::externalDeviceDisplayName - returning \"%s\"", name.utf8().data());
230     
231     return name;
232 #else
233     return emptyString();
234 #endif
235 }
236
237 String MediaControlsHost::externalDeviceType() const
238 {
239     DEPRECATED_DEFINE_STATIC_LOCAL(String, none, (ASCIILiteral("none")));
240     String type = none;
241     
242 #if ENABLE(IOS_AIRPLAY)
243     DEPRECATED_DEFINE_STATIC_LOCAL(String, airplay, (ASCIILiteral("airplay")));
244     DEPRECATED_DEFINE_STATIC_LOCAL(String, tvout, (ASCIILiteral("tvout")));
245     
246     MediaPlayer* player = m_mediaElement->player();
247     if (!player) {
248         LOG(Media, "MediaControlsHost::externalDeviceType - returning \"none\" because player is NULL");
249         return none;
250     }
251     
252     switch (player->wirelessPlaybackTargetType()) {
253     case MediaPlayer::TargetTypeNone:
254         type = none;
255         break;
256     case MediaPlayer::TargetTypeAirPlay:
257         type = airplay;
258         break;
259     case MediaPlayer::TargetTypeTVOut:
260         type = tvout;
261         break;
262     }
263 #endif
264     
265     LOG(Media, "MediaControlsHost::externalDeviceType - returning \"%s\"", type.utf8().data());
266     
267     return type;
268 }
269
270 bool MediaControlsHost::controlsDependOnPageScaleFactor() const
271 {
272     return m_mediaElement->mediaControlsDependOnPageScaleFactor();
273 }
274
275 void MediaControlsHost::setControlsDependOnPageScaleFactor(bool value)
276 {
277     m_mediaElement->setMediaControlsDependOnPageScaleFactor(value);
278 }
279
280 }
281
282 #endif