REGRESSION (206001): Scrubbed video on Youtube stops playing
[WebKit-https.git] / Source / WebCore / platform / mock / mediasource / MockSourceBufferPrivate.cpp
1 /*
2  * Copyright (C) 2013-2014 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 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 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 #include <wtf/PrintStream.h>
42
43 namespace WebCore {
44
45 class MockMediaSample final : public MediaSample {
46 public:
47     static Ref<MockMediaSample> create(const MockSampleBox& box) { return adoptRef(*new MockMediaSample(box)); }
48     virtual ~MockMediaSample() { }
49
50 private:
51     MockMediaSample(const MockSampleBox& box)
52         : m_box(box)
53         , m_id(String::format("%d", box.trackID()))
54     {
55     }
56
57     MediaTime presentationTime() const override { return m_box.presentationTimestamp(); }
58     MediaTime decodeTime() const override { return m_box.decodeTimestamp(); }
59     MediaTime duration() const override { return m_box.duration(); }
60     AtomicString trackID() const override { return m_id; }
61     void setTrackID(const String& id) override { m_id = id; }
62     size_t sizeInBytes() const override { return sizeof(m_box); }
63     SampleFlags flags() const override;
64     PlatformSample platformSample() override;
65     FloatSize presentationSize() const override { return FloatSize(); }
66     void dump(PrintStream&) const override;
67     void offsetTimestampsBy(const MediaTime& offset) override { m_box.offsetTimestampsBy(offset); }
68     void setTimestamps(const MediaTime& presentationTimestamp, const MediaTime& decodeTimestamp) override { m_box.setTimestamps(presentationTimestamp, decodeTimestamp); }
69     bool isDivisable() const override { return false; }
70     std::pair<RefPtr<MediaSample>, RefPtr<MediaSample>> divide(const MediaTime&) override { return {nullptr, nullptr}; }
71
72
73     unsigned generation() const { return m_box.generation(); }
74
75     MockSampleBox m_box;
76     AtomicString m_id;
77 };
78
79 MediaSample::SampleFlags MockMediaSample::flags() const
80 {
81     unsigned flags = None;
82     if (m_box.flags() & MockSampleBox::IsSync)
83         flags |= IsSync;
84     return SampleFlags(flags);
85 }
86
87 PlatformSample MockMediaSample::platformSample()
88 {
89     PlatformSample sample = { PlatformSample::MockSampleBoxType, { &m_box } };
90     return sample;
91 }
92
93 void MockMediaSample::dump(PrintStream& out) const
94 {
95     out.print("{PTS(", presentationTime(), "), DTS(", decodeTime(), "), duration(", duration(), "), flags(", (int)flags(), "), generation(", generation(), ")}");
96 }
97
98 class MockMediaDescription final : public MediaDescription {
99 public:
100     static RefPtr<MockMediaDescription> create(const MockTrackBox& box) { return adoptRef(new MockMediaDescription(box)); }
101     virtual ~MockMediaDescription() { }
102
103     AtomicString codec() const override { return m_box.codec(); }
104     bool isVideo() const override { return m_box.kind() == MockTrackBox::Video; }
105     bool isAudio() const override { return m_box.kind() == MockTrackBox::Audio; }
106     bool isText() const override { return m_box.kind() == MockTrackBox::Text; }
107
108 protected:
109     MockMediaDescription(const MockTrackBox& box) : m_box(box) { }
110     MockTrackBox m_box;
111 };
112
113 RefPtr<MockSourceBufferPrivate> MockSourceBufferPrivate::create(MockMediaSourcePrivate* parent)
114 {
115     return adoptRef(new MockSourceBufferPrivate(parent));
116 }
117
118 MockSourceBufferPrivate::MockSourceBufferPrivate(MockMediaSourcePrivate* parent)
119     : m_mediaSource(parent)
120     , m_client(0)
121 {
122 }
123
124 MockSourceBufferPrivate::~MockSourceBufferPrivate()
125 {
126 }
127
128 void MockSourceBufferPrivate::setClient(SourceBufferPrivateClient* client)
129 {
130     m_client = client;
131 }
132
133 void MockSourceBufferPrivate::append(const unsigned char* data, unsigned length)
134 {
135     m_inputBuffer.append(data, length);
136     SourceBufferPrivateClient::AppendResult result = SourceBufferPrivateClient::AppendSucceeded;
137
138     while (m_inputBuffer.size() && result == SourceBufferPrivateClient::AppendSucceeded) {
139         RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(m_inputBuffer.data(), m_inputBuffer.size());
140         size_t boxLength = MockBox::peekLength(buffer.get());
141         if (boxLength > buffer->byteLength())
142             break;
143
144         String type = MockBox::peekType(buffer.get());
145         if (type == MockInitializationBox::type()) {
146             MockInitializationBox initBox = MockInitializationBox(buffer.get());
147             didReceiveInitializationSegment(initBox);
148         } else if (type == MockSampleBox::type()) {
149             MockSampleBox sampleBox = MockSampleBox(buffer.get());
150             didReceiveSample(sampleBox);
151         } else
152             result = SourceBufferPrivateClient::ParsingFailed;
153
154         m_inputBuffer.remove(0, boxLength);
155     }
156
157     if (m_client)
158         m_client->sourceBufferPrivateAppendComplete(this, result);
159 }
160
161 void MockSourceBufferPrivate::didReceiveInitializationSegment(const MockInitializationBox& initBox)
162 {
163     if (!m_client)
164         return;
165
166     SourceBufferPrivateClient::InitializationSegment segment;
167     segment.duration = initBox.duration();
168
169     for (auto it = initBox.tracks().begin(); it != initBox.tracks().end(); ++it) {
170         const MockTrackBox& trackBox = *it;
171         if (trackBox.kind() == MockTrackBox::Video) {
172             SourceBufferPrivateClient::InitializationSegment::VideoTrackInformation info;
173             info.track = MockVideoTrackPrivate::create(trackBox);
174             info.description = MockMediaDescription::create(trackBox);
175             segment.videoTracks.append(info);
176         } else if (trackBox.kind() == MockTrackBox::Audio) {
177             SourceBufferPrivateClient::InitializationSegment::AudioTrackInformation info;
178             info.track = MockAudioTrackPrivate::create(trackBox);
179             info.description = MockMediaDescription::create(trackBox);
180             segment.audioTracks.append(info);
181         } else if (trackBox.kind() == MockTrackBox::Text) {
182             SourceBufferPrivateClient::InitializationSegment::TextTrackInformation info;
183             info.track = MockTextTrackPrivate::create(trackBox);
184             info.description = MockMediaDescription::create(trackBox);
185             segment.textTracks.append(info);
186         }
187     }
188
189     m_client->sourceBufferPrivateDidReceiveInitializationSegment(this, segment);
190 }
191
192
193 void MockSourceBufferPrivate::didReceiveSample(const MockSampleBox& sampleBox)
194 {
195     if (!m_client)
196         return;
197
198     m_client->sourceBufferPrivateDidReceiveSample(this, MockMediaSample::create(sampleBox));
199 }
200
201 void MockSourceBufferPrivate::abort()
202 {
203 }
204
205 void MockSourceBufferPrivate::removedFromMediaSource()
206 {
207     if (m_mediaSource)
208         m_mediaSource->removeSourceBuffer(this);
209 }
210
211 MediaPlayer::ReadyState MockSourceBufferPrivate::readyState() const
212 {
213     return m_mediaSource ? m_mediaSource->player()->readyState() : MediaPlayer::HaveNothing;
214 }
215
216 void MockSourceBufferPrivate::setReadyState(MediaPlayer::ReadyState readyState)
217 {
218     if (m_mediaSource)
219         m_mediaSource->player()->setReadyState(readyState);
220 }
221
222 void MockSourceBufferPrivate::setActive(bool isActive)
223 {
224     if (m_mediaSource)
225         m_mediaSource->sourceBufferPrivateDidChangeActiveState(this, isActive);
226 }
227
228 Vector<String> MockSourceBufferPrivate::enqueuedSamplesForTrackID(AtomicString)
229 {
230     return m_enqueuedSamples;
231 }
232
233 void MockSourceBufferPrivate::enqueueSample(PassRefPtr<MediaSample> prpSample, AtomicString)
234 {
235     RefPtr<MediaSample> sample = prpSample;
236     if (!m_mediaSource || !sample)
237         return;
238
239     PlatformSample platformSample = sample->platformSample();
240     if (platformSample.type != PlatformSample::MockSampleBoxType)
241         return;
242
243     MockSampleBox* box = platformSample.sample.mockSampleBox;
244     if (!box)
245         return;
246
247     m_mediaSource->incrementTotalVideoFrames();
248     if (box->isCorrupted())
249         m_mediaSource->incrementCorruptedFrames();
250     if (box->isDropped())
251         m_mediaSource->incrementDroppedFrames();
252     if (box->isDelayed())
253         m_mediaSource->incrementTotalFrameDelayBy(MediaTime(1, 1));
254
255     m_enqueuedSamples.append(toString(sample));
256 }
257
258 bool MockSourceBufferPrivate::hasVideo() const
259 {
260     if (!m_client)
261         return false;
262
263     return m_client->sourceBufferPrivateHasVideo(this);
264 }
265
266 bool MockSourceBufferPrivate::hasAudio() const
267 {
268     if (!m_client)
269         return false;
270
271     return m_client->sourceBufferPrivateHasAudio(this);
272 }
273
274
275 MediaTime MockSourceBufferPrivate::fastSeekTimeForMediaTime(const MediaTime& time, const MediaTime& negativeThreshold, const MediaTime& positiveThreshold)
276 {
277     if (m_client)
278         return m_client->sourceBufferPrivateFastSeekTimeForMediaTime(this, time, negativeThreshold, positiveThreshold);
279     return time;
280 }
281
282 void MockSourceBufferPrivate::seekToTime(const MediaTime& time)
283 {
284     if (m_client)
285         m_client->sourceBufferPrivateSeekToTime(this, time);
286 }
287
288 }
289
290 #endif
291