[Mac] Update for output device API change
[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 AVOutputContext;
38 OBJC_CLASS AVPlayer;
39 OBJC_CLASS AVPlayerItem;
40 OBJC_CLASS AVPlayerItemLegibleOutput;
41 OBJC_CLASS AVPlayerItemTrack;
42 OBJC_CLASS AVPlayerItemVideoOutput;
43 OBJC_CLASS AVPlayerLayer;
44 OBJC_CLASS AVURLAsset;
45 OBJC_CLASS NSArray;
46 OBJC_CLASS NSURLAuthenticationChallenge;
47 OBJC_CLASS WebCoreAVFMovieObserver;
48 OBJC_CLASS WebCoreAVFPullDelegate;
49
50 typedef struct objc_object* id;
51
52 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
53 OBJC_CLASS WebCoreAVFLoaderDelegate;
54 OBJC_CLASS AVAssetResourceLoadingRequest;
55 #endif
56
57 typedef struct CGImage *CGImageRef;
58 typedef struct __CVBuffer *CVPixelBufferRef;
59 typedef struct OpaqueVTPixelTransferSession* VTPixelTransferSessionRef;
60
61 namespace WebCore {
62
63 class AudioSourceProviderAVFObjC;
64 class AudioTrackPrivateAVFObjC;
65 class InbandMetadataTextTrackPrivateAVF;
66 class InbandTextTrackPrivateAVFObjC;
67 class MediaPlaybackTarget;
68 class MediaSelectionGroupAVFObjC;
69 class VideoTrackPrivateAVFObjC;
70 class WebCoreAVFResourceLoader;
71
72 class MediaPlayerPrivateAVFoundationObjC : public MediaPlayerPrivateAVFoundation {
73 public:
74     explicit MediaPlayerPrivateAVFoundationObjC(MediaPlayer*);
75     virtual ~MediaPlayerPrivateAVFoundationObjC();
76
77     static void registerMediaEngine(MediaEngineRegistrar);
78
79     void setAsset(id);
80     virtual void tracksChanged() override;
81
82 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
83     RetainPtr<AVPlayerItem> playerItem() const { return m_avPlayerItem; }
84     void processCue(NSArray *, NSArray *, const MediaTime&);
85     void flushCues();
86 #endif
87     
88 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
89     bool shouldWaitForLoadingOfResource(AVAssetResourceLoadingRequest*);
90     bool shouldWaitForResponseToAuthenticationChallenge(NSURLAuthenticationChallenge*);
91     void didCancelLoadingRequest(AVAssetResourceLoadingRequest*);
92     void didStopLoadingRequest(AVAssetResourceLoadingRequest *);
93 #endif
94
95 #if ENABLE(ENCRYPTED_MEDIA_V2)
96     RetainPtr<AVAssetResourceLoadingRequest> takeRequestForKeyURI(const String&);
97     virtual void keyAdded() override;
98 #endif
99
100     void playerItemStatusDidChange(int);
101     void playbackLikelyToKeepUpWillChange();
102     void playbackLikelyToKeepUpDidChange(bool);
103     void playbackBufferEmptyWillChange();
104     void playbackBufferEmptyDidChange(bool);
105     void playbackBufferFullWillChange();
106     void playbackBufferFullDidChange(bool);
107     void loadedTimeRangesDidChange(RetainPtr<NSArray>);
108     void seekableTimeRangesDidChange(RetainPtr<NSArray>);
109     void tracksDidChange(RetainPtr<NSArray>);
110     void hasEnabledAudioDidChange(bool);
111     void presentationSizeDidChange(FloatSize);
112     void durationDidChange(const MediaTime&);
113     void rateDidChange(double);
114     void metadataDidArrive(RetainPtr<NSArray>, const MediaTime&);
115     void firstFrameAvailableDidChange(bool);
116     void trackEnabledDidChange(bool);
117     void canPlayFastReverseDidChange(bool);
118     void canPlayFastForwardDidChange(bool);
119
120     virtual void setShouldBufferData(bool);
121
122 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
123     void outputMediaDataWillChange(AVPlayerItemVideoOutput*);
124 #endif
125
126 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
127     void playbackTargetIsWirelessDidChange();
128 #endif
129     
130 #if ENABLE(AVF_CAPTIONS)
131     virtual void notifyTrackModeChanged() override;
132     virtual void synchronizeTextTrackState() override;
133 #endif
134     
135     WeakPtr<MediaPlayerPrivateAVFoundationObjC> createWeakPtr() { return m_weakPtrFactory.createWeakPtr(); }
136
137 private:
138     // engine support
139     static void getSupportedTypes(HashSet<String>& types);
140     static MediaPlayer::SupportsType supportsType(const MediaEngineSupportParameters&);
141     static bool supportsKeySystem(const String& keySystem, const String& mimeType);
142
143     static bool isAvailable();
144
145     virtual void cancelLoad();
146
147     virtual PlatformMedia platformMedia() const;
148
149     virtual void platformSetVisible(bool);
150     virtual void platformPlay();
151     virtual void platformPause();
152     virtual MediaTime currentMediaTime() const override;
153     virtual void setVolume(float);
154     virtual void setClosedCaptionsVisible(bool);
155     virtual void paint(GraphicsContext*, const FloatRect&);
156     virtual void paintCurrentFrameInContext(GraphicsContext*, const FloatRect&);
157     virtual PlatformLayer* platformLayer() const;
158 #if PLATFORM(IOS)
159     virtual void setVideoFullscreenLayer(PlatformLayer*);
160     virtual void setVideoFullscreenFrame(FloatRect);
161     virtual void setVideoFullscreenGravity(MediaPlayer::VideoGravity);
162
163     virtual NSArray *timedMetadata() const override;
164     virtual String accessLog() const;
165     virtual String errorLog() const;
166 #endif
167
168     virtual bool supportsAcceleratedRendering() const { return true; }
169     virtual MediaTime mediaTimeForTimeValue(const MediaTime&) const;
170     virtual double maximumDurationToCacheMediaTime() const;
171
172     virtual void createAVPlayer();
173     virtual void createAVPlayerItem();
174     virtual void createAVPlayerLayer();
175     virtual void createAVAssetForURL(const String& url);
176     virtual MediaPlayerPrivateAVFoundation::ItemStatus playerItemStatus() const;
177     virtual MediaPlayerPrivateAVFoundation::AssetStatus assetStatus() const;
178     virtual long assetErrorCode() const;
179
180     virtual void checkPlayability();
181     virtual void setRateDouble(double) override;
182     virtual double rate() const;
183     void setPreservesPitch(bool) override;
184     virtual void seekToTime(const MediaTime&, const MediaTime& negativeTolerance, const MediaTime& positiveTolerance);
185     virtual unsigned long long totalBytes() const;
186     virtual std::unique_ptr<PlatformTimeRanges> platformBufferedTimeRanges() const;
187     virtual MediaTime platformMinTimeSeekable() const;
188     virtual MediaTime platformMaxTimeSeekable() const;
189     virtual MediaTime platformDuration() const;
190     virtual MediaTime platformMaxTimeLoaded() const;
191     virtual void beginLoadingMetadata();
192     virtual void sizeChanged();
193
194     virtual bool hasAvailableVideoFrame() const;
195
196     virtual void createContextVideoRenderer();
197     virtual void destroyContextVideoRenderer();
198
199     virtual void createVideoLayer();
200     virtual void destroyVideoLayer();
201
202     virtual bool hasContextRenderer() const;
203     virtual bool hasLayerRenderer() const;
204
205     virtual void updateVideoLayerGravity() override;
206
207     virtual bool hasSingleSecurityOrigin() const;
208
209 #if ENABLE(VIDEO_TRACK)
210     virtual bool requiresTextTrackRepresentation() const override;
211     virtual void setTextTrackRepresentation(TextTrackRepresentation*) override;
212     virtual void syncTextTrackBounds() override;
213 #endif
214
215     void setAVPlayerItem(AVPlayerItem *);
216
217 #if ENABLE(WEB_AUDIO) && USE(MEDIATOOLBOX)
218     virtual AudioSourceProvider* audioSourceProvider();
219 #endif
220
221     void createImageGenerator();
222     void destroyImageGenerator();
223     RetainPtr<CGImageRef> createImageForTimeInRect(float, const FloatRect&);
224     void paintWithImageGenerator(GraphicsContext*, const FloatRect&);
225
226 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
227     void createVideoOutput();
228     void destroyVideoOutput();
229     RetainPtr<CVPixelBufferRef> createPixelBuffer();
230     void updateLastImage();
231     bool videoOutputHasAvailableFrame();
232     void paintWithVideoOutput(GraphicsContext*, const FloatRect&);
233     virtual PassNativeImagePtr nativeImageForCurrentTime() override;
234     void waitForVideoOutputMediaDataWillChange();
235 #endif
236
237 #if ENABLE(ENCRYPTED_MEDIA)
238     virtual MediaPlayer::MediaKeyException addKey(const String&, const unsigned char*, unsigned, const unsigned char*, unsigned, const String&);
239     virtual MediaPlayer::MediaKeyException generateKeyRequest(const String&, const unsigned char*, unsigned);
240     virtual MediaPlayer::MediaKeyException cancelKeyRequest(const String&, const String&);
241 #endif
242
243 #if ENABLE(ENCRYPTED_MEDIA_V2)
244     std::unique_ptr<CDMSession> createSession(const String& keySystem);
245 #endif
246
247     virtual String languageOfPrimaryAudioTrack() const override;
248
249 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
250     void processMediaSelectionOptions();
251     bool hasLoadedMediaSelectionGroups();
252
253     AVMediaSelectionGroup* safeMediaSelectionGroupForLegibleMedia();
254     AVMediaSelectionGroup* safeMediaSelectionGroupForAudibleMedia();
255     AVMediaSelectionGroup* safeMediaSelectionGroupForVisualMedia();
256 #endif
257
258 #if ENABLE(DATACUE_VALUE)
259     void processMetadataTrack();
260 #endif
261
262     virtual void setCurrentTextTrack(InbandTextTrackPrivateAVF*) override;
263     virtual InbandTextTrackPrivateAVF* currentTextTrack() const override { return m_currentTextTrack; }
264
265 #if !HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
266     void processLegacyClosedCaptionsTracks();
267 #endif
268
269 #if ENABLE(VIDEO_TRACK)
270     void updateAudioTracks();
271     void updateVideoTracks();
272 #endif
273
274 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
275     virtual bool isCurrentPlaybackTargetWireless() const override;
276     virtual String wirelessPlaybackTargetName() const override;
277     virtual MediaPlayer::WirelessPlaybackTargetType wirelessPlaybackTargetType() const override;
278     virtual bool wirelessVideoPlaybackDisabled() const override;
279 #if !PLATFORM(IOS)
280     virtual void setWirelessPlaybackTarget(const MediaPlaybackTarget&) override;
281 #endif
282     virtual void setWirelessVideoPlaybackDisabled(bool) override;
283     void updateDisableExternalPlayback();
284 #endif
285
286     virtual double maxFastForwardRate() const override { return m_cachedCanPlayFastForward ? std::numeric_limits<double>::infinity() : 2.0; }
287     virtual double minFastReverseRate() const override { return m_cachedCanPlayFastReverse ? -std::numeric_limits<double>::infinity() : 0.0; }
288
289     virtual URL resolvedURL() const override;
290
291     WeakPtrFactory<MediaPlayerPrivateAVFoundationObjC> m_weakPtrFactory;
292
293     RetainPtr<AVURLAsset> m_avAsset;
294     RetainPtr<AVPlayer> m_avPlayer;
295     RetainPtr<AVPlayerItem> m_avPlayerItem;
296     RetainPtr<AVPlayerLayer> m_videoLayer;
297 #if PLATFORM(IOS)
298     RetainPtr<PlatformLayer> m_videoInlineLayer;
299     RetainPtr<PlatformLayer> m_videoFullscreenLayer;
300     FloatRect m_videoFullscreenFrame;
301     MediaPlayer::VideoGravity m_videoFullscreenGravity;
302     RetainPtr<PlatformLayer> m_textTrackRepresentationLayer;
303 #endif
304     RetainPtr<WebCoreAVFMovieObserver> m_objcObserver;
305     RetainPtr<id> m_timeObserver;
306     mutable String m_languageOfPrimaryAudioTrack;
307     bool m_videoFrameHasDrawn;
308     bool m_haveCheckedPlayability;
309
310 #if ENABLE(WEB_AUDIO) && USE(MEDIATOOLBOX)
311     RefPtr<AudioSourceProviderAVFObjC> m_provider;
312 #endif
313
314     RetainPtr<AVAssetImageGenerator> m_imageGenerator;
315 #if HAVE(AVFOUNDATION_VIDEO_OUTPUT)
316     RetainPtr<AVPlayerItemVideoOutput> m_videoOutput;
317     RetainPtr<WebCoreAVFPullDelegate> m_videoOutputDelegate;
318     RetainPtr<CGImageRef> m_lastImage;
319     dispatch_semaphore_t m_videoOutputSemaphore;
320 #endif
321
322 #if USE(VIDEOTOOLBOX)
323     RetainPtr<VTPixelTransferSessionRef> m_pixelTransferSession;
324 #endif
325
326 #if HAVE(AVFOUNDATION_LOADER_DELEGATE)
327     friend class WebCoreAVFResourceLoader;
328     HashMap<RetainPtr<AVAssetResourceLoadingRequest>, RefPtr<WebCoreAVFResourceLoader>> m_resourceLoaderMap;
329     RetainPtr<WebCoreAVFLoaderDelegate> m_loaderDelegate;
330     HashMap<String, RetainPtr<AVAssetResourceLoadingRequest>> m_keyURIToRequestMap;
331     HashMap<String, RetainPtr<AVAssetResourceLoadingRequest>> m_sessionIDToRequestMap;
332 #endif
333
334 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
335     RetainPtr<AVPlayerItemLegibleOutput> m_legibleOutput;
336 #endif
337
338 #if ENABLE(VIDEO_TRACK)
339     Vector<RefPtr<AudioTrackPrivateAVFObjC>> m_audioTracks;
340     Vector<RefPtr<VideoTrackPrivateAVFObjC>> m_videoTracks;
341 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
342     RefPtr<MediaSelectionGroupAVFObjC> m_audibleGroup;
343     RefPtr<MediaSelectionGroupAVFObjC> m_visualGroup;
344 #endif
345 #endif
346
347     InbandTextTrackPrivateAVF* m_currentTextTrack;
348
349 #if ENABLE(DATACUE_VALUE)
350     RefPtr<InbandMetadataTextTrackPrivateAVF> m_metadataTrack;
351 #endif
352
353 #if PLATFORM(MAC) && ENABLE(WIRELESS_PLAYBACK_TARGET)
354     RetainPtr<AVOutputContext> m_outputContext;
355 #endif
356
357     mutable RetainPtr<NSArray> m_cachedSeekableRanges;
358     mutable RetainPtr<NSArray> m_cachedLoadedRanges;
359     RetainPtr<NSArray> m_cachedTracks;
360     RetainPtr<NSArray> m_currentMetaData;
361     FloatSize m_cachedPresentationSize;
362     MediaTime m_cachedDuration;
363     double m_cachedRate;
364     mutable long long m_cachedTotalBytes;
365     unsigned m_pendingStatusChanges;
366     int m_cachedItemStatus;
367     bool m_cachedLikelyToKeepUp;
368     bool m_cachedBufferEmpty;
369     bool m_cachedBufferFull;
370     bool m_cachedHasEnabledAudio;
371     bool m_shouldBufferData;
372     bool m_cachedIsReadyForDisplay;
373     bool m_haveBeenAskedToCreateLayer;
374     bool m_cachedCanPlayFastForward;
375     bool m_cachedCanPlayFastReverse;
376 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
377     mutable bool m_allowsWirelessVideoPlayback;
378 #endif
379 };
380
381 }
382
383 #endif
384 #endif