Home | History | Annotate | Download | only in html
      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 MediaController_h
     27 #define MediaController_h
     28 
     29 #include "bindings/v8/ScriptWrappable.h"
     30 #include "core/dom/Event.h"
     31 #include "core/dom/EventTarget.h"
     32 #include "core/html/MediaControllerInterface.h"
     33 #include "core/platform/Timer.h"
     34 #include "wtf/PassRefPtr.h"
     35 #include "wtf/RefCounted.h"
     36 #include "wtf/Vector.h"
     37 
     38 namespace WebCore {
     39 
     40 class Clock;
     41 class Event;
     42 class ExceptionState;
     43 class HTMLMediaElement;
     44 class ScriptExecutionContext;
     45 
     46 class MediaController : public RefCounted<MediaController>, public ScriptWrappable, public MediaControllerInterface, public EventTarget {
     47 public:
     48     static PassRefPtr<MediaController> create(ScriptExecutionContext*);
     49     virtual ~MediaController();
     50 
     51     void addMediaElement(HTMLMediaElement*);
     52     void removeMediaElement(HTMLMediaElement*);
     53     bool containsMediaElement(HTMLMediaElement*) const;
     54 
     55     const String& mediaGroup() const { return m_mediaGroup; }
     56 
     57     virtual PassRefPtr<TimeRanges> buffered() const;
     58     virtual PassRefPtr<TimeRanges> seekable() const;
     59     virtual PassRefPtr<TimeRanges> played();
     60 
     61     virtual double duration() const;
     62     virtual double currentTime() const;
     63     virtual void setCurrentTime(double, ExceptionState&);
     64 
     65     virtual bool paused() const { return m_paused; }
     66     virtual void play();
     67     virtual void pause();
     68     void unpause();
     69 
     70     virtual double defaultPlaybackRate() const { return m_defaultPlaybackRate; }
     71     virtual void setDefaultPlaybackRate(double);
     72 
     73     virtual double playbackRate() const;
     74     virtual void setPlaybackRate(double);
     75 
     76     virtual double volume() const { return m_volume; }
     77     virtual void setVolume(double, ExceptionState&);
     78 
     79     virtual bool muted() const { return m_muted; }
     80     virtual void setMuted(bool);
     81 
     82     virtual ReadyState readyState() const { return m_readyState; }
     83 
     84     enum PlaybackState { WAITING, PLAYING, ENDED };
     85     const AtomicString& playbackState() const;
     86 
     87     virtual bool supportsFullscreen() const { return false; }
     88     virtual bool isFullscreen() const { return false; }
     89     virtual void enterFullscreen() { }
     90 
     91     virtual bool hasAudio() const;
     92     virtual bool hasVideo() const;
     93     virtual bool hasClosedCaptions() const;
     94     virtual void setClosedCaptionsVisible(bool);
     95     virtual bool closedCaptionsVisible() const { return m_closedCaptionsVisible; }
     96 
     97     virtual void beginScrubbing();
     98     virtual void endScrubbing();
     99 
    100     virtual bool canPlay() const;
    101 
    102     virtual bool hasCurrentSrc() const;
    103 
    104     bool isBlocked() const;
    105 
    106     // EventTarget
    107     using RefCounted<MediaController>::ref;
    108     using RefCounted<MediaController>::deref;
    109 
    110 private:
    111     MediaController(ScriptExecutionContext*);
    112     void reportControllerState();
    113     void updateReadyState();
    114     void updatePlaybackState();
    115     void updateMediaElements();
    116     void bringElementUpToSpeed(HTMLMediaElement*);
    117     void scheduleEvent(const AtomicString& eventName);
    118     void asyncEventTimerFired(Timer<MediaController>*);
    119     void clearPositionTimerFired(Timer<MediaController>*);
    120     bool hasEnded() const;
    121     void scheduleTimeupdateEvent();
    122     void timeupdateTimerFired(Timer<MediaController>*);
    123     void startTimeupdateTimer();
    124 
    125     // EventTarget
    126     virtual void refEventTarget() { ref(); }
    127     virtual void derefEventTarget() { deref(); }
    128     virtual const AtomicString& interfaceName() const;
    129     virtual ScriptExecutionContext* scriptExecutionContext() const { return m_scriptExecutionContext; };
    130     virtual EventTargetData* eventTargetData() { return &m_eventTargetData; }
    131     virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; }
    132     EventTargetData m_eventTargetData;
    133 
    134     friend class HTMLMediaElement;
    135     friend class MediaControllerEventListener;
    136     Vector<HTMLMediaElement*> m_mediaElements;
    137     bool m_paused;
    138     double m_defaultPlaybackRate;
    139     double m_volume;
    140     mutable double m_position;
    141     bool m_muted;
    142     ReadyState m_readyState;
    143     PlaybackState m_playbackState;
    144     Vector<RefPtr<Event> > m_pendingEvents;
    145     Timer<MediaController> m_asyncEventTimer;
    146     mutable Timer<MediaController> m_clearPositionTimer;
    147     String m_mediaGroup;
    148     bool m_closedCaptionsVisible;
    149     PassRefPtr<Clock> m_clock;
    150     ScriptExecutionContext* m_scriptExecutionContext;
    151     Timer<MediaController> m_timeupdateTimer;
    152     double m_previousTimeupdateTime;
    153 };
    154 
    155 } // namespace WebCore
    156 
    157 #endif
    158