X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Ffilter%2FTimeCounterFilter.java;h=4c58acc6bd893294c6bcea2a0d95e82aa56c4161;hb=3a12209e82233cd79677a0d847321f41b41aa9a5;hp=1864b1c6856917eeb923c527d440cde0cb7c6d08;hpb=c548332c486d812bfce23a9121219bf86ecc5588;p=sonitus.git
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..4c58acc 100644
--- a/src/main/java/net/pterodactylus/sonitus/data/filter/TimeCounterFilter.java
+++ b/src/main/java/net/pterodactylus/sonitus/data/filter/TimeCounterFilter.java
@@ -19,53 +19,56 @@ package net.pterodactylus.sonitus.data.filter;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
+import net.pterodactylus.sonitus.data.AbstractFilter;
+import net.pterodactylus.sonitus.data.DataPacket;
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
+ * {@link #process(DataPacket) processed} together with the {@link Metadata} to
* calculate how long a source is already playing.
*
* @author David âBombeâ Roden
*/
-public class TimeCounterFilter extends DummyFilter {
+public class TimeCounterFilter extends AbstractFilter implements Filter {
/** The byte counter. */
private final AtomicLong counter = new AtomicLong();
+ /** The parentâs metdata. */
+ private final AtomicReference parentMetadata = new AtomicReference();
+
/** Whether to reset the counter on a metadata update. */
private final boolean resetOnMetadataUpdate;
+ /** The last displayed timestamp. */
+ private final AtomicLong lastTimestamp = new AtomicLong(-1);
+
/**
* 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;
}
@@ -75,7 +78,7 @@ public class TimeCounterFilter extends DummyFilter {
/**
* Returns the number of milliseconds worth of data that has been passed into
- * {@link #process(byte[])}. If no metadata has yet been set, {@code 0} is
+ * {@link #process(DataPacket)}. If no metadata has yet been set, {@code 0} is
* returned.
*
* @return The number of milliseconds the current source is already playing
@@ -94,21 +97,36 @@ public class TimeCounterFilter extends DummyFilter {
}
//
- // DUMMYFILTER METHODS
+ // FILTER METHODS
//
@Override
public void metadataUpdated(Metadata metadata) {
- super.metadataUpdated(metadata);
+ parentMetadata.set(metadata);
if (resetOnMetadataUpdate) {
reset();
}
+ updateTimestamp(true);
}
@Override
- public void process(byte[] buffer) throws IOException {
- super.process(buffer);
- counter.getAndAdd(buffer.length);
+ public void process(DataPacket dataPacket) throws IOException {
+ super.process(dataPacket);
+ counter.getAndAdd(dataPacket.buffer().length);
+ updateTimestamp(false);
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ /** Updates the timestamp in the metadata. */
+ private void updateTimestamp(boolean now) {
+ long timestamp = getMillis() / 1000;
+ if (now || (lastTimestamp.get() != timestamp)) {
+ super.metadataUpdated(parentMetadata.get().comment(String.format("%s%02d:%02d", (timestamp >= 3600) ? String.format("%d:", timestamp / 3600) : "" , (timestamp % 3600) / 60, timestamp % 60)));
+ lastTimestamp.set(timestamp);
+ }
}
}