X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Fsource%2FStreamSource.java;h=8b0c518896440e0e058d559331acab2c51d51e7c;hb=eacf380129e247dd03f7d054d67e34cb43658959;hp=604a868c006345a0437dff6be1da6b500c3119e7;hpb=d07ec839a266057079d5a065176d46a0cc567b5d;p=sonitus.git diff --git a/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java b/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java index 604a868..8b0c518 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java +++ b/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java @@ -25,32 +25,33 @@ import java.net.URLConnection; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.logging.Logger; +import net.pterodactylus.sonitus.data.AbstractFilter; import net.pterodactylus.sonitus.data.ContentMetadata; import net.pterodactylus.sonitus.data.Controller; +import net.pterodactylus.sonitus.data.DataPacket; +import net.pterodactylus.sonitus.data.Filter; import net.pterodactylus.sonitus.data.FormatMetadata; import net.pterodactylus.sonitus.data.Metadata; -import net.pterodactylus.sonitus.data.Source; -import net.pterodactylus.sonitus.data.event.MetadataUpdated; import net.pterodactylus.sonitus.io.MetadataStream; import com.google.common.base.Optional; import com.google.common.collect.Maps; -import com.google.common.eventbus.EventBus; import com.google.common.primitives.Ints; /** - * {@link Source} implementation that can download an audio stream from a + * {@link Filter} implementation that can download an audio stream from a * streaming server. *

* Currently only “audio/mpeg” (aka MP3) streams are supported. * * @author David ‘Bombe’ Roden */ -public class StreamSource implements Source { +public class StreamSource extends AbstractFilter { - /** The event bus. */ - private final EventBus eventBus; + /** The logger. */ + private static final Logger logger = Logger.getLogger(StreamSource.class.getName()); /** The URL of the stream. */ private final String streamUrl; @@ -61,23 +62,18 @@ public class StreamSource implements Source { /** The metadata stream. */ private final MetadataStream metadataStream; - /** The current metadata. */ - private Metadata metadata; - /** * Creates a new stream source. This will also connect to the server and parse * the response header for vital information (sampling frequency, number of * channels, etc.). * - * @param eventBus - * The event bus * @param streamUrl * The URL of the stream * @throws IOException * if an I/O error occurs */ - public StreamSource(EventBus eventBus, String streamUrl) throws IOException { - this.eventBus = eventBus; + public StreamSource(String streamUrl) throws IOException { + super(null); this.streamUrl = streamUrl; URL url = new URL(streamUrl); @@ -90,6 +86,7 @@ public class StreamSource implements Source { httpUrlConnection.setRequestProperty("ICY-Metadata", "1"); /* connect. */ + logger.info(String.format("Connecting to %s...", streamUrl)); httpUrlConnection.connect(); /* check content type. */ @@ -123,13 +120,13 @@ public class StreamSource implements Source { throw new IllegalArgumentException(String.format("Invalid Metadata Interval header: %s", metadataIntervalHeader)); } - metadata = new Metadata(new FormatMetadata(audioParameters.get("ice-channels"), audioParameters.get("ice-samplerate"), "MP3"), new ContentMetadata()); + metadataUpdated(new Metadata(new FormatMetadata(audioParameters.get("ice-channels"), audioParameters.get("ice-samplerate"), "MP3"), new ContentMetadata())); metadataStream = new MetadataStream(new BufferedInputStream(httpUrlConnection.getInputStream()), metadataInterval); streamName = httpUrlConnection.getHeaderField("ICY-Name"); } // - // CONTROLLED METHODS + // FILTER METHODS // @Override @@ -142,26 +139,26 @@ public class StreamSource implements Source { return Collections.emptyList(); } - // - // SOURCE METHODS - // - @Override public Metadata metadata() { Optional streamMetadata = metadataStream.getContentMetadata(); if (!streamMetadata.isPresent()) { - return metadata; + return super.metadata(); } - metadata = metadata.title(streamMetadata.get().title()); - eventBus.post(new MetadataUpdated(this, metadata)); - return metadata; + metadataUpdated(super.metadata().title(streamMetadata.get().title())); + return super.metadata(); + } + + @Override + public void open(Metadata metadata) throws IOException { + /* ignore metadata when opening. */ } @Override - public byte[] get(int bufferSize) throws IOException { + public DataPacket get(int bufferSize) throws IOException { byte[] buffer = new byte[bufferSize]; metadataStream.read(buffer); - return buffer; + return new DataPacket(metadata(), buffer); } // @@ -170,7 +167,7 @@ public class StreamSource implements Source { @Override public String toString() { - return String.format("StreamSource(%s,%s)", streamUrl, metadata); + return String.format("StreamSource(%s,%s)", streamUrl, metadata()); } }