From: David ‘Bombe’ Roden Date: Tue, 28 May 2013 08:31:35 +0000 (+0200) Subject: Move event and metadata handling into abstract base class. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=cbeadf6d9eea57ab98cacd60e2419dd3c18bef89;p=sonitus.git Move event and metadata handling into abstract base class. --- diff --git a/src/main/java/net/pterodactylus/sonitus/data/AbstractControlledComponent.java b/src/main/java/net/pterodactylus/sonitus/data/AbstractControlledComponent.java index 2c63a08..15740ad 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/AbstractControlledComponent.java +++ b/src/main/java/net/pterodactylus/sonitus/data/AbstractControlledComponent.java @@ -18,6 +18,7 @@ package net.pterodactylus.sonitus.data; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; import com.google.common.collect.Lists; @@ -29,8 +30,24 @@ import com.google.common.collect.Lists; */ public abstract class AbstractControlledComponent implements ControlledComponent { + /** The name of this filter. */ + private final String name; + /** The list of metadata listeners. */ - private final List metadataListeners = Lists.newArrayList(); + private final List metadataListeners = Lists.newCopyOnWriteArrayList(); + + /** The current metadata. */ + private AtomicReference metadata = new AtomicReference(); + + /** + * Creates a new abstract controlled component. + * + * @param name + * The name of the component + */ + protected AbstractControlledComponent(String name) { + this.name = name; + } // // LISTENER MANAGEMENT @@ -47,6 +64,29 @@ public abstract class AbstractControlledComponent implements ControlledComponent } // + // CONTROLLEDCOMPONENT METHODS + // + + @Override + public String name() { + return name; + } + + @Override + public Metadata metadata() { + return metadata.get(); + } + + @Override + public void metadataUpdated(Metadata metadata) { + if (metadata.equals(this.metadata.get())) { + return; + } + this.metadata.set(metadata); + fireMetadataUpdated(metadata); + } + + // // EVENT METHODS // diff --git a/src/main/java/net/pterodactylus/sonitus/data/ControlledComponent.java b/src/main/java/net/pterodactylus/sonitus/data/ControlledComponent.java index ee50620..d3fde8e 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/ControlledComponent.java +++ b/src/main/java/net/pterodactylus/sonitus/data/ControlledComponent.java @@ -63,4 +63,14 @@ public interface ControlledComponent { */ public List> controllers(); + /** + * Notifies the sink that the metadata of the audio stream has changed. This + * method should return as fast as possible, i.e. every heavy lifting should be + * done from another thread. + * + * @param metadata + * The new metadata + */ + void metadataUpdated(Metadata metadata); + } diff --git a/src/main/java/net/pterodactylus/sonitus/data/Pipeline.java b/src/main/java/net/pterodactylus/sonitus/data/Pipeline.java index ee67eec..c24502f 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/Pipeline.java +++ b/src/main/java/net/pterodactylus/sonitus/data/Pipeline.java @@ -335,8 +335,6 @@ public class Pipeline implements Iterable { Metadata firstMetadata = null; while (!stopped.get()) { try { - final Metadata lastMetadata = firstMetadata; - final Metadata metadata = firstMetadata = source.metadata(); final byte[] buffer; try { logger.finest(String.format("Getting %d bytes from %s...", 4096, source)); @@ -353,9 +351,6 @@ public class Pipeline implements Iterable { @Override public Void call() throws Exception { - if (!metadata.equals(lastMetadata)) { - sink.metadataUpdated(metadata); - } try { logger.finest(String.format("Sending %d bytes to %s.", buffer.length, sink)); sink.process(buffer); diff --git a/src/main/java/net/pterodactylus/sonitus/data/Sink.java b/src/main/java/net/pterodactylus/sonitus/data/Sink.java index cacfdb7..937a4e5 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/Sink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/Sink.java @@ -33,14 +33,4 @@ public interface Sink extends ControlledComponent { */ void process(byte[] buffer) throws IOException; - /** - * Notifies the sink that the metadata of the audio stream has changed. This - * method should return as fast as possible, i.e. every heavy lifting should be - * done from another thread. - * - * @param metadata - * The new metadata - */ - void metadataUpdated(Metadata metadata); - } diff --git a/src/main/java/net/pterodactylus/sonitus/data/event/MetadataUpdated.java b/src/main/java/net/pterodactylus/sonitus/data/event/MetadataUpdated.java deleted file mode 100644 index d60f33a..0000000 --- a/src/main/java/net/pterodactylus/sonitus/data/event/MetadataUpdated.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Sonitus - MetadataUpdated.java - Copyright © 2013 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sonitus.data.event; - -import net.pterodactylus.sonitus.data.ControlledComponent; -import net.pterodactylus.sonitus.data.Metadata; - -/** - * Event that notifies all listeners that the {@link Metadata} of a {@link - * ControlledComponent} component was changed. - * - * @author David ‘Bombe’ Roden - */ -public class MetadataUpdated { - - /** The controlled component. */ - private final ControlledComponent controlledComponent; - - /** The new metadata. */ - private final Metadata metadata; - - /** - * Creates a new metadata updated event. - * - * @param controlledComponent - * The controlled component - * @param metadata - * The new metadata - */ - public MetadataUpdated(ControlledComponent controlledComponent, Metadata metadata) { - this.controlledComponent = controlledComponent; - this.metadata = metadata; - } - - // - // ACCESSORS - // - - /** - * Returns the controlled component. - * - * @return The controlled component - */ - public ControlledComponent controlled() { - return controlledComponent; - } - - /** - * Returns the new metadata. - * - * @return The new metadata - */ - public Metadata metadata() { - return metadata; - } - -} diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/AudioProcessingFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/AudioProcessingFilter.java index b584395..e87935b 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/AudioProcessingFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/AudioProcessingFilter.java @@ -23,8 +23,6 @@ import java.io.OutputStream; import net.pterodactylus.sonitus.data.Filter; import net.pterodactylus.sonitus.io.ProcessingOutputStream; -import com.google.common.eventbus.EventBus; - /** * {@link Filter} implementation that can process audio samples internally. * @@ -35,13 +33,11 @@ public abstract class AudioProcessingFilter extends DummyFilter { /** * Creates a new audio processing filter with the given name. * - * @param eventBus - * The event bus * @param name * The name of the filter */ - protected AudioProcessingFilter(EventBus eventBus, String name) { - super(eventBus, name); + protected AudioProcessingFilter(String name) { + super(name); } // diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/DummyFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/DummyFilter.java index 4b23c06..ae6899d 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/DummyFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/DummyFilter.java @@ -31,9 +31,7 @@ import net.pterodactylus.sonitus.data.AbstractControlledComponent; import net.pterodactylus.sonitus.data.Controller; import net.pterodactylus.sonitus.data.Filter; import net.pterodactylus.sonitus.data.Metadata; -import net.pterodactylus.sonitus.data.event.MetadataUpdated; -import com.google.common.eventbus.EventBus; import com.google.common.io.Closeables; /** @@ -43,32 +41,20 @@ import com.google.common.io.Closeables; */ public class DummyFilter extends AbstractControlledComponent implements Filter { - /** The name of this filter. */ - private final String name; - - /** The event bus. */ - private final EventBus eventBus; - /** The input stream from which to read. */ private InputStream inputStream; /** The output stream to which to write. */ private OutputStream outputStream; - /** The current metadata. */ - private Metadata metadata; - /** * Creates a new dummy filter with the given name. * - * @param eventBus - * The event bus * @param name * The name of the filter */ - public DummyFilter(EventBus eventBus, String name) { - this.eventBus = eventBus; - this.name = name; + public DummyFilter(String name) { + super(name); } // @@ -76,11 +62,6 @@ public class DummyFilter extends AbstractControlledComponent implements Filter { // @Override - public String name() { - return name; - } - - @Override public List> controllers() { return Collections.emptyList(); } @@ -107,18 +88,6 @@ public class DummyFilter extends AbstractControlledComponent implements Filter { } @Override - public Metadata metadata() { - return metadata; - } - - @Override - public void metadataUpdated(Metadata metadata) { - this.metadata = metadata; - fireMetadataUpdated(metadata); - eventBus.post(new MetadataUpdated(this, metadata)); - } - - @Override public void process(byte[] buffer) throws IOException { outputStream.write(buffer); outputStream.flush(); diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java index a6e2966..990f976 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java @@ -27,7 +27,6 @@ import net.pterodactylus.sonitus.io.InputStreamDrainer; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import com.google.common.eventbus.EventBus; /** * {@link net.pterodactylus.sonitus.data.Filter} implementation that runs its @@ -46,13 +45,11 @@ public abstract class ExternalFilter extends DummyFilter { /** * Creates a new external filter with the given name. * - * @param eventBus - * The event bus * @param name * The name of the filter */ - protected ExternalFilter(EventBus eventBus, String name) { - super(eventBus, name); + protected ExternalFilter(String name) { + super(name); } // diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Decoder.java b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Decoder.java index d97708d..f81d532 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Decoder.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Decoder.java @@ -24,8 +24,6 @@ import java.io.IOException; import net.pterodactylus.sonitus.data.Metadata; -import com.google.common.eventbus.EventBus; - /** * Basic {@link net.pterodactylus.sonitus.data.filter.ExternalFilter} * implementation that verifies that the connected source is MP3-encoded and @@ -38,13 +36,11 @@ public abstract class ExternalMp3Decoder extends ExternalFilter { /** * Creates a new external MP3 decoder. * - * @param eventBus - * The event bus * @param name * The name of the filter */ - protected ExternalMp3Decoder(EventBus eventBus, String name) { - super(eventBus, name); + protected ExternalMp3Decoder(String name) { + super(name); } @Override diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Encoder.java b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Encoder.java index 85810c9..21654af 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Encoder.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Encoder.java @@ -24,8 +24,6 @@ import java.io.IOException; import net.pterodactylus.sonitus.data.Metadata; -import com.google.common.eventbus.EventBus; - /** * Basic {@link net.pterodactylus.sonitus.data.filter.ExternalFilter} * implementation that verifies that the connected source is PCM-encoded and @@ -38,13 +36,11 @@ public abstract class ExternalMp3Encoder extends ExternalFilter { /** * Creates a new external MP3 encoder. * - * @param eventBus - * The event bus * @param name * The name of the filter */ - protected ExternalMp3Encoder(EventBus eventBus, String name) { - super(eventBus, name); + protected ExternalMp3Encoder(String name) { + super(name); } @Override diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/FlacDecoder.java b/src/main/java/net/pterodactylus/sonitus/data/filter/FlacDecoder.java index fb644d3..e8447ef 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/FlacDecoder.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/FlacDecoder.java @@ -20,7 +20,6 @@ package net.pterodactylus.sonitus.data.filter; import net.pterodactylus.sonitus.data.Metadata; import com.google.common.collect.ImmutableList; -import com.google.common.eventbus.EventBus; /** * Decoder {@link net.pterodactylus.sonitus.data.Filter} for FLAC files. @@ -38,13 +37,11 @@ public class FlacDecoder extends ExternalFilter { /** * Creates a new FLAC decoder. * - * @param eventBus - * The event bus * @param binary * The location of the binary */ - public FlacDecoder(EventBus eventBus, String binary) { - super(eventBus, "FLAC Decoder"); + public FlacDecoder(String binary) { + super("FLAC Decoder"); this.binary = binary; } diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Decoder.java b/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Decoder.java index 17e0973..7493b3e 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Decoder.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Decoder.java @@ -20,7 +20,6 @@ package net.pterodactylus.sonitus.data.filter; import net.pterodactylus.sonitus.data.Metadata; import com.google.common.collect.ImmutableList; -import com.google.common.eventbus.EventBus; /** * {@link ExternalMp3Decoder} implementation that uses LAME to decode an MP3. @@ -38,13 +37,11 @@ public class LameMp3Decoder extends ExternalMp3Decoder { /** * Creates a new LAME MP3 decoder. * - * @param eventBus - * The event bus * @param binary * The location of the binary */ - public LameMp3Decoder(EventBus eventBus, String binary) { - super(eventBus, "LAME Decoder"); + public LameMp3Decoder(String binary) { + super("LAME Decoder"); this.binary = binary; } diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Encoder.java b/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Encoder.java index 77df8aa..ffd4cc6 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Encoder.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Encoder.java @@ -23,7 +23,6 @@ import net.pterodactylus.sonitus.data.Metadata; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; -import com.google.common.eventbus.EventBus; /** * {@link ExternalMp3Encoder} implementation that uses LAME to encode MP3s. @@ -67,45 +66,36 @@ public class LameMp3Encoder extends ExternalMp3Encoder { /** * Creates a new LAME MP3 encoder. * - * @param eventBus - * The event bus * @param binary * The location of the binary * @param preset - * The preset to use */ - public LameMp3Encoder(EventBus eventBus, String binary, Preset preset) { - this(eventBus, binary, preset, -1); + public LameMp3Encoder(String binary, Preset preset) { + this(binary, preset, -1); } /** * Creates a new LAME MP3 encoder. * - * @param eventBus - * The event bus * @param binary * The location of the binary * @param bitrate - * The bitrate to encode to (in kbps) */ - public LameMp3Encoder(EventBus eventBus, String binary, int bitrate) { - this(eventBus, binary, null, bitrate); + public LameMp3Encoder(String binary, int bitrate) { + this(binary, null, bitrate); } /** * Creates a new LAME MP3 encoder. * - * @param eventBus - * The event bus * @param binary * The location of the binary * @param preset * The preset to use * @param bitrate - * The bitrate to encode to (in kbps) */ - private LameMp3Encoder(EventBus eventBus, String binary, Preset preset, int bitrate) { - super(eventBus, "LAME Encoder"); + private LameMp3Encoder(String binary, Preset preset, int bitrate) { + super("LAME Encoder"); this.binary = binary; this.preset = Optional.fromNullable(preset); this.bitrate = (bitrate < 0) ? Optional.absent() : Optional.of(bitrate); diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/OggVorbisDecoder.java b/src/main/java/net/pterodactylus/sonitus/data/filter/OggVorbisDecoder.java index d98abe1..b142fdd 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/OggVorbisDecoder.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/OggVorbisDecoder.java @@ -25,7 +25,6 @@ import java.io.IOException; import net.pterodactylus.sonitus.data.Metadata; import com.google.common.collect.ImmutableList; -import com.google.common.eventbus.EventBus; /** * Ogg Vorbis decoder that uses {@code oggdec} (from the {@code vorbis-tools} @@ -44,13 +43,11 @@ public class OggVorbisDecoder extends ExternalFilter { /** * Creates a new Ogg Vorbis decoder. * - * @param eventBus - * The event bus * @param binary * The location of the binary */ - public OggVorbisDecoder(EventBus eventBus, String binary) { - super(eventBus, "Ogg Vorbis Decoder"); + public OggVorbisDecoder(String binary) { + super("Ogg Vorbis Decoder"); this.binary = binary; } diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/PredicateFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/PredicateFilter.java index 4edc661..4000a5b 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/PredicateFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/PredicateFilter.java @@ -26,7 +26,6 @@ import net.pterodactylus.sonitus.data.Filter; import net.pterodactylus.sonitus.data.Metadata; import com.google.common.base.Predicate; -import com.google.common.eventbus.EventBus; /** * {@link Filter} implementation that uses a {@link Predicate} to determine @@ -48,15 +47,12 @@ public class PredicateFilter extends DummyFilter { /** * Creates a new predicate filter. * - * @param eventBus - * The event bus * @param metadataPredicate * The predicate to evaluate every time the metadata changes * @param filter - * The filter to use if the predicate matches the metadata */ - public PredicateFilter(EventBus eventBus, Predicate metadataPredicate, Filter filter) { - super(eventBus, String.format("%s (maybe)", filter.name())); + public PredicateFilter(Predicate metadataPredicate, Filter filter) { + super(String.format("%s (maybe)", filter.name())); this.metadataPredicate = metadataPredicate; this.filter = filter; } @@ -106,15 +102,6 @@ public class PredicateFilter extends DummyFilter { } @Override - public Metadata metadata() { - if (metadataMatches.get()) { - return filter.metadata(); - } else { - return super.metadata(); - } - } - - @Override public byte[] get(int bufferSize) throws IOException { if (metadataMatches.get()) { return filter.get(bufferSize); diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/RateLimitingFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/RateLimitingFilter.java index d67604b..b2890ca 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/RateLimitingFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/RateLimitingFilter.java @@ -22,8 +22,6 @@ import java.util.logging.Logger; import net.pterodactylus.sonitus.data.Metadata; -import com.google.common.eventbus.EventBus; - /** * Rate limiting filter that only passes a specified amount of data per second * from its {@link net.pterodactylus.sonitus.data.Source} to its {@link @@ -48,32 +46,26 @@ public class RateLimitingFilter extends DummyFilter { /** * Creates a new rate limiting filter. * - * @param eventBus - * The event bus * @param name * The name of the filter * @param rate - * The limiting rate (in bytes/second) */ - public RateLimitingFilter(EventBus eventBus, String name, int rate) { - this(eventBus, name, rate, 0); + public RateLimitingFilter(String name, int rate) { + this(name, rate, 0); } /** * Creates a new rate limiting filter. * - * @param eventBus - * The event bus * @param name * The name of the filter * @param rate * The limiting rate (in bytes/second) * @param fastStartTime * The amount of time at the start of the filtering during which no delay - * will occur (in milliseconds) */ - public RateLimitingFilter(EventBus eventBus, String name, int rate, long fastStartTime) { - super(eventBus, name); + public RateLimitingFilter(String name, int rate, long fastStartTime) { + super(name); this.rate = rate; this.counter = (long) (-rate * (fastStartTime / 1000.0)); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/SoxResampleFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/SoxResampleFilter.java index ecbe1cb..2415bbb 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/SoxResampleFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/SoxResampleFilter.java @@ -25,7 +25,6 @@ import java.io.IOException; import net.pterodactylus.sonitus.data.Metadata; import com.google.common.collect.ImmutableList; -import com.google.common.eventbus.EventBus; /** * {@link net.pterodactylus.sonitus.data.Filter} implementation that uses {@code @@ -44,15 +43,12 @@ public class SoxResampleFilter extends ExternalFilter { /** * Creates a new resample filter. * - * @param eventBus - * The event bus * @param binary * The location of the binary * @param rate - * The new sampling rate */ - public SoxResampleFilter(EventBus eventBus, String binary, int rate) { - super(eventBus, String.format("Resample to %s kHz", rate / 1000.0)); + public SoxResampleFilter(String binary, int rate) { + super(String.format("Resample to %s kHz", rate / 1000.0)); this.binary = binary; this.rate = rate; } diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/StereoSeparationFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/StereoSeparationFilter.java index def3f3d..3993e11 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/StereoSeparationFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/StereoSeparationFilter.java @@ -24,8 +24,6 @@ import net.pterodactylus.sonitus.data.Controller; import net.pterodactylus.sonitus.data.Filter; import net.pterodactylus.sonitus.data.controller.Knob; -import com.google.common.eventbus.EventBus; - /** * {@link Filter} implementation that can reduce the stereo width of a signal, * or even reverse the channels. @@ -37,14 +35,9 @@ public class StereoSeparationFilter extends AudioProcessingFilter { /** The separation knob. */ private final Knob separationKnob; - /** - * Creates a new stereo separation filter. - * - * @param eventBus - * The event bus - */ - public StereoSeparationFilter(EventBus eventBus) { - super(eventBus, "Stereo Separation"); + /** Creates a new stereo separation filter. */ + public StereoSeparationFilter() { + super("Stereo Separation"); separationKnob = new Knob("Separation", 1.0); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/TimeCounterFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/TimeCounterFilter.java index 1864b1c..a528200 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/TimeCounterFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/TimeCounterFilter.java @@ -23,8 +23,6 @@ import java.util.concurrent.atomic.AtomicLong; import net.pterodactylus.sonitus.data.Filter; import net.pterodactylus.sonitus.data.Metadata; -import com.google.common.eventbus.EventBus; - /** * {@link Filter} implementation that uses the number of bytes that have been * {@link #process(byte[]) processed} together with the {@link Metadata} to @@ -44,28 +42,24 @@ public class TimeCounterFilter extends DummyFilter { * Creates a new time counter filter that automatically resets the counter when * the metadata is {@link #metadataUpdated(Metadata) updated}. * - * @param eventBus - * The event bus * @param name * The name of the filter */ - public TimeCounterFilter(EventBus eventBus, String name) { - this(eventBus, name, true); + public TimeCounterFilter(String name) { + this(name, true); } /** * Creates a new time counter filter. * - * @param eventBus - * The event bus * @param name * The name of the filter * @param resetOnMetadataUpdate * {@code true} if the counter should automatically be reset if the metadata * is updated, {@code false} otherwise */ - public TimeCounterFilter(EventBus eventBus, String name, boolean resetOnMetadataUpdate) { - super(eventBus, name); + public TimeCounterFilter(String name, boolean resetOnMetadataUpdate) { + super(name); this.resetOnMetadataUpdate = resetOnMetadataUpdate; } diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/VolumeFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/VolumeFilter.java index 702594a..02570db 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/VolumeFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/VolumeFilter.java @@ -25,8 +25,6 @@ import net.pterodactylus.sonitus.data.Filter; import net.pterodactylus.sonitus.data.controller.Fader; import net.pterodactylus.sonitus.data.controller.Switch; -import com.google.common.eventbus.EventBus; - /** * Internal {@link Filter} implementation that can reduce the volume of the * signal. @@ -41,14 +39,9 @@ public class VolumeFilter extends AudioProcessingFilter { /** The mute switch. */ private final Switch muteSwitch; - /** - * Creates a new volume filter. - * - * @param eventBus - * The event bus - */ - public VolumeFilter(EventBus eventBus) { - super(eventBus, "Volume"); + /** Creates a new volume filter. */ + public VolumeFilter() { + super("Volume"); volumeFader = new Fader("Volume", 1.0); muteSwitch = new Switch("Mute", false); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java index 7c28f2c..3409280 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java @@ -41,11 +41,9 @@ import net.pterodactylus.sonitus.data.Sink; import net.pterodactylus.sonitus.data.Source; import net.pterodactylus.sonitus.data.controller.Fader; import net.pterodactylus.sonitus.data.controller.Switch; -import net.pterodactylus.sonitus.data.event.MetadataUpdated; import net.pterodactylus.sonitus.io.IntegralWriteOutputStream; import com.google.common.base.Preconditions; -import com.google.common.eventbus.EventBus; /** * {@link Sink} implementation that uses the JDK’s {@link AudioSystem} to play @@ -58,18 +56,12 @@ public class AudioSink extends AbstractControlledComponent implements Sink { /** The logger. */ private static final Logger logger = Logger.getLogger(AudioSink.class.getName()); - /** The event bus. */ - private final EventBus eventBus; - /** The volume fader. */ private final Fader volumeFader; /** The “mute” switch. */ private final Switch muteSwitch; - /** The current metadata. */ - private Metadata metadata; - /** The audio output. */ private SourceDataLine sourceDataLine; @@ -93,14 +85,9 @@ public class AudioSink extends AbstractControlledComponent implements Sink { } }, 1024); - /** - * Creates a new audio sink. - * - * @param eventBus - * The event bus - */ - public AudioSink(EventBus eventBus) { - this.eventBus = eventBus; + /** Creates a new audio sink. */ + public AudioSink() { + super("Audio Output"); volumeFader = new Fader("Volume") { @Override @@ -152,16 +139,6 @@ public class AudioSink extends AbstractControlledComponent implements Sink { // @Override - public String name() { - return "Audio Output"; - } - - @Override - public Metadata metadata() { - return metadata; - } - - @Override public List> controllers() { return Arrays.>asList(volumeFader, muteSwitch); } @@ -194,10 +171,8 @@ public class AudioSink extends AbstractControlledComponent implements Sink { @Override public void metadataUpdated(Metadata metadata) { - logger.info(String.format("Now playing %s.", metadata)); - this.metadata = metadata; - fireMetadataUpdated(metadata); - eventBus.post(new MetadataUpdated(this, metadata)); + super.metadataUpdated(metadata); + logger.fine(String.format("Now playing %s.", metadata)); } @Override diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java index b3b1ff0..be934f3 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java @@ -27,9 +27,6 @@ import net.pterodactylus.sonitus.data.AbstractControlledComponent; import net.pterodactylus.sonitus.data.Controller; import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Sink; -import net.pterodactylus.sonitus.data.event.MetadataUpdated; - -import com.google.common.eventbus.EventBus; /** * {@link net.pterodactylus.sonitus.data.Sink} that writes all received data @@ -42,28 +39,20 @@ public class FileSink extends AbstractControlledComponent implements Sink { /** The logger. */ private static final Logger logger = Logger.getLogger(FileSink.class.getName()); - /** The event bus. */ - private final EventBus eventBus; - /** The path of the file to write to. */ private final String path; /** The output stream writing to the file. */ private FileOutputStream fileOutputStream; - /** The current metadata. */ - private Metadata metadata; - /** * Creates a new file sink that will write to the given path. * - * @param eventBus - * The event bus * @param path * The path of the file to write to */ - public FileSink(EventBus eventBus, String path) { - this.eventBus = eventBus; + public FileSink(String path) { + super(path); this.path = path; } @@ -72,16 +61,6 @@ public class FileSink extends AbstractControlledComponent implements Sink { // @Override - public String name() { - return path; - } - - @Override - public Metadata metadata() { - return metadata; - } - - @Override public List> controllers() { return Collections.emptyList(); } @@ -106,13 +85,6 @@ public class FileSink extends AbstractControlledComponent implements Sink { } @Override - public void metadataUpdated(Metadata metadata) { - this.metadata = metadata; - fireMetadataUpdated(metadata); - eventBus.post(new MetadataUpdated(this, metadata)); - } - - @Override public void process(byte[] buffer) throws IOException { fileOutputStream.write(buffer); logger.finest(String.format("FileSink: Wrote %d Bytes.", buffer.length)); diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java index 515aece..8e1c154 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java @@ -32,10 +32,8 @@ import net.pterodactylus.sonitus.data.AbstractControlledComponent; import net.pterodactylus.sonitus.data.Controller; import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Sink; -import net.pterodactylus.sonitus.data.event.MetadataUpdated; import net.pterodactylus.sonitus.io.InputStreamDrainer; -import com.google.common.eventbus.EventBus; import com.google.common.io.BaseEncoding; import com.google.common.io.Closeables; @@ -50,9 +48,6 @@ public class Icecast2Sink extends AbstractControlledComponent implements Sink { /** The logger. */ private static final Logger logger = Logger.getLogger(Icecast2Sink.class.getName()); - /** The event bus. */ - private final EventBus eventBus; - /** The server name. */ private final String server; @@ -80,9 +75,6 @@ public class Icecast2Sink extends AbstractControlledComponent implements Sink { /** The output stream to the server. */ private OutputStream socketOutputStream; - /** The current metadata. */ - private Metadata metadata; - /** * Creates a new Icecast2 sink. * @@ -106,8 +98,8 @@ public class Icecast2Sink extends AbstractControlledComponent implements Sink { * {@code true} to publish the server in a public directory, {@code false} to * not publish it */ - public Icecast2Sink(EventBus eventBus, String server, int port, String password, String mountPoint, String serverName, String serverDescription, String genre, boolean publishServer) { - this.eventBus = eventBus; + public Icecast2Sink(String server, int port, String password, String mountPoint, String serverName, String serverDescription, String genre, boolean publishServer) { + super(String.format("icecast://%s:%d/%s", server, port, mountPoint)); this.server = server; this.port = port; this.password = password; @@ -123,16 +115,6 @@ public class Icecast2Sink extends AbstractControlledComponent implements Sink { // @Override - public String name() { - return String.format("icecast://%s:%d/%s", server, port, mountPoint); - } - - @Override - public Metadata metadata() { - return metadata; - } - - @Override public List> controllers() { return Collections.emptyList(); } @@ -175,7 +157,7 @@ public class Icecast2Sink extends AbstractControlledComponent implements Sink { @Override public void metadataUpdated(final Metadata metadata) { - this.metadata = metadata; + super.metadataUpdated(metadata); new Thread(new Runnable() { @Override @@ -210,8 +192,6 @@ public class Icecast2Sink extends AbstractControlledComponent implements Sink { } } }).start(); - fireMetadataUpdated(metadata); - eventBus.post(new MetadataUpdated(this, metadata)); } @Override diff --git a/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java b/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java index 2829a42..9aff8d4 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java +++ b/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java @@ -46,9 +46,6 @@ public class FileSource extends AbstractControlledComponent implements Source { /** The path of the file. */ private final String path; - /** The identified metadata of the file. */ - private final Metadata metadata; - /** The input stream. */ private InputStream fileInputStream; @@ -61,16 +58,17 @@ public class FileSource extends AbstractControlledComponent implements Source { * if the file can not be found, or an I/O error occurs */ public FileSource(String path) throws IOException { + super(path); this.path = checkNotNull(path, "path must not be null"); fileInputStream = new FileInputStream(path); /* identify file type. */ Optional identifyingInputStream = IdentifyingInputStream.create(new FileInputStream(path)); if (identifyingInputStream.isPresent()) { - metadata = identifyingInputStream.get().metadata(); + metadataUpdated(identifyingInputStream.get().metadata()); } else { /* fallback. */ - metadata = new Metadata().name(path); + metadataUpdated(new Metadata().name(path)); } } @@ -79,11 +77,6 @@ public class FileSource extends AbstractControlledComponent implements Source { // @Override - public String name() { - return path; - } - - @Override public List> controllers() { return Collections.emptyList(); } @@ -102,18 +95,13 @@ public class FileSource extends AbstractControlledComponent implements Source { return Arrays.copyOf(buffer, read); } - @Override - public Metadata metadata() { - return metadata; - } - // // OBJECT METHODS // @Override public String toString() { - return String.format("%s (%s)", path, metadata); + return String.format("%s (%s)", path, metadata()); } } diff --git a/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java b/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java index 1d8f289..2123986 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java +++ b/src/main/java/net/pterodactylus/sonitus/data/source/MultiSource.java @@ -28,9 +28,7 @@ import java.util.logging.Logger; import net.pterodactylus.sonitus.data.AbstractControlledComponent; import net.pterodactylus.sonitus.data.Controller; -import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Source; -import net.pterodactylus.sonitus.data.event.MetadataUpdated; import net.pterodactylus.sonitus.data.event.SourceFinishedEvent; import com.google.common.eventbus.EventBus; @@ -59,12 +57,10 @@ public class MultiSource extends AbstractControlledComponent implements Source { /** * Creates a new multi source. - * - * @param eventBus - * The event bus */ @Inject public MultiSource(EventBus eventBus) { + super("Multisource"); this.eventBus = eventBus; } @@ -87,8 +83,7 @@ public class MultiSource extends AbstractControlledComponent implements Source { sourceChanged = true; this.source.notifyAll(); } - fireMetadataUpdated(source.metadata()); - eventBus.post(new MetadataUpdated(this, source.metadata())); + metadataUpdated(source.metadata()); logger.info(String.format("Next Source set: %s", source)); } } @@ -98,11 +93,6 @@ public class MultiSource extends AbstractControlledComponent implements Source { // @Override - public String name() { - return "Multisource"; - } - - @Override public List> controllers() { return Collections.emptyList(); } @@ -112,11 +102,6 @@ public class MultiSource extends AbstractControlledComponent implements Source { // @Override - public Metadata metadata() { - return source.get().metadata(); - } - - @Override public byte[] get(int bufferSize) throws EOFException, IOException { while (true) { try { 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 4cb47e1..a55654f 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java +++ b/src/main/java/net/pterodactylus/sonitus/data/source/StreamSource.java @@ -32,12 +32,10 @@ import net.pterodactylus.sonitus.data.Controller; 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; /** @@ -50,9 +48,6 @@ import com.google.common.primitives.Ints; */ public class StreamSource extends AbstractControlledComponent implements Source { - /** The event bus. */ - private final EventBus eventBus; - /** The URL of the stream. */ private final String streamUrl; @@ -62,9 +57,6 @@ public class StreamSource extends AbstractControlledComponent 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 @@ -77,8 +69,8 @@ public class StreamSource extends AbstractControlledComponent 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); @@ -124,7 +116,7 @@ public class StreamSource extends AbstractControlledComponent 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"); } @@ -151,12 +143,10 @@ public class StreamSource extends AbstractControlledComponent implements Source public Metadata metadata() { Optional streamMetadata = metadataStream.getContentMetadata(); if (!streamMetadata.isPresent()) { - return metadata; + return super.metadata(); } - metadata = metadata.title(streamMetadata.get().title()); - fireMetadataUpdated(metadata); - eventBus.post(new MetadataUpdated(this, metadata)); - return metadata; + metadataUpdated(super.metadata().title(streamMetadata.get().title())); + return super.metadata(); } @Override @@ -172,7 +162,7 @@ public class StreamSource extends AbstractControlledComponent implements Source @Override public String toString() { - return String.format("StreamSource(%s,%s)", streamUrl, metadata); + return String.format("StreamSource(%s,%s)", streamUrl, metadata()); } }