-
Notifications
You must be signed in to change notification settings - Fork 8
/
MessicRadioPluginIceCast2.java
338 lines (287 loc) · 9.44 KB
/
MessicRadioPluginIceCast2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package org.messic.server.api.radio.icecast2;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Locale;
import java.util.Properties;
import org.messic.server.api.plugin.MessicPlugin;
import org.messic.server.api.plugin.radio.MessicRadioInfo;
import org.messic.server.api.plugin.radio.MessicRadioPlugin;
import org.messic.server.api.plugin.radio.MessicRadioSong;
import org.messic.server.api.plugin.radio.MessicRadioStatus;
import org.messic.server.api.radio.icecast2.libshout.LibShout;
import org.messic.server.api.radio.icecast2.libshout.LibShoutFormat;
import org.messic.server.api.radio.icecast2.libshout.LibShoutProtocol;
public class MessicRadioPluginIceCast2
implements MessicRadioPlugin
{
public static final String NAME = "ICECAST2";
public static final String EN_DESCRIPTION = "Plugin to stream music through an icecast2 server.";
public static final float VERSION = 1.0f;
public static final float MINIMUM_MESSIC_VERSION = 1.0f;
public static final String PARAMETER_ENABLE = "plugin-radio-icecast2";
public static final String PARAMETER_HOST = "plugin-radio-icecast2-host";
public static final String PARAMETER_PUBLIC_HOST = "plugin-radio-icecast2-publichost";
public static final String PARAMETER_PUBLIC_PORT = "plugin-radio-icecast2-publicport";
public static final String PARAMETER_PUBLIC_URLPATH = "plugin-radio-icecast2-publicurlpath";
public static final String PARAMETER_PORT = "plugin-radio-icecast2-port";
public static final String PARAMETER_PASSWORD = "plugin-radio-icecast2-password";
public static final String PARAMETER_USER = "plugin-radio-icecast2-user";
public static final String PARAMETER_MOUNT = "plugin-radio-icecast2-mount";
private MessicRadioInfo info = new MessicRadioInfo();
private String host;
private int port;
private String mount;
/** configuration for the plugin */
private Properties configuration;
private MRPCastThread thread = null;
protected Proxy getProxy()
{
if ( this.configuration != null )
{
String url = (String) this.configuration.get( MessicPlugin.CONFIG_PROXY_URL );
String port = (String) this.configuration.get( MessicPlugin.CONFIG_PROXY_PORT );
if ( url != null && port != null && url.length() > 0 && port.length() > 0 )
{
SocketAddress addr = new InetSocketAddress( url, Integer.valueOf( port ) );
Proxy proxy = new Proxy( Proxy.Type.HTTP, addr );
return proxy;
}
}
return null;
}
private void checkConnection()
{
if ( isEnabled() )
{
if ( isStarted() )
{
try
{
if ( !LibShout.get().isConnected() )
{
this.info.status = MessicRadioStatus.NOT_STARTED;
}
}
catch ( Exception e )
{
this.info.status = MessicRadioStatus.NOT_ENABLED;
}
catch ( Error e )
{
this.info.status = MessicRadioStatus.NOT_ENABLED;
}
}
}
else
{
this.info.status = MessicRadioStatus.NOT_ENABLED;
}
}
/**
* Flag to know if it is the first time we try to connect with the server
*/
private static boolean flagFirstTime = true;
@Override
public synchronized void startCast()
{
checkConnection();
if ( isEnabled() && !isStarted() )
{
try
{
if ( !flagFirstTime )
{
LibShout.get().close();
if ( thread != null )
{
thread.stopCast();
}
LibShout.get().reinit();
}
else
{
flagFirstTime = false;
}
host = (String) getConfiguration().get( PARAMETER_HOST );
port = Integer.valueOf( (String) getConfiguration().get( PARAMETER_PORT ) );
mount = (String) getConfiguration().getProperty( PARAMETER_MOUNT );
LibShout icecast = LibShout.get();
icecast.setHost( host );
icecast.setPort( port );
icecast.setProtocol( LibShoutProtocol.SHOUT_PROTOCOL_HTTP );
icecast.setUser( (String) getConfiguration().getProperty( PARAMETER_USER ) );
icecast.setPassword( (String) getConfiguration().getProperty( PARAMETER_PASSWORD ) );
icecast.setMount( mount );
icecast.setFormat( LibShoutFormat.SHOUT_FORMAT_MP3 );
icecast.open();
// just to be sure...
if ( !icecast.isConnected() || !checkIcecast2PortListening( host, port ) )
{
throw new Exception( "ICECAST SERVER Not connected!" );
}
// we start the thread to send music to the cast server
// this.info.status = MessicRadioStatus.WAITING; //we cannot do this yet
thread = new MRPCastThread( this.getInfo() );
thread.start();
// the status is started
this.info.publicURLHost = (String) getConfiguration().get( PARAMETER_PUBLIC_HOST );
try
{
this.info.publicURLPort =
Integer.valueOf( (String) getConfiguration().get( PARAMETER_PUBLIC_PORT ) );
}
catch ( Exception e )
{
e.printStackTrace();
this.info.publicURLPort = 80;
}
this.info.publicURLPath = (String) getConfiguration().get( PARAMETER_PUBLIC_URLPATH );
}
catch ( Exception e )
{
e.printStackTrace();
this.info.status = MessicRadioStatus.NOT_AVAILABLE;
}
}
}
/**
* Check if the icecast2 server is listening at the hostname and port specified
*
* @param hostname {@link String} hostname to check
* @param port int port to check
* @return boolean true->if it is listening
*/
private boolean checkIcecast2PortListening( String hostname, int port )
{
try
{
Socket socket = new Socket();
socket.connect( new InetSocketAddress( hostname, port ), 3000 );
socket.close();
return true;
}
catch ( Exception e )
{
return false;
}
}
public synchronized void castSong( MessicRadioSong mp3song )
throws IOException
{
if ( isEnabled() && isStarted() )
{
thread.setNextFile( mp3song );
}
}
public synchronized void stopCast()
{
if ( isEnabled() && isStarted() )
{
try
{
thread.stopCast();
Thread.sleep( 1000 );
}
catch ( InterruptedException e )
{
e.printStackTrace();
}
this.info.status = MessicRadioStatus.NOT_STARTED;
try
{
LibShout.get().close();
LibShout.get().shutdown();
}
catch ( Exception e )
{
e.printStackTrace();
}
this.info.status = MessicRadioStatus.NOT_STARTED;
}
}
@Override
public float getVersion()
{
return VERSION;
}
@Override
public float getMinimumMessicVersion()
{
return MINIMUM_MESSIC_VERSION;
}
@Override
public String getName()
{
return NAME;
}
@Override
public String getDescription( Locale locale )
{
return EN_DESCRIPTION;
}
@Override
public Properties getConfiguration()
{
return this.configuration;
}
@Override
public void setConfiguration( Properties properties )
{
this.configuration = properties;
if ( this.configuration != null )
{
if ( !isEnabled() )
{
this.info.status = MessicRadioStatus.NOT_ENABLED;
}
else
{
switch ( this.info.status )
{
case NONE:
this.info.status = MessicRadioStatus.NOT_STARTED;
break;
default:
}
}
}
}
private boolean isEnabled()
{
String senable = (String) getConfiguration().get( PARAMETER_ENABLE );
if ( senable != null )
{
return Boolean.valueOf( senable );
}
else
{
return false;
}
}
public boolean isStarted()
{
switch ( this.info.status )
{
case NONE:
case NOT_AVAILABLE:
case NOT_ENABLED:
case NOT_STARTED:
return false;
default:
return true;
}
}
@Override
public MessicRadioStatus getStatus()
{
return this.info.status;
}
@Override
public MessicRadioInfo getInfo()
{
return this.info;
}
}