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