Allow configuring encoder.

PiperOrigin-RevId: 400151886
This commit is contained in:
claincly 2021-10-01 10:52:53 +01:00 committed by Oliver Woodman
parent 7a3dedce07
commit d4343ed858
2 changed files with 20 additions and 6 deletions

View File

@ -18,6 +18,7 @@ package com.google.android.exoplayer2.transformer;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Assertions.checkState; import static com.google.android.exoplayer2.util.Assertions.checkState;
import static java.lang.Math.ceil;
import android.media.MediaCodec; import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo; import android.media.MediaCodec.BufferInfo;
@ -38,6 +39,7 @@ import com.google.android.exoplayer2.util.MimeTypes;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@ -199,21 +201,30 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
* MediaCodecAdapter} video encoder. * MediaCodecAdapter} video encoder.
* *
* @param format The {@link Format} (of the output data) used to determine the underlying {@link * @param format The {@link Format} (of the output data) used to determine the underlying {@link
* MediaCodec} and its configuration values. * MediaCodec} and its configuration values. {@link Format#width}, {@link Format#height},
* {@link Format#frameRate} and {@link Format#averageBitrate} must be set to those of the
* desired output video format.
* @param additionalEncoderConfig A map of {@link MediaFormat}'s integer settings, where the keys
* are from {@code MediaFormat.KEY_*} constants. Its values will override those in {@code
* format}.
* @return A configured and started encoder wrapper. * @return A configured and started encoder wrapper.
* @throws IOException If the underlying codec cannot be created. * @throws IOException If the underlying codec cannot be created.
*/ */
public static MediaCodecAdapterWrapper createForVideoEncoding(Format format) throws IOException { public static MediaCodecAdapterWrapper createForVideoEncoding(
Format format, Map<String, Integer> additionalEncoderConfig) throws IOException {
@Nullable MediaCodecAdapter adapter = null; @Nullable MediaCodecAdapter adapter = null;
try { try {
MediaFormat mediaFormat = MediaFormat mediaFormat =
MediaFormat.createVideoFormat( MediaFormat.createVideoFormat(
checkNotNull(format.sampleMimeType), format.width, format.height); checkNotNull(format.sampleMimeType), format.width, format.height);
// TODO(claincly): enable configuration.
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, CodecCapabilities.COLOR_FormatSurface); mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, CodecCapabilities.COLOR_FormatSurface);
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 30); mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, (int) ceil(format.frameRate));
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1); mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 5_000_000); mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, format.averageBitrate);
for (Map.Entry<String, Integer> encoderSetting : additionalEncoderConfig.entrySet()) {
mediaFormat.setInteger(encoderSetting.getKey(), encoderSetting.getValue());
}
adapter = adapter =
new Factory() new Factory()

View File

@ -39,6 +39,7 @@ import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer; import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.source.SampleStream; import com.google.android.exoplayer2.source.SampleStream;
import com.google.android.exoplayer2.util.GlUtil; import com.google.android.exoplayer2.util.GlUtil;
import com.google.common.collect.ImmutableMap;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -158,7 +159,9 @@ import java.nio.ByteBuffer;
} }
try { try {
encoder = MediaCodecAdapterWrapper.createForVideoEncoding(encoderConfigurationOutputFormat); encoder =
MediaCodecAdapterWrapper.createForVideoEncoding(
encoderConfigurationOutputFormat, ImmutableMap.of());
} catch (IOException e) { } catch (IOException e) {
throw createRendererException( throw createRendererException(
// TODO(claincly): should be "ENCODER_INIT_FAILED" // TODO(claincly): should be "ENCODER_INIT_FAILED"