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.AbstractControlledComponent;
30 import net.pterodactylus.sonitus.data.ContentMetadata;
31 import net.pterodactylus.sonitus.data.Controller;
32 import net.pterodactylus.sonitus.data.FormatMetadata;
33 import net.pterodactylus.sonitus.data.Metadata;
34 import net.pterodactylus.sonitus.data.Source;
35 import net.pterodactylus.sonitus.io.MetadataStream;
37 import com.google.common.base.Optional;
38 import com.google.common.collect.Maps;
39 import com.google.common.primitives.Ints;
42 * {@link Source} implementation that can download an audio stream from a
45 * Currently only “audio/mpeg” (aka MP3) streams are supported.
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public class StreamSource extends AbstractControlledComponent implements Source {
51 /** The URL of the stream. */
52 private final String streamUrl;
54 /** The name of the station. */
55 private final String streamName;
57 /** The metadata stream. */
58 private final MetadataStream metadataStream;
61 * Creates a new stream source. This will also connect to the server and parse
62 * the response header for vital information (sampling frequency, number of
68 * The URL of the stream
70 * if an I/O error occurs
72 public StreamSource(String streamUrl) throws IOException {
74 this.streamUrl = streamUrl;
75 URL url = new URL(streamUrl);
77 /* set up connection. */
78 URLConnection urlConnection = url.openConnection();
79 if (!(urlConnection instanceof HttpURLConnection)) {
80 throw new IllegalArgumentException("Not an HTTP URL!");
82 HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
83 httpUrlConnection.setRequestProperty("ICY-Metadata", "1");
86 httpUrlConnection.connect();
88 /* check content type. */
89 String contentType = httpUrlConnection.getContentType();
90 if (!contentType.startsWith("audio/mpeg")) {
91 throw new IllegalArgumentException("Not an MP3 stream!");
94 /* get ice-audio-info header. */
95 String iceAudioInfo = httpUrlConnection.getHeaderField("ICE-Audio-Info");
96 if (iceAudioInfo == null) {
97 throw new IllegalArgumentException("No ICE Audio Info!");
100 /* parse ice-audio-info header. */
101 String[] audioInfos = iceAudioInfo.split(";");
102 Map<String, Integer> audioParameters = Maps.newHashMap();
103 for (String audioInfo : audioInfos) {
104 String key = audioInfo.substring(0, audioInfo.indexOf('=')).toLowerCase();
105 int value = Ints.tryParse(audioInfo.substring(audioInfo.indexOf('=') + 1));
106 audioParameters.put(key, value);
109 /* check metadata interval. */
110 String metadataIntervalHeader = httpUrlConnection.getHeaderField("ICY-MetaInt");
111 if (metadataIntervalHeader == null) {
112 throw new IllegalArgumentException("No Metadata Interval header!");
114 Integer metadataInterval = Ints.tryParse(metadataIntervalHeader);
115 if (metadataInterval == null) {
116 throw new IllegalArgumentException(String.format("Invalid Metadata Interval header: %s", metadataIntervalHeader));
119 metadataUpdated(new Metadata(new FormatMetadata(audioParameters.get("ice-channels"), audioParameters.get("ice-samplerate"), "MP3"), new ContentMetadata()));
120 metadataStream = new MetadataStream(new BufferedInputStream(httpUrlConnection.getInputStream()), metadataInterval);
121 streamName = httpUrlConnection.getHeaderField("ICY-Name");
125 // CONTROLLED METHODS
129 public String name() {
134 public List<Controller<?>> controllers() {
135 return Collections.emptyList();
143 public Metadata metadata() {
144 Optional<ContentMetadata> streamMetadata = metadataStream.getContentMetadata();
145 if (!streamMetadata.isPresent()) {
146 return super.metadata();
148 metadataUpdated(super.metadata().title(streamMetadata.get().title()));
149 return super.metadata();
153 public byte[] get(int bufferSize) throws IOException {
154 byte[] buffer = new byte[bufferSize];
155 metadataStream.read(buffer);
164 public String toString() {
165 return String.format("StreamSource(%s,%s)", streamUrl, metadata());