HTMLMediaElement.h 26.3 KB
Newer Older
antti's avatar
antti committed
1
/*
2
 * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
antti's avatar
antti committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#ifndef HTMLMediaElement_h
#define HTMLMediaElement_h

#if ENABLE(VIDEO)
#include "HTMLElement.h"
31
#include "ActiveDOMObject.h"
32
#include "GenericEventQueue.h"
33
#include "MediaCanStartListener.h"
34
#include "MediaControllerInterface.h"
antti@apple.com's avatar
antti@apple.com committed
35
#include "MediaPlayer.h"
antti's avatar
antti committed
36

37 38 39 40
#if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
#include "MediaPlayerProxy.h"
#endif

41
#if ENABLE(VIDEO_TRACK)
42
#include "PODIntervalTree.h"
43
#include "TextTrack.h"
44
#include "TextTrackCue.h"
45 46
#endif

antti's avatar
antti committed
47 48
namespace WebCore {

49 50 51 52
#if ENABLE(WEB_AUDIO)
class AudioSourceProvider;
class MediaElementAudioSourceNode;
#endif
53
class Event;
54
class HTMLSourceElement;
55
class HTMLTrackElement;
56
class MediaController;
57
class MediaControls;
antti's avatar
antti committed
58
class MediaError;
darin@apple.com's avatar
darin@apple.com committed
59
class KURL;
60
class TextTrackList;
61
class TimeRanges;
62 63 64
#if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
class Widget;
#endif
65 66 67
#if PLATFORM(MAC)
class DisplaySleepDisabler;
#endif
68 69 70
#if ENABLE(ENCRYPTED_MEDIA_V2)
class MediaKeys;
#endif
71

eric.carlson@apple.com's avatar
eric.carlson@apple.com committed
72
#if ENABLE(VIDEO_TRACK)
73 74
class InbandTextTrackPrivate;

eric.carlson@apple.com's avatar
eric.carlson@apple.com committed
75
typedef PODIntervalTree<double, TextTrackCue*> CueIntervalTree;
76 77
typedef CueIntervalTree::IntervalType CueInterval;
typedef Vector<CueInterval> CueList;
eric.carlson@apple.com's avatar
eric.carlson@apple.com committed
78 79
#endif

80 81 82 83
// FIXME: The inheritance from MediaPlayerClient here should be private inheritance.
// But it can't be until the Chromium WebMediaPlayerClientImpl class is fixed so it
// no longer depends on typecasting a MediaPlayerClient to an HTMLMediaElement.

84
class HTMLMediaElement : public HTMLElement, public MediaPlayerClient, public MediaPlayerSupportsTypeClient, private MediaCanStartListener, public ActiveDOMObject, public MediaControllerInterface
85
#if ENABLE(VIDEO_TRACK)
86
    , private TextTrackClient
87
#endif
88 89 90
#if USE(PLATFORM_TEXT_TRACK_MENU)
    , public PlatformTextTrackMenuClient
#endif
91
{
antti's avatar
antti committed
92
public:
antti@apple.com's avatar
antti@apple.com committed
93
    MediaPlayer* player() const { return m_player.get(); }
94

95
    virtual bool isVideo() const = 0;
96
    virtual bool hasVideo() const { return false; }
97
    virtual bool hasAudio() const;
98

99 100
    void rewind(float timeDelta);
    void returnToRealtime();
101 102 103

    // Eventually overloaded in HTMLVideoElement
    virtual bool supportsFullscreen() const { return false; };
104

105
    virtual bool supportsSave() const;
106
    virtual bool supportsScanning() const;
107 108
    
    PlatformMedia platformMedia() const;
109 110 111
#if USE(ACCELERATED_COMPOSITING)
    PlatformLayer* platformLayer() const;
#endif
112

113 114 115 116
    enum DelayedActionType {
        LoadMediaResource = 1 << 0,
        LoadTextTrackResource = 1 << 1,
        TextTrackChangesNotification = 1 << 2
117
    };
118
    void scheduleDelayedAction(DelayedActionType);
antti's avatar
antti committed
119
    
120 121
    MediaPlayer::MovieLoadType movieLoadType() const;
    
122
    bool inActiveDocument() const { return m_inActiveDocument; }
antti@apple.com's avatar
antti@apple.com committed
123
    
antti's avatar
antti committed
124 125 126 127 128 129
// DOM API
// error state
    PassRefPtr<MediaError> error() const;

// network state
    void setSrc(const String&);
130
    const KURL& currentSrc() const { return m_currentSrc; }
131

132
    enum NetworkState { NETWORK_EMPTY, NETWORK_IDLE, NETWORK_LOADING, NETWORK_NO_SOURCE };
antti's avatar
antti committed
133
    NetworkState networkState() const;
134

135 136
    String preload() const;    
    void setPreload(const String&);
137

antti's avatar
antti committed
138
    PassRefPtr<TimeRanges> buffered() const;
139
    void load();
140
    String canPlayType(const String& mimeType, const String& keySystem = String(), const KURL& = KURL()) const;
antti's avatar
antti committed
141 142 143 144 145 146 147 148

// ready state
    ReadyState readyState() const;
    bool seeking() const;

// playback state
    float currentTime() const;
    void setCurrentTime(float, ExceptionCode&);
149
    double initialTime() const;
150
    float startTime() const;
antti's avatar
antti committed
151 152 153
    float duration() const;
    bool paused() const;
    float defaultPlaybackRate() const;
154
    void setDefaultPlaybackRate(float);
antti's avatar
antti committed
155
    float playbackRate() const;
156
    void setPlaybackRate(float);
157
    void updatePlaybackRate();
158 159
    bool webkitPreservesPitch() const;
    void setWebkitPreservesPitch(bool);
160
    PassRefPtr<TimeRanges> played();
antti's avatar
antti committed
161
    PassRefPtr<TimeRanges> seekable() const;
adele@apple.com's avatar
adele@apple.com committed
162
    bool ended() const;
antti's avatar
antti committed
163 164
    bool autoplay() const;    
    void setAutoplay(bool b);
165 166
    bool loop() const;    
    void setLoop(bool b);
167 168
    void play();
    void pause();
169

170 171 172 173 174
// captions
    bool webkitHasClosedCaptions() const;
    bool webkitClosedCaptionsVisible() const;
    void setWebkitClosedCaptionsVisible(bool);

175 176
#if ENABLE(MEDIA_STATISTICS)
// Statistics
177 178
    unsigned webkitAudioDecodedByteCount() const;
    unsigned webkitVideoDecodedByteCount() const;
179 180
#endif

181 182
#if ENABLE(MEDIA_SOURCE)
//  Media Source.
183
    void setSourceState(const String&);
184 185
#endif 

186 187 188 189 190 191
#if ENABLE(ENCRYPTED_MEDIA)
    void webkitGenerateKeyRequest(const String& keySystem, PassRefPtr<Uint8Array> initData, ExceptionCode&);
    void webkitGenerateKeyRequest(const String& keySystem, ExceptionCode&);
    void webkitAddKey(const String& keySystem, PassRefPtr<Uint8Array> key, PassRefPtr<Uint8Array> initData, const String& sessionId, ExceptionCode&);
    void webkitAddKey(const String& keySystem, PassRefPtr<Uint8Array> key, ExceptionCode&);
    void webkitCancelKeyRequest(const String& keySystem, const String& sessionId, ExceptionCode&);
192 193 194 195

    DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitkeyadded);
    DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitkeyerror);
    DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitkeymessage);
196 197
#endif
#if ENABLE(ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA_V2)
198
    DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitneedkey);
199 200
#endif

201 202 203 204 205
#if ENABLE(ENCRYPTED_MEDIA_V2)
    MediaKeys* mediaKeys() const { return m_mediaKeys.get(); }
    void setMediaKeys(MediaKeys*);
#endif

antti's avatar
antti committed
206 207 208 209 210 211 212
// controls
    bool controls() const;
    void setControls(bool);
    float volume() const;
    void setVolume(float, ExceptionCode&);
    bool muted() const;
    void setMuted(bool);
213

214
    void togglePlayState();
215 216
    void beginScrubbing();
    void endScrubbing();
217
    
adele@apple.com's avatar
adele@apple.com committed
218 219
    bool canPlay() const;

220 221
    float percentLoaded() const;

222
#if ENABLE(VIDEO_TRACK)
223 224 225
    PassRefPtr<TextTrack> addTextTrack(const String& kind, const String& label, const String& language, ExceptionCode&);
    PassRefPtr<TextTrack> addTextTrack(const String& kind, const String& label, ExceptionCode& ec) { return addTextTrack(kind, label, emptyString(), ec); }
    PassRefPtr<TextTrack> addTextTrack(const String& kind, ExceptionCode& ec) { return addTextTrack(kind, emptyString(), emptyString(), ec); }
226 227

    TextTrackList* textTracks();
eric.carlson@apple.com's avatar
eric.carlson@apple.com committed
228
    CueList currentlyActiveCues() const { return m_currentlyActiveCues; }
229

230
    void addTrack(TextTrack*);
231 232
    void removeTrack(TextTrack*);
    void removeAllInbandTracks();
233 234
    void closeCaptionTracksChanged();
    void notifyMediaPlayerOfTextTrackChanges();
235

236
    virtual void didAddTrack(HTMLTrackElement*);
237
    virtual void didRemoveTrack(HTMLTrackElement*);
238

239 240 241
    virtual void mediaPlayerDidAddTrack(PassRefPtr<InbandTextTrackPrivate>) OVERRIDE;
    virtual void mediaPlayerDidRemoveTrack(PassRefPtr<InbandTextTrackPrivate>) OVERRIDE;

242 243 244 245 246 247
#if USE(PLATFORM_TEXT_TRACK_MENU)
    virtual void setSelectedTextTrack(PassRefPtr<PlatformTextTrack>) OVERRIDE;
    virtual Vector<RefPtr<PlatformTextTrack> > platformTextTracks() OVERRIDE;
    PlatformTextTrackMenuInterface* platformTextTrackMenu();
#endif

248 249 250 251 252 253 254 255 256 257 258
    struct TrackGroup {
        enum GroupKind { CaptionsAndSubtitles, Description, Chapter, Metadata, Other };

        TrackGroup(GroupKind kind)
            : visibleTrack(0)
            , defaultTrack(0)
            , kind(kind)
            , hasSrcLang(false)
        {
        }

259 260 261
        Vector<RefPtr<TextTrack> > tracks;
        RefPtr<TextTrack> visibleTrack;
        RefPtr<TextTrack> defaultTrack;
262 263 264 265 266
        GroupKind kind;
        bool hasSrcLang;
    };

    void configureTextTrackGroupForLanguage(const TrackGroup&) const;
267
    void configureTextTracks();
268
    void configureTextTrackGroup(const TrackGroup&);
269

270
    void setSelectedTextTrack(TextTrack*);
271
    static int textTracksOffIndex() { return -1; }
272
    static int textTracksIndexNotFound() { return -2; }
273

274
    bool userPrefersCaptions() const;
275
    bool textTracksAreReady() const;
eric.carlson@apple.com's avatar
eric.carlson@apple.com committed
276
    void configureTextTrackDisplay();
277
    void updateTextTrackDisplay();
278 279 280

    // TextTrackClient
    virtual void textTrackReadyStateChanged(TextTrack*);
281
    virtual void textTrackKindChanged(TextTrack*);
282 283 284 285 286
    virtual void textTrackModeChanged(TextTrack*);
    virtual void textTrackAddCues(TextTrack*, const TextTrackCueList*);
    virtual void textTrackRemoveCues(TextTrack*, const TextTrackCueList*);
    virtual void textTrackAddCue(TextTrack*, PassRefPtr<TextTrackCue>);
    virtual void textTrackRemoveCue(TextTrack*, PassRefPtr<TextTrackCue>);
287 288 289

    bool requiresTextTrackRepresentation() const;
    void setTextTrackRepresentation(TextTrackRepresentation*);
290 291
#endif

292
#if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
293
    void allocateMediaPlayerIfNecessary();
294
    void setNeedWidgetUpdate(bool needWidgetUpdate) { m_needWidgetUpdate = needWidgetUpdate; }
295 296 297 298
    void deliverNotification(MediaPlayerProxyNotificationType notification);
    void setMediaPlayerProxy(WebMediaPlayerProxy* proxy);
    void getPluginProxyParams(KURL& url, Vector<String>& names, Vector<String>& values);
    void createMediaPlayerProxy();
299
    void updateWidget(PluginCreationOption);
300 301
#endif

302 303 304 305 306 307 308
    // EventTarget function.
    // Both Node (via HTMLElement) and ActiveDOMObject define this method, which
    // causes an ambiguity error at compile time. This class's constructor
    // ensures that both implementations return document, so return the result
    // of one of them here.
    virtual ScriptExecutionContext* scriptExecutionContext() const OVERRIDE { return HTMLElement::scriptExecutionContext(); }

309
    bool hasSingleSecurityOrigin() const { return !m_player || m_player->hasSingleSecurityOrigin(); }
310
    
311
    bool isFullscreen() const;
312 313
    void enterFullscreen();
    void exitFullscreen();
314

315 316 317 318
    bool hasClosedCaptions() const;
    bool closedCaptionsVisible() const;
    void setClosedCaptionsVisible(bool);

319
    MediaControls* mediaControls() const;
320

321
    void sourceWasRemoved(HTMLSourceElement*);
322 323
    void sourceWasAdded(HTMLSourceElement*);

324 325
    void privateBrowsingStateDidChange();

326 327 328 329
    // Media cache management.
    static void getSitesInMediaCache(Vector<String>&);
    static void clearMediaCache();
    static void clearMediaCacheForSite(const String&);
330
    static void resetMediaEngines();
331

332 333
    bool isPlaying() const { return m_playing; }

334 335
    virtual bool hasPendingActivity() const;

336 337 338 339 340 341 342
#if ENABLE(WEB_AUDIO)
    MediaElementAudioSourceNode* audioSourceNode() { return m_audioSourceNode; }
    void setAudioSourceNode(MediaElementAudioSourceNode*);

    AudioSourceProvider* audioSourceProvider();
#endif

343 344 345
    enum InvalidURLAction { DoNothing, Complain };
    bool isSafeToLoadURL(const KURL&, InvalidURLAction);

346 347 348 349 350 351
    const String& mediaGroup() const;
    void setMediaGroup(const String&);

    MediaController* controller() const;
    void setController(PassRefPtr<MediaController>);

352
    virtual bool dispatchEvent(PassRefPtr<Event>) OVERRIDE;
353

354 355
    virtual bool willRespondToMouseClickEvents() OVERRIDE;

356 357
    virtual void reportMemoryUsage(MemoryObjectInfo*) const;

antti's avatar
antti committed
358
protected:
359
    HTMLMediaElement(const QualifiedName&, Document*, bool);
360
    virtual ~HTMLMediaElement();
361

362
    virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
363
    virtual void finishParsingChildren();
364
    virtual bool isURLAttribute(const Attribute&) const OVERRIDE;
365
    virtual void attach() OVERRIDE;
366

367
    virtual void didMoveToNewDocument(Document* oldDocument) OVERRIDE;
368

369
    enum DisplayMode { Unknown, None, Poster, PosterWaitingForVideo, Video };
370 371
    DisplayMode displayMode() const { return m_displayMode; }
    virtual void setDisplayMode(DisplayMode mode) { m_displayMode = mode; }
372 373
    
    virtual bool isMediaElement() const { return true; }
374

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    // Restrictions to change default behaviors.
    enum BehaviorRestrictionFlags {
        NoRestrictions = 0,
        RequireUserGestureForLoadRestriction = 1 << 0,
        RequireUserGestureForRateChangeRestriction = 1 << 1,
        RequireUserGestureForFullscreenRestriction = 1 << 2,
        RequirePageConsentToLoadMediaRestriction = 1 << 3,
    };
    typedef unsigned BehaviorRestrictions;
    
    bool userGestureRequiredForLoad() const { return m_restrictions & RequireUserGestureForLoadRestriction; }
    bool userGestureRequiredForRateChange() const { return m_restrictions & RequireUserGestureForRateChangeRestriction; }
    bool userGestureRequiredForFullscreen() const { return m_restrictions & RequireUserGestureForFullscreenRestriction; }
    bool pageConsentRequiredForLoad() const { return m_restrictions & RequirePageConsentToLoadMediaRestriction; }
    
    void addBehaviorRestriction(BehaviorRestrictions restriction) { m_restrictions |= restriction; }
    void removeBehaviorRestriction(BehaviorRestrictions restriction) { m_restrictions &= ~restriction; }
392 393 394 395 396 397 398

#if ENABLE(VIDEO_TRACK)
    bool ignoreTrackDisplayUpdateRequests() const { return m_ignoreTrackDisplayUpdate > 0; }
    void beginIgnoringTrackDisplayUpdateRequests();
    void endIgnoringTrackDisplayUpdateRequests();
#endif

399
private:
400 401
    void createMediaPlayer();

402
    virtual bool alwaysCreateUserAgentShadowRoot() const OVERRIDE { return true; }
403 404
    virtual bool areAuthorShadowsAllowed() const OVERRIDE { return false; }

405
    virtual bool hasCustomFocusLogic() const OVERRIDE;
406
    virtual bool supportsFocus() const;
407
    virtual bool isMouseFocusable() const;
408
    virtual bool rendererIsNeeded(const NodeRenderingContext&);
409
    virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
410
    virtual bool childShouldCreateRenderer(const NodeRenderingContext&) const OVERRIDE;
411 412
    virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
    virtual void removedFrom(ContainerNode*) OVERRIDE;
antti@apple.com's avatar
antti@apple.com committed
413
    virtual void didRecalcStyle(StyleChange);
414 415
    
    virtual void defaultEventHandler(Event*);
416 417 418 419

    virtual void didBecomeFullscreenElement();
    virtual void willStopBeingFullscreenElement();

420 421
    // ActiveDOMObject functions.
    virtual bool canSuspend() const;
422
    virtual void suspend(ReasonForSuspension);
423 424 425
    virtual void resume();
    virtual void stop();
    
mitz@apple.com's avatar
mitz@apple.com committed
426
    virtual void mediaVolumeDidChange();
427 428

    virtual void updateDisplayState() { }
antti's avatar
antti committed
429
    
430 431
    void setReadyState(MediaPlayer::ReadyState);
    void setNetworkState(MediaPlayer::NetworkState);
432

433
    virtual Document* mediaPlayerOwningDocument();
antti@apple.com's avatar
antti@apple.com committed
434 435 436
    virtual void mediaPlayerNetworkStateChanged(MediaPlayer*);
    virtual void mediaPlayerReadyStateChanged(MediaPlayer*);
    virtual void mediaPlayerTimeChanged(MediaPlayer*);
437
    virtual void mediaPlayerVolumeChanged(MediaPlayer*);
438
    virtual void mediaPlayerMuteChanged(MediaPlayer*);
439 440
    virtual void mediaPlayerDurationChanged(MediaPlayer*);
    virtual void mediaPlayerRateChanged(MediaPlayer*);
441
    virtual void mediaPlayerPlaybackStateChanged(MediaPlayer*);
442
    virtual void mediaPlayerSawUnsupportedTracks(MediaPlayer*);
443
    virtual void mediaPlayerResourceNotSupported(MediaPlayer*);
444 445 446 447
    virtual void mediaPlayerRepaint(MediaPlayer*);
    virtual void mediaPlayerSizeChanged(MediaPlayer*);
#if USE(ACCELERATED_COMPOSITING)
    virtual bool mediaPlayerRenderingCanBeAccelerated(MediaPlayer*);
448
    virtual void mediaPlayerRenderingModeChanged(MediaPlayer*);
449
#endif
450
    virtual void mediaPlayerEngineUpdated(MediaPlayer*);
451
    
452
    virtual void mediaPlayerFirstVideoFrameAvailable(MediaPlayer*);
453
    virtual void mediaPlayerCharacteristicChanged(MediaPlayer*);
454

455 456 457
#if ENABLE(ENCRYPTED_MEDIA)
    virtual void mediaPlayerKeyAdded(MediaPlayer*, const String& keySystem, const String& sessionId) OVERRIDE;
    virtual void mediaPlayerKeyError(MediaPlayer*, const String& keySystem, const String& sessionId, MediaPlayerClient::MediaKeyErrorCode, unsigned short systemCode) OVERRIDE;
458
    virtual void mediaPlayerKeyMessage(MediaPlayer*, const String& keySystem, const String& sessionId, const unsigned char* message, unsigned messageLength, const KURL& defaultURL) OVERRIDE;
459
    virtual bool mediaPlayerKeyNeeded(MediaPlayer*, const String& keySystem, const String& sessionId, const unsigned char* initData, unsigned initDataLength) OVERRIDE;
460 461
#endif

462
#if ENABLE(ENCRYPTED_MEDIA_V2)
463
    virtual bool mediaPlayerKeyNeeded(MediaPlayer*, Uint8Array*);
464 465
#endif

466
    virtual String mediaPlayerReferrer() const OVERRIDE;
467
    virtual String mediaPlayerUserAgent() const OVERRIDE;
468
    virtual CORSMode mediaPlayerCORSMode() const OVERRIDE;
469

470 471 472
    virtual bool mediaPlayerNeedsSiteSpecificHacks() const OVERRIDE;
    virtual String mediaPlayerDocumentHost() const OVERRIDE;

473
    virtual void mediaPlayerEnterFullscreen() OVERRIDE;
474
    virtual void mediaPlayerExitFullscreen() OVERRIDE;
475 476
    virtual bool mediaPlayerIsFullscreen() const OVERRIDE;
    virtual bool mediaPlayerIsFullscreenPermitted() const OVERRIDE;
477 478 479 480 481 482
    virtual bool mediaPlayerIsVideo() const OVERRIDE;
    virtual LayoutRect mediaPlayerContentBoxRect() const OVERRIDE;
    virtual void mediaPlayerSetSize(const IntSize&) OVERRIDE;
    virtual void mediaPlayerPause() OVERRIDE;
    virtual void mediaPlayerPlay() OVERRIDE;
    virtual bool mediaPlayerIsPaused() const OVERRIDE;
483
    virtual bool mediaPlayerIsLooping() const OVERRIDE;
484 485
    virtual HostWindow* mediaPlayerHostWindow() OVERRIDE;
    virtual IntRect mediaPlayerWindowClipRect() OVERRIDE;
486
    virtual CachedResourceLoader* mediaPlayerCachedResourceLoader() OVERRIDE;
487

488 489 490 491
#if PLATFORM(WIN) && USE(AVFOUNDATION)
    virtual GraphicsDeviceAdapter* mediaPlayerGraphicsDeviceAdapter(const MediaPlayer*) const OVERRIDE;
#endif

antti's avatar
antti committed
492 493
    void loadTimerFired(Timer<HTMLMediaElement>*);
    void progressEventTimerFired(Timer<HTMLMediaElement>*);
494 495 496 497 498 499
    void playbackProgressTimerFired(Timer<HTMLMediaElement>*);
    void startPlaybackProgressTimer();
    void startProgressEventTimer();
    void stopPeriodicTimers();

    void seek(float time, ExceptionCode&);
500
    void finishSeek();
antti's avatar
antti committed
501
    void checkIfSeekNeeded();
502
    void addPlayedRange(float start, float end);
503 504 505 506 507 508
    
    void scheduleTimeupdateEvent(bool periodicEvent);
    void scheduleEvent(const AtomicString& eventName);
    
    // loading
    void selectMediaResource();
509
    void loadResource(const KURL&, ContentType&, const String& keySystem);
510
    void scheduleNextSourceChild();
511 512
    void loadNextSourceChild();
    void userCancelledLoad();
513
    void clearMediaPlayer(int flags);
514 515 516
    bool havePotentialSourceChild();
    void noneSupported();
    void mediaEngineError(PassRefPtr<MediaError> err);
517
    void cancelPendingEventsAndCallbacks();
518
    void waitForSourceChange();
519
    void prepareToPlay();
520

521 522
    KURL selectNextSourceChild(ContentType*, String* keySystem, InvalidURLAction);

523
    void mediaLoadingFailed(MediaPlayer::NetworkState);
524

525
#if ENABLE(VIDEO_TRACK)
526
    void updateActiveTextTrackCues(float);
527
    HTMLTrackElement* showingTrackWithSameKind(HTMLTrackElement*) const;
528

529 530
    void markCaptionAndSubtitleTracksAsUnconfigured();
    virtual void captionPreferencesChanged() OVERRIDE;
531 532
#endif

533
    // These "internal" functions do not check user gesture restrictions.
534 535 536
    void loadInternal();
    void playInternal();
    void pauseInternal();
537 538

    void prepareForLoad();
539 540
    void allowVideoRendering();

541 542 543
    bool processingMediaPlayerCallback() const { return m_processingMediaPlayerCallback > 0; }
    void beginProcessingMediaPlayerCallback() { ++m_processingMediaPlayerCallback; }
    void endProcessingMediaPlayerCallback() { ASSERT(m_processingMediaPlayerCallback); --m_processingMediaPlayerCallback; }
544

antti@apple.com's avatar
antti@apple.com committed
545 546
    void updateVolume();
    void updatePlayState();
547
    bool potentiallyPlaying() const;
antti's avatar
antti committed
548
    bool endedPlayback() const;
549 550
    bool stoppedDueToErrors() const;
    bool pausedForUserInteraction() const;
551
    bool couldPlayIfEnoughData() const;
552

553 554 555
    float minTimeSeekable() const;
    float maxTimeSeekable() const;

556 557 558
    // Pauses playback without changing any states or generating events
    void setPausedInternal(bool);

559 560
    void setPlaybackRateInternal(float);

561 562
    virtual void mediaCanStart();

563
    void setShouldDelayLoadEvent(bool);
564 565 566
    void invalidateCachedTime();
    void refreshCachedTime() const;

567
    bool hasMediaControls() const;
568
    bool createMediaControls();
569
    void configureMediaControls();
570

571 572 573
    void prepareMediaFragmentURI();
    void applyMediaFragmentURI();

574 575
    virtual void* preDispatchEventHandler(Event*);

576 577
    void changeNetworkStateFromLoadingToIdle();

578 579
    void removeBehaviorsRestrictionsAfterFirstUserGesture();

580 581 582 583 584
#if ENABLE(MICRODATA)
    virtual String itemValueText() const;
    virtual void setItemValueText(const String&, ExceptionCode&);
#endif

585 586 587 588 589 590 591
    void updateMediaController();
    bool isBlocked() const;
    bool isBlockedOnMediaController() const;
    bool hasCurrentSrc() const { return !m_currentSrc.isEmpty(); }
    bool isLiveStream() const { return movieLoadType() == MediaPlayer::LiveStream; }
    bool isAutoplaying() const { return m_autoplaying; }

592 593 594 595 596
#if PLATFORM(MAC)
    void updateDisableSleep();
    bool shouldDisableSleep() const;
#endif

antti's avatar
antti committed
597 598
    Timer<HTMLMediaElement> m_loadTimer;
    Timer<HTMLMediaElement> m_progressEventTimer;
599
    Timer<HTMLMediaElement> m_playbackProgressTimer;
600
    RefPtr<TimeRanges> m_playedTimeRanges;
601
    OwnPtr<GenericEventQueue> m_asyncEventQueue;
602

603
    float m_playbackRate;
antti's avatar
antti committed
604
    float m_defaultPlaybackRate;
605
    bool m_webkitPreservesPitch;
antti's avatar
antti committed
606 607
    NetworkState m_networkState;
    ReadyState m_readyState;
608
    ReadyState m_readyStateMaximum;
609 610
    KURL m_currentSrc;

antti's avatar
antti committed
611
    RefPtr<MediaError> m_error;
612

antti's avatar
antti committed
613
    float m_volume;
614
    float m_lastSeekTime;
antti@apple.com's avatar
antti@apple.com committed
615
    
antti's avatar
antti committed
616 617
    unsigned m_previousProgress;
    double m_previousProgressTime;
618

619
    // The last time a timeupdate event was sent (wall clock).
620 621
    double m_lastTimeUpdateEventWallTime;

622
    // The last time a timeupdate event was sent in movie time.
623
    float m_lastTimeUpdateEventMovieTime;
antti's avatar
antti committed
624
    
625
    // Loading state.
626 627
    enum LoadState { WaitingForSource, LoadingFromSrcAttr, LoadingFromSourceElement };
    LoadState m_loadState;
628 629
    RefPtr<HTMLSourceElement> m_currentSourceNode;
    RefPtr<Node> m_nextChildNodeToConsider;
630

antti@apple.com's avatar
antti@apple.com committed
631
    OwnPtr<MediaPlayer> m_player;
632 633 634
#if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
    RefPtr<Widget> m_proxyWidget;
#endif
635

636
    BehaviorRestrictions m_restrictions;
637 638
    
    MediaPlayer::Preload m_preload;
639

640
    DisplayMode m_displayMode;
641

642 643
    // Counter incremented while processing a callback from the media player, so we can avoid
    // calling the media engine recursively.
644 645
    int m_processingMediaPlayerCallback;

646
#if ENABLE(MEDIA_SOURCE)
647
    RefPtr<MediaSource> m_mediaSource;
648 649
#endif

650 651 652
    mutable float m_cachedTime;
    mutable double m_cachedTimeWallClockUpdateTime;
    mutable double m_minimumWallClockTimeToCacheMediaTime;
653 654 655 656

    double m_fragmentStartTime;
    double m_fragmentEndTime;

657 658
    typedef unsigned PendingActionFlags;
    PendingActionFlags m_pendingActionFlags;
659

660 661
    bool m_playing : 1;
    bool m_isWaitingUntilMediaCanStart : 1;
662
    bool m_shouldDelayLoadEvent : 1;
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
    bool m_haveFiredLoadedData : 1;
    bool m_inActiveDocument : 1;
    bool m_autoplaying : 1;
    bool m_muted : 1;
    bool m_paused : 1;
    bool m_seeking : 1;

    // data has not been loaded since sending a "stalled" event
    bool m_sentStalledEvent : 1;

    // time has not changed since sending an "ended" event
    bool m_sentEndEvent : 1;

    bool m_pausedInternal : 1;

678 679
    // Not all media engines provide enough information about a file to be able to
    // support progress events so setting m_sendProgressEvents disables them 
680
    bool m_sendProgressEvents : 1;
681

682
    bool m_isFullscreen : 1;
683
    bool m_closedCaptionsVisible : 1;
684

685
#if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
686
    bool m_needWidgetUpdate : 1;
687
#endif
688 689

    bool m_dispatchingCanPlayEvent : 1;
690
    bool m_loadInitiatedByUserGesture : 1;
691
    bool m_completelyLoaded : 1;
692
    bool m_havePreparedToPlay : 1;
693
    bool m_parsingInProgress : 1;
694 695

#if ENABLE(VIDEO_TRACK)
696
    bool m_tracksAreReady : 1;
eric.carlson@apple.com's avatar
eric.carlson@apple.com committed
697
    bool m_haveVisibleTextTrack : 1;
698
    bool m_processingPreferenceChange : 1;
699
    float m_lastTextTrackUpdateTime;
eric.carlson@apple.com's avatar
eric.carlson@apple.com committed
700

701
    RefPtr<TextTrackList> m_textTracks;
702
    Vector<RefPtr<TextTrack> > m_textTracksWhenResourceSelectionBegan;
703

704
    CueIntervalTree m_cueTree;
705

eric.carlson@apple.com's avatar
eric.carlson@apple.com committed
706
    CueList m_currentlyActiveCues;
707
    int m_ignoreTrackDisplayUpdate;
708
#endif
709

710 711 712 713 714 715 716
#if ENABLE(WEB_AUDIO)
    // This is a weak reference, since m_audioSourceNode holds a reference to us.
    // The value is set just after the MediaElementAudioSourceNode is created.
    // The value is cleared in MediaElementAudioSourceNode::~MediaElementAudioSourceNode().
    MediaElementAudioSourceNode* m_audioSourceNode;
#endif

717 718 719
    String m_mediaGroup;
    friend class MediaController;
    RefPtr<MediaController> m_mediaController;
720 721 722 723

#if PLATFORM(MAC)
    OwnPtr<DisplaySleepDisabler> m_sleepDisabler;
#endif
724 725

    friend class TrackDisplayUpdateScope;
726 727 728 729

#if ENABLE(ENCRYPTED_MEDIA_V2)
    RefPtr<MediaKeys> m_mediaKeys;
#endif
730 731 732 733

#if USE(PLATFORM_TEXT_TRACK_MENU)
    RefPtr<PlatformTextTrackMenuInterface> m_platformMenu;
#endif
antti's avatar
antti committed
734 735
};

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
#if ENABLE(VIDEO_TRACK)
#ifndef NDEBUG
// Template specializations required by PodIntervalTree in debug mode.
template <>
struct ValueToString<double> {
    static String string(const double value)
    {
        return String::number(value);
    }
};

template <>
struct ValueToString<TextTrackCue*> {
    static String string(TextTrackCue* const& cue)
    {
751
        return String::format("%p id=%s interval=%f-->%f cue=%s)", cue, cue->id().utf8().data(), cue->startTime(), cue->endTime(), cue->text().utf8().data());
752 753 754 755 756
    }
};
#endif
#endif

757 758 759 760 761 762 763
inline bool isMediaElement(Node* node)
{
    return node && node->isElementNode() && toElement(node)->isMediaElement();
}

inline HTMLMediaElement* toMediaElement(Node* node)
{
764
    ASSERT_WITH_SECURITY_IMPLICATION(!node || isMediaElement(node));
765 766 767
    return static_cast<HTMLMediaElement*>(node);
}

antti's avatar
antti committed
768 769 770 771
} //namespace

#endif
#endif