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 SourceBuffer_h
32#define SourceBuffer_h
33
34#include "core/dom/ActiveDOMObject.h"
35#include "core/fileapi/FileReaderLoaderClient.h"
36#include "modules/EventTargetModules.h"
37#include "platform/AsyncMethodRunner.h"
38#include "platform/weborigin/KURL.h"
39#include "public/platform/WebSourceBufferClient.h"
40#include "wtf/Forward.h"
41#include "wtf/RefCounted.h"
42#include "wtf/text/WTFString.h"
43
44namespace blink {
45
46class ExceptionState;
47class FileReaderLoader;
48class GenericEventQueue;
49class MediaSource;
50class Stream;
51class TimeRanges;
52class WebSourceBuffer;
53
54class SourceBuffer FINAL : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<SourceBuffer>, public ActiveDOMObject, public EventTargetWithInlineData, public FileReaderLoaderClient, public WebSourceBufferClient {
55    DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<SourceBuffer>);
56    DEFINE_WRAPPERTYPEINFO();
57    WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(SourceBuffer);
58public:
59    static SourceBuffer* create(PassOwnPtr<WebSourceBuffer>, MediaSource*, GenericEventQueue*);
60    static const AtomicString& segmentsKeyword();
61    static const AtomicString& sequenceKeyword();
62
63    virtual ~SourceBuffer();
64
65    // SourceBuffer.idl methods
66    const AtomicString& mode() const { return m_mode; }
67    void setMode(const AtomicString&, ExceptionState&);
68    bool updating() const { return m_updating; }
69    PassRefPtrWillBeRawPtr<TimeRanges> buffered(ExceptionState&) const;
70    double timestampOffset() const;
71    void setTimestampOffset(double, ExceptionState&);
72    void appendBuffer(PassRefPtr<ArrayBuffer> data, ExceptionState&);
73    void appendBuffer(PassRefPtr<ArrayBufferView> data, ExceptionState&);
74    void appendStream(PassRefPtrWillBeRawPtr<Stream>, ExceptionState&);
75    void appendStream(PassRefPtrWillBeRawPtr<Stream>, unsigned long long maxSize, ExceptionState&);
76    void abort(ExceptionState&);
77    void remove(double start, double end, ExceptionState&);
78    double appendWindowStart() const;
79    void setAppendWindowStart(double, ExceptionState&);
80    double appendWindowEnd() const;
81    void setAppendWindowEnd(double, ExceptionState&);
82
83    void abortIfUpdating();
84    void removedFromMediaSource();
85
86    // ActiveDOMObject interface
87    virtual bool hasPendingActivity() const OVERRIDE;
88    virtual void suspend() OVERRIDE;
89    virtual void resume() OVERRIDE;
90    virtual void stop() OVERRIDE;
91
92    // EventTarget interface
93    virtual ExecutionContext* executionContext() const OVERRIDE;
94    virtual const AtomicString& interfaceName() const OVERRIDE;
95
96    // WebSourceBufferClient interface
97    virtual void initializationSegmentReceived() OVERRIDE;
98
99    virtual void trace(Visitor*) OVERRIDE;
100
101private:
102    SourceBuffer(PassOwnPtr<WebSourceBuffer>, MediaSource*, GenericEventQueue*);
103
104    bool isRemoved() const;
105    void scheduleEvent(const AtomicString& eventName);
106
107    void appendBufferInternal(const unsigned char*, unsigned, ExceptionState&);
108    void appendBufferAsyncPart();
109
110    void removeAsyncPart();
111
112    void appendStreamInternal(PassRefPtrWillBeRawPtr<Stream>, ExceptionState&);
113    void appendStreamAsyncPart();
114    void appendStreamDone(bool success);
115    void clearAppendStreamState();
116
117    // FileReaderLoaderClient interface
118    virtual void didStartLoading() OVERRIDE;
119    virtual void didReceiveDataForClient(const char* data, unsigned dataLength) OVERRIDE;
120    virtual void didFinishLoading() OVERRIDE;
121    virtual void didFail(FileError::ErrorCode) OVERRIDE;
122
123    OwnPtr<WebSourceBuffer> m_webSourceBuffer;
124    Member<MediaSource> m_source;
125    GenericEventQueue* m_asyncEventQueue;
126
127    AtomicString m_mode;
128    bool m_updating;
129    double m_timestampOffset;
130    double m_appendWindowStart;
131    double m_appendWindowEnd;
132    bool m_firstInitializationSegmentReceived;
133
134    Vector<unsigned char> m_pendingAppendData;
135    size_t m_pendingAppendDataOffset;
136    AsyncMethodRunner<SourceBuffer> m_appendBufferAsyncPartRunner;
137
138    double m_pendingRemoveStart;
139    double m_pendingRemoveEnd;
140    AsyncMethodRunner<SourceBuffer> m_removeAsyncPartRunner;
141
142    bool m_streamMaxSizeValid;
143    unsigned long long m_streamMaxSize;
144    AsyncMethodRunner<SourceBuffer> m_appendStreamAsyncPartRunner;
145    RefPtrWillBeMember<Stream> m_stream;
146    OwnPtr<FileReaderLoader> m_loader;
147};
148
149} // namespace blink
150
151#endif // SourceBuffer_h
152