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.eventbus.EventBus;
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 implements Source {
52 private final EventBus eventBus;
54 /** The URL of the stream. */
55 private final String streamUrl;
57 /** The name of the station. */
58 private final String streamName;
60 /** The metadata stream. */
61 private final MetadataStream metadataStream;
63 /** The current metadata. */
64 private Metadata metadata;
67 * Creates a new stream source. This will also connect to the server and parse
68 * the response header for vital information (sampling frequency, number of
74 * The URL of the stream
76 * if an I/O error occurs
78 public StreamSource(EventBus eventBus, String streamUrl) throws IOException {
79 this.eventBus = eventBus;
80 this.streamUrl = streamUrl;
81 URL url = new URL(streamUrl);
83 /* set up connection. */
84 URLConnection urlConnection = url.openConnection();
85 if (!(urlConnection instanceof HttpURLConnection)) {
86 throw new IllegalArgumentException("Not an HTTP URL!");
88 HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
89 httpUrlConnection.setRequestProperty("ICY-Metadata", "1");
92 httpUrlConnection.connect();
94 /* check content type. */
95 String contentType = httpUrlConnection.getContentType();
96 if (!contentType.startsWith("audio/mpeg")) {
97 throw new IllegalArgumentException("Not an MP3 stream!");
100 /* get ice-audio-info header. */
101 String iceAudioInfo = httpUrlConnection.getHeaderField("ICE-Audio-Info");
102 if (iceAudioInfo == null) {
103 throw new IllegalArgumentException("No ICE Audio Info!");
106 /* parse ice-audio-info header. */
107 String[] audioInfos = iceAudioInfo.split(";");
108 Map<String, Integer> audioParameters = Maps.newHashMap();
109 for (String audioInfo : audioInfos) {
110 String key = audioInfo.substring(0, audioInfo.indexOf('=')).toLowerCase();
111 int value = Ints.tryParse(audioInfo.substring(audioInfo.indexOf('=') + 1));
112 audioParameters.put(key, value);
115 /* check metadata interval. */
116 String metadataIntervalHeader = httpUrlConnection.getHeaderField("ICY-MetaInt");
117 if (metadataIntervalHeader == null) {
118 throw new IllegalArgumentException("No Metadata Interval header!");
120 Integer metadataInterval = Ints.tryParse(metadataIntervalHeader);
121 if (metadataInterval == null) {
122 throw new IllegalArgumentException(String.format("Invalid Metadata Interval header: %s", metadataIntervalHeader));
125 metadata = new Metadata(new FormatMetadata(audioParameters.get("ice-channels"), audioParameters.get("ice-samplerate"), "MP3"), new ContentMetadata());
126 metadataStream = new MetadataStream(new BufferedInputStream(httpUrlConnection.getInputStream()), metadataInterval);
127 streamName = httpUrlConnection.getHeaderField("ICY-Name");
131 // CONTROLLED METHODS
135 public String name() {
140 public List<Controller<?>> controllers() {
141 return Collections.emptyList();
149 public Metadata metadata() {
150 Optional<ContentMetadata> streamMetadata = metadataStream.getContentMetadata();
151 if (!streamMetadata.isPresent()) {
154 metadata = metadata.title(streamMetadata.get().title());
159 public byte[] get(int bufferSize) throws IOException {
160 byte[] buffer = new byte[bufferSize];
161 metadataStream.read(buffer);
170 public String toString() {
171 return String.format("StreamSource(%s,%s)", streamUrl, metadata);