Skip to content
Snippets Groups Projects
Commit 0c404b27 authored by Leo Ma's avatar Leo Ma
Browse files

Add thread for non blocking of preview callback


Signed-off-by: default avatarLeo Ma <begeekmyfriend@gmail.com>
parent 9d3802a9
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -10,7 +10,9 @@ import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
import java.nio.IntBuffer;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* Created by leo.ma on 2016/9/13.
Loading
Loading
@@ -28,6 +30,10 @@ public class SrsCameraView extends SurfaceView implements SurfaceHolder.Callback
private boolean mIsEncoding;
private boolean mIsTorchOn = false;
private Thread worker;
private final Object writeLock = new Object();
private ConcurrentLinkedQueue<byte []> mByteBufferCache = new ConcurrentLinkedQueue<>();
public interface PreviewCallback {
void onGetYuvFrame(byte[] data);
}
Loading
Loading
@@ -92,11 +98,43 @@ public class SrsCameraView extends SurfaceView implements SurfaceHolder.Callback
}
public void enableEncoding() {
worker = new Thread(new Runnable() {
@Override
public void run() {
while (!Thread.interrupted()) {
while (!mByteBufferCache.isEmpty()) {
mPrevCb.onGetYuvFrame(mByteBufferCache.poll());
}
// Waiting for next frame
synchronized (writeLock) {
try {
// isEmpty() may take some time, so we set timeout to detect next frame
writeLock.wait(500);
} catch (InterruptedException ie) {
worker.interrupt();
}
}
}
}
});
worker.start();
mIsEncoding = true;
}
public void disableEncoding() {
mIsEncoding = false;
mByteBufferCache.clear();
if (worker != null) {
worker.interrupt();
try {
worker.join();
} catch (InterruptedException e) {
e.printStackTrace();
worker.interrupt();
}
worker = null;
}
}
public boolean startCamera() {
Loading
Loading
@@ -230,7 +268,10 @@ public class SrsCameraView extends SurfaceView implements SurfaceHolder.Callback
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (mIsEncoding) {
mPrevCb.onGetYuvFrame(data);
mByteBufferCache.add(data);
synchronized (writeLock) {
writeLock.notifyAll();
}
camera.addCallbackBuffer(mYuvPreviewFrame);
}
}
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment