Commit eb1210d4 by Oliver Woodman

Make sampleQueue thread safe

parent 6b123590
...@@ -32,9 +32,9 @@ import android.util.SparseArray; ...@@ -32,9 +32,9 @@ import android.util.SparseArray;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
/** /**
* Facilitates the extraction of data from the MPEG-2 TS container format. * Facilitates the extraction of data from the MPEG-2 TS container format.
...@@ -144,10 +144,10 @@ public final class TsExtractor { ...@@ -144,10 +144,10 @@ public final class TsExtractor {
public boolean getSample(int track, SampleHolder out) { public boolean getSample(int track, SampleHolder out) {
Assertions.checkState(prepared); Assertions.checkState(prepared);
Queue<Sample> queue = pesPayloadReaders.valueAt(track).sampleQueue; Queue<Sample> queue = pesPayloadReaders.valueAt(track).sampleQueue;
if (queue.isEmpty()) { Sample sample = queue.poll();
if (sample == null) {
return false; return false;
} }
Sample sample = queue.remove();
convert(sample, out); convert(sample, out);
samplePool.recycle(sample); samplePool.recycle(sample);
return true; return true;
...@@ -473,14 +473,14 @@ public final class TsExtractor { ...@@ -473,14 +473,14 @@ public final class TsExtractor {
*/ */
private abstract class PesPayloadReader { private abstract class PesPayloadReader {
public final LinkedList<Sample> sampleQueue; public final ConcurrentLinkedQueue<Sample> sampleQueue;
private MediaFormat mediaFormat; private MediaFormat mediaFormat;
private boolean foundFirstKeyframe; private boolean foundFirstKeyframe;
private boolean foundLastKeyframe; private boolean foundLastKeyframe;
protected PesPayloadReader() { protected PesPayloadReader() {
this.sampleQueue = new LinkedList<Sample>(); this.sampleQueue = new ConcurrentLinkedQueue<Sample>();
} }
public boolean hasMediaFormat() { public boolean hasMediaFormat() {
...@@ -498,8 +498,10 @@ public final class TsExtractor { ...@@ -498,8 +498,10 @@ public final class TsExtractor {
public abstract void read(BitArray pesBuffer, int pesPayloadSize, long pesTimeUs); public abstract void read(BitArray pesBuffer, int pesPayloadSize, long pesTimeUs);
public void clear() { public void clear() {
while (!sampleQueue.isEmpty()) { Sample toRecycle = sampleQueue.poll();
samplePool.recycle(sampleQueue.remove()); while (toRecycle != null) {
samplePool.recycle(toRecycle);
toRecycle = sampleQueue.poll();
} }
} }
......
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 sign in to comment