Files
palemoon27/dom/media/MediaDecoderReaderWrapper.h
T
roytam1 061201563e import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1274192 part 1 - make the MediaDecoderReaderWrapper as a proxy of WaitForData(); r=jwwang (9debdca9a9)
- Bug 1274192 part 2 - make MDSM and SeekTask to adopt new MediaDecoderReaderWrapper API; r=jwwang (156f924dc9)
- Bug 1276495: Don't reset audio promises for video only seek. r=jwwang,jya (c4632c7ff6)
- Bug 1276318, part 1 - Fix leading tabs in ipc/glue. r=billm (46bcc4ccb3)
- Bug 1035125 Part 1: Back out changeset 1910714b56c6 and associated subsequent changes. r=bsmedberg (3ead5efb66)
- Bug 1035125 Part 2: Back out changeset 3c59642f6445 and associated subsequent changes. r=glandium (e86b4f4651)
- Bug 1035125 Part 3: Back out changeset fa15c3e929d0 and associated subsequent changes. r=glandium (49585d6caf)
- Bug 1188045 - Part 2: Delay-load xul.dll when built with clang-cl (90877526a3)
- Bug 1188045 - Part 1: Move the definition of sandboxTarget::Instance() out-of-line; r=bobowen,glandium (5523a15f0e)
- Bug 1035125 Part 4: Back out changeset 8ae39d920f5c and associated subsequent changes. r=glandium (4b952b6311)
- Bug 1035125 Part 5: Back out changeset baa3f852133b and associated subsequent changes. r=glandium (0b08d2e71d)
- Bug 1035125 Part 6: Take Chromium commit 3181ba39ee787e1b40f4aea4be23f4f666ad0945 to add Windows 10 version to enumeration. r=aklotz (af62c8065f)
- Bug 1035125 Part 7: Remove unused functions in security/sandbox/chromium/base/time/time.h to avoid nspr dependency. r=aklotz (e10430c6ae)
- Bug 1035125 Part 8: Pass sandboxing pointers through XRE_InitChildProcess instead of linking to more functions in xul. r=aklotz,glandium (07590ba9cd)
- Bug 1035125 Part 9: Link Chromium sandbox into firefox.exe instead of having a separate DLL. r=aklotz,glandium (ae40d509d5)
- Bug 1270752 - Fix lifetime of buffer passed to PR_SetEnv(). r=jduell (bfce06ebea)
- Bug 1275117 - Fix static strings leaks when mozlogging is on. r=jduell (8e9066c5d7)
- Bug 1114647 - Use firefox for child processes instead of plugin-container. r=ted (4e5871566d)
- Bug 1271169 - Move all device binding code into librlz and remove static runtime requirement. r=glandium (fc9cfccbec)
- Bug 1271169 - Move EME/GMP device binding code into GMPDeviceBinding.h/cpp. r=gerald (3fabde64b8)
- Bug 1273048 - Add MOZ_GCC_VERSION_AT_MOST macro. r=froydnj (cfb5acdbe6)
- Bug 1269968 - Remove unnecessary deduction on RefPtr.h. r=froydnj (30cf9bbf47)
- Bug 525063 - add attribute to mark member variables that should be skipped by clang plugin initialization checker. r=nfroyd (78aa863f88)
- Bug 1278391 - add move constructor to EnumeratedArray (r=njn) (5effe4e473)
- Bug 1268518: Update basic-integer tests for rotations + clean up; r=sunfish (46827b6211)
- Bug 1270370 - Part 1: Move SetIteratorObject to MapObject.h; r=jorendorff (01a27fbd50)
- Bug 1270370 - Part 2: Expose MapIterator and SetIterator through ESClassValues; r=jorendorff (3edb3c7551)
- Bug 1267551 (part 3) - Use MOZ_MUST_USE more in js/src/builtin/. r=jonco. (ea96d0fae0)
- Bug 1267551 (part 4) - Use MOZ_MUST_USE more in js/src/asmjs/. r=bbouvier. (3e622c736d)
- Bug 1268910: Refactor WebAssembly trap handling; r=luke (b59394a287)
- Bug 1268910: Trap on edge cases for integer div/mod; r=sunfish (3d85230cc8)
- Bug 1267551 (part 5) - Use MOZ_MUST_USE more in js/src/ctypes/. r=sfink. (e6580fb645)
- Bug 1267551 (part 6) - Remove dead GenerateBlockId declaration. r=jorendorff. (74afcc549e)
- Bug 1240072 - Add public JS APIs related to modules r=shu Bug 1267551 (part 7) - Use MOZ_MUST_USE more in js/src/frontend/. r=jorendorff. (5275c57768)
- Bug 1267551 (part 8) - Use MOZ_MUST_USE more in js/src/gc/. r=terrence. (2572cc1c58)
- Bug 1261063 - Part 1 - Remove public Telemetry functions that return raw histogram instances. r=chutten (3a68653c8f)
- Bug 1261063 - Part 2 - Expose C++ function to clear Telemetry histograms. r=chutten (1f42f6aa1e)
- Bug 1261063 - Part 3 - Make RasterImage use the public Telemetry API. r=tnikkel (0f564f3836)
- Bug 1261063 - Part 4 - Use a count histogram for WebRTC call counts. r=jesup (b6b7b649e0)
- Bug 1270073 - Point to Telemetry documentation in Telemetry.h. r=chutten (517279b284)
- Bug 1261052 - Move C++ histogram implementation code into a separate module. r=gfritzsche. (66bcb1e167)
- Bug 1145164 - Allow non-unitary increments to count histograms. r=gfritzsche (507e076e28)
- Bug 1173447 - Add test for incremental pre-barriers when storing things under roots, r=jonco (caf8bd16be)
- Bug 1267551 (part 10) - Use MOZ_MUST_USE in AutoVectorRooterBase. r=terrence. (2c5ed87a9f)
- This catches a missing check. (b2fcd30fc5)
- Bug 1267551 (part 11b) - Follow-up to fix Android bustage. r=me (f464ca7b98)
- Bug 1271854 - Part 1: Allow specifying multiple GC zeal levels; r=terrence (da3c3e1281)
- Bug 1271854 - Part 2: Allow specifying zeal modes by name as well; r=terrence (f63926e45b)
- Bug 1271854 - Part 3: Avoid saving the GC zeal string inside the JS shell; r=terrence (f09b262e55)
- Bug 1270062 - Unbust Windows full duplex. a=Tomcat (3c8b5887b6)
- Bug 1270062 - Update libcubeb. rs=jesup (71805cb0e0)
- Bug 1270004 - Update libcubeb. rs=jesup (2d90b9662f)
- Bug 1280280 - Update cubeb to 073c9f011114. r=kinetik (6bee6a7f21)
- Bug 1280280 - Update media/libcube/update.sh to add cubeb_jack.cpp, and sort the list of files. r=kinetik (430b7533c4)
- Bug 1272604 - Add a zeal mode to check the heap after a moving GC r=terrence (85c018d71f)
- Bug 1272604 - Fix bad implicit conversion constructor build error r=me (5c4e1dbc44)
- Bug 1272604 - Fix hazard analysis failure r=me (0591aa2dca)
- Bug 1232417 - Use a Variant to represent the CrossCompartmentWrapperMap key; r=jonco (19e759f1ba)
- Bug 1269928 - Fold DirectProxyHandler into Wrapper, which is now its only (non-test) consumer. r=efaust (7550dde64d)
- Bug 1273639 - Add a nonunified spidermonkey build, r=terrence (065364af2e)
- Bug 1273639 - Fix nonunified spidermonkey builds, r=terrence (626042a5e0)
- Bug 1273180 - Trigger major GC if necessary after minor GC r=terrence (cab82a4146)
- Bug 1268992 - Assert that the heap is empty after a shutdown GC; r=jonco (35e37de1b2)
- Bug 1273908 - Refactor GC to make it easier to add new kinds of GC thing r=terrence (edc8c4d66a)
2024-10-09 16:54:32 +08:00

464 lines
15 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/. */
#ifndef MediaDecoderReaderWrapper_h_
#define MediaDecoderReaderWrapper_h_
#include "mozilla/AbstractThread.h"
#include "mozilla/RefPtr.h"
#include "nsISupportsImpl.h"
#include "MediaDecoderReader.h"
#include "MediaCallbackID.h"
namespace mozilla {
class StartTimeRendezvous;
typedef MozPromise<bool, bool, /* isExclusive = */ false> HaveStartTimePromise;
/**
* A wrapper around MediaDecoderReader to offset the timestamps of Audio/Video
* samples by the start time to ensure MDSM can always assume zero start time.
* It also adjusts the seek target passed to Seek() to ensure correct seek time
* is passed to the underlying reader.
*/
class MediaDecoderReaderWrapper {
typedef MediaDecoderReader::MetadataPromise MetadataPromise;
typedef MediaDecoderReader::MediaDataPromise MediaDataPromise;
typedef MediaDecoderReader::SeekPromise SeekPromise;
typedef MediaDecoderReader::WaitForDataPromise WaitForDataPromise;
typedef MediaDecoderReader::BufferedUpdatePromise BufferedUpdatePromise;
typedef MediaDecoderReader::TargetQueues TargetQueues;
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaDecoderReaderWrapper);
/*
* Type 1: void(MediaData*)
* void(RefPtr<MediaData>)
*/
template <typename T>
class ArgType1CheckHelper {
template<typename C, typename... Ts>
static TrueType
test(void(C::*aMethod)(Ts...),
decltype((DeclVal<C>().*aMethod)(DeclVal<MediaData*>()), 0));
template <typename F>
static TrueType
test(F&&, decltype(DeclVal<F>()(DeclVal<MediaData*>()), 0));
static FalseType test(...);
public:
typedef decltype(test(DeclVal<T>(), 0)) Type;
};
template <typename T>
struct ArgType1Check : public ArgType1CheckHelper<T>::Type {};
/*
* Type 2: void(MediaData*, TimeStamp)
* void(RefPtr<MediaData>, TimeStamp)
* void(MediaData*, TimeStamp&)
* void(RefPtr<MediaData>, const TimeStamp&&)
*/
template <typename T>
class ArgType2CheckHelper {
template<typename C, typename... Ts>
static TrueType
test(void(C::*aMethod)(Ts...),
decltype((DeclVal<C>().*aMethod)(DeclVal<MediaData*>(), DeclVal<TimeStamp>()), 0));
template <typename F>
static TrueType
test(F&&, decltype(DeclVal<F>()(DeclVal<MediaData*>(), DeclVal<TimeStamp>()), 0));
static FalseType test(...);
public:
typedef decltype(test(DeclVal<T>(), 0)) Type;
};
template <typename T>
struct ArgType2Check : public ArgType2CheckHelper<T>::Type {};
struct CallbackBase
{
virtual ~CallbackBase() {}
virtual void OnResolved(MediaData*, TimeStamp) = 0;
virtual void OnRejected(MediaDecoderReader::NotDecodedReason) = 0;
};
template<typename ThisType, typename ResolveMethodType, typename RejectMethodType>
struct MethodCallback : public CallbackBase
{
MethodCallback(ThisType* aThis, ResolveMethodType aResolveMethod, RejectMethodType aRejectMethod)
: mThis(aThis), mResolveMethod(aResolveMethod), mRejectMethod(aRejectMethod)
{
}
template<typename F>
typename EnableIf<ArgType1Check<F>::value, void>::Type
CallHelper(MediaData* aSample, TimeStamp)
{
(mThis->*mResolveMethod)(aSample);
}
template<typename F>
typename EnableIf<ArgType2Check<F>::value, void>::Type
CallHelper(MediaData* aSample, TimeStamp aDecodeStartTime)
{
(mThis->*mResolveMethod)(aSample, aDecodeStartTime);
}
void OnResolved(MediaData* aSample, TimeStamp aDecodeStartTime) override
{
CallHelper<ResolveMethodType>(aSample, aDecodeStartTime);
}
void OnRejected(MediaDecoderReader::NotDecodedReason aReason) override
{
(mThis->*mRejectMethod)(aReason);
}
RefPtr<ThisType> mThis;
ResolveMethodType mResolveMethod;
RejectMethodType mRejectMethod;
};
template<typename ResolveFunctionType, typename RejectFunctionType>
struct FunctionCallback : public CallbackBase
{
FunctionCallback(ResolveFunctionType&& aResolveFuntion, RejectFunctionType&& aRejectFunction)
: mResolveFuntion(Move(aResolveFuntion)), mRejectFunction(Move(aRejectFunction))
{
}
template<typename F>
typename EnableIf<ArgType1Check<F>::value, void>::Type
CallHelper(MediaData* aSample, TimeStamp)
{
mResolveFuntion(aSample);
}
template<typename F>
typename EnableIf<ArgType2Check<F>::value, void>::Type
CallHelper(MediaData* aSample, TimeStamp aDecodeStartTime)
{
mResolveFuntion(aSample, aDecodeStartTime);
}
void OnResolved(MediaData* aSample, TimeStamp aDecodeStartTime) override
{
CallHelper<ResolveFunctionType>(aSample, aDecodeStartTime);
}
void OnRejected(MediaDecoderReader::NotDecodedReason aReason) override
{
mRejectFunction(aReason);
}
ResolveFunctionType mResolveFuntion;
RejectFunctionType mRejectFunction;
};
struct WaitForDataCallbackBase
{
virtual ~WaitForDataCallbackBase() {}
virtual void OnResolved(MediaData::Type aType) = 0;
virtual void OnRejected(WaitForDataRejectValue aRejection) = 0;
};
template<typename ThisType, typename ResolveMethodType, typename RejectMethodType>
struct WaitForDataMethodCallback : public WaitForDataCallbackBase
{
WaitForDataMethodCallback(ThisType* aThis, ResolveMethodType aResolveMethod, RejectMethodType aRejectMethod)
: mThis(aThis), mResolveMethod(aResolveMethod), mRejectMethod(aRejectMethod)
{
}
void OnResolved(MediaData::Type aType) override
{
(mThis->*mResolveMethod)(aType);
}
void OnRejected(WaitForDataRejectValue aRejection) override
{
(mThis->*mRejectMethod)(aRejection);
}
RefPtr<ThisType> mThis;
ResolveMethodType mResolveMethod;
RejectMethodType mRejectMethod;
};
template<typename ResolveFunctionType, typename RejectFunctionType>
struct WaitForDataFunctionCallback : public WaitForDataCallbackBase
{
WaitForDataFunctionCallback(ResolveFunctionType&& aResolveFuntion, RejectFunctionType&& aRejectFunction)
: mResolveFuntion(Move(aResolveFuntion)), mRejectFunction(Move(aRejectFunction))
{
}
void OnResolved(MediaData::Type aType) override
{
mResolveFuntion(aType);
}
void OnRejected(WaitForDataRejectValue aRejection) override
{
mRejectFunction(aRejection);
}
ResolveFunctionType mResolveFuntion;
RejectFunctionType mRejectFunction;
};
public:
MediaDecoderReaderWrapper(bool aIsRealTime,
AbstractThread* aOwnerThread,
MediaDecoderReader* aReader);
media::TimeUnit StartTime() const;
RefPtr<MetadataPromise> ReadMetadata();
RefPtr<HaveStartTimePromise> AwaitStartTime();
template<typename ThisType, typename ResolveMethodType, typename RejectMethodType>
CallbackID
SetAudioCallback(ThisType* aThisVal,
ResolveMethodType aResolveMethod,
RejectMethodType aRejectMethod)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mRequestAudioDataCB,
"Please cancel the original callback before setting a new one.");
mRequestAudioDataCB.reset(
new MethodCallback<ThisType, ResolveMethodType, RejectMethodType>(
aThisVal, aResolveMethod, aRejectMethod));
return mAudioCallbackID;
}
template<typename ResolveFunction, typename RejectFunction>
CallbackID
SetAudioCallback(ResolveFunction&& aResolveFunction,
RejectFunction&& aRejectFunction)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mRequestAudioDataCB,
"Please cancel the original callback before setting a new one.");
mRequestAudioDataCB.reset(
new FunctionCallback<ResolveFunction, RejectFunction>(
Move(aResolveFunction), Move(aRejectFunction)));
return mAudioCallbackID;
}
template<typename ThisType, typename ResolveMethodType, typename RejectMethodType>
CallbackID
SetVideoCallback(ThisType* aThisVal,
ResolveMethodType aResolveMethod,
RejectMethodType aRejectMethod)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mRequestVideoDataCB,
"Please cancel the original callback before setting a new one.");
mRequestVideoDataCB.reset(
new MethodCallback<ThisType, ResolveMethodType, RejectMethodType>(
aThisVal, aResolveMethod, aRejectMethod));
return mVideoCallbackID;
}
template<typename ResolveFunction, typename RejectFunction>
CallbackID
SetVideoCallback(ResolveFunction&& aResolveFunction,
RejectFunction&& aRejectFunction)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mRequestVideoDataCB,
"Please cancel the original callback before setting a new one.");
mRequestVideoDataCB.reset(
new FunctionCallback<ResolveFunction, RejectFunction>(
Move(aResolveFunction), Move(aRejectFunction)));
return mVideoCallbackID;
}
template<typename ThisType, typename ResolveMethodType, typename RejectMethodType>
CallbackID
SetWaitAudioCallback(ThisType* aThisVal,
ResolveMethodType aResolveMethod,
RejectMethodType aRejectMethod)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mWaitAudioDataCB,
"Please cancel the original callback before setting a new one.");
mWaitAudioDataCB.reset(
new WaitForDataMethodCallback<ThisType, ResolveMethodType, RejectMethodType>(
aThisVal, aResolveMethod, aRejectMethod));
return mWaitAudioCallbackID;
}
template<typename ResolveFunction, typename RejectFunction>
CallbackID
SetWaitAudioCallback(ResolveFunction&& aResolveFunction,
RejectFunction&& aRejectFunction)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mWaitAudioDataCB,
"Please cancel the original callback before setting a new one.");
mWaitAudioDataCB.reset(
new WaitForDataFunctionCallback<ResolveFunction, RejectFunction>(
Move(aResolveFunction), Move(aRejectFunction)));
return mWaitAudioCallbackID;
}
template<typename ThisType, typename ResolveMethodType, typename RejectMethodType>
CallbackID
SetWaitVideoCallback(ThisType* aThisVal,
ResolveMethodType aResolveMethod,
RejectMethodType aRejectMethod)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mWaitVideoDataCB,
"Please cancel the original callback before setting a new one.");
mWaitVideoDataCB.reset(
new WaitForDataMethodCallback<ThisType, ResolveMethodType, RejectMethodType>(
aThisVal, aResolveMethod, aRejectMethod));
return mWaitVideoCallbackID;
}
template<typename ResolveFunction, typename RejectFunction>
CallbackID
SetWaitVideoCallback(ResolveFunction&& aResolveFunction,
RejectFunction&& aRejectFunction)
{
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
MOZ_ASSERT(!mWaitVideoDataCB,
"Please cancel the original callback before setting a new one.");
mWaitVideoDataCB.reset(
new WaitForDataFunctionCallback<ResolveFunction, RejectFunction>(
Move(aResolveFunction), Move(aRejectFunction)));
return mWaitVideoCallbackID;
}
void CancelAudioCallback(CallbackID aID);
void CancelVideoCallback(CallbackID aID);
void CancelWaitAudioCallback(CallbackID aID);
void CancelWaitVideoCallback(CallbackID aID);
// NOTE: please set callbacks before requesting audio/video data!
void RequestAudioData();
void RequestVideoData(bool aSkipToNextKeyframe, media::TimeUnit aTimeThreshold);
// NOTE: please set callbacks before invoking WaitForData()!
void WaitForData(MediaData::Type aType);
bool IsRequestingAudioData() const;
bool IsRequestingVideoData() const;
bool IsWaitingAudioData() const;
bool IsWaitingVideoData() const;
RefPtr<SeekPromise> Seek(SeekTarget aTarget, media::TimeUnit aEndTime);
RefPtr<BufferedUpdatePromise> UpdateBufferedWithPromise();
RefPtr<ShutdownPromise> Shutdown();
void ReleaseMediaResources();
void SetIdle();
void ResetDecode(TargetQueues aQueues);
nsresult Init() { return mReader->Init(); }
bool IsWaitForDataSupported() const { return mReader->IsWaitForDataSupported(); }
bool IsAsync() const { return mReader->IsAsync(); }
bool UseBufferingHeuristics() const { return mReader->UseBufferingHeuristics(); }
bool ForceZeroStartTime() const { return mReader->ForceZeroStartTime(); }
bool VideoIsHardwareAccelerated() const {
return mReader->VideoIsHardwareAccelerated();
}
TimedMetadataEventSource& TimedMetadataEvent() {
return mReader->TimedMetadataEvent();
}
MediaEventSource<void>& OnMediaNotSeekable() {
return mReader->OnMediaNotSeekable();
}
size_t SizeOfVideoQueueInBytes() const {
return mReader->SizeOfVideoQueueInBytes();
}
size_t SizeOfAudioQueueInBytes() const {
return mReader->SizeOfAudioQueueInBytes();
}
size_t SizeOfAudioQueueInFrames() const {
return mReader->SizeOfAudioQueueInFrames();
}
size_t SizeOfVideoQueueInFrames() const {
return mReader->SizeOfVideoQueueInFrames();
}
void ReadUpdatedMetadata(MediaInfo* aInfo) {
mReader->ReadUpdatedMetadata(aInfo);
}
AbstractCanonical<media::TimeIntervals>* CanonicalBuffered() {
return mReader->CanonicalBuffered();
}
#ifdef MOZ_EME
void SetCDMProxy(CDMProxy* aProxy) { mReader->SetCDMProxy(aProxy); }
#endif
private:
~MediaDecoderReaderWrapper();
void OnMetadataRead(MetadataHolder* aMetadata);
void OnMetadataNotRead() {}
void OnSampleDecoded(CallbackBase* aCallback, MediaData* aSample,
TimeStamp aVideoDecodeStartTime);
void OnNotDecoded(CallbackBase* aCallback,
MediaDecoderReader::NotDecodedReason aReason);
UniquePtr<WaitForDataCallbackBase>& WaitCallbackRef(MediaData::Type aType);
MozPromiseRequestHolder<WaitForDataPromise>& WaitRequestRef(MediaData::Type aType);
const bool mForceZeroStartTime;
const RefPtr<AbstractThread> mOwnerThread;
const RefPtr<MediaDecoderReader> mReader;
bool mShutdown = false;
RefPtr<StartTimeRendezvous> mStartTimeRendezvous;
UniquePtr<CallbackBase> mRequestAudioDataCB;
UniquePtr<CallbackBase> mRequestVideoDataCB;
UniquePtr<WaitForDataCallbackBase> mWaitAudioDataCB;
UniquePtr<WaitForDataCallbackBase> mWaitVideoDataCB;
MozPromiseRequestHolder<MediaDataPromise> mAudioDataRequest;
MozPromiseRequestHolder<MediaDataPromise> mVideoDataRequest;
MozPromiseRequestHolder<WaitForDataPromise> mAudioWaitRequest;
MozPromiseRequestHolder<WaitForDataPromise> mVideoWaitRequest;
/*
* These callback ids are used to prevent mis-canceling callback.
*/
CallbackID mAudioCallbackID;
CallbackID mVideoCallbackID;
CallbackID mWaitAudioCallbackID;
CallbackID mWaitVideoCallbackID;
};
} // namespace mozilla
#endif // MediaDecoderReaderWrapper_h_