Replace PassRef with Ref/Ref&& across the board.
[WebKit-https.git] / Source / WebCore / Modules / mediasource / SourceBuffer.h
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  * Copyright (C) 2013-2014 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #ifndef SourceBuffer_h
33 #define SourceBuffer_h
34
35 #if ENABLE(MEDIA_SOURCE)
36
37 #include "ActiveDOMObject.h"
38 #include "AudioTrack.h"
39 #include "EventTarget.h"
40 #include "ExceptionCode.h"
41 #include "GenericEventQueue.h"
42 #include "ScriptWrappable.h"
43 #include "SourceBufferPrivateClient.h"
44 #include "TextTrack.h"
45 #include "Timer.h"
46 #include "VideoTrack.h"
47 #include <runtime/ArrayBufferView.h>
48 #include <wtf/PassRefPtr.h>
49 #include <wtf/RefCounted.h>
50 #include <wtf/text/WTFString.h>
51
52 namespace WebCore {
53
54 class AudioTrackList;
55 class MediaSource;
56 class PlatformTimeRanges;
57 class SourceBufferPrivate;
58 class TextTrackList;
59 class TimeRanges;
60 class VideoTrackList;
61
62 class SourceBuffer final : public RefCounted<SourceBuffer>, public ActiveDOMObject, public EventTargetWithInlineData, public ScriptWrappable, public SourceBufferPrivateClient, public AudioTrackClient, public VideoTrackClient, public TextTrackClient {
63 public:
64     static Ref<SourceBuffer> create(Ref<SourceBufferPrivate>&&, MediaSource*);
65
66     static const AtomicString& segmentsKeyword();
67     static const AtomicString& sequenceKeyword();
68
69     virtual ~SourceBuffer();
70
71     // SourceBuffer.idl methods
72     bool updating() const { return m_updating; }
73     PassRefPtr<TimeRanges> buffered(ExceptionCode&) const;
74     const RefPtr<TimeRanges>& buffered() const;
75     double timestampOffset() const;
76     void setTimestampOffset(double, ExceptionCode&);
77     void appendBuffer(PassRefPtr<ArrayBuffer> data, ExceptionCode&);
78     void appendBuffer(PassRefPtr<ArrayBufferView> data, ExceptionCode&);
79     void abort(ExceptionCode&);
80     void remove(double start, double end, ExceptionCode&);
81     void remove(const MediaTime&, const MediaTime&, ExceptionCode&);
82
83     void abortIfUpdating();
84     void removedFromMediaSource();
85     void seekToTime(const MediaTime&);
86
87 #if ENABLE(VIDEO_TRACK)
88     VideoTrackList* videoTracks();
89     AudioTrackList* audioTracks();
90     TextTrackList* textTracks();
91 #endif
92
93     bool hasCurrentTime() const;
94     bool hasFutureTime() const;
95     bool canPlayThrough();
96
97     bool hasVideo() const;
98     bool hasAudio() const;
99
100     bool active() const { return m_active; }
101
102     // ActiveDOMObject interface
103     virtual bool hasPendingActivity() const override;
104     virtual void stop() override;
105
106     // EventTarget interface
107     virtual ScriptExecutionContext* scriptExecutionContext() const override { return ActiveDOMObject::scriptExecutionContext(); }
108     virtual EventTargetInterface eventTargetInterface() const override { return SourceBufferEventTargetInterfaceType; }
109
110     using RefCounted<SourceBuffer>::ref;
111     using RefCounted<SourceBuffer>::deref;
112
113     struct TrackBuffer;
114
115     Document& document() const;
116
117     const AtomicString& mode() const { return m_mode; }
118     void setMode(const AtomicString&, ExceptionCode&);
119
120     bool shouldGenerateTimestamps() const { return m_shouldGenerateTimestamps; }
121     void setShouldGenerateTimestamps(bool flag) { m_shouldGenerateTimestamps = flag; }
122
123 protected:
124     // EventTarget interface
125     virtual void refEventTarget() override { ref(); }
126     virtual void derefEventTarget() override { deref(); }
127
128 private:
129     SourceBuffer(Ref<SourceBufferPrivate>&&, MediaSource*);
130
131     // SourceBufferPrivateClient
132     virtual void sourceBufferPrivateDidEndStream(SourceBufferPrivate*, const WTF::AtomicString&) override;
133     virtual void sourceBufferPrivateDidReceiveInitializationSegment(SourceBufferPrivate*, const InitializationSegment&) override;
134     virtual void sourceBufferPrivateDidReceiveSample(SourceBufferPrivate*, PassRefPtr<MediaSample>) override;
135     virtual bool sourceBufferPrivateHasAudio(const SourceBufferPrivate*) const override;
136     virtual bool sourceBufferPrivateHasVideo(const SourceBufferPrivate*) const override;
137     virtual void sourceBufferPrivateDidBecomeReadyForMoreSamples(SourceBufferPrivate*, AtomicString trackID) override;
138     virtual MediaTime sourceBufferPrivateFastSeekTimeForMediaTime(SourceBufferPrivate*, const MediaTime&, const MediaTime& negativeThreshold, const MediaTime& positiveThreshold);
139     virtual void sourceBufferPrivateAppendComplete(SourceBufferPrivate*, AppendResult) override;
140     virtual void sourceBufferPrivateDidReceiveRenderingError(SourceBufferPrivate*, int errorCode) override;
141
142     // AudioTrackClient
143     virtual void audioTrackEnabledChanged(AudioTrack*) override;
144
145     // VideoTrackClient
146     virtual void videoTrackSelectedChanged(VideoTrack*) override;
147
148     // TextTrackClient
149     virtual void textTrackKindChanged(TextTrack*) override;
150     virtual void textTrackModeChanged(TextTrack*) override;
151     virtual void textTrackAddCues(TextTrack*, const TextTrackCueList*) override;
152     virtual void textTrackRemoveCues(TextTrack*, const TextTrackCueList*) override;
153     virtual void textTrackAddCue(TextTrack*, PassRefPtr<TextTrackCue>) override;
154     virtual void textTrackRemoveCue(TextTrack*, PassRefPtr<TextTrackCue>) override;
155
156     static const WTF::AtomicString& decodeError();
157     static const WTF::AtomicString& networkError();
158
159     bool isRemoved() const;
160     void scheduleEvent(const AtomicString& eventName);
161
162     void appendBufferInternal(unsigned char*, unsigned, ExceptionCode&);
163     void appendBufferTimerFired();
164     void resetParserState();
165
166     void setActive(bool);
167
168     bool validateInitializationSegment(const InitializationSegment&);
169
170     void reenqueueMediaForTime(TrackBuffer&, AtomicString trackID, const MediaTime&);
171     void provideMediaData(TrackBuffer&, AtomicString trackID);
172     void didDropSample();
173     void evictCodedFrames(size_t newDataSize);
174     size_t maximumBufferSize() const;
175
176     void monitorBufferingRate();
177
178     void removeTimerFired();
179     void removeCodedFrames(const MediaTime& start, const MediaTime& end);
180
181     size_t extraMemoryCost() const;
182     void reportExtraMemoryCost();
183
184     std::unique_ptr<PlatformTimeRanges> bufferedAccountingForEndOfStream() const;
185
186     // Internals
187     friend class Internals;
188     Vector<String> bufferedSamplesForTrackID(const AtomicString&);
189
190     Ref<SourceBufferPrivate> m_private;
191     MediaSource* m_source;
192     GenericEventQueue m_asyncEventQueue;
193     AtomicString m_mode;
194
195     Vector<unsigned char> m_pendingAppendData;
196     Timer m_appendBufferTimer;
197
198     RefPtr<VideoTrackList> m_videoTracks;
199     RefPtr<AudioTrackList> m_audioTracks;
200     RefPtr<TextTrackList> m_textTracks;
201
202     Vector<AtomicString> m_videoCodecs;
203     Vector<AtomicString> m_audioCodecs;
204     Vector<AtomicString> m_textCodecs;
205
206     MediaTime m_timestampOffset;
207     MediaTime m_groupStartTimestamp;
208     MediaTime m_groupEndTimestamp;
209
210     HashMap<AtomicString, TrackBuffer> m_trackBufferMap;
211     RefPtr<TimeRanges> m_buffered;
212
213     enum AppendStateType { WaitingForSegment, ParsingInitSegment, ParsingMediaSegment };
214     AppendStateType m_appendState;
215
216     double m_timeOfBufferingMonitor;
217     double m_bufferedSinceLastMonitor;
218     double m_averageBufferRate;
219
220     size_t m_reportedExtraMemoryCost;
221
222     MediaTime m_pendingRemoveStart;
223     MediaTime m_pendingRemoveEnd;
224     Timer m_removeTimer;
225
226     bool m_updating;
227     bool m_receivedFirstInitializationSegment;
228     bool m_active;
229     bool m_bufferFull;
230     bool m_shouldGenerateTimestamps;
231 };
232
233 } // namespace WebCore
234
235 #endif
236
237 #endif