content/media/webm/WebMBufferedParser.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6 #if !defined(WebMBufferedParser_h_)
michael@0 7 #define WebMBufferedParser_h_
michael@0 8
michael@0 9 #include "nsISupportsImpl.h"
michael@0 10 #include "nsTArray.h"
michael@0 11 #include "mozilla/ReentrantMonitor.h"
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14
michael@0 15 namespace dom {
michael@0 16 class TimeRanges;
michael@0 17 }
michael@0 18
michael@0 19 // Stores a stream byte offset and the scaled timecode of the block at
michael@0 20 // that offset. The timecode must be scaled by the stream's timecode
michael@0 21 // scale before use.
michael@0 22 struct WebMTimeDataOffset
michael@0 23 {
michael@0 24 WebMTimeDataOffset(int64_t aOffset, uint64_t aTimecode)
michael@0 25 : mOffset(aOffset), mTimecode(aTimecode)
michael@0 26 {}
michael@0 27
michael@0 28 bool operator==(int64_t aOffset) const {
michael@0 29 return mOffset == aOffset;
michael@0 30 }
michael@0 31
michael@0 32 bool operator<(int64_t aOffset) const {
michael@0 33 return mOffset < aOffset;
michael@0 34 }
michael@0 35
michael@0 36 int64_t mOffset;
michael@0 37 uint64_t mTimecode;
michael@0 38 };
michael@0 39
michael@0 40 // A simple WebM parser that produces data offset to timecode pairs as it
michael@0 41 // consumes blocks. A new parser is created for each distinct range of data
michael@0 42 // received and begins parsing from the first WebM cluster within that
michael@0 43 // range. Old parsers are destroyed when their range merges with a later
michael@0 44 // parser or an already parsed range. The parser may start at any position
michael@0 45 // within the stream.
michael@0 46 struct WebMBufferedParser
michael@0 47 {
michael@0 48 WebMBufferedParser(int64_t aOffset)
michael@0 49 : mStartOffset(aOffset), mCurrentOffset(aOffset), mState(CLUSTER_SYNC), mClusterIDPos(0)
michael@0 50 {}
michael@0 51
michael@0 52 // Steps the parser through aLength bytes of data. Always consumes
michael@0 53 // aLength bytes. Updates mCurrentOffset before returning. Acquires
michael@0 54 // aReentrantMonitor before using aMapping.
michael@0 55 void Append(const unsigned char* aBuffer, uint32_t aLength,
michael@0 56 nsTArray<WebMTimeDataOffset>& aMapping,
michael@0 57 ReentrantMonitor& aReentrantMonitor);
michael@0 58
michael@0 59 bool operator==(int64_t aOffset) const {
michael@0 60 return mCurrentOffset == aOffset;
michael@0 61 }
michael@0 62
michael@0 63 bool operator<(int64_t aOffset) const {
michael@0 64 return mCurrentOffset < aOffset;
michael@0 65 }
michael@0 66
michael@0 67 // The offset at which this parser started parsing. Used to merge
michael@0 68 // adjacent parsers, in which case the later parser adopts the earlier
michael@0 69 // parser's mStartOffset.
michael@0 70 int64_t mStartOffset;
michael@0 71
michael@0 72 // Current offset with the stream. Updated in chunks as Append() consumes
michael@0 73 // data.
michael@0 74 int64_t mCurrentOffset;
michael@0 75
michael@0 76 private:
michael@0 77 enum State {
michael@0 78 // Parser start state. Scans forward searching for stream sync by
michael@0 79 // matching CLUSTER_ID with the curernt byte. The match state is stored
michael@0 80 // in mClusterIDPos. Once this reaches sizeof(CLUSTER_ID), stream may
michael@0 81 // have sync. The parser then advances to read the cluster size and
michael@0 82 // timecode.
michael@0 83 CLUSTER_SYNC,
michael@0 84
michael@0 85 /*
michael@0 86 The the parser states below assume that CLUSTER_SYNC has found a valid
michael@0 87 sync point within the data. If parsing fails in these states, the
michael@0 88 parser returns to CLUSTER_SYNC to find a new sync point.
michael@0 89 */
michael@0 90
michael@0 91 // Read the first byte of a variable length integer. The first byte
michael@0 92 // encodes both the variable integer's length and part of the value.
michael@0 93 // The value read so far is stored in mVInt and the length is stored in
michael@0 94 // mVIntLength. The number of bytes left to read is stored in
michael@0 95 // mVIntLeft.
michael@0 96 READ_VINT,
michael@0 97
michael@0 98 // Reads the remaining mVIntLeft bytes into mVInt.
michael@0 99 READ_VINT_REST,
michael@0 100
michael@0 101 // Check that the next element is TIMECODE_ID. The cluster timecode is
michael@0 102 // required to be the first element in a cluster. Advances to READ_VINT
michael@0 103 // to read the timecode's length into mVInt.
michael@0 104 TIMECODE_SYNC,
michael@0 105
michael@0 106 // mVInt holds the length of the variable length unsigned integer
michael@0 107 // containing the cluster timecode. Read mVInt bytes into
michael@0 108 // mClusterTimecode.
michael@0 109 READ_CLUSTER_TIMECODE,
michael@0 110
michael@0 111 // Skips elements with a cluster until BLOCKGROUP_ID or SIMPLEBLOCK_ID
michael@0 112 // is found. If BLOCKGROUP_ID is found, the parser returns to
michael@0 113 // ANY_BLOCK_ID searching for a BLOCK_ID. Once a block or simpleblock
michael@0 114 // is found, the current data offset is stored in mBlockOffset. If the
michael@0 115 // current byte is the beginning of a four byte variant integer, it
michael@0 116 // indicates the parser has reached a top-level element ID and the
michael@0 117 // parser returns to CLUSTER_SYNC.
michael@0 118 ANY_BLOCK_SYNC,
michael@0 119
michael@0 120 // Start reading a block. Blocks and simpleblocks are parsed the same
michael@0 121 // way as the initial layouts are identical. mBlockSize is initialized
michael@0 122 // from mVInt (holding the element size), and mBlockTimecode(Length) is
michael@0 123 // initialized for parsing.
michael@0 124 READ_BLOCK,
michael@0 125
michael@0 126 // Reads mBlockTimecodeLength bytes of data into mBlockTimecode. When
michael@0 127 // mBlockTimecodeLength reaches 0, the timecode has been read. The sum
michael@0 128 // of mClusterTimecode and mBlockTimecode is stored as a pair with
michael@0 129 // mBlockOffset into the offset-to-time map.
michael@0 130 READ_BLOCK_TIMECODE,
michael@0 131
michael@0 132 // Skip mSkipBytes of data before resuming parse at mNextState.
michael@0 133 SKIP_DATA,
michael@0 134
michael@0 135 // Skip the content of an element. mVInt holds the element length.
michael@0 136 SKIP_ELEMENT
michael@0 137 };
michael@0 138
michael@0 139 // Current state machine action.
michael@0 140 State mState;
michael@0 141
michael@0 142 // Next state machine action. SKIP_DATA and READ_VINT_REST advance to
michael@0 143 // mNextState when the current action completes.
michael@0 144 State mNextState;
michael@0 145
michael@0 146 // Match position within CLUSTER_ID. Used to find sync within arbitrary
michael@0 147 // data.
michael@0 148 uint32_t mClusterIDPos;
michael@0 149
michael@0 150 // Variable length integer read from data.
michael@0 151 uint64_t mVInt;
michael@0 152
michael@0 153 // Encoding length of mVInt. This is the total number of bytes used to
michael@0 154 // encoding mVInt's value.
michael@0 155 uint32_t mVIntLength;
michael@0 156
michael@0 157 // Number of bytes of mVInt left to read. mVInt is complete once this
michael@0 158 // reaches 0.
michael@0 159 uint32_t mVIntLeft;
michael@0 160
michael@0 161 // Size of the block currently being parsed. Any unused data within the
michael@0 162 // block is skipped once the block timecode has been parsed.
michael@0 163 uint64_t mBlockSize;
michael@0 164
michael@0 165 // Cluster-level timecode.
michael@0 166 uint64_t mClusterTimecode;
michael@0 167
michael@0 168 // Start offset of the block currently being parsed. Used as the byte
michael@0 169 // offset for the offset-to-time mapping once the block timecode has been
michael@0 170 // parsed.
michael@0 171 int64_t mBlockOffset;
michael@0 172
michael@0 173 // Block-level timecode. This is summed with mClusterTimecode to produce
michael@0 174 // an absolute timecode for the offset-to-time mapping.
michael@0 175 int16_t mBlockTimecode;
michael@0 176
michael@0 177 // Number of bytes of mBlockTimecode left to read.
michael@0 178 uint32_t mBlockTimecodeLength;
michael@0 179
michael@0 180 // Count of bytes left to skip before resuming parse at mNextState.
michael@0 181 // Mostly used to skip block payload data after reading a block timecode.
michael@0 182 uint32_t mSkipBytes;
michael@0 183 };
michael@0 184
michael@0 185 class WebMBufferedState MOZ_FINAL
michael@0 186 {
michael@0 187 NS_INLINE_DECL_REFCOUNTING(WebMBufferedState)
michael@0 188
michael@0 189 public:
michael@0 190 WebMBufferedState() : mReentrantMonitor("WebMBufferedState") {
michael@0 191 MOZ_COUNT_CTOR(WebMBufferedState);
michael@0 192 }
michael@0 193
michael@0 194 void NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset);
michael@0 195 bool CalculateBufferedForRange(int64_t aStartOffset, int64_t aEndOffset,
michael@0 196 uint64_t* aStartTime, uint64_t* aEndTime);
michael@0 197 bool GetOffsetForTime(uint64_t aTime, int64_t* aOffset);
michael@0 198
michael@0 199 private:
michael@0 200 // Private destructor, to discourage deletion outside of Release():
michael@0 201 ~WebMBufferedState() {
michael@0 202 MOZ_COUNT_DTOR(WebMBufferedState);
michael@0 203 }
michael@0 204
michael@0 205 // Synchronizes access to the mTimeMapping array.
michael@0 206 ReentrantMonitor mReentrantMonitor;
michael@0 207
michael@0 208 // Sorted (by offset) map of data offsets to timecodes. Populated
michael@0 209 // on the main thread as data is received and parsed by WebMBufferedParsers.
michael@0 210 nsTArray<WebMTimeDataOffset> mTimeMapping;
michael@0 211
michael@0 212 // Sorted (by offset) live parser instances. Main thread only.
michael@0 213 nsTArray<WebMBufferedParser> mRangeParsers;
michael@0 214 };
michael@0 215
michael@0 216 } // namespace mozilla
michael@0 217
michael@0 218 #endif

mercurial