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

PiperOrigin-RevId: 585104621
(cherry picked from commit ad40db448943fef579879c9c2a988f514b254109)
This commit is contained in:
andrewlewis 2023-11-24 08:54:01 -08:00 committed by microkatz
parent 36f634d8d1
commit 97c9e234d2
3 changed files with 8 additions and 4 deletions

View File

@ -8,6 +8,8 @@
`LiveConfiguration.min/maxOffset` range keep adjusting the offset back
to `min/maxOffset`.
* Transformer:
* Work around an issue where the encoder would throw at configuration time
due to setting a high operating rate.
* Track Selection:
* Extractors:
* Audio:

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