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;
25 import java.util.Collections;
26 import java.util.List;
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;
36 import com.google.common.base.Optional;
37 import com.google.common.collect.Maps;
38 import com.google.common.primitives.Ints;
41 * {@link Source} implementation that can download an audio stream from a
44 * Currently only “audio/mpeg” (aka MP3) streams are supported.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class StreamSource implements Source {
50 /** The URL of the stream. */
51 private final String streamUrl;
53 /** The metadata stream. */
54 private final MetadataStream metadataStream;
56 /** The current metadata. */
57 private Metadata metadata;
60 * Creates a new stream source. This will also connect to the server and parse
61 * the response header for vital information (sampling frequency, number of
65 * The URL of the stream
67 * if an I/O error occurs
69 public StreamSource(String streamUrl) throws IOException {
70 this.streamUrl = streamUrl;
71 URL url = new URL(streamUrl);
73 /* set up connection. */
74 URLConnection urlConnection = url.openConnection();
75 if (!(urlConnection instanceof HttpURLConnection)) {
76 throw new IllegalArgumentException("Not an HTTP URL!");
78 HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
79 httpUrlConnection.setRequestProperty("ICY-Metadata", "1");
82 httpUrlConnection.connect();
84 /* check content type. */
85 String contentType = httpUrlConnection.getContentType();
86 if (!contentType.startsWith("audio/mpeg")) {
87 throw new IllegalArgumentException("Not an MP3 stream!");
90 /* get ice-audio-info header. */
91 String iceAudioInfo = httpUrlConnection.getHeaderField("ICE-Audio-Info");
92 if (iceAudioInfo == null) {
93 throw new IllegalArgumentException("No ICE Audio Info!");
96 /* parse ice-audio-info header. */
97 String[] audioInfos = iceAudioInfo.split(";");
98 Map<String, Integer> audioParameters = Maps.newHashMap();
99 for (String audioInfo : audioInfos) {
100 String key = audioInfo.substring(0, audioInfo.indexOf('=')).toLowerCase();
101 int value = Ints.tryParse(audioInfo.substring(audioInfo.indexOf('=') + 1));
102 audioParameters.put(key, value);
105 /* check metadata interval. */
106 String metadataIntervalHeader = httpUrlConnection.getHeaderField("ICY-MetaInt");
107 if (metadataIntervalHeader == null) {
108 throw new IllegalArgumentException("No Metadata Interval header!");
110 Integer metadataInterval = Ints.tryParse(metadataIntervalHeader);
111 if (metadataInterval == null) {
112 throw new IllegalArgumentException(String.format("Invalid Metadata Interval header: %s", metadataIntervalHeader));
115 metadata = new Metadata(new FormatMetadata(audioParameters.get("ice-channels"), audioParameters.get("ice-samplerate"), "MP3"), new ContentMetadata());
116 metadataStream = new MetadataStream(new BufferedInputStream(httpUrlConnection.getInputStream()), metadataInterval);
120 // CONTROLLED METHODS
124 public List<Controller<?>> controllers() {
125 return Collections.emptyList();
133 public Metadata metadata() {
134 Optional<ContentMetadata> streamMetadata = metadataStream.getContentMetadata();
135 if (!streamMetadata.isPresent()) {
138 return metadata = metadata.title(streamMetadata.get().title());
142 public byte[] get(int bufferSize) throws IOException {
143 byte[] buffer = new byte[bufferSize];
144 metadataStream.read(buffer);
153 public String toString() {
154 return String.format("StreamSource(%s,%s)", streamUrl, metadata);