2014-04-14 15:24:00 +04:00
|
|
|
/* -*- mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2013-09-27 09:22:37 +04: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 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2013-09-27 09:22:37 +04:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2015-07-22 04:09:21 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2014-08-13 16:22:00 +04:00
|
|
|
#include "MediaDecoderStateMachine.h"
|
2013-10-05 12:04:39 +04:00
|
|
|
#include "MediaSource.h"
|
2014-08-13 16:22:00 +04:00
|
|
|
#include "MediaSourceResource.h"
|
2014-08-20 12:07:00 +04:00
|
|
|
#include "MediaSourceUtils.h"
|
2015-02-12 10:52:12 +03:00
|
|
|
#include "VideoUtils.h"
|
2015-06-12 02:26:57 +03:00
|
|
|
#include "MediaSourceDemuxer.h"
|
2015-07-22 04:09:21 +03:00
|
|
|
#include "SourceBufferList.h"
|
2015-12-02 07:09:47 +03:00
|
|
|
#include <algorithm>
|
2014-07-22 12:20:00 +04:00
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
extern mozilla::LogModule* GetMediaSourceLog();
|
2014-08-11 05:21:17 +04:00
|
|
|
|
2015-06-04 01:25:57 +03: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 09:22:37 +04:00
|
|
|
|
2015-06-04 00:46:08 +03:00
|
|
|
using namespace mozilla::media;
|
|
|
|
|
2013-09-27 09:22:37 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-11-18 08:22:47 +04:00
|
|
|
MediaSourceDecoder::MediaSourceDecoder(dom::HTMLMediaElement* aElement)
|
2015-10-15 06:39:45 +03:00
|
|
|
: MediaDecoder(aElement)
|
|
|
|
, mMediaSource(nullptr)
|
2015-06-12 02:26:57 +03:00
|
|
|
, mEnded(false)
|
2013-09-27 09:22:37 +04:00
|
|
|
{
|
2015-06-03 00:44:26 +03:00
|
|
|
SetExplicitDuration(UnspecifiedNaN<double>());
|
2013-09-27 09:22:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaDecoder*
|
2015-10-15 06:37:47 +03:00
|
|
|
MediaSourceDecoder::Clone(MediaDecoderOwner* aOwner)
|
2013-09-27 09:22:37 +04:00
|
|
|
{
|
|
|
|
// TODO: Sort out cloning.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaDecoderStateMachine*
|
|
|
|
MediaSourceDecoder::CreateStateMachine()
|
|
|
|
{
|
2015-09-30 01:55:21 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-09-14 10:52:30 +03:00
|
|
|
mDemuxer = new MediaSourceDemuxer();
|
2016-01-18 02:21:59 +03:00
|
|
|
mReader = new MediaFormatReader(this, mDemuxer, GetVideoFrameContainer());
|
|
|
|
return new MediaDecoderStateMachine(this, mReader);
|
2013-09-27 09:22:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-10-14 06:46:27 +03:00
|
|
|
MediaSourceDecoder::Load(nsIStreamListener**)
|
2013-09-27 09:22:37 +04:00
|
|
|
{
|
2015-09-30 01:55:21 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-04-02 20:49:01 +03:00
|
|
|
MOZ_ASSERT(!GetStateMachine());
|
|
|
|
SetStateMachine(CreateStateMachine());
|
|
|
|
if (!GetStateMachine()) {
|
2014-02-18 02:53:51 +04:00
|
|
|
NS_WARNING("Failed to create state machine!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-04-01 18:10:44 +03:00
|
|
|
nsresult rv = GetStateMachine()->Init(this);
|
2014-08-05 18:55:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
SetStateMachineParameters();
|
2015-07-23 13:39:09 +03:00
|
|
|
return NS_OK;
|
2013-09-27 09:22:37 +04:00
|
|
|
}
|
|
|
|
|
2015-05-18 09:15:47 +03:00
|
|
|
media::TimeIntervals
|
|
|
|
MediaSourceDecoder::GetSeekable()
|
2013-10-05 12:04:39 +04:00
|
|
|
{
|
2014-08-25 08:09:44 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!mMediaSource) {
|
2015-05-18 09:15:47 +03:00
|
|
|
NS_WARNING("MediaSource element isn't attached");
|
|
|
|
return media::TimeIntervals::Invalid();
|
2014-08-25 08:09:44 +04:00
|
|
|
}
|
2014-09-16 07:15:55 +04:00
|
|
|
|
2015-05-18 09:15:47 +03:00
|
|
|
media::TimeIntervals seekable;
|
2013-10-05 12:04:39 +04:00
|
|
|
double duration = mMediaSource->Duration();
|
|
|
|
if (IsNaN(duration)) {
|
|
|
|
// Return empty range.
|
|
|
|
} else if (duration > 0 && mozilla::IsInfinite(duration)) {
|
2015-06-18 00:22:10 +03:00
|
|
|
media::TimeIntervals buffered = GetBuffered();
|
2015-05-18 09:15:47 +03:00
|
|
|
if (buffered.Length()) {
|
2015-11-11 13:21:35 +03:00
|
|
|
seekable +=
|
|
|
|
media::TimeInterval(media::TimeUnit::FromSeconds(0), buffered.GetEnd());
|
2015-05-18 09:15:47 +03:00
|
|
|
}
|
2013-10-05 12:04:39 +04:00
|
|
|
} else {
|
2015-05-18 09:15:47 +03:00
|
|
|
seekable += media::TimeInterval(media::TimeUnit::FromSeconds(0),
|
|
|
|
media::TimeUnit::FromSeconds(duration));
|
2013-10-05 12:04:39 +04:00
|
|
|
}
|
2015-05-18 09:15:47 +03:00
|
|
|
MSE_DEBUG("ranges=%s", DumpTimeRanges(seekable).get());
|
|
|
|
return seekable;
|
2013-10-05 12:04:39 +04:00
|
|
|
}
|
|
|
|
|
2015-06-18 00:22:10 +03:00
|
|
|
media::TimeIntervals
|
|
|
|
MediaSourceDecoder::GetBuffered()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
dom::SourceBufferList* sourceBuffers = mMediaSource->ActiveSourceBuffers();
|
2015-12-02 04:59:18 +03:00
|
|
|
if (!sourceBuffers) {
|
|
|
|
// Media source object is shutting down.
|
|
|
|
return TimeIntervals();
|
|
|
|
}
|
2015-06-18 00:22:10 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-04 06:53:02 +03:00
|
|
|
RefPtr<ShutdownPromise>
|
2014-08-25 08:09:44 +04:00
|
|
|
MediaSourceDecoder::Shutdown()
|
|
|
|
{
|
2015-09-30 01:55:21 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-02-12 10:52:13 +03:00
|
|
|
MSE_DEBUG("Shutdown");
|
2014-11-04 07:24:52 +03:00
|
|
|
// Detach first so that TrackBuffers are unused on the main thread when
|
|
|
|
// shut down on the decode task queue.
|
2014-08-28 07:44:58 +04:00
|
|
|
if (mMediaSource) {
|
|
|
|
mMediaSource->Detach();
|
|
|
|
}
|
2015-07-14 06:12:29 +03:00
|
|
|
mDemuxer = nullptr;
|
2014-11-04 07:24:52 +03:00
|
|
|
|
2016-01-04 06:53:02 +03:00
|
|
|
return MediaDecoder::Shutdown();
|
2014-08-25 08:09:44 +04:00
|
|
|
}
|
|
|
|
|
2014-04-14 15:24:00 +04:00
|
|
|
/*static*/
|
|
|
|
already_AddRefed<MediaResource>
|
2014-10-15 05:17:03 +04:00
|
|
|
MediaSourceDecoder::CreateResource(nsIPrincipal* aPrincipal)
|
2014-04-14 15:24:00 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
return RefPtr<MediaResource>(new MediaSourceResource(aPrincipal)).forget();
|
2014-04-14 15:24:00 +04:00
|
|
|
}
|
|
|
|
|
2013-09-27 09:22:37 +04:00
|
|
|
void
|
2013-11-18 08:22:47 +04:00
|
|
|
MediaSourceDecoder::AttachMediaSource(dom::MediaSource* aMediaSource)
|
2013-09-27 09:22:37 +04:00
|
|
|
{
|
2015-04-02 20:49:01 +03:00
|
|
|
MOZ_ASSERT(!mMediaSource && !GetStateMachine() && NS_IsMainThread());
|
2013-09-27 09:22:37 +04:00
|
|
|
mMediaSource = aMediaSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceDecoder::DetachMediaSource()
|
|
|
|
{
|
2014-08-25 08:09:44 +04:00
|
|
|
MOZ_ASSERT(mMediaSource && NS_IsMainThread());
|
2013-09-27 09:22:37 +04:00
|
|
|
mMediaSource = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-26 11:25:09 +04:00
|
|
|
void
|
2015-03-23 13:08:05 +03:00
|
|
|
MediaSourceDecoder::Ended(bool aEnded)
|
2014-08-26 11:25:09 +04:00
|
|
|
{
|
2015-09-30 01:55:21 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-23 13:08:05 +03:00
|
|
|
static_cast<MediaSourceResource*>(GetResource())->SetEnded(aEnded);
|
2015-06-12 02:26:57 +03:00
|
|
|
mEnded = true;
|
2014-08-26 11:25:09 +04:00
|
|
|
}
|
|
|
|
|
2015-08-21 01:10:33 +03:00
|
|
|
void
|
|
|
|
MediaSourceDecoder::AddSizeOfResources(ResourceSizes* aSizes)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (GetDemuxer()) {
|
|
|
|
GetDemuxer()->AddSizeOfResources(aSizes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 07:11:33 +03:00
|
|
|
void
|
2015-02-04 12:20:15 +03:00
|
|
|
MediaSourceDecoder::SetInitialDuration(int64_t aDuration)
|
2014-11-12 07:11:33 +03:00
|
|
|
{
|
2015-06-03 00:18:47 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-12 07:11:33 +03:00
|
|
|
// Only use the decoded duration if one wasn't already
|
|
|
|
// set.
|
2015-06-03 00:44:26 +03:00
|
|
|
if (!mMediaSource || !IsNaN(ExplicitDuration())) {
|
2014-08-25 08:09:44 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-11-12 07:11:33 +03:00
|
|
|
double duration = aDuration;
|
2015-01-16 15:49:01 +03:00
|
|
|
// A duration of -1 is +Infinity.
|
|
|
|
if (aDuration >= 0) {
|
|
|
|
duration /= USECS_PER_S;
|
|
|
|
}
|
2015-02-04 12:20:15 +03:00
|
|
|
SetMediaSourceDuration(duration, MSRangeRemovalAction::SKIP);
|
2014-11-12 07:11:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-16 15:49:02 +03:00
|
|
|
MediaSourceDecoder::SetMediaSourceDuration(double aDuration, MSRangeRemovalAction aAction)
|
2014-11-12 07:11:33 +03:00
|
|
|
{
|
2015-06-03 00:18:47 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-06-03 00:44:26 +03:00
|
|
|
double oldDuration = ExplicitDuration();
|
2015-01-16 15:49:01 +03:00
|
|
|
if (aDuration >= 0) {
|
2015-02-12 10:52:12 +03: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-03 00:44:26 +03:00
|
|
|
SetExplicitDuration(aDuration);
|
2015-01-16 15:49:01 +03:00
|
|
|
} else {
|
2015-06-03 00:44:26 +03:00
|
|
|
SetExplicitDuration(PositiveInfinity<double>());
|
2015-01-16 15:49:01 +03:00
|
|
|
}
|
2015-01-16 15:49:02 +03:00
|
|
|
|
2015-06-03 00:18:47 +03:00
|
|
|
if (mMediaSource && aAction != MSRangeRemovalAction::SKIP) {
|
|
|
|
mMediaSource->DurationChange(oldDuration, aDuration);
|
2014-11-12 07:11:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
MediaSourceDecoder::GetMediaSourceDuration()
|
|
|
|
{
|
2015-09-30 01:55:21 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-06-03 00:44:26 +03:00
|
|
|
return ExplicitDuration();
|
2014-08-25 08:09:44 +04:00
|
|
|
}
|
|
|
|
|
2015-01-29 05:35:58 +03:00
|
|
|
void
|
|
|
|
MediaSourceDecoder::GetMozDebugReaderData(nsAString& aString)
|
|
|
|
{
|
2016-01-18 07:34:07 +03:00
|
|
|
if (mReader && mDemuxer) {
|
|
|
|
mReader->GetMozDebugReaderData(aString);
|
|
|
|
mDemuxer->GetMozDebugReaderData(aString);
|
|
|
|
}
|
2015-01-29 05:35:58 +03:00
|
|
|
}
|
|
|
|
|
2015-02-12 10:52:12 +03:00
|
|
|
double
|
|
|
|
MediaSourceDecoder::GetDuration()
|
|
|
|
{
|
2015-09-30 01:55:21 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-06-03 00:44:26 +03:00
|
|
|
return ExplicitDuration();
|
2015-02-12 10:52:12 +03:00
|
|
|
}
|
|
|
|
|
2015-12-02 05:01:40 +03:00
|
|
|
MediaDecoderOwner::NextFrameStatus
|
|
|
|
MediaSourceDecoder::NextFrameBufferedStatus()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-02-10 09:06:18 +03:00
|
|
|
|
|
|
|
if (!mMediaSource ||
|
|
|
|
mMediaSource->ReadyState() == dom::MediaSourceReadyState::Closed) {
|
|
|
|
return MediaDecoderOwner::NEXT_FRAME_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
2015-12-02 05:01:40 +03:00
|
|
|
// Next frame hasn't been decoded yet.
|
|
|
|
// Use the buffered range to consider if we have the next frame available.
|
|
|
|
TimeUnit currentPosition = TimeUnit::FromMicroseconds(CurrentPosition());
|
|
|
|
TimeInterval interval(currentPosition,
|
|
|
|
currentPosition + media::TimeUnit::FromMicroseconds(DEFAULT_NEXT_FRAME_AVAILABLE_BUFFERED),
|
|
|
|
MediaSourceDemuxer::EOS_FUZZ);
|
|
|
|
return GetBuffered().Contains(interval)
|
|
|
|
? MediaDecoderOwner::NEXT_FRAME_AVAILABLE
|
|
|
|
: MediaDecoderOwner::NEXT_FRAME_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
2015-12-02 07:09:47 +03:00
|
|
|
bool
|
|
|
|
MediaSourceDecoder::CanPlayThrough()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-03-22 02:34:30 +03:00
|
|
|
|
|
|
|
if (NextFrameBufferedStatus() == MediaDecoderOwner::NEXT_FRAME_UNAVAILABLE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-02 07:09:47 +03:00
|
|
|
if (IsNaN(mMediaSource->Duration())) {
|
|
|
|
// Don't have any data yet.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
TimeUnit duration = TimeUnit::FromSeconds(mMediaSource->Duration());
|
|
|
|
TimeUnit currentPosition = TimeUnit::FromMicroseconds(CurrentPosition());
|
|
|
|
if (duration.IsInfinite()) {
|
|
|
|
// We can't make an informed decision and just assume that it's a live stream
|
|
|
|
return true;
|
|
|
|
} else if (duration <= currentPosition) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// If we have data up to the mediasource's duration or 30s ahead, we can
|
|
|
|
// assume that we can play without interruption.
|
|
|
|
TimeUnit timeAhead =
|
|
|
|
std::min(duration, currentPosition + TimeUnit::FromSeconds(30));
|
|
|
|
TimeInterval interval(currentPosition,
|
|
|
|
timeAhead,
|
|
|
|
MediaSourceDemuxer::EOS_FUZZ);
|
|
|
|
return GetBuffered().Contains(interval);
|
|
|
|
}
|
|
|
|
|
2015-02-12 10:52:13 +03:00
|
|
|
#undef MSE_DEBUG
|
|
|
|
#undef MSE_DEBUGV
|
|
|
|
|
2013-09-27 09:22:37 +04:00
|
|
|
} // namespace mozilla
|