Skip to content

Commit

Permalink
can create capture session now
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaji Khan committed Mar 15, 2024
1 parent 398230d commit 00a8de7
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions app/src/main/java/com/shajikhan/ladspa/amprack/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.CamcorderProfile;
import android.media.ImageReader;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
Expand Down Expand Up @@ -41,7 +42,7 @@ public class Camera2 {
// parameters for the encoder
private static final String MIME_TYPE = "video/avc"; // H.264 Advanced Video Coding
private static final int FRAME_RATE = 30; // 15fps
private static final int IFRAME_INTERVAL = 10; // 10 seconds between I-frames
private static final int IFRAME_INTERVAL = 1; // 10 seconds between I-frames
private int mWidth = -1;
private int mHeight = -1;
// bit rate, in bits per second
Expand Down Expand Up @@ -72,7 +73,6 @@ public class Camera2 {
protected CameraCaptureSession cameraCaptureSessions;
protected CaptureRequest captureRequest;
protected CaptureRequest.Builder captureRequestBuilder;
protected CaptureRequest.Builder videoBuilder;
private Size imageDimension;
private ImageReader imageReader;
ArrayList<String> cameras;
Expand Down Expand Up @@ -179,20 +179,21 @@ public void createCameraPreview() {
try {
SurfaceTexture texture = textureView.getSurfaceTexture();
assert texture != null;
// fixme: change this!
imageDimension = new Size (320, 240);
texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
Log.d(TAG, "createCameraPreview: created surface with dimensions".format (" %d x %d", imageDimension.getWidth(), imageDimension.getHeight()));
Log.d(TAG, "createCameraPreview: created surface with dimensions" + ":".format (" %d x %d", imageDimension.getWidth(), imageDimension.getHeight()));
Surface surface = new Surface(texture);
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
videoBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
captureRequestBuilder.addTarget(surface);

prepareEncoder();
videoBuilder.addTarget(mInputSurface);
captureRequestBuilder.addTarget(mInputSurface);
List<Surface> surfaceList = new ArrayList<>();
surfaceList.add(surface);
surfaceList.add(mInputSurface);

cameraDevice.createCaptureSession(surfaceList, new CameraCaptureSession.StateCallback() {
cameraDevice.createCaptureSession(Arrays.asList(surface,mInputSurface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
//The camera is already closed
Expand All @@ -210,6 +211,7 @@ public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession
}
}, null);
} catch (CameraAccessException e) {
MainActivity.alert("Failed to start camera \uD83D\uDE13", e.getMessage());
e.printStackTrace();
}
}
Expand All @@ -218,10 +220,10 @@ protected void updatePreview() {
if (null == cameraDevice) {
Log.e(TAG, "updatePreview error, return");
}
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
// captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
try {
cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
cameraCaptureSessions.setRepeatingRequest(videoBuilder.build(), null, mBackgroundHandler);
// cameraCaptureSessions.setRepeatingRequest(videoBuilder.build(), null, encoderHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -267,6 +269,7 @@ private void prepareEncoder() {
} catch (IOException e) {
throw new RuntimeException(e);
}

mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mInputSurface = mEncoder.createInputSurface();
mEncoder.start();
Expand Down

0 comments on commit 00a8de7

Please sign in to comment.