b85351e5d97839047578cd90204122a878decc9a
[WebKit-https.git] / Source / WebCore / Modules / mediasource / SourceBuffer.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 SourceBuffer_h
32 #define SourceBuffer_h
33
34 #if ENABLE(MEDIA_SOURCE)
35
36 #include "ActiveDOMObject.h"
37 #include "AudioTrack.h"
38 #include "EventTarget.h"
39 #include "ExceptionCode.h"
40 #include "GenericEventQueue.h"
41 #include "ScriptWrappable.h"
42 #include "SourceBufferPrivateClient.h"
43 #include "TextTrack.h"
44 #include "Timer.h"
45 #include "VideoTrack.h"
46 #include <runtime/ArrayBufferView.h>
47 #include <wtf/PassRefPtr.h>
48 #include <wtf/RefCounted.h>
49 #include <wtf/text/WTFString.h>
50
51 namespace WebCore {
52
53 class AudioTrackList;
54 class MediaSource;
55 class SourceBufferPrivate;
56 class TextTrackList;
57 class TimeRanges;
58 class VideoTrackList;
59
60 class SourceBuffer FINAL : public RefCounted<SourceBuffer>, public ActiveDOMObject, public EventTargetWithInlineData, public ScriptWrappable, public SourceBufferPrivateClient, public AudioTrackClient, public VideoTrackClient, public TextTrackClient {
61 public:
62     static PassRef<SourceBuffer> create(PassRef<SourceBufferPrivate>, MediaSource*);
63
64     virtual ~SourceBuffer();
65
66     // SourceBuffer.idl methods
67     bool updating() const { return m_updating; }
68     PassRefPtr<TimeRanges> buffered(ExceptionCode&) const;
69     const RefPtr<TimeRanges>& buffered() const;
70     double timestampOffset() const;
71     void setTimestampOffset(double, ExceptionCode&);
72     void appendBuffer(PassRefPtr<ArrayBuffer> data, ExceptionCode&);
73     void appendBuffer(PassRefPtr<ArrayBufferView> data, ExceptionCode&);
74     void abort(ExceptionCode&);
75
76     void abortIfUpdating();
77     void removedFromMediaSource();
78     const MediaTime& highestPresentationEndTimestamp() const { return m_highestPresentationEndTimestamp; }
79
80 #if ENABLE(VIDEO_TRACK)
81     VideoTrackList* videoTracks();
82     AudioTrackList* audioTracks();
83     TextTrackList* textTracks();
84 #endif
85
86     // ActiveDOMObject interface
87     virtual bool hasPendingActivity() const OVERRIDE;
88     virtual void stop() OVERRIDE;
89
90     // EventTarget interface
91     virtual ScriptExecutionContext* scriptExecutionContext() const OVERRIDE { return ActiveDOMObject::scriptExecutionContext(); }
92     virtual EventTargetInterface eventTargetInterface() const OVERRIDE { return SourceBufferEventTargetInterfaceType; }
93
94     using RefCounted<SourceBuffer>::ref;
95     using RefCounted<SourceBuffer>::deref;
96
97 protected:
98     // EventTarget interface
99     virtual void refEventTarget() OVERRIDE { ref(); }
100     virtual void derefEventTarget() OVERRIDE { deref(); }
101
102 private:
103     SourceBuffer(PassRef<SourceBufferPrivate>, MediaSource*);
104
105     // SourceBufferPrivateClient
106     virtual void sourceBufferPrivateDidEndStream(SourceBufferPrivate*, const WTF::AtomicString&) OVERRIDE;
107     virtual void sourceBufferPrivateDidReceiveInitializationSegment(SourceBufferPrivate*, const InitializationSegment&) OVERRIDE;
108     virtual void sourceBufferPrivateDidReceiveSample(SourceBufferPrivate*, PassRefPtr<MediaSample>) OVERRIDE;
109     virtual bool sourceBufferPrivateHasAudio(const SourceBufferPrivate*) const OVERRIDE;
110     virtual bool sourceBufferPrivateHasVideo(const SourceBufferPrivate*) const OVERRIDE;
111     virtual void sourceBufferPrivateDidBecomeReadyForMoreSamples(SourceBufferPrivate*, AtomicString trackID) OVERRIDE;
112     virtual void sourceBufferPrivateSeekToTime(SourceBufferPrivate*, const MediaTime&);
113     virtual MediaTime sourceBufferPrivateFastSeekTimeForMediaTime(SourceBufferPrivate*, const MediaTime&, const MediaTime& negativeThreshold, const MediaTime& positiveThreshold);
114
115
116     // AudioTrackClient
117     virtual void audioTrackEnabledChanged(AudioTrack*) OVERRIDE;
118
119     // VideoTrackClient
120     virtual void videoTrackSelectedChanged(VideoTrack*) OVERRIDE;
121
122     // TextTrackClient
123     virtual void textTrackKindChanged(TextTrack*) OVERRIDE;
124     virtual void textTrackModeChanged(TextTrack*) OVERRIDE;
125     virtual void textTrackAddCues(TextTrack*, const TextTrackCueList*) OVERRIDE;
126     virtual void textTrackRemoveCues(TextTrack*, const TextTrackCueList*) OVERRIDE;
127     virtual void textTrackAddCue(TextTrack*, PassRefPtr<TextTrackCue>) OVERRIDE;
128     virtual void textTrackRemoveCue(TextTrack*, PassRefPtr<TextTrackCue>) OVERRIDE;
129
130     static const WTF::AtomicString& decodeError();
131     static const WTF::AtomicString& networkError();
132
133     bool isRemoved() const;
134     void scheduleEvent(const AtomicString& eventName);
135
136     void appendBufferInternal(unsigned char*, unsigned, ExceptionCode&);
137     void appendBufferTimerFired(Timer<SourceBuffer>*);
138
139     void setActive(bool);
140
141     bool validateInitializationSegment(const InitializationSegment&);
142
143     struct TrackBuffer;
144     void provideMediaData(TrackBuffer&, AtomicString trackID);
145     void didDropSample();
146
147     RefPtr<SourceBufferPrivate> m_private;
148     MediaSource* m_source;
149     GenericEventQueue m_asyncEventQueue;
150
151     bool m_updating;
152
153     Vector<unsigned char> m_pendingAppendData;
154     Timer<SourceBuffer> m_appendBufferTimer;
155
156     RefPtr<VideoTrackList> m_videoTracks;
157     RefPtr<AudioTrackList> m_audioTracks;
158     RefPtr<TextTrackList> m_textTracks;
159
160     Vector<AtomicString> m_videoCodecs;
161     Vector<AtomicString> m_audioCodecs;
162     Vector<AtomicString> m_textCodecs;
163
164     MediaTime m_timestampOffset;
165     MediaTime m_highestPresentationEndTimestamp;
166
167     HashMap<AtomicString, TrackBuffer> m_trackBufferMap;
168     bool m_receivedFirstInitializationSegment;
169     RefPtr<TimeRanges> m_buffered;
170     bool m_active;
171
172     enum AppendStateType { WaitingForSegment, ParsingInitSegment, ParsingMediaSegment };
173     AppendStateType m_appendState;
174
175 };
176
177 } // namespace WebCore
178
179 #endif
180
181 #endif