Skip to content

Commit

Permalink
Fixed ogg streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Walkyst committed Jul 4, 2023
1 parent 17c75f5 commit 08cfbc0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.sedmelluq.discord.lavaplayer.container.ogg;

import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.tools.io.SeekableInputStream;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import com.sedmelluq.discord.lavaplayer.track.BaseAudioTrack;
import com.sedmelluq.discord.lavaplayer.track.playback.AudioProcessingContext;
import com.sedmelluq.discord.lavaplayer.track.playback.LocalAudioTrackExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import static com.sedmelluq.discord.lavaplayer.tools.FriendlyException.Severity.SUSPICIOUS;

/**
* Audio track which handles an OGG stream.
*/
Expand All @@ -32,15 +36,27 @@ public void process(final LocalAudioTrackExecutor localExecutor) throws IOExcept
OggPacketInputStream packetInputStream = new OggPacketInputStream(inputStream, false);
OggTrackBlueprint blueprint = OggTrackLoader.loadTrackBlueprint(packetInputStream);

log.debug("Starting to play an OGG track {}", getIdentifier());

if (blueprint == null) {
throw new IOException("Stream terminated before the first packet.");
}

OggTrackHandler handler = blueprint.loadTrackHandler(packetInputStream);
localExecutor.executeProcessingLoop(() -> {
try {
processTrackLoop(packetInputStream, localExecutor.getProcessingContext(), handler, blueprint);
} catch (IOException e) {
throw new FriendlyException("Stream broke when playing OGG track.", SUSPICIOUS, e);
}
}, handler::seekToTimecode, true);
}

log.debug("Starting to play an OGG track {}", getIdentifier());

handler.initialise(localExecutor.getProcessingContext(), 0, 0);
localExecutor.executeProcessingLoop(handler::provideFrames, handler::seekToTimecode, true);
private void processTrackLoop(OggPacketInputStream packetInputStream, AudioProcessingContext context, OggTrackHandler handler, OggTrackBlueprint blueprint) throws IOException, InterruptedException {
while (blueprint != null) {
handler.initialise(context, 0, 0);
handler.provideFrames();
blueprint = OggTrackLoader.loadTrackBlueprint(packetInputStream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public MediaContainerDetectionResult probe(AudioReference reference, SeekableInp

log.debug("Track {} is an OGG file.", reference.identifier);

AudioTrackInfoBuilder infoBuilder = AudioTrackInfoBuilder.create(reference, stream).setIsStream(true);
AudioTrackInfoBuilder infoBuilder = AudioTrackInfoBuilder.create(reference, stream);

try {
collectStreamInformation(stream, infoBuilder);
Expand All @@ -62,7 +62,6 @@ private void collectStreamInformation(SeekableInputStream stream, AudioTrackInfo
OggMetadata metadata = OggTrackLoader.loadMetadata(packetInputStream);

if (metadata != null) {
if (metadata.getLength() != null) infoBuilder.setIsStream(false);
infoBuilder.apply(metadata);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,7 @@ public long seek(long timecode) throws IOException {
}
}

if (mid > 0) {
mid--;
} else {
if (mid == 0) {
mid++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import com.sedmelluq.discord.lavaplayer.container.ogg.OggCodecHandler;
import com.sedmelluq.discord.lavaplayer.container.ogg.OggMetadata;
import com.sedmelluq.discord.lavaplayer.container.ogg.OggPacketInputStream;
import com.sedmelluq.discord.lavaplayer.container.ogg.OggSeekPoint;
import com.sedmelluq.discord.lavaplayer.container.ogg.OggStreamSizeInfo;
import com.sedmelluq.discord.lavaplayer.container.ogg.OggTrackBlueprint;
import com.sedmelluq.discord.lavaplayer.container.ogg.OggTrackHandler;
import com.sedmelluq.discord.lavaplayer.tools.Units;
import com.sedmelluq.discord.lavaplayer.tools.io.DirectBufferStreamBroker;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;

public class OggVorbisCodecHandler implements OggCodecHandler {
private static final int VORBIS_IDENTIFIER = ByteBuffer.wrap(new byte[] { 0x01, 'v', 'o', 'r' }).getInt();
Expand All @@ -37,7 +40,8 @@ public OggTrackBlueprint loadBlueprint(OggPacketInputStream stream, DirectBuffer
loadCommentsHeader(stream, broker, true);
ByteBuffer infoBuffer = ByteBuffer.wrap(infoPacket);
int sampleRate = Integer.reverseBytes(infoBuffer.getInt(12));
stream.setSeekPoints(stream.createSeekTable(sampleRate));
List<OggSeekPoint> seekPointList = stream.createSeekTable(sampleRate);
if (seekPointList != null) stream.setSeekPoints(seekPointList);
return new Blueprint(sampleRate, infoPacket, broker);
}

Expand All @@ -60,7 +64,7 @@ public OggMetadata loadMetadata(OggPacketInputStream stream, DirectBufferStreamB

return new OggMetadata(
VorbisCommentParser.parse(commentsPacket, broker.isTruncated()),
sizeInfo.getDuration()
sizeInfo != null ? sizeInfo.getDuration() : Units.DURATION_MS_UNKNOWN
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class StandardAudioDataFormats {
*/
public static final AudioDataFormat COMMON_PCM_S16_BE = new Pcm16AudioDataFormat(2, 44100, 960, true);
/**
* Signed 16-bit big-endian PCM format matching with the most common sample rate.
* Signed 16-bit little-endian PCM format matching with the most common sample rate.
*/
public static final AudioDataFormat COMMON_PCM_S16_LE = new Pcm16AudioDataFormat(2, 44100, 960, false);
}

0 comments on commit 08cfbc0

Please sign in to comment.