Fix some ANRs and force-close errors when connection failures occur.

This commit is contained in:
Cameron Gutman 2013-11-11 17:21:50 -05:00
parent 4d80373f25
commit 83ad55e436
2 changed files with 41 additions and 23 deletions

View File

@ -64,8 +64,13 @@ public class NvAudioStream {
} catch (InterruptedException e) { } } catch (InterruptedException e) { }
} }
if (session != null) {
//session.endSession(); //session.endSession();
}
if (track != null) {
track.release(); track.release();
}
threads.clear(); threads.clear();
} }

View File

@ -36,6 +36,8 @@ public class NvVideoStream {
private RTPSession session; private RTPSession session;
private DatagramSocket rtp, rtcp; private DatagramSocket rtp, rtcp;
private Socket firstFrameSocket;
private LinkedList<Thread> threads = new LinkedList<Thread>(); private LinkedList<Thread> threads = new LinkedList<Thread>();
@ -63,6 +65,11 @@ public class NvVideoStream {
if (rtcp != null) { if (rtcp != null) {
rtcp.close(); rtcp.close();
} }
if (firstFrameSocket != null) {
try {
firstFrameSocket.close();
} catch (IOException e) {}
}
// Wait for threads to terminate // Wait for threads to terminate
for (Thread t : threads) { for (Thread t : threads) {
@ -71,8 +78,12 @@ public class NvVideoStream {
} catch (InterruptedException e) { } } catch (InterruptedException e) { }
} }
if (session != null) {
//session.endSession(); //session.endSession();
}
if (videoDecoder != null) {
videoDecoder.release(); videoDecoder.release();
}
threads.clear(); threads.clear();
} }
@ -85,10 +96,12 @@ public class NvVideoStream {
private void readFirstFrame(String host) throws IOException private void readFirstFrame(String host) throws IOException
{ {
byte[] firstFrame = depacketizer.allocatePacketBuffer(); byte[] firstFrame = depacketizer.allocatePacketBuffer();
Socket s = new Socket(host, FIRST_FRAME_PORT);
System.out.println("VID: Waiting for first frame"); System.out.println("VID: Waiting for first frame");
InputStream firstFrameStream = s.getInputStream(); firstFrameSocket = new Socket(host, FIRST_FRAME_PORT);
try {
InputStream firstFrameStream = firstFrameSocket.getInputStream();
int offset = 0; int offset = 0;
for (;;) for (;;)
@ -101,11 +114,12 @@ public class NvVideoStream {
offset += bytesRead; offset += bytesRead;
} }
s.close();
System.out.println("VID: First frame read ("+offset+" bytes)"); System.out.println("VID: First frame read ("+offset+" bytes)");
depacketizer.addInputData(new AvVideoPacket(new AvByteBufferDescriptor(firstFrame, 0, offset))); depacketizer.addInputData(new AvVideoPacket(new AvByteBufferDescriptor(firstFrame, 0, offset)));
} finally {
firstFrameSocket.close();
firstFrameSocket = null;
}
} }
public void setupRtpSession(String host) throws SocketException public void setupRtpSession(String host) throws SocketException
@ -161,7 +175,6 @@ public class NvVideoStream {
try { try {
readFirstFrame(host); readFirstFrame(host);
} catch (IOException e2) { } catch (IOException e2) {
e2.printStackTrace();
abort(); abort();
return; return;
} }