47895216d21de715b50f69aa45641e9d0b92ebd1
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / objc / MediaPlayerPrivateAVFoundationObjC.h
1 /*
2  * Copyright (C) 2011-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 #ifndef MediaPlayerPrivateAVFoundationObjC_h
27 #define MediaPlayerPrivateAVFoundationObjC_h
28
29 #if ENABLE(VIDEO) && USE(AVFOUNDATION)
30
31 #include "MediaPlayerPrivateAVFoundation.h"
32 #include <wtf/HashMap.h>
33
34 OBJC_CLASS AVAssetImageGenerator;
35 OBJC_CLASS AVAssetResourceLoadingRequest;
36 OBJC_CLASS AVMediaSelectionGroup;
37 OBJC_CLASS AVPlayer;
38 OBJC_CLASS AVPlayerItem;
39 OBJC_CLASS AVPlayerItemLegibleOutput;
40 OBJC_CLASS AVPlayerItemTrack;
41 OBJC_CLASS AVPlayerItemVideoOutput;
42 OBJC_CLASS AVPlayerLayer;
43 OBJC_CLASS AVURLAsset;
44 OBJC_CLASS NSArray;
45 OBJC_CLASS NSURLAuthenticationChallenge;
46 OBJC_CLASS WebCoreAVFMovieObserver;
47 OBJC_CLASS WebCoreAVFPullDelegate;
48
49 typedef struct objc_object* id;
50
51 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
52 OBJC_CLASS WebCoreAVFLoaderDelegate;
53 OBJC_CLASS AVAssetResourceLoadingRequest;
54 #endif
55
56 typedef struct CGImage *CGImageRef;
57 typedef struct __CVBuffer *CVPixelBufferRef;
58 typedef struct OpaqueVTPixelTransferSession* VTPixelTransferSessionRef;
59
60 namespace WebCore {
61
62 class WebCoreAVFResourceLoader;
63 class InbandTextTrackPrivateAVFObjC;
64 class AudioTrackPrivateAVFObjC;
65 class VideoTrackPrivateAVFObjC;
66
67 class MediaPlayerPrivateAVFoundationObjC : public MediaPlayerPrivateAVFoundation {
68 public:
69     virtual ~MediaPlayerPrivateAVFoundationObjC();
70
71     static void registerMediaEngine(MediaEngineRegistrar);
72
73     void setAsset(id);
74     virtual void tracksChanged() override;
75
76 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
77     RetainPtr<AVPlayerItem> playerItem() const { return m_avPlayerItem; }
78     void processCue(NSArray *, double);
79     void flushCues();
80 #endif
81     
82 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
83     bool shouldWaitForLoadingOfResource(AVAssetResourceLoadingRequest*);
84     bool shouldWaitForResponseToAuthenticationChallenge(NSURLAuthenticationChallenge*);
85     void didCancelLoadingRequest(AVAssetResourceLoadingRequest*);
86     void didStopLoadingRequest(AVAssetResourceLoadingRequest *);
87 #endif
88
89 #if ENABLE(ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA_V2)
90     static bool extractKeyURIKeyIDAndCertificateFromInitData(Uint8Array* initData, String& keyURI, String& keyID, RefPtr<Uint8Array>& certificate);
91 #endif
92
93 #if ENABLE(ENCRYPTED_MEDIA_V2)
94     RetainPtr<AVAssetResourceLoadingRequest> takeRequestForKeyURI(const String&);
95 #endif
96
97     void playerItemStatusDidChange(int);
98     void playbackLikelyToKeepUpWillChange();
99     void playbackLikelyToKeepUpDidChange(bool);
100     void playbackBufferEmptyWillChange();
101     void playbackBufferEmptyDidChange(bool);
102     void playbackBufferFullWillChange();
103     void playbackBufferFullDidChange(bool);
104     void loadedTimeRangesDidChange(RetainPtr<NSArray>);
105     void seekableTimeRangesDidChange(RetainPtr<NSArray>);
106     void tracksDidChange(RetainPtr<NSArray>);
107     void hasEnabledAudioDidChange(bool);
108     void presentationSizeDidChange(FloatSize);
109     void durationDidChange(double);
110     void rateDidChange(double);
111
112 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
113     void outputMediaDataWillChange(AVPlayerItemVideoOutput*);
114 #endif
115
116 #if ENABLE(IOS_AIRPLAY)
117     void playbackTargetIsWirelessDidChange();
118 #endif
119     
120 #if ENABLE(AVF_CAPTIONS)
121     virtual void notifyTrackModeChanged() override;
122     virtual void synchronizeTextTrackState() override;
123 #endif
124     
125     WeakPtr<MediaPlayerPrivateAVFoundationObjC> createWeakPtr() { return m_weakPtrFactory.createWeakPtr(); }
126
127 private:
128     MediaPlayerPrivateAVFoundationObjC(MediaPlayer*);
129
130     // engine support
131     static PassOwnPtr<MediaPlayerPrivateInterface> create(MediaPlayer*);
132     static void getSupportedTypes(HashSet<String>& types);
133     static MediaPlayer::SupportsType supportsType(const MediaEngineSupportParameters&);
134     static bool supportsKeySystem(const String& keySystem, const String& mimeType);
135
136     static bool isAvailable();
137
138     virtual void cancelLoad();
139
140     virtual PlatformMedia platformMedia() const;
141
142     virtual void platformSetVisible(bool);
143     virtual void platformPlay();
144     virtual void platformPause();
145     virtual float currentTime() const;
146     virtual void setVolume(float);
147     virtual void setClosedCaptionsVisible(bool);
148     virtual void paint(GraphicsContext*, const IntRect&);
149     virtual void paintCurrentFrameInContext(GraphicsContext*, const IntRect&);
150     virtual PlatformLayer* platformLayer() const;
151 #if PLATFORM(IOS)
152     virtual void setVideoFullscreenLayer(PlatformLayer*);
153     virtual void setVideoFullscreenFrame(FloatRect);
154     virtual void setVideoFullscreenGravity(MediaPlayer::VideoGravity);
155 #endif
156     virtual bool supportsAcceleratedRendering() const { return true; }
157     virtual float mediaTimeForTimeValue(float) const;
158     virtual double maximumDurationToCacheMediaTime() const { return 5; }
159
160     virtual void createAVPlayer();
161     virtual void createAVPlayerItem();
162     virtual void createAVAssetForURL(const String& url);
163     virtual MediaPlayerPrivateAVFoundation::ItemStatus playerItemStatus() const;
164     virtual MediaPlayerPrivateAVFoundation::AssetStatus assetStatus() const;
165
166     virtual void checkPlayability();
167     virtual void updateRate();
168     virtual float rate() const;
169     virtual void seekToTime(double time, double negativeTolerance, double positiveTolerance);
170     virtual unsigned long long totalBytes() const;
171     virtual std::unique_ptr<PlatformTimeRanges> platformBufferedTimeRanges() const;
172     virtual double platformMinTimeSeekable() const;
173     virtual double platformMaxTimeSeekable() const;
174     virtual float platformDuration() const;
175     virtual float platformMaxTimeLoaded() const;
176     virtual void beginLoadingMetadata();
177     virtual void sizeChanged();
178
179     virtual bool hasAvailableVideoFrame() const;
180
181     virtual void createContextVideoRenderer();
182     virtual void destroyContextVideoRenderer();
183
184     virtual void createVideoLayer();
185     virtual void destroyVideoLayer();
186
187     virtual bool hasContextRenderer() const;
188     virtual bool hasLayerRenderer() const;
189
190     virtual void updateVideoLayerGravity() override;
191
192     virtual bool hasSingleSecurityOrigin() const;
193
194 #if ENABLE(VIDEO_TRACK)
195     virtual bool requiresTextTrackRepresentation() const override;
196     virtual void setTextTrackRepresentation(TextTrackRepresentation*) override;
197 #endif
198
199     void createImageGenerator();
200     void destroyImageGenerator();
201     RetainPtr<CGImageRef> createImageForTimeInRect(float, const IntRect&);
202     void paintWithImageGenerator(GraphicsContext*, const IntRect&);
203
204 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
205     void createVideoOutput();
206     void destroyVideoOutput();
207     RetainPtr<CVPixelBufferRef> createPixelBuffer();
208     void updateLastImage();
209     bool videoOutputHasAvailableFrame();
210     void paintWithVideoOutput(GraphicsContext*, const IntRect&);
211     virtual PassNativeImagePtr nativeImageForCurrentTime() override;
212     void waitForVideoOutputMediaDataWillChange();
213 #endif
214
215 #if ENABLE(ENCRYPTED_MEDIA)
216     virtual MediaPlayer::MediaKeyException addKey(const String&, const unsigned char*, unsigned, const unsigned char*, unsigned, const String&);
217     virtual MediaPlayer::MediaKeyException generateKeyRequest(const String&, const unsigned char*, unsigned);
218     virtual MediaPlayer::MediaKeyException cancelKeyRequest(const String&, const String&);
219 #endif
220
221 #if ENABLE(ENCRYPTED_MEDIA_V2)
222     std::unique_ptr<CDMSession> createSession(const String& keySystem);
223 #endif
224
225     virtual String languageOfPrimaryAudioTrack() const override;
226
227 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
228     void processMediaSelectionOptions();
229     AVMediaSelectionGroup* safeMediaSelectionGroupForLegibleMedia();
230 #endif
231
232     virtual void setCurrentTrack(InbandTextTrackPrivateAVF*) override;
233     virtual InbandTextTrackPrivateAVF* currentTrack() const override { return m_currentTrack; }
234
235 #if !HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
236     void processLegacyClosedCaptionsTracks();
237 #endif
238
239 #if ENABLE(VIDEO_TRACK)
240     void updateAudioTracks();
241     void updateVideoTracks();
242 #endif
243
244 #if ENABLE(IOS_AIRPLAY)
245     virtual bool isCurrentPlaybackTargetWireless() const override;
246     virtual String wirelessPlaybackTargetName() const override;
247     virtual MediaPlayer::WirelessPlaybackTargetType wirelessPlaybackTargetType() const override;
248     virtual bool wirelessVideoPlaybackDisabled() const override;
249     virtual void setWirelessVideoPlaybackDisabled(bool) override;
250 #endif
251
252     WeakPtrFactory<MediaPlayerPrivateAVFoundationObjC> m_weakPtrFactory;
253
254     RetainPtr<AVURLAsset> m_avAsset;
255     RetainPtr<AVPlayer> m_avPlayer;
256     RetainPtr<AVPlayerItem> m_avPlayerItem;
257     RetainPtr<AVPlayerLayer> m_videoLayer;
258 #if PLATFORM(IOS)
259     RetainPtr<PlatformLayer> m_videoFullscreenLayer;
260     FloatRect m_videoFullscreenFrame;
261     MediaPlayer::VideoGravity m_videoFullscreenGravity;
262     RetainPtr<PlatformLayer> m_textTrackRepresentationLayer;
263 #endif
264     RetainPtr<WebCoreAVFMovieObserver> m_objcObserver;
265     RetainPtr<id> m_timeObserver;
266     mutable String m_languageOfPrimaryAudioTrack;
267     bool m_videoFrameHasDrawn;
268     bool m_haveCheckedPlayability;
269
270     RetainPtr<AVAssetImageGenerator> m_imageGenerator;
271 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
272     RetainPtr<AVPlayerItemVideoOutput> m_videoOutput;
273     RetainPtr<WebCoreAVFPullDelegate> m_videoOutputDelegate;
274     RetainPtr<CGImageRef> m_lastImage;
275     dispatch_semaphore_t m_videoOutputSemaphore;
276 #endif
277
278 #if USE(VIDEOTOOLBOX)
279     RetainPtr<VTPixelTransferSessionRef> m_pixelTransferSession;
280 #endif
281
282 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
283     friend class WebCoreAVFResourceLoader;
284     HashMap<RetainPtr<AVAssetResourceLoadingRequest>, RefPtr<WebCoreAVFResourceLoader>> m_resourceLoaderMap;
285     RetainPtr<WebCoreAVFLoaderDelegate> m_loaderDelegate;
286     HashMap<String, RetainPtr<AVAssetResourceLoadingRequest>> m_keyURIToRequestMap;
287     HashMap<String, RetainPtr<AVAssetResourceLoadingRequest>> m_sessionIDToRequestMap;
288 #endif
289
290 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
291     RetainPtr<AVPlayerItemLegibleOutput> m_legibleOutput;
292 #endif
293
294 #if ENABLE(VIDEO_TRACK)
295     Vector<RefPtr<AudioTrackPrivateAVFObjC>> m_audioTracks;
296     Vector<RefPtr<VideoTrackPrivateAVFObjC>> m_videoTracks;
297 #endif
298
299     InbandTextTrackPrivateAVF* m_currentTrack;
300
301     mutable RetainPtr<NSArray> m_cachedSeekableRanges;
302     mutable RetainPtr<NSArray> m_cachedLoadedRanges;
303     RetainPtr<NSArray> m_cachedTracks;
304     FloatSize m_cachedPresentationSize;
305     double m_cachedDuration;
306     double m_cachedRate;
307     unsigned m_pendingStatusChanges;
308     int m_cachedItemStatus;
309     bool m_cachedLikelyToKeepUp;
310     bool m_cachedBufferEmpty;
311     bool m_cachedBufferFull;
312     bool m_cachedHasEnabledAudio;
313 #if ENABLE(IOS_AIRPLAY)
314     mutable bool m_allowsWirelessVideoPlayback;
315 #endif
316 };
317
318 }
319
320 #endif
321 #endif