9
9
import android .app .Activity ;
10
10
import android .content .Context ;
11
11
import android .graphics .ImageFormat ;
12
+ import android .graphics .SurfaceTexture ;
12
13
import android .hardware .camera2 .CameraAccessException ;
13
14
import android .hardware .camera2 .CameraCaptureSession ;
14
15
import android .hardware .camera2 .CameraDevice ;
62
63
import io .flutter .plugins .camera .media .MediaRecorderBuilder ;
63
64
import io .flutter .plugins .camera .types .CameraCaptureProperties ;
64
65
import io .flutter .plugins .camera .types .CaptureTimeoutsWrapper ;
65
- import io .flutter .view .TextureRegistry ;
66
+ import io .flutter .view .TextureRegistry . SurfaceTextureEntry ;
66
67
import java .io .File ;
67
68
import java .io .IOException ;
68
69
import java .util .ArrayList ;
@@ -112,7 +113,7 @@ class Camera
112
113
*/
113
114
@ VisibleForTesting int initialCameraFacing ;
114
115
115
- @ VisibleForTesting final TextureRegistry . SurfaceProducer surfaceProducer ;
116
+ @ VisibleForTesting final SurfaceTextureEntry flutterTexture ;
116
117
private final VideoCaptureSettings videoCaptureSettings ;
117
118
private final Context applicationContext ;
118
119
final DartMessenger dartMessenger ;
@@ -213,16 +214,17 @@ public VideoCaptureSettings(@NonNull ResolutionPreset resolutionPreset, boolean
213
214
214
215
public Camera (
215
216
final Activity activity ,
216
- final TextureRegistry . SurfaceProducer surfaceProducer ,
217
+ final SurfaceTextureEntry flutterTexture ,
217
218
final CameraFeatureFactory cameraFeatureFactory ,
218
219
final DartMessenger dartMessenger ,
219
220
final CameraProperties cameraProperties ,
220
221
final VideoCaptureSettings videoCaptureSettings ) {
222
+
221
223
if (activity == null ) {
222
224
throw new IllegalStateException ("No activity available!" );
223
225
}
224
226
this .activity = activity ;
225
- this .surfaceProducer = surfaceProducer ;
227
+ this .flutterTexture = flutterTexture ;
226
228
this .dartMessenger = dartMessenger ;
227
229
this .applicationContext = activity .getApplicationContext ();
228
230
this .cameraProperties = cameraProperties ;
@@ -241,6 +243,7 @@ public Camera(
241
243
if (videoCaptureSettings .fps != null && videoCaptureSettings .fps .intValue () > 0 ) {
242
244
recordingFps = videoCaptureSettings .fps ;
243
245
} else {
246
+
244
247
if (SdkCapabilityChecker .supportsEncoderProfiles ()) {
245
248
EncoderProfiles encoderProfiles = getRecordingProfile ();
246
249
if (encoderProfiles != null && encoderProfiles .getVideoProfiles ().size () > 0 ) {
@@ -253,6 +256,7 @@ public Camera(
253
256
}
254
257
255
258
if (recordingFps != null && recordingFps .intValue () > 0 ) {
259
+
256
260
final FpsRangeFeature fpsRange = new FpsRangeFeature (cameraProperties );
257
261
fpsRange .setValue (new Range <Integer >(recordingFps , recordingFps ));
258
262
this .cameraFeatures .setFpsRange (fpsRange );
@@ -303,9 +307,8 @@ private void prepareMediaRecorder(String outputFilePath) throws IOException {
303
307
304
308
MediaRecorderBuilder mediaRecorderBuilder ;
305
309
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
309
312
if (SdkCapabilityChecker .supportsEncoderProfiles () && getRecordingProfile () != null ) {
310
313
mediaRecorderBuilder =
311
314
new MediaRecorderBuilder (
@@ -383,8 +386,7 @@ public void onOpened(@NonNull CameraDevice device) {
383
386
cameraDevice = new DefaultCameraDeviceWrapper (device );
384
387
try {
385
388
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
388
390
dartMessenger .sendCameraInitializedEvent (
389
391
resolutionFeature .getPreviewSize ().getWidth (),
390
392
resolutionFeature .getPreviewSize ().getHeight (),
@@ -468,10 +470,11 @@ private void createCaptureSession(
468
470
469
471
// Build Flutter surface to render to.
470
472
ResolutionFeature resolutionFeature = cameraFeatures .getResolution ();
471
- surfaceProducer .setSize (
473
+ SurfaceTexture surfaceTexture = flutterTexture .surfaceTexture ();
474
+ surfaceTexture .setDefaultBufferSize (
472
475
resolutionFeature .getPreviewSize ().getWidth (),
473
476
resolutionFeature .getPreviewSize ().getHeight ());
474
- Surface flutterSurface = surfaceProducer . getSurface ( );
477
+ Surface flutterSurface = new Surface ( surfaceTexture );
475
478
previewRequestBuilder .addTarget (flutterSurface );
476
479
477
480
List <Surface > remainingSurfaces = Arrays .asList (surfaces );
@@ -1157,8 +1160,7 @@ public void resumePreview() {
1157
1160
}
1158
1161
1159
1162
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.
1162
1164
if (recordingVideo ) {
1163
1165
startPreviewWithVideoRendererStream ();
1164
1166
} else {
@@ -1191,6 +1193,7 @@ private void startPreviewWithVideoRendererStream()
1191
1193
}
1192
1194
1193
1195
if (cameraProperties .getLensFacing () != initialCameraFacing ) {
1196
+
1194
1197
// If the new camera is facing the opposite way than the initial recording,
1195
1198
// the rotation should be flipped 180 degrees.
1196
1199
rotation = (rotation + 180 ) % 360 ;
@@ -1358,13 +1361,13 @@ public void uncaughtException(Thread thread, Throwable ex) {
1358
1361
1359
1362
public void setDescriptionWhileRecording (
1360
1363
@ NonNull final Result result , CameraProperties properties ) {
1364
+
1361
1365
if (!recordingVideo ) {
1362
1366
result .error ("setDescriptionWhileRecordingFailed" , "Device was not recording" , null );
1363
1367
return ;
1364
1368
}
1365
1369
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.
1368
1371
if (!SdkCapabilityChecker .supportsEglRecordableAndroid ()) {
1369
1372
result .error (
1370
1373
"setDescriptionWhileRecordingFailed" ,
@@ -1397,7 +1400,7 @@ public void dispose() {
1397
1400
Log .i (TAG , "dispose" );
1398
1401
1399
1402
close ();
1400
- surfaceProducer .release ();
1403
+ flutterTexture .release ();
1401
1404
getDeviceOrientationManager ().stop ();
1402
1405
}
1403
1406
0 commit comments