2 * Sonitus - StreamSource.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sonitus.data.source;
20 import java.io.BufferedInputStream;
21 import java.io.IOException;
22 import java.net.HttpURLConnection;
24 import java.net.URLConnection;
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;
33 import com.google.common.base.Optional;
34 import com.google.common.collect.Maps;
35 import com.google.common.primitives.Ints;
38 * {@link Source} implementation that can download an audio stream from a
41 * Currently only “audio/mpeg” (aka MP3) streams are supported.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class StreamSource implements Source {
47 /** The URL of the stream. */
48 private final String streamUrl;
50 /** The metadata stream. */
51 private final MetadataStream metadataStream;
53 /** The current metadata. */
54 private Metadata metadata;
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
62 * The URL of the stream
64 * if an I/O error occurs
66 public StreamSource(String streamUrl) throws IOException {
67 this.streamUrl = streamUrl;
68 URL url = new URL(streamUrl);
70 /* set up connection. */
71 URLConnection urlConnection = url.openConnection();
72 if (!(urlConnection instanceof HttpURLConnection)) {
73 throw new IllegalArgumentException("Not an HTTP URL!");
75 HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
76 httpUrlConnection.setRequestProperty("ICY-Metadata", "1");
79 httpUrlConnection.connect();
81 /* check content type. */
82 String contentType = httpUrlConnection.getContentType();
83 if (!contentType.startsWith("audio/mpeg")) {
84 throw new IllegalArgumentException("Not an MP3 stream!");
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!");
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);
102 /* check metadata interval. */
103 String metadataIntervalHeader = httpUrlConnection.getHeaderField("ICY-MetaInt");
104 if (metadataIntervalHeader == null) {
105 throw new IllegalArgumentException("No Metadata Interval header!");
107 Integer metadataInterval = Ints.tryParse(metadataIntervalHeader);
108 if (metadataInterval == null) {
109 throw new IllegalArgumentException(String.format("Invalid Metadata Interval header: %s", metadataIntervalHeader));
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);
121 public Metadata metadata() {
122 Optional<ContentMetadata> streamMetadata = metadataStream.getContentMetadata();
123 if (!streamMetadata.isPresent()) {
126 return metadata = metadata.title(streamMetadata.get().title());
130 public byte[] get(int bufferSize) throws IOException {
131 byte[] buffer = new byte[bufferSize];
132 metadataStream.read(buffer);
141 public String toString() {
142 return String.format("StreamSource(%s,%s)", streamUrl, metadata);