Skip to content

Commit c003e10

Browse files
authored
Merge pull request #1480 from smartdevicelink/bugfix/issue_1398
Fix Issue 1398 - Avoid deadlock from using blocking queues
2 parents cc044f0 + fdfecc1 commit c003e10

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

android/sdl_android/src/main/java/com/smartdevicelink/streaming/StreamPacketizer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public void stop() {
104104
t = null;
105105
}
106106

107+
mOutputQueue.clear();
108+
107109
}
108110

109111
public void run() {
@@ -252,6 +254,10 @@ private void sendArrayData(byte[] data, int offset, int length)
252254
throw new ArrayIndexOutOfBoundsException();
253255
}
254256

257+
if (data == null || t == null || t.isInterrupted()) {
258+
return;
259+
}
260+
255261
// StreamPacketizer does not need to split a video frame into NAL units
256262
ByteBuffer buffer = ByteBuffer.allocate(length);
257263
buffer.put(data, offset, length);
@@ -265,7 +271,7 @@ private void sendArrayData(byte[] data, int offset, int length)
265271
}
266272

267273
private void sendByteBufferData(ByteBuffer data, CompletionListener completionListener) {
268-
if (data == null || data.remaining() == 0) {
274+
if (data == null || data.remaining() == 0 || t == null || t.isInterrupted()) {
269275
return;
270276
}
271277

base/src/main/java/com/smartdevicelink/streaming/video/RTPH264Packetizer.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.smartdevicelink.protocol.enums.SessionType;
3838
import com.smartdevicelink.streaming.AbstractPacketizer;
3939
import com.smartdevicelink.streaming.IStreamListener;
40+
import com.smartdevicelink.util.DebugTool;
4041

4142
import java.io.IOException;
4243
import java.nio.ByteBuffer;
@@ -64,6 +65,8 @@
6465
@RestrictTo(RestrictTo.Scope.LIBRARY)
6566
public class RTPH264Packetizer extends AbstractPacketizer implements IVideoStreamListener, Runnable {
6667

68+
private static final String TAG = "RTPH264Packetizer";
69+
6770
// Approximate size of data that mOutputQueue can hold in bytes.
6871
// By adding a buffer, we accept underlying transport being stuck for a short time. By setting
6972
// a limit of the buffer size, we avoid buffer overflows when underlying transport is too slow.
@@ -172,6 +175,10 @@ public void start() throws IOException {
172175
return;
173176
}
174177

178+
if(mOutputQueue != null){
179+
mOutputQueue.clear();
180+
}
181+
175182
mThread = new Thread(this);
176183
mThread.start();
177184
}
@@ -295,13 +302,18 @@ private void onEncoderOutput(NALUnitReader nalUnitReader, long ptsInUs) {
295302
}
296303

297304
private boolean outputRTPFrames(ByteBuffer nalUnit, long ptsInUs, boolean isLast) {
305+
if((mThread == null || mThread.isInterrupted())) {
306+
DebugTool.logError(TAG, "Dropping potential buffer because consumer thread is not alive");
307+
return false;
308+
}
309+
298310
if (RTP_HEADER_LEN + nalUnit.remaining() > MAX_RTP_PACKET_SIZE) {
299311
// Split into multiple Fragmentation Units ([5.8] in RFC 6184)
300312
byte firstByte = nalUnit.get();
301313
boolean firstFragment = true;
302314
boolean lastFragment = false;
303315

304-
while (nalUnit.remaining() > 0) {
316+
while (nalUnit.remaining() > 0 && mThread != null && !mThread.isInterrupted()) {
305317
int payloadLength = MAX_RTP_PACKET_SIZE - (RTP_HEADER_LEN + FU_INDICATOR_LEN + FU_HEADER_LEN);
306318
if (nalUnit.remaining() <= payloadLength) {
307319
payloadLength = nalUnit.remaining();

0 commit comments

Comments
 (0)