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/. */
|
2010-09-13 01:45:50 -07:00
|
|
|
|
|
|
|
#include "nsAlgorithm.h"
|
2012-11-14 11:46:40 -08:00
|
|
|
#include "WebMBufferedParser.h"
|
2013-03-02 11:14:44 -08:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2011-03-23 15:28:58 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2011-03-23 15:28:58 -07:00
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
namespace mozilla {
|
2010-09-20 17:49:50 -07:00
|
|
|
|
2011-01-16 19:03:00 -08:00
|
|
|
static const double NS_PER_S = 1e9;
|
2010-09-13 01:45:50 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t
|
|
|
|
VIntLength(unsigned char aFirstByte, uint32_t* aMask)
|
2010-09-13 01:45:50 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = 1;
|
|
|
|
uint32_t mask = 1 << 7;
|
2010-09-13 01:45:50 -07:00
|
|
|
while (count < 8) {
|
|
|
|
if ((aFirstByte & mask) != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mask >>= 1;
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
if (aMask) {
|
|
|
|
*aMask = mask;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(count >= 1 && count <= 8, "Insane VInt length.");
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void WebMBufferedParser::Append(const unsigned char* aBuffer, uint32_t aLength,
|
|
|
|
nsTArray<WebMTimeDataOffset>& aMapping,
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitor& aReentrantMonitor)
|
2010-09-13 01:45:50 -07:00
|
|
|
{
|
|
|
|
static const unsigned char CLUSTER_ID[] = { 0x1f, 0x43, 0xb6, 0x75 };
|
|
|
|
static const unsigned char TIMECODE_ID = 0xe7;
|
|
|
|
static const unsigned char BLOCKGROUP_ID = 0xa0;
|
|
|
|
static const unsigned char BLOCK_ID = 0xa1;
|
|
|
|
static const unsigned char SIMPLEBLOCK_ID = 0xa3;
|
|
|
|
|
|
|
|
const unsigned char* p = aBuffer;
|
|
|
|
|
|
|
|
// Parse each byte in aBuffer one-by-one, producing timecodes and updating
|
|
|
|
// aMapping as we go. Parser pauses at end of stream (which may be at any
|
|
|
|
// point within the parse) and resumes parsing the next time Append is
|
|
|
|
// called with new data.
|
|
|
|
while (p < aBuffer + aLength) {
|
|
|
|
switch (mState) {
|
|
|
|
case CLUSTER_SYNC:
|
|
|
|
if (*p++ == CLUSTER_ID[mClusterIDPos]) {
|
|
|
|
mClusterIDPos += 1;
|
|
|
|
} else {
|
|
|
|
mClusterIDPos = 0;
|
|
|
|
}
|
|
|
|
// Cluster ID found, it's likely this is a valid sync point. If this
|
|
|
|
// is a spurious match, the later parse steps will encounter an error
|
|
|
|
// and return to CLUSTER_SYNC.
|
|
|
|
if (mClusterIDPos == sizeof(CLUSTER_ID)) {
|
|
|
|
mClusterIDPos = 0;
|
|
|
|
mState = READ_VINT;
|
|
|
|
mNextState = TIMECODE_SYNC;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case READ_VINT: {
|
|
|
|
unsigned char c = *p++;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mask;
|
2010-09-13 01:45:50 -07:00
|
|
|
mVIntLength = VIntLength(c, &mask);
|
|
|
|
mVIntLeft = mVIntLength - 1;
|
|
|
|
mVInt = c & ~mask;
|
|
|
|
mState = READ_VINT_REST;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case READ_VINT_REST:
|
|
|
|
if (mVIntLeft) {
|
|
|
|
mVInt <<= 8;
|
|
|
|
mVInt |= *p++;
|
|
|
|
mVIntLeft -= 1;
|
|
|
|
} else {
|
|
|
|
mState = mNextState;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIMECODE_SYNC:
|
|
|
|
if (*p++ != TIMECODE_ID) {
|
|
|
|
p -= 1;
|
|
|
|
mState = CLUSTER_SYNC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mClusterTimecode = 0;
|
|
|
|
mState = READ_VINT;
|
|
|
|
mNextState = READ_CLUSTER_TIMECODE;
|
|
|
|
break;
|
|
|
|
case READ_CLUSTER_TIMECODE:
|
|
|
|
if (mVInt) {
|
|
|
|
mClusterTimecode <<= 8;
|
|
|
|
mClusterTimecode |= *p++;
|
|
|
|
mVInt -= 1;
|
|
|
|
} else {
|
|
|
|
mState = ANY_BLOCK_SYNC;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ANY_BLOCK_SYNC: {
|
|
|
|
unsigned char c = *p++;
|
|
|
|
if (c == BLOCKGROUP_ID) {
|
|
|
|
mState = READ_VINT;
|
|
|
|
mNextState = ANY_BLOCK_SYNC;
|
|
|
|
} else if (c == SIMPLEBLOCK_ID || c == BLOCK_ID) {
|
|
|
|
mBlockOffset = mCurrentOffset + (p - aBuffer) - 1;
|
|
|
|
mState = READ_VINT;
|
|
|
|
mNextState = READ_BLOCK;
|
|
|
|
} else {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length = VIntLength(c, nullptr);
|
2010-09-13 01:45:50 -07:00
|
|
|
if (length == 4) {
|
|
|
|
p -= 1;
|
|
|
|
mState = CLUSTER_SYNC;
|
|
|
|
} else {
|
|
|
|
mState = READ_VINT;
|
|
|
|
mNextState = SKIP_ELEMENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case READ_BLOCK:
|
|
|
|
mBlockSize = mVInt;
|
|
|
|
mBlockTimecode = 0;
|
|
|
|
mBlockTimecodeLength = 2;
|
|
|
|
mState = READ_VINT;
|
|
|
|
mNextState = READ_BLOCK_TIMECODE;
|
|
|
|
break;
|
|
|
|
case READ_BLOCK_TIMECODE:
|
|
|
|
if (mBlockTimecodeLength) {
|
|
|
|
mBlockTimecode <<= 8;
|
|
|
|
mBlockTimecode |= *p++;
|
|
|
|
mBlockTimecodeLength -= 1;
|
|
|
|
} else {
|
|
|
|
// It's possible we've parsed this data before, so avoid inserting
|
2012-11-14 11:46:40 -08:00
|
|
|
// duplicate WebMTimeDataOffset entries.
|
2011-03-23 15:28:58 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(aReentrantMonitor);
|
2013-02-13 07:11:53 -08:00
|
|
|
uint32_t idx = aMapping.IndexOfFirstElementGt(mBlockOffset);
|
|
|
|
if (idx == 0 || !(aMapping[idx-1] == mBlockOffset)) {
|
2012-11-14 11:46:40 -08:00
|
|
|
WebMTimeDataOffset entry(mBlockOffset, mClusterTimecode + mBlockTimecode);
|
2011-03-23 15:28:58 -07:00
|
|
|
aMapping.InsertElementAt(idx, entry);
|
|
|
|
}
|
2010-09-13 01:45:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip rest of block header and the block's payload.
|
|
|
|
mBlockSize -= mVIntLength;
|
|
|
|
mBlockSize -= 2;
|
2012-08-22 08:56:38 -07:00
|
|
|
mSkipBytes = uint32_t(mBlockSize);
|
2010-09-13 01:45:50 -07:00
|
|
|
mState = SKIP_DATA;
|
|
|
|
mNextState = ANY_BLOCK_SYNC;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SKIP_DATA:
|
|
|
|
if (mSkipBytes) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t left = aLength - (p - aBuffer);
|
2013-01-15 04:22:03 -08:00
|
|
|
left = std::min(left, mSkipBytes);
|
2010-09-13 01:45:50 -07:00
|
|
|
p += left;
|
|
|
|
mSkipBytes -= left;
|
|
|
|
} else {
|
|
|
|
mState = mNextState;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SKIP_ELEMENT:
|
2012-08-22 08:56:38 -07:00
|
|
|
mSkipBytes = uint32_t(mVInt);
|
2010-09-13 01:45:50 -07:00
|
|
|
mState = SKIP_DATA;
|
|
|
|
mNextState = ANY_BLOCK_SYNC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(p == aBuffer + aLength, "Must have parsed to end of data.");
|
|
|
|
mCurrentOffset += aLength;
|
|
|
|
}
|
2010-09-20 17:49:50 -07:00
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
bool WebMBufferedState::CalculateBufferedForRange(int64_t aStartOffset, int64_t aEndOffset,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t* aStartTime, uint64_t* aEndTime)
|
2010-09-20 17:49:50 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2011-03-23 15:28:58 -07:00
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
// Find the first WebMTimeDataOffset at or after aStartOffset.
|
2013-02-13 07:11:53 -08:00
|
|
|
uint32_t start = mTimeMapping.IndexOfFirstElementGt(aStartOffset-1);
|
2010-09-20 17:49:50 -07:00
|
|
|
if (start == mTimeMapping.Length()) {
|
2010-09-23 18:57:30 -07:00
|
|
|
return false;
|
2010-09-20 17:49:50 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
// Find the first WebMTimeDataOffset at or before aEndOffset.
|
2013-02-13 07:11:53 -08:00
|
|
|
uint32_t end = mTimeMapping.IndexOfFirstElementGt(aEndOffset-1);
|
|
|
|
if (end > 0) {
|
2010-09-20 17:49:50 -07:00
|
|
|
end -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Range is empty.
|
|
|
|
if (end <= start) {
|
2010-09-23 18:57:30 -07:00
|
|
|
return false;
|
2010-09-20 17:49:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mTimeMapping[start].mOffset >= aStartOffset &&
|
|
|
|
mTimeMapping[end].mOffset <= aEndOffset,
|
|
|
|
"Computed time range must lie within data range.");
|
|
|
|
if (start > 0) {
|
|
|
|
NS_ASSERTION(mTimeMapping[start - 1].mOffset <= aStartOffset,
|
2012-11-14 11:46:40 -08:00
|
|
|
"Must have found least WebMTimeDataOffset for start");
|
2010-09-20 17:49:50 -07:00
|
|
|
}
|
|
|
|
if (end < mTimeMapping.Length() - 1) {
|
|
|
|
NS_ASSERTION(mTimeMapping[end + 1].mOffset >= aEndOffset,
|
2012-11-14 11:46:40 -08:00
|
|
|
"Must have found greatest WebMTimeDataOffset for end");
|
2010-09-20 17:49:50 -07:00
|
|
|
}
|
|
|
|
|
2010-10-06 15:58:36 -07:00
|
|
|
// The timestamp of the first media sample, in ns. We must subtract this
|
|
|
|
// from the ranges' start and end timestamps, so that those timestamps are
|
|
|
|
// normalized in the range [0,duration].
|
|
|
|
|
2010-09-23 18:57:30 -07:00
|
|
|
*aStartTime = mTimeMapping[start].mTimecode;
|
|
|
|
*aEndTime = mTimeMapping[end].mTimecode;
|
|
|
|
return true;
|
2010-09-20 17:49:50 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void WebMBufferedState::NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset)
|
2010-09-20 17:49:50 -07:00
|
|
|
{
|
2011-03-23 15:28:58 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2013-02-13 07:11:53 -08:00
|
|
|
uint32_t idx = mRangeParsers.IndexOfFirstElementGt(aOffset - 1);
|
|
|
|
if (idx == 0 || !(mRangeParsers[idx-1] == aOffset)) {
|
2010-09-20 17:49:50 -07:00
|
|
|
// If the incoming data overlaps an already parsed range, adjust the
|
|
|
|
// buffer so that we only reparse the new data. It's also possible to
|
|
|
|
// have an overlap where the end of the incoming data is within an
|
|
|
|
// already parsed range, but we don't bother handling that other than by
|
|
|
|
// avoiding storing duplicate timecodes when the parser runs.
|
|
|
|
if (idx != mRangeParsers.Length() && mRangeParsers[idx].mStartOffset <= aOffset) {
|
|
|
|
// Complete overlap, skip parsing.
|
|
|
|
if (aOffset + aLength <= mRangeParsers[idx].mCurrentOffset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Partial overlap, adjust the buffer to parse only the new data.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t adjust = mRangeParsers[idx].mCurrentOffset - aOffset;
|
2010-09-20 17:49:50 -07:00
|
|
|
NS_ASSERTION(adjust >= 0, "Overlap detection bug.");
|
|
|
|
aBuffer += adjust;
|
2012-08-22 08:56:38 -07:00
|
|
|
aLength -= uint32_t(adjust);
|
2010-09-20 17:49:50 -07:00
|
|
|
} else {
|
2012-11-14 11:46:40 -08:00
|
|
|
mRangeParsers.InsertElementAt(idx, WebMBufferedParser(aOffset));
|
2010-09-20 17:49:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-23 15:28:58 -07:00
|
|
|
mRangeParsers[idx].Append(reinterpret_cast<const unsigned char*>(aBuffer),
|
|
|
|
aLength,
|
|
|
|
mTimeMapping,
|
2011-04-29 12:21:57 -07:00
|
|
|
mReentrantMonitor);
|
2010-09-20 17:49:50 -07:00
|
|
|
|
|
|
|
// Merge parsers with overlapping regions and clean up the remnants.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i = 0;
|
2010-09-20 17:49:50 -07:00
|
|
|
while (i + 1 < mRangeParsers.Length()) {
|
|
|
|
if (mRangeParsers[i].mCurrentOffset >= mRangeParsers[i + 1].mStartOffset) {
|
|
|
|
mRangeParsers[i + 1].mStartOffset = mRangeParsers[i].mStartOffset;
|
|
|
|
mRangeParsers.RemoveElementAt(i);
|
|
|
|
} else {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-14 11:45:33 -08:00
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|