99import android .app .Activity ;
1010import android .content .Context ;
1111import android .graphics .ImageFormat ;
12+ import android .graphics .SurfaceTexture ;
1213import android .hardware .camera2 .CameraAccessException ;
1314import android .hardware .camera2 .CameraCaptureSession ;
1415import android .hardware .camera2 .CameraDevice ;
6263import io .flutter .plugins .camera .media .MediaRecorderBuilder ;
6364import io .flutter .plugins .camera .types .CameraCaptureProperties ;
6465import io .flutter .plugins .camera .types .CaptureTimeoutsWrapper ;
65- import io .flutter .view .TextureRegistry ;
66+ import io .flutter .view .TextureRegistry . SurfaceTextureEntry ;
6667import java .io .File ;
6768import java .io .IOException ;
6869import java .util .ArrayList ;
@@ -112,7 +113,7 @@ class Camera
112113 */
113114 @ VisibleForTesting int initialCameraFacing ;
114115
115- @ VisibleForTesting final TextureRegistry . SurfaceProducer surfaceProducer ;
116+ @ VisibleForTesting final SurfaceTextureEntry flutterTexture ;
116117 private final VideoCaptureSettings videoCaptureSettings ;
117118 private final Context applicationContext ;
118119 final DartMessenger dartMessenger ;
@@ -213,16 +214,17 @@ public VideoCaptureSettings(@NonNull ResolutionPreset resolutionPreset, boolean
213214
214215 public Camera (
215216 final Activity activity ,
216- final TextureRegistry . SurfaceProducer surfaceProducer ,
217+ final SurfaceTextureEntry flutterTexture ,
217218 final CameraFeatureFactory cameraFeatureFactory ,
218219 final DartMessenger dartMessenger ,
219220 final CameraProperties cameraProperties ,
220221 final VideoCaptureSettings videoCaptureSettings ) {
222+
221223 if (activity == null ) {
222224 throw new IllegalStateException ("No activity available!" );
223225 }
224226 this .activity = activity ;
225- this .surfaceProducer = surfaceProducer ;
227+ this .flutterTexture = flutterTexture ;
226228 this .dartMessenger = dartMessenger ;
227229 this .applicationContext = activity .getApplicationContext ();
228230 this .cameraProperties = cameraProperties ;
@@ -241,6 +243,7 @@ public Camera(
241243 if (videoCaptureSettings .fps != null && videoCaptureSettings .fps .intValue () > 0 ) {
242244 recordingFps = videoCaptureSettings .fps ;
243245 } else {
246+
244247 if (SdkCapabilityChecker .supportsEncoderProfiles ()) {
245248 EncoderProfiles encoderProfiles = getRecordingProfile ();
246249 if (encoderProfiles != null && encoderProfiles .getVideoProfiles ().size () > 0 ) {
@@ -253,6 +256,7 @@ public Camera(
253256 }
254257
255258 if (recordingFps != null && recordingFps .intValue () > 0 ) {
259+
256260 final FpsRangeFeature fpsRange = new FpsRangeFeature (cameraProperties );
257261 fpsRange .setValue (new Range <Integer >(recordingFps , recordingFps ));
258262 this .cameraFeatures .setFpsRange (fpsRange );
@@ -303,9 +307,8 @@ private void prepareMediaRecorder(String outputFilePath) throws IOException {
303307
304308 MediaRecorderBuilder mediaRecorderBuilder ;
305309
306- // TODO(camsim99): Revert changes that allow legacy code to be used when recordingProfile is
307- // null once this has largely been fixed on the Android side.
308- // https://github.com/flutter/flutter/issues/119668
310+ // TODO(camsim99): Revert changes that allow legacy code to be used when recordingProfile is null
311+ // once this has largely been fixed on the Android side. https://github.com/flutter/flutter/issues/119668
309312 if (SdkCapabilityChecker .supportsEncoderProfiles () && getRecordingProfile () != null ) {
310313 mediaRecorderBuilder =
311314 new MediaRecorderBuilder (
@@ -383,8 +386,7 @@ public void onOpened(@NonNull CameraDevice device) {
383386 cameraDevice = new DefaultCameraDeviceWrapper (device );
384387 try {
385388 startPreview ();
386- if (!recordingVideo ) { // only send initialization if we werent already recording and
387- // switching cameras
389+ if (!recordingVideo ) { // only send initialization if we werent already recording and switching cameras
388390 dartMessenger .sendCameraInitializedEvent (
389391 resolutionFeature .getPreviewSize ().getWidth (),
390392 resolutionFeature .getPreviewSize ().getHeight (),
@@ -468,10 +470,11 @@ private void createCaptureSession(
468470
469471 // Build Flutter surface to render to.
470472 ResolutionFeature resolutionFeature = cameraFeatures .getResolution ();
471- surfaceProducer .setSize (
473+ SurfaceTexture surfaceTexture = flutterTexture .surfaceTexture ();
474+ surfaceTexture .setDefaultBufferSize (
472475 resolutionFeature .getPreviewSize ().getWidth (),
473476 resolutionFeature .getPreviewSize ().getHeight ());
474- Surface flutterSurface = surfaceProducer . getSurface ( );
477+ Surface flutterSurface = new Surface ( surfaceTexture );
475478 previewRequestBuilder .addTarget (flutterSurface );
476479
477480 List <Surface > remainingSurfaces = Arrays .asList (surfaces );
@@ -1157,8 +1160,7 @@ public void resumePreview() {
11571160 }
11581161
11591162 public void startPreview () throws CameraAccessException , InterruptedException {
1160- // If recording is already in progress, the camera is being flipped, so send it through the
1161- // VideoRenderer to keep the correct orientation.
1163+ // If recording is already in progress, the camera is being flipped, so send it through the VideoRenderer to keep the correct orientation.
11621164 if (recordingVideo ) {
11631165 startPreviewWithVideoRendererStream ();
11641166 } else {
@@ -1191,6 +1193,7 @@ private void startPreviewWithVideoRendererStream()
11911193 }
11921194
11931195 if (cameraProperties .getLensFacing () != initialCameraFacing ) {
1196+
11941197 // If the new camera is facing the opposite way than the initial recording,
11951198 // the rotation should be flipped 180 degrees.
11961199 rotation = (rotation + 180 ) % 360 ;
@@ -1358,13 +1361,13 @@ public void uncaughtException(Thread thread, Throwable ex) {
13581361
13591362 public void setDescriptionWhileRecording (
13601363 @ NonNull final Result result , CameraProperties properties ) {
1364+
13611365 if (!recordingVideo ) {
13621366 result .error ("setDescriptionWhileRecordingFailed" , "Device was not recording" , null );
13631367 return ;
13641368 }
13651369
1366- // See VideoRenderer.java; support for this EGL extension is required to switch camera while
1367- // recording.
1370+ // See VideoRenderer.java; support for this EGL extension is required to switch camera while recording.
13681371 if (!SdkCapabilityChecker .supportsEglRecordableAndroid ()) {
13691372 result .error (
13701373 "setDescriptionWhileRecordingFailed" ,
@@ -1397,7 +1400,7 @@ public void dispose() {
13971400 Log .i (TAG , "dispose" );
13981401
13991402 close ();
1400- surfaceProducer .release ();
1403+ flutterTexture .release ();
14011404 getDeviceOrientationManager ().stop ();
14021405 }
14031406
0 commit comments