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 { private PlayQueueInfo doPlayInternetRadio(HttpServletRequest request, Player player, InternetRadio radio) throws Exception {
internetRadioService.clearInternetRadioSourceCache(radio.getId());
player.getPlayQueue().clear(); player.getPlayQueue().clear();
player.getPlayQueue().setRandomSearchCriteria(null); player.getPlayQueue().setRandomSearchCriteria(null);
player.getPlayQueue().setInternetRadio(radio); player.getPlayQueue().setInternetRadio(radio);
@ -742,35 +743,31 @@ public class PlayQueueService {
final String radioName = radio.getName(); final String radioName = radio.getName();
List<PlayQueueInfo.Entry> entries = new ArrayList<>(); List<PlayQueueInfo.Entry> entries = new ArrayList<>();
for (String streamUrl : internetRadioService.getStreamUrls(radio)) { for (InternetRadioSource streamSource: internetRadioService.getInternetRadioSources(radio)) {
// Fake stream title using the URL // Fake entry id so that the source can be selected in the UI
String streamTitle = streamUrl;
String streamAlbum = radioName;
String streamGenre = "Internet Radio";
// Fake entry id so that the source can be selected
Integer streamId = -(1+entries.size()); Integer streamId = -(1+entries.size());
Integer streamTrackNumber = entries.size(); Integer streamTrackNumber = entries.size();
Integer streamYear = 0; String streamUrl = streamSource.getStreamUrl();
entries.add(new PlayQueueInfo.Entry( entries.add(new PlayQueueInfo.Entry(
streamId, // Entry id streamId, // Entry id
streamTrackNumber, // Track number streamTrackNumber, // Track number
streamTitle, // Use URL as stream title streamUrl, // Track title (use radio stream URL for now)
"", "", // Track artist
streamAlbum, // Album name radioName, // Album name (use radio name)
streamGenre, "Internet Radio", // Genre
streamYear, 0, // Year
"", "", // Bit rate
0, 0, // Duration
"", "", // Duration (as string)
"", "", // Format
"", "", // Content Type
"", "", // File size
false, false, // Starred
radioHomepageUrl, // Album URL radioHomepageUrl, // Album URL (use radio home page URL)
streamUrl, // Stream URL streamUrl, // Stream URL
streamUrl, // Remote stream URL streamUrl, // Remote stream URL
null, null, // Cover art URL
null 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 chameleon.playlist.*;
import org.airsonic.player.domain.InternetRadio; import org.airsonic.player.domain.InternetRadio;
import org.airsonic.player.domain.InternetRadioSource;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.*;
import java.util.List;
@Service @Service
public class InternetRadioService { public class InternetRadioService {
private static final Logger LOG = LoggerFactory.getLogger(InternetRadioService.class); 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 // Retrieve radio playlist and parse it
URL playlistUrl = new URL(radio.getStreamUrl()); URL playlistUrl = new URL(radio.getStreamUrl());
SpecificPlaylist inputPlaylist = null; SpecificPlaylist inputPlaylist = null;
@ -33,7 +53,7 @@ public class InternetRadioService {
} }
// Retrieve stream URLs // Retrieve stream URLs
List<String> entries = new ArrayList<>(); List<InternetRadioSource> entries = new ArrayList<>();
inputPlaylist.toPlaylist().acceptDown(new PlaylistVisitor() { inputPlaylist.toPlaylist().acceptDown(new PlaylistVisitor() {
@Override @Override
public void beginVisitPlaylist(Playlist playlist) throws Exception { public void beginVisitPlaylist(Playlist playlist) throws Exception {
@ -69,7 +89,9 @@ public class InternetRadioService {
public void beginVisitMedia(Media media) throws Exception { public void beginVisitMedia(Media media) throws Exception {
String streamUrl = media.getSource().getURI().toString(); String streamUrl = media.getSource().getURI().toString();
LOG.info("Got source media at {}...", streamUrl); LOG.info("Got source media at {}...", streamUrl);
entries.add(streamUrl); entries.add(new InternetRadioSource(
streamUrl
));
} }
@Override @Override

Loading…
Cancel
Save