d5612ec3b99b611c137ffdc0060b038d0f17258e
[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)
31
32 #include <WebCore/EventListener.h>
33 #include <WebCore/HTMLMediaElementEnums.h>
34 #include <WebCore/PlatformLayer.h>
35 #include <WebCore/Timer.h>
36 #include <WebCore/WebVideoFullscreenInterface.h>
37 #include <functional>
38 #include <objc/objc.h>
39 #include <wtf/RefCounted.h>
40 #include <wtf/RefPtr.h>
41 #include <wtf/RetainPtr.h>
42
43 OBJC_CLASS WebAVPlayerController;
44 OBJC_CLASS AVPlayerViewController;
45 OBJC_CLASS UIViewController;
46 OBJC_CLASS UIWindow;
47 OBJC_CLASS UIView;
48 OBJC_CLASS CALayer;
49 OBJC_CLASS WebAVPlayerLayerView;
50 OBJC_CLASS WebAVPlayerLayer;
51
52 namespace WTF {
53 class String;
54 }
55
56 namespace WebCore {
57 class IntRect;
58 class WebVideoFullscreenModel;
59     
60 class WebVideoFullscreenChangeObserver {
61 public:
62     virtual ~WebVideoFullscreenChangeObserver() { };
63     virtual void didSetupFullscreen() = 0;
64     virtual void didEnterFullscreen() = 0;
65     virtual void didExitFullscreen() = 0;
66     virtual void didCleanupFullscreen() = 0;
67     virtual void fullscreenMayReturnToInline() = 0;
68 };
69
70 class WEBCORE_EXPORT WebVideoFullscreenInterfaceAVKit
71     : public WebVideoFullscreenInterface
72     , public RefCounted<WebVideoFullscreenInterfaceAVKit> {
73
74 public:
75     static Ref<WebVideoFullscreenInterfaceAVKit> create()
76     {
77         return adoptRef(*new WebVideoFullscreenInterfaceAVKit());
78     }
79     virtual ~WebVideoFullscreenInterfaceAVKit();
80     WEBCORE_EXPORT void setWebVideoFullscreenModel(WebVideoFullscreenModel*);
81     WEBCORE_EXPORT void setWebVideoFullscreenChangeObserver(WebVideoFullscreenChangeObserver*);
82     
83     WEBCORE_EXPORT virtual void resetMediaState() override;
84     WEBCORE_EXPORT virtual void setDuration(double) override;
85     WEBCORE_EXPORT virtual void setCurrentTime(double currentTime, double anchorTime) override;
86     WEBCORE_EXPORT virtual void setBufferedTime(double bufferedTime) override;
87     WEBCORE_EXPORT virtual void setRate(bool isPlaying, float playbackRate) override;
88     WEBCORE_EXPORT virtual void setVideoDimensions(bool hasVideo, float width, float height) override;
89     WEBCORE_EXPORT virtual void setSeekableRanges(const TimeRanges&) override;
90     WEBCORE_EXPORT virtual void setCanPlayFastReverse(bool) override;
91     WEBCORE_EXPORT virtual void setAudioMediaSelectionOptions(const Vector<WTF::String>& options, uint64_t selectedIndex) override;
92     WEBCORE_EXPORT virtual void setLegibleMediaSelectionOptions(const Vector<WTF::String>& options, uint64_t selectedIndex) override;
93     WEBCORE_EXPORT virtual void setExternalPlayback(bool enabled, ExternalPlaybackTargetType, WTF::String localizedDeviceName) override;
94     WEBCORE_EXPORT virtual void setWirelessVideoPlaybackDisabled(bool) override;
95
96     WEBCORE_EXPORT virtual void setupFullscreen(UIView&, const IntRect& initialRect, UIView *, HTMLMediaElementEnums::VideoFullscreenMode, bool allowsPictureInPicturePlayback);
97     WEBCORE_EXPORT virtual void enterFullscreen();
98     WEBCORE_EXPORT virtual void exitFullscreen(const IntRect& finalRect);
99     WEBCORE_EXPORT virtual void cleanupFullscreen();
100     WEBCORE_EXPORT virtual void invalidate();
101     WEBCORE_EXPORT virtual void requestHideAndExitFullscreen();
102     WEBCORE_EXPORT virtual void preparedToReturnToInline(bool visible, const IntRect& inlineRect);
103
104     enum class ExitFullScreenReason {
105         DoneButtonTapped,
106         FullScreenButtonTapped,
107         PinchGestureHandled,
108         RemoteControlStopEventReceived,
109         PictureInPictureStarted
110     };
111
112     bool shouldExitFullscreenWithReason(ExitFullScreenReason);
113     HTMLMediaElementEnums::VideoFullscreenMode mode() const { return m_mode; }
114     bool allowsPictureInPicturePlayback() const { return m_allowsPictureInPicturePlayback; }
115     WEBCORE_EXPORT bool mayAutomaticallyShowVideoPictureInPicture() const;
116     void fullscreenMayReturnToInline(std::function<void(bool)> callback);
117     bool wirelessVideoPlaybackDisabled() const;
118
119     void willStartPictureInPicture();
120     void didStartPictureInPicture();
121     void failedToStartPictureInPicture();
122     void willStopPictureInPicture();
123     void didStopPictureInPicture();
124     void prepareForPictureInPictureStopWithCompletionHandler(void (^)(BOOL));
125
126     void setMode(HTMLMediaElementEnums::VideoFullscreenMode);
127     void clearMode(HTMLMediaElementEnums::VideoFullscreenMode);
128     bool hasMode(HTMLMediaElementEnums::VideoFullscreenMode mode) const { return m_mode & mode; }
129     bool isMode(HTMLMediaElementEnums::VideoFullscreenMode mode) const { return m_mode == mode; }
130
131 protected:
132     WEBCORE_EXPORT WebVideoFullscreenInterfaceAVKit();
133     void beginSession();
134     void enterPictureInPicture();
135     void enterFullscreenStandard();
136     void watchdogTimerFired();
137
138     RetainPtr<WebAVPlayerController> m_playerController;
139     RetainPtr<AVPlayerViewController> m_playerViewController;
140     WebVideoFullscreenModel* m_videoFullscreenModel { nullptr };
141     WebVideoFullscreenChangeObserver* m_fullscreenChangeObserver { nullptr };
142
143     // These are only used when fullscreen is presented in a separate window.
144     RetainPtr<UIWindow> m_window;
145     RetainPtr<UIViewController> m_viewController;
146     RetainPtr<UIView> m_parentView;
147     RetainPtr<UIWindow> m_parentWindow;
148     RetainPtr<WebAVPlayerLayerView> m_playerLayerView;
149     HTMLMediaElementEnums::VideoFullscreenMode m_mode { HTMLMediaElementEnums::VideoFullscreenModeNone };
150     std::function<void(bool)> m_prepareToInlineCallback;
151     Timer m_watchdogTimer;
152     bool m_allowsPictureInPicturePlayback { false };
153     bool m_exitRequested { false };
154     bool m_exitCompleted { false };
155     bool m_enterRequested { false };
156     bool m_wirelessVideoPlaybackDisabled { true };
157
158     void doEnterFullscreen();
159 };
160
161 bool supportsPictureInPicture();
162
163 }
164
165 #endif
166
167 #endif