Why following code have performance issue, the frame from camera is not smooth.
public class VideoCaptureAndroid implements PreviewCallback, Callback{
private Integer deviceRotation = Integer.MAX_VALUE;
public VideoCaptureAndroid(Context context, int id, long native_capturer) {
deviceRotationNotifier = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) {
public void onOrientationChanged(int orientation) {
if (orientation == ORIENTATION_UNKNOWN) {
Log.d(TAG, "The device rotation angle is unknown.");
return;
}
synchronized(deviceRotation) {
if (deviceRotation != orientation) {
deviceRotation = orientation;
}
}
}
};
Exchanger<Handler> handlerExchanger = new Exchanger<Handler>();
cameraThread = new CameraThread(handlerExchanger);
cameraThread.start();
}
public synchronized void onPreviewFrame(byte[] data, Camera callbackCamera) {
int frameRotation = info.orientation;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
frameRotation = (info.orientation - deviceRotation + 360) % 360;
} else if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
frameRotation = (info.orientation + deviceRotation) % 360;
}
onFrame(data, data.length, native_capturer, frameRotation);
camera.addCallbackBuffer(data);
}
}
Seems if I comment out the following code, the frame are smooth, and no performance issues. But I didn't using synchronized
in onPreviewFrame
to access the deviceRotation
, why it will be impacted by onOrientationChanged
?
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
frameRotation = (info.orientation - deviceRotation + 360) % 360;
} else if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
frameRotation = (info.orientation + deviceRotation) % 360;
}