a72c1602cdf5f9a3194d6785ca44deeb29aa9a61
[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     void metadataDidArrive(RetainPtr<NSArray>);
112
113 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
114     void outputMediaDataWillChange(AVPlayerItemVideoOutput*);
115 #endif
116
117 #if ENABLE(IOS_AIRPLAY)
118     void playbackTargetIsWirelessDidChange();
119 #endif
120     
121 #if ENABLE(AVF_CAPTIONS)
122     virtual void notifyTrackModeChanged() override;
123     virtual void synchronizeTextTrackState() override;
124 #endif
125     
126     WeakPtr<MediaPlayerPrivateAVFoundationObjC> createWeakPtr() { return m_weakPtrFactory.createWeakPtr(); }
127
128 private:
129     MediaPlayerPrivateAVFoundationObjC(MediaPlayer*);
130
131     // engine support
132     static PassOwnPtr<MediaPlayerPrivateInterface> create(MediaPlayer*);
133     static void getSupportedTypes(HashSet<String>& types);
134     static MediaPlayer::SupportsType supportsType(const MediaEngineSupportParameters&);
135     static bool supportsKeySystem(const String& keySystem, const String& mimeType);
136
137     static bool isAvailable();
138
139     virtual void cancelLoad();
140
141     virtual PlatformMedia platformMedia() const;
142
143     virtual void platformSetVisible(bool);
144     virtual void platformPlay();
145     virtual void platformPause();
146     virtual float currentTime() const;
147     virtual void setVolume(float);
148     virtual void setClosedCaptionsVisible(bool);
149     virtual void paint(GraphicsContext*, const IntRect&);
150     virtual void paintCurrentFrameInContext(GraphicsContext*, const IntRect&);
151     virtual PlatformLayer* platformLayer() const;
152 #if PLATFORM(IOS)
153     virtual void setVideoFullscreenLayer(PlatformLayer*);
154     virtual void setVideoFullscreenFrame(FloatRect);
155     virtual void setVideoFullscreenGravity(MediaPlayer::VideoGravity);
156
157     virtual NSArray *timedMetadata() const override;
158     virtual String accessLog() const;
159     virtual String errorLog() const;
160 #endif
161
162     virtual bool supportsAcceleratedRendering() const { return true; }
163     virtual float mediaTimeForTimeValue(float) const;
164     virtual double maximumDurationToCacheMediaTime() const { return 5; }
165
166     virtual void createAVPlayer();
167     virtual void createAVPlayerItem();
168     virtual void createAVAssetForURL(const String& url);
169     virtual MediaPlayerPrivateAVFoundation::ItemStatus playerItemStatus() const;
170     virtual MediaPlayerPrivateAVFoundation::AssetStatus assetStatus() const;
171
172     virtual void checkPlayability();
173     virtual void updateRate();
174     virtual float rate() const;
175     virtual void seekToTime(double time, double negativeTolerance, double positiveTolerance);
176     virtual unsigned long long totalBytes() const;
177     virtual std::unique_ptr<PlatformTimeRanges> platformBufferedTimeRanges() const;
178     virtual double platformMinTimeSeekable() const;
179     virtual double platformMaxTimeSeekable() const;
180     virtual float platformDuration() const;
181     virtual float platformMaxTimeLoaded() const;
182     virtual void beginLoadingMetadata();
183     virtual void sizeChanged();
184
185     virtual bool hasAvailableVideoFrame() const;
186
187     virtual void createContextVideoRenderer();
188     virtual void destroyContextVideoRenderer();
189
190     virtual void createVideoLayer();
191     virtual void destroyVideoLayer();
192
193     virtual bool hasContextRenderer() const;
194     virtual bool hasLayerRenderer() const;
195
196     virtual void updateVideoLayerGravity() override;
197
198     virtual bool hasSingleSecurityOrigin() const;
199
200 #if ENABLE(VIDEO_TRACK)
201     virtual bool requiresTextTrackRepresentation() const override;
202     virtual void setTextTrackRepresentation(TextTrackRepresentation*) override;
203 #endif
204
205     void createImageGenerator();
206     void destroyImageGenerator();
207     RetainPtr<CGImageRef> createImageForTimeInRect(float, const IntRect&);
208     void paintWithImageGenerator(GraphicsContext*, const IntRect&);
209
210 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
211     void createVideoOutput();
212     void destroyVideoOutput();
213     RetainPtr<CVPixelBufferRef> createPixelBuffer();
214     void updateLastImage();
215     bool videoOutputHasAvailableFrame();
216     void paintWithVideoOutput(GraphicsContext*, const IntRect&);
217     virtual PassNativeImagePtr nativeImageForCurrentTime() override;
218     void waitForVideoOutputMediaDataWillChange();
219 #endif
220
221 #if ENABLE(ENCRYPTED_MEDIA)
222     virtual MediaPlayer::MediaKeyException addKey(const String&, const unsigned char*, unsigned, const unsigned char*, unsigned, const String&);
223     virtual MediaPlayer::MediaKeyException generateKeyRequest(const String&, const unsigned char*, unsigned);
224     virtual MediaPlayer::MediaKeyException cancelKeyRequest(const String&, const String&);
225 #endif
226
227 #if ENABLE(ENCRYPTED_MEDIA_V2)
228     std::unique_ptr<CDMSession> createSession(const String& keySystem);
229 #endif
230
231     virtual String languageOfPrimaryAudioTrack() const override;
232
233 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
234     void processMediaSelectionOptions();
235     AVMediaSelectionGroup* safeMediaSelectionGroupForLegibleMedia();
236 #endif
237
238     virtual void setCurrentTrack(InbandTextTrackPrivateAVF*) override;
239     virtual InbandTextTrackPrivateAVF* currentTrack() const override { return m_currentTrack; }
240
241 #if !HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
242     void processLegacyClosedCaptionsTracks();
243 #endif
244
245 #if ENABLE(VIDEO_TRACK)
246     void updateAudioTracks();
247     void updateVideoTracks();
248 #endif
249
250 #if ENABLE(IOS_AIRPLAY)
251     virtual bool isCurrentPlaybackTargetWireless() const override;
252     virtual String wirelessPlaybackTargetName() const override;
253     virtual MediaPlayer::WirelessPlaybackTargetType wirelessPlaybackTargetType() const override;
254     virtual bool wirelessVideoPlaybackDisabled() const override;
255     virtual void setWirelessVideoPlaybackDisabled(bool) override;
256 #endif
257
258     WeakPtrFactory<MediaPlayerPrivateAVFoundationObjC> m_weakPtrFactory;
259
260     RetainPtr<AVURLAsset> m_avAsset;
261     RetainPtr<AVPlayer> m_avPlayer;
262     RetainPtr<AVPlayerItem> m_avPlayerItem;
263     RetainPtr<AVPlayerLayer> m_videoLayer;
264 #if PLATFORM(IOS)
265     RetainPtr<PlatformLayer> m_videoFullscreenLayer;
266     FloatRect m_videoFullscreenFrame;
267     MediaPlayer::VideoGravity m_videoFullscreenGravity;
268     RetainPtr<PlatformLayer> m_textTrackRepresentationLayer;
269 #endif
270     RetainPtr<WebCoreAVFMovieObserver> m_objcObserver;
271     RetainPtr<id> m_timeObserver;
272     mutable String m_languageOfPrimaryAudioTrack;
273     bool m_videoFrameHasDrawn;
274     bool m_haveCheckedPlayability;
275
276     RetainPtr<AVAssetImageGenerator> m_imageGenerator;
277 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
278     RetainPtr<AVPlayerItemVideoOutput> m_videoOutput;
279     RetainPtr<WebCoreAVFPullDelegate> m_videoOutputDelegate;
280     RetainPtr<CGImageRef> m_lastImage;
281     dispatch_semaphore_t m_videoOutputSemaphore;
282 #endif
283
284 #if USE(VIDEOTOOLBOX)
285     RetainPtr<VTPixelTransferSessionRef> m_pixelTransferSession;
286 #endif
287
288 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
289     friend class WebCoreAVFResourceLoader;
290     HashMap<RetainPtr<AVAssetResourceLoadingRequest>, RefPtr<WebCoreAVFResourceLoader>> m_resourceLoaderMap;
291     RetainPtr<WebCoreAVFLoaderDelegate> m_loaderDelegate;
292     HashMap<String, RetainPtr<AVAssetResourceLoadingRequest>> m_keyURIToRequestMap;
293     HashMap<String, RetainPtr<AVAssetResourceLoadingRequest>> m_sessionIDToRequestMap;
294 #endif
295
296 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
297     RetainPtr<AVPlayerItemLegibleOutput> m_legibleOutput;
298 #endif
299
300 #if ENABLE(VIDEO_TRACK)
301     Vector<RefPtr<AudioTrackPrivateAVFObjC>> m_audioTracks;
302     Vector<RefPtr<VideoTrackPrivateAVFObjC>> m_videoTracks;
303 #endif
304
305     InbandTextTrackPrivateAVF* m_currentTrack;
306
307     mutable RetainPtr<NSArray> m_cachedSeekableRanges;
308     mutable RetainPtr<NSArray> m_cachedLoadedRanges;
309     RetainPtr<NSArray> m_cachedTracks;
310     RetainPtr<NSArray> m_currentMetaData;
311     FloatSize m_cachedPresentationSize;
312     double m_cachedDuration;
313     double m_cachedRate;
314     unsigned m_pendingStatusChanges;
315     int m_cachedItemStatus;
316     bool m_cachedLikelyToKeepUp;
317     bool m_cachedBufferEmpty;
318     bool m_cachedBufferFull;
319     bool m_cachedHasEnabledAudio;
320 #if ENABLE(IOS_AIRPLAY)
321     mutable bool m_allowsWirelessVideoPlayback;
322 #endif
323 };
324
325 }
326
327 #endif
328 #endif