2014-09-11 15:47:06 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-09-03 18:57:06 -07:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "TrackBuffer.h"
|
|
|
|
|
2014-09-16 23:33:00 -07:00
|
|
|
#include "ContainerParser.h"
|
2015-01-26 07:01:09 -08:00
|
|
|
#include "MediaData.h"
|
2014-09-03 18:57:06 -07:00
|
|
|
#include "MediaSourceDecoder.h"
|
|
|
|
#include "SharedThreadPool.h"
|
|
|
|
#include "MediaTaskQueue.h"
|
|
|
|
#include "SourceBufferDecoder.h"
|
|
|
|
#include "SourceBufferResource.h"
|
|
|
|
#include "VideoUtils.h"
|
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2014-12-17 15:39:34 -08:00
|
|
|
#include "mozilla/Preferences.h"
|
2014-09-03 18:57:06 -07:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
extern PRLogModuleInfo* GetMediaSourceLog();
|
|
|
|
|
2015-02-11 23:52:13 -08:00
|
|
|
#define MSE_DEBUG(arg, ...) PR_LOG(GetMediaSourceLog(), PR_LOG_DEBUG, ("TrackBuffer(%p:%s)::%s: " arg, this, mType.get(), __func__, ##__VA_ARGS__))
|
2014-09-03 18:57:06 -07:00
|
|
|
#else
|
|
|
|
#define MSE_DEBUG(...)
|
|
|
|
#endif
|
|
|
|
|
2015-01-15 19:14:56 -08:00
|
|
|
// Time in seconds to substract from the current time when deciding the
|
|
|
|
// time point to evict data before in a decoder. This is used to help
|
2015-01-24 02:45:58 -08:00
|
|
|
// prevent evicting the current playback point.
|
2015-01-15 19:14:56 -08:00
|
|
|
#define MSE_EVICT_THRESHOLD_TIME 2.0
|
|
|
|
|
2015-02-09 04:29:00 -08:00
|
|
|
// Time in microsecond under which a timestamp will be considered to be 0.
|
|
|
|
#define FUZZ_TIMESTAMP_OFFSET 100000
|
|
|
|
|
2015-02-12 21:52:42 -08:00
|
|
|
#define EOS_FUZZ_US 125000
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
TrackBuffer::TrackBuffer(MediaSourceDecoder* aParentDecoder, const nsACString& aType)
|
|
|
|
: mParentDecoder(aParentDecoder)
|
|
|
|
, mType(aType)
|
|
|
|
, mLastStartTimestamp(0)
|
2015-01-07 15:58:55 -08:00
|
|
|
, mLastTimestampOffset(0)
|
2015-02-09 04:29:00 -08:00
|
|
|
, mAdjustedTimestamp(0)
|
2014-12-18 04:59:00 -08:00
|
|
|
, mShutdown(false)
|
2014-09-03 18:57:06 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(TrackBuffer);
|
2014-09-16 23:33:00 -07:00
|
|
|
mParser = ContainerParser::CreateForMIMEType(aType);
|
2014-09-03 18:57:06 -07:00
|
|
|
mTaskQueue = new MediaTaskQueue(GetMediaDecodeThreadPool());
|
|
|
|
aParentDecoder->AddTrackBuffer(this);
|
2014-12-17 15:39:34 -08:00
|
|
|
mDecoderPerSegment = Preferences::GetBool("media.mediasource.decoder-per-segment", false);
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("TrackBuffer created for parent decoder %p", aParentDecoder);
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TrackBuffer::~TrackBuffer()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(TrackBuffer);
|
|
|
|
}
|
|
|
|
|
2014-12-08 20:24:55 -08:00
|
|
|
class MOZ_STACK_CLASS DecodersToInitialize MOZ_FINAL {
|
2014-11-05 20:09:19 -08:00
|
|
|
public:
|
|
|
|
explicit DecodersToInitialize(TrackBuffer* aOwner)
|
|
|
|
: mOwner(aOwner)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~DecodersToInitialize()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < mDecoders.Length(); i++) {
|
|
|
|
mOwner->QueueInitializeDecoder(mDecoders[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 15:58:55 -08:00
|
|
|
bool NewDecoder(int64_t aTimestampOffset)
|
2014-11-05 20:09:19 -08:00
|
|
|
{
|
2015-01-07 15:58:55 -08:00
|
|
|
nsRefPtr<SourceBufferDecoder> decoder = mOwner->NewDecoder(aTimestampOffset);
|
2014-11-05 20:09:19 -08:00
|
|
|
if (!decoder) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mDecoders.AppendElement(decoder);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-04 01:20:15 -08:00
|
|
|
size_t Length()
|
|
|
|
{
|
|
|
|
return mDecoders.Length();
|
|
|
|
}
|
|
|
|
|
2015-02-01 16:41:43 -08:00
|
|
|
void AppendElement(SourceBufferDecoder* aDecoder)
|
|
|
|
{
|
|
|
|
mDecoders.AppendElement(aDecoder);
|
|
|
|
}
|
|
|
|
|
2014-11-05 20:09:19 -08:00
|
|
|
private:
|
|
|
|
TrackBuffer* mOwner;
|
2015-02-01 16:41:43 -08:00
|
|
|
nsAutoTArray<nsRefPtr<SourceBufferDecoder>,1> mDecoders;
|
2014-11-05 20:09:19 -08:00
|
|
|
};
|
|
|
|
|
2014-12-09 11:43:21 -08:00
|
|
|
nsRefPtr<ShutdownPromise>
|
2014-09-03 18:57:06 -07:00
|
|
|
TrackBuffer::Shutdown()
|
|
|
|
{
|
2014-12-18 04:59:00 -08:00
|
|
|
mParentDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
mShutdown = true;
|
2015-02-04 01:20:16 -08:00
|
|
|
mInitializationPromise.RejectIfExists(NS_ERROR_ABORT, __func__);
|
2014-12-18 04:59:00 -08:00
|
|
|
|
2014-12-09 11:43:21 -08:00
|
|
|
MOZ_ASSERT(mShutdownPromise.IsEmpty());
|
|
|
|
nsRefPtr<ShutdownPromise> p = mShutdownPromise.Ensure(__func__);
|
|
|
|
|
|
|
|
RefPtr<MediaTaskQueue> queue = mTaskQueue;
|
2014-09-03 18:57:06 -07:00
|
|
|
mTaskQueue = nullptr;
|
2014-12-09 11:43:21 -08:00
|
|
|
queue->BeginShutdown()
|
|
|
|
->Then(mParentDecoder->GetReader()->GetTaskQueue(), __func__, this,
|
|
|
|
&TrackBuffer::ContinueShutdown, &TrackBuffer::ContinueShutdown);
|
2014-09-03 18:57:06 -07:00
|
|
|
|
2014-12-09 11:43:21 -08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 14:22:23 -08:00
|
|
|
TrackBuffer::ContinueShutdown()
|
2014-12-09 11:43:21 -08:00
|
|
|
{
|
2014-09-03 18:57:06 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2014-12-09 11:43:21 -08:00
|
|
|
if (mDecoders.Length()) {
|
|
|
|
mDecoders[0]->GetReader()->Shutdown()
|
|
|
|
->Then(mParentDecoder->GetReader()->GetTaskQueue(), __func__, this,
|
|
|
|
&TrackBuffer::ContinueShutdown, &TrackBuffer::ContinueShutdown);
|
|
|
|
mShutdownDecoders.AppendElement(mDecoders[0]);
|
|
|
|
mDecoders.RemoveElementAt(0);
|
|
|
|
return;
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
2014-12-09 11:43:21 -08:00
|
|
|
|
2015-02-10 14:30:39 -08:00
|
|
|
MOZ_ASSERT(!mCurrentDecoder, "Detach() should have been called");
|
2014-09-04 00:35:01 -07:00
|
|
|
mInitializedDecoders.Clear();
|
2014-09-03 18:57:06 -07:00
|
|
|
mParentDecoder = nullptr;
|
2014-12-09 11:43:21 -08:00
|
|
|
|
|
|
|
mShutdownPromise.Resolve(true, __func__);
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
|
2015-02-04 01:20:16 -08:00
|
|
|
nsRefPtr<TrackBufferAppendPromise>
|
2015-01-26 07:01:09 -08:00
|
|
|
TrackBuffer::AppendData(LargeDataBuffer* aData, int64_t aTimestampOffset)
|
2014-09-16 23:33:00 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-02-04 01:20:16 -08:00
|
|
|
MOZ_ASSERT(mInitializationPromise.IsEmpty());
|
|
|
|
|
2014-11-05 20:09:19 -08:00
|
|
|
DecodersToInitialize decoders(this);
|
2015-02-04 01:20:16 -08:00
|
|
|
nsRefPtr<TrackBufferAppendPromise> p = mInitializationPromise.Ensure(__func__);
|
2015-02-01 16:41:43 -08:00
|
|
|
bool hadInitData = mParser->HasInitData();
|
|
|
|
bool hadCompleteInitData = mParser->HasCompleteInitData();
|
|
|
|
nsRefPtr<LargeDataBuffer> oldInit = mParser->InitData();
|
|
|
|
bool newInitData = mParser->IsInitSegmentPresent(aData);
|
2015-02-04 01:20:15 -08:00
|
|
|
|
2014-09-16 23:33:00 -07:00
|
|
|
// TODO: Run more of the buffer append algorithm asynchronously.
|
2015-02-01 16:41:43 -08:00
|
|
|
if (newInitData) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("New initialization segment.");
|
2015-02-01 16:41:43 -08:00
|
|
|
} else if (!hadInitData) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("Non-init segment appended during initialization.");
|
2015-02-04 01:20:15 -08:00
|
|
|
mInitializationPromise.Reject(NS_ERROR_FAILURE, __func__);
|
|
|
|
return p;
|
2014-09-16 23:33:00 -07:00
|
|
|
}
|
|
|
|
|
2015-02-04 01:20:14 -08:00
|
|
|
int64_t start = 0, end = 0;
|
2015-02-01 16:41:43 -08:00
|
|
|
bool gotMedia = mParser->ParseStartAndEndTimestamps(aData, start, end);
|
|
|
|
bool gotInit = mParser->HasCompleteInitData();
|
|
|
|
|
|
|
|
if (newInitData) {
|
|
|
|
if (!gotInit) {
|
|
|
|
// We need a new decoder, but we can't initialize it yet.
|
2015-02-09 04:29:00 -08:00
|
|
|
nsRefPtr<SourceBufferDecoder> decoder =
|
2015-02-10 22:40:03 -08:00
|
|
|
NewDecoder(aTimestampOffset);
|
2015-02-01 16:41:43 -08:00
|
|
|
// The new decoder is stored in mDecoders/mCurrentDecoder, so we
|
|
|
|
// don't need to do anything with 'decoder'. It's only a placeholder.
|
|
|
|
if (!decoder) {
|
|
|
|
mInitializationPromise.Reject(NS_ERROR_FAILURE, __func__);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
} else {
|
2015-02-10 22:40:03 -08:00
|
|
|
if (!decoders.NewDecoder(aTimestampOffset)) {
|
2015-02-01 16:41:43 -08:00
|
|
|
mInitializationPromise.Reject(NS_ERROR_FAILURE, __func__);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!hadCompleteInitData && gotInit) {
|
|
|
|
MOZ_ASSERT(mCurrentDecoder);
|
|
|
|
// Queue pending decoder for initialization now that we have a full
|
|
|
|
// init segment.
|
|
|
|
decoders.AppendElement(mCurrentDecoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gotMedia) {
|
2015-02-10 22:40:10 -08:00
|
|
|
if (mParser->IsMediaSegmentPresent(aData) && mLastEndTimestamp &&
|
2014-12-17 15:39:34 -08:00
|
|
|
(!mParser->TimestampsFuzzyEqual(start, mLastEndTimestamp.value()) ||
|
2015-01-07 15:58:55 -08:00
|
|
|
mLastTimestampOffset != aTimestampOffset ||
|
2015-02-01 16:41:43 -08:00
|
|
|
mDecoderPerSegment ||
|
|
|
|
(mCurrentDecoder && mCurrentDecoder->WasTrimmed()))) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("Data last=[%lld, %lld] overlaps [%lld, %lld]",
|
|
|
|
mLastStartTimestamp, mLastEndTimestamp.value(), start, end);
|
2014-09-16 23:33:00 -07:00
|
|
|
|
2015-02-01 16:41:43 -08:00
|
|
|
if (!newInitData) {
|
|
|
|
// This data is earlier in the timeline than data we have already
|
|
|
|
// processed or not continuous, so we must create a new decoder
|
|
|
|
// to handle the decoding.
|
2015-02-09 04:29:00 -08:00
|
|
|
if (!hadCompleteInitData ||
|
2015-02-10 22:40:03 -08:00
|
|
|
!decoders.NewDecoder(aTimestampOffset)) {
|
2015-02-01 16:41:43 -08:00
|
|
|
mInitializationPromise.Reject(NS_ERROR_FAILURE, __func__);
|
|
|
|
return p;
|
|
|
|
}
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("Decoder marked as initialized.");
|
2015-02-04 01:20:16 -08:00
|
|
|
AppendDataToCurrentResource(oldInit, 0);
|
2014-09-16 23:33:00 -07:00
|
|
|
}
|
|
|
|
mLastStartTimestamp = start;
|
2014-11-13 20:12:48 -08:00
|
|
|
} else {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("Segment last=[%lld, %lld] [%lld, %lld]",
|
|
|
|
mLastStartTimestamp,
|
|
|
|
mLastEndTimestamp ? mLastEndTimestamp.value() : 0, start, end);
|
2014-09-16 23:33:00 -07:00
|
|
|
}
|
2014-11-13 20:12:48 -08:00
|
|
|
mLastEndTimestamp.reset();
|
|
|
|
mLastEndTimestamp.emplace(end);
|
2014-09-16 23:33:00 -07:00
|
|
|
}
|
|
|
|
|
2015-02-10 22:40:06 -08:00
|
|
|
if (gotMedia && start != mAdjustedTimestamp &&
|
|
|
|
((start < 0 && -start < FUZZ_TIMESTAMP_OFFSET && start < mAdjustedTimestamp) ||
|
|
|
|
(start > 0 && (start < FUZZ_TIMESTAMP_OFFSET || start < mAdjustedTimestamp)))) {
|
2015-02-09 04:29:00 -08:00
|
|
|
AdjustDecodersTimestampOffset(mAdjustedTimestamp - start);
|
|
|
|
mAdjustedTimestamp = start;
|
|
|
|
}
|
|
|
|
|
2015-01-26 07:01:09 -08:00
|
|
|
if (!AppendDataToCurrentResource(aData, end - start)) {
|
2015-02-04 01:20:15 -08:00
|
|
|
mInitializationPromise.Reject(NS_ERROR_FAILURE, __func__);
|
|
|
|
return p;
|
2014-09-16 23:33:00 -07:00
|
|
|
}
|
|
|
|
|
2015-02-04 01:20:15 -08:00
|
|
|
if (decoders.Length()) {
|
2015-02-01 16:41:43 -08:00
|
|
|
// We're going to have to wait for the decoder to initialize, the promise
|
|
|
|
// will be resolved once initialization completes.
|
2015-02-04 01:20:15 -08:00
|
|
|
return p;
|
|
|
|
}
|
2015-02-10 22:40:10 -08:00
|
|
|
|
|
|
|
// Tell our reader that we have more data to ensure that playback starts if
|
|
|
|
// required when data is appended.
|
|
|
|
mParentDecoder->GetReader()->MaybeNotifyHaveData();
|
|
|
|
|
2015-02-01 16:41:43 -08:00
|
|
|
mInitializationPromise.Resolve(gotMedia, __func__);
|
2015-02-04 01:20:15 -08:00
|
|
|
return p;
|
2014-09-16 23:33:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-01-26 07:01:09 -08:00
|
|
|
TrackBuffer::AppendDataToCurrentResource(LargeDataBuffer* aData, uint32_t aDuration)
|
2014-09-03 18:57:06 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!mCurrentDecoder) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-26 23:25:44 -08:00
|
|
|
SourceBufferResource* resource = mCurrentDecoder->GetResource();
|
|
|
|
int64_t appendOffset = resource->GetLength();
|
|
|
|
resource->AppendData(aData);
|
2015-01-24 02:45:58 -08:00
|
|
|
mCurrentDecoder->SetRealMediaDuration(mCurrentDecoder->GetRealMediaDuration() + aDuration);
|
2015-01-26 23:25:44 -08:00
|
|
|
// XXX: For future reference: NDA call must run on the main thread.
|
|
|
|
mCurrentDecoder->NotifyDataArrived(reinterpret_cast<const char*>(aData->Elements()),
|
|
|
|
aData->Length(), appendOffset);
|
2014-12-09 13:45:06 -08:00
|
|
|
mParentDecoder->NotifyBytesDownloaded();
|
2014-09-07 04:54:00 -07:00
|
|
|
mParentDecoder->NotifyTimeRangesChanged();
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-10 13:49:54 -08:00
|
|
|
class DecoderSorter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool LessThan(SourceBufferDecoder* aFirst, SourceBufferDecoder* aSecond) const
|
|
|
|
{
|
|
|
|
nsRefPtr<dom::TimeRanges> first = new dom::TimeRanges();
|
|
|
|
aFirst->GetBuffered(first);
|
|
|
|
|
|
|
|
nsRefPtr<dom::TimeRanges> second = new dom::TimeRanges();
|
|
|
|
aSecond->GetBuffered(second);
|
|
|
|
|
|
|
|
return first->GetStartTime() < second->GetStartTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Equals(SourceBufferDecoder* aFirst, SourceBufferDecoder* aSecond) const
|
|
|
|
{
|
|
|
|
nsRefPtr<dom::TimeRanges> first = new dom::TimeRanges();
|
|
|
|
aFirst->GetBuffered(first);
|
|
|
|
|
|
|
|
nsRefPtr<dom::TimeRanges> second = new dom::TimeRanges();
|
|
|
|
aSecond->GetBuffered(second);
|
|
|
|
|
|
|
|
return first->GetStartTime() == second->GetStartTime();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
bool
|
2015-01-15 19:14:56 -08:00
|
|
|
TrackBuffer::EvictData(double aPlaybackTime,
|
|
|
|
uint32_t aThreshold,
|
|
|
|
double* aBufferStartTime)
|
2014-09-03 18:57:06 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-03 14:16:34 -08:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2014-09-09 02:12:00 -07:00
|
|
|
|
2015-01-15 19:14:56 -08:00
|
|
|
if (!mCurrentDecoder) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-19 17:19:13 -08:00
|
|
|
int64_t totalSize = GetSize();
|
2014-09-09 02:12:00 -07:00
|
|
|
|
|
|
|
int64_t toEvict = totalSize - aThreshold;
|
2015-01-11 18:35:35 -08:00
|
|
|
if (toEvict <= 0 || mInitializedDecoders.IsEmpty()) {
|
2014-09-09 02:12:00 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-12 21:52:42 -08:00
|
|
|
// Get a list of initialized decoders.
|
2014-12-10 13:49:54 -08:00
|
|
|
nsTArray<SourceBufferDecoder*> decoders;
|
|
|
|
decoders.AppendElements(mInitializedDecoders);
|
|
|
|
|
2014-12-10 13:50:41 -08:00
|
|
|
// First try to evict data before the current play position, starting
|
2015-02-12 21:52:42 -08:00
|
|
|
// with the oldest decoder.
|
|
|
|
for (uint32_t i = 0; i < decoders.Length() && toEvict > 0; ++i) {
|
2015-01-15 19:14:56 -08:00
|
|
|
nsRefPtr<dom::TimeRanges> buffered = new dom::TimeRanges();
|
|
|
|
decoders[i]->GetBuffered(buffered);
|
|
|
|
|
2015-02-23 21:38:41 -08:00
|
|
|
MSE_DEBUG("Step1. decoder=%u/%u threshold=%u toEvict=%lld",
|
2015-02-12 21:52:42 -08:00
|
|
|
i, decoders.Length(), aThreshold, toEvict);
|
|
|
|
|
|
|
|
// To ensure we don't evict data past the current playback position
|
|
|
|
// we apply a threshold of a few seconds back and evict data up to
|
|
|
|
// that point.
|
|
|
|
if (aPlaybackTime > MSE_EVICT_THRESHOLD_TIME) {
|
|
|
|
double time = aPlaybackTime - MSE_EVICT_THRESHOLD_TIME;
|
2015-02-23 21:38:41 -08:00
|
|
|
bool isActive = decoders[i] == mCurrentDecoder ||
|
|
|
|
mParentDecoder->IsActiveReader(decoders[i]->GetReader());
|
|
|
|
if (!isActive && buffered->GetEndTime() < time) {
|
|
|
|
// The entire decoder is contained before our current playback time.
|
|
|
|
// It can be fully evicted.
|
|
|
|
MSE_DEBUG("evicting all bufferedEnd=%f "
|
|
|
|
"aPlaybackTime=%f time=%f, size=%lld",
|
|
|
|
buffered->GetEndTime(), aPlaybackTime, time,
|
|
|
|
decoders[i]->GetResource()->GetSize());
|
|
|
|
toEvict -= decoders[i]->GetResource()->EvictAll();
|
|
|
|
} else {
|
|
|
|
int64_t playbackOffset = decoders[i]->ConvertToByteOffset(time);
|
|
|
|
MSE_DEBUG("evicting some bufferedEnd=%f "
|
|
|
|
"aPlaybackTime=%f time=%f, playbackOffset=%lld size=%lld",
|
|
|
|
buffered->GetEndTime(), aPlaybackTime, time,
|
|
|
|
playbackOffset, decoders[i]->GetResource()->GetSize());
|
|
|
|
if (playbackOffset > 0) {
|
|
|
|
toEvict -= decoders[i]->GetResource()->EvictData(playbackOffset,
|
|
|
|
playbackOffset);
|
|
|
|
}
|
2015-01-15 19:14:56 -08:00
|
|
|
}
|
2014-12-10 13:50:41 -08:00
|
|
|
}
|
|
|
|
}
|
2014-12-10 13:50:41 -08:00
|
|
|
|
2015-02-12 21:52:42 -08:00
|
|
|
// Evict all data from decoders we've likely already read from.
|
|
|
|
for (uint32_t i = 0; i < decoders.Length() && toEvict > 0; ++i) {
|
2015-02-23 21:38:41 -08:00
|
|
|
MSE_DEBUG("Step2. decoder=%u/%u threshold=%u toEvict=%lld",
|
|
|
|
i, decoders.Length(), aThreshold, toEvict);
|
2015-02-12 21:52:42 -08:00
|
|
|
if (mParentDecoder->IsActiveReader(decoders[i]->GetReader())) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (decoders[i] == mCurrentDecoder) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-15 19:14:56 -08:00
|
|
|
nsRefPtr<dom::TimeRanges> buffered = new dom::TimeRanges();
|
|
|
|
decoders[i]->GetBuffered(buffered);
|
2015-02-12 21:52:42 -08:00
|
|
|
|
|
|
|
// Remove data from older decoders than the current one.
|
|
|
|
MSE_DEBUG("evicting all "
|
|
|
|
"bufferedStart=%f bufferedEnd=%f aPlaybackTime=%f size=%lld",
|
|
|
|
buffered->GetStartTime(), buffered->GetEndTime(),
|
|
|
|
aPlaybackTime, decoders[i]->GetResource()->GetSize());
|
|
|
|
toEvict -= decoders[i]->GetResource()->EvictAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evict all data from future decoders, starting furthest away from
|
|
|
|
// current playback position.
|
|
|
|
// We will ignore the currently playing decoder and the one playing after that
|
|
|
|
// in order to ensure we give enough time to the DASH player to re-buffer
|
|
|
|
// as necessary.
|
|
|
|
// TODO: This step should be done using RangeRemoval:
|
|
|
|
// Something like: RangeRemoval(aPlaybackTime + 60s, End);
|
|
|
|
|
|
|
|
// Find the reader currently being played with.
|
|
|
|
SourceBufferDecoder* playingDecoder = nullptr;
|
|
|
|
for (uint32_t i = 0; i < decoders.Length() && toEvict > 0; ++i) {
|
|
|
|
if (mParentDecoder->IsActiveReader(decoders[i]->GetReader())) {
|
|
|
|
playingDecoder = decoders[i];
|
|
|
|
break;
|
2015-01-15 19:14:56 -08:00
|
|
|
}
|
2015-02-12 21:52:42 -08:00
|
|
|
}
|
|
|
|
// Find the next decoder we're likely going to play with.
|
|
|
|
nsRefPtr<SourceBufferDecoder> nextPlayingDecoder = nullptr;
|
|
|
|
if (playingDecoder) {
|
|
|
|
nsRefPtr<dom::TimeRanges> buffered = new dom::TimeRanges();
|
|
|
|
playingDecoder->GetBuffered(buffered);
|
|
|
|
nextPlayingDecoder =
|
|
|
|
mParentDecoder->SelectDecoder(buffered->GetEndTime() * USECS_PER_S + 1,
|
|
|
|
EOS_FUZZ_US,
|
|
|
|
mInitializedDecoders);
|
|
|
|
}
|
2014-12-10 13:50:41 -08:00
|
|
|
|
2015-02-12 21:52:42 -08:00
|
|
|
// Sort decoders by their start times.
|
|
|
|
decoders.Sort(DecoderSorter());
|
|
|
|
|
|
|
|
for (int32_t i = int32_t(decoders.Length()) - 1; i >= 0 && toEvict > 0; --i) {
|
2015-02-23 21:38:41 -08:00
|
|
|
MSE_DEBUG("Step3. decoder=%u/%u threshold=%u toEvict=%lld",
|
|
|
|
i, decoders.Length(), aThreshold, toEvict);
|
2015-02-12 21:52:42 -08:00
|
|
|
if (decoders[i] == playingDecoder || decoders[i] == nextPlayingDecoder ||
|
|
|
|
decoders[i] == mCurrentDecoder) {
|
|
|
|
continue;
|
2014-12-10 13:50:41 -08:00
|
|
|
}
|
2015-02-12 21:52:42 -08:00
|
|
|
nsRefPtr<dom::TimeRanges> buffered = new dom::TimeRanges();
|
|
|
|
decoders[i]->GetBuffered(buffered);
|
|
|
|
|
|
|
|
MSE_DEBUG("evicting all "
|
|
|
|
"bufferedStart=%f bufferedEnd=%f aPlaybackTime=%f size=%lld",
|
|
|
|
buffered->GetStartTime(), buffered->GetEndTime(),
|
|
|
|
aPlaybackTime, decoders[i]->GetResource()->GetSize());
|
|
|
|
toEvict -= decoders[i]->GetResource()->EvictAll();
|
2014-12-10 13:50:41 -08:00
|
|
|
}
|
2015-01-15 19:14:56 -08:00
|
|
|
|
2015-02-12 21:52:42 -08:00
|
|
|
RemoveEmptyDecoders(decoders);
|
|
|
|
|
2015-01-15 19:14:56 -08:00
|
|
|
bool evicted = toEvict < (totalSize - aThreshold);
|
|
|
|
if (evicted) {
|
2015-01-20 19:17:49 -08:00
|
|
|
nsRefPtr<dom::TimeRanges> ranges = new dom::TimeRanges();
|
2015-01-15 19:14:56 -08:00
|
|
|
mCurrentDecoder->GetBuffered(ranges);
|
|
|
|
*aBufferStartTime = std::max(0.0, ranges->GetStartTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
return evicted;
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
|
2015-02-12 21:52:42 -08:00
|
|
|
void
|
|
|
|
TrackBuffer::RemoveEmptyDecoders(nsTArray<mozilla::SourceBufferDecoder*>& aDecoders)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
|
|
|
|
// Remove decoders that have no data in them
|
|
|
|
for (uint32_t i = 0; i < aDecoders.Length(); ++i) {
|
|
|
|
nsRefPtr<dom::TimeRanges> buffered = new dom::TimeRanges();
|
|
|
|
aDecoders[i]->GetBuffered(buffered);
|
|
|
|
MSE_DEBUG("maybe remove empty decoders=%d "
|
|
|
|
"size=%lld start=%f end=%f",
|
|
|
|
i, aDecoders[i]->GetResource()->GetSize(),
|
|
|
|
buffered->GetStartTime(), buffered->GetEndTime());
|
|
|
|
if (aDecoders[i] == mCurrentDecoder ||
|
|
|
|
mParentDecoder->IsActiveReader(aDecoders[i]->GetReader())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aDecoders[i]->GetResource()->GetSize() == 0 ||
|
|
|
|
buffered->GetStartTime() < 0.0 ||
|
|
|
|
buffered->GetEndTime() < 0.0) {
|
|
|
|
MSE_DEBUG("remove empty decoders=%d", i);
|
|
|
|
RemoveDecoder(aDecoders[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 17:19:13 -08:00
|
|
|
int64_t
|
|
|
|
TrackBuffer::GetSize()
|
|
|
|
{
|
|
|
|
int64_t totalSize = 0;
|
|
|
|
for (uint32_t i = 0; i < mInitializedDecoders.Length(); ++i) {
|
|
|
|
totalSize += mInitializedDecoders[i]->GetResource()->GetSize();
|
|
|
|
}
|
|
|
|
return totalSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TrackBuffer::HasOnlyIncompleteMedia()
|
|
|
|
{
|
|
|
|
if (!mCurrentDecoder) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsRefPtr<dom::TimeRanges> buffered = new dom::TimeRanges();
|
|
|
|
mCurrentDecoder->GetBuffered(buffered);
|
2015-02-23 21:38:41 -08:00
|
|
|
MSE_DEBUG("mCurrentDecoder.size=%lld, start=%f end=%f",
|
|
|
|
mCurrentDecoder->GetResource()->GetSize(),
|
|
|
|
buffered->GetStartTime(), buffered->GetEndTime());
|
2015-02-19 17:19:13 -08:00
|
|
|
return mCurrentDecoder->GetResource()->GetSize() && !buffered->Length();
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
void
|
|
|
|
TrackBuffer::EvictBefore(double aTime)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-03 14:16:34 -08:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
for (uint32_t i = 0; i < mInitializedDecoders.Length(); ++i) {
|
|
|
|
int64_t endOffset = mInitializedDecoders[i]->ConvertToByteOffset(aTime);
|
2014-09-09 02:12:00 -07:00
|
|
|
if (endOffset > 0) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("decoder=%u offset=%lld",
|
|
|
|
i, endOffset);
|
2015-01-26 23:25:44 -08:00
|
|
|
mInitializedDecoders[i]->GetResource()->EvictBefore(endOffset);
|
2014-09-09 02:12:00 -07:00
|
|
|
}
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
TrackBuffer::Buffered(dom::TimeRanges* aRanges)
|
|
|
|
{
|
2014-09-04 00:35:01 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
double highestEndTime = 0;
|
|
|
|
|
2015-02-01 16:41:43 -08:00
|
|
|
for (uint32_t i = 0; i < mInitializedDecoders.Length(); ++i) {
|
2014-09-03 18:57:06 -07:00
|
|
|
nsRefPtr<dom::TimeRanges> r = new dom::TimeRanges();
|
2015-02-01 16:41:43 -08:00
|
|
|
mInitializedDecoders[i]->GetBuffered(r);
|
2014-09-03 18:57:06 -07:00
|
|
|
if (r->Length() > 0) {
|
|
|
|
highestEndTime = std::max(highestEndTime, r->GetEndTime());
|
2014-12-10 13:52:57 -08:00
|
|
|
aRanges->Union(r, double(mParser->GetRoundingError()) / USECS_PER_S);
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return highestEndTime;
|
|
|
|
}
|
|
|
|
|
2014-11-05 20:09:19 -08:00
|
|
|
already_AddRefed<SourceBufferDecoder>
|
2015-01-07 15:58:55 -08:00
|
|
|
TrackBuffer::NewDecoder(int64_t aTimestampOffset)
|
2014-09-03 18:57:06 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-09-16 23:33:00 -07:00
|
|
|
MOZ_ASSERT(mParentDecoder);
|
|
|
|
|
2015-02-04 01:20:16 -08:00
|
|
|
DiscardCurrentDecoder();
|
2014-09-03 18:57:06 -07:00
|
|
|
|
2015-02-10 22:40:03 -08:00
|
|
|
nsRefPtr<SourceBufferDecoder> decoder =
|
|
|
|
mParentDecoder->CreateSubDecoder(mType, aTimestampOffset - mAdjustedTimestamp);
|
2014-09-03 18:57:06 -07:00
|
|
|
if (!decoder) {
|
2014-11-05 20:09:19 -08:00
|
|
|
return nullptr;
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
mCurrentDecoder = decoder;
|
2014-09-04 00:35:01 -07:00
|
|
|
mDecoders.AppendElement(decoder);
|
2014-09-03 18:57:06 -07:00
|
|
|
|
|
|
|
mLastStartTimestamp = 0;
|
2014-11-13 20:12:48 -08:00
|
|
|
mLastEndTimestamp.reset();
|
2015-01-07 15:58:55 -08:00
|
|
|
mLastTimestampOffset = aTimestampOffset;
|
2014-09-03 18:57:06 -07:00
|
|
|
|
2014-10-13 15:05:00 -07:00
|
|
|
decoder->SetTaskQueue(mTaskQueue);
|
2014-11-05 20:09:19 -08:00
|
|
|
return decoder.forget();
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-10-01 22:04:06 -07:00
|
|
|
TrackBuffer::QueueInitializeDecoder(SourceBufferDecoder* aDecoder)
|
2014-09-03 18:57:06 -07:00
|
|
|
{
|
2014-12-14 18:30:20 -08:00
|
|
|
if (NS_WARN_IF(!mTaskQueue)) {
|
2015-02-04 01:20:15 -08:00
|
|
|
mInitializationPromise.RejectIfExists(NS_ERROR_FAILURE, __func__);
|
2014-12-14 18:30:20 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
RefPtr<nsIRunnable> task =
|
2014-10-01 22:04:06 -07:00
|
|
|
NS_NewRunnableMethodWithArg<SourceBufferDecoder*>(this,
|
|
|
|
&TrackBuffer::InitializeDecoder,
|
|
|
|
aDecoder);
|
2014-09-03 18:57:06 -07:00
|
|
|
if (NS_FAILED(mTaskQueue->Dispatch(task))) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("failed to enqueue decoder initialization task");
|
2014-10-01 22:04:06 -07:00
|
|
|
RemoveDecoder(aDecoder);
|
2015-02-04 01:20:15 -08:00
|
|
|
mInitializationPromise.RejectIfExists(NS_ERROR_FAILURE, __func__);
|
2014-09-03 18:57:06 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-10-01 22:04:06 -07:00
|
|
|
TrackBuffer::InitializeDecoder(SourceBufferDecoder* aDecoder)
|
2014-09-03 18:57:06 -07:00
|
|
|
{
|
2015-02-04 01:20:16 -08:00
|
|
|
if (!mParentDecoder) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("decoder was shutdown. Aborting initialization.");
|
2015-02-04 01:20:16 -08:00
|
|
|
return;
|
|
|
|
}
|
2014-12-18 04:59:00 -08:00
|
|
|
// ReadMetadata may block the thread waiting on data, so we must be able
|
|
|
|
// to leave the monitor while we call it. For the rest of this function
|
|
|
|
// we want to hold the monitor though, since we run on a different task queue
|
|
|
|
// from the reader and interact heavily with it.
|
2014-12-19 00:00:07 -08:00
|
|
|
mParentDecoder->GetReentrantMonitor().AssertNotCurrentThreadIn();
|
2014-12-18 04:59:00 -08:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
|
2015-02-04 01:20:16 -08:00
|
|
|
if (mCurrentDecoder != aDecoder) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("append was cancelled. Aborting initialization.");
|
2015-02-04 01:20:16 -08:00
|
|
|
// If we reached this point, the SourceBuffer would have disconnected
|
|
|
|
// the promise. So no need to reject it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-18 04:59:00 -08:00
|
|
|
// We may be shut down at any time by the reader on another thread. So we need
|
|
|
|
// to check for this each time we acquire the monitor. If that happens, we
|
|
|
|
// need to abort immediately, because the reader has forgotten about us, and
|
|
|
|
// important pieces of our state (like mTaskQueue) have also been torn down.
|
|
|
|
if (mShutdown) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("was shut down. Aborting initialization.");
|
2015-02-04 01:20:16 -08:00
|
|
|
RemoveDecoder(aDecoder);
|
2014-12-18 04:59:00 -08:00
|
|
|
return;
|
|
|
|
}
|
2014-09-03 18:57:06 -07:00
|
|
|
|
2014-12-18 04:59:00 -08:00
|
|
|
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
|
2014-09-03 18:57:06 -07:00
|
|
|
MediaDecoderReader* reader = aDecoder->GetReader();
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("Initializing subdecoder %p reader %p",
|
|
|
|
aDecoder, reader);
|
2014-09-03 18:57:06 -07:00
|
|
|
|
|
|
|
MediaInfo mi;
|
|
|
|
nsAutoPtr<MetadataTags> tags; // TODO: Handle metadata.
|
2014-12-18 04:59:00 -08:00
|
|
|
nsresult rv;
|
2015-02-01 16:41:43 -08:00
|
|
|
|
|
|
|
// HACK WARNING:
|
|
|
|
// We only reach this point once we know that we have a complete init segment.
|
|
|
|
// We don't want the demuxer to do a blocking read as no more data can be
|
|
|
|
// appended while this routine is running. Marking the SourceBufferResource
|
|
|
|
// as ended will cause any incomplete reads to abort.
|
|
|
|
// As this decoder hasn't been initialized yet, the resource isn't yet in use
|
|
|
|
// and so it is safe to do so.
|
|
|
|
bool wasEnded = aDecoder->GetResource()->IsEnded();
|
|
|
|
if (!wasEnded) {
|
|
|
|
aDecoder->GetResource()->Ended();
|
|
|
|
}
|
2014-12-18 04:59:00 -08:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoExit mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
rv = reader->ReadMetadata(&mi, getter_Transfers(tags));
|
|
|
|
}
|
2015-02-01 16:41:43 -08:00
|
|
|
if (!wasEnded) {
|
|
|
|
// Adding an empty buffer will reopen the SourceBufferResource
|
|
|
|
nsRefPtr<LargeDataBuffer> emptyBuffer = new LargeDataBuffer;
|
|
|
|
aDecoder->GetResource()->AppendData(emptyBuffer);
|
|
|
|
}
|
|
|
|
// HACK END.
|
2014-12-18 04:59:00 -08:00
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
reader->SetIdle();
|
2014-12-18 04:59:00 -08:00
|
|
|
if (mShutdown) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("was shut down while reading metadata. Aborting initialization.");
|
2014-12-18 04:59:00 -08:00
|
|
|
return;
|
|
|
|
}
|
2015-02-23 21:38:41 -08:00
|
|
|
if (mCurrentDecoder != aDecoder) {
|
|
|
|
MSE_DEBUG("append was cancelled. Aborting initialization.");
|
|
|
|
return;
|
|
|
|
}
|
2014-10-13 15:05:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && reader->IsWaitingOnCDMResource()) {
|
|
|
|
mWaitingDecoders.AppendElement(aDecoder);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aDecoder->SetTaskQueue(nullptr);
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
if (NS_FAILED(rv) || (!mi.HasVideo() && !mi.HasAudio())) {
|
|
|
|
// XXX: Need to signal error back to owning SourceBuffer.
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("Reader %p failed to initialize rv=%x audio=%d video=%d",
|
|
|
|
reader, rv, mi.HasAudio(), mi.HasVideo());
|
2014-09-09 19:21:17 -07:00
|
|
|
RemoveDecoder(aDecoder);
|
2015-02-04 01:20:15 -08:00
|
|
|
mInitializationPromise.RejectIfExists(NS_ERROR_FAILURE, __func__);
|
2014-09-03 18:57:06 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mi.HasVideo()) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("Reader %p video resolution=%dx%d",
|
|
|
|
reader, mi.mVideo.mDisplay.width, mi.mVideo.mDisplay.height);
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
if (mi.HasAudio()) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("Reader %p audio sampleRate=%d channels=%d",
|
|
|
|
reader, mi.mAudio.mRate, mi.mAudio.mChannels);
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
|
2015-02-04 01:20:15 -08:00
|
|
|
RefPtr<nsIRunnable> task =
|
|
|
|
NS_NewRunnableMethodWithArg<SourceBufferDecoder*>(this,
|
|
|
|
&TrackBuffer::CompleteInitializeDecoder,
|
|
|
|
aDecoder);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(task))) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("Failed to enqueue decoder initialization task");
|
2015-02-04 01:20:15 -08:00
|
|
|
RemoveDecoder(aDecoder);
|
|
|
|
mInitializationPromise.RejectIfExists(NS_ERROR_FAILURE, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TrackBuffer::CompleteInitializeDecoder(SourceBufferDecoder* aDecoder)
|
|
|
|
{
|
2015-02-04 01:20:16 -08:00
|
|
|
if (!mParentDecoder) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("was shutdown. Aborting initialization.");
|
2015-02-04 01:20:16 -08:00
|
|
|
return;
|
|
|
|
}
|
2015-02-04 01:20:15 -08:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2015-02-04 01:20:16 -08:00
|
|
|
if (mCurrentDecoder != aDecoder) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("append was cancelled. Aborting initialization.");
|
2015-02-04 01:20:16 -08:00
|
|
|
// If we reached this point, the SourceBuffer would have disconnected
|
|
|
|
// the promise. So no need to reject it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-04 01:20:15 -08:00
|
|
|
if (mShutdown) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("was shut down. Aborting initialization.");
|
2015-02-04 01:20:15 -08:00
|
|
|
RemoveDecoder(aDecoder);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-09 19:21:17 -07:00
|
|
|
if (!RegisterDecoder(aDecoder)) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("Reader %p not activated",
|
|
|
|
aDecoder->GetReader());
|
2014-09-09 19:21:17 -07:00
|
|
|
RemoveDecoder(aDecoder);
|
2015-02-04 01:20:15 -08:00
|
|
|
mInitializationPromise.RejectIfExists(NS_ERROR_FAILURE, __func__);
|
2014-09-09 19:21:17 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-12-22 00:20:31 -08:00
|
|
|
|
2015-02-04 01:20:15 -08:00
|
|
|
int64_t duration = aDecoder->GetMediaDuration();
|
|
|
|
if (!duration) {
|
|
|
|
// Treat a duration of 0 as infinity
|
|
|
|
duration = -1;
|
|
|
|
}
|
|
|
|
mParentDecoder->SetInitialDuration(duration);
|
|
|
|
|
2014-12-22 00:20:31 -08:00
|
|
|
// Tell our reader that we have more data to ensure that playback starts if
|
|
|
|
// required when data is appended.
|
|
|
|
mParentDecoder->GetReader()->MaybeNotifyHaveData();
|
|
|
|
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("Reader %p activated",
|
|
|
|
aDecoder->GetReader());
|
2015-02-04 01:20:15 -08:00
|
|
|
mInitializationPromise.ResolveIfExists(aDecoder->GetRealMediaDuration() > 0, __func__);
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
|
2014-09-09 19:21:17 -07:00
|
|
|
bool
|
|
|
|
TrackBuffer::ValidateTrackFormats(const MediaInfo& aInfo)
|
|
|
|
{
|
|
|
|
if (mInfo.HasAudio() != aInfo.HasAudio() ||
|
|
|
|
mInfo.HasVideo() != aInfo.HasVideo()) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("audio/video track mismatch");
|
2014-09-09 19:21:17 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Support dynamic audio format changes.
|
|
|
|
if (mInfo.HasAudio() &&
|
|
|
|
(mInfo.mAudio.mRate != aInfo.mAudio.mRate ||
|
|
|
|
mInfo.mAudio.mChannels != aInfo.mAudio.mChannels)) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("audio format mismatch");
|
2014-09-09 19:21:17 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-10-01 22:04:06 -07:00
|
|
|
TrackBuffer::RegisterDecoder(SourceBufferDecoder* aDecoder)
|
2014-09-03 18:57:06 -07:00
|
|
|
{
|
2014-12-18 04:59:00 -08:00
|
|
|
mParentDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2014-09-03 18:57:06 -07:00
|
|
|
const MediaInfo& info = aDecoder->GetReader()->GetMediaInfo();
|
|
|
|
// Initialize the track info since this is the first decoder.
|
2014-09-04 00:35:01 -07:00
|
|
|
if (mInitializedDecoders.IsEmpty()) {
|
2014-09-09 19:21:17 -07:00
|
|
|
mInfo = info;
|
|
|
|
mParentDecoder->OnTrackBufferConfigured(this, mInfo);
|
|
|
|
}
|
|
|
|
if (!ValidateTrackFormats(info)) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("mismatched audio/video tracks");
|
2014-09-09 19:21:17 -07:00
|
|
|
return false;
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
2014-09-04 00:35:01 -07:00
|
|
|
mInitializedDecoders.AppendElement(aDecoder);
|
2014-09-07 04:54:00 -07:00
|
|
|
mParentDecoder->NotifyTimeRangesChanged();
|
2014-09-09 19:21:17 -07:00
|
|
|
return true;
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-04 01:20:16 -08:00
|
|
|
TrackBuffer::DiscardCurrentDecoder()
|
2014-09-03 18:57:06 -07:00
|
|
|
{
|
2015-02-10 14:30:39 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-09-03 18:57:06 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2015-02-04 01:20:16 -08:00
|
|
|
EndCurrentDecoder();
|
2014-09-03 18:57:06 -07:00
|
|
|
mCurrentDecoder = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-16 04:48:25 -08:00
|
|
|
void
|
|
|
|
TrackBuffer::EndCurrentDecoder()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
if (mCurrentDecoder) {
|
2015-01-26 23:25:44 -08:00
|
|
|
mCurrentDecoder->GetResource()->Ended();
|
2015-01-16 04:48:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
void
|
|
|
|
TrackBuffer::Detach()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mCurrentDecoder) {
|
2015-02-04 01:20:16 -08:00
|
|
|
DiscardCurrentDecoder();
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TrackBuffer::HasInitSegment()
|
2014-09-18 13:10:15 -07:00
|
|
|
{
|
2014-09-03 18:57:06 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2015-02-01 16:41:43 -08:00
|
|
|
return mParser->HasCompleteInitData();
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TrackBuffer::IsReady()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2014-09-09 19:21:17 -07:00
|
|
|
MOZ_ASSERT((mInfo.HasAudio() || mInfo.HasVideo()) || mInitializedDecoders.IsEmpty());
|
2015-02-04 01:20:16 -08:00
|
|
|
return mInfo.HasAudio() || mInfo.HasVideo();
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-01-30 17:45:49 -08:00
|
|
|
TrackBuffer::ContainsTime(int64_t aTime, int64_t aTolerance)
|
2014-09-03 18:57:06 -07:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2014-09-04 00:35:01 -07:00
|
|
|
for (uint32_t i = 0; i < mInitializedDecoders.Length(); ++i) {
|
2014-09-03 18:57:06 -07:00
|
|
|
nsRefPtr<dom::TimeRanges> r = new dom::TimeRanges();
|
2014-09-04 00:35:01 -07:00
|
|
|
mInitializedDecoders[i]->GetBuffered(r);
|
2015-01-30 17:45:49 -08:00
|
|
|
if (r->Find(double(aTime) / USECS_PER_S,
|
|
|
|
double(aTolerance) / USECS_PER_S) != dom::TimeRanges::NoIndex) {
|
2014-09-03 18:57:06 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TrackBuffer::BreakCycles()
|
|
|
|
{
|
2014-10-09 21:58:50 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-12-09 11:43:21 -08:00
|
|
|
for (uint32_t i = 0; i < mShutdownDecoders.Length(); ++i) {
|
|
|
|
mShutdownDecoders[i]->BreakCycles();
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
2014-12-09 11:43:21 -08:00
|
|
|
mShutdownDecoders.Clear();
|
2014-10-09 21:22:41 -07:00
|
|
|
|
|
|
|
// These are cleared in Shutdown()
|
2014-12-09 11:43:21 -08:00
|
|
|
MOZ_ASSERT(!mDecoders.Length());
|
2014-10-09 21:22:41 -07:00
|
|
|
MOZ_ASSERT(mInitializedDecoders.IsEmpty());
|
|
|
|
MOZ_ASSERT(!mParentDecoder);
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TrackBuffer::ResetDecode()
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mDecoders.Length(); ++i) {
|
|
|
|
mDecoders[i]->GetReader()->ResetDecode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 04:48:25 -08:00
|
|
|
void
|
|
|
|
TrackBuffer::ResetParserState()
|
|
|
|
{
|
2015-02-04 01:20:16 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mParser->HasInitData() && !mParser->HasCompleteInitData()) {
|
|
|
|
// We have an incomplete init segment pending. reset current parser and
|
|
|
|
// discard the current decoder.
|
|
|
|
mParser = ContainerParser::CreateForMIMEType(mType);
|
2015-02-04 01:20:16 -08:00
|
|
|
DiscardCurrentDecoder();
|
2015-02-04 01:20:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-04 01:20:16 -08:00
|
|
|
TrackBuffer::AbortAppendData()
|
2015-02-04 01:20:16 -08:00
|
|
|
{
|
2015-02-04 01:20:16 -08:00
|
|
|
DiscardCurrentDecoder();
|
|
|
|
// The SourceBuffer would have disconnected its promise.
|
|
|
|
// However we must ensure that the MediaPromiseHolder handle all pending
|
|
|
|
// promises.
|
2015-02-04 01:20:16 -08:00
|
|
|
mInitializationPromise.RejectIfExists(NS_ERROR_ABORT, __func__);
|
2015-01-16 04:48:25 -08:00
|
|
|
}
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
const nsTArray<nsRefPtr<SourceBufferDecoder>>&
|
|
|
|
TrackBuffer::Decoders()
|
|
|
|
{
|
|
|
|
// XXX assert OnDecodeThread
|
2014-09-04 00:35:01 -07:00
|
|
|
return mInitializedDecoders;
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
|
2014-10-13 15:05:00 -07:00
|
|
|
#ifdef MOZ_EME
|
|
|
|
nsresult
|
|
|
|
TrackBuffer::SetCDMProxy(CDMProxy* aProxy)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mDecoders.Length(); ++i) {
|
|
|
|
nsresult rv = mDecoders[i]->SetCDMProxy(aProxy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mWaitingDecoders.Length(); ++i) {
|
|
|
|
CDMCaps::AutoLock caps(aProxy->Capabilites());
|
|
|
|
caps.CallOnMainThreadWhenCapsAvailable(
|
|
|
|
NS_NewRunnableMethodWithArg<SourceBufferDecoder*>(this,
|
|
|
|
&TrackBuffer::QueueInitializeDecoder,
|
|
|
|
mWaitingDecoders[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
mWaitingDecoders.Clear();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-11 15:47:06 -07:00
|
|
|
#if defined(DEBUG)
|
|
|
|
void
|
|
|
|
TrackBuffer::Dump(const char* aPath)
|
|
|
|
{
|
|
|
|
char path[255];
|
|
|
|
PR_snprintf(path, sizeof(path), "%s/trackbuffer-%p", aPath, this);
|
|
|
|
PR_MkDir(path, 0700);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mDecoders.Length(); ++i) {
|
|
|
|
char buf[255];
|
|
|
|
PR_snprintf(buf, sizeof(buf), "%s/reader-%p", path, mDecoders[i]->GetReader());
|
|
|
|
PR_MkDir(buf, 0700);
|
|
|
|
|
|
|
|
mDecoders[i]->GetResource()->Dump(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-10 14:12:39 -08:00
|
|
|
class ReleaseDecoderTask : public nsRunnable {
|
|
|
|
public:
|
2015-02-15 21:20:56 -08:00
|
|
|
explicit ReleaseDecoderTask(SourceBufferDecoder* aDecoder)
|
2015-02-10 14:12:39 -08:00
|
|
|
: mDecoder(aDecoder)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE MOZ_FINAL {
|
|
|
|
mDecoder->GetReader()->BreakCycles();
|
|
|
|
mDecoder = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<SourceBufferDecoder> mDecoder;
|
|
|
|
};
|
|
|
|
|
2014-11-03 14:16:34 -08:00
|
|
|
class DelayedDispatchToMainThread : public nsRunnable {
|
|
|
|
public:
|
2015-02-10 14:12:39 -08:00
|
|
|
DelayedDispatchToMainThread(SourceBufferDecoder* aDecoder, TrackBuffer* aTrackBuffer)
|
2014-11-03 14:16:34 -08:00
|
|
|
: mDecoder(aDecoder)
|
2015-02-10 14:12:39 -08:00
|
|
|
, mTrackBuffer(aTrackBuffer)
|
2014-11-03 14:16:34 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE MOZ_FINAL {
|
|
|
|
// Shutdown the reader, and remove its reference to the decoder
|
|
|
|
// so that it can't accidentally read it after the decoder
|
|
|
|
// is destroyed.
|
|
|
|
mDecoder->GetReader()->Shutdown();
|
2015-02-11 18:57:40 -08:00
|
|
|
RefPtr<nsIRunnable> task = new ReleaseDecoderTask(mDecoder);
|
2014-11-03 14:16:34 -08:00
|
|
|
mDecoder = nullptr;
|
|
|
|
// task now holds the only ref to the decoder.
|
|
|
|
NS_DispatchToMainThread(task);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-02-10 14:12:39 -08:00
|
|
|
nsRefPtr<SourceBufferDecoder> mDecoder;
|
|
|
|
nsRefPtr<TrackBuffer> mTrackBuffer;
|
2014-11-03 14:16:34 -08:00
|
|
|
};
|
|
|
|
|
2014-09-09 19:21:17 -07:00
|
|
|
void
|
2014-10-01 22:04:06 -07:00
|
|
|
TrackBuffer::RemoveDecoder(SourceBufferDecoder* aDecoder)
|
2014-09-09 19:21:17 -07:00
|
|
|
{
|
2015-02-10 14:12:39 -08:00
|
|
|
MSE_DEBUG("TrackBuffer(%p)::RemoveDecoder(%p, %p)", this, aDecoder, aDecoder->GetReader());
|
|
|
|
RefPtr<nsIRunnable> task = new DelayedDispatchToMainThread(aDecoder, this);
|
2014-10-01 22:04:06 -07:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2015-01-15 19:14:56 -08:00
|
|
|
// There should be no other references to the decoder. Assert that
|
|
|
|
// we aren't using it in the MediaSourceReader.
|
|
|
|
MOZ_ASSERT(!mParentDecoder->IsActiveReader(aDecoder->GetReader()));
|
2014-11-27 16:20:09 -08:00
|
|
|
mInitializedDecoders.RemoveElement(aDecoder);
|
2014-10-01 22:04:06 -07:00
|
|
|
mDecoders.RemoveElement(aDecoder);
|
|
|
|
}
|
2014-11-27 16:20:09 -08:00
|
|
|
aDecoder->GetReader()->GetTaskQueue()->Dispatch(task);
|
2014-09-09 19:21:17 -07:00
|
|
|
}
|
|
|
|
|
2015-01-24 02:45:58 -08:00
|
|
|
bool
|
|
|
|
TrackBuffer::RangeRemoval(int64_t aStart, int64_t aEnd)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
|
|
|
|
nsRefPtr<dom::TimeRanges> buffered = new dom::TimeRanges();
|
|
|
|
int64_t bufferedEnd = Buffered(buffered) * USECS_PER_S;
|
|
|
|
int64_t bufferedStart = buffered->GetStartTime() * USECS_PER_S;
|
|
|
|
|
|
|
|
if (bufferedStart < 0 || aStart > bufferedEnd || aEnd < bufferedStart) {
|
|
|
|
// Nothing to remove.
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-23 21:38:42 -08:00
|
|
|
|
|
|
|
if (aStart > bufferedStart && aEnd < bufferedEnd) {
|
|
|
|
// TODO. We only handle trimming and removal from the start.
|
2015-02-11 23:52:13 -08:00
|
|
|
NS_WARNING("RangeRemoval unsupported arguments. "
|
2015-02-23 21:38:42 -08:00
|
|
|
"Can only handle trimming (trim left or trim right");
|
2015-01-24 02:45:58 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<SourceBufferDecoder*> decoders;
|
|
|
|
decoders.AppendElements(mInitializedDecoders);
|
|
|
|
|
2015-02-23 21:38:42 -08:00
|
|
|
if (aStart <= bufferedStart && aEnd < bufferedEnd) {
|
|
|
|
// Evict data from beginning.
|
|
|
|
for (size_t i = 0; i < decoders.Length(); ++i) {
|
|
|
|
nsRefPtr<dom::TimeRanges> buffered = new dom::TimeRanges();
|
|
|
|
decoders[i]->GetBuffered(buffered);
|
|
|
|
if (buffered->GetEndTime() < aEnd) {
|
|
|
|
// Can be fully removed.
|
|
|
|
MSE_DEBUG("remove all bufferedEnd=%f time=%f, size=%lld",
|
|
|
|
buffered->GetEndTime(), time,
|
|
|
|
decoders[i]->GetResource()->GetSize());
|
|
|
|
decoders[i]->GetResource()->EvictAll();
|
|
|
|
} else {
|
|
|
|
int64_t offset = decoders[i]->ConvertToByteOffset(aEnd);
|
|
|
|
MSE_DEBUG("removing some bufferedEnd=%f offset=%lld size=%lld",
|
|
|
|
buffered->GetEndTime(), offset,
|
|
|
|
decoders[i]->GetResource()->GetSize());
|
|
|
|
if (offset > 0) {
|
|
|
|
decoders[i]->GetResource()->EvictData(offset, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Only trimming existing buffers.
|
|
|
|
for (size_t i = 0; i < decoders.Length(); ++i) {
|
|
|
|
if (aStart <= buffered->GetStartTime()) {
|
|
|
|
// It will be entirely emptied, can clear all data.
|
|
|
|
decoders[i]->GetResource()->EvictAll();
|
|
|
|
} else {
|
|
|
|
decoders[i]->Trim(aStart);
|
2015-01-24 02:45:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 21:38:42 -08:00
|
|
|
|
|
|
|
RemoveEmptyDecoders(decoders);
|
|
|
|
|
2015-01-24 02:45:58 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-09 04:29:00 -08:00
|
|
|
void
|
|
|
|
TrackBuffer::AdjustDecodersTimestampOffset(int32_t aOffset)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
for (uint32_t i = 0; i < mDecoders.Length(); i++) {
|
|
|
|
mDecoders[i]->SetTimestampOffset(mDecoders[i]->GetTimestampOffset() + aOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 23:52:13 -08:00
|
|
|
#undef MSE_DEBUG
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
} // namespace mozilla
|