From 08cfbc05953128f3cf727ea3bcbe41dabcd1c7db Mon Sep 17 00:00:00 2001 From: Walkyst <41806921+Walkyst@users.noreply.github.com> Date: Tue, 4 Jul 2023 15:46:36 +0300 Subject: [PATCH] Fixed ogg streaming --- .../container/ogg/OggAudioTrack.java | 24 +++++++++++++++---- .../container/ogg/OggContainerProbe.java | 3 +-- .../container/ogg/OggPacketInputStream.java | 4 +--- .../ogg/vorbis/OggVorbisCodecHandler.java | 8 +++++-- .../format/StandardAudioDataFormats.java | 2 +- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/OggAudioTrack.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/OggAudioTrack.java index 1b6e5f81d..cb240d04c 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/OggAudioTrack.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/OggAudioTrack.java @@ -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. */ @@ -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); + } } } diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/OggContainerProbe.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/OggContainerProbe.java index 099ad7156..56c175a85 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/OggContainerProbe.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/OggContainerProbe.java @@ -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); @@ -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); } } diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/OggPacketInputStream.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/OggPacketInputStream.java index 476eb1f20..5c1e42636 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/OggPacketInputStream.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/OggPacketInputStream.java @@ -308,9 +308,7 @@ public long seek(long timecode) throws IOException { } } - if (mid > 0) { - mid--; - } else { + if (mid == 0) { mid++; } diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/vorbis/OggVorbisCodecHandler.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/vorbis/OggVorbisCodecHandler.java index 2868557a5..91481425b 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/vorbis/OggVorbisCodecHandler.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/ogg/vorbis/OggVorbisCodecHandler.java @@ -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(); @@ -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 seekPointList = stream.createSeekTable(sampleRate); + if (seekPointList != null) stream.setSeekPoints(seekPointList); return new Blueprint(sampleRate, infoPacket, broker); } @@ -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 ); } diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/format/StandardAudioDataFormats.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/format/StandardAudioDataFormats.java index 8b5779ba7..3b5c125d0 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/format/StandardAudioDataFormats.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/format/StandardAudioDataFormats.java @@ -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); }