Transport metadata inline.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / AbstractFilter.java
1 /*
2  * Sonitus - AbstractFilter.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sonitus.data;
19
20 import java.io.EOFException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.PipedInputStream;
25 import java.io.PipedOutputStream;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.concurrent.atomic.AtomicReference;
30
31 import com.google.common.collect.Lists;
32 import com.google.common.io.Closeables;
33
34 /**
35  * Abstract {@link Filter} implementation that takes care of managing {@link
36  * MetadataListener}s and pipes its input to its output.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public abstract class AbstractFilter implements Filter {
41
42         /** The name of this filter. */
43         private final String name;
44
45         /** The list of metadata listeners. */
46         private final List<MetadataListener> metadataListeners = Lists.newCopyOnWriteArrayList();
47
48         /** The current metadata. */
49         private final AtomicReference<Metadata> metadata = new AtomicReference<Metadata>();
50
51         /** The input stream from which to read. */
52         private InputStream inputStream;
53
54         /** The output stream to which to write. */
55         private OutputStream outputStream;
56
57         /**
58          * Creates a new abstract filter.
59          *
60          * @param name
61          *              The name of the filter
62          */
63         protected AbstractFilter(String name) {
64                 this.name = name;
65         }
66
67         //
68         // LISTENER MANAGEMENT
69         //
70
71         @Override
72         public void addMetadataListener(MetadataListener metadataListener) {
73                 metadataListeners.add(metadataListener);
74         }
75
76         @Override
77         public void removeMetadataListener(MetadataListener metadataListener) {
78                 metadataListeners.remove(metadataListener);
79         }
80
81         //
82         // FILTER METHODS
83         //
84
85         @Override
86         public String name() {
87                 return name;
88         }
89
90         @Override
91         public List<Controller<?>> controllers() {
92                 return Collections.emptyList();
93         }
94
95         @Override
96         public Metadata metadata() {
97                 return metadata.get();
98         }
99
100         @Override
101         public void metadataUpdated(Metadata metadata) {
102                 if (metadata.equals(this.metadata.get())) {
103                         return;
104                 }
105                 this.metadata.set(metadata);
106                 fireMetadataUpdated(metadata);
107         }
108
109         @Override
110         public void open(Metadata metadata) throws IOException {
111                 metadataUpdated(metadata);
112                 inputStream = createInputStream();
113                 outputStream = createOutputStream();
114         }
115
116         @Override
117         public void close() {
118                 try {
119                         Closeables.close(outputStream, true);
120                         Closeables.close(inputStream, true);
121                 } catch (IOException e) {
122                         /* won’t throw. */
123                 }
124         }
125
126         @Override
127         public void process(DataPacket dataPacket) throws IOException {
128                 if (dataPacket.metadata().isPresent() && !dataPacket.metadata().get().equalsIgnoreComment(this.metadata.get())) {
129                         metadataUpdated(dataPacket.metadata().get());
130                 }
131                 outputStream.write(dataPacket.buffer());
132                 outputStream.flush();
133         }
134
135         @Override
136         public DataPacket get(int bufferSize) throws IOException {
137                 byte[] buffer = new byte[bufferSize];
138                 int read = inputStream.read(buffer);
139                 if (read == -1) {
140                         throw new EOFException();
141                 }
142                 return new DataPacket(metadata(), Arrays.copyOf(buffer, read));
143         }
144
145         //
146         // EVENT METHODS
147         //
148
149         /**
150          * Notifies all registered metadata listeners that the metadata has changed.
151          *
152          * @param metadata
153          *              The new metadata
154          */
155         protected void fireMetadataUpdated(Metadata metadata) {
156                 for (MetadataListener metadataListener : metadataListeners) {
157                         metadataListener.metadataUpdated(this, metadata);
158                 }
159         }
160
161         //
162         // SUBCLASS METHODS
163         //
164
165         /**
166          * Creates the input stream from which {@link net.pterodactylus.sonitus.data.Pipeline}
167          * will read the audio data. If you override this, you have to override {@link
168          * #createOutputStream()}, too!
169          *
170          * @return The input stream to read from
171          * @throws IOException
172          *              if an I/O error occurs
173          */
174         protected InputStream createInputStream() throws IOException {
175                 return new PipedInputStream();
176         }
177
178         /**
179          * Creates the output stream to which {@link net.pterodactylus.sonitus.data.Pipeline}
180          * will write the audio data. If you override this, you have to override {@link
181          * #createInputStream()}, too!
182          *
183          * @return The output stream to write to
184          * @throws IOException
185          *              if an I/O error occurs
186          */
187         protected OutputStream createOutputStream() throws IOException {
188                 return new PipedOutputStream((PipedInputStream) inputStream);
189         }
190
191 }