X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Fsource%2FStreamSource.java;h=34a952ccd8570252dfd75e65192a912d8d00527d;hb=0502acae0ba629fae4b66bf08f13ff5f442c9fb4;hp=a116d803da4cdded15617247eed6fc01dcd5a970;hpb=c548332c486d812bfce23a9121219bf86ecc5588;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 a116d80..34a952c 100644
--- a/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java
+++ b/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java
@@ -25,17 +25,18 @@ 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.FormatMetadata;
import net.pterodactylus.sonitus.data.Metadata;
-import net.pterodactylus.sonitus.data.Source;
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;
/**
@@ -46,10 +47,10 @@ import com.google.common.primitives.Ints;
*
* @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;
@@ -60,9 +61,6 @@ 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
@@ -75,8 +73,8 @@ public class StreamSource implements Source {
* @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);
@@ -89,6 +87,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. */
@@ -122,13 +121,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
@@ -141,25 +140,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());
- 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);
}
//
@@ -168,7 +168,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());
}
}