Bug 1898344 - part2 : split the creation of MediaCapabilitiesInfo into another function and reject the promise properly when error occurs. r=media-playback-reviewers,padenot

Differential Revision: https://phabricator.services.mozilla.com/D212321
This commit is contained in:
alwu 2024-06-10 23:20:43 +00:00
Родитель 4784c207eb
Коммит aafdf39a76
2 изменённых файлов: 38 добавлений и 27 удалений

Просмотреть файл

@ -129,10 +129,10 @@ already_AddRefed<Promise> MediaCapabilities::DecodingInfo(
// rejected with a TypeError.
if (!aConfiguration.mVideo.WasPassed() &&
!aConfiguration.mAudio.WasPassed()) {
aRv.ThrowTypeError<MSG_MISSING_REQUIRED_DICTIONARY_MEMBER>(
promise->MaybeRejectWithTypeError(
"'audio' or 'video' member of argument of "
"MediaCapabilities.decodingInfo");
return nullptr;
return promise.forget();
}
// If configuration.keySystemConfiguration exists, run the following substeps:
@ -156,6 +156,16 @@ already_AddRefed<Promise> MediaCapabilities::DecodingInfo(
}
}
// In parallel, run the Create a MediaCapabilitiesDecodingInfo algorithm with
// configuration and resolve p with its result.
CreateMediaCapabilitiesDecodingInfo(aConfiguration, aRv, promise);
return promise.forget();
}
// https://w3c.github.io/media-capabilities/#create-media-capabilities-decoding-info
void MediaCapabilities::CreateMediaCapabilitiesDecodingInfo(
const MediaDecodingConfiguration& aConfiguration, ErrorResult& aRv,
Promise* aPromise) {
LOG("Processing %s", MediaDecodingConfigurationToStr(aConfiguration).get());
bool supported = true;
@ -167,8 +177,8 @@ already_AddRefed<Promise> MediaCapabilities::DecodingInfo(
if (aConfiguration.mVideo.WasPassed()) {
videoContainer = CheckVideoConfiguration(aConfiguration.mVideo.Value());
if (!videoContainer) {
aRv.ThrowTypeError<MSG_INVALID_MEDIA_VIDEO_CONFIGURATION>();
return nullptr;
aPromise->MaybeRejectWithTypeError("Invalid VideoConfiguration");
return;
}
// We have a video configuration and it is valid. Check if it is supported.
@ -181,8 +191,8 @@ already_AddRefed<Promise> MediaCapabilities::DecodingInfo(
if (aConfiguration.mAudio.WasPassed()) {
audioContainer = CheckAudioConfiguration(aConfiguration.mAudio.Value());
if (!audioContainer) {
aRv.ThrowTypeError<MSG_INVALID_MEDIA_AUDIO_CONFIGURATION>();
return nullptr;
aPromise->MaybeRejectWithTypeError("Invalid AudioConfiguration");
return;
}
// We have an audio configuration and it is valid. Check if it is supported.
supported &=
@ -197,8 +207,8 @@ already_AddRefed<Promise> MediaCapabilities::DecodingInfo(
false /* supported */, false /* smooth */, false /* power efficient */);
LOG("%s -> %s", MediaDecodingConfigurationToStr(aConfiguration).get(),
MediaCapabilitiesInfoToStr(info.get()).get());
promise->MaybeResolve(std::move(info));
return promise.forget();
aPromise->MaybeResolve(std::move(info));
return;
}
nsTArray<UniquePtr<TrackInfo>> tracks;
@ -210,17 +220,17 @@ already_AddRefed<Promise> MediaCapabilities::DecodingInfo(
// describing a single media codec. Otherwise, it MUST contain no
// parameters.
if (videoTracks.Length() != 1) {
promise->MaybeRejectWithTypeError<MSG_NO_CODECS_PARAMETER>(
videoContainer->OriginalString());
return promise.forget();
aPromise->MaybeRejectWithTypeError(nsPrintfCString(
"The provided type '%s' does not have a 'codecs' parameter.",
videoContainer->OriginalString().get()));
return;
}
MOZ_DIAGNOSTIC_ASSERT(videoTracks.ElementAt(0),
"must contain a valid trackinfo");
// If the type refers to an audio codec, reject now.
if (videoTracks[0]->GetType() != TrackInfo::kVideoTrack) {
promise
->MaybeRejectWithTypeError<MSG_INVALID_MEDIA_VIDEO_CONFIGURATION>();
return promise.forget();
aPromise->MaybeRejectWithTypeError("Invalid VideoConfiguration");
return;
}
tracks.AppendElements(std::move(videoTracks));
}
@ -232,17 +242,17 @@ already_AddRefed<Promise> MediaCapabilities::DecodingInfo(
// describing a single media codec. Otherwise, it MUST contain no
// parameters.
if (audioTracks.Length() != 1) {
promise->MaybeRejectWithTypeError<MSG_NO_CODECS_PARAMETER>(
audioContainer->OriginalString());
return promise.forget();
aPromise->MaybeRejectWithTypeError(nsPrintfCString(
"The provided type '%s' does not have a 'codecs' parameter.",
audioContainer->OriginalString().get()));
return;
}
MOZ_DIAGNOSTIC_ASSERT(audioTracks.ElementAt(0),
"must contain a valid trackinfo");
// If the type refers to a video codec, reject now.
if (audioTracks[0]->GetType() != TrackInfo::kAudioTrack) {
promise
->MaybeRejectWithTypeError<MSG_INVALID_MEDIA_AUDIO_CONFIGURATION>();
return promise.forget();
aPromise->MaybeRejectWithTypeError("Invalid AudioConfiguration");
return;
}
tracks.AppendElements(std::move(audioTracks));
}
@ -462,9 +472,8 @@ already_AddRefed<Promise> MediaCapabilities::DecodingInfo(
holder->DisconnectIfExists();
});
if (NS_WARN_IF(!workerRef)) {
// The worker is shutting down.
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
aPromise->MaybeRejectWithInvalidStateError("The worker is shutting down");
return;
}
}
@ -475,8 +484,8 @@ already_AddRefed<Promise> MediaCapabilities::DecodingInfo(
CapabilitiesPromise::All(targetThread, promises)
->Then(targetThread, __func__,
[promise, tracks = std::move(tracks), workerRef, holder,
aConfiguration, self,
[promise = RefPtr<Promise>{aPromise}, tracks = std::move(tracks),
workerRef, holder, aConfiguration, self,
this](CapabilitiesPromise::AllPromiseType::ResolveOrRejectValue&&
aValue) {
holder->Complete();
@ -504,8 +513,6 @@ already_AddRefed<Promise> MediaCapabilities::DecodingInfo(
promise->MaybeResolve(std::move(info));
})
->Track(*holder);
return promise.forget();
}
already_AddRefed<Promise> MediaCapabilities::EncodingInfo(

Просмотреть файл

@ -72,6 +72,10 @@ class MediaCapabilities final : public nsISupports, public nsWrapperCache {
bool CheckTypeForFile(const nsAString& aType);
bool CheckTypeForEncoder(const nsAString& aType);
already_AddRefed<layers::KnowsCompositor> GetCompositor();
void CreateMediaCapabilitiesDecodingInfo(
const MediaDecodingConfiguration& aConfiguration, ErrorResult& aRv,
Promise* aPromise);
nsCOMPtr<nsIGlobalObject> mParent;
};