2010-09-13 01:45:50 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-11-14 11:46:40 -08:00
|
|
|
#if !defined(WebMBufferedParser_h_)
|
|
|
|
#define WebMBufferedParser_h_
|
2010-09-13 01:45:50 -07:00
|
|
|
|
2010-09-20 17:49:50 -07:00
|
|
|
#include "nsISupportsImpl.h"
|
2010-09-13 01:45:50 -07:00
|
|
|
#include "nsTArray.h"
|
2011-04-29 12:21:57 -07:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2010-09-13 01:45:50 -07:00
|
|
|
|
2013-03-02 11:14:44 -08:00
|
|
|
class TimeRanges;
|
2012-11-14 11:45:33 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
2010-09-20 17:49:50 -07:00
|
|
|
|
2010-09-13 01:45:50 -07:00
|
|
|
// Stores a stream byte offset and the scaled timecode of the block at
|
|
|
|
// that offset. The timecode must be scaled by the stream's timecode
|
|
|
|
// scale before use.
|
2012-11-14 11:46:40 -08:00
|
|
|
struct WebMTimeDataOffset
|
2010-09-13 01:45:50 -07:00
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
WebMTimeDataOffset(int64_t aOffset, uint64_t aTimecode)
|
2010-09-13 01:45:50 -07:00
|
|
|
: mOffset(aOffset), mTimecode(aTimecode)
|
|
|
|
{}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool operator==(int64_t aOffset) const {
|
2010-09-13 01:45:50 -07:00
|
|
|
return mOffset == aOffset;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool operator<(int64_t aOffset) const {
|
2010-09-13 01:45:50 -07:00
|
|
|
return mOffset < aOffset;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mOffset;
|
|
|
|
uint64_t mTimecode;
|
2010-09-13 01:45:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// A simple WebM parser that produces data offset to timecode pairs as it
|
|
|
|
// consumes blocks. A new parser is created for each distinct range of data
|
|
|
|
// received and begins parsing from the first WebM cluster within that
|
|
|
|
// range. Old parsers are destroyed when their range merges with a later
|
|
|
|
// parser or an already parsed range. The parser may start at any position
|
|
|
|
// within the stream.
|
2012-11-14 11:46:40 -08:00
|
|
|
struct WebMBufferedParser
|
2010-09-13 01:45:50 -07:00
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
WebMBufferedParser(int64_t aOffset)
|
2010-09-13 01:45:50 -07:00
|
|
|
: mStartOffset(aOffset), mCurrentOffset(aOffset), mState(CLUSTER_SYNC), mClusterIDPos(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Steps the parser through aLength bytes of data. Always consumes
|
2011-03-23 15:28:58 -07:00
|
|
|
// aLength bytes. Updates mCurrentOffset before returning. Acquires
|
2011-04-29 12:21:57 -07:00
|
|
|
// aReentrantMonitor before using aMapping.
|
2012-08-22 08:56:38 -07:00
|
|
|
void Append(const unsigned char* aBuffer, uint32_t aLength,
|
2012-11-14 11:46:40 -08:00
|
|
|
nsTArray<WebMTimeDataOffset>& aMapping,
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitor& aReentrantMonitor);
|
2010-09-13 01:45:50 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool operator==(int64_t aOffset) const {
|
2010-09-13 01:45:50 -07:00
|
|
|
return mCurrentOffset == aOffset;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool operator<(int64_t aOffset) const {
|
2010-09-13 01:45:50 -07:00
|
|
|
return mCurrentOffset < aOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The offset at which this parser started parsing. Used to merge
|
|
|
|
// adjacent parsers, in which case the later parser adopts the earlier
|
|
|
|
// parser's mStartOffset.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mStartOffset;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
|
|
|
// Current offset with the stream. Updated in chunks as Append() consumes
|
|
|
|
// data.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mCurrentOffset;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum State {
|
|
|
|
// Parser start state. Scans forward searching for stream sync by
|
|
|
|
// matching CLUSTER_ID with the curernt byte. The match state is stored
|
|
|
|
// in mClusterIDPos. Once this reaches sizeof(CLUSTER_ID), stream may
|
|
|
|
// have sync. The parser then advances to read the cluster size and
|
|
|
|
// timecode.
|
|
|
|
CLUSTER_SYNC,
|
|
|
|
|
|
|
|
/*
|
|
|
|
The the parser states below assume that CLUSTER_SYNC has found a valid
|
|
|
|
sync point within the data. If parsing fails in these states, the
|
|
|
|
parser returns to CLUSTER_SYNC to find a new sync point.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Read the first byte of a variable length integer. The first byte
|
|
|
|
// encodes both the variable integer's length and part of the value.
|
|
|
|
// The value read so far is stored in mVInt and the length is stored in
|
|
|
|
// mVIntLength. The number of bytes left to read is stored in
|
|
|
|
// mVIntLeft.
|
|
|
|
READ_VINT,
|
|
|
|
|
|
|
|
// Reads the remaining mVIntLeft bytes into mVInt.
|
|
|
|
READ_VINT_REST,
|
|
|
|
|
|
|
|
// Check that the next element is TIMECODE_ID. The cluster timecode is
|
|
|
|
// required to be the first element in a cluster. Advances to READ_VINT
|
|
|
|
// to read the timecode's length into mVInt.
|
|
|
|
TIMECODE_SYNC,
|
|
|
|
|
|
|
|
// mVInt holds the length of the variable length unsigned integer
|
|
|
|
// containing the cluster timecode. Read mVInt bytes into
|
|
|
|
// mClusterTimecode.
|
|
|
|
READ_CLUSTER_TIMECODE,
|
|
|
|
|
|
|
|
// Skips elements with a cluster until BLOCKGROUP_ID or SIMPLEBLOCK_ID
|
|
|
|
// is found. If BLOCKGROUP_ID is found, the parser returns to
|
|
|
|
// ANY_BLOCK_ID searching for a BLOCK_ID. Once a block or simpleblock
|
|
|
|
// is found, the current data offset is stored in mBlockOffset. If the
|
|
|
|
// current byte is the beginning of a four byte variant integer, it
|
|
|
|
// indicates the parser has reached a top-level element ID and the
|
|
|
|
// parser returns to CLUSTER_SYNC.
|
|
|
|
ANY_BLOCK_SYNC,
|
|
|
|
|
|
|
|
// Start reading a block. Blocks and simpleblocks are parsed the same
|
|
|
|
// way as the initial layouts are identical. mBlockSize is initialized
|
|
|
|
// from mVInt (holding the element size), and mBlockTimecode(Length) is
|
|
|
|
// initialized for parsing.
|
|
|
|
READ_BLOCK,
|
|
|
|
|
|
|
|
// Reads mBlockTimecodeLength bytes of data into mBlockTimecode. When
|
|
|
|
// mBlockTimecodeLength reaches 0, the timecode has been read. The sum
|
|
|
|
// of mClusterTimecode and mBlockTimecode is stored as a pair with
|
|
|
|
// mBlockOffset into the offset-to-time map.
|
|
|
|
READ_BLOCK_TIMECODE,
|
|
|
|
|
|
|
|
// Skip mSkipBytes of data before resuming parse at mNextState.
|
|
|
|
SKIP_DATA,
|
|
|
|
|
|
|
|
// Skip the content of an element. mVInt holds the element length.
|
|
|
|
SKIP_ELEMENT
|
|
|
|
};
|
|
|
|
|
|
|
|
// Current state machine action.
|
|
|
|
State mState;
|
|
|
|
|
|
|
|
// Next state machine action. SKIP_DATA and READ_VINT_REST advance to
|
|
|
|
// mNextState when the current action completes.
|
|
|
|
State mNextState;
|
|
|
|
|
|
|
|
// Match position within CLUSTER_ID. Used to find sync within arbitrary
|
|
|
|
// data.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mClusterIDPos;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
|
|
|
// Variable length integer read from data.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t mVInt;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
|
|
|
// Encoding length of mVInt. This is the total number of bytes used to
|
|
|
|
// encoding mVInt's value.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mVIntLength;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
|
|
|
// Number of bytes of mVInt left to read. mVInt is complete once this
|
|
|
|
// reaches 0.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mVIntLeft;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
|
|
|
// Size of the block currently being parsed. Any unused data within the
|
|
|
|
// block is skipped once the block timecode has been parsed.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t mBlockSize;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
|
|
|
// Cluster-level timecode.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t mClusterTimecode;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
|
|
|
// Start offset of the block currently being parsed. Used as the byte
|
|
|
|
// offset for the offset-to-time mapping once the block timecode has been
|
|
|
|
// parsed.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mBlockOffset;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
|
|
|
// Block-level timecode. This is summed with mClusterTimecode to produce
|
|
|
|
// an absolute timecode for the offset-to-time mapping.
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t mBlockTimecode;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
|
|
|
// Number of bytes of mBlockTimecode left to read.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mBlockTimecodeLength;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
|
|
|
// Count of bytes left to skip before resuming parse at mNextState.
|
|
|
|
// Mostly used to skip block payload data after reading a block timecode.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mSkipBytes;
|
2010-09-13 01:45:50 -07:00
|
|
|
};
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
class WebMBufferedState
|
2010-09-20 17:49:50 -07:00
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(WebMBufferedState)
|
2010-09-20 17:49:50 -07:00
|
|
|
|
|
|
|
public:
|
2012-11-14 11:46:40 -08:00
|
|
|
WebMBufferedState() : mReentrantMonitor("WebMBufferedState") {
|
|
|
|
MOZ_COUNT_CTOR(WebMBufferedState);
|
2010-09-20 17:49:50 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
~WebMBufferedState() {
|
|
|
|
MOZ_COUNT_DTOR(WebMBufferedState);
|
2010-09-20 17:49:50 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset);
|
|
|
|
bool CalculateBufferedForRange(int64_t aStartOffset, int64_t aEndOffset,
|
|
|
|
uint64_t* aStartTime, uint64_t* aEndTime);
|
2010-09-20 17:49:50 -07:00
|
|
|
|
|
|
|
private:
|
2011-03-23 15:28:58 -07:00
|
|
|
// Synchronizes access to the mTimeMapping array.
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitor mReentrantMonitor;
|
2011-03-23 15:28:58 -07:00
|
|
|
|
2010-09-20 17:49:50 -07:00
|
|
|
// Sorted (by offset) map of data offsets to timecodes. Populated
|
2012-11-14 11:46:40 -08:00
|
|
|
// on the main thread as data is received and parsed by WebMBufferedParsers.
|
|
|
|
nsTArray<WebMTimeDataOffset> mTimeMapping;
|
2010-09-20 17:49:50 -07:00
|
|
|
|
|
|
|
// Sorted (by offset) live parser instances. Main thread only.
|
2012-11-14 11:46:40 -08:00
|
|
|
nsTArray<WebMBufferedParser> mRangeParsers;
|
2010-09-20 17:49:50 -07:00
|
|
|
};
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2010-09-13 01:45:50 -07:00
|
|
|
#endif
|