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 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#ifndef MockBox_h
27#define MockBox_h
28
29#if ENABLE(MEDIA_SOURCE)
30
31#include <wtf/MediaTime.h>
32#include <wtf/text/WTFString.h>
33
34namespace JSC {
35class ArrayBuffer;
36}
37
38namespace WebCore {
39
40// A MockBox represents a ISO-BMFF like data structure. Data in the
41// structure is little-endian. The layout of the data structure as follows:
42//
43// 4 bytes : 4CC : identifier
44// 4 bytes : unsigned : length
45//
46class MockBox {
47public:
48    static String peekType(JSC::ArrayBuffer*);
49    static size_t peekLength(JSC::ArrayBuffer*);
50
51    size_t length() const { return m_length; }
52    const String& type() const { return m_type; }
53
54protected:
55    MockBox(JSC::ArrayBuffer*);
56
57    size_t m_length;
58    String m_type;
59};
60
61// A MockTrackBox extends MockBox and expects the following
62// data structure:
63//
64// 4 bytes : 4CC : identifier = 'trak'
65// 4 bytes : unsigned : length = 17
66// 4 bytes : signed : track ID
67// 4 bytes : 4CC : codec
68// 1 byte  : unsigned : kind
69//
70class MockTrackBox final : public MockBox {
71public:
72    static const String& type();
73    MockTrackBox(JSC::ArrayBuffer*);
74
75    int32_t trackID() const { return m_trackID; }
76
77    const String& codec() const { return m_codec; }
78
79    enum TrackKind { Audio, Video, Text };
80    TrackKind kind() const { return m_kind; }
81
82protected:
83    uint8_t m_trackID;
84    String m_codec;
85    TrackKind m_kind;
86};
87
88// A MockInitializationBox extends MockBox and contains 0 or more
89// MockTrackBoxes. It expects the following data structure:
90//
91// 4 bytes : 4CC : identifier = 'init'
92// 4 bytes : unsigned : length = 16 + (13 * num tracks)
93// 4 bytes : signed : duration time value
94// 4 bytes : signed : duration time scale
95// N bytes : MockTrackBoxes : tracks
96//
97class MockInitializationBox final : public MockBox {
98public:
99    static const String& type();
100    MockInitializationBox(JSC::ArrayBuffer*);
101
102    MediaTime duration() const { return m_duration; }
103    const Vector<MockTrackBox>& tracks() const { return m_tracks; }
104
105protected:
106    MediaTime m_duration;
107    Vector<MockTrackBox> m_tracks;
108};
109
110// A MockSampleBox extends MockBox and expects the following data structure:
111//
112// 4 bytes : 4CC : identifier = 'smpl'
113// 4 bytes : unsigned : length = 29
114// 4 bytes : signed : time scale
115// 4 bytes : signed : presentation time value
116// 4 bytes : signed : decode time value
117// 4 bytes : signed : duration time value
118// 4 bytes : signed : track ID
119// 1 byte  : unsigned : flags
120// 1 byte  : unsigned : generation
121//
122class MockSampleBox final : public MockBox {
123public:
124    static const String& type();
125    MockSampleBox(JSC::ArrayBuffer*);
126
127    MediaTime presentationTimestamp() const { return m_presentationTimestamp; }
128    MediaTime decodeTimestamp() const { return m_decodeTimestamp; }
129    MediaTime duration() const { return m_duration; }
130    int32_t trackID() const { return m_trackID; }
131    uint8_t flags() const { return m_flags; }
132    uint8_t generation() const { return m_generation; }
133
134    enum {
135        IsSync = 1 << 0,
136        IsCorrupted = 1 << 1,
137        IsDropped = 1 << 2,
138        IsDelayed = 1 << 3,
139    };
140    bool isSync() const { return m_flags & IsSync; }
141    bool isCorrupted() const { return m_flags & IsCorrupted; }
142    bool isDropped() const { return m_flags & IsDropped; }
143    bool isDelayed() const { return m_flags & IsDelayed; }
144
145protected:
146    MediaTime m_presentationTimestamp;
147    MediaTime m_decodeTimestamp;
148    MediaTime m_duration;
149    int32_t m_trackID;
150    uint8_t m_flags;
151    uint8_t m_generation;
152};
153
154}
155
156#endif
157
158#endif
159