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