[MSE] Add mock MediaSource classes for testing.
[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 { return None; }
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 PlatformSample MockMediaSample::platformSample()
69 {
70     PlatformSample sample = { PlatformSample::MockSampleBoxType, { &m_box } };
71     return sample;
72 }
73
74 class MockMediaDescription FINAL : public MediaDescription {
75 public:
76     static RefPtr<MockMediaDescription> create(const MockTrackBox& box) { return adoptRef(new MockMediaDescription(box)); }
77     virtual ~MockMediaDescription() { }
78
79     virtual AtomicString codec() const OVERRIDE { return m_box.codec(); }
80     virtual bool isVideo() const OVERRIDE { return m_box.kind() == MockTrackBox::Video; }
81     virtual bool isAudio() const OVERRIDE { return m_box.kind() == MockTrackBox::Audio; }
82     virtual bool isText() const OVERRIDE { return m_box.kind() == MockTrackBox::Text; }
83
84 protected:
85     MockMediaDescription(const MockTrackBox& box) : m_box(box) { }
86     MockTrackBox m_box;
87 };
88
89 RefPtr<MockSourceBufferPrivate> MockSourceBufferPrivate::create(MockMediaSourcePrivate* parent)
90 {
91     return adoptRef(new MockSourceBufferPrivate(parent));
92 }
93
94 MockSourceBufferPrivate::MockSourceBufferPrivate(MockMediaSourcePrivate* parent)
95     : m_parent(parent)
96     , m_client(0)
97 {
98 }
99
100 MockSourceBufferPrivate::~MockSourceBufferPrivate()
101 {
102 }
103
104 void MockSourceBufferPrivate::setClient(SourceBufferPrivateClient* client)
105 {
106     m_client = client;
107 }
108
109 void MockSourceBufferPrivate::append(const unsigned char* data, unsigned length)
110 {
111     m_inputBuffer.append(data, length);
112
113     while (m_inputBuffer.size()) {
114         RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(m_inputBuffer.data(), m_inputBuffer.size());
115         size_t boxLength = MockBox::peekLength(buffer.get());
116         if (boxLength > buffer->byteLength())
117             break;
118
119         String type = MockBox::peekType(buffer.get());
120         if (type == MockInitializationBox::type()) {
121             MockInitializationBox initBox = MockInitializationBox(buffer.get());
122             didReceiveInitializationSegment(initBox);
123         }
124
125         if (type == MockSampleBox::type()) {
126             MockSampleBox sampleBox = MockSampleBox(buffer.get());
127             didReceiveSample(sampleBox);
128         }
129
130         m_inputBuffer.remove(0, boxLength);
131     }
132 }
133
134 void MockSourceBufferPrivate::didReceiveInitializationSegment(const MockInitializationBox& initBox)
135 {
136     if (!m_client)
137         return;
138
139     SourceBufferPrivateClient::InitializationSegment segment;
140     segment.duration = initBox.duration();
141
142     for (auto it = initBox.tracks().begin(); it != initBox.tracks().end(); ++it) {
143         const MockTrackBox& trackBox = *it;
144         if (trackBox.kind() == MockTrackBox::Video) {
145             SourceBufferPrivateClient::InitializationSegment::VideoTrackInformation info;
146             info.track = MockVideoTrackPrivate::create(trackBox);
147             info.description = MockMediaDescription::create(trackBox);
148             segment.videoTracks.append(info);
149         } else if (trackBox.kind() == MockTrackBox::Audio) {
150             SourceBufferPrivateClient::InitializationSegment::AudioTrackInformation info;
151             info.track = MockAudioTrackPrivate::create(trackBox);
152             info.description = MockMediaDescription::create(trackBox);
153             segment.audioTracks.append(info);
154         } else if (trackBox.kind() == MockTrackBox::Text) {
155             SourceBufferPrivateClient::InitializationSegment::TextTrackInformation info;
156             info.track = MockTextTrackPrivate::create(trackBox);
157             info.description = MockMediaDescription::create(trackBox);
158             segment.textTracks.append(info);
159         }
160     }
161
162     m_client->sourceBufferPrivateDidReceiveInitializationSegment(this, segment);
163 }
164
165
166 void MockSourceBufferPrivate::didReceiveSample(const MockSampleBox& sampleBox)
167 {
168     if (!m_client)
169         return;
170
171     m_client->sourceBufferPrivateDidReceiveSample(this, MockMediaSample::create(sampleBox));
172 }
173
174 void MockSourceBufferPrivate::abort()
175 {
176 }
177
178 void MockSourceBufferPrivate::removedFromMediaSource()
179 {
180     m_parent->removeSourceBuffer(this);
181 }
182
183 MediaPlayer::ReadyState MockSourceBufferPrivate::readyState() const
184 {
185     return m_parent->player()->readyState();
186 }
187
188 void MockSourceBufferPrivate::setReadyState(MediaPlayer::ReadyState readyState)
189 {
190     m_parent->player()->setReadyState(readyState);
191 }
192
193 bool MockSourceBufferPrivate::hasVideo() const
194 {
195     if (!m_client)
196         return false;
197
198     return m_client->sourceBufferPrivateHasVideo(this);
199 }
200
201 bool MockSourceBufferPrivate::hasAudio() const
202 {
203     if (!m_client)
204         return false;
205
206     return m_client->sourceBufferPrivateHasAudio(this);
207 }
208
209 }
210
211 #endif
212