2014-09-11 22:47:06 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-09-04 01:57:06 +00: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-17 06:33:00 +00:00
|
|
|
#include "ContainerParser.h"
|
2014-09-04 01:57:06 +00: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 23:39:34 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2014-09-04 01:57:06 +00:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
extern PRLogModuleInfo* GetMediaSourceLog();
|
|
|
|
extern PRLogModuleInfo* GetMediaSourceAPILog();
|
|
|
|
|
|
|
|
#define MSE_DEBUG(...) PR_LOG(GetMediaSourceLog(), PR_LOG_DEBUG, (__VA_ARGS__))
|
|
|
|
#define MSE_DEBUGV(...) PR_LOG(GetMediaSourceLog(), PR_LOG_DEBUG+1, (__VA_ARGS__))
|
|
|
|
#define MSE_API(...) PR_LOG(GetMediaSourceAPILog(), PR_LOG_DEBUG, (__VA_ARGS__))
|
|
|
|
#else
|
|
|
|
#define MSE_DEBUG(...)
|
|
|
|
#define MSE_DEBUGV(...)
|
|
|
|
#define MSE_API(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
TrackBuffer::TrackBuffer(MediaSourceDecoder* aParentDecoder, const nsACString& aType)
|
|
|
|
: mParentDecoder(aParentDecoder)
|
|
|
|
, mType(aType)
|
|
|
|
, mLastStartTimestamp(0)
|
2014-12-19 06:26:52 +00:00
|
|
|
, mShutdown(false)
|
2014-09-04 01:57:06 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(TrackBuffer);
|
2014-09-17 06:33:00 +00:00
|
|
|
mParser = ContainerParser::CreateForMIMEType(aType);
|
2014-09-04 01:57:06 +00:00
|
|
|
mTaskQueue = new MediaTaskQueue(GetMediaDecodeThreadPool());
|
|
|
|
aParentDecoder->AddTrackBuffer(this);
|
2014-12-17 23:39:34 +00:00
|
|
|
mDecoderPerSegment = Preferences::GetBool("media.mediasource.decoder-per-segment", false);
|
2014-12-19 06:26:52 +00:00
|
|
|
MSE_DEBUG("TrackBuffer(%p) created for parent decoder %p", this, aParentDecoder);
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TrackBuffer::~TrackBuffer()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(TrackBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ReleaseDecoderTask : public nsRunnable {
|
|
|
|
public:
|
2014-10-02 05:04:06 +00:00
|
|
|
explicit ReleaseDecoderTask(SourceBufferDecoder* aDecoder)
|
2014-10-10 04:58:50 +00:00
|
|
|
: mDecoder(aDecoder)
|
2014-09-04 01:57:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE MOZ_FINAL {
|
2014-11-03 22:16:34 +00:00
|
|
|
mDecoder->GetReader()->BreakCycles();
|
2014-10-10 04:58:50 +00:00
|
|
|
mDecoder = nullptr;
|
2014-09-04 01:57:06 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-10-10 04:58:50 +00:00
|
|
|
nsRefPtr<SourceBufferDecoder> mDecoder;
|
2014-09-04 01:57:06 +00:00
|
|
|
};
|
|
|
|
|
2014-12-09 04:24:55 +00:00
|
|
|
class MOZ_STACK_CLASS DecodersToInitialize MOZ_FINAL {
|
2014-11-06 04:09:19 +00:00
|
|
|
public:
|
|
|
|
explicit DecodersToInitialize(TrackBuffer* aOwner)
|
|
|
|
: mOwner(aOwner)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~DecodersToInitialize()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < mDecoders.Length(); i++) {
|
|
|
|
mOwner->QueueInitializeDecoder(mDecoders[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NewDecoder()
|
|
|
|
{
|
|
|
|
nsRefPtr<SourceBufferDecoder> decoder = mOwner->NewDecoder();
|
|
|
|
if (!decoder) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mDecoders.AppendElement(decoder);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TrackBuffer* mOwner;
|
|
|
|
nsAutoTArray<nsRefPtr<SourceBufferDecoder>,2> mDecoders;
|
|
|
|
};
|
|
|
|
|
2014-12-09 19:43:21 +00:00
|
|
|
nsRefPtr<ShutdownPromise>
|
2014-09-04 01:57:06 +00:00
|
|
|
TrackBuffer::Shutdown()
|
|
|
|
{
|
2014-12-19 06:26:52 +00:00
|
|
|
mParentDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
mShutdown = true;
|
|
|
|
|
2014-12-09 19:43:21 +00:00
|
|
|
MOZ_ASSERT(mShutdownPromise.IsEmpty());
|
|
|
|
nsRefPtr<ShutdownPromise> p = mShutdownPromise.Ensure(__func__);
|
|
|
|
|
|
|
|
RefPtr<MediaTaskQueue> queue = mTaskQueue;
|
2014-09-04 01:57:06 +00:00
|
|
|
mTaskQueue = nullptr;
|
2014-12-09 19:43:21 +00:00
|
|
|
queue->BeginShutdown()
|
|
|
|
->Then(mParentDecoder->GetReader()->GetTaskQueue(), __func__, this,
|
|
|
|
&TrackBuffer::ContinueShutdown, &TrackBuffer::ContinueShutdown);
|
2014-09-04 01:57:06 +00:00
|
|
|
|
2014-12-09 19:43:21 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 22:22:23 +00:00
|
|
|
TrackBuffer::ContinueShutdown()
|
2014-12-09 19:43:21 +00:00
|
|
|
{
|
2014-09-04 01:57:06 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2014-12-09 19:43:21 +00: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-04 01:57:06 +00:00
|
|
|
}
|
2014-12-09 19:43:21 +00:00
|
|
|
|
2014-09-04 07:35:01 +00:00
|
|
|
mInitializedDecoders.Clear();
|
2014-09-04 01:57:06 +00:00
|
|
|
mParentDecoder = nullptr;
|
2014-12-09 19:43:21 +00:00
|
|
|
|
|
|
|
mShutdownPromise.Resolve(true, __func__);
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TrackBuffer::AppendData(const uint8_t* aData, uint32_t aLength)
|
2014-09-17 06:33:00 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-06 04:09:19 +00:00
|
|
|
DecodersToInitialize decoders(this);
|
2014-09-17 06:33:00 +00:00
|
|
|
// TODO: Run more of the buffer append algorithm asynchronously.
|
|
|
|
if (mParser->IsInitSegmentPresent(aData, aLength)) {
|
|
|
|
MSE_DEBUG("TrackBuffer(%p)::AppendData: New initialization segment.", this);
|
2014-11-06 04:09:19 +00:00
|
|
|
if (!decoders.NewDecoder()) {
|
2014-09-17 06:33:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (!mParser->HasInitData()) {
|
|
|
|
MSE_DEBUG("TrackBuffer(%p)::AppendData: Non-init segment appended during initialization.", this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t start, end;
|
|
|
|
if (mParser->ParseStartAndEndTimestamps(aData, aLength, start, end)) {
|
|
|
|
if (mParser->IsMediaSegmentPresent(aData, aLength) &&
|
2014-11-14 04:12:48 +00:00
|
|
|
mLastEndTimestamp &&
|
2014-12-17 23:39:34 +00:00
|
|
|
(!mParser->TimestampsFuzzyEqual(start, mLastEndTimestamp.value()) ||
|
|
|
|
mDecoderPerSegment)) {
|
2014-09-17 06:33:00 +00:00
|
|
|
MSE_DEBUG("TrackBuffer(%p)::AppendData: Data last=[%lld, %lld] overlaps [%lld, %lld]",
|
2014-11-14 04:12:48 +00:00
|
|
|
this, mLastStartTimestamp, mLastEndTimestamp.value(), start, end);
|
2014-09-17 06:33:00 +00:00
|
|
|
|
|
|
|
// This data is earlier in the timeline than data we have already
|
|
|
|
// processed, so we must create a new decoder to handle the decoding.
|
2014-11-06 04:09:19 +00:00
|
|
|
if (!decoders.NewDecoder()) {
|
2014-09-17 06:33:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MSE_DEBUG("TrackBuffer(%p)::AppendData: Decoder marked as initialized.", this);
|
|
|
|
const nsTArray<uint8_t>& initData = mParser->InitData();
|
|
|
|
AppendDataToCurrentResource(initData.Elements(), initData.Length());
|
|
|
|
mLastStartTimestamp = start;
|
2014-11-14 04:12:48 +00:00
|
|
|
} else {
|
|
|
|
MSE_DEBUG("TrackBuffer(%p)::AppendData: Segment last=[%lld, %lld] [%lld, %lld]",
|
|
|
|
this, mLastStartTimestamp, mLastEndTimestamp ? mLastEndTimestamp.value() : 0, start, end);
|
2014-09-17 06:33:00 +00:00
|
|
|
}
|
2014-11-14 04:12:48 +00:00
|
|
|
mLastEndTimestamp.reset();
|
|
|
|
mLastEndTimestamp.emplace(end);
|
2014-09-17 06:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!AppendDataToCurrentResource(aData, aLength)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule the state machine thread to ensure playback starts if required
|
|
|
|
// when data is appended.
|
|
|
|
mParentDecoder->ScheduleStateMachineThread();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TrackBuffer::AppendDataToCurrentResource(const uint8_t* aData, uint32_t aLength)
|
2014-09-04 01:57:06 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!mCurrentDecoder) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceBufferResource* resource = mCurrentDecoder->GetResource();
|
2014-09-07 11:54:00 +00:00
|
|
|
int64_t appendOffset = resource->GetLength();
|
|
|
|
resource->AppendData(aData, aLength);
|
2014-09-04 01:57:06 +00:00
|
|
|
// XXX: For future reference: NDA call must run on the main thread.
|
|
|
|
mCurrentDecoder->NotifyDataArrived(reinterpret_cast<const char*>(aData),
|
2014-09-07 11:54:00 +00:00
|
|
|
aLength, appendOffset);
|
2014-12-09 21:45:06 +00:00
|
|
|
mParentDecoder->NotifyBytesDownloaded();
|
2014-09-07 11:54:00 +00:00
|
|
|
mParentDecoder->NotifyTimeRangesChanged();
|
|
|
|
|
2014-09-04 01:57:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-10 21:49:54 +00: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-04 01:57:06 +00:00
|
|
|
bool
|
|
|
|
TrackBuffer::EvictData(uint32_t aThreshold)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-03 22:16:34 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2014-09-09 09:12:00 +00:00
|
|
|
|
|
|
|
int64_t totalSize = 0;
|
|
|
|
for (uint32_t i = 0; i < mDecoders.Length(); ++i) {
|
|
|
|
totalSize += mDecoders[i]->GetResource()->GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t toEvict = totalSize - aThreshold;
|
|
|
|
if (toEvict <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-10 21:50:41 +00:00
|
|
|
// Get a list of initialized decoders, sorted by their start times.
|
2014-12-10 21:49:54 +00:00
|
|
|
nsTArray<SourceBufferDecoder*> decoders;
|
|
|
|
decoders.AppendElements(mInitializedDecoders);
|
|
|
|
decoders.Sort(DecoderSorter());
|
|
|
|
|
2014-12-10 21:50:41 +00:00
|
|
|
// First try to evict data before the current play position, starting
|
|
|
|
// with the earliest time.
|
|
|
|
uint32_t i = 0;
|
|
|
|
for (; i < decoders.Length(); ++i) {
|
2014-09-09 09:12:00 +00:00
|
|
|
MSE_DEBUG("TrackBuffer(%p)::EvictData decoder=%u threshold=%u toEvict=%lld",
|
|
|
|
this, i, aThreshold, toEvict);
|
2014-12-10 21:49:54 +00:00
|
|
|
toEvict -= decoders[i]->GetResource()->EvictData(toEvict);
|
|
|
|
if (!decoders[i]->GetResource()->GetSize() &&
|
|
|
|
decoders[i] != mCurrentDecoder) {
|
|
|
|
RemoveDecoder(decoders[i]);
|
|
|
|
}
|
|
|
|
if (toEvict <= 0 || decoders[i] == mCurrentDecoder) {
|
|
|
|
break;
|
2014-12-10 21:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-10 21:50:41 +00:00
|
|
|
|
|
|
|
// If we still need to evict more, then try to evict entire decoders,
|
|
|
|
// starting from the end.
|
|
|
|
if (toEvict > 0) {
|
|
|
|
uint32_t end = i;
|
|
|
|
MOZ_ASSERT(decoders[end] == mCurrentDecoder);
|
|
|
|
|
|
|
|
for (i = decoders.Length() - 1; i > end; --i) {
|
|
|
|
MSE_DEBUG("TrackBuffer(%p)::EvictData removing entire decoder=%u from end toEvict=%lld",
|
|
|
|
this, i, toEvict);
|
|
|
|
// TODO: We could implement forward-eviction within a decoder and
|
|
|
|
// be able to evict within the current decoder.
|
|
|
|
toEvict -= decoders[i]->GetResource()->GetSize();
|
|
|
|
RemoveDecoder(decoders[i]);
|
|
|
|
if (toEvict <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-09 09:12:00 +00:00
|
|
|
return toEvict < (totalSize - aThreshold);
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TrackBuffer::EvictBefore(double aTime)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-03 22:16:34 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
for (uint32_t i = 0; i < mInitializedDecoders.Length(); ++i) {
|
|
|
|
int64_t endOffset = mInitializedDecoders[i]->ConvertToByteOffset(aTime);
|
2014-09-09 09:12:00 +00:00
|
|
|
if (endOffset > 0) {
|
|
|
|
MSE_DEBUG("TrackBuffer(%p)::EvictBefore decoder=%u offset=%lld", this, i, endOffset);
|
2014-11-03 22:16:34 +00:00
|
|
|
mInitializedDecoders[i]->GetResource()->EvictBefore(endOffset);
|
|
|
|
if (!mInitializedDecoders[i]->GetResource()->GetSize() &&
|
|
|
|
mInitializedDecoders[i] != mCurrentDecoder) {
|
|
|
|
RemoveDecoder(mInitializedDecoders[i]);
|
|
|
|
}
|
2014-09-09 09:12:00 +00:00
|
|
|
}
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
TrackBuffer::Buffered(dom::TimeRanges* aRanges)
|
|
|
|
{
|
2014-09-04 07:35:01 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
|
2014-09-04 01:57:06 +00:00
|
|
|
double highestEndTime = 0;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mDecoders.Length(); ++i) {
|
|
|
|
nsRefPtr<dom::TimeRanges> r = new dom::TimeRanges();
|
|
|
|
mDecoders[i]->GetBuffered(r);
|
|
|
|
if (r->Length() > 0) {
|
|
|
|
highestEndTime = std::max(highestEndTime, r->GetEndTime());
|
2014-12-10 21:52:57 +00:00
|
|
|
aRanges->Union(r, double(mParser->GetRoundingError()) / USECS_PER_S);
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return highestEndTime;
|
|
|
|
}
|
|
|
|
|
2014-11-06 04:09:19 +00:00
|
|
|
already_AddRefed<SourceBufferDecoder>
|
2014-09-04 01:57:06 +00:00
|
|
|
TrackBuffer::NewDecoder()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-09-17 06:33:00 +00:00
|
|
|
MOZ_ASSERT(mParentDecoder);
|
|
|
|
|
|
|
|
DiscardDecoder();
|
2014-09-04 01:57:06 +00:00
|
|
|
|
|
|
|
nsRefPtr<SourceBufferDecoder> decoder = mParentDecoder->CreateSubDecoder(mType);
|
|
|
|
if (!decoder) {
|
2014-11-06 04:09:19 +00:00
|
|
|
return nullptr;
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
mCurrentDecoder = decoder;
|
2014-09-04 07:35:01 +00:00
|
|
|
mDecoders.AppendElement(decoder);
|
2014-09-04 01:57:06 +00:00
|
|
|
|
|
|
|
mLastStartTimestamp = 0;
|
2014-11-14 04:12:48 +00:00
|
|
|
mLastEndTimestamp.reset();
|
2014-09-04 01:57:06 +00:00
|
|
|
|
2014-10-13 22:05:00 +00:00
|
|
|
decoder->SetTaskQueue(mTaskQueue);
|
2014-11-06 04:09:19 +00:00
|
|
|
return decoder.forget();
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-10-02 05:04:06 +00:00
|
|
|
TrackBuffer::QueueInitializeDecoder(SourceBufferDecoder* aDecoder)
|
2014-09-04 01:57:06 +00:00
|
|
|
{
|
2014-12-15 02:30:20 +00:00
|
|
|
if (NS_WARN_IF(!mTaskQueue)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:57:06 +00:00
|
|
|
RefPtr<nsIRunnable> task =
|
2014-10-02 05:04:06 +00:00
|
|
|
NS_NewRunnableMethodWithArg<SourceBufferDecoder*>(this,
|
|
|
|
&TrackBuffer::InitializeDecoder,
|
|
|
|
aDecoder);
|
2014-09-04 01:57:06 +00:00
|
|
|
if (NS_FAILED(mTaskQueue->Dispatch(task))) {
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p): Failed to enqueue decoder initialization task", this);
|
2014-10-02 05:04:06 +00:00
|
|
|
RemoveDecoder(aDecoder);
|
2014-09-04 01:57:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-10-02 05:04:06 +00:00
|
|
|
TrackBuffer::InitializeDecoder(SourceBufferDecoder* aDecoder)
|
2014-09-04 01:57:06 +00:00
|
|
|
{
|
2014-12-19 06:26:52 +00: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-09-04 01:57:06 +00:00
|
|
|
mParentDecoder->GetReentrantMonitor().AssertNotCurrentThreadIn();
|
2014-12-19 06:26:52 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
MSE_DEBUG("TrackBuffer(%p) was shut down. Aborting initialization.", this);
|
|
|
|
return;
|
|
|
|
}
|
2014-09-04 01:57:06 +00:00
|
|
|
|
2014-12-19 06:26:52 +00:00
|
|
|
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
|
2014-09-04 01:57:06 +00:00
|
|
|
MediaDecoderReader* reader = aDecoder->GetReader();
|
|
|
|
MSE_DEBUG("TrackBuffer(%p): Initializing subdecoder %p reader %p",
|
2014-10-02 05:04:06 +00:00
|
|
|
this, aDecoder, reader);
|
2014-09-04 01:57:06 +00:00
|
|
|
|
|
|
|
MediaInfo mi;
|
|
|
|
nsAutoPtr<MetadataTags> tags; // TODO: Handle metadata.
|
2014-12-19 06:26:52 +00:00
|
|
|
nsresult rv;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoExit mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
rv = reader->ReadMetadata(&mi, getter_Transfers(tags));
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:57:06 +00:00
|
|
|
reader->SetIdle();
|
2014-12-19 06:26:52 +00:00
|
|
|
if (mShutdown) {
|
|
|
|
MSE_DEBUG("TrackBuffer(%p) was shut down while reading metadata. Aborting initialization.", this);
|
|
|
|
return;
|
|
|
|
}
|
2014-10-13 22:05:00 +00:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && reader->IsWaitingOnCDMResource()) {
|
|
|
|
mWaitingDecoders.AppendElement(aDecoder);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aDecoder->SetTaskQueue(nullptr);
|
|
|
|
|
2014-09-04 01:57:06 +00:00
|
|
|
if (NS_FAILED(rv) || (!mi.HasVideo() && !mi.HasAudio())) {
|
|
|
|
// XXX: Need to signal error back to owning SourceBuffer.
|
|
|
|
MSE_DEBUG("TrackBuffer(%p): Reader %p failed to initialize rv=%x audio=%d video=%d",
|
|
|
|
this, reader, rv, mi.HasAudio(), mi.HasVideo());
|
2014-09-10 02:21:17 +00:00
|
|
|
RemoveDecoder(aDecoder);
|
2014-09-04 01:57:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mi.HasVideo()) {
|
|
|
|
MSE_DEBUG("TrackBuffer(%p): Reader %p video resolution=%dx%d",
|
|
|
|
this, reader, mi.mVideo.mDisplay.width, mi.mVideo.mDisplay.height);
|
|
|
|
}
|
|
|
|
if (mi.HasAudio()) {
|
|
|
|
MSE_DEBUG("TrackBuffer(%p): Reader %p audio sampleRate=%d channels=%d",
|
|
|
|
this, reader, mi.mAudio.mRate, mi.mAudio.mChannels);
|
|
|
|
}
|
|
|
|
|
2014-09-10 02:21:17 +00:00
|
|
|
if (!RegisterDecoder(aDecoder)) {
|
|
|
|
// XXX: Need to signal error back to owning SourceBuffer.
|
|
|
|
MSE_DEBUG("TrackBuffer(%p): Reader %p not activated", this, reader);
|
|
|
|
RemoveDecoder(aDecoder);
|
|
|
|
return;
|
|
|
|
}
|
2014-09-04 01:57:06 +00:00
|
|
|
MSE_DEBUG("TrackBuffer(%p): Reader %p activated", this, reader);
|
|
|
|
}
|
|
|
|
|
2014-09-10 02:21:17 +00:00
|
|
|
bool
|
|
|
|
TrackBuffer::ValidateTrackFormats(const MediaInfo& aInfo)
|
|
|
|
{
|
|
|
|
if (mInfo.HasAudio() != aInfo.HasAudio() ||
|
|
|
|
mInfo.HasVideo() != aInfo.HasVideo()) {
|
|
|
|
MSE_DEBUG("TrackBuffer(%p)::ValidateTrackFormats audio/video track mismatch", this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Support dynamic audio format changes.
|
|
|
|
if (mInfo.HasAudio() &&
|
|
|
|
(mInfo.mAudio.mRate != aInfo.mAudio.mRate ||
|
|
|
|
mInfo.mAudio.mChannels != aInfo.mAudio.mChannels)) {
|
|
|
|
MSE_DEBUG("TrackBuffer(%p)::ValidateTrackFormats audio format mismatch", this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-10-02 05:04:06 +00:00
|
|
|
TrackBuffer::RegisterDecoder(SourceBufferDecoder* aDecoder)
|
2014-09-04 01:57:06 +00:00
|
|
|
{
|
2014-12-19 06:26:52 +00:00
|
|
|
mParentDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2014-09-04 01:57:06 +00:00
|
|
|
const MediaInfo& info = aDecoder->GetReader()->GetMediaInfo();
|
|
|
|
// Initialize the track info since this is the first decoder.
|
2014-09-04 07:35:01 +00:00
|
|
|
if (mInitializedDecoders.IsEmpty()) {
|
2014-09-10 02:21:17 +00:00
|
|
|
mInfo = info;
|
|
|
|
mParentDecoder->OnTrackBufferConfigured(this, mInfo);
|
|
|
|
}
|
|
|
|
if (!ValidateTrackFormats(info)) {
|
2014-09-04 01:57:06 +00:00
|
|
|
MSE_DEBUG("TrackBuffer(%p)::RegisterDecoder with mismatched audio/video tracks", this);
|
2014-09-10 02:21:17 +00:00
|
|
|
return false;
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
2014-09-04 07:35:01 +00:00
|
|
|
mInitializedDecoders.AppendElement(aDecoder);
|
2014-09-07 11:54:00 +00:00
|
|
|
mParentDecoder->NotifyTimeRangesChanged();
|
2014-09-10 02:21:17 +00:00
|
|
|
return true;
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TrackBuffer::DiscardDecoder()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
|
|
|
if (mCurrentDecoder) {
|
|
|
|
mCurrentDecoder->GetResource()->Ended();
|
|
|
|
}
|
|
|
|
mCurrentDecoder = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TrackBuffer::Detach()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mCurrentDecoder) {
|
|
|
|
DiscardDecoder();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TrackBuffer::HasInitSegment()
|
2014-09-18 20:10:15 +00:00
|
|
|
{
|
2014-09-04 01:57:06 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2014-09-17 06:33:00 +00:00
|
|
|
return mParser->HasInitData();
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TrackBuffer::IsReady()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2014-09-10 02:21:17 +00:00
|
|
|
MOZ_ASSERT((mInfo.HasAudio() || mInfo.HasVideo()) || mInitializedDecoders.IsEmpty());
|
2014-09-17 06:33:00 +00:00
|
|
|
return mParser->HasInitData() && (mInfo.HasAudio() || mInfo.HasVideo());
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-09-10 02:27:51 +00:00
|
|
|
TrackBuffer::ContainsTime(int64_t aTime)
|
2014-09-04 01:57:06 +00:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2014-09-04 07:35:01 +00:00
|
|
|
for (uint32_t i = 0; i < mInitializedDecoders.Length(); ++i) {
|
2014-09-04 01:57:06 +00:00
|
|
|
nsRefPtr<dom::TimeRanges> r = new dom::TimeRanges();
|
2014-09-04 07:35:01 +00:00
|
|
|
mInitializedDecoders[i]->GetBuffered(r);
|
2014-09-10 02:27:51 +00:00
|
|
|
if (r->Find(double(aTime) / USECS_PER_S) != dom::TimeRanges::NoIndex) {
|
2014-09-04 01:57:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TrackBuffer::BreakCycles()
|
|
|
|
{
|
2014-10-10 04:58:50 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-12-09 19:43:21 +00:00
|
|
|
for (uint32_t i = 0; i < mShutdownDecoders.Length(); ++i) {
|
|
|
|
mShutdownDecoders[i]->BreakCycles();
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
2014-12-09 19:43:21 +00:00
|
|
|
mShutdownDecoders.Clear();
|
2014-10-10 04:22:41 +00:00
|
|
|
|
|
|
|
// These are cleared in Shutdown()
|
2014-12-09 19:43:21 +00:00
|
|
|
MOZ_ASSERT(!mDecoders.Length());
|
2014-10-10 04:22:41 +00:00
|
|
|
MOZ_ASSERT(mInitializedDecoders.IsEmpty());
|
|
|
|
MOZ_ASSERT(!mParentDecoder);
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TrackBuffer::ResetDecode()
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mDecoders.Length(); ++i) {
|
|
|
|
mDecoders[i]->GetReader()->ResetDecode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsTArray<nsRefPtr<SourceBufferDecoder>>&
|
|
|
|
TrackBuffer::Decoders()
|
|
|
|
{
|
|
|
|
// XXX assert OnDecodeThread
|
2014-09-04 07:35:01 +00:00
|
|
|
return mInitializedDecoders;
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 22:05:00 +00: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 22:47:06 +00: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
|
|
|
|
|
2014-11-03 22:16:34 +00:00
|
|
|
class DelayedDispatchToMainThread : public nsRunnable {
|
|
|
|
public:
|
|
|
|
explicit DelayedDispatchToMainThread(SourceBufferDecoder* aDecoder)
|
|
|
|
: mDecoder(aDecoder)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
mDecoder->GetReader()->ClearDecoder();
|
|
|
|
RefPtr<nsIRunnable> task = new ReleaseDecoderTask(mDecoder);
|
|
|
|
mDecoder = nullptr;
|
|
|
|
// task now holds the only ref to the decoder.
|
|
|
|
NS_DispatchToMainThread(task);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<SourceBufferDecoder> mDecoder;
|
|
|
|
};
|
|
|
|
|
2014-09-10 02:21:17 +00:00
|
|
|
void
|
2014-10-02 05:04:06 +00:00
|
|
|
TrackBuffer::RemoveDecoder(SourceBufferDecoder* aDecoder)
|
2014-09-10 02:21:17 +00:00
|
|
|
{
|
2014-11-28 00:20:09 +00:00
|
|
|
RefPtr<nsIRunnable> task = new DelayedDispatchToMainThread(aDecoder);
|
|
|
|
|
2014-10-02 05:04:06 +00:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
|
2014-11-28 00:20:09 +00:00
|
|
|
mInitializedDecoders.RemoveElement(aDecoder);
|
2014-10-02 05:04:06 +00:00
|
|
|
mDecoders.RemoveElement(aDecoder);
|
2014-11-03 22:16:34 +00:00
|
|
|
|
2014-10-02 05:04:06 +00:00
|
|
|
if (mCurrentDecoder == aDecoder) {
|
|
|
|
DiscardDecoder();
|
|
|
|
}
|
|
|
|
}
|
2014-11-28 00:20:09 +00:00
|
|
|
aDecoder->GetReader()->GetTaskQueue()->Dispatch(task);
|
2014-09-10 02:21:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-04 01:57:06 +00:00
|
|
|
} // namespace mozilla
|