tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / MediaPlayer.h
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 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 MediaPlayer_h
27 #define MediaPlayer_h
28
29 #if ENABLE(VIDEO)
30
31 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
32 #include "MediaPlayerProxy.h"
33 #endif
34
35 #include "Document.h"
36 #include "IntRect.h"
37 #include "KURL.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>
44
45 #if USE(ACCELERATED_COMPOSITING)
46 #include "GraphicsLayer.h"
47 #endif
48
49 OBJC_CLASS AVPlayer;
50 OBJC_CLASS QTMovie;
51
52 class AVCFPlayer;
53 class QTMovieGWorld;
54 class QTMovieVisualContext;
55
56 namespace WebCore {
57
58 class AudioSourceProvider;
59 class GStreamerGWorld;
60 class MediaPlayerPrivateInterface;
61
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 {
67     enum {
68         None,
69         QTMovieType,
70         QTMovieGWorldType,
71         QTMovieVisualContextType,
72         GStreamerGWorldType,
73         ChromiumMediaPlayerType,
74         QtMediaPlayerType,
75         AVFoundationMediaPlayerType,
76         AVFoundationCFMediaPlayerType
77     } type;
78
79     union {
80         QTMovie* qtMovie;
81         QTMovieGWorld* qtMovieGWorld;
82         QTMovieVisualContext* qtMovieVisualContext;
83         GStreamerGWorld* gstreamerGWorld;
84         MediaPlayerPrivateInterface* chromiumMediaPlayer;
85         MediaPlayerPrivateInterface* qtMediaPlayer;
86         AVPlayer* avfMediaPlayer;
87         AVCFPlayer* avcfMediaPlayer;
88     } media;
89 };
90
91 extern const PlatformMedia NoPlatformMedia;
92
93 class ContentType;
94 class FrameView;
95 class GraphicsContext;
96 class IntRect;
97 class IntSize;
98 class MediaPlayer;
99 struct MediaPlayerFactory;
100 class TimeRanges;
101 class HostWindow;
102
103 #if PLATFORM(WIN) && USE(AVFOUNDATION)
104 struct GraphicsDeviceAdapter;
105 #endif
106
107 class MediaPlayerClient {
108 public:
109     enum CORSMode { Unspecified, Anonymous, UseCredentials };
110
111     virtual ~MediaPlayerClient() { }
112
113     // Get the document which the media player is owned by
114     virtual Document* mediaPlayerOwningDocument() { return 0; }
115
116     // the network state has changed
117     virtual void mediaPlayerNetworkStateChanged(MediaPlayer*) { }
118
119     // the ready state has changed
120     virtual void mediaPlayerReadyStateChanged(MediaPlayer*) { }
121
122     // the volume state has changed
123     virtual void mediaPlayerVolumeChanged(MediaPlayer*) { }
124
125     // the mute state has changed
126     virtual void mediaPlayerMuteChanged(MediaPlayer*) { }
127
128     // time has jumped, eg. not as a result of normal playback
129     virtual void mediaPlayerTimeChanged(MediaPlayer*) { }
130
131     // the media file duration has changed, or is now known
132     virtual void mediaPlayerDurationChanged(MediaPlayer*) { }
133
134     // the playback rate has changed
135     virtual void mediaPlayerRateChanged(MediaPlayer*) { }
136
137     // the play/pause status changed
138     virtual void mediaPlayerPlaybackStateChanged(MediaPlayer*) { }
139
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*) { }
144
145     // The MediaPlayer could not discover an engine which supports the requested resource.
146     virtual void mediaPlayerResourceNotSupported(MediaPlayer*) { }
147
148 // Presentation-related methods
149     // a new frame of video is available
150     virtual void mediaPlayerRepaint(MediaPlayer*) { }
151
152     // the movie size has changed
153     virtual void mediaPlayerSizeChanged(MediaPlayer*) { }
154
155     virtual void mediaPlayerEngineUpdated(MediaPlayer*) { }
156
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*) { }
160
161     // A characteristic of the media file, eg. video, audio, closed captions, etc, has changed.
162     virtual void mediaPlayerCharacteristicChanged(MediaPlayer*) { }
163     
164 #if USE(ACCELERATED_COMPOSITING)
165     // whether the rendering system can accelerate the display of this MediaPlayer.
166     virtual bool mediaPlayerRenderingCanBeAccelerated(MediaPlayer*) { return false; }
167
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*) { }
171 #endif
172
173 #if PLATFORM(WIN) && USE(AVFOUNDATION)
174     virtual GraphicsDeviceAdapter* mediaPlayerGraphicsDeviceAdapter(const MediaPlayer*) const { return 0; }
175 #endif
176
177 #if ENABLE(MEDIA_SOURCE)
178     virtual void mediaPlayerSourceOpened() { }
179     virtual String mediaPlayerSourceURL() const { return "x-media-source-unsupported:"; }
180 #endif
181
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) { }
188 #endif
189
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(); }
203 };
204
205 class MediaPlayerSupportsTypeClient {
206 public:
207     virtual ~MediaPlayerSupportsTypeClient() { }
208
209     virtual bool mediaPlayerNeedsSiteSpecificHacks() const { return false; }
210     virtual String mediaPlayerDocumentHost() const { return String(); }
211 };
212
213 class MediaPlayer {
214     WTF_MAKE_NONCOPYABLE(MediaPlayer); WTF_MAKE_FAST_ALLOCATED;
215 public:
216
217     static PassOwnPtr<MediaPlayer> create(MediaPlayerClient* client)
218     {
219         return adoptPtr(new MediaPlayer(client));
220     }
221     virtual ~MediaPlayer();
222
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&);
231
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;
238 #endif
239
240     IntSize naturalSize();
241     bool hasVideo() const;
242     bool hasAudio() const;
243
244 #if !ENABLE(PLUGIN_PROXY_FOR_VIDEO)
245     void enterFullscreen();
246     void exitFullscreen();
247 #endif
248     void setFrameView(FrameView* frameView) { m_frameView = frameView; }
249     FrameView* frameView() { return m_frameView; }
250     bool inMediaDocument();
251
252     IntSize size() const { return m_size; }
253     void setSize(const IntSize& size);
254
255     bool load(const KURL&, const ContentType&, const String& keySystem);
256     void cancelLoad();
257
258     bool visible() const;
259     void setVisible(bool);
260
261     void prepareToPlay();
262     void play();
263     void pause();    
264
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);
274 #endif
275
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 };
280
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);
284 #endif
285
286     bool paused() const;
287     bool seeking() const;
288
289     static float invalidTime() { return -1.0f;}
290     float duration() const;
291     float currentTime() const;
292     void seek(float time);
293
294     float startTime() const;
295
296     double initialTime() const;
297
298     float rate() const;
299     void setRate(float);
300
301     bool preservesPitch() const;    
302     void setPreservesPitch(bool);
303
304     PassRefPtr<TimeRanges> buffered();
305     PassRefPtr<TimeRanges> seekable();
306     float maxTimeSeekable();
307
308     bool didLoadingProgress();
309
310     float volume() const;
311     void setVolume(float);
312
313     bool muted() const;
314     void setMuted(bool);
315
316     bool hasClosedCaptions() const;
317     void setClosedCaptionsVisible(bool closedCaptionsVisible);
318
319     bool autoplay() const;    
320     void setAutoplay(bool);
321
322     void paint(GraphicsContext*, const IntRect&);
323     void paintCurrentFrameInContext(GraphicsContext*, const IntRect&);
324
325     enum NetworkState { Empty, Idle, Loading, Loaded, FormatError, NetworkError, DecodeError };
326     NetworkState networkState();
327
328     enum ReadyState  { HaveNothing, HaveMetadata, HaveCurrentData, HaveFutureData, HaveEnoughData };
329     ReadyState readyState();
330
331     enum MovieLoadType { Unknown, Download, StoredStream, LiveStream };
332     MovieLoadType movieLoadType() const;
333
334     enum Preload { None, MetaData, Auto };
335     Preload preload() const;
336     void setPreload(Preload);
337
338     void networkStateChanged();
339     void readyStateChanged();
340     void volumeChanged(float);
341     void muteChanged(bool);
342     void timeChanged();
343     void sizeChanged();
344     void rateChanged();
345     void playbackStateChanged();
346     void durationChanged();
347     void firstVideoFrameAvailable();
348     void characteristicChanged();
349
350     void repaint();
351
352     MediaPlayerClient* mediaPlayerClient() const { return m_mediaPlayerClient; }
353
354     bool hasAvailableVideoFrame() const;
355     void prepareForRendering();
356
357     bool canLoadPoster() const;
358     void setPoster(const String&);
359
360 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
361     void deliverNotification(MediaPlayerProxyNotificationType notification);
362     void setMediaPlayerProxy(WebMediaPlayerProxy* proxy);
363     void setControls(bool);
364 #endif
365
366 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO) || USE(NATIVE_FULLSCREEN_VIDEO)
367     void enterFullscreen();
368     void exitFullscreen();
369 #endif
370
371 #if USE(NATIVE_FULLSCREEN_VIDEO)
372     bool canEnterFullscreen() const;
373 #endif
374
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();
380 #endif
381
382 #if PLATFORM(WIN) && USE(AVFOUNDATION)
383     GraphicsDeviceAdapter* graphicsDeviceAdapter() const;
384 #endif
385
386     bool hasSingleSecurityOrigin() const;
387
388     bool didPassCORSAccessCheck() const;
389
390     float mediaTimeForTimeValue(float) const;
391
392     double maximumDurationToCacheMediaTime() const;
393
394     unsigned decodedFrameCount() const;
395     unsigned droppedFrameCount() const;
396     unsigned audioDecodedByteCount() const;
397     unsigned videoDecodedByteCount() const;
398
399     void setPrivateBrowsingMode(bool);
400
401 #if ENABLE(WEB_AUDIO)
402     AudioSourceProvider* audioSourceProvider();
403 #endif
404
405 #if ENABLE(MEDIA_SOURCE)
406     void sourceOpened();
407     String sourceURL() const;
408 #endif
409
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);
415 #endif
416
417     String referrer() const;
418     String userAgent() const;
419
420     String engineDescription() const;
421
422 private:
423     MediaPlayer(MediaPlayerClient*);
424     void loadWithNextMediaEngine(MediaPlayerFactory*);
425     void reloadTimerFired(Timer<MediaPlayer>*);
426
427     static void initializeMediaEngines();
428
429     MediaPlayerClient* m_mediaPlayerClient;
430     Timer<MediaPlayer> m_reloadTimer;
431     OwnPtr<MediaPlayerPrivateInterface> m_private;
432     MediaPlayerFactory* m_currentMediaEngine;
433     KURL m_url;
434     String m_contentMIMEType;
435     String m_contentTypeCodecs;
436     String m_keySystem;
437     FrameView* m_frameView;
438     IntSize m_size;
439     Preload m_preload;
440     bool m_visible;
441     float m_rate;
442     float m_volume;
443     bool m_muted;
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
450 #endif
451 };
452
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);
457 #else
458 typedef MediaPlayer::SupportsType (*MediaEngineSupportsType)(const String& type, const String& codecs, const KURL& url);
459 #endif
460 typedef void (*MediaEngineGetSitesInMediaCache)(Vector<String>&);
461 typedef void (*MediaEngineClearMediaCache)();
462 typedef void (*MediaEngineClearMediaCacheForSite)(const String&);
463
464 typedef void (*MediaEngineRegistrar)(CreateMediaEnginePlayer, MediaEngineSupportedTypes, MediaEngineSupportsType,
465     MediaEngineGetSitesInMediaCache, MediaEngineClearMediaCache, MediaEngineClearMediaCacheForSite);
466
467 }
468
469 #endif // ENABLE(VIDEO)
470
471 #endif