Files
palemoon27/dom/media/mediasource/MediaSourceDecoder.cpp
T
roytam1 fef8b08889 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1237842 - Unlock mMutex before calling CloseActive. r=cpearce (d0677f1981)
- Bug 1230857 - Ensure GMPService has sufficient file permissions to delete GMPs. r=gerald (e7f0c4b2b6)
- Bug 1236380 - GMPStorage::mShutdown=true until Init() succeeds - r=cpearce (fde2025f4f)
- Bug 1254311: [mp4] Ignore empty raw sample. r=cpearce (216a9417c3)
- Bug 1215115 - part1: Replace the vorbis by opus in MediaEncoder and also reomve the VorbisTrackEncoder files. r=rillian (760c559e3c)
- Bug 1215115 - part2: Mux opus into webm, remove bitdepth. r=rillian (1c996f0aee)
- Bug 1215115 - part3: Fix gtest. Remove TestVorbisTrackEncoder.cpp. r=rillian (5a68915a4a)
- Bug 1215115 - part4: Enable MOZ_WEBM_ENCODER by default. r=ted (6638b7fffb)
- Bug 1257318: Pass TRACK_EVENT_ENDED events through to the TrackEncoders r=padenot (b92b2dcc94)
- Bug 1261007 - Part 3 - Remove the same/redundant code of checking the unique image. r=jolin (608e6477bc)
- Bug 1243611 - When EOS, call vpx_codec_encode correctly. r=rillian (83887c89c8)
- Bug 1260353 - Remove unnecessary method AnimValuesStyleRule::AddPropertiesToSet() r=hiro (36f5e7fcc9)
- Bug 1213775: VP8 automatic resizing breaks ffmpeg-based players; turn it off in VP8TrackEncoder r=jya (23c2a27371)
- Bug 1185171 - Modify gmp-test-output-protection.h to prevent failure on machines without a physical monitor attached. r=bobowen (8375c5075d)
- Bug 1185171: Add 0xc02625e5 as a valid failure code for GMPOutputProtection test. r=cpearce (1d10a75aeb)
- Bug 1151746 - Origin tuples in should include schemes. r=edwin (32610b0cfa)
- Bug 1180101 - Test 0 length atom inside moov; r=jya (3fae8aee45)
- Bug 1244523: [mp4] P4. Add gtest. r=kentuckyfriedtakahe (3f71b5060a)
- Bug 1255626: [gtest] Properly shutdown task queue should error occurs. r=gerald (4ec1bf360e)
- Bug 1224363 - Added vp8/ivf test case - r=rillian Bug 1224369 - p1: Test cases given as list - r=rillian Bug 1224369 - p2: Added vp8/ivf test case - r=rillian Bug 1224361 - Added vp8/ivf test case - r=rillian (595ebe09be)
- Bug 1231075. Respect the timestamp of video frames and don't pop frames as fast as we can in real-time mode. r=roc. (b72329c0fa)
- Bug 1237160: Do not count frames not composited as dropped. r=cpearce (e7e18d0700)
- Bug 1233648 - Fix some insufficient includes. r=kinetik. (e36cdd3e05)
- Bug 1216460 - [1.1] Refactor data types, fix logs and prevent harmful type promotions in SourceBuffer eviction handling. r=jya (047a7ca64f)
- Bug 1259916: [MSE] P1. Fix eviction. r=gerald (13195f392b)
- Bug 1216460 - [2.2] Refactor SourceBuffer frame eviction and threshold defaults. r=jya (105962c942)
- Bug 1259274: [MSE] P1. Remove unnecessary abstraction layer. r=gerald (e7b7603f30)
- Bug 1259274: [MSE] P2. Remove unused code path. r=gerald (dce9fa447c)
- Bug 1259274: [MSE] P3. Refactor handling of tasks so they only ever run concurrently. r=gerald (9c3f40d9b8)
- Bug 1259274: [MSE] P4. Add AutoTaskQueue convenience class. r=gerald Just like TaskQueue, but doesn't require to be shutdown. (0310ff2b7f)
- Bug 1259274: [MSE] P5. Use new AutoTaskQueue with MSE objects. r=gerald (3f72558eb2)
- Bug 1259916: [MSE] P2. Bump audio source buffer eviction threshold to 30MB. r=gerald (2ffe148c1a)
- Bug 1259916: [MSE] P3. Simplify eviction calculation logic. r=gerald (11250c02bc)
- Bug 1199879: [MSE] Use latest demux end time to detect discontinuities. r=gerald (f89bdd763f)
- Bug 1239983 - Diags around TrackBuffersMgr promises - r=jya (57f3e58636)
- Bug 1258410: [MSE] P1. Abort if mInputDemuxer has been reset. r=gerald (07ca58adb0)
- Bug 1258410: [MSE] P2. Disconnect init promise if any pending. r=gerald (0627c5a174)
- Bug 1259985 - Add missing return after null-check - r=jya (b6ee457b89)
- bit of Bwqug 1259274: [MSE] P3 (200d743676)
- Bug 1216560 - [3.1] Make eviction thresholds const. r=jya (b44c78f999)
- Bug 1259473 - per comment 14, move actions involving |this| to Init() from the constructor. r=jya. (30c402aacb)
- Bug 1258562: MSE] Abort if MediaSource has been shutdown. r=gerald (6fce6bc9db)
- Bug 1246358: [MSE] Take pre-roll time into consideration when seeking. r=gerald (dacbcd7f36)
- spaces (abbb56d413)
- Bug 657791 - Update WebM demuxer to clamp cueless seeks instead of failing. r=kinetik (785ae83126)
- Bug 1219178 - [9.1] Make SeekPosition available with tests disabled. a=me for fixing build problems (cd1bdef203)
- minor format (4a718e47f2)
- Bug 1265399 - Replace 0.7071 with sqrt(0.5) in downmixing equations; r=padenot (2243d331c5)
- Bug 1265794: P1. Ensure we can always fit a complete audio frame in an audio buffer. r=rillian (37f575184c)
- Bug 1256626. Workaround Microsoft macro silliness. r=me (18930fbccd)
- Bug 1264898 - Remove unnecessary |FinishAddTracks| call in |DOMHwMediaStream::Init|. r=jesup, r=pehrsons (1b610cdb4f)
- Bug 848994 - p5. Check Silverlight presence - r=cpearce (98b4521ae3)
- Bug 848994 - p6. Analyze Windows issues - r=cpearce (9de769166a)
- Bug 848994 - p7. Filter front-end notifications - r=cpearce (e3aab89a95)
- Bug 1256533 - Use std::deque<int32_t> instead of nsDeque - r=cpearce (e21c02fcab)
2024-06-23 07:20:41 +08:00

323 lines
9.1 KiB
C++

/* -*- mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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"
#include "mozilla/Logging.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/Preferences.h"
#include "MediaDecoderStateMachine.h"
#include "MediaSource.h"
#include "MediaSourceResource.h"
#include "MediaSourceUtils.h"
#include "VideoUtils.h"
#include "MediaSourceDemuxer.h"
#include "SourceBufferList.h"
#include <algorithm>
extern mozilla::LogModule* GetMediaSourceLog();
#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__))
using namespace mozilla::media;
namespace mozilla {
MediaSourceDecoder::MediaSourceDecoder(dom::HTMLMediaElement* aElement)
: MediaDecoder(aElement)
, mMediaSource(nullptr)
, mEnded(false)
{
SetExplicitDuration(UnspecifiedNaN<double>());
}
MediaDecoder*
MediaSourceDecoder::Clone(MediaDecoderOwner* aOwner)
{
// TODO: Sort out cloning.
return nullptr;
}
MediaDecoderStateMachine*
MediaSourceDecoder::CreateStateMachine()
{
MOZ_ASSERT(NS_IsMainThread());
mDemuxer = new MediaSourceDemuxer();
mReader = new MediaFormatReader(this, mDemuxer, GetVideoFrameContainer());
return new MediaDecoderStateMachine(this, mReader);
}
nsresult
MediaSourceDecoder::Load(nsIStreamListener**)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!GetStateMachine());
SetStateMachine(CreateStateMachine());
if (!GetStateMachine()) {
NS_WARNING("Failed to create state machine!");
return NS_ERROR_FAILURE;
}
nsresult rv = GetStateMachine()->Init(this);
NS_ENSURE_SUCCESS(rv, rv);
SetStateMachineParameters();
return NS_OK;
}
media::TimeIntervals
MediaSourceDecoder::GetSeekable()
{
MOZ_ASSERT(NS_IsMainThread());
if (!mMediaSource) {
NS_WARNING("MediaSource element isn't attached");
return media::TimeIntervals::Invalid();
}
media::TimeIntervals seekable;
double duration = mMediaSource->Duration();
if (IsNaN(duration)) {
// Return empty range.
} else if (duration > 0 && mozilla::IsInfinite(duration)) {
media::TimeIntervals buffered = GetBuffered();
// 1. If live seekable range is not empty:
if (mMediaSource->HasLiveSeekableRange()) {
// 1. Let union ranges be the union of live seekable range and the
// HTMLMediaElement.buffered attribute.
media::TimeIntervals unionRanges =
buffered + mMediaSource->LiveSeekableRange();
// 2. Return a single range with a start time equal to the earliest start
// time in union ranges and an end time equal to the highest end time in
// union ranges and abort these steps.
seekable +=
media::TimeInterval(unionRanges.GetStart(), unionRanges.GetEnd());
return seekable;
}
if (buffered.Length()) {
seekable +=
media::TimeInterval(media::TimeUnit::FromSeconds(0), buffered.GetEnd());
}
} else {
seekable += media::TimeInterval(media::TimeUnit::FromSeconds(0),
media::TimeUnit::FromSeconds(duration));
}
MSE_DEBUG("ranges=%s", DumpTimeRanges(seekable).get());
return seekable;
}
media::TimeIntervals
MediaSourceDecoder::GetBuffered()
{
MOZ_ASSERT(NS_IsMainThread());
dom::SourceBufferList* sourceBuffers = mMediaSource->ActiveSourceBuffers();
if (!sourceBuffers) {
// Media source object is shutting down.
return TimeIntervals();
}
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;
}
RefPtr<ShutdownPromise>
MediaSourceDecoder::Shutdown()
{
MOZ_ASSERT(NS_IsMainThread());
MSE_DEBUG("Shutdown");
// Detach first so that TrackBuffers are unused on the main thread when
// shut down on the decode task queue.
if (mMediaSource) {
mMediaSource->Detach();
}
mDemuxer = nullptr;
return MediaDecoder::Shutdown();
}
/*static*/
already_AddRefed<MediaResource>
MediaSourceDecoder::CreateResource(nsIPrincipal* aPrincipal)
{
return RefPtr<MediaResource>(new MediaSourceResource(aPrincipal)).forget();
}
void
MediaSourceDecoder::AttachMediaSource(dom::MediaSource* aMediaSource)
{
MOZ_ASSERT(!mMediaSource && !GetStateMachine() && NS_IsMainThread());
mMediaSource = aMediaSource;
}
void
MediaSourceDecoder::DetachMediaSource()
{
MOZ_ASSERT(mMediaSource && NS_IsMainThread());
mMediaSource = nullptr;
}
void
MediaSourceDecoder::Ended(bool aEnded)
{
MOZ_ASSERT(NS_IsMainThread());
static_cast<MediaSourceResource*>(GetResource())->SetEnded(aEnded);
mEnded = true;
}
void
MediaSourceDecoder::AddSizeOfResources(ResourceSizes* aSizes)
{
MOZ_ASSERT(NS_IsMainThread());
if (GetDemuxer()) {
GetDemuxer()->AddSizeOfResources(aSizes);
}
}
void
MediaSourceDecoder::SetInitialDuration(int64_t aDuration)
{
MOZ_ASSERT(NS_IsMainThread());
// Only use the decoded duration if one wasn't already
// set.
if (!mMediaSource || !IsNaN(ExplicitDuration())) {
return;
}
double duration = aDuration;
// A duration of -1 is +Infinity.
if (aDuration >= 0) {
duration /= USECS_PER_S;
}
SetMediaSourceDuration(duration, MSRangeRemovalAction::SKIP);
}
void
MediaSourceDecoder::SetMediaSourceDuration(double aDuration, MSRangeRemovalAction aAction)
{
MOZ_ASSERT(NS_IsMainThread());
double oldDuration = ExplicitDuration();
if (aDuration >= 0) {
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;
}
SetExplicitDuration(aDuration);
} else {
SetExplicitDuration(PositiveInfinity<double>());
}
if (mMediaSource && aAction != MSRangeRemovalAction::SKIP) {
mMediaSource->DurationChange(oldDuration, aDuration);
}
}
double
MediaSourceDecoder::GetMediaSourceDuration()
{
MOZ_ASSERT(NS_IsMainThread());
return ExplicitDuration();
}
void
MediaSourceDecoder::GetMozDebugReaderData(nsAString& aString)
{
if (mReader && mDemuxer) {
mReader->GetMozDebugReaderData(aString);
mDemuxer->GetMozDebugReaderData(aString);
}
}
double
MediaSourceDecoder::GetDuration()
{
MOZ_ASSERT(NS_IsMainThread());
return ExplicitDuration();
}
MediaDecoderOwner::NextFrameStatus
MediaSourceDecoder::NextFrameBufferedStatus()
{
MOZ_ASSERT(NS_IsMainThread());
if (!mMediaSource ||
mMediaSource->ReadyState() == dom::MediaSourceReadyState::Closed) {
return MediaDecoderOwner::NEXT_FRAME_UNAVAILABLE;
}
// 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;
}
bool
MediaSourceDecoder::CanPlayThrough()
{
MOZ_ASSERT(NS_IsMainThread());
if (NextFrameBufferedStatus() == MediaDecoderOwner::NEXT_FRAME_UNAVAILABLE) {
return false;
}
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 <= 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);
}
#undef MSE_DEBUG
#undef MSE_DEBUGV
} // namespace mozilla