[iOS] When simultaneously exiting-and-entering fullscreen, WebVideoFullscreenManager...
[WebKit-https.git] / Source / WebCore / platform / ios / WebVideoFullscreenInterfaceAVKit.h
1 /*
2  * Copyright (C) 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
27 #ifndef WebVideoFullscreenInterfaceAVKit_h
28 #define WebVideoFullscreenInterfaceAVKit_h
29
30 #if PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000
31
32 #include <WebCore/EventListener.h>
33 #include <WebCore/HTMLMediaElement.h>
34 #include <WebCore/PlatformLayer.h>
35 #include <WebCore/WebVideoFullscreenInterface.h>
36 #include <wtf/RefPtr.h>
37 #include <wtf/RetainPtr.h>
38 #include <wtf/ThreadSafeRefCounted.h>
39
40 OBJC_CLASS WebAVPlayerController;
41 OBJC_CLASS AVPlayerViewController;
42 OBJC_CLASS UIViewController;
43 OBJC_CLASS UIWindow;
44 OBJC_CLASS UIView;
45 OBJC_CLASS CALayer;
46 OBJC_CLASS WebAVVideoLayer;
47
48 namespace WTF {
49 class String;
50 }
51
52 namespace WebCore {
53 class IntRect;
54 class WebVideoFullscreenModel;
55     
56 class WebVideoFullscreenChangeObserver {
57 public:
58     virtual ~WebVideoFullscreenChangeObserver() { };
59     virtual void didSetupFullscreen() = 0;
60     virtual void didEnterFullscreen() = 0;
61     virtual void didExitFullscreen() = 0;
62     virtual void didCleanupFullscreen() = 0;
63     virtual void fullscreenMayReturnToInline() = 0;
64 };
65
66 class WEBCORE_EXPORT WebVideoFullscreenInterfaceAVKit
67     : public WebVideoFullscreenInterface
68     , public ThreadSafeRefCounted<WebVideoFullscreenInterfaceAVKit> {
69
70 public:
71     static Ref<WebVideoFullscreenInterfaceAVKit> create()
72     {
73         return adoptRef(*new WebVideoFullscreenInterfaceAVKit());
74     }
75     virtual ~WebVideoFullscreenInterfaceAVKit() { }
76     WEBCORE_EXPORT void setWebVideoFullscreenModel(WebVideoFullscreenModel*);
77     WEBCORE_EXPORT void setWebVideoFullscreenChangeObserver(WebVideoFullscreenChangeObserver*);
78     
79     WEBCORE_EXPORT virtual void resetMediaState() override;
80     WEBCORE_EXPORT virtual void setDuration(double) override;
81     WEBCORE_EXPORT virtual void setCurrentTime(double currentTime, double anchorTime) override;
82     WEBCORE_EXPORT virtual void setBufferedTime(double bufferedTime) override;
83     WEBCORE_EXPORT virtual void setRate(bool isPlaying, float playbackRate) override;
84     WEBCORE_EXPORT virtual void setVideoDimensions(bool hasVideo, float width, float height) override;
85     WEBCORE_EXPORT virtual void setSeekableRanges(const TimeRanges&) override;
86     WEBCORE_EXPORT virtual void setCanPlayFastReverse(bool) override;
87     WEBCORE_EXPORT virtual void setAudioMediaSelectionOptions(const Vector<WTF::String>& options, uint64_t selectedIndex) override;
88     WEBCORE_EXPORT virtual void setLegibleMediaSelectionOptions(const Vector<WTF::String>& options, uint64_t selectedIndex) override;
89     WEBCORE_EXPORT virtual void setExternalPlayback(bool enabled, ExternalPlaybackTargetType, WTF::String localizedDeviceName) override;
90     
91     WEBCORE_EXPORT virtual void setupFullscreen(PlatformLayer&, const IntRect& initialRect, UIView *, HTMLMediaElement::VideoFullscreenMode, bool allowOptimizedFullscreen);
92     WEBCORE_EXPORT virtual void enterFullscreen();
93     WEBCORE_EXPORT virtual void exitFullscreen(const IntRect& finalRect);
94     WEBCORE_EXPORT virtual void cleanupFullscreen();
95     WEBCORE_EXPORT virtual void invalidate();
96     WEBCORE_EXPORT virtual void requestHideAndExitFullscreen();
97     WEBCORE_EXPORT virtual void preparedToReturnToInline(bool visible, const IntRect& inlineRect);
98
99     HTMLMediaElement::VideoFullscreenMode mode() const { return m_mode; }
100     void setIsOptimized(bool);
101     WEBCORE_EXPORT bool mayAutomaticallyShowVideoOptimized() const;
102     void fullscreenMayReturnToInline(std::function<void(bool)> callback);
103
104     void willStartOptimizedFullscreen();
105     void didStartOptimizedFullscreen();
106     void willStopOptimizedFullscreen();
107     void didStopOptimizedFullscreen();
108     void willCancelOptimizedFullscreen();
109     void didCancelOptimizedFullscreen();
110     void prepareForOptimizedFullscreenStopWithCompletionHandler(void (^)(BOOL));
111
112     void setMode(HTMLMediaElement::VideoFullscreenMode);
113     void clearMode(HTMLMediaElement::VideoFullscreenMode);
114     bool hasMode(HTMLMediaElement::VideoFullscreenMode mode) const { return m_mode & mode; }
115     bool isMode(HTMLMediaElement::VideoFullscreenMode mode) const { return m_mode == mode; }
116
117 protected:
118     WEBCORE_EXPORT WebVideoFullscreenInterfaceAVKit();
119     void beginSession();
120     void setupFullscreenInternal(PlatformLayer&, const IntRect& initialRect, UIView *, HTMLMediaElement::VideoFullscreenMode, bool allowOptimizedFullscreen);
121     void enterFullscreenOptimized();
122     void enterFullscreenStandard();
123     void exitFullscreenInternal(const IntRect& finalRect);
124     void cleanupFullscreenInternal();
125
126     RetainPtr<WebAVPlayerController> m_playerController;
127     RetainPtr<AVPlayerViewController> m_playerViewController;
128     RetainPtr<CALayer> m_videoLayer;
129     RetainPtr<WebAVVideoLayer> m_videoLayerContainer;
130     WebVideoFullscreenModel* m_videoFullscreenModel;
131     WebVideoFullscreenChangeObserver* m_fullscreenChangeObserver;
132
133     // These are only used when fullscreen is presented in a separate window.
134     RetainPtr<UIWindow> m_window;
135     RetainPtr<UIViewController> m_viewController;
136     RetainPtr<UIView> m_parentView;
137     RetainPtr<UIWindow> m_parentWindow;
138     HTMLMediaElement::VideoFullscreenMode m_mode;
139     std::function<void(bool)> m_prepareToInlineCallback;
140     bool m_exitRequested;
141     bool m_exitCompleted;
142     bool m_enterRequested;
143
144     void doEnterFullscreen();
145 };
146
147 }
148
149 #endif
150
151 #endif