mirror of
https://github.com/androidx/media.git
synced 2025-05-12 01:59:50 +08:00
Add support for MediaCodecAudioRenderer to use float output
Enables the MediaCodec to use float PCM output when the sink supports float PCM input PiperOrigin-RevId: 322856138
This commit is contained in:
parent
cf2a58e6dc
commit
ec78bde50c
@ -19,6 +19,7 @@ import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
|
|||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.media.AudioFormat;
|
||||||
import android.media.MediaCodec;
|
import android.media.MediaCodec;
|
||||||
import android.media.MediaCrypto;
|
import android.media.MediaCrypto;
|
||||||
import android.media.MediaFormat;
|
import android.media.MediaFormat;
|
||||||
@ -101,11 +102,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
|
|||||||
* @param mediaCodecSelector A decoder selector.
|
* @param mediaCodecSelector A decoder selector.
|
||||||
*/
|
*/
|
||||||
public MediaCodecAudioRenderer(Context context, MediaCodecSelector mediaCodecSelector) {
|
public MediaCodecAudioRenderer(Context context, MediaCodecSelector mediaCodecSelector) {
|
||||||
this(
|
this(context, mediaCodecSelector, /* eventHandler= */ null, /* eventListener= */ null);
|
||||||
context,
|
|
||||||
mediaCodecSelector,
|
|
||||||
/* eventHandler= */ null,
|
|
||||||
/* eventListener= */ null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -120,12 +117,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
|
|||||||
MediaCodecSelector mediaCodecSelector,
|
MediaCodecSelector mediaCodecSelector,
|
||||||
@Nullable Handler eventHandler,
|
@Nullable Handler eventHandler,
|
||||||
@Nullable AudioRendererEventListener eventListener) {
|
@Nullable AudioRendererEventListener eventListener) {
|
||||||
this(
|
this(context, mediaCodecSelector, eventHandler, eventListener, (AudioCapabilities) null);
|
||||||
context,
|
|
||||||
mediaCodecSelector,
|
|
||||||
eventHandler,
|
|
||||||
eventListener,
|
|
||||||
(AudioCapabilities) null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -378,8 +370,8 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCodecInitialized(String name, long initializedTimestampMs,
|
protected void onCodecInitialized(
|
||||||
long initializationDurationMs) {
|
String name, long initializedTimestampMs, long initializationDurationMs) {
|
||||||
eventDispatcher.decoderInitialized(name, initializedTimestampMs, initializationDurationMs);
|
eventDispatcher.decoderInitialized(name, initializedTimestampMs, initializationDurationMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -406,15 +398,17 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
|
|||||||
} else {
|
} else {
|
||||||
MediaFormat mediaFormat = getCodec().getOutputFormat();
|
MediaFormat mediaFormat = getCodec().getOutputFormat();
|
||||||
@C.PcmEncoding int pcmEncoding;
|
@C.PcmEncoding int pcmEncoding;
|
||||||
if (mediaFormat.containsKey(VIVO_BITS_PER_SAMPLE_KEY)) {
|
if (MimeTypes.AUDIO_RAW.equals(outputFormat.sampleMimeType)) {
|
||||||
|
// For PCM streams, the encoder passes through int samples despite set to float mode.
|
||||||
|
pcmEncoding = outputFormat.pcmEncoding;
|
||||||
|
} else if (Util.SDK_INT >= 24 && mediaFormat.containsKey(MediaFormat.KEY_PCM_ENCODING)) {
|
||||||
|
pcmEncoding = mediaFormat.getInteger(MediaFormat.KEY_PCM_ENCODING);
|
||||||
|
} else if (mediaFormat.containsKey(VIVO_BITS_PER_SAMPLE_KEY)) {
|
||||||
pcmEncoding = Util.getPcmEncoding(mediaFormat.getInteger(VIVO_BITS_PER_SAMPLE_KEY));
|
pcmEncoding = Util.getPcmEncoding(mediaFormat.getInteger(VIVO_BITS_PER_SAMPLE_KEY));
|
||||||
} else {
|
} else {
|
||||||
// If the format is anything other than PCM then we assume that the audio decoder will
|
// If the format is anything other than PCM then we assume that the audio decoder will
|
||||||
// output 16-bit PCM.
|
// output 16-bit PCM.
|
||||||
pcmEncoding =
|
pcmEncoding = C.ENCODING_PCM_16BIT;
|
||||||
MimeTypes.AUDIO_RAW.equals(outputFormat.sampleMimeType)
|
|
||||||
? outputFormat.pcmEncoding
|
|
||||||
: C.ENCODING_PCM_16BIT;
|
|
||||||
}
|
}
|
||||||
audioSinkInputFormat =
|
audioSinkInputFormat =
|
||||||
new Format.Builder()
|
new Format.Builder()
|
||||||
@ -741,6 +735,12 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
|
|||||||
// not sync frames. Set a format key to override this.
|
// not sync frames. Set a format key to override this.
|
||||||
mediaFormat.setInteger("ac4-is-sync", 1);
|
mediaFormat.setInteger("ac4-is-sync", 1);
|
||||||
}
|
}
|
||||||
|
if (Util.SDK_INT >= 24
|
||||||
|
&& audioSink.getFormatSupport(
|
||||||
|
Util.getPcmFormat(C.ENCODING_PCM_FLOAT, format.channelCount, format.sampleRate))
|
||||||
|
== AudioSink.SINK_FORMAT_SUPPORTED_DIRECTLY) {
|
||||||
|
mediaFormat.setInteger(MediaFormat.KEY_PCM_ENCODING, AudioFormat.ENCODING_PCM_FLOAT);
|
||||||
|
}
|
||||||
return mediaFormat;
|
return mediaFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -769,15 +769,17 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
|
|||||||
/**
|
/**
|
||||||
* Returns whether the decoder is known to output six audio channels when provided with input with
|
* Returns whether the decoder is known to output six audio channels when provided with input with
|
||||||
* fewer than six channels.
|
* fewer than six channels.
|
||||||
* <p>
|
*
|
||||||
* See [Internal: b/35655036].
|
* <p>See [Internal: b/35655036].
|
||||||
*/
|
*/
|
||||||
private static boolean codecNeedsDiscardChannelsWorkaround(String codecName) {
|
private static boolean codecNeedsDiscardChannelsWorkaround(String codecName) {
|
||||||
// The workaround applies to Samsung Galaxy S6 and Samsung Galaxy S7.
|
// The workaround applies to Samsung Galaxy S6 and Samsung Galaxy S7.
|
||||||
return Util.SDK_INT < 24 && "OMX.SEC.aac.dec".equals(codecName)
|
return Util.SDK_INT < 24
|
||||||
|
&& "OMX.SEC.aac.dec".equals(codecName)
|
||||||
&& "samsung".equals(Util.MANUFACTURER)
|
&& "samsung".equals(Util.MANUFACTURER)
|
||||||
&& (Util.DEVICE.startsWith("zeroflte") || Util.DEVICE.startsWith("herolte")
|
&& (Util.DEVICE.startsWith("zeroflte")
|
||||||
|| Util.DEVICE.startsWith("heroqlte"));
|
|| Util.DEVICE.startsWith("herolte")
|
||||||
|
|| Util.DEVICE.startsWith("heroqlte"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user