Skip to content

Commit

Permalink
added retry to connect
Browse files Browse the repository at this point in the history
  • Loading branch information
segler-alex committed May 10, 2016
1 parent 2a24fb3 commit e606a4e
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,19 @@ private void Play(final Context context, final String stationId) {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
return Utils.getRealStationLink(context, stationId);
String result = null;
for (int i=0;i<20;i++){
result = Utils.getRealStationLink(context, stationId);
if (result != null){
return result;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Log.e("ALARM","Play() "+e);
}
}
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import android.widget.Toast;

import net.programmierecke.radiodroid2.data.ShoutcastInfo;
import net.programmierecke.radiodroid2.interfaces.IConnectionReady;
import net.programmierecke.radiodroid2.interfaces.IStreamProxyEventReceiver;

public class PlayerService extends Service implements IConnectionReady {
public class PlayerService extends Service implements IStreamProxyEventReceiver {
protected static final int NOTIFY_ID = 1;
public static final String PLAYER_SERVICE_TIMER_UPDATE = "net.programmierecke.radiodroid2.timerupdate";
public static final String PLAYER_SERVICE_STATUS_UPDATE = "net.programmierecke.radiodroid2.statusupdate";
Expand Down Expand Up @@ -406,6 +406,11 @@ public void foundLiveStreamInfo(Map<String, String> liveInfo) {
UpdateNotification();
}

@Override
public void streamStopped() {
Stop();
}

public void Stop() {
if (itsMediaPlayer != null) {
if (itsMediaPlayer.isPlaying()) {
Expand Down
215 changes: 120 additions & 95 deletions app/src/main/java/net/programmierecke/radiodroid2/StreamProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import android.util.Log;

import net.programmierecke.radiodroid2.data.ShoutcastInfo;
import net.programmierecke.radiodroid2.interfaces.IConnectionReady;
import net.programmierecke.radiodroid2.interfaces.IStreamProxyEventReceiver;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -25,7 +24,7 @@
import java.util.Map;

public class StreamProxy {
private IConnectionReady callback;
private IStreamProxyEventReceiver callback;
private String uri;
private long connectionBytesTotal = 0;
private Socket socketProxy;
Expand All @@ -35,7 +34,7 @@ public class StreamProxy {
private boolean isStopped = false;
private String outFileName = null;

public StreamProxy(Context context, String uri, IConnectionReady callback) {
public StreamProxy(Context context, String uri, IStreamProxyEventReceiver callback) {
this.context = context;
this.uri = uri;
this.callback = callback;
Expand All @@ -51,7 +50,7 @@ private void createProxy() {
int port = proxyServer.getLocalPort();
localAdress = String.format(Locale.US,"http://localhost:%d",port);
} catch (IOException e) {
Log.e("ABC",""+e);
Log.e("PROXY","createProxy() create server socket: "+e);
}

if (proxyServer != null) {
Expand All @@ -66,16 +65,16 @@ public void run() {

doConnectToStream();

Log.i("ABC", "juhu");
Log.i("PROXY", "createProxy() ended");
} catch (IOException e) {
Log.e("ABC", "" + e);
Log.e("PROXY", "" + e);
}
}
}).start();

while (localAdress == null) {
try {
Log.i("ABC", "starting serversock...");
Log.i("PROXY", "starting serversock...");
Thread.sleep(100);
} catch (Exception e) {
}
Expand All @@ -87,105 +86,120 @@ public void run() {
OutputStream out;

private void doConnectToStream() {
try {
// connect to stream
URLConnection connection = new URL(uri).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestProperty("Icy-MetaData", "1");
connection.connect();

in = connection.getInputStream();

// send ok message to local mediaplayer
out = socketProxy.getOutputStream();
out.write(("HTTP/1.0 200 OK\r\n" +
"Pragma: no-cache\r\n" +
"Content-Type: " + connection.getContentType() +
"\r\n\r\n").getBytes("utf-8"));

// try to get shoutcast information from stream connection
ShoutcastInfo info = ShoutcastInfo.Decode(connection);

int bytesUntilMetaData = 0;
boolean readMetaData = false;
boolean filterOutMetaData = false;

if (info != null) {
callback.foundShoutcastStream(info);
bytesUntilMetaData = info.metadataOffset;
filterOutMetaData = true;
}

byte buf[] = new byte[16384];
byte bufMetadata[] = new byte[256 * 16];
int readBytesBuffer = 0;
int readBytesBufferMetadata = 0;

while (!isStopped) {
int readBytes = 0;
if (!filterOutMetaData || (bytesUntilMetaData > 0))
{
int bytesToRead = buf.length - readBytesBuffer;
if (filterOutMetaData){
bytesToRead = Math.min(bytesUntilMetaData,bytesToRead);
}
readBytes = in.read(buf, readBytesBuffer, bytesToRead);
if (readBytes == 0) {
continue;
}
if (readBytes < 0) {
break;
}
readBytesBuffer += readBytes;
connectionBytesTotal += readBytes;
if (filterOutMetaData){
bytesUntilMetaData -= readBytes;
try{
final int MaxRetries = 30;
int retry = MaxRetries;
while (!isStopped && retry > 0) {
try {
// connect to stream
URLConnection connection = new URL(uri).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestProperty("Icy-MetaData", "1");
connection.connect();

in = connection.getInputStream();

// send ok message to local mediaplayer
out = socketProxy.getOutputStream();
out.write(("HTTP/1.0 200 OK\r\n" +
"Pragma: no-cache\r\n" +
"Content-Type: " + connection.getContentType() +
"\r\n\r\n").getBytes("utf-8"));

// try to get shoutcast information from stream connection
ShoutcastInfo info = ShoutcastInfo.Decode(connection);

int bytesUntilMetaData = 0;
boolean readMetaData = false;
boolean filterOutMetaData = false;

if (info != null) {
callback.foundShoutcastStream(info);
bytesUntilMetaData = info.metadataOffset;
filterOutMetaData = true;
}

Log.v("ABC","in:"+readBytes);
if (readBytesBuffer > buf.length / 2) {
Log.v("ABC","out:"+readBytesBuffer);
out.write(buf, 0, readBytesBuffer);
if (fileOutputStream != null) {
Log.v("ABC","writing to record file..");
fileOutputStream.write(buf, 0, readBytesBuffer);
}
readBytesBuffer = 0;
}
}else
{
int metadataBytes = in.read() * 16;
int metadataBytesToRead = metadataBytes;
readBytesBufferMetadata = 0;
bytesUntilMetaData = info.metadataOffset;
Log.d("ABC","metadata size:"+metadataBytes);
if (metadataBytes > 0) {
Arrays.fill(bufMetadata, (byte) 0);
while (true) {
readBytes = in.read(bufMetadata, readBytesBufferMetadata, metadataBytesToRead);
byte buf[] = new byte[16384];
byte bufMetadata[] = new byte[256 * 16];
int readBytesBuffer = 0;
int readBytesBufferMetadata = 0;

while (!isStopped) {
int readBytes = 0;
if (!filterOutMetaData || (bytesUntilMetaData > 0)) {
int bytesToRead = buf.length - readBytesBuffer;
if (filterOutMetaData) {
bytesToRead = Math.min(bytesUntilMetaData, bytesToRead);
}
readBytes = in.read(buf, readBytesBuffer, bytesToRead);
if (readBytes == 0) {
continue;
}
if (readBytes < 0) {
break;
}
metadataBytesToRead -= readBytes;
readBytesBufferMetadata += readBytes;
if (metadataBytesToRead <= 0) {
String s = new String(bufMetadata, 0, metadataBytes, "utf-8");
Log.d("ABC", "METADATA:" + s);
Map<String,String> dict = DecodeShoutcastMetadata(s);
Log.d("ABC", "META:"+dict.get("StreamTitle"));
callback.foundLiveStreamInfo(dict);
break;
readBytesBuffer += readBytes;
connectionBytesTotal += readBytes;
if (filterOutMetaData) {
bytesUntilMetaData -= readBytes;
}

Log.v("ABC", "in:" + readBytes);
if (readBytesBuffer > buf.length / 2) {
Log.v("ABC", "out:" + readBytesBuffer);
out.write(buf, 0, readBytesBuffer);
if (fileOutputStream != null) {
Log.v("ABC", "writing to record file..");
fileOutputStream.write(buf, 0, readBytesBuffer);
}
readBytesBuffer = 0;
}
} else {
int metadataBytes = in.read() * 16;
int metadataBytesToRead = metadataBytes;
readBytesBufferMetadata = 0;
bytesUntilMetaData = info.metadataOffset;
Log.d("ABC", "metadata size:" + metadataBytes);
if (metadataBytes > 0) {
Arrays.fill(bufMetadata, (byte) 0);
while (true) {
readBytes = in.read(bufMetadata, readBytesBufferMetadata, metadataBytesToRead);
if (readBytes == 0) {
continue;
}
if (readBytes < 0) {
break;
}
metadataBytesToRead -= readBytes;
readBytesBufferMetadata += readBytes;
if (metadataBytesToRead <= 0) {
String s = new String(bufMetadata, 0, metadataBytes, "utf-8");
Log.d("ABC", "METADATA:" + s);
Map<String, String> dict = DecodeShoutcastMetadata(s);
Log.d("ABC", "META:" + dict.get("StreamTitle"));
callback.foundLiveStreamInfo(dict);
break;
}
}
}
}
// reset retry count, if connection was ok
retry = MaxRetries;
}
} catch (Exception e) {
Log.e("PROXY", "Play()" + e);
}

retry--;
Thread.sleep(1000);
}
}catch(Exception e){
Log.e("ABC",""+e);
} catch (InterruptedException e) {
Log.e("PROXY","Play() "+e);
}
// inform outside if stream stopped, only if outside did not initiate stop
if (!isStopped){
callback.streamStopped();
}
stop();
}
Expand Down Expand Up @@ -281,19 +295,30 @@ public String getLocalAdress() {
}

public void stop() {
stopRecord();
Log.i("PROXY","stop()");
isStopped = true;
stopRecord();
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e("PROXY","stop() in.close() "+e);
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.e("PROXY","stop() out.close() "+e);
}
}
if (socketProxy != null){
try {
socketProxy.close();
} catch (IOException e) {
Log.e("PROXY","stop() socketProxy.close() "+e);
}
socketProxy = null;
}
in = null;
out = null;
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/net/programmierecke/radiodroid2/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static String getCacheFile(Context ctx, String theURI) {
return null;
}
catch(Exception e){
Log.e("UTIL",""+e);
Log.e("UTIL","getCacheFile() "+e);
}
return null;
}
Expand All @@ -77,7 +77,7 @@ public static void writeFileCache(Context ctx, String theURI, String content){
aStream.close();
}
catch(Exception e){
Log.e("UTIL","could not write to cache file for:"+theURI);
Log.e("UTIL","writeFileCache() could not write to cache file for:"+theURI);
}
}

Expand Down Expand Up @@ -112,7 +112,7 @@ public static String downloadFeed(Context ctx, String theURI, boolean forceUpdat
Log.w("UTIL","wrote cache file for:"+theURI);
return s;
} catch (Exception e) {
Log.e("UTIL",""+e);
Log.e("UTIL","downloadFeed() "+e);
}

return null;
Expand All @@ -128,7 +128,7 @@ public static String getRealStationLink(Context ctx, String stationId){
jsonObj = jsonArr.getJSONObject(0);
return jsonObj.getString("url");
} catch (Exception e) {
Log.e("UTIL", "" + e);
Log.e("UTIL", "getRealStationLink() " + e);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import java.util.Map;

public interface IConnectionReady {
public interface IStreamProxyEventReceiver {
void foundShoutcastStream(ShoutcastInfo bitrate);
void foundLiveStreamInfo(Map<String,String> liveInfo);
void streamStopped();
}

0 comments on commit e606a4e

Please sign in to comment.