673d60ce86a36c7a45c1f34fd04425e55282849e
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / mediasource / MediaSource.h
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
44 namespace blink {
45
46 class ExceptionState;
47 class GenericEventQueue;
48 class WebSourceBuffer;
49
50 class 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     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MediaSource);
57 public:
58     static const AtomicString& openKeyword();
59     static const AtomicString& closedKeyword();
60     static const AtomicString& endedKeyword();
61
62     static MediaSource* create(ExecutionContext*);
63     virtual ~MediaSource();
64
65     // MediaSource.idl methods
66     SourceBufferList* sourceBuffers() { return m_sourceBuffers.get(); }
67     SourceBufferList* activeSourceBuffers() { return m_activeSourceBuffers.get(); }
68     SourceBuffer* addSourceBuffer(const String& type, ExceptionState&);
69     void removeSourceBuffer(SourceBuffer*, ExceptionState&);
70     void setDuration(double, ExceptionState&);
71     const AtomicString& readyState() const { return m_readyState; }
72     void endOfStream(const AtomicString& error, ExceptionState&);
73     void endOfStream(ExceptionState&);
74     static bool isTypeSupported(const String& type);
75
76     // HTMLMediaSource
77     virtual bool attachToElement(HTMLMediaElement*) OVERRIDE;
78     virtual void setWebMediaSourceAndOpen(PassOwnPtr<WebMediaSource>) OVERRIDE;
79     virtual void close() OVERRIDE;
80     virtual bool isClosed() const OVERRIDE;
81     virtual double duration() const OVERRIDE;
82     virtual PassRefPtrWillBeRawPtr<TimeRanges> buffered() const OVERRIDE;
83 #if !ENABLE(OILPAN)
84     virtual void refHTMLMediaSource() OVERRIDE { ref(); }
85     virtual void derefHTMLMediaSource() OVERRIDE { deref(); }
86 #endif
87
88     // EventTarget interface
89     virtual const AtomicString& interfaceName() const OVERRIDE;
90     virtual ExecutionContext* executionContext() const OVERRIDE;
91
92     // ActiveDOMObject interface
93     virtual bool hasPendingActivity() const OVERRIDE;
94     virtual void stop() OVERRIDE;
95
96     // URLRegistrable interface
97     virtual URLRegistry& registry() const OVERRIDE;
98
99     // Used by SourceBuffer.
100     void openIfInEndedState();
101     bool isOpen() const;
102
103     // Used by MediaSourceRegistry.
104     void addedToRegistry();
105     void removedFromRegistry();
106
107     void trace(Visitor*);
108     void clearWeakMembers(Visitor*);
109
110 private:
111     explicit MediaSource(ExecutionContext*);
112
113     void setReadyState(const AtomicString&);
114     void onReadyStateChange(const AtomicString&, const AtomicString&);
115
116     bool isUpdating() const;
117
118     PassOwnPtr<WebSourceBuffer> createWebSourceBuffer(const String& type, const Vector<String>& codecs, ExceptionState&);
119     void scheduleEvent(const AtomicString& eventName);
120     void endOfStreamInternal(const WebMediaSource::EndOfStreamStatus, ExceptionState&);
121
122     // Implements the duration change algorithm.
123     // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#duration-change-algorithm
124     void durationChangeAlgorithm(double newDuration);
125
126     OwnPtr<WebMediaSource> m_webMediaSource;
127     AtomicString m_readyState;
128     OwnPtrWillBeMember<GenericEventQueue> m_asyncEventQueue;
129     RawPtrWillBeWeakMember<HTMLMediaElement> m_attachedElement;
130
131     Member<SourceBufferList> m_sourceBuffers;
132     Member<SourceBufferList> m_activeSourceBuffers;
133
134     bool m_isAddedToRegistry;
135 };
136
137 } // namespace blink
138
139 #endif