Simplify codec configuration.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=114427767
This commit is contained in:
olly 2016-02-11 05:39:57 -08:00 committed by Oliver Woodman
parent 335bb0aff2
commit ebf87a3619
3 changed files with 25 additions and 34 deletions

View File

@ -25,6 +25,7 @@ import com.google.android.exoplayer.util.Util;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.MediaCodec; import android.media.MediaCodec;
import android.media.MediaCrypto;
import android.media.PlaybackParams; import android.media.PlaybackParams;
import android.media.audiofx.Virtualizer; import android.media.audiofx.Virtualizer;
import android.os.Handler; import android.os.Handler;
@ -237,17 +238,15 @@ public class MediaCodecAudioTrackRenderer extends MediaCodecTrackRenderer implem
} }
@Override @Override
protected void configureCodec(MediaCodec codec, boolean codecIsAdaptive, protected void configureCodec(MediaCodec codec, MediaFormat format, MediaCrypto crypto) {
android.media.MediaFormat format, android.media.MediaCrypto crypto) {
String mimeType = format.getString(android.media.MediaFormat.KEY_MIME);
if (passthroughEnabled) { if (passthroughEnabled) {
// Override the MIME type used to configure the codec if we are using a passthrough decoder. // Override the MIME type used to configure the codec if we are using a passthrough decoder.
format.setString(android.media.MediaFormat.KEY_MIME, MimeTypes.AUDIO_RAW); passthroughMediaFormat = format.getFrameworkMediaFormatV16();
codec.configure(format, null, crypto, 0); passthroughMediaFormat.setString(android.media.MediaFormat.KEY_MIME, MimeTypes.AUDIO_RAW);
format.setString(android.media.MediaFormat.KEY_MIME, mimeType); codec.configure(passthroughMediaFormat, null, crypto, 0);
passthroughMediaFormat = format; passthroughMediaFormat.setString(android.media.MediaFormat.KEY_MIME, format.mimeType);
} else { } else {
codec.configure(format, null, crypto, 0); codec.configure(format.getFrameworkMediaFormatV16(), null, crypto, 0);
passthroughMediaFormat = null; passthroughMediaFormat = null;
} }
} }

View File

@ -318,14 +318,10 @@ public abstract class MediaCodecTrackRenderer extends SampleSourceTrackRenderer
* wish to configure the codec with a non-null surface. * wish to configure the codec with a non-null surface.
* *
* @param codec The {@link MediaCodec} to configure. * @param codec The {@link MediaCodec} to configure.
* @param codecIsAdaptive Whether the codec is adaptive.
* @param format The format for which the codec is being configured. * @param format The format for which the codec is being configured.
* @param crypto For drm protected playbacks, a {@link MediaCrypto} to use for decryption. * @param crypto For drm protected playbacks, a {@link MediaCrypto} to use for decryption.
*/ */
protected void configureCodec(MediaCodec codec, boolean codecIsAdaptive, protected abstract void configureCodec(MediaCodec codec, MediaFormat format, MediaCrypto crypto);
android.media.MediaFormat format, MediaCrypto crypto) {
codec.configure(format, null, crypto, 0);
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
protected final void maybeInitCodec() throws ExoPlaybackException { protected final void maybeInitCodec() throws ExoPlaybackException {
@ -381,7 +377,7 @@ public abstract class MediaCodecTrackRenderer extends SampleSourceTrackRenderer
codec = MediaCodec.createByCodecName(codecName); codec = MediaCodec.createByCodecName(codecName);
TraceUtil.endSection(); TraceUtil.endSection();
TraceUtil.beginSection("configureCodec"); TraceUtil.beginSection("configureCodec");
configureCodec(codec, decoderInfo.adaptive, format.getFrameworkMediaFormatV16(), mediaCrypto); configureCodec(codec, format, mediaCrypto);
TraceUtil.endSection(); TraceUtil.endSection();
TraceUtil.beginSection("codec.start()"); TraceUtil.beginSection("codec.start()");
codec.start(); codec.start();

View File

@ -345,10 +345,8 @@ public class MediaCodecVideoTrackRenderer extends MediaCodecTrackRenderer {
// Override configureCodec to provide the surface. // Override configureCodec to provide the surface.
@Override @Override
protected void configureCodec(MediaCodec codec, boolean codecIsAdaptive, protected void configureCodec(MediaCodec codec, MediaFormat format, MediaCrypto crypto) {
android.media.MediaFormat format, MediaCrypto crypto) { codec.configure(getFrameworkMediaFormat(format), surface, crypto, 0);
maybeSetMaxInputSize(format, codecIsAdaptive);
codec.configure(format, surface, crypto, 0);
codec.setVideoScalingMode(videoScalingMode); codec.setVideoScalingMode(videoScalingMode);
} }
@ -519,27 +517,24 @@ public class MediaCodecVideoTrackRenderer extends MediaCodecTrackRenderer {
} }
@SuppressLint("InlinedApi") @SuppressLint("InlinedApi")
private void maybeSetMaxInputSize(android.media.MediaFormat format, boolean codecIsAdaptive) { private android.media.MediaFormat getFrameworkMediaFormat(MediaFormat format) {
if (format.containsKey(android.media.MediaFormat.KEY_MAX_INPUT_SIZE)) { android.media.MediaFormat frameworkMediaFormat = format.getFrameworkMediaFormatV16();
// Already set. The source of the format may know better, so do nothing. if (format.maxInputSize > 0) {
return; // The format already has a maximum input size.
} return frameworkMediaFormat;
int maxHeight = format.getInteger(android.media.MediaFormat.KEY_HEIGHT);
if (codecIsAdaptive && format.containsKey(android.media.MediaFormat.KEY_MAX_HEIGHT)) {
maxHeight = Math.max(maxHeight, format.getInteger(android.media.MediaFormat.KEY_MAX_HEIGHT));
}
int maxWidth = format.getInteger(android.media.MediaFormat.KEY_WIDTH);
if (codecIsAdaptive && format.containsKey(android.media.MediaFormat.KEY_MAX_WIDTH)) {
maxWidth = Math.max(maxHeight, format.getInteger(android.media.MediaFormat.KEY_MAX_WIDTH));
} }
// If the format doesn't define a maximum input size, determine one ourselves.
int maxHeight = Math.max(format.maxHeight, format.height);
int maxWidth = Math.max(format.maxWidth, format.maxWidth);
int maxPixels; int maxPixels;
int minCompressionRatio; int minCompressionRatio;
switch (format.getString(android.media.MediaFormat.KEY_MIME)) { switch (format.mimeType) {
case MimeTypes.VIDEO_H264: case MimeTypes.VIDEO_H264:
if ("BRAVIA 4K 2015".equals(Util.MODEL)) { if ("BRAVIA 4K 2015".equals(Util.MODEL)) {
// The Sony BRAVIA 4k TV has input buffers that are too small for the calculated 4k video // The Sony BRAVIA 4k TV has input buffers that are too small for the calculated 4k video
// maximum input size, so use the default value. // maximum input size, so use the default value.
return; return frameworkMediaFormat;
} }
// Round up width/height to an integer number of macroblocks. // Round up width/height to an integer number of macroblocks.
maxPixels = ((maxWidth + 15) / 16) * ((maxHeight + 15) / 16) * 16 * 16; maxPixels = ((maxWidth + 15) / 16) * ((maxHeight + 15) / 16) * 16 * 16;
@ -556,11 +551,12 @@ public class MediaCodecVideoTrackRenderer extends MediaCodecTrackRenderer {
break; break;
default: default:
// Leave the default max input size. // Leave the default max input size.
return; return frameworkMediaFormat;
} }
// Estimate the maximum input size assuming three channel 4:2:0 subsampled input frames. // Estimate the maximum input size assuming three channel 4:2:0 subsampled input frames.
int maxInputSize = (maxPixels * 3) / (2 * minCompressionRatio); int maxInputSize = (maxPixels * 3) / (2 * minCompressionRatio);
format.setInteger(android.media.MediaFormat.KEY_MAX_INPUT_SIZE, maxInputSize); frameworkMediaFormat.setInteger(android.media.MediaFormat.KEY_MAX_INPUT_SIZE, maxInputSize);
return frameworkMediaFormat;
} }
private void maybeNotifyVideoSizeChanged() { private void maybeNotifyVideoSizeChanged() {