2 * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
31 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
32 #include "MediaPlayerProxy.h"
38 #include <wtf/Forward.h>
39 #include <wtf/HashSet.h>
40 #include <wtf/OwnPtr.h>
41 #include <wtf/Noncopyable.h>
42 #include <wtf/PassOwnPtr.h>
43 #include <wtf/text/StringHash.h>
45 #if USE(ACCELERATED_COMPOSITING)
46 #include "GraphicsLayer.h"
54 class QTMovieVisualContext;
58 class AudioSourceProvider;
59 class GStreamerGWorld;
60 class MediaPlayerPrivateInterface;
62 // Structure that will hold every native
63 // types supported by the current media player.
64 // We have to do that has multiple media players
65 // backend can live at runtime.
66 struct PlatformMedia {
71 QTMovieVisualContextType,
73 ChromiumMediaPlayerType,
75 AVFoundationMediaPlayerType,
76 AVFoundationCFMediaPlayerType
81 QTMovieGWorld* qtMovieGWorld;
82 QTMovieVisualContext* qtMovieVisualContext;
83 GStreamerGWorld* gstreamerGWorld;
84 MediaPlayerPrivateInterface* chromiumMediaPlayer;
85 MediaPlayerPrivateInterface* qtMediaPlayer;
86 AVPlayer* avfMediaPlayer;
87 AVCFPlayer* avcfMediaPlayer;
91 extern const PlatformMedia NoPlatformMedia;
95 class GraphicsContext;
99 struct MediaPlayerFactory;
103 #if PLATFORM(WIN) && USE(AVFOUNDATION)
104 struct GraphicsDeviceAdapter;
107 class MediaPlayerClient {
109 enum CORSMode { Unspecified, Anonymous, UseCredentials };
111 virtual ~MediaPlayerClient() { }
113 // Get the document which the media player is owned by
114 virtual Document* mediaPlayerOwningDocument() { return 0; }
116 // the network state has changed
117 virtual void mediaPlayerNetworkStateChanged(MediaPlayer*) { }
119 // the ready state has changed
120 virtual void mediaPlayerReadyStateChanged(MediaPlayer*) { }
122 // the volume state has changed
123 virtual void mediaPlayerVolumeChanged(MediaPlayer*) { }
125 // the mute state has changed
126 virtual void mediaPlayerMuteChanged(MediaPlayer*) { }
128 // time has jumped, eg. not as a result of normal playback
129 virtual void mediaPlayerTimeChanged(MediaPlayer*) { }
131 // the media file duration has changed, or is now known
132 virtual void mediaPlayerDurationChanged(MediaPlayer*) { }
134 // the playback rate has changed
135 virtual void mediaPlayerRateChanged(MediaPlayer*) { }
137 // the play/pause status changed
138 virtual void mediaPlayerPlaybackStateChanged(MediaPlayer*) { }
140 // The MediaPlayer has found potentially problematic media content.
141 // This is used internally to trigger swapping from a <video>
142 // element to an <embed> in standalone documents
143 virtual void mediaPlayerSawUnsupportedTracks(MediaPlayer*) { }
145 // The MediaPlayer could not discover an engine which supports the requested resource.
146 virtual void mediaPlayerResourceNotSupported(MediaPlayer*) { }
148 // Presentation-related methods
149 // a new frame of video is available
150 virtual void mediaPlayerRepaint(MediaPlayer*) { }
152 // the movie size has changed
153 virtual void mediaPlayerSizeChanged(MediaPlayer*) { }
155 virtual void mediaPlayerEngineUpdated(MediaPlayer*) { }
157 // The first frame of video is available to render. A media engine need only make this callback if the
158 // first frame is not available immediately when prepareForRendering is called.
159 virtual void mediaPlayerFirstVideoFrameAvailable(MediaPlayer*) { }
161 // A characteristic of the media file, eg. video, audio, closed captions, etc, has changed.
162 virtual void mediaPlayerCharacteristicChanged(MediaPlayer*) { }
164 #if USE(ACCELERATED_COMPOSITING)
165 // whether the rendering system can accelerate the display of this MediaPlayer.
166 virtual bool mediaPlayerRenderingCanBeAccelerated(MediaPlayer*) { return false; }
168 // called when the media player's rendering mode changed, which indicates a change in the
169 // availability of the platformLayer().
170 virtual void mediaPlayerRenderingModeChanged(MediaPlayer*) { }
173 #if PLATFORM(WIN) && USE(AVFOUNDATION)
174 virtual GraphicsDeviceAdapter* mediaPlayerGraphicsDeviceAdapter(const MediaPlayer*) const { return 0; }
177 #if ENABLE(MEDIA_SOURCE)
178 virtual void mediaPlayerSourceOpened() { }
179 virtual String mediaPlayerSourceURL() const { return "x-media-source-unsupported:"; }
182 #if ENABLE(ENCRYPTED_MEDIA)
183 enum MediaKeyErrorCode { UnknownError = 1, ClientError, ServiceError, OutputError, HardwareChangeError, DomainError };
184 virtual void mediaPlayerKeyAdded(MediaPlayer*, const String& keySystem, const String& sessionId) { }
185 virtual void mediaPlayerKeyError(MediaPlayer*, const String& keySystem, const String& sessionId, MediaKeyErrorCode errorCode, unsigned short systemCode) { }
186 virtual void mediaPlayerKeyMessage(MediaPlayer*, const String& keySystem, const String& sessionId, const unsigned char* message, unsigned messageLength) { }
187 virtual void mediaPlayerKeyNeeded(MediaPlayer*, const String& keySystem, const String& sessionId, const unsigned char* initData, unsigned initDataLength) { }
190 virtual String mediaPlayerReferrer() const { return String(); }
191 virtual String mediaPlayerUserAgent() const { return String(); }
192 virtual CORSMode mediaPlayerCORSMode() const { return Unspecified; }
193 virtual void mediaPlayerExitFullscreen() { }
194 virtual bool mediaPlayerIsVideo() const { return false; }
195 virtual LayoutRect mediaPlayerContentBoxRect() const { return LayoutRect(); }
196 virtual void mediaPlayerSetSize(const IntSize&) { }
197 virtual void mediaPlayerPause() { }
198 virtual void mediaPlayerPlay() { }
199 virtual bool mediaPlayerIsPaused() const { return true; }
200 virtual bool mediaPlayerIsLooping() const { return false; }
201 virtual HostWindow* mediaPlayerHostWindow() { return 0; }
202 virtual IntRect mediaPlayerWindowClipRect() { return IntRect(); }
205 class MediaPlayerSupportsTypeClient {
207 virtual ~MediaPlayerSupportsTypeClient() { }
209 virtual bool mediaPlayerNeedsSiteSpecificHacks() const { return false; }
210 virtual String mediaPlayerDocumentHost() const { return String(); }
214 WTF_MAKE_NONCOPYABLE(MediaPlayer); WTF_MAKE_FAST_ALLOCATED;
217 static PassOwnPtr<MediaPlayer> create(MediaPlayerClient* client)
219 return adoptPtr(new MediaPlayer(client));
221 virtual ~MediaPlayer();
223 // Media engine support.
224 enum SupportsType { IsNotSupported, IsSupported, MayBeSupported };
225 static MediaPlayer::SupportsType supportsType(const ContentType&, const String& keySystem, const KURL&, const MediaPlayerSupportsTypeClient*);
226 static void getSupportedTypes(HashSet<String>&);
227 static bool isAvailable();
228 static void getSitesInMediaCache(Vector<String>&);
229 static void clearMediaCache();
230 static void clearMediaCacheForSite(const String&);
232 bool supportsFullscreen() const;
233 bool supportsSave() const;
234 bool supportsScanning() const;
235 PlatformMedia platformMedia() const;
236 #if USE(ACCELERATED_COMPOSITING)
237 PlatformLayer* platformLayer() const;
240 IntSize naturalSize();
241 bool hasVideo() const;
242 bool hasAudio() const;
244 #if !ENABLE(PLUGIN_PROXY_FOR_VIDEO)
245 void enterFullscreen();
246 void exitFullscreen();
248 void setFrameView(FrameView* frameView) { m_frameView = frameView; }
249 FrameView* frameView() { return m_frameView; }
250 bool inMediaDocument();
252 IntSize size() const { return m_size; }
253 void setSize(const IntSize& size);
255 bool load(const KURL&, const ContentType&, const String& keySystem);
258 bool visible() const;
259 void setVisible(bool);
261 void prepareToPlay();
265 #if ENABLE(MEDIA_SOURCE)
266 enum AddIdStatus { Ok, NotSupported, ReachedIdLimit };
267 AddIdStatus sourceAddId(const String& id, const String& type, const Vector<String>& codecs);
268 bool sourceRemoveId(const String& id);
269 PassRefPtr<TimeRanges> sourceBuffered(const String& id);
270 bool sourceAppend(const String& id, const unsigned char* data, unsigned length);
271 bool sourceAbort(const String& id);
272 enum EndOfStreamStatus { EosNoError, EosNetworkError, EosDecodeError };
273 void sourceEndOfStream(EndOfStreamStatus);
276 #if ENABLE(ENCRYPTED_MEDIA)
277 // Represents synchronous exceptions that can be thrown from the Encrypted Media methods.
278 // This is different from the asynchronous MediaKeyError.
279 enum MediaKeyException { NoError, InvalidPlayerState, KeySystemNotSupported };
281 MediaKeyException generateKeyRequest(const String& keySystem, const unsigned char* initData, unsigned initDataLength);
282 MediaKeyException addKey(const String& keySystem, const unsigned char* key, unsigned keyLength, const unsigned char* initData, unsigned initDataLength, const String& sessionId);
283 MediaKeyException cancelKeyRequest(const String& keySystem, const String& sessionId);
287 bool seeking() const;
289 static float invalidTime() { return -1.0f;}
290 float duration() const;
291 float currentTime() const;
292 void seek(float time);
294 float startTime() const;
296 double initialTime() const;
301 bool preservesPitch() const;
302 void setPreservesPitch(bool);
304 PassRefPtr<TimeRanges> buffered();
305 PassRefPtr<TimeRanges> seekable();
306 float maxTimeSeekable();
308 bool didLoadingProgress();
310 float volume() const;
311 void setVolume(float);
316 bool hasClosedCaptions() const;
317 void setClosedCaptionsVisible(bool closedCaptionsVisible);
319 bool autoplay() const;
320 void setAutoplay(bool);
322 void paint(GraphicsContext*, const IntRect&);
323 void paintCurrentFrameInContext(GraphicsContext*, const IntRect&);
325 enum NetworkState { Empty, Idle, Loading, Loaded, FormatError, NetworkError, DecodeError };
326 NetworkState networkState();
328 enum ReadyState { HaveNothing, HaveMetadata, HaveCurrentData, HaveFutureData, HaveEnoughData };
329 ReadyState readyState();
331 enum MovieLoadType { Unknown, Download, StoredStream, LiveStream };
332 MovieLoadType movieLoadType() const;
334 enum Preload { None, MetaData, Auto };
335 Preload preload() const;
336 void setPreload(Preload);
338 void networkStateChanged();
339 void readyStateChanged();
340 void volumeChanged(float);
341 void muteChanged(bool);
345 void playbackStateChanged();
346 void durationChanged();
347 void firstVideoFrameAvailable();
348 void characteristicChanged();
352 MediaPlayerClient* mediaPlayerClient() const { return m_mediaPlayerClient; }
354 bool hasAvailableVideoFrame() const;
355 void prepareForRendering();
357 bool canLoadPoster() const;
358 void setPoster(const String&);
360 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
361 void deliverNotification(MediaPlayerProxyNotificationType notification);
362 void setMediaPlayerProxy(WebMediaPlayerProxy* proxy);
363 void setControls(bool);
366 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO) || USE(NATIVE_FULLSCREEN_VIDEO)
367 void enterFullscreen();
368 void exitFullscreen();
371 #if USE(NATIVE_FULLSCREEN_VIDEO)
372 bool canEnterFullscreen() const;
375 #if USE(ACCELERATED_COMPOSITING)
376 // whether accelerated rendering is supported by the media engine for the current media.
377 bool supportsAcceleratedRendering() const;
378 // called when the rendering system flips the into or out of accelerated rendering mode.
379 void acceleratedRenderingStateChanged();
382 #if PLATFORM(WIN) && USE(AVFOUNDATION)
383 GraphicsDeviceAdapter* graphicsDeviceAdapter() const;
386 bool hasSingleSecurityOrigin() const;
388 bool didPassCORSAccessCheck() const;
390 float mediaTimeForTimeValue(float) const;
392 double maximumDurationToCacheMediaTime() const;
394 unsigned decodedFrameCount() const;
395 unsigned droppedFrameCount() const;
396 unsigned audioDecodedByteCount() const;
397 unsigned videoDecodedByteCount() const;
399 void setPrivateBrowsingMode(bool);
401 #if ENABLE(WEB_AUDIO)
402 AudioSourceProvider* audioSourceProvider();
405 #if ENABLE(MEDIA_SOURCE)
407 String sourceURL() const;
410 #if ENABLE(ENCRYPTED_MEDIA)
411 void keyAdded(const String& keySystem, const String& sessionId);
412 void keyError(const String& keySystem, const String& sessionId, MediaPlayerClient::MediaKeyErrorCode, unsigned short systemCode);
413 void keyMessage(const String& keySystem, const String& sessionId, const unsigned char* message, unsigned messageLength);
414 void keyNeeded(const String& keySystem, const String& sessionId, const unsigned char* initData, unsigned initDataLength);
417 String referrer() const;
418 String userAgent() const;
420 String engineDescription() const;
423 MediaPlayer(MediaPlayerClient*);
424 void loadWithNextMediaEngine(MediaPlayerFactory*);
425 void reloadTimerFired(Timer<MediaPlayer>*);
427 static void initializeMediaEngines();
429 MediaPlayerClient* m_mediaPlayerClient;
430 Timer<MediaPlayer> m_reloadTimer;
431 OwnPtr<MediaPlayerPrivateInterface> m_private;
432 MediaPlayerFactory* m_currentMediaEngine;
434 String m_contentMIMEType;
435 String m_contentTypeCodecs;
437 FrameView* m_frameView;
444 bool m_preservesPitch;
445 bool m_privateBrowsing;
446 bool m_shouldPrepareToRender;
447 bool m_contentMIMETypeWasInferredFromExtension;
448 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
449 WebMediaPlayerProxy* m_playerProxy; // not owned or used, passed to m_private
453 typedef PassOwnPtr<MediaPlayerPrivateInterface> (*CreateMediaEnginePlayer)(MediaPlayer*);
454 typedef void (*MediaEngineSupportedTypes)(HashSet<String>& types);
455 #if ENABLE(ENCRYPTED_MEDIA)
456 typedef MediaPlayer::SupportsType (*MediaEngineSupportsType)(const String& type, const String& codecs, const String& keySystem, const KURL& url);
458 typedef MediaPlayer::SupportsType (*MediaEngineSupportsType)(const String& type, const String& codecs, const KURL& url);
460 typedef void (*MediaEngineGetSitesInMediaCache)(Vector<String>&);
461 typedef void (*MediaEngineClearMediaCache)();
462 typedef void (*MediaEngineClearMediaCacheForSite)(const String&);
464 typedef void (*MediaEngineRegistrar)(CreateMediaEnginePlayer, MediaEngineSupportedTypes, MediaEngineSupportsType,
465 MediaEngineGetSitesInMediaCache, MediaEngineClearMediaCache, MediaEngineClearMediaCacheForSite);
469 #endif // ENABLE(VIDEO)