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