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     void enterFullscreenMode();
    194 #endif
    195 
    196     void setFrameView(FrameView* frameView) { m_frameView = frameView; }
    197     FrameView* frameView() { return m_frameView; }
    198     bool inMediaDocument();
    199 
    200     IntSize size() const { return m_size; }
    201     void setSize(const IntSize& size);
    202 
    203     void load(const String& url, const ContentType&);
    204     void cancelLoad();
    205 
    206     bool visible() const;
    207     void setVisible(bool);
    208 
    209     void prepareToPlay();
    210     void play();
    211     void pause();
    212 
    213     bool paused() const;
    214     bool seeking() const;
    215 
    216     float duration() const;
    217     float currentTime() const;
    218     void seek(float time);
    219 
    220     float startTime() const;
    221 
    222     float rate() const;
    223     void setRate(float);
    224 
    225     bool preservesPitch() const;
    226     void setPreservesPitch(bool);
    227 
    228     PassRefPtr<TimeRanges> buffered();
    229     float maxTimeSeekable();
    230 
    231     unsigned bytesLoaded();
    232 
    233     float volume() const;
    234     void setVolume(float);
    235 
    236     bool muted() const;
    237     void setMuted(bool);
    238 
    239     bool hasClosedCaptions() const;
    240     void setClosedCaptionsVisible(bool closedCaptionsVisible);
    241 
    242     bool autoplay() const;
    243     void setAutoplay(bool);
    244 
    245     void paint(GraphicsContext*, const IntRect&);
    246     void paintCurrentFrameInContext(GraphicsContext*, const IntRect&);
    247 
    248     enum NetworkState { Empty, Idle, Loading, Loaded, FormatError, NetworkError, DecodeError };
    249     NetworkState networkState();
    250 
    251     enum ReadyState  { HaveNothing, HaveMetadata, HaveCurrentData, HaveFutureData, HaveEnoughData };
    252     ReadyState readyState();
    253 
    254     enum MovieLoadType { Unknown, Download, StoredStream, LiveStream };
    255     MovieLoadType movieLoadType() const;
    256 
    257     enum Preload { None, MetaData, Auto };
    258     Preload preload() const;
    259     void setPreload(Preload);
    260 
    261     void networkStateChanged();
    262     void readyStateChanged();
    263     void volumeChanged(float);
    264     void muteChanged(bool);
    265     void timeChanged();
    266     void sizeChanged();
    267     void rateChanged();
    268     void playbackStateChanged();
    269     void durationChanged();
    270     void firstVideoFrameAvailable();
    271 
    272     void repaint();
    273 
    274     MediaPlayerClient* mediaPlayerClient() const { return m_mediaPlayerClient; }
    275 
    276     bool hasAvailableVideoFrame() const;
    277     void prepareForRendering();
    278 
    279     bool canLoadPoster() const;
    280     void setPoster(const String&);
    281 
    282 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
    283     void deliverNotification(MediaPlayerProxyNotificationType notification);
    284     void setMediaPlayerProxy(WebMediaPlayerProxy* proxy);
    285     void setControls(bool);
    286     void enterFullscreen();
    287     void exitFullscreen();
    288 #endif
    289 
    290 #if USE(ACCELERATED_COMPOSITING)
    291     // whether accelerated rendering is supported by the media engine for the current media.
    292     bool supportsAcceleratedRendering() const;
    293     // called when the rendering system flips the into or out of accelerated rendering mode.
    294     void acceleratedRenderingStateChanged();
    295 #endif
    296 
    297     bool hasSingleSecurityOrigin() const;
    298 
    299     float mediaTimeForTimeValue(float) const;
    300 
    301     double maximumDurationToCacheMediaTime() const;
    302 
    303     unsigned decodedFrameCount() const;
    304     unsigned droppedFrameCount() const;
    305     unsigned audioDecodedByteCount() const;
    306     unsigned videoDecodedByteCount() const;
    307 
    308     void setPrivateBrowsingMode(bool);
    309 
    310 private:
    311     MediaPlayer(MediaPlayerClient*);
    312     void loadWithNextMediaEngine(MediaPlayerFactory*);
    313     void reloadTimerFired(Timer<MediaPlayer>*);
    314 
    315     static void initializeMediaEngines();
    316 
    317     MediaPlayerClient* m_mediaPlayerClient;
    318     Timer<MediaPlayer> m_reloadTimer;
    319     OwnPtr<MediaPlayerPrivateInterface*> m_private;
    320     MediaPlayerFactory* m_currentMediaEngine;
    321     String m_url;
    322     String m_contentMIMEType;
    323     String m_contentTypeCodecs;
    324     FrameView* m_frameView;
    325     IntSize m_size;
    326     Preload m_preload;
    327     bool m_visible;
    328     float m_rate;
    329     float m_volume;
    330     bool m_muted;
    331     bool m_preservesPitch;
    332     bool m_privateBrowsing;
    333     bool m_shouldPrepareToRender;
    334 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
    335     WebMediaPlayerProxy* m_playerProxy;    // not owned or used, passed to m_private
    336 #endif
    337 #if PLATFORM(ANDROID)
    338     MediaElementType m_mediaElementType;
    339 #endif
    340 };
    341 
    342 typedef MediaPlayerPrivateInterface* (*CreateMediaEnginePlayer)(MediaPlayer*);
    343 typedef void (*MediaEngineSupportedTypes)(HashSet<String>& types);
    344 typedef MediaPlayer::SupportsType (*MediaEngineSupportsType)(const String& type, const String& codecs);
    345 typedef void (*MediaEngineGetSitesInMediaCache)(Vector<String>&);
    346 typedef void (*MediaEngineClearMediaCache)();
    347 typedef void (*MediaEngineClearMediaCacheForSite)(const String&);
    348 
    349 typedef void (*MediaEngineRegistrar)(CreateMediaEnginePlayer, MediaEngineSupportedTypes, MediaEngineSupportsType,
    350     MediaEngineGetSitesInMediaCache, MediaEngineClearMediaCache, MediaEngineClearMediaCacheForSite);
    351 
    352 
    353 }
    354 
    355 #endif // ENABLE(VIDEO)
    356 
    357 #endif
    358