Add internet radio source cache

This helps us avoid loading and parsing the external playlists each time
we try to load an internet radio.
master
François-Xavier Thomas 6 years ago
parent cab3f60a25
commit aeabfa1044
  1. 41
      airsonic-main/src/main/java/org/airsonic/player/ajax/PlayQueueService.java
  2. 11
      airsonic-main/src/main/java/org/airsonic/player/domain/InternetRadioSource.java
  3. 32
      airsonic-main/src/main/java/org/airsonic/player/service/InternetRadioService.java

@ -449,6 +449,7 @@ public class PlayQueueService {
}
private PlayQueueInfo doPlayInternetRadio(HttpServletRequest request, Player player, InternetRadio radio) throws Exception {
internetRadioService.clearInternetRadioSourceCache(radio.getId());
player.getPlayQueue().clear();
player.getPlayQueue().setRandomSearchCriteria(null);
player.getPlayQueue().setInternetRadio(radio);
@ -742,35 +743,31 @@ public class PlayQueueService {
final String radioName = radio.getName();
List<PlayQueueInfo.Entry> entries = new ArrayList<>();
for (String streamUrl : internetRadioService.getStreamUrls(radio)) {
// Fake stream title using the URL
String streamTitle = streamUrl;
String streamAlbum = radioName;
String streamGenre = "Internet Radio";
// Fake entry id so that the source can be selected
for (InternetRadioSource streamSource: internetRadioService.getInternetRadioSources(radio)) {
// Fake entry id so that the source can be selected in the UI
Integer streamId = -(1+entries.size());
Integer streamTrackNumber = entries.size();
Integer streamYear = 0;
String streamUrl = streamSource.getStreamUrl();
entries.add(new PlayQueueInfo.Entry(
streamId, // Entry id
streamTrackNumber, // Track number
streamTitle, // Use URL as stream title
"",
streamAlbum, // Album name
streamGenre,
streamYear,
"",
0,
"",
"",
"",
"",
false,
radioHomepageUrl, // Album URL
streamUrl, // Track title (use radio stream URL for now)
"", // Track artist
radioName, // Album name (use radio name)
"Internet Radio", // Genre
0, // Year
"", // Bit rate
0, // Duration
"", // Duration (as string)
"", // Format
"", // Content Type
"", // File size
false, // Starred
radioHomepageUrl, // Album URL (use radio home page URL)
streamUrl, // Stream URL
streamUrl, // Remote stream URL
null,
null
null, // Cover art URL
null // Remote cover art URL
));
}

@ -0,0 +1,11 @@
package org.airsonic.player.domain;
public class InternetRadioSource {
private String streamUrl;
public InternetRadioSource(String streamUrl) {
this.streamUrl = streamUrl;
}
public String getStreamUrl() { return streamUrl; }
}

@ -2,21 +2,41 @@ package org.airsonic.player.service;
import chameleon.playlist.*;
import org.airsonic.player.domain.InternetRadio;
import org.airsonic.player.domain.InternetRadioSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
@Service
public class InternetRadioService {
private static final Logger LOG = LoggerFactory.getLogger(InternetRadioService.class);
public List<String> getStreamUrls(InternetRadio radio) throws Exception {
private Map<Integer, List<InternetRadioSource>> cachedSources;
public InternetRadioService() {
this.cachedSources = new HashMap<>();
}
public void clearInternetRadioSourceCache() {
cachedSources.clear();
}
public List<InternetRadioSource> getInternetRadioSources(InternetRadio radio) throws Exception {
List<InternetRadioSource> sources;
if (cachedSources.containsKey(radio.getId())) {
sources = cachedSources.get(radio.getId());
} else {
sources = retrieveInternetRadioSources(radio);
cachedSources.put(radio.getId(), sources);
}
return sources;
}
private List<InternetRadioSource> retrieveInternetRadioSources(InternetRadio radio) throws Exception {
// Retrieve radio playlist and parse it
URL playlistUrl = new URL(radio.getStreamUrl());
SpecificPlaylist inputPlaylist = null;
@ -33,7 +53,7 @@ public class InternetRadioService {
}
// Retrieve stream URLs
List<String> entries = new ArrayList<>();
List<InternetRadioSource> entries = new ArrayList<>();
inputPlaylist.toPlaylist().acceptDown(new PlaylistVisitor() {
@Override
public void beginVisitPlaylist(Playlist playlist) throws Exception {
@ -69,7 +89,9 @@ public class InternetRadioService {
public void beginVisitMedia(Media media) throws Exception {
String streamUrl = media.getSource().getURI().toString();
LOG.info("Got source media at {}...", streamUrl);
entries.add(streamUrl);
entries.add(new InternetRadioSource(
streamUrl
));
}
@Override

Loading…
Cancel
Save