Change all uses of FINAL to final now that all our compilers support it
[WebKit-https.git] / Source / WebCore / platform / mock / mediasource / MockSourceBufferPrivate.cpp
1 /*
2  * Copyright (C) 2013 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "MockSourceBufferPrivate.h"
28
29 #if ENABLE(MEDIA_SOURCE)
30
31 #include "MediaDescription.h"
32 #include "MediaPlayer.h"
33 #include "MediaSample.h"
34 #include "MockBox.h"
35 #include "MockMediaPlayerMediaSource.h"
36 #include "MockMediaSourcePrivate.h"
37 #include "MockTracks.h"
38 #include "SourceBufferPrivateClient.h"
39 #include <map>
40 #include <runtime/ArrayBuffer.h>
41
42 namespace WebCore {
43
44 class MockMediaSample final : public MediaSample {
45 public:
46     static RefPtr<MockMediaSample> create(const MockSampleBox& box) { return adoptRef(new MockMediaSample(box)); }
47     virtual ~MockMediaSample() { }
48
49     virtual MediaTime presentationTime() const override { return m_box.presentationTimestamp(); }
50     virtual MediaTime decodeTime() const override { return m_box.decodeTimestamp(); }
51     virtual MediaTime duration() const override { return m_box.duration(); }
52     virtual AtomicString trackID() const override { return m_id; }
53
54     virtual SampleFlags flags() const override;
55     virtual PlatformSample platformSample() override;
56
57 protected:
58     MockMediaSample(const MockSampleBox& box)
59         : m_box(box)
60         , m_id(String::format("%d", box.trackID()))
61     {
62     }
63
64     MockSampleBox m_box;
65     AtomicString m_id;
66 };
67
68 MediaSample::SampleFlags MockMediaSample::flags() const
69 {
70     unsigned flags = None;
71     if (m_box.flags() & MockSampleBox::IsSync)
72         flags |= IsSync;
73     return SampleFlags(flags);
74 }
75
76 PlatformSample MockMediaSample::platformSample()
77 {
78     PlatformSample sample = { PlatformSample::MockSampleBoxType, { &m_box } };
79     return sample;
80 }
81
82 class MockMediaDescription final : public MediaDescription {
83 public:
84     static RefPtr<MockMediaDescription> create(const MockTrackBox& box) { return adoptRef(new MockMediaDescription(box)); }
85     virtual ~MockMediaDescription() { }
86
87     virtual AtomicString codec() const override { return m_box.codec(); }
88     virtual bool isVideo() const override { return m_box.kind() == MockTrackBox::Video; }
89     virtual bool isAudio() const override { return m_box.kind() == MockTrackBox::Audio; }
90     virtual bool isText() const override { return m_box.kind() == MockTrackBox::Text; }
91
92 protected:
93     MockMediaDescription(const MockTrackBox& box) : m_box(box) { }
94     MockTrackBox m_box;
95 };
96
97 RefPtr<MockSourceBufferPrivate> MockSourceBufferPrivate::create(MockMediaSourcePrivate* parent)
98 {
99     return adoptRef(new MockSourceBufferPrivate(parent));
100 }
101
102 MockSourceBufferPrivate::MockSourceBufferPrivate(MockMediaSourcePrivate* parent)
103     : m_mediaSource(parent)
104     , m_client(0)
105 {
106 }
107
108 MockSourceBufferPrivate::~MockSourceBufferPrivate()
109 {
110 }
111
112 void MockSourceBufferPrivate::setClient(SourceBufferPrivateClient* client)
113 {
114     m_client = client;
115 }
116
117 SourceBufferPrivate::AppendResult MockSourceBufferPrivate::append(const unsigned char* data, unsigned length)
118 {
119     m_inputBuffer.append(data, length);
120     AppendResult result = AppendSucceeded;
121
122     while (m_inputBuffer.size() && result == AppendSucceeded) {
123         RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(m_inputBuffer.data(), m_inputBuffer.size());
124         size_t boxLength = MockBox::peekLength(buffer.get());
125         if (boxLength > buffer->byteLength())
126             break;
127
128         String type = MockBox::peekType(buffer.get());
129         if (type == MockInitializationBox::type()) {
130             MockInitializationBox initBox = MockInitializationBox(buffer.get());
131             didReceiveInitializationSegment(initBox);
132         } else if (type == MockSampleBox::type()) {
133             MockSampleBox sampleBox = MockSampleBox(buffer.get());
134             didReceiveSample(sampleBox);
135         } else
136             result = ParsingFailed;
137
138         m_inputBuffer.remove(0, boxLength);
139     }
140
141     return result;
142 }
143
144 void MockSourceBufferPrivate::didReceiveInitializationSegment(const MockInitializationBox& initBox)
145 {
146     if (!m_client)
147         return;
148
149     SourceBufferPrivateClient::InitializationSegment segment;
150     segment.duration = initBox.duration();
151
152     for (auto it = initBox.tracks().begin(); it != initBox.tracks().end(); ++it) {
153         const MockTrackBox& trackBox = *it;
154         if (trackBox.kind() == MockTrackBox::Video) {
155             SourceBufferPrivateClient::InitializationSegment::VideoTrackInformation info;
156             info.track = MockVideoTrackPrivate::create(trackBox);
157             info.description = MockMediaDescription::create(trackBox);
158             segment.videoTracks.append(info);
159         } else if (trackBox.kind() == MockTrackBox::Audio) {
160             SourceBufferPrivateClient::InitializationSegment::AudioTrackInformation info;
161             info.track = MockAudioTrackPrivate::create(trackBox);
162             info.description = MockMediaDescription::create(trackBox);
163             segment.audioTracks.append(info);
164         } else if (trackBox.kind() == MockTrackBox::Text) {
165             SourceBufferPrivateClient::InitializationSegment::TextTrackInformation info;
166             info.track = MockTextTrackPrivate::create(trackBox);
167             info.description = MockMediaDescription::create(trackBox);
168             segment.textTracks.append(info);
169         }
170     }
171
172     m_client->sourceBufferPrivateDidReceiveInitializationSegment(this, segment);
173 }
174
175
176 void MockSourceBufferPrivate::didReceiveSample(const MockSampleBox& sampleBox)
177 {
178     if (!m_client)
179         return;
180
181     m_client->sourceBufferPrivateDidReceiveSample(this, MockMediaSample::create(sampleBox));
182 }
183
184 void MockSourceBufferPrivate::abort()
185 {
186 }
187
188 void MockSourceBufferPrivate::removedFromMediaSource()
189 {
190     if (m_mediaSource)
191         m_mediaSource->removeSourceBuffer(this);
192 }
193
194 MediaPlayer::ReadyState MockSourceBufferPrivate::readyState() const
195 {
196     return m_mediaSource ? m_mediaSource->player()->readyState() : MediaPlayer::HaveNothing;
197 }
198
199 void MockSourceBufferPrivate::setReadyState(MediaPlayer::ReadyState readyState)
200 {
201     if (m_mediaSource)
202         m_mediaSource->player()->setReadyState(readyState);
203 }
204
205 void MockSourceBufferPrivate::evictCodedFrames()
206 {
207     // No-op.
208 }
209
210 bool MockSourceBufferPrivate::isFull()
211 {
212     return false;
213 }
214
215 void MockSourceBufferPrivate::setActive(bool isActive)
216 {
217     if (m_mediaSource)
218         m_mediaSource->sourceBufferPrivateDidChangeActiveState(this, isActive);
219 }
220
221 void MockSourceBufferPrivate::enqueueSample(PassRefPtr<MediaSample> sample, AtomicString)
222 {
223     if (!m_mediaSource || !sample)
224         return;
225
226     PlatformSample platformSample = sample->platformSample();
227     if (platformSample.type != PlatformSample::MockSampleBoxType)
228         return;
229
230     MockSampleBox* box = platformSample.sample.mockSampleBox;
231     if (!box)
232         return;
233
234     m_mediaSource->incrementTotalVideoFrames();
235     if (box->isCorrupted())
236         m_mediaSource->incrementCorruptedFrames();
237     if (box->isDropped())
238         m_mediaSource->incrementDroppedFrames();
239     if (box->isDelayed())
240         m_mediaSource->incrementTotalFrameDelayBy(1);
241 }
242
243 bool MockSourceBufferPrivate::hasVideo() const
244 {
245     if (!m_client)
246         return false;
247
248     return m_client->sourceBufferPrivateHasVideo(this);
249 }
250
251 bool MockSourceBufferPrivate::hasAudio() const
252 {
253     if (!m_client)
254         return false;
255
256     return m_client->sourceBufferPrivateHasAudio(this);
257 }
258
259
260 MediaTime MockSourceBufferPrivate::fastSeekTimeForMediaTime(const MediaTime& time, const MediaTime& negativeThreshold, const MediaTime& positiveThreshold)
261 {
262     if (m_client)
263         return m_client->sourceBufferPrivateFastSeekTimeForMediaTime(this, time, negativeThreshold, positiveThreshold);
264     return time;
265 }
266
267 void MockSourceBufferPrivate::seekToTime(const MediaTime& time)
268 {
269     if (m_client)
270         m_client->sourceBufferPrivateSeekToTime(this, time);
271 }
272
273 }
274
275 #endif
276