Avoid value close to overflow for KEY_OPERATING_RATE

Using `Integer.MAX_VALUE` risks causing arithmetic overflow in the codec
implementation.

Issue: androidx/media#810

#minor-release

PiperOrigin-RevId: 585104621
This commit is contained in:
andrewlewis 2023-11-24 08:54:01 -08:00 committed by Copybara-Service
parent bc36553e81
commit ad40db4489
3 changed files with 8 additions and 4 deletions

View file

@ -30,6 +30,8 @@
* Transformer:
* Add support for flattening H.265/HEVC SEF slow motion videos.
* Increase transmuxing speed, especially for 'remove video' edits.
* Work around an issue where the encoder would throw at configuration time
due to setting a high operating rate.
* Track Selection:
* Add `DefaultTrackSelector.selectImageTrack` to enable image track
selection.

View file

@ -51,8 +51,6 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
/** Best effort, or as-fast-as-possible priority setting for {@link MediaFormat#KEY_PRIORITY}. */
private static final int PRIORITY_BEST_EFFORT = 1;
private static final String TAG = "DefaultEncoderFactory";
/** A builder for {@link DefaultEncoderFactory} instances. */
public static final class Builder {
private final Context context;
@ -519,6 +517,11 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
if (Util.SDK_INT == 26) {
mediaFormat.setInteger(MediaFormat.KEY_OPERATING_RATE, DEFAULT_FRAME_RATE);
} else if (Util.SDK_INT <= 34) {
// On some devices setting Integer.MAX_VALUE will cause the encoder to throw at configuration
// time. Setting the operating to 1000 avoids being close to an integer overflow limit while
// being higher than a maximum feasible operating rate. See [internal b/311206113].
mediaFormat.setInteger(MediaFormat.KEY_OPERATING_RATE, 1000);
} else {
mediaFormat.setInteger(MediaFormat.KEY_OPERATING_RATE, Integer.MAX_VALUE);
}

View file

@ -231,8 +231,7 @@ public class DefaultEncoderFactoryTest {
assertThat(configurationMediaFormat.containsKey(MediaFormat.KEY_PRIORITY)).isTrue();
assertThat(configurationMediaFormat.getInteger(MediaFormat.KEY_PRIORITY)).isEqualTo(1);
assertThat(configurationMediaFormat.containsKey(MediaFormat.KEY_OPERATING_RATE)).isTrue();
assertThat(configurationMediaFormat.getInteger(MediaFormat.KEY_OPERATING_RATE))
.isEqualTo(Integer.MAX_VALUE);
assertThat(configurationMediaFormat.getInteger(MediaFormat.KEY_OPERATING_RATE)).isEqualTo(1000);
}
@Test