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