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;
28 import java.util.logging.Logger;
30 import net.pterodactylus.sonitus.data.AbstractControlledComponent;
31 import net.pterodactylus.sonitus.data.ContentMetadata;
32 import net.pterodactylus.sonitus.data.Controller;
33 import net.pterodactylus.sonitus.data.FormatMetadata;
34 import net.pterodactylus.sonitus.data.Metadata;
35 import net.pterodactylus.sonitus.data.Source;
36 import net.pterodactylus.sonitus.io.MetadataStream;
38 import com.google.common.base.Optional;
39 import com.google.common.collect.Maps;
40 import com.google.common.primitives.Ints;
43 * {@link Source} implementation that can download an audio stream from a
46 * Currently only “audio/mpeg” (aka MP3) streams are supported.
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 public class StreamSource extends AbstractControlledComponent implements Source {
53 private static final Logger logger = Logger.getLogger(StreamSource.class.getName());
55 /** The URL of the stream. */
56 private final String streamUrl;
58 /** The name of the station. */
59 private final String streamName;
61 /** The metadata stream. */
62 private final MetadataStream metadataStream;
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
72 * The URL of the stream
74 * if an I/O error occurs
76 public StreamSource(String streamUrl) throws IOException {
78 this.streamUrl = streamUrl;
79 URL url = new URL(streamUrl);
81 /* set up connection. */
82 URLConnection urlConnection = url.openConnection();
83 if (!(urlConnection instanceof HttpURLConnection)) {
84 throw new IllegalArgumentException("Not an HTTP URL!");
86 HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
87 httpUrlConnection.setRequestProperty("ICY-Metadata", "1");
90 logger.info(String.format("Connecting to %s...", streamUrl));
91 httpUrlConnection.connect();
93 /* check content type. */
94 String contentType = httpUrlConnection.getContentType();
95 if (!contentType.startsWith("audio/mpeg")) {
96 throw new IllegalArgumentException("Not an MP3 stream!");
99 /* get ice-audio-info header. */
100 String iceAudioInfo = httpUrlConnection.getHeaderField("ICE-Audio-Info");
101 if (iceAudioInfo == null) {
102 throw new IllegalArgumentException("No ICE Audio Info!");
105 /* parse ice-audio-info header. */
106 String[] audioInfos = iceAudioInfo.split(";");
107 Map<String, Integer> audioParameters = Maps.newHashMap();
108 for (String audioInfo : audioInfos) {
109 String key = audioInfo.substring(0, audioInfo.indexOf('=')).toLowerCase();
110 int value = Ints.tryParse(audioInfo.substring(audioInfo.indexOf('=') + 1));
111 audioParameters.put(key, value);
114 /* check metadata interval. */
115 String metadataIntervalHeader = httpUrlConnection.getHeaderField("ICY-MetaInt");
116 if (metadataIntervalHeader == null) {
117 throw new IllegalArgumentException("No Metadata Interval header!");
119 Integer metadataInterval = Ints.tryParse(metadataIntervalHeader);
120 if (metadataInterval == null) {
121 throw new IllegalArgumentException(String.format("Invalid Metadata Interval header: %s", metadataIntervalHeader));
124 metadataUpdated(new Metadata(new FormatMetadata(audioParameters.get("ice-channels"), audioParameters.get("ice-samplerate"), "MP3"), new ContentMetadata()));
125 metadataStream = new MetadataStream(new BufferedInputStream(httpUrlConnection.getInputStream()), metadataInterval);
126 streamName = httpUrlConnection.getHeaderField("ICY-Name");
130 // CONTROLLED METHODS
134 public String name() {
139 public List<Controller<?>> controllers() {
140 return Collections.emptyList();
148 public Metadata metadata() {
149 Optional<ContentMetadata> streamMetadata = metadataStream.getContentMetadata();
150 if (!streamMetadata.isPresent()) {
151 return super.metadata();
153 metadataUpdated(super.metadata().title(streamMetadata.get().title()));
154 return super.metadata();
158 public byte[] get(int bufferSize) throws IOException {
159 byte[] buffer = new byte[bufferSize];
160 metadataStream.read(buffer);
169 public String toString() {
170 return String.format("StreamSource(%s,%s)", streamUrl, metadata());