Skip to content

Remove a dependency cycle involving StreamPlayer, StreamPlayerEvent and StreamPlayerEventLauncher. #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

package com.goxr3plus.streamplayer.stream;

import com.goxr3plus.streamplayer.enums.Status;
import com.goxr3plus.streamplayer.stream.StreamPlayerException.PlayerException;
import javazoom.spi.PropertiesContainer;
import org.tritonus.share.sampled.TAudioFormat;
import org.tritonus.share.sampled.file.TAudioFileFormat;

import javax.naming.OperationNotSupportedException;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -31,28 +39,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.OperationNotSupportedException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.BooleanControl;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

import org.tritonus.share.sampled.TAudioFormat;
import org.tritonus.share.sampled.file.TAudioFileFormat;

import com.goxr3plus.streamplayer.enums.Status;
import com.goxr3plus.streamplayer.stream.StreamPlayerException.PlayerException;

import javazoom.spi.PropertiesContainer;

/**
* StreamPlayer is a class based on JavaSound API. It has been successfully tested under Java 10
*
Expand Down Expand Up @@ -223,7 +209,7 @@ public void reset() {
private String generateEvent(final Status status, final int encodedStreamPosition, final Object description) {
try {
return eventsExecutorService
.submit(new StreamPlayerEventLauncher(this, status, encodedStreamPosition, description, listeners))
.submit(new StreamPlayerEventLauncher(this, status, encodedStreamPosition, description, listeners, logger))
.get();
} catch (InterruptedException | ExecutionException ex) {
logger.log(Level.WARNING, "Problem in StreamPlayer generateEvent() method", ex);
Expand Down Expand Up @@ -1412,10 +1398,6 @@ public boolean isSeeking() {
return status == Status.SEEKING;
}

Logger getLogger() {
return logger;
}

@Override
public SourceDataLine getSourceDataLine() {
return outlet.getSourceDataLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,21 @@ public class StreamPlayerEvent {
/** The stream position. */
private int encodedStreamPosition = -1;

/** The source. */
private StreamPlayer source = null;

/** The description. */
private Object description = null;

/**
* Constructor.
*
* @param source
* the source
* @param status
* the status
* @param encodededStreamPosition
* the stream position
* @param description
* the description
*/
public StreamPlayerEvent(StreamPlayer source, Status status, int encodededStreamPosition, Object description) {
this.source = source;
this.playerStatus = status;
public StreamPlayerEvent(Status status, int encodededStreamPosition, Object description) {
this.playerStatus = status;
this.encodedStreamPosition = encodededStreamPosition;
this.description = description;
}
Expand Down Expand Up @@ -91,18 +85,9 @@ public Object getDescription() {
return description;
}

/**
* Gets the source.
*
* @return the source
*/
public Object getSource() {
return source;
}

@Override
public String toString() {
return "Source :=" + source + " , Player Status := " + playerStatus + " , EncodedStreamPosition :="
return "Player Status := " + playerStatus + " , EncodedStreamPosition :="
+ encodedStreamPosition + " , Description :=" + description;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
*/
package com.goxr3plus.streamplayer.stream;

import com.goxr3plus.streamplayer.enums.Status;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.goxr3plus.streamplayer.enums.Status;

/**
* The Class StreamPlayerEventLauncher.
*
Expand All @@ -50,37 +50,34 @@ public class StreamPlayerEventLauncher implements Callable<String> {
private List<StreamPlayerListener> listeners = null;

/** The source. */
private StreamPlayer source = null;
private StreamPlayerInterface source = null;

/**
* Instantiates a new stream player event launcher.
*
* @param source
* the source
* @param playerStatus
* the play state
* @param encodedStreamPosition
* the stream position
* @param description
* the description
* @param listeners
*
* @param source the source
* @param playerStatus the play state
* @param encodedStreamPosition the stream position
* @param description the description
* @param listeners will be called when events happens
* @param logger Logger to use for logging
*/
public StreamPlayerEventLauncher(StreamPlayer source, Status playerStatus, int encodedStreamPosition, Object description,
List<StreamPlayerListener> listeners) {
public StreamPlayerEventLauncher(StreamPlayerInterface source, Status playerStatus, int encodedStreamPosition, Object description,
List<StreamPlayerListener> listeners, Logger logger) {
this.source = source;
this.playerState = playerStatus;
this.encodedStreamPosition = encodedStreamPosition;
this.description = description;
this.listeners = listeners;
this.logger = source.getLogger();
this.logger = logger;
}

@Override
public String call() {
// Notify all the listeners that the state has been updated
if (listeners != null) {
listeners.forEach(listener -> listener
.statusUpdated(new StreamPlayerEvent(source, playerState, encodedStreamPosition, description)));
.statusUpdated(new StreamPlayerEvent(playerState, encodedStreamPosition, description)));
}
logger.log(Level.INFO, "Stream player Status -> " + playerState);
return "OK";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@
import com.goxr3plus.streamplayer.enums.Status;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.configuration.IMockitoConfiguration;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;

class StreamPlayerEventTest {

private StreamPlayer source;
private Object description;
private Status status;
private int encodededStreamPosition;
private int encodedStreamPosition;
private StreamPlayerEvent event;

@BeforeEach
void setUp() {
description = new Object();
source = mock(StreamPlayer.class);
status = Status.RESUMED;
encodededStreamPosition = 12345;
event = new StreamPlayerEvent(source, status, encodededStreamPosition, description);
encodedStreamPosition = 12345;
event = new StreamPlayerEvent(status, encodedStreamPosition, description);
}

@Test
Expand All @@ -32,12 +29,7 @@ void itReturnsTheStatus() {

@Test
void itReturnsTheEncodedStreamPosition() {
assertEquals(encodededStreamPosition, event.getEncodedStreamPosition());
}

@Test
void itReturnsTheSource() {
assertSame(source, event.getSource());
assertEquals(encodedStreamPosition, event.getEncodedStreamPosition());
}

@Test
Expand All @@ -48,12 +40,8 @@ void itReturnsTheDescription() {
@Test
void itReturnsAString() {
final String actual = event.toString();
final String expected = "Source :="
+ source.toString()
+ " , Player Status := RESUMED , EncodedStreamPosition :=12345 , Description :="
final String expected = "Player Status := RESUMED , EncodedStreamPosition :=12345 , Description :="
+ description.toString();
assertEquals(expected, actual);
}


}