Home | History | Annotate | Download | only in mediasource
      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 "bindings/v8/ScriptWrappable.h"
     35 #include "core/dom/ActiveDOMObject.h"
     36 #include "core/events/EventTarget.h"
     37 #include "core/fileapi/FileReaderLoaderClient.h"
     38 #include "platform/AsyncMethodRunner.h"
     39 #include "platform/weborigin/KURL.h"
     40 #include "wtf/OwnPtr.h"
     41 #include "wtf/PassOwnPtr.h"
     42 #include "wtf/PassRefPtr.h"
     43 #include "wtf/RefCounted.h"
     44 #include "wtf/RefPtr.h"
     45 #include "wtf/text/WTFString.h"
     46 
     47 namespace blink {
     48 class WebSourceBuffer;
     49 }
     50 
     51 namespace WebCore {
     52 
     53 class ExceptionState;
     54 class FileReaderLoader;
     55 class GenericEventQueue;
     56 class MediaSource;
     57 class Stream;
     58 class TimeRanges;
     59 
     60 class SourceBuffer : public RefCounted<SourceBuffer>, public ActiveDOMObject, public EventTargetWithInlineData, public ScriptWrappable, public FileReaderLoaderClient {
     61     REFCOUNTED_EVENT_TARGET(SourceBuffer);
     62 public:
     63     static PassRefPtr<SourceBuffer> create(PassOwnPtr<blink::WebSourceBuffer>, MediaSource*, GenericEventQueue*);
     64 
     65     virtual ~SourceBuffer();
     66 
     67     // SourceBuffer.idl methods
     68     bool updating() const { return m_updating; }
     69     PassRefPtr<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(PassRefPtr<Stream>, ExceptionState&);
     75     void appendStream(PassRefPtr<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 private:
     97     SourceBuffer(PassOwnPtr<blink::WebSourceBuffer>, MediaSource*, GenericEventQueue*);
     98 
     99     bool isRemoved() const;
    100     void scheduleEvent(const AtomicString& eventName);
    101 
    102     void appendBufferInternal(const unsigned char*, unsigned, ExceptionState&);
    103     void appendBufferAsyncPart();
    104 
    105     void removeAsyncPart();
    106 
    107     void appendStreamInternal(PassRefPtr<Stream>, ExceptionState&);
    108     void appendStreamAsyncPart();
    109     void appendStreamDone(bool success);
    110     void clearAppendStreamState();
    111 
    112     // FileReaderLoaderClient interface
    113     virtual void didStartLoading() OVERRIDE;
    114     virtual void didReceiveDataForClient(const char* data, unsigned dataLength) OVERRIDE;
    115     virtual void didFinishLoading() OVERRIDE;
    116     virtual void didFail(FileError::ErrorCode) OVERRIDE;
    117 
    118     OwnPtr<blink::WebSourceBuffer> m_webSourceBuffer;
    119     MediaSource* m_source;
    120     GenericEventQueue* m_asyncEventQueue;
    121 
    122     bool m_updating;
    123     double m_timestampOffset;
    124     double m_appendWindowStart;
    125     double m_appendWindowEnd;
    126 
    127     Vector<unsigned char> m_pendingAppendData;
    128     AsyncMethodRunner<SourceBuffer> m_appendBufferAsyncPartRunner;
    129 
    130     double m_pendingRemoveStart;
    131     double m_pendingRemoveEnd;
    132     AsyncMethodRunner<SourceBuffer> m_removeAsyncPartRunner;
    133 
    134     bool m_streamMaxSizeValid;
    135     unsigned long long m_streamMaxSize;
    136     AsyncMethodRunner<SourceBuffer> m_appendStreamAsyncPartRunner;
    137     RefPtr<Stream> m_stream;
    138     OwnPtr<FileReaderLoader> m_loader;
    139 };
    140 
    141 } // namespace WebCore
    142 
    143 #endif
    144