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