Home | History | Annotate | Download | only in avfoundation
      1 /*
      2  * Copyright (C) 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 MediaPlayerPrivateAVFoundation_h
     27 #define MediaPlayerPrivateAVFoundation_h
     28 
     29 #if ENABLE(VIDEO) && USE(AVFOUNDATION)
     30 
     31 #include "FloatSize.h"
     32 #include "MediaPlayerPrivate.h"
     33 #include "Timer.h"
     34 #include <wtf/RetainPtr.h>
     35 
     36 namespace WebCore {
     37 
     38 class ApplicationCacheResource;
     39 
     40 class MediaPlayerPrivateAVFoundation : public MediaPlayerPrivateInterface {
     41 public:
     42 
     43     virtual void repaint();
     44     virtual void metadataLoaded();
     45     virtual void loadStateChanged();
     46     virtual void playabilityKnown();
     47     virtual void rateChanged();
     48     virtual void loadedTimeRangesChanged();
     49     virtual void seekableTimeRangesChanged();
     50     virtual void timeChanged(double);
     51     virtual void seekCompleted(bool);
     52     virtual void didEnd();
     53 
     54     class Notification {
     55     public:
     56         enum Type {
     57             None,
     58             ItemDidPlayToEndTime,
     59             ItemTracksChanged,
     60             ItemStatusChanged,
     61             ItemSeekableTimeRangesChanged,
     62             ItemLoadedTimeRangesChanged,
     63             ItemPresentationSizeChanged,
     64             ItemIsPlaybackLikelyToKeepUpChanged,
     65             ItemIsPlaybackBufferEmptyChanged,
     66             ItemIsPlaybackBufferFullChanged,
     67             AssetMetadataLoaded,
     68             AssetPlayabilityKnown,
     69             PlayerRateChanged,
     70             PlayerTimeChanged,
     71             SeekCompleted,
     72         };
     73 
     74         Notification()
     75             : m_type(None)
     76             , m_time(0)
     77             , m_finished(false)
     78         {
     79         }
     80 
     81         Notification(Type type, double time)
     82             : m_type(type)
     83             , m_time(time)
     84             , m_finished(false)
     85         {
     86         }
     87 
     88         Notification(Type type, bool finished)
     89         : m_type(type)
     90         , m_time(0)
     91         , m_finished(finished)
     92         {
     93         }
     94 
     95         Type type() { return m_type; }
     96         bool isValid() { return m_type != None; }
     97         double time() { return m_time; }
     98         bool finished() { return m_finished; }
     99 
    100     private:
    101         Type m_type;
    102         double m_time;
    103         bool m_finished;
    104     };
    105 
    106     void scheduleMainThreadNotification(Notification);
    107     void scheduleMainThreadNotification(Notification::Type, double time = 0);
    108     void scheduleMainThreadNotification(Notification::Type, bool completed);
    109     void dispatchNotification();
    110     void clearMainThreadPendingFlag();
    111 
    112 protected:
    113     MediaPlayerPrivateAVFoundation(MediaPlayer*);
    114     virtual ~MediaPlayerPrivateAVFoundation();
    115 
    116     // MediaPlayerPrivatePrivateInterface overrides.
    117     virtual void load(const String& url);
    118     virtual void cancelLoad() = 0;
    119 
    120     virtual void prepareToPlay();
    121     virtual PlatformMedia platformMedia() const = 0;
    122 
    123     virtual void play();
    124     virtual void pause();
    125 
    126     virtual IntSize naturalSize() const;
    127     virtual bool hasVideo() const { return m_cachedHasVideo; }
    128     virtual bool hasAudio() const { return m_cachedHasAudio; }
    129     virtual void setVisible(bool);
    130     virtual float duration() const;
    131     virtual float currentTime() const = 0;
    132     virtual void seek(float);
    133     virtual bool seeking() const;
    134     virtual void setRate(float);
    135     virtual bool paused() const;
    136     virtual void setVolume(float) = 0;
    137     virtual bool hasClosedCaptions() const { return m_cachedHasCaptions; }
    138     virtual void setClosedCaptionsVisible(bool) = 0;
    139     virtual MediaPlayer::NetworkState networkState() const { return m_networkState; }
    140     virtual MediaPlayer::ReadyState readyState() const { return m_readyState; }
    141     virtual float maxTimeSeekable() const;
    142     virtual PassRefPtr<TimeRanges> buffered() const;
    143     virtual unsigned bytesLoaded() const;
    144     virtual void setSize(const IntSize&);
    145     virtual void paint(GraphicsContext*, const IntRect&);
    146     virtual void paintCurrentFrameInContext(GraphicsContext*, const IntRect&) = 0;
    147     virtual void setPreload(MediaPlayer::Preload);
    148     virtual bool hasAvailableVideoFrame() const;
    149 #if USE(ACCELERATED_COMPOSITING)
    150     virtual PlatformLayer* platformLayer() const { return 0; }
    151     virtual bool supportsAcceleratedRendering() const = 0;
    152     virtual void acceleratedRenderingStateChanged();
    153 #endif
    154     virtual bool hasSingleSecurityOrigin() const { return true; }
    155     virtual MediaPlayer::MovieLoadType movieLoadType() const;
    156     virtual void prepareForRendering();
    157     virtual float mediaTimeForTimeValue(float) const = 0;
    158 
    159     virtual bool supportsFullscreen() const;
    160 
    161     // Required interfaces for concrete derived classes.
    162     virtual void createAVPlayerForURL(const String& url) = 0;
    163 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
    164     virtual void createAVPlayerForCacheResource(ApplicationCacheResource*) = 0;
    165 #endif
    166 
    167     enum ItemStatus {
    168         MediaPlayerAVPlayerItemStatusUnknown,
    169         MediaPlayerAVPlayerItemStatusFailed,
    170         MediaPlayerAVPlayerItemStatusReadyToPlay,
    171         MediaPlayerAVPlayerItemStatusPlaybackBufferEmpty,
    172         MediaPlayerAVPlayerItemStatusPlaybackBufferFull,
    173         MediaPlayerAVPlayerItemStatusPlaybackLikelyToKeepUp,
    174     };
    175     virtual ItemStatus playerItemStatus() const = 0;
    176 
    177     enum AVAssetStatus {
    178         MediaPlayerAVAssetStatusUnknown,
    179         MediaPlayerAVAssetStatusLoading,
    180         MediaPlayerAVAssetStatusFailed,
    181         MediaPlayerAVAssetStatusCancelled,
    182         MediaPlayerAVAssetStatusLoaded,
    183         MediaPlayerAVAssetStatusPlayable,
    184     };
    185     virtual AVAssetStatus assetStatus() const = 0;
    186 
    187     virtual void platformPlay() = 0;
    188     virtual void platformPause() = 0;
    189     virtual void checkPlayability() = 0;
    190     virtual void updateRate() = 0;
    191     virtual float rate() const = 0;
    192     virtual void seekToTime(float time) = 0;
    193     virtual unsigned totalBytes() const = 0;
    194     virtual PassRefPtr<TimeRanges> platformBufferedTimeRanges() const = 0;
    195     virtual float platformMaxTimeSeekable() const = 0;
    196     virtual float platformMaxTimeLoaded() const = 0;
    197     virtual float platformDuration() const = 0;
    198 
    199     virtual void beginLoadingMetadata() = 0;
    200     virtual void tracksChanged() = 0;
    201     virtual void sizeChanged() = 0;
    202 
    203     virtual void createContextVideoRenderer() = 0;
    204     virtual void destroyContextVideoRenderer() = 0;
    205 
    206     virtual void createVideoLayer() = 0;
    207     virtual void destroyVideoLayer() = 0;
    208     virtual bool videoLayerIsReadyToDisplay() const = 0;
    209 
    210     virtual bool hasContextRenderer() const = 0;
    211     virtual bool hasLayerRenderer() const = 0;
    212 
    213 protected:
    214     void resumeLoad();
    215     void updateStates();
    216 
    217     void setHasVideo(bool b) { m_cachedHasVideo = b; };
    218     void setHasAudio(bool b) { m_cachedHasAudio = b; }
    219     void setHasClosedCaptions(bool b) { m_cachedHasCaptions = b; }
    220     void setDelayCallbacks(bool);
    221     void setIgnoreLoadStateChanges(bool delay) { m_ignoreLoadStateChanges = delay; }
    222     void setNaturalSize(IntSize);
    223 
    224     enum MediaRenderingMode { MediaRenderingNone, MediaRenderingToContext, MediaRenderingToLayer };
    225     MediaRenderingMode currentRenderingMode() const;
    226     MediaRenderingMode preferredRenderingMode() const;
    227 
    228     bool metaDataAvailable() const { return m_readyState >= MediaPlayer::HaveMetadata; }
    229     float requestedRate() const { return m_requestedRate; }
    230     float maxTimeLoaded() const;
    231     bool isReadyForVideoSetup() const;
    232     virtual void setUpVideoRendering();
    233     virtual void tearDownVideoRendering();
    234     bool hasSetUpVideoRendering() const;
    235 
    236     static void mainThreadCallback(void*);
    237 
    238 private:
    239 
    240     MediaPlayer* m_player;
    241 
    242     Vector<Notification> m_queuedNotifications;
    243     Mutex m_queueMutex;
    244     bool m_mainThreadCallPending;
    245 
    246     mutable RefPtr<TimeRanges> m_cachedLoadedTimeRanges;
    247 
    248     MediaPlayer::NetworkState m_networkState;
    249     MediaPlayer::ReadyState m_readyState;
    250 
    251     String m_assetURL;
    252     MediaPlayer::Preload m_preload;
    253     FloatSize m_scaleFactor;
    254 
    255     IntSize m_cachedNaturalSize;
    256     mutable float m_cachedMaxTimeLoaded;
    257     mutable float m_cachedMaxTimeSeekable;
    258     mutable float m_cachedDuration;
    259     float m_reportedDuration;
    260 
    261     float m_seekTo;
    262     float m_requestedRate;
    263     int m_delayCallbacks;
    264     bool m_havePreparedToPlay;
    265     bool m_assetIsPlayable;
    266     bool m_visible;
    267     bool m_videoFrameHasDrawn;
    268     bool m_loadingMetadata;
    269     bool m_delayingLoad;
    270     bool m_isAllowedToRender;
    271     bool m_cachedHasAudio;
    272     bool m_cachedHasVideo;
    273     bool m_cachedHasCaptions;
    274     bool m_ignoreLoadStateChanges;
    275     bool m_haveReportedFirstVideoFrame;
    276     bool m_playWhenFramesAvailable;
    277 };
    278 
    279 }
    280 
    281 #endif
    282 #endif
    283