2010-05-05 19:31:02 -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-12-14 15:58:45 -08:00
|
|
|
|
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
#include "nsError.h"
|
2012-11-14 11:46:40 -08:00
|
|
|
#include "MediaDecoderStateMachine.h"
|
|
|
|
#include "MediaDecoder.h"
|
|
|
|
#include "OggReader.h"
|
2010-05-05 19:31:02 -07:00
|
|
|
#include "VideoUtils.h"
|
|
|
|
#include "theora/theoradec.h"
|
2012-05-01 17:29:34 -07:00
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
#include "opus/opus.h"
|
2012-08-09 15:53:23 -07:00
|
|
|
extern "C" {
|
|
|
|
#include "opus/opus_multistream.h"
|
|
|
|
}
|
2012-05-01 17:29:34 -07:00
|
|
|
#endif
|
2010-08-25 01:43:00 -07:00
|
|
|
#include "nsTimeRanges.h"
|
2011-03-23 15:28:57 -07:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2012-10-25 03:09:41 -07:00
|
|
|
#include "VorbisUtils.h"
|
2012-11-30 05:17:54 -08:00
|
|
|
#include "MediaMetadataManager.h"
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
namespace mozilla {
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2012-09-02 15:56:29 -07:00
|
|
|
// On B2G estimate the buffered ranges rather than calculating them explicitly.
|
|
|
|
// This prevents us doing I/O on the main thread, which is prohibited in B2G.
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
#define OGG_ESTIMATE_BUFFERED 1
|
|
|
|
#endif
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
// Un-comment to enable logging of seek bisections.
|
|
|
|
//#define SEEK_LOGGING
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-11-14 11:46:40 -08:00
|
|
|
extern PRLogModuleInfo* gMediaDecoderLog;
|
|
|
|
#define LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg)
|
2010-05-05 19:31:02 -07:00
|
|
|
#ifdef SEEK_LOGGING
|
2012-11-14 11:46:40 -08:00
|
|
|
#define SEEK_LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg)
|
2010-05-05 19:31:02 -07:00
|
|
|
#else
|
|
|
|
#define SEEK_LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define LOG(type, msg)
|
|
|
|
#define SEEK_LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// The number of microseconds of "fuzz" we use in a bisection search over
|
2010-08-19 15:50:37 -07:00
|
|
|
// HTTP. When we're seeking with fuzz, we'll stop the search if a bisection
|
2011-04-13 15:12:23 -07:00
|
|
|
// lands between the seek target and SEEK_FUZZ_USECS microseconds before the
|
2010-08-19 15:50:37 -07:00
|
|
|
// seek target. This is becaue it's usually quicker to just keep downloading
|
|
|
|
// from an exisiting connection than to do another bisection inside that
|
|
|
|
// small range, which would open a new HTTP connetion.
|
2012-08-22 08:56:38 -07:00
|
|
|
static const uint32_t SEEK_FUZZ_USECS = 500000;
|
2010-08-19 15:50:37 -07:00
|
|
|
|
2012-05-31 11:13:17 -07:00
|
|
|
// The number of microseconds of "pre-roll" we use for Opus streams.
|
|
|
|
// The specification recommends 80 ms.
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int64_t SEEK_OPUS_PREROLL = 80 * USECS_PER_MS;
|
2012-05-31 11:13:17 -07:00
|
|
|
|
2010-08-19 15:50:37 -07:00
|
|
|
enum PageSyncResult {
|
|
|
|
PAGE_SYNC_ERROR = 1,
|
|
|
|
PAGE_SYNC_END_OF_RANGE= 2,
|
|
|
|
PAGE_SYNC_OK = 3
|
|
|
|
};
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
// Reads a page from the media resource.
|
2010-08-19 15:50:37 -07:00
|
|
|
static PageSyncResult
|
2012-02-14 20:35:01 -08:00
|
|
|
PageSync(MediaResource* aResource,
|
2010-08-19 15:50:37 -07:00
|
|
|
ogg_sync_state* aState,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aCachedDataOnly,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aEndOffset,
|
2010-08-19 15:50:37 -07:00
|
|
|
ogg_page* aPage,
|
|
|
|
int& aSkippedBytes);
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
// Chunk size to read when reading Ogg files. Average Ogg page length
|
|
|
|
// is about 4300 bytes, so we read the file in chunks larger than that.
|
|
|
|
static const int PAGE_STEP = 8192;
|
|
|
|
|
2012-11-19 07:11:21 -08:00
|
|
|
OggReader::OggReader(AbstractMediaDecoder* aDecoder)
|
2012-11-14 11:46:40 -08:00
|
|
|
: MediaDecoderReader(aDecoder),
|
2012-11-30 05:17:54 -08:00
|
|
|
mMonitor("OggReader"),
|
2012-07-30 07:20:58 -07:00
|
|
|
mTheoraState(nullptr),
|
|
|
|
mVorbisState(nullptr),
|
|
|
|
mOpusState(nullptr),
|
2012-11-14 11:46:40 -08:00
|
|
|
mOpusEnabled(MediaDecoder::IsOpusEnabled()),
|
2012-07-30 07:20:58 -07:00
|
|
|
mSkeletonState(nullptr),
|
2010-12-16 17:39:01 -08:00
|
|
|
mVorbisSerial(0),
|
2012-05-31 11:13:11 -07:00
|
|
|
mOpusSerial(0),
|
2010-12-16 17:39:01 -08:00
|
|
|
mTheoraSerial(0),
|
2012-05-31 11:13:11 -07:00
|
|
|
mOpusPreSkip(0),
|
2012-11-30 05:17:54 -08:00
|
|
|
mPageOffset(0),
|
|
|
|
mIsChained(false),
|
|
|
|
mDecodedAudioFrames(0)
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
MOZ_COUNT_CTOR(OggReader);
|
2011-06-23 15:08:54 -07:00
|
|
|
memset(&mTheoraInfo, 0, sizeof(mTheoraInfo));
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
OggReader::~OggReader()
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
|
|
|
ogg_sync_clear(&mOggState);
|
2012-11-14 11:46:40 -08:00
|
|
|
MOZ_COUNT_DTOR(OggReader);
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult OggReader::Init(MediaDecoderReader* aCloneDonor) {
|
2012-05-18 10:30:49 -07:00
|
|
|
mCodecStates.Init();
|
2010-05-05 19:31:02 -07:00
|
|
|
int ret = ogg_sync_init(&mOggState);
|
|
|
|
NS_ENSURE_TRUE(ret == 0, NS_ERROR_FAILURE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult OggReader::ResetDecode()
|
2012-05-30 21:03:14 -07:00
|
|
|
{
|
|
|
|
return ResetDecode(false);
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult OggReader::ResetDecode(bool start)
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
2011-07-11 20:39:28 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2010-05-05 19:31:02 -07:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
if (NS_FAILED(MediaDecoderReader::ResetDecode())) {
|
2010-05-05 19:31:02 -07:00
|
|
|
res = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:28 -07:00
|
|
|
// Discard any previously buffered packets/pages.
|
|
|
|
ogg_sync_reset(&mOggState);
|
|
|
|
if (mVorbisState && NS_FAILED(mVorbisState->Reset())) {
|
|
|
|
res = NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-05-31 11:13:17 -07:00
|
|
|
if (mOpusState && NS_FAILED(mOpusState->Reset(start))) {
|
|
|
|
res = NS_ERROR_FAILURE;
|
2012-05-01 17:29:34 -07:00
|
|
|
}
|
2011-07-11 20:39:28 -07:00
|
|
|
if (mTheoraState && NS_FAILED(mTheoraState->Reset())) {
|
|
|
|
res = NS_ERROR_FAILURE;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
bool OggReader::ReadHeaders(OggCodecState* aState)
|
2011-05-07 23:24:09 -07:00
|
|
|
{
|
|
|
|
while (!aState->DoneReadingHeaders()) {
|
|
|
|
ogg_packet* packet = NextOggPacket(aState);
|
2012-05-31 11:13:17 -07:00
|
|
|
// DecodeHeader is responsible for releasing packet.
|
|
|
|
if (!packet || !aState->DecodeHeader(packet)) {
|
2011-05-07 23:24:09 -07:00
|
|
|
aState->Deactivate();
|
2012-05-31 11:13:17 -07:00
|
|
|
return false;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
}
|
2011-05-07 23:24:09 -07:00
|
|
|
return aState->Init();
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void OggReader::BuildSerialList(nsTArray<uint32_t>& aTracks)
|
2012-05-31 11:13:11 -07:00
|
|
|
{
|
|
|
|
if (HasVideo()) {
|
|
|
|
aTracks.AppendElement(mTheoraState->mSerial);
|
|
|
|
}
|
|
|
|
if (HasAudio()) {
|
|
|
|
if (mVorbisState) {
|
|
|
|
aTracks.AppendElement(mVorbisState->mSerial);
|
|
|
|
} else if(mOpusState) {
|
|
|
|
aTracks.AppendElement(mOpusState->mSerial);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 11:40:07 -08:00
|
|
|
nsresult OggReader::ReadMetadata(VideoInfo* aInfo,
|
2012-11-08 16:40:08 -08:00
|
|
|
MetadataTags** aTags)
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
2011-07-11 20:39:23 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
// We read packets until all bitstreams have read all their header packets.
|
|
|
|
// We record the offset of the first non-header page so that we know
|
|
|
|
// what page to seek to when seeking to the media start.
|
|
|
|
|
2012-07-30 17:14:29 -07:00
|
|
|
NS_ASSERTION(aTags, "Called with null MetadataTags**.");
|
|
|
|
*aTags = nullptr;
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
ogg_page page;
|
2012-11-14 11:46:40 -08:00
|
|
|
nsAutoTArray<OggCodecState*,4> bitstreams;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool readAllBOS = false;
|
2011-05-07 23:24:09 -07:00
|
|
|
while (!readAllBOS) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t pageOffset = ReadOggPage(&page);
|
2010-05-05 19:31:02 -07:00
|
|
|
if (pageOffset == -1) {
|
|
|
|
// Some kind of error...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int serial = ogg_page_serialno(&page);
|
2012-11-14 11:46:40 -08:00
|
|
|
OggCodecState* codecState = 0;
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2011-08-04 13:44:24 -07:00
|
|
|
if (!ogg_page_bos(&page)) {
|
|
|
|
// We've encountered a non Beginning Of Stream page. No more BOS pages
|
|
|
|
// can follow in this Ogg segment, so there will be no other bitstreams
|
|
|
|
// in the Ogg (unless it's invalid).
|
2011-09-29 16:34:37 -07:00
|
|
|
readAllBOS = true;
|
2012-07-30 07:20:58 -07:00
|
|
|
} else if (!mCodecStates.Get(serial, nullptr)) {
|
2011-08-04 13:44:24 -07:00
|
|
|
// We've not encountered a stream with this serial number before. Create
|
2012-11-14 11:46:40 -08:00
|
|
|
// an OggCodecState to demux it, and map that to the OggCodecState
|
2011-08-04 13:44:24 -07:00
|
|
|
// in mCodecStates.
|
2012-11-14 11:46:40 -08:00
|
|
|
codecState = OggCodecState::Create(&page);
|
2012-05-18 10:30:49 -07:00
|
|
|
mCodecStates.Put(serial, codecState);
|
2010-05-05 19:31:02 -07:00
|
|
|
bitstreams.AppendElement(codecState);
|
2010-12-16 17:39:01 -08:00
|
|
|
mKnownStreams.AppendElement(serial);
|
2010-05-05 19:31:02 -07:00
|
|
|
if (codecState &&
|
2012-11-14 11:46:40 -08:00
|
|
|
codecState->GetType() == OggCodecState::TYPE_VORBIS &&
|
2010-05-05 19:31:02 -07:00
|
|
|
!mVorbisState)
|
|
|
|
{
|
|
|
|
// First Vorbis bitstream, we'll play this one. Subsequent Vorbis
|
|
|
|
// bitstreams will be ignored.
|
2012-11-14 11:46:40 -08:00
|
|
|
mVorbisState = static_cast<VorbisState*>(codecState);
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
if (codecState &&
|
2012-11-14 11:46:40 -08:00
|
|
|
codecState->GetType() == OggCodecState::TYPE_THEORA &&
|
2010-05-05 19:31:02 -07:00
|
|
|
!mTheoraState)
|
|
|
|
{
|
|
|
|
// First Theora bitstream, we'll play this one. Subsequent Theora
|
|
|
|
// bitstreams will be ignored.
|
2012-11-14 11:46:40 -08:00
|
|
|
mTheoraState = static_cast<TheoraState*>(codecState);
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
2012-05-01 17:29:34 -07:00
|
|
|
if (codecState &&
|
2012-11-14 11:46:40 -08:00
|
|
|
codecState->GetType() == OggCodecState::TYPE_OPUS &&
|
2012-05-01 17:29:34 -07:00
|
|
|
!mOpusState)
|
|
|
|
{
|
|
|
|
if (mOpusEnabled) {
|
2012-11-14 11:46:40 -08:00
|
|
|
mOpusState = static_cast<OpusState*>(codecState);
|
2012-05-01 17:29:34 -07:00
|
|
|
} else {
|
|
|
|
NS_WARNING("Opus decoding disabled."
|
|
|
|
" See media.opus.enabled in about:config");
|
|
|
|
}
|
|
|
|
}
|
2010-08-19 15:50:37 -07:00
|
|
|
if (codecState &&
|
2012-11-14 11:46:40 -08:00
|
|
|
codecState->GetType() == OggCodecState::TYPE_SKELETON &&
|
2010-08-19 15:50:37 -07:00
|
|
|
!mSkeletonState)
|
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
mSkeletonState = static_cast<SkeletonState*>(codecState);
|
2010-08-19 15:50:37 -07:00
|
|
|
}
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mCodecStates.Get(serial, &codecState);
|
|
|
|
NS_ENSURE_TRUE(codecState, NS_ERROR_FAILURE);
|
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
if (NS_FAILED(codecState->PageIn(&page))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
}
|
2011-05-07 23:24:09 -07:00
|
|
|
|
|
|
|
// We've read all BOS pages, so we know the streams contained in the media.
|
|
|
|
// Now process all available header packets in the active Theora, Vorbis and
|
|
|
|
// Skeleton streams.
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
// Deactivate any non-primary bitstreams.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < bitstreams.Length(); i++) {
|
2012-11-14 11:46:40 -08:00
|
|
|
OggCodecState* s = bitstreams[i];
|
2012-05-01 17:29:34 -07:00
|
|
|
if (s != mVorbisState && s != mOpusState &&
|
|
|
|
s != mTheoraState && s != mSkeletonState) {
|
2010-05-05 19:31:02 -07:00
|
|
|
s->Deactivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
if (mTheoraState && ReadHeaders(mTheoraState)) {
|
2011-06-23 15:08:54 -07:00
|
|
|
nsIntRect picture = nsIntRect(mTheoraState->mInfo.pic_x,
|
|
|
|
mTheoraState->mInfo.pic_y,
|
|
|
|
mTheoraState->mInfo.pic_width,
|
|
|
|
mTheoraState->mInfo.pic_height);
|
|
|
|
|
|
|
|
nsIntSize displaySize = nsIntSize(mTheoraState->mInfo.pic_width,
|
|
|
|
mTheoraState->mInfo.pic_height);
|
|
|
|
|
|
|
|
// Apply the aspect ratio to produce the intrinsic display size we report
|
|
|
|
// to the element.
|
|
|
|
ScaleDisplayByAspectRatio(displaySize, mTheoraState->mPixelAspectRatio);
|
|
|
|
|
|
|
|
nsIntSize frameSize(mTheoraState->mInfo.frame_width,
|
|
|
|
mTheoraState->mInfo.frame_height);
|
2012-11-28 11:40:07 -08:00
|
|
|
if (VideoInfo::ValidateVideoRegion(frameSize, picture, displaySize)) {
|
2011-06-23 15:08:54 -07:00
|
|
|
// Video track's frame sizes will not overflow. Activate the video track.
|
2011-09-29 16:34:37 -07:00
|
|
|
mInfo.mHasVideo = true;
|
2011-06-23 15:08:54 -07:00
|
|
|
mInfo.mDisplay = displaySize;
|
|
|
|
mPicture = picture;
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
VideoFrameContainer* container = mDecoder->GetVideoFrameContainer();
|
|
|
|
if (container) {
|
|
|
|
container->SetCurrentFrame(gfxIntSize(displaySize.width, displaySize.height),
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr,
|
2012-02-14 20:35:01 -08:00
|
|
|
TimeStamp::Now());
|
|
|
|
}
|
2011-06-23 15:08:54 -07:00
|
|
|
|
|
|
|
// Copy Theora info data for time computations on other threads.
|
|
|
|
memcpy(&mTheoraInfo, &mTheoraState->mInfo, sizeof(mTheoraInfo));
|
|
|
|
mTheoraSerial = mTheoraState->mSerial;
|
|
|
|
}
|
2010-08-19 15:50:37 -07:00
|
|
|
}
|
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
if (mVorbisState && ReadHeaders(mVorbisState)) {
|
2011-09-29 16:34:37 -07:00
|
|
|
mInfo.mHasAudio = true;
|
2011-05-07 23:24:09 -07:00
|
|
|
mInfo.mAudioRate = mVorbisState->mInfo.rate;
|
|
|
|
mInfo.mAudioChannels = mVorbisState->mInfo.channels;
|
|
|
|
// Copy Vorbis info data for time computations on other threads.
|
2010-12-16 17:39:01 -08:00
|
|
|
memcpy(&mVorbisInfo, &mVorbisState->mInfo, sizeof(mVorbisInfo));
|
|
|
|
mVorbisInfo.codec_setup = NULL;
|
|
|
|
mVorbisSerial = mVorbisState->mSerial;
|
2012-09-17 15:42:36 -07:00
|
|
|
*aTags = mVorbisState->GetTags();
|
2011-05-07 23:24:09 -07:00
|
|
|
} else {
|
|
|
|
memset(&mVorbisInfo, 0, sizeof(mVorbisInfo));
|
2010-12-16 17:39:01 -08:00
|
|
|
}
|
2012-05-01 17:29:34 -07:00
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
if (mOpusState && ReadHeaders(mOpusState)) {
|
|
|
|
mInfo.mHasAudio = true;
|
|
|
|
mInfo.mAudioRate = mOpusState->mRate;
|
2012-08-09 15:53:23 -07:00
|
|
|
mInfo.mAudioChannels = mOpusState->mChannels > 2 ? 2 : mOpusState->mChannels;
|
2012-05-31 11:13:11 -07:00
|
|
|
mOpusSerial = mOpusState->mSerial;
|
|
|
|
mOpusPreSkip = mOpusState->mPreSkip;
|
2012-09-17 15:42:36 -07:00
|
|
|
|
|
|
|
*aTags = mOpusState->GetTags();
|
2012-05-01 17:29:34 -07:00
|
|
|
}
|
|
|
|
#endif
|
2011-05-07 23:24:09 -07:00
|
|
|
if (mSkeletonState) {
|
|
|
|
if (!HasAudio() && !HasVideo()) {
|
|
|
|
// We have a skeleton track, but no audio or video, may as well disable
|
|
|
|
// the skeleton, we can't do anything useful with this media.
|
|
|
|
mSkeletonState->Deactivate();
|
|
|
|
} else if (ReadHeaders(mSkeletonState) && mSkeletonState->HasIndex()) {
|
|
|
|
// Extract the duration info out of the index, so we don't need to seek to
|
2012-02-14 20:35:01 -08:00
|
|
|
// the end of resource to get it.
|
2012-08-22 08:56:38 -07:00
|
|
|
nsAutoTArray<uint32_t, 2> tracks;
|
2012-05-31 11:13:11 -07:00
|
|
|
BuildSerialList(tracks);
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t duration = 0;
|
2011-05-07 23:24:09 -07:00
|
|
|
if (NS_SUCCEEDED(mSkeletonState->GetDuration(tracks, duration))) {
|
2011-07-11 20:39:28 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2012-11-19 07:11:21 -08:00
|
|
|
mDecoder->SetMediaDuration(duration);
|
2011-05-07 23:24:09 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("Got duration from Skeleton index %lld", duration));
|
|
|
|
}
|
|
|
|
}
|
2010-12-16 17:39:01 -08:00
|
|
|
}
|
|
|
|
|
2012-06-02 06:29:44 -07:00
|
|
|
if (HasAudio() || HasVideo()) {
|
2011-07-11 20:39:28 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2011-05-08 14:10:28 -07:00
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
2012-11-19 07:11:21 -08:00
|
|
|
if (mDecoder->GetMediaDuration() == -1 &&
|
|
|
|
!mDecoder->IsShutdown() &&
|
2012-02-14 20:35:01 -08:00
|
|
|
resource->GetLength() >= 0 &&
|
2012-11-19 07:11:21 -08:00
|
|
|
mDecoder->IsMediaSeekable())
|
2011-05-08 14:10:28 -07:00
|
|
|
{
|
|
|
|
// We didn't get a duration from the index or a Content-Duration header.
|
|
|
|
// Seek to the end of file to find the end time.
|
2012-08-17 10:01:08 -07:00
|
|
|
mDecoder->GetResource()->StartSeekingForMetadata();
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t length = resource->GetLength();
|
2012-08-17 10:01:08 -07:00
|
|
|
|
2011-05-08 14:10:28 -07:00
|
|
|
NS_ASSERTION(length > 0, "Must have a content length to get end time");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t endTime = 0;
|
2011-05-08 14:10:28 -07:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
|
|
|
endTime = RangeEndTime(length);
|
|
|
|
}
|
|
|
|
if (endTime != -1) {
|
2012-11-19 07:11:21 -08:00
|
|
|
mDecoder->SetMediaEndTime(endTime);
|
2011-05-08 14:10:28 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("Got Ogg duration from seeking to end %lld", endTime));
|
|
|
|
}
|
2012-08-17 10:01:08 -07:00
|
|
|
mDecoder->GetResource()->EndSeekingForMetadata();
|
2011-05-08 14:10:28 -07:00
|
|
|
}
|
2012-06-02 06:29:44 -07:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_FAILURE;
|
2011-05-08 14:10:28 -07:00
|
|
|
}
|
2011-03-23 20:53:03 -07:00
|
|
|
*aInfo = mInfo;
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult OggReader::DecodeVorbis(ogg_packet* aPacket) {
|
2011-05-07 23:24:09 -07:00
|
|
|
NS_ASSERTION(aPacket->granulepos != -1, "Must know vorbis granulepos!");
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
if (vorbis_synthesis(&mVorbisState->mBlock, aPacket) != 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (vorbis_synthesis_blockin(&mVorbisState->mDsp,
|
|
|
|
&mVorbisState->mBlock) != 0)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-10-18 19:55:45 -07:00
|
|
|
VorbisPCMValue** pcm = 0;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t frames = 0;
|
|
|
|
uint32_t channels = mVorbisState->mInfo.channels;
|
2011-09-26 20:31:18 -07:00
|
|
|
ogg_int64_t endFrame = aPacket->granulepos;
|
|
|
|
while ((frames = vorbis_synthesis_pcmout(&mVorbisState->mDsp, &pcm)) > 0) {
|
|
|
|
mVorbisState->ValidateVorbisPacketSamples(aPacket, frames);
|
|
|
|
nsAutoArrayPtr<AudioDataValue> buffer(new AudioDataValue[frames * channels]);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < channels; ++j) {
|
2010-10-18 19:55:45 -07:00
|
|
|
VorbisPCMValue* channel = pcm[j];
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < uint32_t(frames); ++i) {
|
2010-10-18 19:55:45 -07:00
|
|
|
buffer[i*channels + j] = MOZ_CONVERT_VORBIS_SAMPLE(channel[i]);
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t duration = mVorbisState->Time((int64_t)frames);
|
|
|
|
int64_t startTime = mVorbisState->Time(endFrame - frames);
|
2011-08-15 22:19:51 -07:00
|
|
|
mAudioQueue.Push(new AudioData(mPageOffset,
|
2011-07-28 18:54:21 -07:00
|
|
|
startTime,
|
|
|
|
duration,
|
2011-09-26 20:31:18 -07:00
|
|
|
frames,
|
2011-07-28 18:54:21 -07:00
|
|
|
buffer.forget(),
|
|
|
|
channels));
|
2012-11-30 05:17:54 -08:00
|
|
|
|
|
|
|
mDecodedAudioFrames += frames;
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
endFrame -= frames;
|
|
|
|
if (vorbis_synthesis_read(&mVorbisState->mDsp, frames) != 0) {
|
2010-05-05 19:31:02 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-05-01 17:29:34 -07:00
|
|
|
#ifdef MOZ_OPUS
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult OggReader::DecodeOpus(ogg_packet* aPacket) {
|
2012-05-01 17:29:34 -07:00
|
|
|
NS_ASSERTION(aPacket->granulepos != -1, "Must know opus granulepos!");
|
|
|
|
|
2012-12-11 11:25:10 -08:00
|
|
|
// Maximum value is 63*2880, so there's no chance of overflow.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t frames_number = opus_packet_get_nb_frames(aPacket->packet,
|
2012-08-09 15:53:23 -07:00
|
|
|
aPacket->bytes);
|
2012-12-11 11:25:10 -08:00
|
|
|
if (frames_number <= 0)
|
|
|
|
return NS_ERROR_FAILURE; // Invalid packet header.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t samples = opus_packet_get_samples_per_frame(aPacket->packet,
|
2012-08-09 15:53:23 -07:00
|
|
|
(opus_int32) mOpusState->mRate);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t frames = frames_number*samples;
|
2012-08-09 15:53:23 -07:00
|
|
|
|
2012-12-11 11:25:10 -08:00
|
|
|
// A valid Opus packet must be between 2.5 and 120 ms long.
|
|
|
|
if (frames < 120 || frames > 5760)
|
2012-05-01 17:29:34 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t channels = mOpusState->mChannels;
|
2012-05-01 17:29:34 -07:00
|
|
|
nsAutoArrayPtr<AudioDataValue> buffer(new AudioDataValue[frames * channels]);
|
|
|
|
|
|
|
|
// Decode to the appropriate sample type.
|
|
|
|
#ifdef MOZ_SAMPLE_TYPE_FLOAT32
|
2012-08-09 15:53:23 -07:00
|
|
|
int ret = opus_multistream_decode_float(mOpusState->mDecoder,
|
|
|
|
aPacket->packet, aPacket->bytes,
|
|
|
|
buffer, frames, false);
|
2012-05-01 17:29:34 -07:00
|
|
|
#else
|
2012-08-09 15:53:23 -07:00
|
|
|
int ret = opus_multistream_decode(mOpusState->mDecoder,
|
|
|
|
aPacket->packet, aPacket->bytes,
|
|
|
|
buffer, frames, false);
|
2012-05-01 17:29:34 -07:00
|
|
|
#endif
|
|
|
|
if (ret < 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
NS_ASSERTION(ret == frames, "Opus decoded too few audio samples");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t endFrame = aPacket->granulepos;
|
|
|
|
int64_t startFrame;
|
2012-05-31 11:13:17 -07:00
|
|
|
// If this is the last packet, perform end trimming.
|
|
|
|
if (aPacket->e_o_s && mOpusState->mPrevPacketGranulepos != -1) {
|
|
|
|
startFrame = mOpusState->mPrevPacketGranulepos;
|
2012-08-22 08:56:38 -07:00
|
|
|
frames = static_cast<int32_t>(NS_MAX(static_cast<int64_t>(0),
|
2012-05-31 11:13:17 -07:00
|
|
|
NS_MIN(endFrame - startFrame,
|
2012-08-22 08:56:38 -07:00
|
|
|
static_cast<int64_t>(frames))));
|
2012-05-31 11:13:17 -07:00
|
|
|
} else {
|
|
|
|
startFrame = endFrame - frames;
|
|
|
|
}
|
2012-05-01 17:29:34 -07:00
|
|
|
|
2012-05-30 21:03:14 -07:00
|
|
|
// Trim the initial frames while the decoder is settling.
|
|
|
|
if (mOpusState->mSkip > 0) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t skipFrames = NS_MIN(mOpusState->mSkip, frames);
|
2012-05-30 21:03:14 -07:00
|
|
|
if (skipFrames == frames) {
|
|
|
|
// discard the whole packet
|
|
|
|
mOpusState->mSkip -= frames;
|
|
|
|
LOG(PR_LOG_DEBUG, ("Opus decoder skipping %d frames"
|
|
|
|
" (whole packet)", frames));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t keepFrames = frames - skipFrames;
|
2012-05-30 21:03:14 -07:00
|
|
|
int samples = keepFrames * channels;
|
|
|
|
nsAutoArrayPtr<AudioDataValue> trimBuffer(new AudioDataValue[samples]);
|
|
|
|
for (int i = 0; i < samples; i++)
|
|
|
|
trimBuffer[i] = buffer[skipFrames*channels + i];
|
|
|
|
|
2012-05-31 11:13:17 -07:00
|
|
|
startFrame = endFrame - keepFrames;
|
2012-05-30 21:03:14 -07:00
|
|
|
frames = keepFrames;
|
|
|
|
buffer = trimBuffer;
|
|
|
|
|
|
|
|
mOpusState->mSkip -= skipFrames;
|
|
|
|
LOG(PR_LOG_DEBUG, ("Opus decoder skipping %d frames", skipFrames));
|
2012-05-01 17:29:34 -07:00
|
|
|
}
|
2012-05-31 11:13:17 -07:00
|
|
|
// Save this packet's granule position in case we need to perform end
|
|
|
|
// trimming on the next packet.
|
|
|
|
mOpusState->mPrevPacketGranulepos = endFrame;
|
2012-05-01 17:29:34 -07:00
|
|
|
|
2012-05-31 11:13:17 -07:00
|
|
|
// Apply the header gain if one was specified.
|
|
|
|
#ifdef MOZ_SAMPLE_TYPE_FLOAT32
|
|
|
|
if (mOpusState->mGain != 1.0f) {
|
|
|
|
float gain = mOpusState->mGain;
|
|
|
|
int samples = frames * channels;
|
|
|
|
for (int i = 0; i < samples; i++) {
|
|
|
|
buffer[i] *= gain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (mOpusState->mGain_Q16 != 65536) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t gain_Q16 = mOpusState->mGain_Q16;
|
2012-05-31 11:13:17 -07:00
|
|
|
int samples = frames * channels;
|
|
|
|
for (int i = 0; i < samples; i++) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t val = static_cast<int32_t>((gain_Q16*buffer[i] + 32768)>>16);
|
2012-05-31 11:13:17 -07:00
|
|
|
buffer[i] = static_cast<AudioDataValue>(MOZ_CLIP_TO_15(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-09 15:53:23 -07:00
|
|
|
// More than 2 decoded channels must be downmixed to stereo.
|
|
|
|
if (channels > 2) {
|
|
|
|
// Opus doesn't provide a channel mapping for more than 8 channels,
|
|
|
|
// so we can't downmix more than that.
|
|
|
|
if (channels > 8)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
#ifdef MOZ_SAMPLE_TYPE_FLOAT32
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t out_channels;
|
2012-08-09 15:53:23 -07:00
|
|
|
out_channels = 2;
|
|
|
|
|
|
|
|
// dBuffer stores the downmixed sample data.
|
|
|
|
nsAutoArrayPtr<AudioDataValue> dBuffer(new AudioDataValue[frames * out_channels]);
|
|
|
|
// Downmix matrix for channels up to 8, normalized to 2.0.
|
|
|
|
static const float dmatrix[6][8][2]= {
|
|
|
|
/*3*/{ {0.5858f,0}, {0.4142f,0.4142f}, {0,0.5858f}},
|
|
|
|
/*4*/{ {0.4226f,0}, {0,0.4226f}, {0.366f,0.2114f}, {0.2114f,0.366f}},
|
|
|
|
/*5*/{ {0.651f,0}, {0.46f,0.46f}, {0,0.651f}, {0.5636f,0.3254f}, {0.3254f,0.5636f}},
|
|
|
|
/*6*/{ {0.529f,0}, {0.3741f,0.3741f}, {0,0.529f}, {0.4582f,0.2645f}, {0.2645f,0.4582f}, {0.3741f,0.3741f}},
|
2012-09-03 01:41:17 -07:00
|
|
|
/*7*/{ {0.4553f,0}, {0.322f,0.322f}, {0,0.4553f}, {0.3943f,0.2277f}, {0.2277f,0.3943f}, {0.2788f,0.2788f}, {0.322f,0.322f}},
|
2012-08-09 15:53:23 -07:00
|
|
|
/*8*/{ {0.3886f,0}, {0.2748f,0.2748f}, {0,0.3886f}, {0.3366f,0.1943f}, {0.1943f,0.3366f}, {0.3366f,0.1943f}, {0.1943f,0.3366f}, {0.2748f,0.2748f}},
|
|
|
|
};
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < frames; i++) {
|
2012-08-09 15:53:23 -07:00
|
|
|
float sampL = 0.0;
|
|
|
|
float sampR = 0.0;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < channels; j++) {
|
2012-08-09 15:53:23 -07:00
|
|
|
sampL+=buffer[i*channels+j]*dmatrix[channels-3][j][0];
|
|
|
|
sampR+=buffer[i*channels+j]*dmatrix[channels-3][j][1];
|
|
|
|
}
|
|
|
|
dBuffer[i*out_channels]=sampL;
|
|
|
|
dBuffer[i*out_channels+1]=sampR;
|
|
|
|
}
|
|
|
|
channels = out_channels;
|
|
|
|
buffer = dBuffer;
|
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-05-30 21:03:14 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("Opus decoder pushing %d frames", frames));
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t startTime = mOpusState->Time(startFrame);
|
|
|
|
int64_t endTime = mOpusState->Time(endFrame);
|
2012-05-01 17:29:34 -07:00
|
|
|
mAudioQueue.Push(new AudioData(mPageOffset,
|
|
|
|
startTime,
|
2012-05-31 11:13:17 -07:00
|
|
|
endTime - startTime,
|
2012-05-01 17:29:34 -07:00
|
|
|
frames,
|
|
|
|
buffer.forget(),
|
|
|
|
channels));
|
2012-11-30 05:17:54 -08:00
|
|
|
|
|
|
|
mDecodedAudioFrames += frames;
|
|
|
|
|
2012-05-01 17:29:34 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif /* MOZ_OPUS */
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
bool OggReader::DecodeAudioData()
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
2011-07-11 20:39:25 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(mVorbisState != nullptr || mOpusState != nullptr,
|
2012-05-01 17:29:34 -07:00
|
|
|
"Need audio codec state to decode audio");
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
// Read the next data packet. Skip any non-data packets we encounter.
|
|
|
|
ogg_packet* packet = 0;
|
2012-11-14 11:46:40 -08:00
|
|
|
OggCodecState* codecState;
|
2012-05-01 17:29:34 -07:00
|
|
|
if (mVorbisState)
|
2012-11-14 11:46:40 -08:00
|
|
|
codecState = static_cast<OggCodecState*>(mVorbisState);
|
2012-05-01 17:29:34 -07:00
|
|
|
else
|
2012-11-14 11:46:40 -08:00
|
|
|
codecState = static_cast<OggCodecState*>(mOpusState);
|
2011-05-07 23:24:09 -07:00
|
|
|
do {
|
|
|
|
if (packet) {
|
2012-11-14 11:46:40 -08:00
|
|
|
OggCodecState::ReleasePacket(packet);
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
2012-05-01 17:29:34 -07:00
|
|
|
packet = NextOggPacket(codecState);
|
|
|
|
} while (packet && codecState->IsHeader(packet));
|
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
if (!packet) {
|
|
|
|
mAudioQueue.Finish();
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
NS_ASSERTION(packet && packet->granulepos != -1,
|
|
|
|
"Must have packet with known granulepos");
|
2012-05-31 11:13:17 -07:00
|
|
|
nsAutoRef<ogg_packet> autoRelease(packet);
|
2012-05-01 17:29:34 -07:00
|
|
|
if (mVorbisState) {
|
|
|
|
DecodeVorbis(packet);
|
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
} else if (mOpusState) {
|
|
|
|
DecodeOpus(packet);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-11-30 05:17:54 -08:00
|
|
|
if ((packet->e_o_s) && (!ReadOggChain())) {
|
2010-05-05 19:31:02 -07:00
|
|
|
// We've encountered an end of bitstream packet, or we've hit the end of
|
|
|
|
// file while trying to decode, so inform the audio queue that there'll
|
|
|
|
// be no more samples.
|
|
|
|
mAudioQueue.Finish();
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
return true;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
2012-11-30 05:17:54 -08:00
|
|
|
void OggReader::SetChained(bool aIsChained) {
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
mIsChained = aIsChained;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
mDecoder->SetMediaSeekable(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OggReader::ReadOggChain()
|
|
|
|
{
|
|
|
|
|
|
|
|
bool chained = false;
|
|
|
|
OpusState* newOpusState = nullptr;
|
|
|
|
VorbisState* newVorbisState = nullptr;
|
|
|
|
int channels = 0;
|
|
|
|
long rate = 0;
|
|
|
|
MetadataTags* tags = nullptr;
|
|
|
|
|
|
|
|
if (HasVideo() || HasSkeleton() || !HasAudio()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ogg_page page;
|
|
|
|
int64_t pageOffset = ReadOggPage(&page);
|
|
|
|
if ((pageOffset == -1) || (!ogg_page_bos(&page))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int serial = ogg_page_serialno(&page);
|
|
|
|
if (mCodecStates.Get(serial, nullptr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoPtr<OggCodecState> codecState;
|
|
|
|
codecState = OggCodecState::Create(&page);
|
|
|
|
if (!codecState) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mVorbisState && (codecState->GetType() == OggCodecState::TYPE_VORBIS)) {
|
|
|
|
newVorbisState = static_cast<VorbisState*>(codecState.get());
|
|
|
|
}
|
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
else if (mOpusState && (codecState->GetType() == OggCodecState::TYPE_OPUS)) {
|
|
|
|
newOpusState = static_cast<OpusState*>(codecState.get());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCodecStates.Put(serial, codecState.forget());
|
|
|
|
mKnownStreams.AppendElement(serial);
|
|
|
|
OggCodecState* state = nullptr;
|
|
|
|
mCodecStates.Get(serial, &state);
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(state, false);
|
|
|
|
|
|
|
|
if (NS_FAILED(state->PageIn(&page))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((newVorbisState && ReadHeaders(newVorbisState)) &&
|
|
|
|
(mVorbisState->mInfo.rate == newVorbisState->mInfo.rate) &&
|
|
|
|
(mVorbisState->mInfo.channels == newVorbisState->mInfo.channels)) {
|
|
|
|
mVorbisState->Reset();
|
|
|
|
mVorbisState = newVorbisState;
|
|
|
|
mVorbisSerial = mVorbisState->mSerial;
|
|
|
|
LOG(PR_LOG_DEBUG, ("New vorbis ogg link, serial=%d\n", mVorbisSerial));
|
|
|
|
chained = true;
|
|
|
|
rate = mVorbisState->mInfo.rate;
|
|
|
|
channels = mVorbisState->mInfo.channels;
|
|
|
|
tags = mVorbisState->GetTags();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
if ((newOpusState && ReadHeaders(newOpusState)) &&
|
|
|
|
(mOpusState->mRate == newOpusState->mRate) &&
|
2012-12-03 06:44:26 -08:00
|
|
|
(mOpusState->mChannels == newOpusState->mChannels)) {
|
2012-11-30 05:17:54 -08:00
|
|
|
mOpusState->Reset();
|
|
|
|
mOpusState = newOpusState;
|
|
|
|
mOpusSerial = mOpusState->mSerial;
|
|
|
|
chained = true;
|
|
|
|
rate = mOpusState->mRate;
|
|
|
|
channels = mOpusState->mChannels;
|
|
|
|
tags = mOpusState->GetTags();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (chained) {
|
|
|
|
SetChained(true);
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
mDecoder->QueueMetadata((mDecodedAudioFrames * USECS_PER_S) / rate,
|
|
|
|
channels,
|
|
|
|
rate,
|
|
|
|
HasAudio(),
|
|
|
|
tags);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult OggReader::DecodeTheora(ogg_packet* aPacket, int64_t aTimeThreshold)
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
2011-05-07 23:24:09 -07:00
|
|
|
NS_ASSERTION(aPacket->granulepos >= TheoraVersion(&mTheoraState->mInfo,3,2,1),
|
|
|
|
"Packets must have valid granulepos and packetno");
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
int ret = th_decode_packetin(mTheoraState->mCtx, aPacket, 0);
|
|
|
|
if (ret != 0 && ret != TH_DUPFRAME) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t time = mTheoraState->StartTime(aPacket->granulepos);
|
2011-05-07 23:24:09 -07:00
|
|
|
|
|
|
|
// Don't use the frame if it's outside the bounds of the presentation
|
|
|
|
// start time in the skeleton track. Note we still must submit the frame
|
|
|
|
// to the decoder (via th_decode_packetin), as the frames which are
|
|
|
|
// presentable may depend on this frame's data.
|
|
|
|
if (mSkeletonState && !mSkeletonState->IsPresentable(time)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t endTime = mTheoraState->Time(aPacket->granulepos);
|
2011-06-08 13:56:45 -07:00
|
|
|
if (endTime < aTimeThreshold) {
|
|
|
|
// The end time of this frame is already before the current playback
|
|
|
|
// position. It will never be displayed, don't bother enqueing it.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
if (ret == TH_DUPFRAME) {
|
2010-09-14 16:24:47 -07:00
|
|
|
VideoData* v = VideoData::CreateDuplicate(mPageOffset,
|
|
|
|
time,
|
|
|
|
endTime,
|
|
|
|
aPacket->granulepos);
|
2011-05-07 23:24:09 -07:00
|
|
|
mVideoQueue.Push(v);
|
2010-05-05 19:31:02 -07:00
|
|
|
} else if (ret == 0) {
|
|
|
|
th_ycbcr_buffer buffer;
|
|
|
|
ret = th_decode_ycbcr_out(mTheoraState->mCtx, buffer);
|
|
|
|
NS_ASSERTION(ret == 0, "th_decode_ycbcr_out failed");
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isKeyframe = th_packet_iskeyframe(aPacket) == 1;
|
2010-05-05 19:31:02 -07:00
|
|
|
VideoData::YCbCrBuffer b;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i=0; i < 3; ++i) {
|
2010-05-05 19:31:02 -07:00
|
|
|
b.mPlanes[i].mData = buffer[i].data;
|
|
|
|
b.mPlanes[i].mHeight = buffer[i].height;
|
|
|
|
b.mPlanes[i].mWidth = buffer[i].width;
|
|
|
|
b.mPlanes[i].mStride = buffer[i].stride;
|
2012-05-31 17:54:23 -07:00
|
|
|
b.mPlanes[i].mOffset = b.mPlanes[i].mSkip = 0;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
2011-03-23 20:53:03 -07:00
|
|
|
|
2010-05-18 20:04:33 -07:00
|
|
|
VideoData *v = VideoData::Create(mInfo,
|
|
|
|
mDecoder->GetImageContainer(),
|
|
|
|
mPageOffset,
|
2010-05-05 19:31:02 -07:00
|
|
|
time,
|
2010-07-19 18:29:30 -07:00
|
|
|
endTime,
|
2010-05-05 19:31:02 -07:00
|
|
|
b,
|
|
|
|
isKeyframe,
|
2011-06-23 15:08:54 -07:00
|
|
|
aPacket->granulepos,
|
|
|
|
mPicture);
|
2010-05-05 19:31:02 -07:00
|
|
|
if (!v) {
|
2010-05-18 20:04:33 -07:00
|
|
|
// There may be other reasons for this error, but for
|
|
|
|
// simplicity just assume the worst case: out of memory.
|
2010-05-05 19:31:02 -07:00
|
|
|
NS_WARNING("Failed to allocate memory for video frame");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2011-05-07 23:24:09 -07:00
|
|
|
mVideoQueue.Push(v);
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
bool OggReader::DecodeVideoFrame(bool &aKeyframeSkip,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aTimeThreshold)
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
2011-07-11 20:39:25 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2011-03-23 15:28:57 -07:00
|
|
|
|
|
|
|
// Record number of frames decoded and parsed. Automatically update the
|
|
|
|
// stats counters using the AutoNotifyDecoded stack-based class.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t parsed = 0, decoded = 0;
|
2012-11-19 07:11:21 -08:00
|
|
|
AbstractMediaDecoder::AutoNotifyDecoded autoNotify(mDecoder, parsed, decoded);
|
2011-03-23 15:28:57 -07:00
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
// Read the next data packet. Skip any non-data packets we encounter.
|
|
|
|
ogg_packet* packet = 0;
|
|
|
|
do {
|
|
|
|
if (packet) {
|
2012-11-14 11:46:40 -08:00
|
|
|
OggCodecState::ReleasePacket(packet);
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
2011-05-07 23:24:09 -07:00
|
|
|
packet = NextOggPacket(mTheoraState);
|
|
|
|
} while (packet && mTheoraState->IsHeader(packet));
|
|
|
|
if (!packet) {
|
|
|
|
mVideoQueue.Finish();
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
2012-05-31 11:13:17 -07:00
|
|
|
nsAutoRef<ogg_packet> autoRelease(packet);
|
2011-05-07 23:24:09 -07:00
|
|
|
|
|
|
|
parsed++;
|
|
|
|
NS_ASSERTION(packet && packet->granulepos != -1,
|
|
|
|
"Must know first packet's granulepos");
|
2011-09-28 23:19:26 -07:00
|
|
|
bool eos = packet->e_o_s;
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t frameEndTime = mTheoraState->Time(packet->granulepos);
|
2011-05-07 23:24:09 -07:00
|
|
|
if (!aKeyframeSkip ||
|
|
|
|
(th_packet_iskeyframe(packet) && frameEndTime >= aTimeThreshold))
|
|
|
|
{
|
2011-09-29 16:34:37 -07:00
|
|
|
aKeyframeSkip = false;
|
2011-06-08 13:56:45 -07:00
|
|
|
nsresult res = DecodeTheora(packet, aTimeThreshold);
|
2011-05-07 23:24:09 -07:00
|
|
|
decoded++;
|
|
|
|
if (NS_FAILED(res)) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
if (eos) {
|
2010-05-05 19:31:02 -07:00
|
|
|
// We've encountered an end of bitstream packet. Inform the queue that
|
|
|
|
// there will be no more frames.
|
|
|
|
mVideoQueue.Finish();
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
return true;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
int64_t OggReader::ReadOggPage(ogg_page* aPage)
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
2011-07-11 20:39:25 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
while((ret = ogg_sync_pageseek(&mOggState, aPage)) <= 0) {
|
|
|
|
if (ret < 0) {
|
|
|
|
// Lost page sync, have to skip up to next page.
|
|
|
|
mPageOffset += -ret;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Returns a buffer that can be written too
|
|
|
|
// with the given size. This buffer is stored
|
|
|
|
// in the ogg synchronisation structure.
|
|
|
|
char* buffer = ogg_sync_buffer(&mOggState, 4096);
|
|
|
|
NS_ASSERTION(buffer, "ogg_sync_buffer failed");
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
// Read from the resource into the buffer
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bytesRead = 0;
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult rv = mDecoder->GetResource()->Read(buffer, 4096, &bytesRead);
|
2010-05-05 19:31:02 -07:00
|
|
|
if (NS_FAILED(rv) || (bytesRead == 0 && ret == 0)) {
|
|
|
|
// End of file.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDecoder->NotifyBytesConsumed(bytesRead);
|
|
|
|
// Update the synchronisation layer with the number
|
|
|
|
// of bytes written to the buffer
|
|
|
|
ret = ogg_sync_wrote(&mOggState, bytesRead);
|
|
|
|
NS_ENSURE_TRUE(ret == 0, -1);
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t offset = mPageOffset;
|
2010-05-05 19:31:02 -07:00
|
|
|
mPageOffset += aPage->header_len + aPage->body_len;
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
ogg_packet* OggReader::NextOggPacket(OggCodecState* aCodecState)
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
2011-07-11 20:39:25 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
if (!aCodecState || !aCodecState->mActive) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
ogg_packet* packet;
|
2012-07-30 07:20:58 -07:00
|
|
|
while ((packet = aCodecState->PacketOut()) == nullptr) {
|
2010-05-05 19:31:02 -07:00
|
|
|
// The codec state does not have any buffered pages, so try to read another
|
|
|
|
// page from the channel.
|
2011-05-07 23:24:09 -07:00
|
|
|
ogg_page page;
|
2010-05-05 19:31:02 -07:00
|
|
|
if (ReadOggPage(&page) == -1) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t serial = ogg_page_serialno(&page);
|
2012-11-14 11:46:40 -08:00
|
|
|
OggCodecState* codecState = nullptr;
|
2010-05-05 19:31:02 -07:00
|
|
|
mCodecStates.Get(serial, &codecState);
|
2011-05-07 23:24:09 -07:00
|
|
|
if (codecState && NS_FAILED(codecState->PageIn(&page))) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
return packet;
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an ogg page's checksum.
|
|
|
|
static ogg_uint32_t
|
|
|
|
GetChecksum(ogg_page* page)
|
|
|
|
{
|
|
|
|
if (page == 0 || page->header == 0 || page->header_len < 25) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const unsigned char* p = page->header + 22;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t c = p[0] +
|
2010-05-05 19:31:02 -07:00
|
|
|
(p[1] << 8) +
|
|
|
|
(p[2] << 16) +
|
|
|
|
(p[3] << 24);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
int64_t OggReader::RangeStartTime(int64_t aOffset)
|
2010-05-20 14:13:01 -07:00
|
|
|
{
|
2011-07-11 20:39:28 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(resource != nullptr, 0);
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult res = resource->Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
|
2012-07-20 04:16:17 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, 0);
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t startTime = 0;
|
2012-11-14 11:46:40 -08:00
|
|
|
MediaDecoderReader::FindStartTime(startTime);
|
2011-05-08 14:10:28 -07:00
|
|
|
return startTime;
|
2010-05-20 14:13:01 -07:00
|
|
|
}
|
|
|
|
|
2011-05-08 14:10:28 -07:00
|
|
|
struct nsAutoOggSyncState {
|
|
|
|
nsAutoOggSyncState() {
|
|
|
|
ogg_sync_init(&mState);
|
|
|
|
}
|
|
|
|
~nsAutoOggSyncState() {
|
|
|
|
ogg_sync_clear(&mState);
|
|
|
|
}
|
|
|
|
ogg_sync_state mState;
|
|
|
|
};
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
int64_t OggReader::RangeEndTime(int64_t aEndOffset)
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
2011-07-11 20:39:23 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnStateMachineThread() || mDecoder->OnDecodeThread(),
|
|
|
|
"Should be on state machine or decode thread.");
|
2011-05-08 14:10:28 -07:00
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(resource != nullptr, -1);
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t position = resource->Tell();
|
|
|
|
int64_t endTime = RangeEndTime(0, aEndOffset, false);
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult res = resource->Seek(nsISeekableStream::NS_SEEK_SET, position);
|
2010-08-05 00:40:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, -1);
|
|
|
|
return endTime;
|
|
|
|
}
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
int64_t OggReader::RangeEndTime(int64_t aStartOffset,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aEndOffset,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aCachedDataOnly)
|
2010-08-05 00:40:35 -07:00
|
|
|
{
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
2011-05-08 14:10:28 -07:00
|
|
|
nsAutoOggSyncState sync;
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
// We need to find the last page which ends before aEndOffset that
|
|
|
|
// has a granulepos that we can convert to a timestamp. We do this by
|
|
|
|
// backing off from aEndOffset until we encounter a page on which we can
|
|
|
|
// interpret the granulepos. If while backing off we encounter a page which
|
|
|
|
// we've previously encountered before, we'll either backoff again if we
|
|
|
|
// haven't found an end time yet, or return the last end time found.
|
|
|
|
const int step = 5000;
|
2012-06-25 09:38:56 -07:00
|
|
|
const int maxOggPageSize = 65306;
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t readStartOffset = aEndOffset;
|
|
|
|
int64_t readLimitOffset = aEndOffset;
|
|
|
|
int64_t readHead = aEndOffset;
|
|
|
|
int64_t endTime = -1;
|
|
|
|
uint32_t checksumAfterSeek = 0;
|
|
|
|
uint32_t prevChecksumAfterSeek = 0;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mustBackOff = false;
|
2011-09-29 16:34:37 -07:00
|
|
|
while (true) {
|
2010-05-05 19:31:02 -07:00
|
|
|
ogg_page page;
|
2011-05-08 14:10:28 -07:00
|
|
|
int ret = ogg_sync_pageseek(&sync.mState, &page);
|
2010-05-05 19:31:02 -07:00
|
|
|
if (ret == 0) {
|
|
|
|
// We need more data if we've not encountered a page we've seen before,
|
|
|
|
// or we've read to the end of file.
|
2011-03-23 15:28:57 -07:00
|
|
|
if (mustBackOff || readHead == aEndOffset || readHead == aStartOffset) {
|
2010-09-09 20:29:06 -07:00
|
|
|
if (endTime != -1 || readStartOffset == 0) {
|
2010-05-05 19:31:02 -07:00
|
|
|
// We have encountered a page before, or we're at the end of file.
|
|
|
|
break;
|
|
|
|
}
|
2011-09-29 16:34:37 -07:00
|
|
|
mustBackOff = false;
|
2010-05-05 19:31:02 -07:00
|
|
|
prevChecksumAfterSeek = checksumAfterSeek;
|
|
|
|
checksumAfterSeek = 0;
|
2011-05-08 14:10:28 -07:00
|
|
|
ogg_sync_reset(&sync.mState);
|
2012-08-22 08:56:38 -07:00
|
|
|
readStartOffset = NS_MAX(static_cast<int64_t>(0), readStartOffset - step);
|
2012-06-25 09:38:56 -07:00
|
|
|
// There's no point reading more than the maximum size of
|
|
|
|
// an Ogg page into data we've previously scanned. Any data
|
|
|
|
// between readLimitOffset and aEndOffset must be garbage
|
|
|
|
// and we can ignore it thereafter.
|
|
|
|
readLimitOffset = NS_MIN(readLimitOffset,
|
|
|
|
readStartOffset + maxOggPageSize);
|
2011-03-23 15:28:57 -07:00
|
|
|
readHead = NS_MAX(aStartOffset, readStartOffset);
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
2012-09-27 23:57:33 -07:00
|
|
|
int64_t limit = NS_MIN(static_cast<int64_t>(UINT32_MAX),
|
2010-08-05 00:40:35 -07:00
|
|
|
aEndOffset - readHead);
|
2012-08-22 08:56:38 -07:00
|
|
|
limit = NS_MAX(static_cast<int64_t>(0), limit);
|
|
|
|
limit = NS_MIN(limit, static_cast<int64_t>(step));
|
|
|
|
uint32_t bytesToRead = static_cast<uint32_t>(limit);
|
|
|
|
uint32_t bytesRead = 0;
|
2011-05-08 14:10:28 -07:00
|
|
|
char* buffer = ogg_sync_buffer(&sync.mState, bytesToRead);
|
2010-05-05 19:31:02 -07:00
|
|
|
NS_ASSERTION(buffer, "Must have buffer");
|
2010-08-05 00:40:35 -07:00
|
|
|
nsresult res;
|
|
|
|
if (aCachedDataOnly) {
|
2012-02-14 20:35:01 -08:00
|
|
|
res = resource->ReadFromCache(buffer, readHead, bytesToRead);
|
2011-03-23 15:28:57 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, -1);
|
2010-08-05 00:40:35 -07:00
|
|
|
bytesRead = bytesToRead;
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(readHead < aEndOffset,
|
2012-02-14 20:35:01 -08:00
|
|
|
"resource pos must be before range end");
|
|
|
|
res = resource->Seek(nsISeekableStream::NS_SEEK_SET, readHead);
|
2011-03-23 15:28:57 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, -1);
|
2012-02-14 20:35:01 -08:00
|
|
|
res = resource->Read(buffer, bytesToRead, &bytesRead);
|
2011-03-23 15:28:57 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, -1);
|
2010-08-05 00:40:35 -07:00
|
|
|
}
|
|
|
|
readHead += bytesRead;
|
2012-06-25 09:38:56 -07:00
|
|
|
if (readHead > readLimitOffset) {
|
|
|
|
mustBackOff = true;
|
|
|
|
}
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
// Update the synchronisation layer with the number
|
|
|
|
// of bytes written to the buffer
|
2011-05-08 14:10:28 -07:00
|
|
|
ret = ogg_sync_wrote(&sync.mState, bytesRead);
|
2010-05-05 19:31:02 -07:00
|
|
|
if (ret != 0) {
|
|
|
|
endTime = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0 || ogg_page_granulepos(&page) < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t checksum = GetChecksum(&page);
|
2010-05-05 19:31:02 -07:00
|
|
|
if (checksumAfterSeek == 0) {
|
|
|
|
// This is the first page we've decoded after a backoff/seek. Remember
|
|
|
|
// the page checksum. If we backoff further and encounter this page
|
|
|
|
// again, we'll know that we won't find a page with an end time after
|
|
|
|
// this one, so we'll know to back off again.
|
|
|
|
checksumAfterSeek = checksum;
|
|
|
|
}
|
|
|
|
if (checksum == prevChecksumAfterSeek) {
|
|
|
|
// This page has the same checksum as the first page we encountered
|
|
|
|
// after the last backoff/seek. Since we've already scanned after this
|
|
|
|
// page and failed to find an end time, we may as well backoff again and
|
|
|
|
// try to find an end time from an earlier page.
|
2011-09-29 16:34:37 -07:00
|
|
|
mustBackOff = true;
|
2010-05-05 19:31:02 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t granulepos = ogg_page_granulepos(&page);
|
2010-05-05 19:31:02 -07:00
|
|
|
int serial = ogg_page_serialno(&page);
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
OggCodecState* codecState = nullptr;
|
2010-05-05 19:31:02 -07:00
|
|
|
mCodecStates.Get(serial, &codecState);
|
|
|
|
|
|
|
|
if (!codecState) {
|
|
|
|
// This page is from a bitstream which we haven't encountered yet.
|
|
|
|
// It's probably from a new "link" in a "chained" ogg. Don't
|
|
|
|
// bother even trying to find a duration...
|
2012-11-30 05:17:54 -08:00
|
|
|
SetChained(true);
|
2010-05-25 07:34:09 -07:00
|
|
|
endTime = -1;
|
2010-05-05 19:31:02 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t t = codecState->Time(granulepos);
|
2010-05-05 19:31:02 -07:00
|
|
|
if (t != -1) {
|
|
|
|
endTime = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return endTime;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult OggReader::GetSeekRanges(nsTArray<SeekRange>& aRanges)
|
2011-03-23 15:28:58 -07:00
|
|
|
{
|
2011-07-11 20:39:25 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2012-02-14 20:35:01 -08:00
|
|
|
nsTArray<MediaByteRange> cached;
|
|
|
|
nsresult res = mDecoder->GetResource()->GetCachedRanges(cached);
|
2011-03-23 15:28:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < cached.Length(); index++) {
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaByteRange& range = cached[index];
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t startTime = -1;
|
|
|
|
int64_t endTime = -1;
|
2011-03-23 15:28:58 -07:00
|
|
|
if (NS_FAILED(ResetDecode())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2011-03-23 15:28:58 -07:00
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t startOffset = range.mStart;
|
|
|
|
int64_t endOffset = range.mEnd;
|
2011-05-08 14:10:28 -07:00
|
|
|
startTime = RangeStartTime(startOffset);
|
2011-03-23 15:28:58 -07:00
|
|
|
if (startTime != -1 &&
|
2011-05-08 14:10:28 -07:00
|
|
|
((endTime = RangeEndTime(endOffset)) != -1))
|
2011-03-23 15:28:58 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(startTime < endTime,
|
|
|
|
"Start time must be before end time");
|
|
|
|
aRanges.AppendElement(SeekRange(startOffset,
|
|
|
|
endOffset,
|
|
|
|
startTime,
|
|
|
|
endTime));
|
|
|
|
}
|
2011-03-23 15:28:58 -07:00
|
|
|
}
|
|
|
|
if (NS_FAILED(ResetDecode())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
OggReader::SeekRange
|
|
|
|
OggReader::SelectSeekRange(const nsTArray<SeekRange>& ranges,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aTarget,
|
|
|
|
int64_t aStartTime,
|
|
|
|
int64_t aEndTime,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aExact)
|
2011-03-23 15:28:58 -07:00
|
|
|
{
|
2011-07-11 20:39:25 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t so = 0;
|
|
|
|
int64_t eo = mDecoder->GetResource()->GetLength();
|
|
|
|
int64_t st = aStartTime;
|
|
|
|
int64_t et = aEndTime;
|
|
|
|
for (uint32_t i = 0; i < ranges.Length(); i++) {
|
2011-03-23 15:28:58 -07:00
|
|
|
const SeekRange &r = ranges[i];
|
|
|
|
if (r.mTimeStart < aTarget) {
|
|
|
|
so = r.mOffsetStart;
|
|
|
|
st = r.mTimeStart;
|
|
|
|
}
|
|
|
|
if (r.mTimeEnd >= aTarget && r.mTimeEnd < et) {
|
|
|
|
eo = r.mOffsetEnd;
|
|
|
|
et = r.mTimeEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r.mTimeStart < aTarget && aTarget <= r.mTimeEnd) {
|
|
|
|
// Target lies exactly in this range.
|
|
|
|
return ranges[i];
|
|
|
|
}
|
|
|
|
}
|
2011-03-31 21:18:43 -07:00
|
|
|
if (aExact || eo == -1) {
|
|
|
|
return SeekRange();
|
|
|
|
}
|
|
|
|
return SeekRange(so, eo, st, et);
|
2011-03-23 15:28:58 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
OggReader::IndexedSeekResult OggReader::RollbackIndexedSeek(int64_t aOffset)
|
2010-08-19 15:50:37 -07:00
|
|
|
{
|
|
|
|
mSkeletonState->Deactivate();
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(resource != nullptr, SEEK_FATAL_ERROR);
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult res = resource->Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
|
2010-08-19 15:50:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, SEEK_FATAL_ERROR);
|
|
|
|
return SEEK_INDEX_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
OggReader::IndexedSeekResult OggReader::SeekToKeyframeUsingIndex(int64_t aTarget)
|
2010-08-19 15:50:37 -07:00
|
|
|
{
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(resource != nullptr, SEEK_FATAL_ERROR);
|
2010-08-19 15:50:37 -07:00
|
|
|
if (!HasSkeleton() || !mSkeletonState->HasIndex()) {
|
|
|
|
return SEEK_INDEX_FAIL;
|
|
|
|
}
|
|
|
|
// We have an index from the Skeleton track, try to use it to seek.
|
2012-08-22 08:56:38 -07:00
|
|
|
nsAutoTArray<uint32_t, 2> tracks;
|
2012-05-31 11:13:11 -07:00
|
|
|
BuildSerialList(tracks);
|
2012-11-14 11:46:40 -08:00
|
|
|
SkeletonState::nsSeekTarget keyframe;
|
2010-08-19 15:50:37 -07:00
|
|
|
if (NS_FAILED(mSkeletonState->IndexedSeekTarget(aTarget,
|
|
|
|
tracks,
|
|
|
|
keyframe)))
|
|
|
|
{
|
|
|
|
// Could not locate a keypoint for the target in the index.
|
|
|
|
return SEEK_INDEX_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
// Remember original resource read cursor position so we can rollback on failure.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t tell = resource->Tell();
|
2010-08-19 15:50:37 -07:00
|
|
|
|
|
|
|
// Seek to the keypoint returned by the index.
|
2012-02-14 20:35:01 -08:00
|
|
|
if (keyframe.mKeyPoint.mOffset > resource->GetLength() ||
|
2010-08-19 15:50:37 -07:00
|
|
|
keyframe.mKeyPoint.mOffset < 0)
|
|
|
|
{
|
|
|
|
// Index must be invalid.
|
|
|
|
return RollbackIndexedSeek(tell);
|
|
|
|
}
|
|
|
|
LOG(PR_LOG_DEBUG, ("Seeking using index to keyframe at offset %lld\n",
|
|
|
|
keyframe.mKeyPoint.mOffset));
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult res = resource->Seek(nsISeekableStream::NS_SEEK_SET,
|
2010-08-19 15:50:37 -07:00
|
|
|
keyframe.mKeyPoint.mOffset);
|
|
|
|
NS_ENSURE_SUCCESS(res, SEEK_FATAL_ERROR);
|
|
|
|
mPageOffset = keyframe.mKeyPoint.mOffset;
|
|
|
|
|
|
|
|
// We've moved the read set, so reset decode.
|
|
|
|
res = ResetDecode();
|
|
|
|
NS_ENSURE_SUCCESS(res, SEEK_FATAL_ERROR);
|
|
|
|
|
|
|
|
// Check that the page the index thinks is exactly here is actually exactly
|
|
|
|
// here. If not, the index is invalid.
|
|
|
|
ogg_page page;
|
|
|
|
int skippedBytes = 0;
|
2012-02-14 20:35:01 -08:00
|
|
|
PageSyncResult syncres = PageSync(resource,
|
2010-08-19 15:50:37 -07:00
|
|
|
&mOggState,
|
2011-09-29 16:34:37 -07:00
|
|
|
false,
|
2010-08-19 15:50:37 -07:00
|
|
|
mPageOffset,
|
2012-02-14 20:35:01 -08:00
|
|
|
resource->GetLength(),
|
2010-08-19 15:50:37 -07:00
|
|
|
&page,
|
|
|
|
skippedBytes);
|
|
|
|
NS_ENSURE_TRUE(syncres != PAGE_SYNC_ERROR, SEEK_FATAL_ERROR);
|
|
|
|
if (syncres != PAGE_SYNC_OK || skippedBytes != 0) {
|
|
|
|
LOG(PR_LOG_DEBUG, ("Indexed-seek failure: Ogg Skeleton Index is invalid "
|
|
|
|
"or sync error after seek"));
|
|
|
|
return RollbackIndexedSeek(tell);
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t serial = ogg_page_serialno(&page);
|
2010-08-19 15:50:37 -07:00
|
|
|
if (serial != keyframe.mSerial) {
|
|
|
|
// Serialno of page at offset isn't what the index told us to expect.
|
|
|
|
// Assume the index is invalid.
|
|
|
|
return RollbackIndexedSeek(tell);
|
|
|
|
}
|
2012-11-14 11:46:40 -08:00
|
|
|
OggCodecState* codecState = nullptr;
|
2010-08-19 15:50:37 -07:00
|
|
|
mCodecStates.Get(serial, &codecState);
|
|
|
|
if (codecState &&
|
|
|
|
codecState->mActive &&
|
|
|
|
ogg_stream_pagein(&codecState->mState, &page) != 0)
|
|
|
|
{
|
2012-02-14 20:35:01 -08:00
|
|
|
// Couldn't insert page into the ogg resource, or somehow the resource
|
2010-08-19 15:50:37 -07:00
|
|
|
// is no longer active.
|
|
|
|
return RollbackIndexedSeek(tell);
|
2012-11-06 14:33:01 -08:00
|
|
|
}
|
2010-08-19 15:50:37 -07:00
|
|
|
mPageOffset = keyframe.mKeyPoint.mOffset + page.header_len + page.body_len;
|
|
|
|
return SEEK_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult OggReader::SeekInBufferedRange(int64_t aTarget,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aAdjustedTarget,
|
|
|
|
int64_t aStartTime,
|
|
|
|
int64_t aEndTime,
|
2011-03-23 15:28:58 -07:00
|
|
|
const nsTArray<SeekRange>& aRanges,
|
|
|
|
const SeekRange& aRange)
|
2010-08-19 15:50:37 -07:00
|
|
|
{
|
2011-07-13 14:24:35 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Seeking in buffered data to %lld using bisection search", mDecoder, aTarget));
|
2012-05-31 11:13:17 -07:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
if (HasVideo() || aAdjustedTarget >= aTarget) {
|
|
|
|
// We know the exact byte range in which the target must lie. It must
|
|
|
|
// be buffered in the media cache. Seek there.
|
|
|
|
nsresult res = SeekBisection(aTarget, aRange, 0);
|
|
|
|
if (NS_FAILED(res) || !HasVideo()) {
|
|
|
|
return res;
|
|
|
|
}
|
2010-08-19 15:50:37 -07:00
|
|
|
|
2012-05-31 11:13:17 -07:00
|
|
|
// We have an active Theora bitstream. Decode the next Theora frame, and
|
|
|
|
// extract its keyframe's time.
|
|
|
|
bool eof;
|
|
|
|
do {
|
|
|
|
bool skip = false;
|
|
|
|
eof = !DecodeVideoFrame(skip, 0);
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2012-11-19 07:11:21 -08:00
|
|
|
if (mDecoder->IsShutdown()) {
|
2012-05-31 11:13:17 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2010-08-19 15:50:37 -07:00
|
|
|
}
|
2012-05-31 11:13:17 -07:00
|
|
|
} while (!eof &&
|
|
|
|
mVideoQueue.GetSize() == 0);
|
|
|
|
|
|
|
|
VideoData* video = mVideoQueue.PeekFront();
|
|
|
|
if (video && !video->mKeyframe) {
|
|
|
|
// First decoded frame isn't a keyframe, seek back to previous keyframe,
|
|
|
|
// otherwise we'll get visual artifacts.
|
|
|
|
NS_ASSERTION(video->mTimecode != -1, "Must have a granulepos");
|
|
|
|
int shift = mTheoraState->mInfo.keyframe_granule_shift;
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t keyframeGranulepos = (video->mTimecode >> shift) << shift;
|
|
|
|
int64_t keyframeTime = mTheoraState->StartTime(keyframeGranulepos);
|
2012-05-31 11:13:17 -07:00
|
|
|
SEEK_LOG(PR_LOG_DEBUG, ("Keyframe for %lld is at %lld, seeking back to it",
|
|
|
|
video->mTime, keyframeTime));
|
|
|
|
aAdjustedTarget = NS_MIN(aAdjustedTarget, keyframeTime);
|
2010-08-19 15:50:37 -07:00
|
|
|
}
|
2012-05-31 11:13:17 -07:00
|
|
|
}
|
|
|
|
if (aAdjustedTarget < aTarget) {
|
2011-03-23 15:28:58 -07:00
|
|
|
SeekRange k = SelectSeekRange(aRanges,
|
2012-05-31 11:13:17 -07:00
|
|
|
aAdjustedTarget,
|
2011-03-23 15:28:58 -07:00
|
|
|
aStartTime,
|
|
|
|
aEndTime,
|
2011-09-29 16:34:37 -07:00
|
|
|
false);
|
2012-05-31 11:13:17 -07:00
|
|
|
res = SeekBisection(aAdjustedTarget, k, SEEK_FUZZ_USECS);
|
2010-08-19 15:50:37 -07:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult OggReader::SeekInUnbuffered(int64_t aTarget,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aStartTime,
|
|
|
|
int64_t aEndTime,
|
2011-03-23 15:28:58 -07:00
|
|
|
const nsTArray<SeekRange>& aRanges)
|
2010-08-19 15:50:37 -07:00
|
|
|
{
|
2011-07-13 14:24:35 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Seeking in unbuffered data to %lld using bisection search", mDecoder, aTarget));
|
2010-08-19 15:50:37 -07:00
|
|
|
|
|
|
|
// If we've got an active Theora bitstream, determine the maximum possible
|
2011-04-13 15:12:23 -07:00
|
|
|
// time in usecs which a keyframe could be before a given interframe. We
|
2010-08-19 15:50:37 -07:00
|
|
|
// subtract this from our seek target, seek to the new target, and then
|
|
|
|
// will decode forward to the original seek target. We should encounter a
|
|
|
|
// keyframe in that interval. This prevents us from needing to run two
|
|
|
|
// bisections; one for the seek target frame, and another to find its
|
|
|
|
// keyframe. It's usually faster to just download this extra data, rather
|
|
|
|
// tham perform two bisections to find the seek target's keyframe. We
|
|
|
|
// don't do this offsetting when seeking in a buffered range,
|
|
|
|
// as the extra decoding causes a noticeable speed hit when all the data
|
|
|
|
// is buffered (compared to just doing a bisection to exactly find the
|
|
|
|
// keyframe).
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t keyframeOffsetMs = 0;
|
2010-08-19 15:50:37 -07:00
|
|
|
if (HasVideo() && mTheoraState) {
|
|
|
|
keyframeOffsetMs = mTheoraState->MaxKeyframeOffset();
|
|
|
|
}
|
2012-05-31 11:13:17 -07:00
|
|
|
// Add in the Opus pre-roll if necessary, as well.
|
|
|
|
if (HasAudio() && mOpusState) {
|
|
|
|
keyframeOffsetMs = NS_MAX(keyframeOffsetMs, SEEK_OPUS_PREROLL);
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t seekTarget = NS_MAX(aStartTime, aTarget - keyframeOffsetMs);
|
2010-08-19 15:50:37 -07:00
|
|
|
// Minimize the bisection search space using the known timestamps from the
|
|
|
|
// buffered ranges.
|
2011-09-29 16:34:37 -07:00
|
|
|
SeekRange k = SelectSeekRange(aRanges, seekTarget, aStartTime, aEndTime, false);
|
2011-05-07 23:24:09 -07:00
|
|
|
return SeekBisection(seekTarget, k, SEEK_FUZZ_USECS);
|
2010-08-19 15:50:37 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult OggReader::Seek(int64_t aTarget,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aStartTime,
|
|
|
|
int64_t aEndTime,
|
|
|
|
int64_t aCurrentTime)
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
2011-07-11 20:39:25 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2012-11-30 05:17:54 -08:00
|
|
|
if (mIsChained)
|
|
|
|
return NS_ERROR_FAILURE;
|
2011-07-13 14:24:35 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p About to seek to %lld", mDecoder, aTarget));
|
2010-08-19 15:50:37 -07:00
|
|
|
nsresult res;
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(resource != nullptr, NS_ERROR_FAILURE);
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t adjustedTarget = aTarget;
|
2012-05-31 11:13:17 -07:00
|
|
|
if (HasAudio() && mOpusState){
|
|
|
|
adjustedTarget = NS_MAX(aStartTime, aTarget - SEEK_OPUS_PREROLL);
|
|
|
|
}
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2012-05-31 11:13:17 -07:00
|
|
|
if (adjustedTarget == aStartTime) {
|
2010-08-19 15:50:37 -07:00
|
|
|
// We've seeked to the media start. Just seek to the offset of the first
|
|
|
|
// content page.
|
2012-02-14 20:35:01 -08:00
|
|
|
res = resource->Seek(nsISeekableStream::NS_SEEK_SET, 0);
|
2010-08-19 15:50:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
mPageOffset = 0;
|
2012-05-30 21:03:14 -07:00
|
|
|
res = ResetDecode(true);
|
2010-08-19 15:50:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
NS_ASSERTION(aStartTime != -1, "mStartTime should be known");
|
|
|
|
{
|
2011-07-11 20:39:28 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2010-05-05 19:31:02 -07:00
|
|
|
mDecoder->UpdatePlaybackPosition(aStartTime);
|
|
|
|
}
|
|
|
|
} else {
|
2012-05-31 11:13:17 -07:00
|
|
|
// TODO: This may seek back unnecessarily far in the video, but we don't
|
|
|
|
// have a way of asking Skeleton to seek to a different target for each
|
|
|
|
// stream yet. Using adjustedTarget here is at least correct, if slow.
|
|
|
|
IndexedSeekResult sres = SeekToKeyframeUsingIndex(adjustedTarget);
|
2010-08-19 15:50:37 -07:00
|
|
|
NS_ENSURE_TRUE(sres != SEEK_FATAL_ERROR, NS_ERROR_FAILURE);
|
|
|
|
if (sres == SEEK_INDEX_FAIL) {
|
|
|
|
// No index or other non-fatal index-related failure. Try to seek
|
|
|
|
// using a bisection search. Determine the already downloaded data
|
|
|
|
// in the media cache, so we can try to seek in the cached data first.
|
2011-03-23 15:28:58 -07:00
|
|
|
nsAutoTArray<SeekRange, 16> ranges;
|
|
|
|
res = GetSeekRanges(ranges);
|
2010-08-19 15:50:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2010-08-19 15:50:37 -07:00
|
|
|
// Figure out if the seek target lies in a buffered range.
|
2011-09-29 16:34:37 -07:00
|
|
|
SeekRange r = SelectSeekRange(ranges, aTarget, aStartTime, aEndTime, true);
|
2010-08-17 15:49:14 -07:00
|
|
|
|
2010-08-19 15:50:37 -07:00
|
|
|
if (!r.IsNull()) {
|
|
|
|
// We know the buffered range in which the seek target lies, do a
|
|
|
|
// bisection search in that buffered range.
|
2012-05-31 11:13:17 -07:00
|
|
|
res = SeekInBufferedRange(aTarget, adjustedTarget, aStartTime, aEndTime, ranges, r);
|
2010-08-19 15:50:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
|
|
|
} else {
|
|
|
|
// The target doesn't lie in a buffered range. Perform a bisection
|
|
|
|
// search over the whole media, using the known buffered ranges to
|
|
|
|
// reduce the search space.
|
|
|
|
res = SeekInUnbuffered(aTarget, aStartTime, aEndTime, ranges);
|
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
2010-08-17 18:48:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-19 15:50:37 -07:00
|
|
|
|
|
|
|
// The decode position must now be either close to the seek target, or
|
|
|
|
// we've seeked to before the keyframe before the seek target. Decode
|
|
|
|
// forward to the seek target frame.
|
2010-08-12 19:28:15 -07:00
|
|
|
return DecodeToTarget(aTarget);
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
// Reads a page from the media resource.
|
2010-05-05 19:31:02 -07:00
|
|
|
static PageSyncResult
|
2012-02-14 20:35:01 -08:00
|
|
|
PageSync(MediaResource* aResource,
|
2010-08-05 00:40:35 -07:00
|
|
|
ogg_sync_state* aState,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aCachedDataOnly,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aEndOffset,
|
2010-05-05 19:31:02 -07:00
|
|
|
ogg_page* aPage,
|
|
|
|
int& aSkippedBytes)
|
|
|
|
{
|
|
|
|
aSkippedBytes = 0;
|
|
|
|
// Sync to the next page.
|
|
|
|
int ret = 0;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bytesRead = 0;
|
|
|
|
int64_t readHead = aOffset;
|
2010-05-05 19:31:02 -07:00
|
|
|
while (ret <= 0) {
|
|
|
|
ret = ogg_sync_pageseek(aState, aPage);
|
|
|
|
if (ret == 0) {
|
|
|
|
char* buffer = ogg_sync_buffer(aState, PAGE_STEP);
|
|
|
|
NS_ASSERTION(buffer, "Must have a buffer");
|
|
|
|
|
|
|
|
// Read from the file into the buffer
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t bytesToRead = NS_MIN(static_cast<int64_t>(PAGE_STEP),
|
2011-03-31 21:18:43 -07:00
|
|
|
aEndOffset - readHead);
|
2012-09-27 23:57:33 -07:00
|
|
|
NS_ASSERTION(bytesToRead <= UINT32_MAX, "bytesToRead range check");
|
2010-05-05 19:31:02 -07:00
|
|
|
if (bytesToRead <= 0) {
|
|
|
|
return PAGE_SYNC_END_OF_RANGE;
|
|
|
|
}
|
2010-08-05 00:40:35 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (aCachedDataOnly) {
|
2012-02-14 20:35:01 -08:00
|
|
|
rv = aResource->ReadFromCache(buffer, readHead,
|
2012-08-22 08:56:38 -07:00
|
|
|
static_cast<uint32_t>(bytesToRead));
|
2010-08-05 00:40:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv,PAGE_SYNC_ERROR);
|
2012-08-22 08:56:38 -07:00
|
|
|
bytesRead = static_cast<uint32_t>(bytesToRead);
|
2010-08-05 00:40:35 -07:00
|
|
|
} else {
|
2012-02-14 20:35:01 -08:00
|
|
|
rv = aResource->Seek(nsISeekableStream::NS_SEEK_SET, readHead);
|
2010-08-05 00:40:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv,PAGE_SYNC_ERROR);
|
2012-02-14 20:35:01 -08:00
|
|
|
rv = aResource->Read(buffer,
|
2012-08-22 08:56:38 -07:00
|
|
|
static_cast<uint32_t>(bytesToRead),
|
2012-02-14 20:35:01 -08:00
|
|
|
&bytesRead);
|
2010-08-05 00:40:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv,PAGE_SYNC_ERROR);
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
if (bytesRead == 0 && NS_SUCCEEDED(rv)) {
|
|
|
|
// End of file.
|
|
|
|
return PAGE_SYNC_END_OF_RANGE;
|
|
|
|
}
|
2010-08-05 00:40:35 -07:00
|
|
|
readHead += bytesRead;
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
// Update the synchronisation layer with the number
|
|
|
|
// of bytes written to the buffer
|
|
|
|
ret = ogg_sync_wrote(aState, bytesRead);
|
|
|
|
NS_ENSURE_TRUE(ret == 0, PAGE_SYNC_ERROR);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
NS_ASSERTION(aSkippedBytes >= 0, "Offset >= 0");
|
|
|
|
aSkippedBytes += -ret;
|
|
|
|
NS_ASSERTION(aSkippedBytes >= 0, "Offset >= 0");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PAGE_SYNC_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult OggReader::SeekBisection(int64_t aTarget,
|
2011-03-23 15:28:58 -07:00
|
|
|
const SeekRange& aRange,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFuzz)
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
2011-07-11 20:39:25 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2010-08-05 00:40:35 -07:00
|
|
|
nsresult res;
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
if (aTarget == aRange.mTimeStart) {
|
|
|
|
if (NS_FAILED(ResetDecode())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-02-14 20:35:01 -08:00
|
|
|
res = resource->Seek(nsISeekableStream::NS_SEEK_SET, 0);
|
2010-08-19 15:50:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
2011-05-07 23:24:09 -07:00
|
|
|
mPageOffset = 0;
|
2010-05-05 19:31:02 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bisection search, find start offset of last page with end time less than
|
|
|
|
// the seek target.
|
|
|
|
ogg_int64_t startOffset = aRange.mOffsetStart;
|
|
|
|
ogg_int64_t startTime = aRange.mTimeStart;
|
2011-02-15 14:08:31 -08:00
|
|
|
ogg_int64_t startLength = 0; // Length of the page at startOffset.
|
2010-05-05 19:31:02 -07:00
|
|
|
ogg_int64_t endOffset = aRange.mOffsetEnd;
|
|
|
|
ogg_int64_t endTime = aRange.mTimeEnd;
|
|
|
|
|
|
|
|
ogg_int64_t seekTarget = aTarget;
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t seekLowerBound = NS_MAX(static_cast<int64_t>(0), aTarget - aFuzz);
|
2010-05-05 19:31:02 -07:00
|
|
|
int hops = 0;
|
2012-03-28 06:14:33 -07:00
|
|
|
DebugOnly<ogg_int64_t> previousGuess = -1;
|
2011-02-15 14:08:31 -08:00
|
|
|
int backsteps = 0;
|
2010-05-05 19:31:02 -07:00
|
|
|
const int maxBackStep = 10;
|
2012-09-27 23:57:33 -07:00
|
|
|
NS_ASSERTION(static_cast<uint64_t>(PAGE_STEP) * pow(2.0, maxBackStep) < INT32_MAX,
|
2010-05-05 19:31:02 -07:00
|
|
|
"Backstep calculation must not overflow");
|
2011-02-15 14:08:31 -08:00
|
|
|
|
|
|
|
// Seek via bisection search. Loop until we find the offset where the page
|
|
|
|
// before the offset is before the seek target, and the page after the offset
|
|
|
|
// is after the seek target.
|
2011-09-29 16:34:37 -07:00
|
|
|
while (true) {
|
2010-05-05 19:31:02 -07:00
|
|
|
ogg_int64_t duration = 0;
|
|
|
|
double target = 0;
|
|
|
|
ogg_int64_t interval = 0;
|
|
|
|
ogg_int64_t guess = 0;
|
|
|
|
ogg_page page;
|
|
|
|
int skippedBytes = 0;
|
|
|
|
ogg_int64_t pageOffset = 0;
|
|
|
|
ogg_int64_t pageLength = 0;
|
|
|
|
ogg_int64_t granuleTime = -1;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mustBackoff = false;
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
// Guess where we should bisect to, based on the bit rate and the time
|
2011-02-15 14:08:31 -08:00
|
|
|
// remaining in the interval. Loop until we can determine the time at
|
|
|
|
// the guess offset.
|
2011-09-29 16:34:37 -07:00
|
|
|
while (true) {
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
// Discard any previously buffered packets/pages.
|
|
|
|
if (NS_FAILED(ResetDecode())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-02-15 14:08:31 -08:00
|
|
|
interval = endOffset - startOffset - startLength;
|
|
|
|
if (interval == 0) {
|
|
|
|
// Our interval is empty, we've found the optimal seek point, as the
|
|
|
|
// page at the start offset is before the seek target, and the page
|
|
|
|
// at the end offset is after the seek target.
|
|
|
|
SEEK_LOG(PR_LOG_DEBUG, ("Interval narrowed, terminating bisection."));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
// Guess bisection point.
|
|
|
|
duration = endTime - startTime;
|
|
|
|
target = (double)(seekTarget - startTime) / (double)duration;
|
|
|
|
guess = startOffset + startLength +
|
2011-02-15 14:08:31 -08:00
|
|
|
static_cast<ogg_int64_t>((double)interval * target);
|
2010-05-05 19:31:02 -07:00
|
|
|
guess = NS_MIN(guess, endOffset - PAGE_STEP);
|
2011-02-15 14:08:31 -08:00
|
|
|
if (mustBackoff) {
|
|
|
|
// We previously failed to determine the time at the guess offset,
|
|
|
|
// probably because we ran out of data to decode. This usually happens
|
|
|
|
// when we guess very close to the end offset. So reduce the guess
|
|
|
|
// offset using an exponential backoff until we determine the time.
|
|
|
|
SEEK_LOG(PR_LOG_DEBUG, ("Backing off %d bytes, backsteps=%d",
|
2012-08-22 08:56:38 -07:00
|
|
|
static_cast<int32_t>(PAGE_STEP * pow(2.0, backsteps)), backsteps));
|
2011-03-23 15:28:58 -07:00
|
|
|
guess -= PAGE_STEP * static_cast<ogg_int64_t>(pow(2.0, backsteps));
|
2011-07-06 14:51:26 -07:00
|
|
|
|
|
|
|
if (guess <= startOffset) {
|
|
|
|
// We've tried to backoff to before the start offset of our seek
|
|
|
|
// range. This means we couldn't find a seek termination position
|
|
|
|
// near the end of the seek range, so just set the seek termination
|
|
|
|
// condition, and break out of the bisection loop. We'll begin
|
|
|
|
// decoding from the start of the seek range.
|
|
|
|
interval = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-02-15 14:08:31 -08:00
|
|
|
backsteps = NS_MIN(backsteps + 1, maxBackStep);
|
|
|
|
// We reset mustBackoff. If we still need to backoff further, it will
|
2011-09-29 16:34:37 -07:00
|
|
|
// be set to true again.
|
|
|
|
mustBackoff = false;
|
2011-02-15 14:08:31 -08:00
|
|
|
} else {
|
|
|
|
backsteps = 0;
|
|
|
|
}
|
2010-05-05 19:31:02 -07:00
|
|
|
guess = NS_MAX(guess, startOffset + startLength);
|
|
|
|
|
2011-02-15 14:08:31 -08:00
|
|
|
SEEK_LOG(PR_LOG_DEBUG, ("Seek loop start[o=%lld..%lld t=%lld] "
|
|
|
|
"end[o=%lld t=%lld] "
|
|
|
|
"interval=%lld target=%lf guess=%lld",
|
|
|
|
startOffset, (startOffset+startLength), startTime,
|
|
|
|
endOffset, endTime, interval, target, guess));
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(guess >= startOffset + startLength, "Guess must be after range start");
|
|
|
|
NS_ASSERTION(guess < endOffset, "Guess must be before range end");
|
2011-03-31 21:18:43 -07:00
|
|
|
NS_ASSERTION(guess != previousGuess, "Guess should be different to previous");
|
2010-05-05 19:31:02 -07:00
|
|
|
previousGuess = guess;
|
|
|
|
|
|
|
|
hops++;
|
|
|
|
|
2010-08-05 00:40:35 -07:00
|
|
|
// Locate the next page after our seek guess, and then figure out the
|
|
|
|
// granule time of the audio and video bitstreams there. We can then
|
|
|
|
// make a bisection decision based on our location in the media.
|
2012-02-14 20:35:01 -08:00
|
|
|
PageSyncResult res = PageSync(resource,
|
2010-08-05 00:40:35 -07:00
|
|
|
&mOggState,
|
2011-09-29 16:34:37 -07:00
|
|
|
false,
|
2010-08-05 00:40:35 -07:00
|
|
|
guess,
|
2010-05-05 19:31:02 -07:00
|
|
|
endOffset,
|
|
|
|
&page,
|
|
|
|
skippedBytes);
|
2010-08-19 15:50:37 -07:00
|
|
|
NS_ENSURE_TRUE(res != PAGE_SYNC_ERROR, NS_ERROR_FAILURE);
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
// We've located a page of length |ret| at |guess + skippedBytes|.
|
|
|
|
// Remember where the page is located.
|
|
|
|
pageOffset = guess + skippedBytes;
|
|
|
|
pageLength = page.header_len + page.body_len;
|
|
|
|
mPageOffset = pageOffset + pageLength;
|
|
|
|
|
2011-02-15 14:08:31 -08:00
|
|
|
if (res == PAGE_SYNC_END_OF_RANGE) {
|
2010-05-05 19:31:02 -07:00
|
|
|
// Our guess was too close to the end, we've ended up reading the end
|
|
|
|
// page. Backoff exponentially from the end point, in case the last
|
|
|
|
// page/frame/sample is huge.
|
2011-09-29 16:34:37 -07:00
|
|
|
mustBackoff = true;
|
2011-02-15 14:08:31 -08:00
|
|
|
SEEK_LOG(PR_LOG_DEBUG, ("Hit the end of range, backing off"));
|
2010-05-05 19:31:02 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read pages until we can determine the granule time of the audio and
|
|
|
|
// video bitstream.
|
|
|
|
ogg_int64_t audioTime = -1;
|
|
|
|
ogg_int64_t videoTime = -1;
|
2011-02-15 14:08:31 -08:00
|
|
|
do {
|
2010-05-05 19:31:02 -07:00
|
|
|
// Add the page to its codec state, determine its granule time.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t serial = ogg_page_serialno(&page);
|
2012-11-14 11:46:40 -08:00
|
|
|
OggCodecState* codecState = nullptr;
|
2010-05-05 19:31:02 -07:00
|
|
|
mCodecStates.Get(serial, &codecState);
|
|
|
|
if (codecState && codecState->mActive) {
|
2011-02-15 14:08:31 -08:00
|
|
|
int ret = ogg_stream_pagein(&codecState->mState, &page);
|
2010-05-05 19:31:02 -07:00
|
|
|
NS_ENSURE_TRUE(ret == 0, NS_ERROR_FAILURE);
|
2011-02-15 14:08:31 -08:00
|
|
|
}
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
ogg_int64_t granulepos = ogg_page_granulepos(&page);
|
|
|
|
|
2012-05-01 17:29:34 -07:00
|
|
|
if (HasAudio() && granulepos > 0 && audioTime == -1) {
|
|
|
|
if (mVorbisState && serial == mVorbisState->mSerial) {
|
|
|
|
audioTime = mVorbisState->Time(granulepos);
|
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
} else if (mOpusState && serial == mOpusState->mSerial) {
|
|
|
|
audioTime = mOpusState->Time(granulepos);
|
|
|
|
#endif
|
|
|
|
}
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (HasVideo() &&
|
2011-07-06 14:50:44 -07:00
|
|
|
granulepos > 0 &&
|
2010-05-05 19:31:02 -07:00
|
|
|
serial == mTheoraState->mSerial &&
|
|
|
|
videoTime == -1) {
|
|
|
|
videoTime = mTheoraState->StartTime(granulepos);
|
|
|
|
}
|
|
|
|
|
2011-02-15 14:08:31 -08:00
|
|
|
if (mPageOffset == endOffset) {
|
|
|
|
// Hit end of readable data.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
if (ReadOggPage(&page) == -1) {
|
|
|
|
break;
|
|
|
|
}
|
2011-02-15 14:08:31 -08:00
|
|
|
|
2012-05-31 11:13:11 -07:00
|
|
|
} while ((HasAudio() && audioTime == -1) ||
|
|
|
|
(HasVideo() && videoTime == -1));
|
2011-02-15 14:08:31 -08:00
|
|
|
|
|
|
|
NS_ASSERTION(mPageOffset <= endOffset, "Page read cursor should be inside range");
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
if ((HasAudio() && audioTime == -1) ||
|
|
|
|
(HasVideo() && videoTime == -1))
|
|
|
|
{
|
2011-02-15 14:08:31 -08:00
|
|
|
// We don't have timestamps for all active tracks...
|
|
|
|
if (pageOffset == startOffset + startLength && mPageOffset == endOffset) {
|
|
|
|
// We read the entire interval without finding timestamps for all
|
|
|
|
// active tracks. We know the interval start offset is before the seek
|
|
|
|
// target, and the interval end is after the seek target, and we can't
|
|
|
|
// terminate inside the interval, so we terminate the seek at the
|
|
|
|
// start of the interval.
|
|
|
|
interval = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should backoff; cause the guess to back off from the end, so
|
|
|
|
// that we've got more room to capture.
|
2011-09-29 16:34:37 -07:00
|
|
|
mustBackoff = true;
|
2010-05-05 19:31:02 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've found appropriate time stamps here. Proceed to bisect
|
|
|
|
// the search space.
|
|
|
|
granuleTime = NS_MAX(audioTime, videoTime);
|
|
|
|
NS_ASSERTION(granuleTime > 0, "Must get a granuletime");
|
|
|
|
break;
|
2011-02-15 14:08:31 -08:00
|
|
|
} // End of "until we determine time at guess offset" loop.
|
2010-05-05 19:31:02 -07:00
|
|
|
|
|
|
|
if (interval == 0) {
|
|
|
|
// Seek termination condition; we've found the page boundary of the
|
|
|
|
// last page before the target, and the first page after the target.
|
2011-02-15 14:08:31 -08:00
|
|
|
SEEK_LOG(PR_LOG_DEBUG, ("Terminating seek at offset=%lld", startOffset));
|
2010-05-05 19:31:02 -07:00
|
|
|
NS_ASSERTION(startTime < aTarget, "Start time must always be less than target");
|
2012-02-14 20:35:01 -08:00
|
|
|
res = resource->Seek(nsISeekableStream::NS_SEEK_SET, startOffset);
|
2010-08-19 15:50:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
2010-05-05 19:31:02 -07:00
|
|
|
mPageOffset = startOffset;
|
|
|
|
if (NS_FAILED(ResetDecode())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:24:35 -07:00
|
|
|
SEEK_LOG(PR_LOG_DEBUG, ("Time at offset %lld is %lld", guess, granuleTime));
|
2010-05-05 19:31:02 -07:00
|
|
|
if (granuleTime < seekTarget && granuleTime > seekLowerBound) {
|
|
|
|
// We're within the fuzzy region in which we want to terminate the search.
|
2012-02-14 20:35:01 -08:00
|
|
|
res = resource->Seek(nsISeekableStream::NS_SEEK_SET, pageOffset);
|
2010-08-05 00:40:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
2011-02-15 14:08:31 -08:00
|
|
|
mPageOffset = pageOffset;
|
2010-05-05 19:31:02 -07:00
|
|
|
if (NS_FAILED(ResetDecode())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-02-15 14:08:31 -08:00
|
|
|
SEEK_LOG(PR_LOG_DEBUG, ("Terminating seek at offset=%lld", mPageOffset));
|
2010-05-05 19:31:02 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (granuleTime >= seekTarget) {
|
|
|
|
// We've landed after the seek target.
|
2010-07-11 04:08:36 -07:00
|
|
|
NS_ASSERTION(pageOffset < endOffset, "offset_end must decrease");
|
2010-05-05 19:31:02 -07:00
|
|
|
endOffset = pageOffset;
|
|
|
|
endTime = granuleTime;
|
|
|
|
} else if (granuleTime < seekTarget) {
|
|
|
|
// Landed before seek target.
|
2011-07-13 14:24:35 -07:00
|
|
|
NS_ASSERTION(pageOffset >= startOffset + startLength,
|
|
|
|
"Bisection point should be at or after end of first page in interval");
|
2010-05-05 19:31:02 -07:00
|
|
|
startOffset = pageOffset;
|
|
|
|
startLength = pageLength;
|
|
|
|
startTime = granuleTime;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(startTime < seekTarget, "Must be before seek target");
|
|
|
|
NS_ASSERTION(endTime >= seekTarget, "End must be after seek target");
|
|
|
|
}
|
|
|
|
|
|
|
|
SEEK_LOG(PR_LOG_DEBUG, ("Seek complete in %d bisections.", hops));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult OggReader::GetBuffered(nsTimeRanges* aBuffered, int64_t aStartTime)
|
2010-08-05 00:40:35 -07:00
|
|
|
{
|
2012-11-30 05:17:54 -08:00
|
|
|
{
|
|
|
|
mozilla::ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
if (mIsChained)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-09-02 15:56:29 -07:00
|
|
|
#ifdef OGG_ESTIMATE_BUFFERED
|
|
|
|
MediaResource* stream = mDecoder->GetResource();
|
|
|
|
int64_t durationUs = 0;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2012-11-19 07:11:21 -08:00
|
|
|
durationUs = mDecoder->GetMediaDuration();
|
2012-09-02 15:56:29 -07:00
|
|
|
}
|
|
|
|
GetEstimatedBufferedTimeRanges(stream, durationUs, aBuffered);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
#else
|
2010-09-09 20:29:06 -07:00
|
|
|
// HasAudio and HasVideo are not used here as they take a lock and cause
|
|
|
|
// a deadlock. Accessing mInfo doesn't require a lock - it doesn't change
|
2012-06-05 22:58:07 -07:00
|
|
|
// after metadata is read.
|
2010-09-09 20:29:06 -07:00
|
|
|
if (!mInfo.mHasVideo && !mInfo.mHasAudio) {
|
|
|
|
// No need to search through the file if there are no audio or video tracks
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
|
|
|
nsTArray<MediaByteRange> ranges;
|
|
|
|
nsresult res = resource->GetCachedRanges(ranges);
|
2011-03-23 15:28:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-08-05 00:40:35 -07:00
|
|
|
|
|
|
|
// Traverse across the buffered byte ranges, determining the time ranges
|
2012-02-14 20:35:01 -08:00
|
|
|
// they contain. MediaResource::GetNextCachedData(offset) returns -1 when
|
|
|
|
// offset is after the end of the media resource, or there's no more cached
|
2010-08-05 00:40:35 -07:00
|
|
|
// data after the offset. This loop will run until we've checked every
|
|
|
|
// buffered range in the media, in increasing order of offset.
|
2011-05-08 14:10:28 -07:00
|
|
|
nsAutoOggSyncState sync;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < ranges.Length(); index++) {
|
2011-03-23 15:28:58 -07:00
|
|
|
// Ensure the offsets are after the header pages.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t startOffset = ranges[index].mStart;
|
|
|
|
int64_t endOffset = ranges[index].mEnd;
|
2011-03-23 15:28:58 -07:00
|
|
|
|
|
|
|
// Because the granulepos time is actually the end time of the page,
|
2011-05-07 23:24:09 -07:00
|
|
|
// we special-case (startOffset == 0) so that the first
|
2011-03-23 15:28:58 -07:00
|
|
|
// buffered range always appears to be buffered from the media start
|
|
|
|
// time, rather than from the end-time of the first page.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t startTime = (startOffset == 0) ? aStartTime : -1;
|
2011-03-23 15:28:58 -07:00
|
|
|
|
|
|
|
// Find the start time of the range. Read pages until we find one with a
|
|
|
|
// granulepos which we can convert into a timestamp to use as the time of
|
|
|
|
// the start of the buffered range.
|
2011-05-08 14:10:28 -07:00
|
|
|
ogg_sync_reset(&sync.mState);
|
2010-08-05 00:40:35 -07:00
|
|
|
while (startTime == -1) {
|
|
|
|
ogg_page page;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t discard;
|
2012-02-14 20:35:01 -08:00
|
|
|
PageSyncResult res = PageSync(resource,
|
2011-05-08 14:10:28 -07:00
|
|
|
&sync.mState,
|
2011-09-29 16:34:37 -07:00
|
|
|
true,
|
2010-08-05 00:40:35 -07:00
|
|
|
startOffset,
|
|
|
|
endOffset,
|
|
|
|
&page,
|
|
|
|
discard);
|
|
|
|
if (res == PAGE_SYNC_ERROR) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
} else if (res == PAGE_SYNC_END_OF_RANGE) {
|
|
|
|
// Hit the end of range without reading a page, give up trying to
|
|
|
|
// find a start time for this buffered range, skip onto the next one.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t granulepos = ogg_page_granulepos(&page);
|
2010-08-05 00:40:35 -07:00
|
|
|
if (granulepos == -1) {
|
|
|
|
// Page doesn't have an end time, advance to the next page
|
|
|
|
// until we find one.
|
|
|
|
startOffset += page.header_len + page.body_len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t serial = ogg_page_serialno(&page);
|
2011-02-07 18:45:20 -08:00
|
|
|
if (mVorbisState && serial == mVorbisSerial) {
|
2012-11-14 11:46:40 -08:00
|
|
|
startTime = VorbisState::Time(&mVorbisInfo, granulepos);
|
2010-12-16 17:39:01 -08:00
|
|
|
NS_ASSERTION(startTime > 0, "Must have positive start time");
|
|
|
|
}
|
2012-05-31 11:13:11 -07:00
|
|
|
else if (mOpusState && serial == mOpusSerial) {
|
2012-11-14 11:46:40 -08:00
|
|
|
startTime = OpusState::Time(mOpusPreSkip, granulepos);
|
2012-05-31 11:13:11 -07:00
|
|
|
NS_ASSERTION(startTime > 0, "Must have positive start time");
|
|
|
|
}
|
2011-02-07 18:45:20 -08:00
|
|
|
else if (mTheoraState && serial == mTheoraSerial) {
|
2012-11-14 11:46:40 -08:00
|
|
|
startTime = TheoraState::Time(&mTheoraInfo, granulepos);
|
2010-08-05 00:40:35 -07:00
|
|
|
NS_ASSERTION(startTime > 0, "Must have positive start time");
|
|
|
|
}
|
2010-12-16 17:39:01 -08:00
|
|
|
else if (IsKnownStream(serial)) {
|
|
|
|
// Stream is not the theora or vorbis stream we're playing,
|
|
|
|
// but is one that we have header data for.
|
2010-09-09 20:29:06 -07:00
|
|
|
startOffset += page.header_len + page.body_len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Page is for a stream we don't know about (possibly a chained
|
2012-07-31 18:24:25 -07:00
|
|
|
// ogg), return OK to abort the finding any further ranges. This
|
|
|
|
// prevents us searching through the rest of the media when we
|
|
|
|
// may not be able to extract timestamps from it.
|
2012-11-30 05:17:54 -08:00
|
|
|
SetChained(true);
|
2012-07-31 18:24:25 -07:00
|
|
|
return NS_OK;
|
2010-09-09 20:29:06 -07:00
|
|
|
}
|
2010-08-05 00:40:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (startTime != -1) {
|
|
|
|
// We were able to find a start time for that range, see if we can
|
|
|
|
// find an end time.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t endTime = RangeEndTime(startOffset, endOffset, true);
|
2010-08-05 00:40:35 -07:00
|
|
|
if (endTime != -1) {
|
2011-06-30 05:25:27 -07:00
|
|
|
aBuffered->Add((startTime - aStartTime) / static_cast<double>(USECS_PER_S),
|
2011-04-13 15:12:23 -07:00
|
|
|
(endTime - aStartTime) / static_cast<double>(USECS_PER_S));
|
2010-08-05 00:40:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-05 00:40:35 -07:00
|
|
|
return NS_OK;
|
2012-09-02 15:56:29 -07:00
|
|
|
#endif
|
2010-08-05 00:40:35 -07:00
|
|
|
}
|
2010-12-16 17:39:01 -08:00
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
bool OggReader::IsKnownStream(uint32_t aSerial)
|
2010-12-16 17:39:01 -08:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mKnownStreams.Length(); i++) {
|
|
|
|
uint32_t serial = mKnownStreams[i];
|
2010-12-16 17:39:01 -08:00
|
|
|
if (serial == aSerial) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return true;
|
2010-12-16 17:39:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-12-16 17:39:01 -08:00
|
|
|
}
|
2012-11-14 11:45:33 -08:00
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|