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.data.event.MetadataUpdated;
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.eventbus.EventBus;
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 implements Source {
53 private final EventBus eventBus;
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;
64 /** The current metadata. */
65 private Metadata metadata;
68 * Creates a new stream source. This will also connect to the server and parse
69 * the response header for vital information (sampling frequency, number of
75 * The URL of the stream
77 * if an I/O error occurs
79 public StreamSource(EventBus eventBus, String streamUrl) throws IOException {
80 this.eventBus = eventBus;
81 this.streamUrl = streamUrl;
82 URL url = new URL(streamUrl);
84 /* set up connection. */
85 URLConnection urlConnection = url.openConnection();
86 if (!(urlConnection instanceof HttpURLConnection)) {
87 throw new IllegalArgumentException("Not an HTTP URL!");
89 HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
90 httpUrlConnection.setRequestProperty("ICY-Metadata", "1");
93 httpUrlConnection.connect();
95 /* check content type. */
96 String contentType = httpUrlConnection.getContentType();
97 if (!contentType.startsWith("audio/mpeg")) {
98 throw new IllegalArgumentException("Not an MP3 stream!");
101 /* get ice-audio-info header. */
102 String iceAudioInfo = httpUrlConnection.getHeaderField("ICE-Audio-Info");
103 if (iceAudioInfo == null) {
104 throw new IllegalArgumentException("No ICE Audio Info!");
107 /* parse ice-audio-info header. */
108 String[] audioInfos = iceAudioInfo.split(";");
109 Map<String, Integer> audioParameters = Maps.newHashMap();
110 for (String audioInfo : audioInfos) {
111 String key = audioInfo.substring(0, audioInfo.indexOf('=')).toLowerCase();
112 int value = Ints.tryParse(audioInfo.substring(audioInfo.indexOf('=') + 1));
113 audioParameters.put(key, value);
116 /* check metadata interval. */
117 String metadataIntervalHeader = httpUrlConnection.getHeaderField("ICY-MetaInt");
118 if (metadataIntervalHeader == null) {
119 throw new IllegalArgumentException("No Metadata Interval header!");
121 Integer metadataInterval = Ints.tryParse(metadataIntervalHeader);
122 if (metadataInterval == null) {
123 throw new IllegalArgumentException(String.format("Invalid Metadata Interval header: %s", metadataIntervalHeader));
126 metadata = new Metadata(new FormatMetadata(audioParameters.get("ice-channels"), audioParameters.get("ice-samplerate"), "MP3"), new ContentMetadata());
127 metadataStream = new MetadataStream(new BufferedInputStream(httpUrlConnection.getInputStream()), metadataInterval);
128 streamName = httpUrlConnection.getHeaderField("ICY-Name");
132 // CONTROLLED METHODS
136 public String name() {
141 public List<Controller<?>> controllers() {
142 return Collections.emptyList();
150 public Metadata metadata() {
151 Optional<ContentMetadata> streamMetadata = metadataStream.getContentMetadata();
152 if (!streamMetadata.isPresent()) {
155 metadata = metadata.title(streamMetadata.get().title());
156 eventBus.post(new MetadataUpdated(this, metadata));
161 public byte[] get(int bufferSize) throws IOException {
162 byte[] buffer = new byte[bufferSize];
163 metadataStream.read(buffer);
172 public String toString() {
173 return String.format("StreamSource(%s,%s)", streamUrl, metadata);