Commit 1b604028 authored by Daniel Sonck's avatar Daniel Sonck
Browse files

Fixed Nullpointer Exception in RtpReceiver

- Properly handle critical exceptions
  - Handle UnknownHostException by preventing to run
  - Handle SocketException by preventing to run
  - Use finally to free the OpusDecoder always
- Try 8 times to resolve hosts and delay by 10 seconds
parent 7d588250
......@@ -9,6 +9,7 @@ import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import fm.touhou.touhoufm.service.RadioService;
import fm.touhou.touhoufm.utils.OpusDecoder;
......@@ -60,31 +61,46 @@ public class RtpReceiver extends Thread {
}
private void initUdpConn() {
try {
// connect
if (address == null) {
address = InetAddress.getByName(RadioService.STREAM_HOST);
}
private void initUdpConn() throws UnknownHostException {
// connect
resolveHost();
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);
} catch (IOException e) {
Log.e(TAG, "Failed to init udp conn", e);
Log.e(TAG, "Failed to send hello", e);
}
}
private void initUdpSock() {
try {
if (mRecvSocket == null) {
mRecvSocket = new DatagramSocket();
} else {
Log.e(TAG, "Recv socket already initialized");
private void resolveHost() throws UnknownHostException {
int count = 8;
while (address == null) {
try {
address = InetAddress.getByName(RadioService.STREAM_HOST);
} catch (UnknownHostException e) {
if (count < 0) {
address = null;
throw e;
} else {
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
interrupt();
}
}
}
} catch (SocketException e) {
Log.e(TAG, "Failed to init sock", e);
count--;
}
}
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 {
@Override
public void run() {
initUdpSock();
initUdpConn();
play();
shutDown();
clrUdpConn();
try {
initUdpSock();
initUdpConn();
play();
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() {
......
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