| 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 | |
| 34 | namespace JSC { |
| 35 | class ArrayBuffer; |
| 36 | } |
| 37 | |
| 38 | namespace 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 | // |
| 46 | class MockBox { |
| 47 | public: |
| 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 | |
| 54 | protected: |
| 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 | // |
| 70 | class MockTrackBox final : public MockBox { |
| 71 | public: |
| 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 | |
| 82 | protected: |
| 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 | // |
| 97 | class MockInitializationBox final : public MockBox { |
| 98 | public: |
| 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 | |
| 105 | protected: |
| 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 | // |
| 122 | class MockSampleBox final : public MockBox { |
| 123 | public: |
| 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 | void offsetTimestampsBy(const MediaTime& offset) |
| 134 | { |
| 135 | m_presentationTimestamp += offset; |
| 136 | m_decodeTimestamp += offset; |
| 137 | } |
| 138 | void setTimestamps(const MediaTime& presentationTimestamp, const MediaTime& decodeTimestamp) |
| 139 | { |
| 140 | m_presentationTimestamp = presentationTimestamp; |
| 141 | m_decodeTimestamp = decodeTimestamp; |
| 142 | } |
| 143 | |
| 144 | void clearFlag(uint8_t flag) { m_flags &= ~flag; } |
| 145 | void setFlag(uint8_t flag) { m_flags |= flag; } |
| 146 | |
| 147 | enum { |
| 148 | IsSync = 1 << 0, |
| 149 | IsCorrupted = 1 << 1, |
| 150 | IsDropped = 1 << 2, |
| 151 | IsDelayed = 1 << 3, |
| 152 | IsNonDisplaying = 1 << 4, |
| 153 | }; |
| 154 | bool isSync() const { return m_flags & IsSync; } |
| 155 | bool isCorrupted() const { return m_flags & IsCorrupted; } |
| 156 | bool isDropped() const { return m_flags & IsDropped; } |
| 157 | bool isDelayed() const { return m_flags & IsDelayed; } |
| 158 | bool isNonDisplaying() const { return m_flags & IsNonDisplaying; } |
| 159 | |
| 160 | protected: |
| 161 | MediaTime m_presentationTimestamp; |
| 162 | MediaTime m_decodeTimestamp; |
| 163 | MediaTime m_duration; |
| 164 | int32_t m_trackID; |
| 165 | uint8_t m_flags; |
| 166 | uint8_t m_generation; |
| 167 | }; |
| 168 | |
| 169 | } |
| 170 | |
| 171 | #endif |
| 172 | |
| 173 | #endif |
| 174 | |