Commit 4224647e authored by Daniel Sonck's avatar Daniel Sonck
Browse files

Merge pull request #12 in THFM/fm.touhou.touhoufm from...

Merge pull request #12 in THFM/fm.touhou.touhoufm from bugfix/THFMA-6-nullpointer-exception-at-rtpreceiver.sendhello to development

* commit '1b604028':
  Fixed Nullpointer Exception in RtpReceiver
parents 7d588250 1b604028
...@@ -9,6 +9,7 @@ import java.net.DatagramSocket; ...@@ -9,6 +9,7 @@ import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.SocketException; import java.net.SocketException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import fm.touhou.touhoufm.service.RadioService; import fm.touhou.touhoufm.service.RadioService;
import fm.touhou.touhoufm.utils.OpusDecoder; import fm.touhou.touhoufm.utils.OpusDecoder;
...@@ -60,31 +61,46 @@ public class RtpReceiver extends Thread { ...@@ -60,31 +61,46 @@ public class RtpReceiver extends Thread {
} }
private void initUdpConn() { private void initUdpConn() throws UnknownHostException {
try { // connect
// connect resolveHost();
if (address == null) {
address = InetAddress.getByName(RadioService.STREAM_HOST);
}
String msg = "Hello"; String msg = "Hello";
DatagramPacket sendPacket = new DatagramPacket(msg.getBytes(), msg.length(), address, RadioService.STREAM_PORT); DatagramPacket sendPacket = new DatagramPacket(msg.getBytes(), msg.length(), address, RadioService.STREAM_PORT);
try {
mRecvSocket.send(sendPacket); mRecvSocket.send(sendPacket);
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Failed to init udp conn", e); Log.e(TAG, "Failed to send hello", e);
} }
} }
private void initUdpSock() { private void resolveHost() throws UnknownHostException {
try { int count = 8;
if (mRecvSocket == null) { while (address == null) {
mRecvSocket = new DatagramSocket(); try {
} else { address = InetAddress.getByName(RadioService.STREAM_HOST);
Log.e(TAG, "Recv socket already initialized"); } catch (UnknownHostException e) {
if (count < 0) {
address = null;
throw e;
} else {
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
interrupt();
}
}
} }
} catch (SocketException e) { count--;
Log.e(TAG, "Failed to init sock", e); }
}
private void initUdpSock() throws SocketException {
if (mRecvSocket == null) {
mRecvSocket = new DatagramSocket();
} else {
Log.e(TAG, "Recv socket already initialized");
} }
} }
...@@ -103,11 +119,19 @@ public class RtpReceiver extends Thread { ...@@ -103,11 +119,19 @@ public class RtpReceiver extends Thread {
@Override @Override
public void run() { public void run() {
initUdpSock(); try {
initUdpConn(); initUdpSock();
play(); initUdpConn();
shutDown(); play();
clrUdpConn(); clrUdpConn();
} catch (SocketException e) {
Log.e(TAG, "UdpSocket error", e);
} catch (UnknownHostException e) {
Log.e(TAG, "Cannot resolve host", e);
} finally {
shutDown();
}
} }
private void play() { private void play() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment