Skip to content

Commit

Permalink
Revert "Migrate camera/android from SurfaceTexture->`SurfaceProdu…
Browse files Browse the repository at this point in the history
…cer`. (flutter#6461)"

This reverts commit 058851b
  • Loading branch information
AlexV525 committed Jun 21, 2024
1 parent 9c6f691 commit cc885ca
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.app.Activity;
import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
Expand Down Expand Up @@ -62,7 +63,7 @@
import io.flutter.plugins.camera.media.MediaRecorderBuilder;
import io.flutter.plugins.camera.types.CameraCaptureProperties;
import io.flutter.plugins.camera.types.CaptureTimeoutsWrapper;
import io.flutter.view.TextureRegistry;
import io.flutter.view.TextureRegistry.SurfaceTextureEntry;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -112,7 +113,7 @@ class Camera
*/
@VisibleForTesting int initialCameraFacing;

@VisibleForTesting final TextureRegistry.SurfaceProducer surfaceProducer;
@VisibleForTesting final SurfaceTextureEntry flutterTexture;
private final VideoCaptureSettings videoCaptureSettings;
private final Context applicationContext;
final DartMessenger dartMessenger;
Expand Down Expand Up @@ -213,16 +214,17 @@ public VideoCaptureSettings(@NonNull ResolutionPreset resolutionPreset, boolean

public Camera(
final Activity activity,
final TextureRegistry.SurfaceProducer surfaceProducer,
final SurfaceTextureEntry flutterTexture,
final CameraFeatureFactory cameraFeatureFactory,
final DartMessenger dartMessenger,
final CameraProperties cameraProperties,
final VideoCaptureSettings videoCaptureSettings) {

if (activity == null) {
throw new IllegalStateException("No activity available!");
}
this.activity = activity;
this.surfaceProducer = surfaceProducer;
this.flutterTexture = flutterTexture;
this.dartMessenger = dartMessenger;
this.applicationContext = activity.getApplicationContext();
this.cameraProperties = cameraProperties;
Expand All @@ -241,6 +243,7 @@ public Camera(
if (videoCaptureSettings.fps != null && videoCaptureSettings.fps.intValue() > 0) {
recordingFps = videoCaptureSettings.fps;
} else {

if (SdkCapabilityChecker.supportsEncoderProfiles()) {
EncoderProfiles encoderProfiles = getRecordingProfile();
if (encoderProfiles != null && encoderProfiles.getVideoProfiles().size() > 0) {
Expand All @@ -253,6 +256,7 @@ public Camera(
}

if (recordingFps != null && recordingFps.intValue() > 0) {

final FpsRangeFeature fpsRange = new FpsRangeFeature(cameraProperties);
fpsRange.setValue(new Range<Integer>(recordingFps, recordingFps));
this.cameraFeatures.setFpsRange(fpsRange);
Expand Down Expand Up @@ -303,9 +307,8 @@ private void prepareMediaRecorder(String outputFilePath) throws IOException {

MediaRecorderBuilder mediaRecorderBuilder;

// TODO(camsim99): Revert changes that allow legacy code to be used when recordingProfile is
// null once this has largely been fixed on the Android side.
// https://github.com/flutter/flutter/issues/119668
// TODO(camsim99): Revert changes that allow legacy code to be used when recordingProfile is null
// once this has largely been fixed on the Android side. https://github.com/flutter/flutter/issues/119668
if (SdkCapabilityChecker.supportsEncoderProfiles() && getRecordingProfile() != null) {
mediaRecorderBuilder =
new MediaRecorderBuilder(
Expand Down Expand Up @@ -383,8 +386,7 @@ public void onOpened(@NonNull CameraDevice device) {
cameraDevice = new DefaultCameraDeviceWrapper(device);
try {
startPreview();
if (!recordingVideo) { // only send initialization if we werent already recording and
// switching cameras
if (!recordingVideo) { // only send initialization if we werent already recording and switching cameras
dartMessenger.sendCameraInitializedEvent(
resolutionFeature.getPreviewSize().getWidth(),
resolutionFeature.getPreviewSize().getHeight(),
Expand Down Expand Up @@ -468,10 +470,11 @@ private void createCaptureSession(

// Build Flutter surface to render to.
ResolutionFeature resolutionFeature = cameraFeatures.getResolution();
surfaceProducer.setSize(
SurfaceTexture surfaceTexture = flutterTexture.surfaceTexture();
surfaceTexture.setDefaultBufferSize(
resolutionFeature.getPreviewSize().getWidth(),
resolutionFeature.getPreviewSize().getHeight());
Surface flutterSurface = surfaceProducer.getSurface();
Surface flutterSurface = new Surface(surfaceTexture);
previewRequestBuilder.addTarget(flutterSurface);

List<Surface> remainingSurfaces = Arrays.asList(surfaces);
Expand Down Expand Up @@ -1157,8 +1160,7 @@ public void resumePreview() {
}

public void startPreview() throws CameraAccessException, InterruptedException {
// If recording is already in progress, the camera is being flipped, so send it through the
// VideoRenderer to keep the correct orientation.
// If recording is already in progress, the camera is being flipped, so send it through the VideoRenderer to keep the correct orientation.
if (recordingVideo) {
startPreviewWithVideoRendererStream();
} else {
Expand Down Expand Up @@ -1191,6 +1193,7 @@ private void startPreviewWithVideoRendererStream()
}

if (cameraProperties.getLensFacing() != initialCameraFacing) {

// If the new camera is facing the opposite way than the initial recording,
// the rotation should be flipped 180 degrees.
rotation = (rotation + 180) % 360;
Expand Down Expand Up @@ -1358,13 +1361,13 @@ public void uncaughtException(Thread thread, Throwable ex) {

public void setDescriptionWhileRecording(
@NonNull final Result result, CameraProperties properties) {

if (!recordingVideo) {
result.error("setDescriptionWhileRecordingFailed", "Device was not recording", null);
return;
}

// See VideoRenderer.java; support for this EGL extension is required to switch camera while
// recording.
// See VideoRenderer.java; support for this EGL extension is required to switch camera while recording.
if (!SdkCapabilityChecker.supportsEglRecordableAndroid()) {
result.error(
"setDescriptionWhileRecordingFailed",
Expand Down Expand Up @@ -1397,7 +1400,7 @@ public void dispose() {
Log.i(TAG, "dispose");

close();
surfaceProducer.release();
flutterTexture.release();
getDeviceOrientationManager().stop();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,25 +393,27 @@ private void instantiateCamera(MethodCall call, Result result) throws CameraAcce
Integer videoBitrate = call.argument("videoBitrate");
Integer audioBitrate = call.argument("audioBitrate");

TextureRegistry.SurfaceProducer surfaceProducer = textureRegistry.createSurfaceProducer();
TextureRegistry.SurfaceTextureEntry flutterSurfaceTexture =
textureRegistry.createSurfaceTexture();
DartMessenger dartMessenger =
new DartMessenger(messenger, surfaceProducer.id(), new Handler(Looper.getMainLooper()));
new DartMessenger(
messenger, flutterSurfaceTexture.id(), new Handler(Looper.getMainLooper()));
CameraProperties cameraProperties =
new CameraPropertiesImpl(cameraName, CameraUtils.getCameraManager(activity));
ResolutionPreset resolutionPreset = ResolutionPreset.valueOf(preset);

camera =
new Camera(
activity,
surfaceProducer,
flutterSurfaceTexture,
new CameraFeatureFactoryImpl(),
dartMessenger,
cameraProperties,
new Camera.VideoCaptureSettings(
resolutionPreset, enableAudio, fps, videoBitrate, audioBitrate));

Map<String, Object> reply = new HashMap<>();
reply.put("cameraId", surfaceProducer.id());
reply.put("cameraId", flutterSurfaceTexture.id());
result.success(reply);
}

Expand Down
Loading

0 comments on commit cc885ca

Please sign in to comment.