#30 WIP - icecast 2 broadcaster cleanup

This commit is contained in:
Denny
2016-12-10 10:21:08 -05:00
parent 293dec8a1c
commit 4290d8e403
17 changed files with 129 additions and 125 deletions

View File

@@ -20,9 +20,7 @@ package audio.broadcast.icecast;
import audio.AudioPacket;
import audio.broadcast.AudioBroadcaster;
import audio.broadcast.BroadcastFormat;
import audio.broadcast.BroadcastState;
import audio.broadcast.BroadcastFactory;
import audio.metadata.AudioMetadata;
import controller.ThreadPoolManager;
import org.asynchttpclient.AsyncHandler;
@@ -42,12 +40,8 @@ import org.reactivestreams.Subscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import properties.SystemProperties;
import record.wave.AudioPacketMonoWaveReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -60,22 +54,27 @@ public class IcecastHTTPAudioBroadcaster extends AudioBroadcaster implements Pub
private static final String HTTP_100_CONTINUE = "100-continue";
private static final long RECONNECT_INTERVAL_MILLISECONDS = 15000; //15 seconds
private long mLastConnectionAttempt = 0;
private DefaultAsyncHttpClient mAsyncHttpClient;
private DefaultAsyncHttpClient mDefaultAsyncHttpClient;
private Subscriber<? super ByteBuffer> mSubscriber;
private List<AudioPacket> mPacketsToBroadcast = new ArrayList<>();
private ConvertedAudioStreamSubscription mSubscription;
private AtomicBoolean mConnecting = new AtomicBoolean();
/**
* Creates an Icecast 2.4.x (or newer) compatible broadcaster using HTTP 1.1 protocol
* Creates an Icecast 2 (v2.4.x or newer) compatible broadcaster using HTTP 1.1 protocol
*
* Note: use @see IcecastTCPAudioBroadcaster for Icecast version 2.3.x and older.
* @param configuration
* @param audioConverter
* Note: use @see IcecastTCPAudioBroadcaster for Icecast 1 (v2.3.x and older).
*
* @param defaultAsyncHttpClient to use when communicating with a remote HTTP server
* @param threadPoolManager
* @param configuration to use when connecting to the remote server
*/
public IcecastHTTPAudioBroadcaster(ThreadPoolManager threadPoolManager, IcecastHTTPConfiguration configuration)
public IcecastHTTPAudioBroadcaster(DefaultAsyncHttpClient defaultAsyncHttpClient,
ThreadPoolManager threadPoolManager,
IcecastHTTPConfiguration configuration)
{
super(threadPoolManager, configuration);
mDefaultAsyncHttpClient = defaultAsyncHttpClient;
}
/**
@@ -94,7 +93,7 @@ public class IcecastHTTPAudioBroadcaster extends AudioBroadcaster implements Pub
}
/**
* Broadcast any queued audio packets using Icecast V2 Protocol
* Broadcast converted audio chunks received from the parent class processor
*/
@Override
protected void broadcastAudio(byte[] audio)
@@ -147,7 +146,7 @@ public class IcecastHTTPAudioBroadcaster extends AudioBroadcaster implements Pub
}
/**
* Creates a connnection to the remote server using the shoutcast configuration information. Once disconnected
* Creates a connection to the remote server using the shoutcast configuration information. Once disconnected
* following a successful connection, attempts to reestablish a connection on a set interval
*/
private void createConnection()
@@ -159,26 +158,21 @@ public class IcecastHTTPAudioBroadcaster extends AudioBroadcaster implements Pub
mLog.debug("Creating a connection to icecast server");
mLastConnectionAttempt = System.currentTimeMillis();
DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder()
.setReadTimeout(-1)
.setRequestTimeout(-1)
.build();
mAsyncHttpClient = new DefaultAsyncHttpClient(config);
Uri uri = new Uri(Uri.HTTP, null, getConfiguration().getHost(), getConfiguration().getPort(),
getConfiguration().getMountPoint(), null);
mLog.debug("URL:" + uri.toUrl());
BoundRequestBuilder builder = mAsyncHttpClient.preparePut(uri.toUrl());
BoundRequestBuilder builder = mDefaultAsyncHttpClient.preparePut(uri.toUrl());
mLog.debug("Setting security realm");
//Use Basic (base64) authentication
Realm realm = new Realm.Builder(getConfiguration().getUserName(), getConfiguration().getPassword())
.setScheme(Realm.AuthScheme.BASIC).setUsePreemptiveAuth(true).build();
builder.setRealm(realm);
builder.setBody(this);
mLog.debug("Adding headers");
builder.addHeader(IcecastHeader.ACCEPT.getValue(), "*/*");
builder.addHeader(IcecastHeader.CONTENT_TYPE.getValue(), getConfiguration().getBroadcastFormat().getValue());
builder.addHeader(IcecastHeader.PUBLIC.getValue(), getConfiguration().isPublic() ? "1" : "0");
@@ -214,6 +208,8 @@ public class IcecastHTTPAudioBroadcaster extends AudioBroadcaster implements Pub
builder.addHeader(IcecastHeader.EXPECT.getValue(), HTTP_100_CONTINUE);
mLog.debug("executing connection with http client");
ListenableFuture<Response> future = builder.execute(new AsyncHttpConnectionResponseHandler());
mLog.debug("Create connection was submitted asynchronously");
@@ -383,57 +379,4 @@ public class IcecastHTTPAudioBroadcaster extends AudioBroadcaster implements Pub
stop();
}
}
public static void main(String[] args)
{
boolean test = false;
IcecastHTTPConfiguration config = new IcecastHTTPConfiguration(BroadcastFormat.MP3);
config.setName("Broadcastify SDRTrunk Test #2");
config.setHost("audio3.broadcastify.com");
config.setPort(80);
config.setMountPoint("/k0yrdpx7zn4h");
config.setUserName("source");
config.setPassword("k8j9405n");
config.setDescription("SDRTrunk Test Feed #2");
config.setGenre("Scanner");
config.setPublic(true);
config.setURL("http://www.radioreference.com");
config.setBitRate(16);
if(test)
{
mLog.debug("Auth:" + config.getEncodedCredentials());
}
else
{
ThreadPoolManager threadPoolManager = new ThreadPoolManager();
DefaultAsyncHttpClient httpClient = new DefaultAsyncHttpClient();
final AudioBroadcaster audioBroadcaster = BroadcastFactory.getBroadcaster(httpClient, threadPoolManager,config);
Path path = Paths.get("/home/denny/Music/PCM.wav");
mLog.debug("Opening: " + path.toString());
mLog.debug("Registering and starting audio playback");
while(true)
{
mLog.debug("Playback started [" + path.toString() + "]");
try (AudioPacketMonoWaveReader reader = new AudioPacketMonoWaveReader(path, true))
{
reader.setListener(audioBroadcaster);
reader.read();
}
catch (IOException e)
{
mLog.error("Error", e);
}
mLog.debug("Playback ended [" + path.toString() + "]");
}
}
}
}

View File

@@ -26,12 +26,12 @@ import java.util.Base64;
public class IcecastHTTPConfiguration extends BroadcastConfiguration
{
private String mUserName;
private String mMountPoint;
private String mUserName = "source";
private String mMountPoint = "stream";
private String mDescription;
private String mGenre;
private boolean mPublic;
private int mBitRate;
private int mBitRate = 16; //kHz
private int mChannels = 1;
private int mSampleRate = 8000;
private String mURL;
@@ -39,6 +39,7 @@ public class IcecastHTTPConfiguration extends BroadcastConfiguration
public IcecastHTTPConfiguration()
{
//no-arg JAXB constructor
this(BroadcastFormat.MP3);
}
/**
@@ -47,6 +48,7 @@ public class IcecastHTTPConfiguration extends BroadcastConfiguration
public IcecastHTTPConfiguration(BroadcastFormat format)
{
super(format);
setPassword("change me!");
}
@Override
@@ -103,16 +105,25 @@ public class IcecastHTTPConfiguration extends BroadcastConfiguration
return "Basic " + base64;
}
/**
* User name. Default is 'source'.
*/
public String getUserName()
{
return mUserName;
}
/**
* Sets the user name.
*/
public void setUserName(String userName)
{
mUserName = userName;
}
/**
* Indicates if this configuration has a user name
*/
public boolean hasUserName()
{
return mUserName != null;
@@ -136,22 +147,34 @@ public class IcecastHTTPConfiguration extends BroadcastConfiguration
mMountPoint = mountPoint;
}
/**
* Indicates if this configuration has a mount point
*/
public boolean hasMountPoint()
{
return mMountPoint != null;
}
/**
* Stream description
*/
public String getDescription()
{
return mDescription;
}
/**
* Sets the stream description
*/
public void setDescription(String description)
{
mDescription = description;
}
/**
* Indicates if this configuration contains a stream description
*/
public boolean hasDescription()
{
return mDescription != null;
@@ -166,14 +189,16 @@ public class IcecastHTTPConfiguration extends BroadcastConfiguration
}
/**
* Stream genre
* @param genre
* Sets the stream genre
*/
public void setGenre(String genre)
{
mGenre = genre;
}
/**
* Indicates if this configuration contains a genre
*/
public boolean hasGenre()
{
return mGenre != null;
@@ -234,21 +259,33 @@ public class IcecastHTTPConfiguration extends BroadcastConfiguration
mBitRate = bitRate;
}
/**
* Indicates if this configuration has a bit rate value
*/
public boolean hasBitRate()
{
return mBitRate > 0;
}
/**
* Sample rate of the input PCM audio samples. Default is 8 kHz
*/
public int getSampleRate()
{
return mSampleRate;
}
/**
* Sets the input PCM audio sample rate
*/
public void setSampleRate(int sampleRate)
{
mSampleRate = sampleRate;
}
/**
* Indicates if this configuration contains an audio sample rate
*/
public boolean hasSampleRate()
{
return mSampleRate > 0;
@@ -264,13 +301,15 @@ public class IcecastHTTPConfiguration extends BroadcastConfiguration
/**
* URL associated with the broadcastAudio where users can find additional details.
* @param url
*/
public void setURL(String url)
{
mURL = url;
}
/**
* Indicates if this configuration contains a stream URL
*/
public boolean hasURL()
{
return mURL != null;

View File

@@ -42,6 +42,7 @@ public class IcecastHTTPConfigurationEditor extends BroadcastConfigurationEditor
private JTextField mServer;
private JTextField mPort;
private JTextField mMountPoint;
private JTextField mUsername;
private JTextField mPassword;
private JButton mSaveButton;
private JButton mResetButton;
@@ -59,7 +60,7 @@ public class IcecastHTTPConfigurationEditor extends BroadcastConfigurationEditor
setLayout( new MigLayout( "fill,wrap 2", "[align right][grow,fill]", "[][][][][][][][][][grow,fill]" ) );
setPreferredSize(new Dimension(150,400));
JLabel channelLabel = new JLabel("Icecast 2.4+ Stream");
JLabel channelLabel = new JLabel("Icecast 2 (v2.4+) Stream");
ImageIcon icon = mSettingsManager.getImageIcon(BroadcastServerType.ICECAST_HTTP.getIconName(), 25);
channelLabel.setIcon(icon);
@@ -90,6 +91,12 @@ public class IcecastHTTPConfigurationEditor extends BroadcastConfigurationEditor
mMountPoint.setToolTipText("Mount point (e.g. \\12345)");
add(mMountPoint);
add(new JLabel("User Name:"));
mUsername = new JTextField();
mUsername.getDocument().addDocumentListener(this);
mUsername.setToolTipText("User name for the stream. Default: source");
add(mUsername);
add(new JLabel("Password:"));
mPassword = new JTextField();
mPassword.getDocument().addDocumentListener(this);
@@ -158,6 +165,7 @@ public class IcecastHTTPConfigurationEditor extends BroadcastConfigurationEditor
mServer.setText(config.getHost());
mPort.setText(config.getPort() != 0 ? String.valueOf(config.getPort()) : null);
mMountPoint.setText(config.getMountPoint());
mUsername.setText(config.getUserName());
mPassword.setText(config.getPassword());
mEnabled.setSelected(config.isEnabled());
}
@@ -167,6 +175,7 @@ public class IcecastHTTPConfigurationEditor extends BroadcastConfigurationEditor
mServer.setText(null);
mPort.setText(null);
mMountPoint.setText(null);
mUsername.setText(null);
mPassword.setText(null);
mEnabled.setSelected(false);
}
@@ -185,6 +194,7 @@ public class IcecastHTTPConfigurationEditor extends BroadcastConfigurationEditor
config.setHost(mServer.getText());
config.setPort(getPort());
config.setMountPoint(mMountPoint.getText());
config.setUserName(mUsername.getText());
config.setPassword(mPassword.getText());
config.setEnabled(mEnabled.isSelected());
@@ -228,16 +238,22 @@ public class IcecastHTTPConfigurationEditor extends BroadcastConfigurationEditor
return true;
}
//Mount Point is optional, but required
//Mount Point is required
if(!validateTextField(mMountPoint, "Invalid Mount Point", "Please specify a mount point."))
{
return true;
return false;
}
//Password is optional, but required
//User name is required
if(!validateTextField(mUsername, "Invalid User Name", "Please specify a user name."))
{
return false;
}
//Password is required
if(!validateTextField(mPassword, "Invalid Password", "Please specify a password."))
{
return true;
return false;
}
return true;

View File

@@ -61,7 +61,7 @@ public class IcecastTCPConfigurationEditor extends BroadcastConfigurationEditor
setLayout( new MigLayout( "fill,wrap 2", "[align right][grow,fill]", "[][][][][][][][][][grow,fill]" ) );
setPreferredSize(new Dimension(150,400));
JLabel channelLabel = new JLabel("Icecast 2.3 Stream");
JLabel channelLabel = new JLabel("Icecast (v2.3) Stream");
ImageIcon icon = mSettingsManager.getImageIcon(BroadcastServerType.ICECAST_TCP.getIconName(), 25);
channelLabel.setIcon(icon);