Bug 1936352 - VideoEncoder: Return unsupported for codec strings with spaces r=media-playback-reviewers,alwu

This patch fixes a crash that occurs when a codec string with leading
spaces is passed to `VideoEncoder::IsConfigSupported`.

The crash happens because `VideoEncoderConfigInternal::ToEncoderConfig`
assumes that its codec string member begins with a supported codec name
(which should be non-space character). When the codec string has leading
spaces, this assumption is invalidated, leading to a crash.

To resolve this issue, the patch removes the space-trimming in
`CanEncode` and calling `IsSupportedVideoCodec` with the given codec
string directly before `ToEncoderConfig`. By doing so, codec strings
with leading spaces are correctly identified as "unsupported", and
`ToEncoderConfig` is not invoked with invalid input, preventing the
crash.

Additionally, this patch adds a WPT to ensure that codec string
containing spaces are reported as "unsupported" and the no crash occurs
when such strings are processed.

Differential Revision: https://phabricator.services.mozilla.com/D231719
This commit is contained in:
Chun-Min Chang
2024-12-23 17:50:42 +00:00
parent 7ce7a6d4db
commit 2e72707782
2 changed files with 10 additions and 4 deletions

View File

@@ -337,13 +337,11 @@ VideoEncoderConfigInternal::Diff(
// https://w3c.github.io/webcodecs/#check-configuration-support
static bool CanEncode(const RefPtr<VideoEncoderConfigInternal>& aConfig) {
auto parsedCodecString =
ParseCodecString(aConfig->mCodec).valueOr(EmptyString());
// TODO: Enable WebCodecs on Android (Bug 1840508)
if (IsOnAndroid()) {
return false;
}
if (!IsSupportedVideoCodec(parsedCodecString)) {
if (!IsSupportedVideoCodec(aConfig->mCodec)) {
return false;
}
if (aConfig->mScalabilityMode.isSome()) {
@@ -352,7 +350,7 @@ static bool CanEncode(const RefPtr<VideoEncoderConfigInternal>& aConfig) {
!aConfig->mScalabilityMode->EqualsLiteral("L1T3")) {
LOGE("Scalability mode %s not supported for codec: %s",
NS_ConvertUTF16toUTF8(aConfig->mScalabilityMode.value()).get(),
NS_ConvertUTF16toUTF8(parsedCodecString).get());
NS_ConvertUTF16toUTF8(aConfig->mCodec).get());
return false;
}
}

View File

@@ -183,6 +183,14 @@ const validButUnsupportedConfigs = [
height: 480,
},
},
{
comment: 'codec with spaces',
config: {
codec: ' vp09.00.10.08 ',
width: 640,
height: 480,
}
}
];
validButUnsupportedConfigs.forEach(entry => {