1/*
2 * Copyright (C) 2013 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef MediaSource_h
32#define MediaSource_h
33
34#include "core/dom/ActiveDOMObject.h"
35#include "core/events/EventTarget.h"
36#include "core/html/HTMLMediaSource.h"
37#include "core/html/URLRegistry.h"
38#include "modules/mediasource/SourceBuffer.h"
39#include "modules/mediasource/SourceBufferList.h"
40#include "public/platform/WebMediaSource.h"
41#include "wtf/PassOwnPtr.h"
42#include "wtf/Vector.h"
43
44namespace blink {
45
46class ExceptionState;
47class GenericEventQueue;
48class WebSourceBuffer;
49
50class MediaSource FINAL
51    : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<MediaSource>
52    , public HTMLMediaSource
53    , public ActiveDOMObject
54    , public EventTargetWithInlineData {
55    DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<MediaSource>);
56    DEFINE_WRAPPERTYPEINFO();
57    WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MediaSource);
58public:
59    static const AtomicString& openKeyword();
60    static const AtomicString& closedKeyword();
61    static const AtomicString& endedKeyword();
62
63    static MediaSource* create(ExecutionContext*);
64    virtual ~MediaSource();
65
66    // MediaSource.idl methods
67    SourceBufferList* sourceBuffers() { return m_sourceBuffers.get(); }
68    SourceBufferList* activeSourceBuffers() { return m_activeSourceBuffers.get(); }
69    SourceBuffer* addSourceBuffer(const String& type, ExceptionState&);
70    void removeSourceBuffer(SourceBuffer*, ExceptionState&);
71    void setDuration(double, ExceptionState&);
72    const AtomicString& readyState() const { return m_readyState; }
73    void endOfStream(const AtomicString& error, ExceptionState&);
74    void endOfStream(ExceptionState&);
75    static bool isTypeSupported(const String& type);
76
77    // HTMLMediaSource
78    virtual bool attachToElement(HTMLMediaElement*) OVERRIDE;
79    virtual void setWebMediaSourceAndOpen(PassOwnPtr<WebMediaSource>) OVERRIDE;
80    virtual void close() OVERRIDE;
81    virtual bool isClosed() const OVERRIDE;
82    virtual double duration() const OVERRIDE;
83    virtual PassRefPtrWillBeRawPtr<TimeRanges> buffered() const OVERRIDE;
84#if !ENABLE(OILPAN)
85    virtual void refHTMLMediaSource() OVERRIDE { ref(); }
86    virtual void derefHTMLMediaSource() OVERRIDE { deref(); }
87#endif
88
89    // EventTarget interface
90    virtual const AtomicString& interfaceName() const OVERRIDE;
91    virtual ExecutionContext* executionContext() const OVERRIDE;
92
93    // ActiveDOMObject interface
94    virtual bool hasPendingActivity() const OVERRIDE;
95    virtual void stop() OVERRIDE;
96
97    // URLRegistrable interface
98    virtual URLRegistry& registry() const OVERRIDE;
99
100    // Used by SourceBuffer.
101    void openIfInEndedState();
102    bool isOpen() const;
103    void setSourceBufferActive(SourceBuffer*);
104
105    // Used by MediaSourceRegistry.
106    void addedToRegistry();
107    void removedFromRegistry();
108
109    void trace(Visitor*);
110    void clearWeakMembers(Visitor*);
111
112private:
113    explicit MediaSource(ExecutionContext*);
114
115    void setReadyState(const AtomicString&);
116    void onReadyStateChange(const AtomicString&, const AtomicString&);
117
118    bool isUpdating() const;
119
120    PassOwnPtr<WebSourceBuffer> createWebSourceBuffer(const String& type, const Vector<String>& codecs, ExceptionState&);
121    void scheduleEvent(const AtomicString& eventName);
122    void endOfStreamInternal(const WebMediaSource::EndOfStreamStatus, ExceptionState&);
123
124    // Implements the duration change algorithm.
125    // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#duration-change-algorithm
126    void durationChangeAlgorithm(double newDuration);
127
128    OwnPtr<WebMediaSource> m_webMediaSource;
129    AtomicString m_readyState;
130    OwnPtrWillBeMember<GenericEventQueue> m_asyncEventQueue;
131    RawPtrWillBeWeakMember<HTMLMediaElement> m_attachedElement;
132
133    Member<SourceBufferList> m_sourceBuffers;
134    Member<SourceBufferList> m_activeSourceBuffers;
135
136    bool m_isAddedToRegistry;
137};
138
139} // namespace blink
140
141#endif // MediaSource_h
142