Skip to content

Commit

Permalink
Remove embedded AAC decoder (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin authored May 22, 2024
1 parent a82893b commit 481c5ee
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 110 deletions.
1 change: 0 additions & 1 deletion main/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ base {
dependencies {
api(projects.common)
implementation(projects.nativesPublish)
implementation(libs.jaadec.fork)
implementation(libs.rhino.engine)
implementation(libs.slf4j)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,58 @@
import com.sedmelluq.discord.lavaplayer.filter.PcmFormat;
import com.sedmelluq.discord.lavaplayer.natives.aac.AacDecoder;
import com.sedmelluq.discord.lavaplayer.track.playback.AudioProcessingContext;
import net.sourceforge.jaad.aac.Decoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;
import java.util.function.Consumer;

public class AacPacketRouter {
private static final Logger log = LoggerFactory.getLogger(AacPacketRouter.class);

private final AudioProcessingContext context;
private final Consumer<AacDecoder> decoderConfigurer;

private Long initialRequestedTimecode;
private Long initialProvidedTimecode;
private AudioPipeline downstream;
private ShortBuffer outputBuffer;

public AacDecoder nativeDecoder;
public Decoder embeddedDecoder;
public AacDecoder decoder;

public AacPacketRouter(AudioProcessingContext context) {
public AacPacketRouter(AudioProcessingContext context, Consumer<AacDecoder> decoderConfigurer) {
this.context = context;
this.decoderConfigurer = decoderConfigurer;
}

public void processInput(ByteBuffer inputBuffer) throws InterruptedException {
if (embeddedDecoder == null) {
nativeDecoder.fill(inputBuffer);
if (decoder == null) {
decoder = new AacDecoder();
decoderConfigurer.accept(decoder);
}

if (downstream == null) {
log.debug("Using native decoder");
AacDecoder.StreamInfo streamInfo = nativeDecoder.resolveStreamInfo();
decoder.fill(inputBuffer);

if (streamInfo != null) {
downstream = AudioPipelineFactory.create(context, new PcmFormat(streamInfo.channels, streamInfo.sampleRate));
outputBuffer = ByteBuffer.allocateDirect(2 * streamInfo.frameSize * streamInfo.channels)
.order(ByteOrder.nativeOrder()).asShortBuffer();
if (downstream == null) {
AacDecoder.StreamInfo streamInfo = decoder.resolveStreamInfo();

if (initialRequestedTimecode != null) {
downstream.seekPerformed(initialRequestedTimecode, initialProvidedTimecode);
}
}
}

if (downstream != null) {
while (nativeDecoder.decode(outputBuffer, false)) {
downstream.process(outputBuffer);
outputBuffer.clear();
}
}
} else {
if (downstream == null) {
log.debug("Using embedded decoder");
downstream = AudioPipelineFactory.create(context, new PcmFormat(
embeddedDecoder.getAudioFormat().getChannels(),
(int) embeddedDecoder.getAudioFormat().getSampleRate()
));
if (streamInfo != null) {
downstream = AudioPipelineFactory.create(context, new PcmFormat(streamInfo.channels, streamInfo.sampleRate));
outputBuffer = ByteBuffer.allocateDirect(2 * streamInfo.frameSize * streamInfo.channels)
.order(ByteOrder.nativeOrder()).asShortBuffer();

if (initialRequestedTimecode != null) {
downstream.seekPerformed(initialRequestedTimecode, initialProvidedTimecode);
}
}
}

if (downstream != null) {
downstream.process(embeddedDecoder.decodeFrame(inputBuffer.array()));
if (downstream != null) {
while (decoder.decode(outputBuffer, false)) {
downstream.process(outputBuffer);
outputBuffer.clear();
}
}
}
Expand All @@ -82,17 +69,15 @@ public void seekPerformed(long requestedTimecode, long providedTimecode) {
this.initialProvidedTimecode = providedTimecode;
}

if (nativeDecoder != null) {
nativeDecoder.close();
nativeDecoder = null;
} else if (embeddedDecoder != null) {
embeddedDecoder = null;
if (decoder != null) {
decoder.close();
decoder = null;
}
}

public void flush() throws InterruptedException {
if (downstream != null) {
while (nativeDecoder.decode(outputBuffer, true)) {
while (decoder.decode(outputBuffer, true)) {
downstream.process(outputBuffer);
outputBuffer.clear();
}
Expand All @@ -105,10 +90,8 @@ public void close() {
downstream.close();
}
} finally {
if (nativeDecoder != null) {
nativeDecoder.close();
} else if (embeddedDecoder != null) {
embeddedDecoder = null;
if (decoder != null) {
decoder.close();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.sedmelluq.discord.lavaplayer.container.mpeg.MpegAacTrackConsumer;
import com.sedmelluq.discord.lavaplayer.natives.aac.AacDecoder;
import com.sedmelluq.discord.lavaplayer.track.playback.AudioProcessingContext;
import net.sourceforge.jaad.aac.Decoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -18,18 +17,17 @@ public class MatroskaAacTrackConsumer implements MatroskaTrackConsumer {
private static final Logger log = LoggerFactory.getLogger(MpegAacTrackConsumer.class);

private final MatroskaFileTrack track;
private final ByteBuffer inputBuffer;
private final AacPacketRouter packetRouter;

private ByteBuffer inputBuffer;
private boolean configured;

/**
* @param context Configuration and output information for processing
* @param track The MP4 audio track descriptor
*/
public MatroskaAacTrackConsumer(AudioProcessingContext context, MatroskaFileTrack track) {
this.track = track;
this.packetRouter = new AacPacketRouter(context);
this.inputBuffer = ByteBuffer.allocateDirect(4096);
this.packetRouter = new AacPacketRouter(context, this::configureDecoder);
}

@Override
Expand All @@ -55,28 +53,6 @@ public void flush() throws InterruptedException {

@Override
public void consume(ByteBuffer data) throws InterruptedException {
if (packetRouter.nativeDecoder == null) {
packetRouter.nativeDecoder = new AacDecoder();
configured = configureDecoder(packetRouter.nativeDecoder);
}

if (configured) {
if (inputBuffer == null) {
inputBuffer = ByteBuffer.allocateDirect(4096);
}

processInput(data);
} else {
if (packetRouter.embeddedDecoder == null) {
packetRouter.embeddedDecoder = Decoder.create(track.codecPrivate);
inputBuffer = ByteBuffer.allocate(4096);
}

processInput(data);
}
}

private void processInput(ByteBuffer data) throws InterruptedException {
while (data.hasRemaining()) {
int chunk = Math.min(data.remaining(), inputBuffer.capacity());
ByteBuffer chunkBuffer = data.duplicate();
Expand All @@ -97,7 +73,7 @@ public void close() {
packetRouter.close();
}

private boolean configureDecoder(AacDecoder decoder) {
return (decoder.configure(track.codecPrivate) == 0);
private void configureDecoder(AacDecoder decoder) {
decoder.configure(track.codecPrivate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.sedmelluq.discord.lavaplayer.track.AudioReference;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import com.sedmelluq.discord.lavaplayer.track.info.AudioTrackInfoBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.sedmelluq.discord.lavaplayer.container.common.AacPacketRouter;
import com.sedmelluq.discord.lavaplayer.natives.aac.AacDecoder;
import com.sedmelluq.discord.lavaplayer.track.playback.AudioProcessingContext;
import net.sourceforge.jaad.aac.Decoder;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,18 +20,17 @@ public class MpegAacTrackConsumer implements MpegTrackConsumer {
private static final Logger log = LoggerFactory.getLogger(MpegAacTrackConsumer.class);

private final MpegTrackInfo track;
private final ByteBuffer inputBuffer;
private final AacPacketRouter packetRouter;

private ByteBuffer inputBuffer;
private boolean configured;

/**
* @param context Configuration and output information for processing
* @param track The MP4 audio track descriptor
*/
public MpegAacTrackConsumer(AudioProcessingContext context, MpegTrackInfo track) {
this.track = track;
this.packetRouter = new AacPacketRouter(context);
this.inputBuffer = ByteBuffer.allocateDirect(4096);
this.packetRouter = new AacPacketRouter(context, this::configureDecoder);
}

@Override
Expand All @@ -58,32 +56,6 @@ public void flush() throws InterruptedException {

@Override
public void consume(ReadableByteChannel channel, int length) throws InterruptedException {
if (packetRouter.nativeDecoder == null) {
packetRouter.nativeDecoder = new AacDecoder();
configured = configureDecoder(packetRouter.nativeDecoder);
}

if (configured) {
if (inputBuffer == null) {
inputBuffer = ByteBuffer.allocateDirect(4096);
}

processInput(channel, length);
} else {
if (packetRouter.embeddedDecoder == null) {
if (track.decoderConfig != null) {
packetRouter.embeddedDecoder = Decoder.create(track.decoderConfig);
} else {
packetRouter.embeddedDecoder = Decoder.create(AacDecoder.AAC_LC, track.sampleRate, track.channelCount);
}
inputBuffer = ByteBuffer.allocate(4096);
}

processInput(channel, length);
}
}

private void processInput(ReadableByteChannel channel, int length) throws InterruptedException {
int remaining = length;

while (remaining > 0) {
Expand Down Expand Up @@ -115,11 +87,11 @@ public void close() {
packetRouter.close();
}

private boolean configureDecoder(AacDecoder decoder) {
private void configureDecoder(AacDecoder decoder) {
if (track.decoderConfig != null) {
return (decoder.configure(track.decoderConfig) == 0);
decoder.configure(track.decoderConfig);
} else {
return (decoder.configure(AacDecoder.AAC_LC, track.sampleRate, track.channelCount) == 0);
decoder.configure(AacDecoder.AAC_LC, track.sampleRate, track.channelCount);
}
}
}
1 change: 0 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ fun VersionCatalogBuilder.common() {

fun VersionCatalogBuilder.others() {
library("ibxm-fork", "com.github.walkyst", "ibxm-fork").version("a75")
library("jaadec-fork", "com.github.walkyst", "JAADec-fork").version("0.1.3")
library("rhino-engine", "org.mozilla", "rhino-engine").version("1.7.14")
}

Expand Down

0 comments on commit 481c5ee

Please sign in to comment.