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