fb0b44c5d5134fe48ac004435ef0800f3e12396b
[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 WebCoreAVFMovieObserver;
46
47 typedef struct objc_object* id;
48
49 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
50 OBJC_CLASS WebCoreAVFLoaderDelegate;
51 OBJC_CLASS AVAssetResourceLoadingRequest;
52 #endif
53
54 typedef struct CGImage *CGImageRef;
55 typedef struct __CVBuffer *CVPixelBufferRef;
56 typedef struct OpaqueVTPixelTransferSession* VTPixelTransferSessionRef;
57
58 namespace WebCore {
59
60 class WebCoreAVFResourceLoader;
61 class InbandTextTrackPrivateAVFObjC;
62 class AudioTrackPrivateAVFObjC;
63
64 class MediaPlayerPrivateAVFoundationObjC : public MediaPlayerPrivateAVFoundation {
65 public:
66     ~MediaPlayerPrivateAVFoundationObjC();
67
68     static void registerMediaEngine(MediaEngineRegistrar);
69
70     void setAsset(id);
71     virtual void tracksChanged();
72
73 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
74     RetainPtr<AVPlayerItem> playerItem() const { return m_avPlayerItem; }
75     void processCue(NSArray *, double);
76     void flushCues();
77 #endif
78     
79 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
80     bool shouldWaitForLoadingOfResource(AVAssetResourceLoadingRequest*);
81     void didCancelLoadingRequest(AVAssetResourceLoadingRequest*);
82     void didStopLoadingRequest(AVAssetResourceLoadingRequest *);
83 #endif
84
85 #if ENABLE(ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA_V2)
86     static bool extractKeyURIKeyIDAndCertificateFromInitData(Uint8Array* initData, String& keyURI, String& keyID, RefPtr<Uint8Array>& certificate);
87 #endif
88
89 #if ENABLE(ENCRYPTED_MEDIA_V2)
90     static RetainPtr<AVAssetResourceLoadingRequest> takeRequestForPlayerAndKeyURI(MediaPlayer*, const String&);
91 #endif
92
93 private:
94     MediaPlayerPrivateAVFoundationObjC(MediaPlayer*);
95
96     // engine support
97     static PassOwnPtr<MediaPlayerPrivateInterface> create(MediaPlayer*);
98     static void getSupportedTypes(HashSet<String>& types);
99     static MediaPlayer::SupportsType supportsType(const String& type, const String& codecs, const URL&);
100 #if ENABLE(ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA_V2)
101     static MediaPlayer::SupportsType extendedSupportsType(const String& type, const String& codecs, const String& keySystem, const URL&);
102 #endif
103
104     static bool isAvailable();
105
106     virtual void cancelLoad();
107
108     virtual PlatformMedia platformMedia() const;
109
110     virtual void platformSetVisible(bool);
111     virtual void platformPlay();
112     virtual void platformPause();
113     virtual float currentTime() const;
114     virtual void setVolume(float);
115     virtual void setClosedCaptionsVisible(bool);
116     virtual void paint(GraphicsContext*, const IntRect&);
117     virtual void paintCurrentFrameInContext(GraphicsContext*, const IntRect&);
118     virtual PlatformLayer* platformLayer() const;
119     virtual bool supportsAcceleratedRendering() const { return true; }
120     virtual float mediaTimeForTimeValue(float) const;
121
122     virtual void createAVPlayer();
123     virtual void createAVPlayerItem();
124     virtual void createAVAssetForURL(const String& url);
125     virtual MediaPlayerPrivateAVFoundation::ItemStatus playerItemStatus() const;
126     virtual MediaPlayerPrivateAVFoundation::AssetStatus assetStatus() const;
127
128     virtual void checkPlayability();
129     virtual void updateRate();
130     virtual float rate() const;
131     virtual void seekToTime(double time);
132     virtual unsigned totalBytes() const;
133     virtual PassRefPtr<TimeRanges> platformBufferedTimeRanges() const;
134     virtual double platformMinTimeSeekable() const;
135     virtual double platformMaxTimeSeekable() const;
136     virtual float platformDuration() const;
137     virtual float platformMaxTimeLoaded() const;
138     virtual void beginLoadingMetadata();
139     virtual void sizeChanged();
140
141     virtual bool hasAvailableVideoFrame() const;
142
143     virtual void createContextVideoRenderer();
144     virtual void destroyContextVideoRenderer();
145
146     virtual void createVideoLayer();
147     virtual void destroyVideoLayer();
148
149     virtual bool hasContextRenderer() const;
150     virtual bool hasLayerRenderer() const;
151
152     virtual void updateVideoLayerGravity() OVERRIDE;
153
154     virtual bool hasSingleSecurityOrigin() const;
155     
156 #if __MAC_OS_X_VERSION_MIN_REQUIRED < 1080
157     void createImageGenerator();
158     void destroyImageGenerator();
159     RetainPtr<CGImageRef> createImageForTimeInRect(float, const IntRect&);
160     void paintWithImageGenerator(GraphicsContext*, const IntRect&);
161 #else
162     void createVideoOutput();
163     void destroyVideoOutput();
164     RetainPtr<CVPixelBufferRef> createPixelBuffer();
165     void paintWithVideoOutput(GraphicsContext*, const IntRect&);
166 #endif
167
168 #if ENABLE(ENCRYPTED_MEDIA)
169     virtual MediaPlayer::MediaKeyException addKey(const String&, const unsigned char*, unsigned, const unsigned char*, unsigned, const String&);
170     virtual MediaPlayer::MediaKeyException generateKeyRequest(const String&, const unsigned char*, unsigned);
171     virtual MediaPlayer::MediaKeyException cancelKeyRequest(const String&, const String&);
172 #endif
173
174     virtual String languageOfPrimaryAudioTrack() const OVERRIDE;
175
176 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP)
177     void processMediaSelectionOptions();
178     AVMediaSelectionGroup* safeMediaSelectionGroupForLegibleMedia();
179 #endif
180
181     virtual void setCurrentTrack(InbandTextTrackPrivateAVF*) OVERRIDE;
182     virtual InbandTextTrackPrivateAVF* currentTrack() const OVERRIDE { return m_currentTrack; }
183
184 #if !HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
185     void processLegacyClosedCaptionsTracks();
186 #endif
187
188     RetainPtr<AVURLAsset> m_avAsset;
189     RetainPtr<AVPlayer> m_avPlayer;
190     RetainPtr<AVPlayerItem> m_avPlayerItem;
191     RetainPtr<AVPlayerLayer> m_videoLayer;
192     RetainPtr<WebCoreAVFMovieObserver> m_objcObserver;
193     RetainPtr<id> m_timeObserver;
194     mutable String m_languageOfPrimaryAudioTrack;
195     bool m_videoFrameHasDrawn;
196     bool m_haveCheckedPlayability;
197
198 #if __MAC_OS_X_VERSION_MIN_REQUIRED < 1080
199     RetainPtr<AVAssetImageGenerator> m_imageGenerator;
200 #else
201     RetainPtr<AVPlayerItemVideoOutput> m_videoOutput;
202     RetainPtr<CVPixelBufferRef> m_lastImage;
203 #endif
204
205 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
206     RetainPtr<VTPixelTransferSessionRef> m_pixelTransferSession;
207
208     friend class WebCoreAVFResourceLoader;
209     HashMap<RetainPtr<AVAssetResourceLoadingRequest>, RefPtr<WebCoreAVFResourceLoader> > m_resourceLoaderMap;
210     RetainPtr<WebCoreAVFLoaderDelegate> m_loaderDelegate;
211     HashMap<String, RetainPtr<AVAssetResourceLoadingRequest> > m_keyURIToRequestMap;
212     HashMap<String, RetainPtr<AVAssetResourceLoadingRequest> > m_sessionIDToRequestMap;
213 #endif
214
215 #if HAVE(AVFOUNDATION_MEDIA_SELECTION_GROUP) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
216     RetainPtr<AVPlayerItemLegibleOutput> m_legibleOutput;
217 #endif
218
219 #if ENABLE(VIDEO_TRACK)
220     Vector<RefPtr<AudioTrackPrivateAVFObjC>> m_audioTracks;
221 #endif
222
223     InbandTextTrackPrivateAVF* m_currentTrack;
224 };
225
226 }
227
228 #endif
229 #endif