Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2007, 2008, 2009, 2010, 2011 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 <wtf/Forward.h>
     38 #include <wtf/HashSet.h>
     39 #include <wtf/OwnPtr.h>
     40 #include <wtf/Noncopyable.h>
     41 #include <wtf/PassOwnPtr.h>
     42 #include <wtf/text/StringHash.h>
     43 
     44 #if USE(ACCELERATED_COMPOSITING)
     45 #include "GraphicsLayer.h"
     46 #endif
     47 
     48 #ifdef __OBJC__
     49 @class AVPlayer;
     50 @class QTMovie;
     51 #else
     52 class AVPlayer;
     53 class QTMovie;
     54 #endif
     55 class QTMovieGWorld;
     56 class QTMovieVisualContext;
     57 
     58 namespace WebCore {
     59 
     60 class GStreamerGWorld;
     61 class MediaPlayerPrivateInterface;
     62 
     63 // Structure that will hold every native
     64 // types supported by the current media player.
     65 // We have to do that has multiple media players
     66 // backend can live at runtime.
     67 struct PlatformMedia {
     68     enum {
     69         None,
     70         QTMovieType,
     71         QTMovieGWorldType,
     72         QTMovieVisualContextType,
     73         GStreamerGWorldType,
     74         ChromiumMediaPlayerType,
     75         QtMediaPlayerType,
     76         AVFoundationMediaPlayerType,
     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     } media;
     88 };
     89 
     90 extern const PlatformMedia NoPlatformMedia;
     91 
     92 class ContentType;
     93 class FrameView;
     94 class GraphicsContext;
     95 class IntRect;
     96 class IntSize;
     97 class MediaPlayer;
     98 struct MediaPlayerFactory;
     99 class TimeRanges;
    100 
    101 class MediaPlayerClient {
    102 public:
    103     virtual ~MediaPlayerClient() { }
    104 
    105     // Get the document which the media player is owned by
    106     virtual Document* mediaPlayerOwningDocument() { return 0; }
    107 
    108     // the network state has changed
    109     virtual void mediaPlayerNetworkStateChanged(MediaPlayer*) { }
    110 
    111     // the ready state has changed
    112     virtual void mediaPlayerReadyStateChanged(MediaPlayer*) { }
    113 
    114     // the volume state has changed
    115     virtual void mediaPlayerVolumeChanged(MediaPlayer*) { }
    116 
    117     // the mute state has changed
    118     virtual void mediaPlayerMuteChanged(MediaPlayer*) { }
    119 
    120     // time has jumped, eg. not as a result of normal playback
    121     virtual void mediaPlayerTimeChanged(MediaPlayer*) { }
    122 
    123     // the media file duration has changed, or is now known
    124     virtual void mediaPlayerDurationChanged(MediaPlayer*) { }
    125 
    126     // the playback rate has changed
    127     virtual void mediaPlayerRateChanged(MediaPlayer*) { }
    128 
    129     // the play/pause status changed
    130     virtual void mediaPlayerPlaybackStateChanged(MediaPlayer*) { }
    131 
    132     // The MediaPlayer has found potentially problematic media content.
    133     // This is used internally to trigger swapping from a <video>
    134     // element to an <embed> in standalone documents
    135     virtual void mediaPlayerSawUnsupportedTracks(MediaPlayer*) { }
    136 
    137 // Presentation-related methods
    138     // a new frame of video is available
    139     virtual void mediaPlayerRepaint(MediaPlayer*) { }
    140 
    141     // the movie size has changed
    142     virtual void mediaPlayerSizeChanged(MediaPlayer*) { }
    143 
    144     virtual void mediaPlayerEngineUpdated(MediaPlayer*) { }
    145 
    146     // The first frame of video is available to render. A media engine need only make this callback if the
    147     // first frame is not available immediately when prepareForRendering is called.
    148     virtual void mediaPlayerFirstVideoFrameAvailable(MediaPlayer*) { }
    149 
    150 #if USE(ACCELERATED_COMPOSITING)
    151     // whether the rendering system can accelerate the display of this MediaPlayer.
    152     virtual bool mediaPlayerRenderingCanBeAccelerated(MediaPlayer*) { return false; }
    153 
    154     // called when the media player's rendering mode changed, which indicates a change in the
    155     // availability of the platformLayer().
    156     virtual void mediaPlayerRenderingModeChanged(MediaPlayer*) { }
    157 #endif
    158 };
    159 
    160 class MediaPlayer {
    161     WTF_MAKE_NONCOPYABLE(MediaPlayer); WTF_MAKE_FAST_ALLOCATED;
    162 public:
    163 
    164     static PassOwnPtr<MediaPlayer> create(MediaPlayerClient* client)
    165     {
    166         return adoptPtr(new MediaPlayer(client));
    167     }
    168     virtual ~MediaPlayer();
    169 
    170     // Media engine support.
    171     enum SupportsType { IsNotSupported, IsSupported, MayBeSupported };
    172     static MediaPlayer::SupportsType supportsType(const ContentType&);
    173     static void getSupportedTypes(HashSet<String>&);
    174     static bool isAvailable();
    175     static void getSitesInMediaCache(Vector<String>&);
    176     static void clearMediaCache();
    177     static void clearMediaCacheForSite(const String&);
    178 
    179     bool supportsFullscreen() const;
    180     bool supportsSave() const;
    181     PlatformMedia platformMedia() const;
    182 #if USE(ACCELERATED_COMPOSITING)
    183     PlatformLayer* platformLayer() const;
    184 #endif
    185 
    186     IntSize naturalSize();
    187     bool hasVideo() const;
    188     bool hasAudio() const;
    189 #if PLATFORM(ANDROID)
    190     enum MediaElementType { Video, Audio };
    191     void setMediaElementType(MediaElementType type) { m_mediaElementType = type; }
    192     MediaElementType mediaElementType() { return m_mediaElementType; }
    193 #endif
    194 
    195     void setFrameView(FrameView* frameView) { m_frameView = frameView; }
    196     FrameView* frameView() { return m_frameView; }
    197     bool inMediaDocument();
    198 
    199     IntSize size() const { return m_size; }
    200     void setSize(const IntSize& size);
    201 
    202     void load(const String& url, const ContentType&);
    203     void cancelLoad();
    204 
    205     bool visible() const;
    206     void setVisible(bool);
    207 
    208     void prepareToPlay();
    209     void play();
    210     void pause();
    211 
    212     bool paused() const;
    213     bool seeking() const;
    214 
    215     float duration() const;
    216     float currentTime() const;
    217     void seek(float time);
    218 
    219     float startTime() const;
    220 
    221     float rate() const;
    222     void setRate(float);
    223 
    224     bool preservesPitch() const;
    225     void setPreservesPitch(bool);
    226 
    227     PassRefPtr<TimeRanges> buffered();
    228     float maxTimeSeekable();
    229 
    230     unsigned bytesLoaded();
    231 
    232     float volume() const;
    233     void setVolume(float);
    234 
    235     bool muted() const;
    236     void setMuted(bool);
    237 
    238     bool hasClosedCaptions() const;
    239     void setClosedCaptionsVisible(bool closedCaptionsVisible);
    240 
    241     bool autoplay() const;
    242     void setAutoplay(bool);
    243 
    244     void paint(GraphicsContext*, const IntRect&);
    245     void paintCurrentFrameInContext(GraphicsContext*, const IntRect&);
    246 
    247     enum NetworkState { Empty, Idle, Loading, Loaded, FormatError, NetworkError, DecodeError };
    248     NetworkState networkState();
    249 
    250     enum ReadyState  { HaveNothing, HaveMetadata, HaveCurrentData, HaveFutureData, HaveEnoughData };
    251     ReadyState readyState();
    252 
    253     enum MovieLoadType { Unknown, Download, StoredStream, LiveStream };
    254     MovieLoadType movieLoadType() const;
    255 
    256     enum Preload { None, MetaData, Auto };
    257     Preload preload() const;
    258     void setPreload(Preload);
    259 
    260     void networkStateChanged();
    261     void readyStateChanged();
    262     void volumeChanged(float);
    263     void muteChanged(bool);
    264     void timeChanged();
    265     void sizeChanged();
    266     void rateChanged();
    267     void playbackStateChanged();
    268     void durationChanged();
    269     void firstVideoFrameAvailable();
    270 
    271     void repaint();
    272 
    273     MediaPlayerClient* mediaPlayerClient() const { return m_mediaPlayerClient; }
    274 
    275     bool hasAvailableVideoFrame() const;
    276     void prepareForRendering();
    277 
    278     bool canLoadPoster() const;
    279     void setPoster(const String&);
    280 
    281 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
    282     void deliverNotification(MediaPlayerProxyNotificationType notification);
    283     void setMediaPlayerProxy(WebMediaPlayerProxy* proxy);
    284     void setControls(bool);
    285     void enterFullscreen();
    286     void exitFullscreen();
    287 #endif
    288 
    289 #if USE(ACCELERATED_COMPOSITING)
    290     // whether accelerated rendering is supported by the media engine for the current media.
    291     bool supportsAcceleratedRendering() const;
    292     // called when the rendering system flips the into or out of accelerated rendering mode.
    293     void acceleratedRenderingStateChanged();
    294 #endif
    295 
    296     bool hasSingleSecurityOrigin() const;
    297 
    298     float mediaTimeForTimeValue(float) const;
    299 
    300     double maximumDurationToCacheMediaTime() const;
    301 
    302     unsigned decodedFrameCount() const;
    303     unsigned droppedFrameCount() const;
    304     unsigned audioDecodedByteCount() const;
    305     unsigned videoDecodedByteCount() const;
    306 
    307     void setPrivateBrowsingMode(bool);
    308 
    309 private:
    310     MediaPlayer(MediaPlayerClient*);
    311     void loadWithNextMediaEngine(MediaPlayerFactory*);
    312     void reloadTimerFired(Timer<MediaPlayer>*);
    313 
    314     static void initializeMediaEngines();
    315 
    316     MediaPlayerClient* m_mediaPlayerClient;
    317     Timer<MediaPlayer> m_reloadTimer;
    318     OwnPtr<MediaPlayerPrivateInterface*> m_private;
    319     MediaPlayerFactory* m_currentMediaEngine;
    320     String m_url;
    321     String m_contentMIMEType;
    322     String m_contentTypeCodecs;
    323     FrameView* m_frameView;
    324     IntSize m_size;
    325     Preload m_preload;
    326     bool m_visible;
    327     float m_rate;
    328     float m_volume;
    329     bool m_muted;
    330     bool m_preservesPitch;
    331     bool m_privateBrowsing;
    332     bool m_shouldPrepareToRender;
    333 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
    334     WebMediaPlayerProxy* m_playerProxy;    // not owned or used, passed to m_private
    335 #endif
    336 #if PLATFORM(ANDROID)
    337     MediaElementType m_mediaElementType;
    338 #endif
    339 };
    340 
    341 typedef MediaPlayerPrivateInterface* (*CreateMediaEnginePlayer)(MediaPlayer*);
    342 typedef void (*MediaEngineSupportedTypes)(HashSet<String>& types);
    343 typedef MediaPlayer::SupportsType (*MediaEngineSupportsType)(const String& type, const String& codecs);
    344 typedef void (*MediaEngineGetSitesInMediaCache)(Vector<String>&);
    345 typedef void (*MediaEngineClearMediaCache)();
    346 typedef void (*MediaEngineClearMediaCacheForSite)(const String&);
    347 
    348 typedef void (*MediaEngineRegistrar)(CreateMediaEnginePlayer, MediaEngineSupportedTypes, MediaEngineSupportsType,
    349     MediaEngineGetSitesInMediaCache, MediaEngineClearMediaCache, MediaEngineClearMediaCacheForSite);
    350 
    351 
    352 }
    353 
    354 #endif // ENABLE(VIDEO)
    355 
    356 #endif
    357