From f18e1f2b4ac005d338d4f1a16fd006918d082664 Mon Sep 17 00:00:00 2001 From: Stephan Fuhrmann Date: Sun, 28 Jan 2024 13:47:03 +0100 Subject: [PATCH] Possible fix for java.lang.IllegalArgumentException: Unsupported conversion: PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian from AAC 44100.0 Hz, 16 bit, stereo, 4 bytes/frame Works with OGG *and* AAC. Endinaness conversion seems to be a thing. --- .../radiorecorder/consumer/StreamPlayConsumer.java | 12 ++++++------ .../consumer/StreamPlayConsumerTest.java | 9 +++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/sfuhrm/radiorecorder/consumer/StreamPlayConsumer.java b/src/main/java/de/sfuhrm/radiorecorder/consumer/StreamPlayConsumer.java index 152f199..5a504fb 100644 --- a/src/main/java/de/sfuhrm/radiorecorder/consumer/StreamPlayConsumer.java +++ b/src/main/java/de/sfuhrm/radiorecorder/consumer/StreamPlayConsumer.java @@ -56,24 +56,24 @@ protected void __accept(HttpConnection t, InputStream inputStream) { byte[] buffer = new byte[BUFFER_SIZE]; String contentType = t.getContentType(); + log.debug("Content type {}", contentType); // this is not needed, but will make the AAC codec fail in an // endless loop because it is thinking MP3 can be interpreted as AAC //AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(t.getURI().toURL()); - boolean bigEndian = true; - if (contentType.equals("audio/ogg")) { - bigEndian = false; - } - AudioFormat targetFormat = new AudioFormat(44100, 16, 2, true, bigEndian); - // many audio codecs need mark() and reset() to work if (! inputStream.markSupported()) { inputStream = new BufferedInputStream(inputStream); } AudioInputStream input = AudioSystem.getAudioInputStream(inputStream); + log.debug("Input format {}", input.getFormat()); + + boolean bigEndian = input.getFormat().isBigEndian(); + AudioFormat targetFormat = new AudioFormat(44100, 16, 2, true, bigEndian); + log.debug("Target format {}", targetFormat); AudioInputStream converted = AudioSystem.getAudioInputStream(targetFormat, input); Mixer.Info mixerInfo = getContext().getMixerInfo(); try (SourceDataLine line = AudioSystem.getSourceDataLine(targetFormat, mixerInfo)) { diff --git a/src/test/java/de/sfuhrm/radiorecorder/consumer/StreamPlayConsumerTest.java b/src/test/java/de/sfuhrm/radiorecorder/consumer/StreamPlayConsumerTest.java index 67e9858..112c52a 100644 --- a/src/test/java/de/sfuhrm/radiorecorder/consumer/StreamPlayConsumerTest.java +++ b/src/test/java/de/sfuhrm/radiorecorder/consumer/StreamPlayConsumerTest.java @@ -20,6 +20,7 @@ import de.sfuhrm.radiorecorder.http.HttpConnectionBuilderFactory; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -80,6 +81,14 @@ void __acceptWithM4A() throws IOException { innerTest(uri); } + // disabled, endless stream + @Disabled + @Test + void __acceptWithAAC() throws IOException { + URI uri = URI.create("https://playerservices.streamtheworld.com/api/livestream-redirect/XHPSFMAAC.aac"); + innerTest(uri); + } + private void innerTest(URI uri) throws IOException { URL url = uri.toURL(); URLConnection urlConnection = url.openConnection();