1/*
2 * Copyright (C) 2013-2017 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 "MockBox.h"
28
29#if ENABLE(MEDIA_SOURCE)
30
31#include <JavaScriptCore/ArrayBuffer.h>
32#include <JavaScriptCore/DataView.h>
33#include <JavaScriptCore/HeapInlines.h>
34#include <JavaScriptCore/Int8Array.h>
35#include <JavaScriptCore/JSCJSValueInlines.h>
36#include <JavaScriptCore/TypedArrayInlines.h>
37#include <wtf/NeverDestroyed.h>
38#include <wtf/text/StringBuilder.h>
39
40namespace WebCore {
41
42MockBox::MockBox(ArrayBuffer* data)
43{
44 m_type = peekType(data);
45 m_length = peekLength(data);
46 ASSERT(m_length >= 8);
47}
48
49String MockBox::peekType(ArrayBuffer* data)
50{
51 StringBuilder builder;
52 auto array = JSC::Int8Array::create(data, 0, 4);
53 for (int i = 0; i < 4; ++i)
54 builder.append(array->item(i));
55 return builder.toString();
56}
57
58size_t MockBox::peekLength(ArrayBuffer* data)
59{
60 auto view = JSC::DataView::create(data, 0, data->byteLength());
61 return view->get<uint32_t>(4, true);
62}
63
64MockTrackBox::MockTrackBox(ArrayBuffer* data)
65 : MockBox(data)
66{
67 ASSERT(m_length == 17);
68
69 auto view = JSC::DataView::create(data, 0, data->byteLength());
70 m_trackID = view->get<int32_t>(8, true);
71
72 StringBuilder builder;
73 auto array = JSC::Int8Array::create(data, 12, 4);
74 for (int i = 0; i < 4; ++i)
75 builder.append(array->item(i));
76 m_codec = builder.toString();
77
78 m_kind = static_cast<TrackKind>(view->get<uint8_t>(16, true));
79}
80
81const String& MockTrackBox::type()
82{
83 static NeverDestroyed<String> trak(MAKE_STATIC_STRING_IMPL("trak"));
84 return trak;
85}
86
87MockInitializationBox::MockInitializationBox(ArrayBuffer* data)
88 : MockBox(data)
89{
90 ASSERT(m_length >= 13);
91
92 auto view = JSC::DataView::create(data, 0, data->byteLength());
93 int32_t timeValue = view->get<int32_t>(8, true);
94 int32_t timeScale = view->get<int32_t>(12, true);
95 m_duration = MediaTime(timeValue, timeScale);
96
97 size_t offset = 16;
98
99 while (offset < m_length) {
100 auto subBuffer = data->slice(offset);
101 if (MockBox::peekType(subBuffer.ptr()) != MockTrackBox::type())
102 break;
103
104 MockTrackBox trackBox(subBuffer.ptr());
105 offset += trackBox.length();
106 m_tracks.append(trackBox);
107 }
108}
109
110const String& MockInitializationBox::type()
111{
112 static NeverDestroyed<String> init(MAKE_STATIC_STRING_IMPL("init"));
113 return init;
114}
115
116MockSampleBox::MockSampleBox(ArrayBuffer* data)
117 : MockBox(data)
118{
119 ASSERT(m_length == 30);
120
121 auto view = JSC::DataView::create(data, 0, data->byteLength());
122 int32_t timeScale = view->get<int32_t>(8, true);
123
124 int32_t timeValue = view->get<int32_t>(12, true);
125 m_presentationTimestamp = MediaTime(timeValue, timeScale);
126
127 timeValue = view->get<int32_t>(16, true);
128 m_decodeTimestamp = MediaTime(timeValue, timeScale);
129
130 timeValue = view->get<int32_t>(20, true);
131 m_duration = MediaTime(timeValue, timeScale);
132
133 m_trackID = view->get<int32_t>(24, true);
134 m_flags = view->get<uint8_t>(28, true);
135 m_generation = view->get<uint8_t>(29, true);
136}
137
138const String& MockSampleBox::type()
139{
140 static NeverDestroyed<String> smpl(MAKE_STATIC_STRING_IMPL("smpl"));
141 return smpl;
142}
143
144}
145
146#endif
147