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