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