Skip to content

Commit fdfecc1

Browse files
committed
Fix potential deadlock in media streaming classes
Fixes #1398
1 parent 707a0a9 commit fdfecc1

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
@@ -102,6 +102,8 @@ public void stop() {
102102
t = null;
103103
}
104104

105+
mOutputQueue.clear();
106+
105107
}
106108

107109
public void run() {
@@ -261,6 +263,10 @@ private void sendArrayData(byte[] data, int offset, int length)
261263
throw new ArrayIndexOutOfBoundsException();
262264
}
263265

266+
if (data == null || t == null || t.isInterrupted()) {
267+
return;
268+
}
269+
264270
// StreamPacketizer does not need to split a video frame into NAL units
265271
ByteBuffer buffer = ByteBuffer.allocate(length);
266272
buffer.put(data, offset, length);
@@ -274,7 +280,7 @@ private void sendArrayData(byte[] data, int offset, int length)
274280
}
275281

276282
private void sendByteBufferData(ByteBuffer data, CompletionListener completionListener) {
277-
if (data == null || data.remaining() == 0) {
283+
if (data == null || data.remaining() == 0 || t == null || t.isInterrupted()) {
278284
return;
279285
}
280286

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.smartdevicelink.proxy.interfaces.IVideoStreamListener;
3737
import com.smartdevicelink.streaming.AbstractPacketizer;
3838
import com.smartdevicelink.streaming.IStreamListener;
39+
import com.smartdevicelink.util.DebugTool;
3940

4041
import java.io.IOException;
4142
import java.nio.ByteBuffer;
@@ -62,6 +63,8 @@
6263
*/
6364
public class RTPH264Packetizer extends AbstractPacketizer implements IVideoStreamListener, Runnable {
6465

66+
private static final String TAG = "RTPH264Packetizer";
67+
6568
// Approximate size of data that mOutputQueue can hold in bytes.
6669
// By adding a buffer, we accept underlying transport being stuck for a short time. By setting
6770
// a limit of the buffer size, we avoid buffer overflows when underlying transport is too slow.
@@ -170,6 +173,10 @@ public void start() throws IOException {
170173
return;
171174
}
172175

176+
if(mOutputQueue != null){
177+
mOutputQueue.clear();
178+
}
179+
173180
mThread = new Thread(this);
174181
mThread.start();
175182
}
@@ -293,13 +300,18 @@ private void onEncoderOutput(NALUnitReader nalUnitReader, long ptsInUs) {
293300
}
294301

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

302-
while (nalUnit.remaining() > 0) {
314+
while (nalUnit.remaining() > 0 && mThread != null && !mThread.isInterrupted()) {
303315
int payloadLength = MAX_RTP_PACKET_SIZE - (RTP_HEADER_LEN + FU_INDICATOR_LEN + FU_HEADER_LEN);
304316
if (nalUnit.remaining() <= payloadLength) {
305317
payloadLength = nalUnit.remaining();

0 commit comments

Comments
 (0)