Search in sources :

Example 1 with UnhandledAudioFormatException

use of com.google.android.exoplayer2.audio.AudioProcessor.UnhandledAudioFormatException in project ExoPlayer by google.

the class DefaultAudioSink method configure.

@Override
public void configure(Format inputFormat, int specifiedBufferSize, @Nullable int[] outputChannels) throws ConfigurationException {
    int inputPcmFrameSize;
    @Nullable AudioProcessor[] availableAudioProcessors;
    @OutputMode int outputMode;
    @C.Encoding int outputEncoding;
    int outputSampleRate;
    int outputChannelConfig;
    int outputPcmFrameSize;
    if (MimeTypes.AUDIO_RAW.equals(inputFormat.sampleMimeType)) {
        Assertions.checkArgument(Util.isEncodingLinearPcm(inputFormat.pcmEncoding));
        inputPcmFrameSize = Util.getPcmFrameSize(inputFormat.pcmEncoding, inputFormat.channelCount);
        availableAudioProcessors = shouldUseFloatOutput(inputFormat.pcmEncoding) ? toFloatPcmAvailableAudioProcessors : toIntPcmAvailableAudioProcessors;
        trimmingAudioProcessor.setTrimFrameCount(inputFormat.encoderDelay, inputFormat.encoderPadding);
        if (Util.SDK_INT < 21 && inputFormat.channelCount == 8 && outputChannels == null) {
            // AudioTrack doesn't support 8 channel output before Android L. Discard the last two (side)
            // channels to give a 6 channel stream that is supported.
            outputChannels = new int[6];
            for (int i = 0; i < outputChannels.length; i++) {
                outputChannels[i] = i;
            }
        }
        channelMappingAudioProcessor.setChannelMap(outputChannels);
        AudioProcessor.AudioFormat outputFormat = new AudioProcessor.AudioFormat(inputFormat.sampleRate, inputFormat.channelCount, inputFormat.pcmEncoding);
        for (AudioProcessor audioProcessor : availableAudioProcessors) {
            try {
                AudioProcessor.AudioFormat nextFormat = audioProcessor.configure(outputFormat);
                if (audioProcessor.isActive()) {
                    outputFormat = nextFormat;
                }
            } catch (UnhandledAudioFormatException e) {
                throw new ConfigurationException(e, inputFormat);
            }
        }
        outputMode = OUTPUT_MODE_PCM;
        outputEncoding = outputFormat.encoding;
        outputSampleRate = outputFormat.sampleRate;
        outputChannelConfig = Util.getAudioTrackChannelConfig(outputFormat.channelCount);
        outputPcmFrameSize = Util.getPcmFrameSize(outputEncoding, outputFormat.channelCount);
    } else {
        inputPcmFrameSize = C.LENGTH_UNSET;
        availableAudioProcessors = new AudioProcessor[0];
        outputSampleRate = inputFormat.sampleRate;
        outputPcmFrameSize = C.LENGTH_UNSET;
        if (useOffloadedPlayback(inputFormat, audioAttributes)) {
            outputMode = OUTPUT_MODE_OFFLOAD;
            outputEncoding = MimeTypes.getEncoding(checkNotNull(inputFormat.sampleMimeType), inputFormat.codecs);
            outputChannelConfig = Util.getAudioTrackChannelConfig(inputFormat.channelCount);
        } else {
            outputMode = OUTPUT_MODE_PASSTHROUGH;
            @Nullable Pair<Integer, Integer> encodingAndChannelConfig = getEncodingAndChannelConfigForPassthrough(inputFormat, audioCapabilities);
            if (encodingAndChannelConfig == null) {
                throw new ConfigurationException("Unable to configure passthrough for: " + inputFormat, inputFormat);
            }
            outputEncoding = encodingAndChannelConfig.first;
            outputChannelConfig = encodingAndChannelConfig.second;
        }
    }
    int bufferSize = specifiedBufferSize != 0 ? specifiedBufferSize : audioTrackBufferSizeProvider.getBufferSizeInBytes(getAudioTrackMinBufferSize(outputSampleRate, outputChannelConfig, outputEncoding), outputEncoding, outputMode, outputPcmFrameSize, outputSampleRate, enableAudioTrackPlaybackParams ? MAX_PLAYBACK_SPEED : DEFAULT_PLAYBACK_SPEED);
    if (outputEncoding == C.ENCODING_INVALID) {
        throw new ConfigurationException("Invalid output encoding (mode=" + outputMode + ") for: " + inputFormat, inputFormat);
    }
    if (outputChannelConfig == AudioFormat.CHANNEL_INVALID) {
        throw new ConfigurationException("Invalid output channel config (mode=" + outputMode + ") for: " + inputFormat, inputFormat);
    }
    offloadDisabledUntilNextConfiguration = false;
    Configuration pendingConfiguration = new Configuration(inputFormat, inputPcmFrameSize, outputMode, outputPcmFrameSize, outputSampleRate, outputChannelConfig, outputEncoding, bufferSize, availableAudioProcessors);
    if (isAudioTrackInitialized()) {
        this.pendingConfiguration = pendingConfiguration;
    } else {
        configuration = pendingConfiguration;
    }
}
Also used : SuppressLint(android.annotation.SuppressLint) UnhandledAudioFormatException(com.google.android.exoplayer2.audio.AudioProcessor.UnhandledAudioFormatException) AudioFormat(android.media.AudioFormat) Nullable(androidx.annotation.Nullable)

Example 2 with UnhandledAudioFormatException

use of com.google.android.exoplayer2.audio.AudioProcessor.UnhandledAudioFormatException in project ExoPlayer by google.

the class SonicAudioProcessorTest method doesNotSupportNon16BitInput.

@Test
public void doesNotSupportNon16BitInput() throws Exception {
    try {
        sonicAudioProcessor.configure(new AudioFormat(/* sampleRate= */
        44100, /* channelCount= */
        2, /* encoding= */
        C.ENCODING_PCM_8BIT));
        fail();
    } catch (UnhandledAudioFormatException e) {
    // Expected.
    }
    try {
        sonicAudioProcessor.configure(new AudioFormat(/* sampleRate= */
        44100, /* channelCount= */
        2, /* encoding= */
        C.ENCODING_PCM_24BIT));
        fail();
    } catch (UnhandledAudioFormatException e) {
    // Expected.
    }
    try {
        sonicAudioProcessor.configure(new AudioFormat(/* sampleRate= */
        44100, /* channelCount= */
        2, /* encoding= */
        C.ENCODING_PCM_32BIT));
        fail();
    } catch (UnhandledAudioFormatException e) {
    // Expected.
    }
}
Also used : UnhandledAudioFormatException(com.google.android.exoplayer2.audio.AudioProcessor.UnhandledAudioFormatException) AudioFormat(com.google.android.exoplayer2.audio.AudioProcessor.AudioFormat) Test(org.junit.Test)

Aggregations

UnhandledAudioFormatException (com.google.android.exoplayer2.audio.AudioProcessor.UnhandledAudioFormatException)2 SuppressLint (android.annotation.SuppressLint)1 AudioFormat (android.media.AudioFormat)1 Nullable (androidx.annotation.Nullable)1 AudioFormat (com.google.android.exoplayer2.audio.AudioProcessor.AudioFormat)1 Test (org.junit.Test)1