jukebox code quality and unit tests

This commit is contained in:
Rémi Cocula
2018-12-22 13:59:44 +01:00
parent 77ca475fbe
commit ac453883fc
8 changed files with 413 additions and 94 deletions
@@ -28,15 +28,10 @@ public class JukeboxJavaService {
private static final float DEFAULT_GAIN = 0.75f;
@Autowired
private AudioScrobblerService audioScrobblerService;
@Autowired
private StatusService statusService;
@Autowired
private SecurityService securityService;
@Autowired
private MediaFileService mediaFileService;
@Autowired
private JavaPlayerFactory javaPlayerFactory;
@@ -45,6 +40,19 @@ public class JukeboxJavaService {
private Map<String, List<com.github.biconou.AudioPlayer.api.Player>> activeAudioPlayersPerMixer = new Hashtable<>();
private final static String DEFAULT_MIXER_ENTRY_KEY = "_default";
public JukeboxJavaService(AudioScrobblerService audioScrobblerService,
StatusService statusService,
SecurityService securityService,
MediaFileService mediaFileService,
JavaPlayerFactory javaPlayerFactory) {
this.audioScrobblerService = audioScrobblerService;
this.statusService = statusService;
this.securityService = securityService;
this.mediaFileService = mediaFileService;
this.javaPlayerFactory = javaPlayerFactory;
}
/**
* Finds the corresponding active audio player for a given airsonic player.
* If no player exists we create one.
@@ -58,9 +66,6 @@ public class JukeboxJavaService {
if (foundPlayer == null) {
synchronized (activeAudioPlayers) {
com.github.biconou.AudioPlayer.api.Player newPlayer = initAudioPlayer(airsonicPlayer);
if (newPlayer == null) {
throw new RuntimeException("Did not initialized a player");
}
activeAudioPlayers.put(airsonicPlayer.getId(), newPlayer);
String mixer = airsonicPlayer.getJavaJukeboxMixer();
if (StringUtils.isBlank(mixer)) {
@@ -96,8 +101,8 @@ public class JukeboxJavaService {
log.info("use default mixer");
audioPlayer = javaPlayerFactory.createJavaPlayer();
}
audioPlayer.setGain(DEFAULT_GAIN);
if (audioPlayer != null) {
audioPlayer.setGain(DEFAULT_GAIN);
audioPlayer.registerListener(new PlayerListener() {
@Override
public void onBegin(int index, File currentFile) {
@@ -123,7 +128,6 @@ public class JukeboxJavaService {
public void onPause() {
// Nothing to do here
}
});
log.info("New audio player {} has been initialized.", audioPlayer.toString());
} else {
@@ -276,7 +280,7 @@ public class JukeboxJavaService {
play(airsonicPlayer);
}
public void stop(Player airsonicPlayer) throws Exception {
public void stop(Player airsonicPlayer) {
log.debug("begin stop jukebox : player = id:{};name:{}", airsonicPlayer.getId(), airsonicPlayer.getName());
com.github.biconou.AudioPlayer.api.Player audioPlayer = retrieveAudioPlayerForAirsonicPlayer(airsonicPlayer);
@@ -320,21 +324,4 @@ public class JukeboxJavaService {
}
}
}
public void setAudioScrobblerService(AudioScrobblerService audioScrobblerService) {
this.audioScrobblerService = audioScrobblerService;
}
public void setStatusService(StatusService statusService) {
this.statusService = statusService;
}
public void setSecurityService(SecurityService securityService) {
this.securityService = securityService;
}
public void setMediaFileService(MediaFileService mediaFileService) {
this.mediaFileService = mediaFileService;
}
}
@@ -19,29 +19,26 @@
*/
package org.airsonic.player.service;
import org.airsonic.player.domain.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.airsonic.player.domain.Player;
import org.airsonic.player.domain.PlayerTechnology;
import org.springframework.stereotype.Service;
/**
*
*
* @author R?mi Cocula
* @author Rémi Cocula
*/
@Service
public class JukeboxService {
private static final Logger log = LoggerFactory.getLogger(JukeboxService.class);
@Autowired
private JukeboxLegacySubsonicService jukeboxLegacySubsonicService;
@Autowired
private JukeboxJavaService jukeboxJavaService;
public JukeboxService(JukeboxLegacySubsonicService jukeboxLegacySubsonicService,
JukeboxJavaService jukeboxJavaService) {
this.jukeboxLegacySubsonicService = jukeboxLegacySubsonicService;
this.jukeboxJavaService = jukeboxJavaService;
}
public void setGain(Player airsonicPlayer,float gain) {
public void setGain(Player airsonicPlayer, float gain) {
switch (airsonicPlayer.getTechnology()) {
case JUKEBOX:
jukeboxLegacySubsonicService.setGain(gain);
@@ -74,10 +71,6 @@ public class JukeboxService {
/**
* This method should be removed when the jukebox is controlled only through rest api.
*
* @param airsonicPlayer
* @param offset
* @throws Exception
*/
@Deprecated
public void updateJukebox(Player airsonicPlayer, int offset) throws Exception {
@@ -96,32 +89,8 @@ public class JukeboxService {
return 0;
}
/**
* This method is only here due to legacy considerations and should be removed
* if the jukeboxLegacySubsonicService is removed.
* @param airsonicPlayer
* @return
*/
@Deprecated
public boolean canControl(Player airsonicPlayer) {
switch (airsonicPlayer.getTechnology()) {
case JUKEBOX:
if (jukeboxLegacySubsonicService.getPlayer() == null) {
return false;
} else {
return jukeboxLegacySubsonicService.getPlayer().getId().equals(airsonicPlayer.getId());
}
case JAVA_JUKEBOX:
return true;
}
return false;
}
/**
* Plays the playQueue of a jukebox player starting at the first item of the queue.
*
* @param airsonicPlayer
* @throws Exception
*/
public void play(Player airsonicPlayer) throws Exception {
switch (airsonicPlayer.getTechnology()) {
@@ -134,7 +103,6 @@ public class JukeboxService {
}
}
public void start(Player airsonicPlayer) throws Exception {
switch (airsonicPlayer.getTechnology()) {
case JUKEBOX:
@@ -168,15 +136,22 @@ public class JukeboxService {
}
}
/* properties setters */
public void setJukeboxLegacySubsonicService(JukeboxLegacySubsonicService jukeboxLegacySubsonicService) {
this.jukeboxLegacySubsonicService = jukeboxLegacySubsonicService;
/**
* This method is only here due to legacy considerations and should be removed
* if the jukeboxLegacySubsonicService is removed.
*/
@Deprecated
public boolean canControl(Player airsonicPlayer) {
switch (airsonicPlayer.getTechnology()) {
case JUKEBOX:
if (jukeboxLegacySubsonicService.getPlayer() == null) {
return false;
} else {
return jukeboxLegacySubsonicService.getPlayer().getId().equals(airsonicPlayer.getId());
}
case JAVA_JUKEBOX:
return true;
}
return false;
}
public void setJukeboxJavaService(JukeboxJavaService jukeboxJavaService) {
this.jukeboxJavaService = jukeboxJavaService;
}
}
@@ -19,7 +19,6 @@
*/
package org.airsonic.player.service.jukebox;
import org.airsonic.player.service.JukeboxService;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -28,7 +27,6 @@ import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.SourceDataLine;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicReference;
@@ -46,7 +44,7 @@ import static org.airsonic.player.service.jukebox.AudioPlayer.State.*;
public class AudioPlayer {
public static final float DEFAULT_GAIN = 0.75f;
private static final Logger LOG = LoggerFactory.getLogger(JukeboxService.class);
private static final Logger LOG = LoggerFactory.getLogger(AudioPlayer.class);
private final InputStream in;
private final Listener listener;