2014-04-14 11:24:00 +00:00
|
|
|
/* -*- mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2013-09-27 05:22:37 +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 "MediaSourceDecoder.h"
|
|
|
|
|
2015-05-19 18:15:34 +00:00
|
|
|
#include "mozilla/Logging.h"
|
2013-09-27 05:22:37 +00:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2014-08-13 12:22:00 +00:00
|
|
|
#include "MediaDecoderStateMachine.h"
|
2013-10-05 08:04:39 +00:00
|
|
|
#include "MediaSource.h"
|
2014-08-13 12:22:00 +00:00
|
|
|
#include "MediaSourceReader.h"
|
|
|
|
#include "MediaSourceResource.h"
|
2014-08-20 08:07:00 +00:00
|
|
|
#include "MediaSourceUtils.h"
|
2015-02-13 05:52:42 +00:00
|
|
|
#include "SourceBufferDecoder.h"
|
2015-02-12 07:52:12 +00:00
|
|
|
#include "VideoUtils.h"
|
2015-06-11 23:26:57 +00:00
|
|
|
#include "MediaFormatReader.h"
|
|
|
|
#include "MediaSourceDemuxer.h"
|
2014-07-22 08:20:00 +00:00
|
|
|
|
2014-08-11 01:21:17 +00:00
|
|
|
extern PRLogModuleInfo* GetMediaSourceLog();
|
|
|
|
|
2015-06-03 22:25:57 +00:00
|
|
|
#define MSE_DEBUG(arg, ...) MOZ_LOG(GetMediaSourceLog(), mozilla::LogLevel::Debug, ("MediaSourceDecoder(%p)::%s: " arg, this, __func__, ##__VA_ARGS__))
|
|
|
|
#define MSE_DEBUGV(arg, ...) MOZ_LOG(GetMediaSourceLog(), mozilla::LogLevel::Verbose, ("MediaSourceDecoder(%p)::%s: " arg, this, __func__, ##__VA_ARGS__))
|
2013-09-27 05:22:37 +00:00
|
|
|
|
2015-06-03 21:46:08 +00:00
|
|
|
using namespace mozilla::media;
|
|
|
|
|
2013-09-27 05:22:37 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-08-22 02:14:36 +00:00
|
|
|
class SourceBufferDecoder;
|
2013-09-27 05:22:37 +00:00
|
|
|
|
2013-11-18 04:22:47 +00:00
|
|
|
MediaSourceDecoder::MediaSourceDecoder(dom::HTMLMediaElement* aElement)
|
2013-09-27 05:22:37 +00:00
|
|
|
: mMediaSource(nullptr)
|
2015-06-11 23:26:57 +00:00
|
|
|
, mIsUsingFormatReader(Preferences::GetBool("media.mediasource.format-reader", false))
|
|
|
|
, mEnded(false)
|
2013-09-27 05:22:37 +00:00
|
|
|
{
|
2015-06-02 21:44:26 +00:00
|
|
|
SetExplicitDuration(UnspecifiedNaN<double>());
|
2013-09-27 05:22:37 +00:00
|
|
|
Init(aElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaDecoder*
|
|
|
|
MediaSourceDecoder::Clone()
|
|
|
|
{
|
|
|
|
// TODO: Sort out cloning.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaDecoderStateMachine*
|
|
|
|
MediaSourceDecoder::CreateStateMachine()
|
|
|
|
{
|
2015-06-11 23:26:57 +00:00
|
|
|
if (mIsUsingFormatReader) {
|
|
|
|
mDemuxer = new MediaSourceDemuxer();
|
|
|
|
mReader = new MediaFormatReader(this, mDemuxer);
|
|
|
|
} else {
|
|
|
|
mReader = new MediaSourceReader(this);
|
|
|
|
}
|
2014-08-17 11:29:00 +00:00
|
|
|
return new MediaDecoderStateMachine(this, mReader);
|
2013-09-27 05:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaSourceDecoder::Load(nsIStreamListener**, MediaDecoder*)
|
|
|
|
{
|
2015-04-02 17:49:01 +00:00
|
|
|
MOZ_ASSERT(!GetStateMachine());
|
|
|
|
SetStateMachine(CreateStateMachine());
|
|
|
|
if (!GetStateMachine()) {
|
2014-02-17 22:53:51 +00:00
|
|
|
NS_WARNING("Failed to create state machine!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-04-02 17:49:01 +00:00
|
|
|
nsresult rv = GetStateMachine()->Init(nullptr);
|
2014-08-05 14:55:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
SetStateMachineParameters();
|
2015-05-06 01:17:51 +00:00
|
|
|
return ScheduleStateMachine();
|
2013-09-27 05:22:37 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 06:15:47 +00:00
|
|
|
media::TimeIntervals
|
|
|
|
MediaSourceDecoder::GetSeekable()
|
2013-10-05 08:04:39 +00:00
|
|
|
{
|
2014-08-25 04:09:44 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!mMediaSource) {
|
2015-05-18 06:15:47 +00:00
|
|
|
NS_WARNING("MediaSource element isn't attached");
|
|
|
|
return media::TimeIntervals::Invalid();
|
2014-08-25 04:09:44 +00:00
|
|
|
}
|
2014-09-16 03:15:55 +00:00
|
|
|
|
2015-05-18 06:15:47 +00:00
|
|
|
media::TimeIntervals seekable;
|
2013-10-05 08:04:39 +00:00
|
|
|
double duration = mMediaSource->Duration();
|
|
|
|
if (IsNaN(duration)) {
|
|
|
|
// Return empty range.
|
|
|
|
} else if (duration > 0 && mozilla::IsInfinite(duration)) {
|
2015-06-17 21:22:10 +00:00
|
|
|
media::TimeIntervals buffered = GetBuffered();
|
2015-05-18 06:15:47 +00:00
|
|
|
if (buffered.Length()) {
|
|
|
|
seekable += media::TimeInterval(buffered.GetStart(), buffered.GetEnd());
|
|
|
|
}
|
2013-10-05 08:04:39 +00:00
|
|
|
} else {
|
2015-05-18 06:15:47 +00:00
|
|
|
seekable += media::TimeInterval(media::TimeUnit::FromSeconds(0),
|
|
|
|
media::TimeUnit::FromSeconds(duration));
|
2013-10-05 08:04:39 +00:00
|
|
|
}
|
2015-05-18 06:15:47 +00:00
|
|
|
MSE_DEBUG("ranges=%s", DumpTimeRanges(seekable).get());
|
|
|
|
return seekable;
|
2013-10-05 08:04:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 21:22:10 +00:00
|
|
|
media::TimeIntervals
|
|
|
|
MediaSourceDecoder::GetBuffered()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
dom::SourceBufferList* sourceBuffers = mMediaSource->ActiveSourceBuffers();
|
|
|
|
media::TimeUnit highestEndTime;
|
|
|
|
nsTArray<media::TimeIntervals> activeRanges;
|
|
|
|
media::TimeIntervals buffered;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < sourceBuffers->Length(); i++) {
|
|
|
|
bool found;
|
|
|
|
dom::SourceBuffer* sb = sourceBuffers->IndexedGetter(i, found);
|
|
|
|
MOZ_ASSERT(found);
|
|
|
|
|
|
|
|
activeRanges.AppendElement(sb->GetTimeIntervals());
|
|
|
|
highestEndTime =
|
|
|
|
std::max(highestEndTime, activeRanges.LastElement().GetEnd());
|
|
|
|
}
|
|
|
|
|
|
|
|
buffered +=
|
|
|
|
media::TimeInterval(media::TimeUnit::FromMicroseconds(0), highestEndTime);
|
|
|
|
|
|
|
|
for (auto& range : activeRanges) {
|
|
|
|
if (mEnded && range.Length()) {
|
|
|
|
// Set the end time on the last range to highestEndTime by adding a
|
|
|
|
// new range spanning the current end time to highestEndTime, which
|
|
|
|
// Normalize() will then merge with the old last range.
|
|
|
|
range +=
|
|
|
|
media::TimeInterval(range.GetEnd(), highestEndTime);
|
|
|
|
}
|
|
|
|
buffered.Intersection(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
MSE_DEBUG("ranges=%s", DumpTimeRanges(buffered).get());
|
|
|
|
return buffered;
|
|
|
|
}
|
|
|
|
|
2014-08-25 04:09:44 +00:00
|
|
|
void
|
|
|
|
MediaSourceDecoder::Shutdown()
|
|
|
|
{
|
2015-02-12 07:52:13 +00:00
|
|
|
MSE_DEBUG("Shutdown");
|
2014-11-04 04:24:52 +00:00
|
|
|
// Detach first so that TrackBuffers are unused on the main thread when
|
|
|
|
// shut down on the decode task queue.
|
2014-08-28 03:44:58 +00:00
|
|
|
if (mMediaSource) {
|
|
|
|
mMediaSource->Detach();
|
|
|
|
}
|
2014-11-04 04:24:52 +00:00
|
|
|
|
|
|
|
MediaDecoder::Shutdown();
|
2014-08-25 04:09:44 +00:00
|
|
|
// Kick WaitForData out of its slumber.
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
|
2014-04-14 11:24:00 +00:00
|
|
|
/*static*/
|
|
|
|
already_AddRefed<MediaResource>
|
2014-10-15 01:17:03 +00:00
|
|
|
MediaSourceDecoder::CreateResource(nsIPrincipal* aPrincipal)
|
2014-04-14 11:24:00 +00:00
|
|
|
{
|
2014-10-15 01:17:03 +00:00
|
|
|
return nsRefPtr<MediaResource>(new MediaSourceResource(aPrincipal)).forget();
|
2014-04-14 11:24:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 05:22:37 +00:00
|
|
|
void
|
2013-11-18 04:22:47 +00:00
|
|
|
MediaSourceDecoder::AttachMediaSource(dom::MediaSource* aMediaSource)
|
2013-09-27 05:22:37 +00:00
|
|
|
{
|
2015-04-02 17:49:01 +00:00
|
|
|
MOZ_ASSERT(!mMediaSource && !GetStateMachine() && NS_IsMainThread());
|
2013-09-27 05:22:37 +00:00
|
|
|
mMediaSource = aMediaSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceDecoder::DetachMediaSource()
|
|
|
|
{
|
2014-08-25 04:09:44 +00:00
|
|
|
MOZ_ASSERT(mMediaSource && NS_IsMainThread());
|
2013-09-27 05:22:37 +00:00
|
|
|
mMediaSource = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-22 02:14:36 +00:00
|
|
|
already_AddRefed<SourceBufferDecoder>
|
2015-01-07 23:58:55 +00:00
|
|
|
MediaSourceDecoder::CreateSubDecoder(const nsACString& aType, int64_t aTimestampOffset)
|
2013-09-27 05:22:37 +00:00
|
|
|
{
|
2015-06-11 23:26:57 +00:00
|
|
|
MOZ_ASSERT(mReader && !mIsUsingFormatReader);
|
|
|
|
return GetReader()->CreateSubDecoder(aType, aTimestampOffset);
|
2014-04-21 13:30:00 +00:00
|
|
|
}
|
|
|
|
|
2014-09-04 01:57:06 +00:00
|
|
|
void
|
|
|
|
MediaSourceDecoder::AddTrackBuffer(TrackBuffer* aTrackBuffer)
|
|
|
|
{
|
2015-06-11 23:26:57 +00:00
|
|
|
MOZ_ASSERT(mReader && !mIsUsingFormatReader);
|
|
|
|
GetReader()->AddTrackBuffer(aTrackBuffer);
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceDecoder::RemoveTrackBuffer(TrackBuffer* aTrackBuffer)
|
|
|
|
{
|
2015-06-11 23:26:57 +00:00
|
|
|
MOZ_ASSERT(mReader && !mIsUsingFormatReader);
|
|
|
|
GetReader()->RemoveTrackBuffer(aTrackBuffer);
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceDecoder::OnTrackBufferConfigured(TrackBuffer* aTrackBuffer, const MediaInfo& aInfo)
|
|
|
|
{
|
2015-06-11 23:26:57 +00:00
|
|
|
MOZ_ASSERT(mReader && !mIsUsingFormatReader);
|
|
|
|
GetReader()->OnTrackBufferConfigured(aTrackBuffer, aInfo);
|
2014-09-04 01:57:06 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 07:25:09 +00:00
|
|
|
void
|
2015-03-23 10:08:05 +00:00
|
|
|
MediaSourceDecoder::Ended(bool aEnded)
|
2014-08-26 07:25:09 +00:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-03-23 10:08:05 +00:00
|
|
|
static_cast<MediaSourceResource*>(GetResource())->SetEnded(aEnded);
|
2015-06-11 23:26:57 +00:00
|
|
|
if (!mIsUsingFormatReader) {
|
|
|
|
GetReader()->Ended(aEnded);
|
|
|
|
}
|
|
|
|
mEnded = true;
|
2014-08-26 07:25:09 +00:00
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:50:20 +00:00
|
|
|
bool
|
|
|
|
MediaSourceDecoder::IsExpectingMoreData()
|
|
|
|
{
|
2015-06-11 23:26:57 +00:00
|
|
|
return !mEnded;
|
2014-11-12 04:50:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 04:11:33 +00:00
|
|
|
void
|
2015-02-04 09:20:15 +00:00
|
|
|
MediaSourceDecoder::SetInitialDuration(int64_t aDuration)
|
2014-11-12 04:11:33 +00:00
|
|
|
{
|
2015-06-02 21:18:47 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-12 04:11:33 +00:00
|
|
|
// Only use the decoded duration if one wasn't already
|
|
|
|
// set.
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-06-02 21:44:26 +00:00
|
|
|
if (!mMediaSource || !IsNaN(ExplicitDuration())) {
|
2014-08-25 04:09:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-11-12 04:11:33 +00:00
|
|
|
double duration = aDuration;
|
2015-01-16 12:49:01 +00:00
|
|
|
// A duration of -1 is +Infinity.
|
|
|
|
if (aDuration >= 0) {
|
|
|
|
duration /= USECS_PER_S;
|
|
|
|
}
|
2015-02-04 09:20:15 +00:00
|
|
|
SetMediaSourceDuration(duration, MSRangeRemovalAction::SKIP);
|
2014-11-12 04:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-16 12:49:02 +00:00
|
|
|
MediaSourceDecoder::SetMediaSourceDuration(double aDuration, MSRangeRemovalAction aAction)
|
2014-11-12 04:11:33 +00:00
|
|
|
{
|
2015-06-02 21:18:47 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-12 04:11:33 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-06-02 21:44:26 +00:00
|
|
|
double oldDuration = ExplicitDuration();
|
2015-01-16 12:49:01 +00:00
|
|
|
if (aDuration >= 0) {
|
2015-02-12 07:52:12 +00:00
|
|
|
int64_t checkedDuration;
|
|
|
|
if (NS_FAILED(SecondsToUsecs(aDuration, checkedDuration))) {
|
|
|
|
// INT64_MAX is used as infinity by the state machine.
|
|
|
|
// We want a very bigger number, but not infinity.
|
|
|
|
checkedDuration = INT64_MAX - 1;
|
|
|
|
}
|
2015-06-02 21:44:26 +00:00
|
|
|
SetExplicitDuration(aDuration);
|
2015-01-16 12:49:01 +00:00
|
|
|
} else {
|
2015-06-02 21:44:26 +00:00
|
|
|
SetExplicitDuration(PositiveInfinity<double>());
|
2015-01-16 12:49:01 +00:00
|
|
|
}
|
2015-06-11 23:26:57 +00:00
|
|
|
if (!mIsUsingFormatReader && GetReader()) {
|
|
|
|
GetReader()->SetMediaSourceDuration(ExplicitDuration());
|
2015-01-24 10:46:21 +00:00
|
|
|
}
|
2015-01-16 12:49:02 +00:00
|
|
|
|
2015-06-02 21:18:47 +00:00
|
|
|
if (mMediaSource && aAction != MSRangeRemovalAction::SKIP) {
|
|
|
|
mMediaSource->DurationChange(oldDuration, aDuration);
|
2014-11-12 04:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
MediaSourceDecoder::GetMediaSourceDuration()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-06-02 21:44:26 +00:00
|
|
|
return ExplicitDuration();
|
2014-08-25 04:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-07 11:54:00 +00:00
|
|
|
MediaSourceDecoder::NotifyTimeRangesChanged()
|
2014-08-25 04:09:44 +00:00
|
|
|
{
|
2015-06-11 23:26:57 +00:00
|
|
|
MOZ_ASSERT(mReader && !mIsUsingFormatReader);
|
|
|
|
GetReader()->NotifyTimeRangesChanged();
|
2014-08-25 04:09:44 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 00:04:54 +00:00
|
|
|
void
|
|
|
|
MediaSourceDecoder::PrepareReaderInitialization()
|
|
|
|
{
|
2015-06-11 23:26:57 +00:00
|
|
|
if (mIsUsingFormatReader) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-05 00:04:54 +00:00
|
|
|
MOZ_ASSERT(mReader);
|
2015-06-11 23:26:57 +00:00
|
|
|
GetReader()->PrepareInitialization();
|
2014-09-05 00:04:54 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 02:35:58 +00:00
|
|
|
void
|
|
|
|
MediaSourceDecoder::GetMozDebugReaderData(nsAString& aString)
|
|
|
|
{
|
2015-06-11 23:26:57 +00:00
|
|
|
if (mIsUsingFormatReader) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GetReader()->GetMozDebugReaderData(aString);
|
2015-01-29 02:35:58 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 22:05:00 +00:00
|
|
|
#ifdef MOZ_EME
|
|
|
|
nsresult
|
|
|
|
MediaSourceDecoder::SetCDMProxy(CDMProxy* aProxy)
|
|
|
|
{
|
|
|
|
nsresult rv = MediaDecoder::SetCDMProxy(aProxy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-06-11 23:26:57 +00:00
|
|
|
if (!mIsUsingFormatReader) {
|
|
|
|
rv = GetReader()->SetCDMProxy(aProxy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2015-04-01 07:48:43 +00:00
|
|
|
if (aProxy) {
|
2015-03-26 06:53:05 +00:00
|
|
|
// The sub readers can't decrypt EME content until they have a CDMProxy,
|
|
|
|
// and the CDMProxy knows the capabilities of the CDM. The MediaSourceReader
|
|
|
|
// remains in "waiting for resources" state until then. We need to kick the
|
|
|
|
// reader out of waiting if the CDM gets added with known capabilities.
|
|
|
|
CDMCaps::AutoLock caps(aProxy->Capabilites());
|
|
|
|
if (!caps.AreCapsKnown()) {
|
|
|
|
nsCOMPtr<nsIRunnable> task(
|
|
|
|
NS_NewRunnableMethod(this, &MediaDecoder::NotifyWaitingForResourcesStatusChanged));
|
|
|
|
caps.CallOnMainThreadWhenCapsAvailable(task);
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 22:05:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-16 03:14:56 +00:00
|
|
|
bool
|
|
|
|
MediaSourceDecoder::IsActiveReader(MediaDecoderReader* aReader)
|
|
|
|
{
|
2015-06-11 23:26:57 +00:00
|
|
|
return !mIsUsingFormatReader && GetReader()->IsActiveReader(aReader);
|
2015-01-16 03:14:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 07:52:12 +00:00
|
|
|
double
|
|
|
|
MediaSourceDecoder::GetDuration()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-06-02 21:44:26 +00:00
|
|
|
return ExplicitDuration();
|
2015-02-12 07:52:12 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 07:52:13 +00:00
|
|
|
#undef MSE_DEBUG
|
|
|
|
#undef MSE_DEBUGV
|
|
|
|
|
2013-09-27 05:22:37 +00:00
|
|
|
} // namespace mozilla
|