Skip to content

Commit

Permalink
Bug 1880143 - Simplify *DecoderConfigInternal's mDescription type r=m…
Browse files Browse the repository at this point in the history
…edia-playback-reviewers,padenot

`Maybe<RefPtr<MediaByteBuffer>> mDescription` can be reduced to
`RefPtr<MediaByteBuffer> mDescription` since the meaning of `Nothing` is
nothing different from a NULL, which can be represented by the inside
pointer.

Differential Revision: https://phabricator.services.mozilla.com/D203543
  • Loading branch information
ChunMinChang committed Apr 10, 2024
1 parent 6a94021 commit 304cfdb
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 47 deletions.
27 changes: 13 additions & 14 deletions dom/media/webcodecs/AudioDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)

AudioDecoderConfigInternal::AudioDecoderConfigInternal(
const nsAString& aCodec, uint32_t aSampleRate, uint32_t aNumberOfChannels,
Maybe<RefPtr<MediaByteBuffer>>&& aDescription)
already_AddRefed<MediaByteBuffer> aDescription)
: mCodec(aCodec),
mSampleRate(aSampleRate),
mNumberOfChannels(aNumberOfChannels),
mDescription(std::move(aDescription)) {}
mDescription(aDescription) {}

/*static*/
UniquePtr<AudioDecoderConfigInternal> AudioDecoderConfigInternal::Create(
Expand All @@ -83,7 +83,7 @@ UniquePtr<AudioDecoderConfigInternal> AudioDecoderConfigInternal::Create(
return nullptr;
}

Maybe<RefPtr<MediaByteBuffer>> description;
RefPtr<MediaByteBuffer> description;
if (aConfig.mDescription.WasPassed()) {
auto rv = GetExtraDataFromArrayBuffer(aConfig.mDescription.Value());
if (rv.isErr()) { // Invalid description data.
Expand All @@ -95,12 +95,12 @@ UniquePtr<AudioDecoderConfigInternal> AudioDecoderConfigInternal::Create(
error.get());
return nullptr;
}
description.emplace(rv.unwrap());
description = rv.unwrap();
}

return UniquePtr<AudioDecoderConfigInternal>(new AudioDecoderConfigInternal(
aConfig.mCodec, aConfig.mSampleRate, aConfig.mNumberOfChannels,
std::move(description)));
description.forget()));
}

nsCString AudioDecoderConfigInternal::ToString() const {
Expand All @@ -111,7 +111,7 @@ nsCString AudioDecoderConfigInternal::ToString() const {
NS_ConvertUTF16toUTF8(mCodec).get(), mSampleRate,
mNumberOfChannels);
if (mDescription) {
rv.AppendPrintf("(%zu bytes of extradata)", (*mDescription)->Length());
rv.AppendPrintf("(%zu bytes of extradata)", mDescription->Length());
} else {
rv.AppendLiteral("(no extradata)");
}
Expand Down Expand Up @@ -247,13 +247,12 @@ Result<UniquePtr<TrackInfo>, nsresult> AudioDecoderTraits::CreateTrackInfo(
return Err(NS_ERROR_INVALID_ARG);
}

if (aConfig.mDescription.isSome()) {
RefPtr<MediaByteBuffer> buf;
buf = aConfig.mDescription.value();
if (buf) {
LOG("The given config has %zu bytes of description data", buf->Length());
ai->mCodecSpecificConfig =
AudioCodecSpecificVariant{AudioCodecSpecificBinaryBlob{buf}};
if (aConfig.mDescription) {
if (!aConfig.mDescription->IsEmpty()) {
LOG("The given config has %zu bytes of description data",
aConfig.mDescription->Length());
ai->mCodecSpecificConfig = AudioCodecSpecificVariant{
AudioCodecSpecificBinaryBlob{aConfig.mDescription}};
}
}

Expand All @@ -263,7 +262,7 @@ Result<UniquePtr<TrackInfo>, nsresult> AudioDecoderTraits::CreateTrackInfo(
LOG("Created AudioInfo %s (%" PRIu32 "ch %" PRIu32
"Hz - with extra-data: %s)",
NS_ConvertUTF16toUTF8(aConfig.mCodec).get(), ai->mChannels, ai->mChannels,
aConfig.mDescription.isSome() ? "yes" : "no");
aConfig.mDescription && !aConfig.mDescription->IsEmpty() ? "yes" : "no");

return track;
}
Expand Down
6 changes: 3 additions & 3 deletions dom/media/webcodecs/AudioEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,9 @@ AudioDecoderConfigInternal AudioEncoder::EncoderConfigToDecoderConfig(
sampleRate = aRawData->mConfig->mSampleRate;
channelCount = aRawData->mConfig->mNumberOfChannels;
}
return AudioDecoderConfigInternal(
aOutputConfig.mCodec, sampleRate, channelCount,
aRawData->mExtraData ? Some(aRawData->mExtraData) : Nothing());
return AudioDecoderConfigInternal(aOutputConfig.mCodec, sampleRate,
channelCount,
do_AddRef(aRawData->mExtraData));
}

#undef LOG
Expand Down
24 changes: 12 additions & 12 deletions dom/media/webcodecs/DecoderTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class VideoDecoderConfigInternal {
Maybe<uint32_t>&& aCodedHeight,
Maybe<uint32_t>&& aCodedWidth,
Maybe<VideoColorSpaceInternal>&& aColorSpace,
Maybe<RefPtr<MediaByteBuffer>>&& aDescription,
already_AddRefed<MediaByteBuffer> aDescription,
Maybe<uint32_t>&& aDisplayAspectHeight,
Maybe<uint32_t>&& aDisplayAspectWidth,
const HardwareAcceleration& aHardwareAcceleration,
Expand All @@ -60,12 +60,12 @@ class VideoDecoderConfigInternal {
nsCString ToString() const;

bool Equals(const VideoDecoderConfigInternal& aOther) const {
if (mDescription.isSome() != aOther.mDescription.isSome()) {
if (mDescription != aOther.mDescription) {
return false;
}
if (mDescription.isSome() && aOther.mDescription.isSome()) {
auto lhs = mDescription.value();
auto rhs = aOther.mDescription.value();
if (mDescription && aOther.mDescription) {
auto lhs = mDescription;
auto rhs = aOther.mDescription;
if (lhs->Length() != rhs->Length()) {
return false;
}
Expand All @@ -86,7 +86,7 @@ class VideoDecoderConfigInternal {
Maybe<uint32_t> mCodedHeight;
Maybe<uint32_t> mCodedWidth;
Maybe<VideoColorSpaceInternal> mColorSpace;
Maybe<RefPtr<MediaByteBuffer>> mDescription;
RefPtr<MediaByteBuffer> mDescription;
Maybe<uint32_t> mDisplayAspectHeight;
Maybe<uint32_t> mDisplayAspectWidth;
HardwareAcceleration mHardwareAcceleration;
Expand Down Expand Up @@ -118,18 +118,18 @@ class AudioDecoderConfigInternal {
public:
AudioDecoderConfigInternal(const nsAString& aCodec, uint32_t aSampleRate,
uint32_t aNumberOfChannels,
Maybe<RefPtr<MediaByteBuffer>>&& aDescription);
already_AddRefed<MediaByteBuffer> aDescription);
static UniquePtr<AudioDecoderConfigInternal> Create(
const AudioDecoderConfig& aConfig);
~AudioDecoderConfigInternal() = default;

bool Equals(const AudioDecoderConfigInternal& aOther) const {
if (mDescription.isSome() != aOther.mDescription.isSome()) {
if (mDescription != aOther.mDescription) {
return false;
}
if (mDescription.isSome() && aOther.mDescription.isSome()) {
auto lhs = mDescription.value();
auto rhs = aOther.mDescription.value();
if (mDescription && aOther.mDescription) {
auto lhs = mDescription;
auto rhs = aOther.mDescription;
if (lhs->Length() != rhs->Length()) {
return false;
}
Expand All @@ -146,7 +146,7 @@ class AudioDecoderConfigInternal {
nsString mCodec;
uint32_t mSampleRate;
uint32_t mNumberOfChannels;
Maybe<RefPtr<MediaByteBuffer>> mDescription;
RefPtr<MediaByteBuffer> mDescription;
// Compilation fix, should be abstracted by DecoderAgent since those are not
// supported
HardwareAcceleration mHardwareAcceleration =
Expand Down
6 changes: 3 additions & 3 deletions dom/media/webcodecs/EncoderTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,12 @@ template <typename T, typename U>
void EncoderTemplate<EncoderType>::CopyExtradataToDescriptionIfNeeded(
nsIGlobalObject* aGlobal, const T& aConfigInternal, U& aConfig) {
if (aConfigInternal.mDescription &&
!aConfigInternal.mDescription.value()->IsEmpty()) {
!aConfigInternal.mDescription->IsEmpty()) {
auto& abov = aConfig.mDescription.Construct();
AutoEntryScript aes(aGlobal, "EncoderConfigToaConfigConfig");
size_t lengthBytes = aConfigInternal.mDescription.value()->Length();
size_t lengthBytes = aConfigInternal.mDescription->Length();
UniquePtr<uint8_t[], JS::FreePolicy> extradata(new uint8_t[lengthBytes]);
PodCopy(extradata.get(), aConfigInternal.mDescription.value()->Elements(),
PodCopy(extradata.get(), aConfigInternal.mDescription->Elements(),
lengthBytes);
JS::Rooted<JSObject*> description(
aes.cx(), JS::NewArrayBufferWithContents(aes.cx(), lengthBytes,
Expand Down
25 changes: 12 additions & 13 deletions dom/media/webcodecs/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ VideoColorSpaceInit VideoColorSpaceInternal::ToColorSpaceInit() const {
VideoDecoderConfigInternal::VideoDecoderConfigInternal(
const nsAString& aCodec, Maybe<uint32_t>&& aCodedHeight,
Maybe<uint32_t>&& aCodedWidth, Maybe<VideoColorSpaceInternal>&& aColorSpace,
Maybe<RefPtr<MediaByteBuffer>>&& aDescription,
already_AddRefed<MediaByteBuffer> aDescription,
Maybe<uint32_t>&& aDisplayAspectHeight,
Maybe<uint32_t>&& aDisplayAspectWidth,
const HardwareAcceleration& aHardwareAcceleration,
Expand All @@ -105,7 +105,7 @@ VideoDecoderConfigInternal::VideoDecoderConfigInternal(
mCodedHeight(std::move(aCodedHeight)),
mCodedWidth(std::move(aCodedWidth)),
mColorSpace(std::move(aColorSpace)),
mDescription(std::move(aDescription)),
mDescription(aDescription),
mDisplayAspectHeight(std::move(aDisplayAspectHeight)),
mDisplayAspectWidth(std::move(aDisplayAspectWidth)),
mHardwareAcceleration(aHardwareAcceleration),
Expand All @@ -120,7 +120,7 @@ UniquePtr<VideoDecoderConfigInternal> VideoDecoderConfigInternal::Create(
return nullptr;
}

Maybe<RefPtr<MediaByteBuffer>> description;
RefPtr<MediaByteBuffer> description;
if (aConfig.mDescription.WasPassed()) {
auto rv = GetExtraDataFromArrayBuffer(aConfig.mDescription.Value());
if (rv.isErr()) { // Invalid description data.
Expand All @@ -130,7 +130,7 @@ UniquePtr<VideoDecoderConfigInternal> VideoDecoderConfigInternal::Create(
static_cast<uint32_t>(rv.unwrapErr()));
return nullptr;
}
description.emplace(rv.unwrap());
description = rv.unwrap();
}

Maybe<VideoColorSpaceInternal> colorSpace;
Expand All @@ -141,7 +141,7 @@ UniquePtr<VideoDecoderConfigInternal> VideoDecoderConfigInternal::Create(
return UniquePtr<VideoDecoderConfigInternal>(new VideoDecoderConfigInternal(
aConfig.mCodec, OptionalToMaybe(aConfig.mCodedHeight),
OptionalToMaybe(aConfig.mCodedWidth), std::move(colorSpace),
std::move(description), OptionalToMaybe(aConfig.mDisplayAspectHeight),
description.forget(), OptionalToMaybe(aConfig.mDisplayAspectHeight),
OptionalToMaybe(aConfig.mDisplayAspectWidth),
aConfig.mHardwareAcceleration,
OptionalToMaybe(aConfig.mOptimizeForLatency)));
Expand All @@ -161,8 +161,8 @@ nsCString VideoDecoderConfigInternal::ToString() const {
if (mColorSpace.isSome()) {
rv.AppendPrintf("colorspace %s", "todo");
}
if (mDescription.isSome() && mDescription.value()) {
rv.AppendPrintf("extradata: %zu bytes", mDescription.value()->Length());
if (mDescription) {
rv.AppendPrintf("extradata: %zu bytes", mDescription->Length());
}
rv.AppendPrintf("hw accel: %s", GetEnumString(mHardwareAcceleration).get());
if (mOptimizeForLatency.isSome()) {
Expand Down Expand Up @@ -667,15 +667,14 @@ Result<UniquePtr<TrackInfo>, nsresult> VideoDecoderTraits::CreateTrackInfo(
}
}

if (aConfig.mDescription.isSome()) {
RefPtr<MediaByteBuffer> buf;
buf = aConfig.mDescription.value();
if (buf) {
LOG("The given config has %zu bytes of description data", buf->Length());
if (aConfig.mDescription) {
if (!aConfig.mDescription->IsEmpty()) {
LOG("The given config has %zu bytes of description data",
aConfig.mDescription->Length());
if (vi->mExtraData) {
LOGW("The default extra data is overwritten");
}
vi->mExtraData = buf;
vi->mExtraData = aConfig.mDescription;
}

// TODO: Make this utility and replace the similar one in MP4Demuxer.cpp.
Expand Down
4 changes: 2 additions & 2 deletions dom/media/webcodecs/VideoEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ VideoDecoderConfigInternal VideoEncoder::EncoderConfigToDecoderConfig(
Some(mOutputConfig.mWidth), /* aCodedWidth */
Some(init), /* aColorSpace */
aRawData->mExtraData && !aRawData->mExtraData->IsEmpty()
? Some(aRawData->mExtraData)
: Nothing(), /* aDescription*/
? aRawData->mExtraData.forget()
: nullptr, /* aDescription*/
Maybe<uint32_t>(mOutputConfig.mDisplayHeight), /* aDisplayAspectHeight*/
Maybe<uint32_t>(mOutputConfig.mDisplayWidth), /* aDisplayAspectWidth */
mOutputConfig.mHardwareAcceleration, /* aHardwareAcceleration */
Expand Down

0 comments on commit 304cfdb

Please sign in to comment.