c3248571c9bda045f9501e9cf0093d5306ad856c
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / source / StreamSource.java
1 /*
2  * Sonitus - StreamSource.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.source;
19
20 import java.io.BufferedInputStream;
21 import java.io.IOException;
22 import java.net.HttpURLConnection;
23 import java.net.URL;
24 import java.net.URLConnection;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.logging.Logger;
29
30 import net.pterodactylus.sonitus.data.AbstractFilter;
31 import net.pterodactylus.sonitus.data.ContentMetadata;
32 import net.pterodactylus.sonitus.data.Controller;
33 import net.pterodactylus.sonitus.data.DataPacket;
34 import net.pterodactylus.sonitus.data.FormatMetadata;
35 import net.pterodactylus.sonitus.data.Metadata;
36 import net.pterodactylus.sonitus.io.MetadataStream;
37
38 import com.google.common.base.Optional;
39 import com.google.common.collect.Maps;
40 import com.google.common.primitives.Ints;
41
42 /**
43  * {@link Source} implementation that can download an audio stream from a
44  * streaming server.
45  * <p/>
46  * Currently only “audio/mpeg” (aka MP3) streams are supported.
47  *
48  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49  */
50 public class StreamSource extends AbstractFilter {
51
52         /** The logger. */
53         private static final Logger logger = Logger.getLogger(StreamSource.class.getName());
54
55         /** The URL of the stream. */
56         private final String streamUrl;
57
58         /** The name of the station. */
59         private final String streamName;
60
61         /** The metadata stream. */
62         private final MetadataStream metadataStream;
63
64         /**
65          * Creates a new stream source. This will also connect to the server and parse
66          * the response header for vital information (sampling frequency, number of
67          * channels, etc.).
68          *
69          * @param streamUrl
70          *              The URL of the stream
71          * @throws IOException
72          *              if an I/O error occurs
73          */
74         public StreamSource(String streamUrl) throws IOException {
75                 super(null);
76                 this.streamUrl = streamUrl;
77                 URL url = new URL(streamUrl);
78
79                 /* set up connection. */
80                 URLConnection urlConnection = url.openConnection();
81                 if (!(urlConnection instanceof HttpURLConnection)) {
82                         throw new IllegalArgumentException("Not an HTTP URL!");
83                 }
84                 HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
85                 httpUrlConnection.setRequestProperty("ICY-Metadata", "1");
86
87                 /* connect. */
88                 logger.info(String.format("Connecting to %s...", streamUrl));
89                 httpUrlConnection.connect();
90
91                 /* check content type. */
92                 String contentType = httpUrlConnection.getContentType();
93                 if (!contentType.startsWith("audio/mpeg")) {
94                         throw new IllegalArgumentException("Not an MP3 stream!");
95                 }
96
97                 /* get ice-audio-info header. */
98                 String iceAudioInfo = httpUrlConnection.getHeaderField("ICE-Audio-Info");
99                 if (iceAudioInfo == null) {
100                         throw new IllegalArgumentException("No ICE Audio Info!");
101                 }
102
103                 /* parse ice-audio-info header. */
104                 String[] audioInfos = iceAudioInfo.split(";");
105                 Map<String, Integer> audioParameters = Maps.newHashMap();
106                 for (String audioInfo : audioInfos) {
107                         String key = audioInfo.substring(0, audioInfo.indexOf('=')).toLowerCase();
108                         int value = Ints.tryParse(audioInfo.substring(audioInfo.indexOf('=') + 1));
109                         audioParameters.put(key, value);
110                 }
111
112                 /* check metadata interval. */
113                 String metadataIntervalHeader = httpUrlConnection.getHeaderField("ICY-MetaInt");
114                 if (metadataIntervalHeader == null) {
115                         throw new IllegalArgumentException("No Metadata Interval header!");
116                 }
117                 Integer metadataInterval = Ints.tryParse(metadataIntervalHeader);
118                 if (metadataInterval == null) {
119                         throw new IllegalArgumentException(String.format("Invalid Metadata Interval header: %s", metadataIntervalHeader));
120                 }
121
122                 metadataUpdated(new Metadata(new FormatMetadata(audioParameters.get("ice-channels"), audioParameters.get("ice-samplerate"), "MP3"), new ContentMetadata()));
123                 metadataStream = new MetadataStream(new BufferedInputStream(httpUrlConnection.getInputStream()), metadataInterval);
124                 streamName = httpUrlConnection.getHeaderField("ICY-Name");
125         }
126
127         //
128         // FILTER METHODS
129         //
130
131         @Override
132         public String name() {
133                 return streamName;
134         }
135
136         @Override
137         public List<Controller<?>> controllers() {
138                 return Collections.emptyList();
139         }
140
141         @Override
142         public Metadata metadata() {
143                 Optional<ContentMetadata> streamMetadata = metadataStream.getContentMetadata();
144                 if (!streamMetadata.isPresent()) {
145                         return super.metadata();
146                 }
147                 metadataUpdated(super.metadata().title(streamMetadata.get().title()));
148                 return super.metadata();
149         }
150
151         @Override
152         public void open(Metadata metadata) throws IOException {
153                 /* ignore metadata when opening. */
154         }
155
156         @Override
157         public DataPacket get(int bufferSize) throws IOException {
158                 byte[] buffer = new byte[bufferSize];
159                 metadataStream.read(buffer);
160                 return new DataPacket(metadata(), buffer);
161         }
162
163         //
164         // OBJECT METHODS
165         //
166
167         @Override
168         public String toString() {
169                 return String.format("StreamSource(%s,%s)", streamUrl, metadata());
170         }
171
172 }