Files
palemoon27/dom/media/MediaDecoderReaderWrapper.cpp
T
roytam1 167ec3b0a2 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1259018 - Part a: Use early returns in nsExternalProtocolHandler::NewChannel2; r=jst (159053ad25)
- Bug 1259018 - Part b: Use early returns and stop accepting null in nsExternalProtocolHandler::HaveExternalProtocolHandler; r=jst (356b509c9a)
- Bug 1259018 - Part c: Pass arguments to the nsExtProtocolChannel constructor; r=jst (c3cab1801c)
- Bug 1261020 - part 1 - implement SeekTask; r=jwwang (d3df4d5de6)
- Bug 1261020 - part 2 - modify MDSM to adopt SeekTask; r=jwwang (b51efe45c8)
- Bug 1265296 - Move SeekTask from namespace mozilla::media to namespace mozilla. r=kaku. (cc5665837b)
- Bug 1265315. Part 1 - remove use of MediaDecoderReader from SeekTask. r=kaku. (ee7a8e9032)
- Bug 1265315. Part 2 - rename mReaderWrapper to mReader for less verbose coding. r=kaku. (f7426bdc4c)
- Bug 1265311. Part 1 - Add more proxy functions to MediaDecoderReaderWrapper. r=jya. (73d59fd4c3)
- Bug 1265311. Part 2 - constify some functions. r=jya. (87388fc7fd)
- Bug 1266304. Part 1 - Merge MediaDecoderReader::AudioDataPromise and MediaDecoderReader::VideoDataPromise. r=kaku. (30ddbd7b03)
- Bug 1266304. Part 2 - remove unnecessary template parameters. r=kaku. (84b012ef96)
- Bug 1265629 - Remove unused MediaDecoderReader::BreakCycles(). r=kaku. (7144e46f3b)
- Bug 1215439 - Don't call ResetDecode() in the destructor of MediaDecoderReader. r=gerald. (be8896cd3b)
- Bug 1265634. Part 1 - add more proxy functions to MediaDecoderReaderWrapper and remove unused members from MDSM. r=kaku. (eacba1547d)
- Bug 1265634. Part 2 - rename mReaderWrapper for less wording. r=kaku. (06179b09fa)
- Bug 1265634. Part 3 - constify some functions. r=jya. (2cc6d72652)
- Bug 1256336 - Do not use httpChannelChild after Send__delete is called. r=jduell (d3ec2be783)
- Bug 1261070 - Move Send__delete__ to FailedAsyncOpen. r=jduell (f3a2d0e2b9)
- Bug 1229369 - Part 2: Drop the aChannel argument to HttpChannelChild::ShouldInterceptURI() (f770dbdced)
- Bug 1225756 - Part1: Let the regular veto handlers check on redirects then check mRedirectChannelChild in the result callback. r=mayhemer (13b87a6a84)
- fix mispatch (04c842e2ba)
- Bug 1210077 - Null check gNeckoChild in HttpChannelChild::ResetInterception(); r=mcmanus (9e1bb91331)
- Bug 1233630 - Remove MediaDecoderStateMachine::AudioDecodedUsecs. r=kikuo. (4e83b86ed1)
- Bug 1261312 - Make sure that audio/video decode task is filed again. r=jwwang (90d90c890a)
- Bug 1238347 - stop prerolling when decoding starts if we are waiting for data. r=cpearce. (dfa46a223c)
- Bug 1242845 - Make the computation of MediaDecoderStateMachine::HasLowUndecodedData() more accurate and consistent. r=jya. (9f3a8a3e2c)
- Bug 1242845. Part 2 - fix format nits. r=me. (9da58b5145)
- Bug 1237174 - Remove the unnecessary code since mState won't change in the case of DECODER_STATE_COMPLETED. r=bechen. (ad706de284)
- Bug 1237806 - update playback position before entering buffering mode so the currentTime of the media element is more accurate during buffering. r=jya. (d491291b12)
- Bug 1238343 - Update MDSM::mIsAudioPrerolling/mIsVideoPrerolling when playback rate changes. r=cpearce. (532419a3b5)
- Bug 1235966 - reset mAudioCompleted and mVideoCompleted when switching MediaSink so they are resolved by the new MediaSink. r=kikuo. (5c701026e1)
2024-07-02 22:04:09 +08:00

335 lines
10 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 "mozilla/MozPromise.h"
#include "MediaDecoderReaderWrapper.h"
namespace mozilla {
extern LazyLogModule gMediaDecoderLog;
#undef LOG
#define LOG(...) \
MOZ_LOG(gMediaDecoderLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
// StartTimeRendezvous is a helper class that quarantines the first sample
// until it gets a sample from both channels, such that we can be guaranteed
// to know the start time by the time On{Audio,Video}Decoded is called on MDSM.
class StartTimeRendezvous {
typedef MediaDecoderReader::MediaDataPromise MediaDataPromise;
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(StartTimeRendezvous);
public:
StartTimeRendezvous(AbstractThread* aOwnerThread,
bool aHasAudio,
bool aHasVideo,
bool aForceZeroStartTime)
: mOwnerThread(aOwnerThread)
{
if (aForceZeroStartTime) {
mAudioStartTime.emplace(0);
mVideoStartTime.emplace(0);
return;
}
if (!aHasAudio) {
mAudioStartTime.emplace(INT64_MAX);
}
if (!aHasVideo) {
mVideoStartTime.emplace(INT64_MAX);
}
}
void Destroy()
{
mAudioStartTime = Some(mAudioStartTime.refOr(INT64_MAX));
mVideoStartTime = Some(mVideoStartTime.refOr(INT64_MAX));
mHaveStartTimePromise.RejectIfExists(false, __func__);
}
RefPtr<HaveStartTimePromise> AwaitStartTime()
{
if (HaveStartTime()) {
return HaveStartTimePromise::CreateAndResolve(true, __func__);
}
return mHaveStartTimePromise.Ensure(__func__);
}
template<MediaData::Type SampleType>
RefPtr<MediaDataPromise>
ProcessFirstSample(MediaData* aData)
{
typedef typename MediaDataPromise::Private PromisePrivate;
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MaybeSetChannelStartTime<SampleType>(aData->mTime);
RefPtr<PromisePrivate> p = new PromisePrivate(__func__);
RefPtr<MediaData> data = aData;
RefPtr<StartTimeRendezvous> self = this;
AwaitStartTime()->Then(
mOwnerThread, __func__,
[p, data, self] () {
MOZ_ASSERT(self->mOwnerThread->IsCurrentThreadIn());
p->Resolve(data, __func__);
},
[p] () {
p->Reject(MediaDecoderReader::CANCELED, __func__);
});
return p.forget();
}
template<MediaData::Type SampleType>
void FirstSampleRejected(MediaDecoderReader::NotDecodedReason aReason)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
if (aReason == MediaDecoderReader::DECODE_ERROR) {
mHaveStartTimePromise.RejectIfExists(false, __func__);
} else if (aReason == MediaDecoderReader::END_OF_STREAM) {
LOG("StartTimeRendezvous=%p SampleType(%d) Has no samples.",
this, SampleType);
MaybeSetChannelStartTime<SampleType>(INT64_MAX);
}
}
bool HaveStartTime() const
{
return mAudioStartTime.isSome() && mVideoStartTime.isSome();
}
int64_t StartTime() const
{
int64_t time = std::min(mAudioStartTime.ref(), mVideoStartTime.ref());
return time == INT64_MAX ? 0 : time;
}
private:
~StartTimeRendezvous() {}
template<MediaData::Type SampleType>
void MaybeSetChannelStartTime(int64_t aStartTime)
{
if (ChannelStartTime(SampleType).isSome()) {
// If we're initialized with aForceZeroStartTime=true, the channel start
// times are already set.
return;
}
LOG("StartTimeRendezvous=%p Setting SampleType(%d) start time to %lld",
this, SampleType, aStartTime);
ChannelStartTime(SampleType).emplace(aStartTime);
if (HaveStartTime()) {
mHaveStartTimePromise.ResolveIfExists(true, __func__);
}
}
Maybe<int64_t>& ChannelStartTime(MediaData::Type aType)
{
return aType == MediaData::AUDIO_DATA ? mAudioStartTime : mVideoStartTime;
}
MozPromiseHolder<HaveStartTimePromise> mHaveStartTimePromise;
RefPtr<AbstractThread> mOwnerThread;
Maybe<int64_t> mAudioStartTime;
Maybe<int64_t> mVideoStartTime;
};
MediaDecoderReaderWrapper::MediaDecoderReaderWrapper(bool aIsRealTime,
AbstractThread* aOwnerThread,
MediaDecoderReader* aReader)
: mForceZeroStartTime(aIsRealTime || aReader->ForceZeroStartTime())
, mOwnerThread(aOwnerThread)
, mReader(aReader)
{}
MediaDecoderReaderWrapper::~MediaDecoderReaderWrapper()
{}
media::TimeUnit
MediaDecoderReaderWrapper::StartTime() const
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mShutdown);
return media::TimeUnit::FromMicroseconds(mStartTimeRendezvous->StartTime());
}
RefPtr<MediaDecoderReaderWrapper::MetadataPromise>
MediaDecoderReaderWrapper::ReadMetadata()
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mShutdown);
return InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
&MediaDecoderReader::AsyncReadMetadata)
->Then(mOwnerThread, __func__, this,
&MediaDecoderReaderWrapper::OnMetadataRead,
&MediaDecoderReaderWrapper::OnMetadataNotRead)
->CompletionPromise();
}
RefPtr<HaveStartTimePromise>
MediaDecoderReaderWrapper::AwaitStartTime()
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mShutdown);
return mStartTimeRendezvous->AwaitStartTime();
}
RefPtr<MediaDecoderReaderWrapper::MediaDataPromise>
MediaDecoderReaderWrapper::RequestAudioData()
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mShutdown);
auto p = InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
&MediaDecoderReader::RequestAudioData);
if (!mStartTimeRendezvous->HaveStartTime()) {
p = p->Then(mOwnerThread, __func__, mStartTimeRendezvous.get(),
&StartTimeRendezvous::ProcessFirstSample<MediaData::AUDIO_DATA>,
&StartTimeRendezvous::FirstSampleRejected<MediaData::AUDIO_DATA>)
->CompletionPromise();
}
return p->Then(mOwnerThread, __func__, this,
&MediaDecoderReaderWrapper::OnSampleDecoded,
&MediaDecoderReaderWrapper::OnNotDecoded)
->CompletionPromise();
}
RefPtr<MediaDecoderReaderWrapper::MediaDataPromise>
MediaDecoderReaderWrapper::RequestVideoData(bool aSkipToNextKeyframe,
media::TimeUnit aTimeThreshold)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mShutdown);
if (aTimeThreshold.ToMicroseconds() > 0 &&
mStartTimeRendezvous->HaveStartTime()) {
aTimeThreshold += StartTime();
}
auto p = InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
&MediaDecoderReader::RequestVideoData,
aSkipToNextKeyframe, aTimeThreshold.ToMicroseconds());
if (!mStartTimeRendezvous->HaveStartTime()) {
p = p->Then(mOwnerThread, __func__, mStartTimeRendezvous.get(),
&StartTimeRendezvous::ProcessFirstSample<MediaData::VIDEO_DATA>,
&StartTimeRendezvous::FirstSampleRejected<MediaData::VIDEO_DATA>)
->CompletionPromise();
}
return p->Then(mOwnerThread, __func__, this,
&MediaDecoderReaderWrapper::OnSampleDecoded,
&MediaDecoderReaderWrapper::OnNotDecoded)
->CompletionPromise();
}
RefPtr<MediaDecoderReader::SeekPromise>
MediaDecoderReaderWrapper::Seek(SeekTarget aTarget, media::TimeUnit aEndTime)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
aTarget.SetTime(aTarget.GetTime() + StartTime());
return InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
&MediaDecoderReader::Seek, aTarget,
aEndTime.ToMicroseconds());
}
RefPtr<MediaDecoderReaderWrapper::WaitForDataPromise>
MediaDecoderReaderWrapper::WaitForData(MediaData::Type aType)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
return InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
&MediaDecoderReader::WaitForData, aType);
}
RefPtr<MediaDecoderReaderWrapper::BufferedUpdatePromise>
MediaDecoderReaderWrapper::UpdateBufferedWithPromise()
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
return InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
&MediaDecoderReader::UpdateBufferedWithPromise);
}
void
MediaDecoderReaderWrapper::ReleaseMediaResources()
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
nsCOMPtr<nsIRunnable> r =
NS_NewRunnableMethod(mReader, &MediaDecoderReader::ReleaseMediaResources);
mReader->OwnerThread()->Dispatch(r.forget());
}
void
MediaDecoderReaderWrapper::SetIdle()
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
nsCOMPtr<nsIRunnable> r =
NS_NewRunnableMethod(mReader, &MediaDecoderReader::SetIdle);
mReader->OwnerThread()->Dispatch(r.forget());
}
void
MediaDecoderReaderWrapper::ResetDecode()
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
nsCOMPtr<nsIRunnable> r =
NS_NewRunnableMethod(mReader, &MediaDecoderReader::ResetDecode);
mReader->OwnerThread()->Dispatch(r.forget());
}
RefPtr<ShutdownPromise>
MediaDecoderReaderWrapper::Shutdown()
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
mShutdown = true;
if (mStartTimeRendezvous) {
mStartTimeRendezvous->Destroy();
mStartTimeRendezvous = nullptr;
}
return InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
&MediaDecoderReader::Shutdown);
}
void
MediaDecoderReaderWrapper::OnMetadataRead(MetadataHolder* aMetadata)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
if (mShutdown) {
return;
}
// Set up the start time rendezvous if it doesn't already exist (which is
// generally the case, unless we're coming out of dormant mode).
if (!mStartTimeRendezvous) {
mStartTimeRendezvous = new StartTimeRendezvous(
mOwnerThread, aMetadata->mInfo.HasAudio(),
aMetadata->mInfo.HasVideo(), mForceZeroStartTime);
RefPtr<MediaDecoderReaderWrapper> self = this;
mStartTimeRendezvous->AwaitStartTime()->Then(
mOwnerThread, __func__,
[self] () {
NS_ENSURE_TRUE_VOID(!self->mShutdown);
self->mReader->DispatchSetStartTime(self->StartTime().ToMicroseconds());
},
[] () {
NS_WARNING("Setting start time on reader failed");
});
}
}
void
MediaDecoderReaderWrapper::OnSampleDecoded(MediaData* aSample)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
if (!mShutdown) {
aSample->AdjustForStartTime(StartTime().ToMicroseconds());
}
}
} // namespace mozilla