Sleep in RTMP reading thread

Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
camera2
Leo Ma 9 years ago
parent f30393bd81
commit bb6d7e00e3

@ -1,6 +1,7 @@
package net.ossrs.sea.rtmp.io; package net.ossrs.sea.rtmp.io;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import android.util.Log; import android.util.Log;
import net.ossrs.sea.rtmp.packets.RtmpPacket; import net.ossrs.sea.rtmp.packets.RtmpPacket;
@ -29,13 +30,18 @@ public class ReadThread extends Thread {
@Override @Override
public void run() { public void run() {
boolean isEof = false;
while (!Thread.interrupted()) { while (!Thread.interrupted()) {
try { try {
RtmpPacket rtmpPacket = rtmpDecoder.readPacket(in); RtmpPacket rtmpPacket = rtmpDecoder.readPacket(in);
packetRxHandler.handleRxPacket(rtmpPacket); packetRxHandler.handleRxPacket(rtmpPacket);
if (isEof) {
isEof = false;
Thread.sleep(500);
}
} catch (EOFException eof) { } catch (EOFException eof) {
// The handler thread will wait until be invoked. isEof = true;
packetRxHandler.handleRxPacket(null);
// } catch (WindowAckRequired war) { // } catch (WindowAckRequired war) {
// Log.i(TAG, "Window Acknowledgment required, notifying packet handler..."); // Log.i(TAG, "Window Acknowledgment required, notifying packet handler...");
// packetRxHandler.notifyWindowAckRequired(war.getBytesRead()); // packetRxHandler.notifyWindowAckRequired(war.getBytesRead());
@ -43,11 +49,13 @@ public class ReadThread extends Thread {
// // Pass to handler // // Pass to handler
// packetRxHandler.handleRxPacket(war.getRtmpPacket()); // packetRxHandler.handleRxPacket(war.getRtmpPacket());
// } // }
} catch (Exception ex) { } catch (IOException ex) {
if (!this.isInterrupted()) { if (!this.isInterrupted()) {
Log.e(TAG, "Caught exception while reading/decoding packet, shutting down...", ex); Log.e(TAG, "Caught exception while reading/decoding packet, shutting down...", ex);
this.interrupt(); this.interrupt();
} }
} catch (InterruptedException ie) {
ie.printStackTrace();
} }
} }
// Close inputstream // Close inputstream

@ -289,8 +289,11 @@ public class RtmpConnection implements RtmpPublisher, PacketRxHandler, ThreadCon
} }
case WINDOW_ACKNOWLEDGEMENT_SIZE: case WINDOW_ACKNOWLEDGEMENT_SIZE:
WindowAckSize windowAckSize = (WindowAckSize) rtmpPacket; WindowAckSize windowAckSize = (WindowAckSize) rtmpPacket;
Log.d(TAG, "handleRxPacketLoop(): Setting acknowledgement window size to: " + windowAckSize.getAcknowledgementWindowSize()); int size = windowAckSize.getAcknowledgementWindowSize();
rtmpSessionInfo.setAcknowledgmentWindowSize(windowAckSize.getAcknowledgementWindowSize()); Log.d(TAG, "handleRxPacketLoop(): Setting acknowledgement window size: " + size);
rtmpSessionInfo.setAcknowledgmentWindowSize(size);
// Set socket option
socket.setSendBufferSize(size);
break; break;
case SET_PEER_BANDWIDTH: case SET_PEER_BANDWIDTH:
int acknowledgementWindowsize = rtmpSessionInfo.getAcknowledgementWindowSize(); int acknowledgementWindowsize = rtmpSessionInfo.getAcknowledgementWindowSize();

@ -49,16 +49,17 @@ public class WriteThread extends Thread {
} }
out.flush(); out.flush();
} catch (SocketException se) { } catch (SocketException se) {
Log.e(TAG, "Caught SocketException during write loop, shutting down", se); Log.e(TAG, "WriteThread: Caught SocketException during write loop, shutting down", se);
active = false; active = false;
continue; continue;
} catch (IOException ex) { } catch (IOException ex) {
Log.e(TAG, "Caught IOException during write loop, shutting down", ex); Log.e(TAG, "WriteThread: Caught IOException during write loop, shutting down", ex);
active = false; active = false;
continue; // Exit this thread continue; // Exit this thread
} }
// Waiting for next packet // Waiting for next packet
Log.d(TAG, "WriteThread: waiting...");
synchronized (txPacketLock) { synchronized (txPacketLock) {
try { try {
txPacketLock.wait(); txPacketLock.wait();
@ -72,7 +73,7 @@ public class WriteThread extends Thread {
try { try {
out.close(); out.close();
} catch (Exception ex) { } catch (Exception ex) {
Log.w(TAG, "Failed to close outputstream", ex); Log.w(TAG, "WriteThread: Failed to close outputstream", ex);
} }
Log.d(TAG, "exiting"); Log.d(TAG, "exiting");
if (threadController != null) { if (threadController != null) {

Loading…
Cancel
Save