Skip to content

Commit

Permalink
Possible fix for
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sfuhrm committed Jan 28, 2024
1 parent 012c248 commit f18e1f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f18e1f2

Please sign in to comment.