-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMainActivity.java
More file actions
1142 lines (994 loc) · 44.1 KB
/
MainActivity.java
File metadata and controls
1142 lines (994 loc) · 44.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2021 Mobile Robotics Lab. at Skoltech.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.googleresearch.capturesync;
import android.Manifest.permission;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.ImageFormat;
import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraDevice.StateCallback;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.CamcorderProfile;
import android.media.MediaCodec;
import android.media.MediaRecorder;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.util.Size;
import android.view.Gravity;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.googleresearch.capturesync.softwaresync.CSVLogger;
import com.googleresearch.capturesync.softwaresync.SoftwareSyncLeader;
import com.googleresearch.capturesync.softwaresync.TimeUtils;
import com.googleresearch.capturesync.softwaresync.phasealign.PeriodCalculator;
import com.googleresearch.capturesync.softwaresync.phasealign.PhaseConfig;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.FutureTask;
import java.util.stream.Collectors;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Main activity for the libsoftwaresync demo app using the camera 2 API.
*/
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private static final int STATIC_LEN = 15_000;
private String lastTimeStamp;
private PeriodCalculator periodCalculator;
public String getLastVideoPath() {
return lastVideoPath;
}
public void deleteUnusedVideo() {
String videoPath = getLastVideoPath();
File videoFile = new File(videoPath);
boolean result = videoFile.delete();
if (!result) {
Log.d(TAG, "Video file could not be deleted");
}
}
private String lastVideoPath;
public Integer getLastVideoSeqId() {
return lastVideoSeqId;
}
private Integer lastVideoSeqId;
public int getCurSequence() {
return curSequence;
}
public void setLogger(CSVLogger mLogger) {
this.mLogger = mLogger;
}
public CSVLogger getLogger() {
return mLogger;
}
private CSVLogger mLogger;
private int curSequence;
private static final String SUBDIR_NAME = "RecSync";
private boolean permissionsGranted = false;
// Phase config file to use for phase alignment, configs are located in the raw folder.
private final int phaseConfigFile = R.raw.default_phaseconfig;
public MediaRecorder getMediaRecorder() {
return mediaRecorder;
}
private MediaRecorder mediaRecorder = new MediaRecorder();
private boolean isVideoRecording = false;
// Camera controls.
private HandlerThread cameraThread;
private Handler cameraHandler;
private Handler send2aHandler;
private CameraManager cameraManager;
private String cameraId;
private CameraDevice cameraDevice;
private CameraCharacteristics cameraCharacteristics;
// Cached camera characteristics.
private Size viewfinderResolution;
private Size rawImageResolution;
private Size yuvImageResolution;
// Top level UI windows.
private int lastOrientation = Configuration.ORIENTATION_UNDEFINED;
// UI controls.
private Button captureStillButton;
private Button getPeriodButton;
private Button phaseAlignButton;
private SeekBar exposureSeekBar;
private SeekBar sensitivitySeekBar;
private TextView statusTextView;
private TextView sensorExposureTextView;
private TextView sensorSensitivityTextView;
private TextView softwaresyncStatusTextView;
private TextView phaseTextView;
// Local variables tracking current manual exposure and sensitivity values.
private long currentSensorExposureTimeNs = seekBarValueToExposureNs(10);
private int currentSensorSensitivity = seekBarValueToSensitivity(3);
// High level camera controls.
private CameraController cameraController;
private CameraCaptureSession captureSession;
/**
* Manages SoftwareSync setup/teardown. Since softwaresync should only run when the camera is
* running, it is instantiated in openCamera() and closed inside closeCamera().
*/
private SoftwareSyncController softwareSyncController;
private AutoFitSurfaceView surfaceView;
private final SurfaceHolder.Callback surfaceCallback =
new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i(TAG, "Surface created.");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.i(TAG, "Surface changed.");
viewfinderSurface = holder.getSurface();
openCamera();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i(TAG, "destroyed.");
}
};
private Surface viewfinderSurface;
private PhaseAlignController phaseAlignController;
private int numCaptures;
private Toast latestToast;
private Surface recorderSurface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate");
periodCalculator = new PeriodCalculator();
checkPermissions();
if (permissionsGranted) {
onCreateWithPermission();
} else {
// Wait for user to finish permissions before setting up the app.
}
}
private void onCreateWithPermission() {
setContentView(R.layout.activity_main);
send2aHandler = new Handler();
createUi();
setupPhaseAlignController();
// Query for camera characteristics and cache them.
try {
cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
cacheCameraCharacteristics();
} catch (CameraAccessException e) {
Toast.makeText(this, R.string.error_msg_cant_open_camera2, Toast.LENGTH_LONG).show();
Log.e(TAG, String.valueOf(R.string.error_msg_cant_open_camera2));
finish();
}
// Set the aspect ratio now that we know the viewfinder resolution.
surfaceView.setAspectRatio(viewfinderResolution.getWidth(), viewfinderResolution.getHeight());
// Process the initial configuration (for i.e. initial orientation)
// We need this because #onConfigurationChanged doesn't get called when
// the app launches
maybeUpdateConfiguration(getResources().getConfiguration());
}
private void setupPhaseAlignController() {
// Set up phase aligner.
PhaseConfig phaseConfig;
try {
phaseConfig = loadPhaseConfigFile();
} catch (JSONException e) {
throw new IllegalArgumentException("Error reading JSON file: ", e);
}
phaseAlignController = new PhaseAlignController(phaseConfig, this);
}
/**
* Called when "configuration" changes, as defined in the manifest. In our case, when the
* orientation changes, screen size changes, or keyboard is hidden.
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
maybeUpdateConfiguration(getResources().getConfiguration());
}
private void maybeUpdateConfiguration(Configuration newConfig) {
if (lastOrientation != newConfig.orientation) {
lastOrientation = newConfig.orientation;
updateViewfinderLayoutParams();
}
}
/**
* Resize the SurfaceView to be centered on screen.
*/
private void updateViewfinderLayoutParams() {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) surfaceView.getLayoutParams();
// displaySize is set by the OS: it's how big the display is.
Point displaySize = new Point();
getWindowManager().getDefaultDisplay().getRealSize(displaySize);
Log.i(TAG, String.format("display resized, now %d x %d", displaySize.x, displaySize.y));
// Fit an image inside a rectangle maximizing the resulting area and centering (coordinates are
// rounded down).
params.width =
Math.min(
displaySize.x,
displaySize.y * viewfinderResolution.getWidth() / viewfinderResolution.getHeight());
params.height =
Math.min(
displaySize.x * viewfinderResolution.getHeight() / viewfinderResolution.getWidth(),
displaySize.y);
params.gravity = Gravity.CENTER;
surfaceView.setLayoutParams(params);
}
@Override
public void onResume() {
Log.d(TAG, "onResume");
super.onResume(); // Required.
surfaceView
.getHolder()
.setFixedSize(viewfinderResolution.getWidth(), viewfinderResolution.getHeight());
surfaceView.getHolder().addCallback(surfaceCallback);
Log.d(TAG, "Surfaceview size: " + surfaceView.getWidth() + ", " + surfaceView.getHeight());
surfaceView.setVisibility(View.VISIBLE);
startCameraThread();
}
@Override
public void onPause() {
Log.d(TAG, "onPause");
closeCamera();
stopCameraThread();
// Make the SurfaceView GONE so that on resume, surfaceCreated() is called,
// and on pause, surfaceDestroyed() is called.
surfaceView.getHolder().removeCallback(surfaceCallback);
surfaceView.setVisibility(View.GONE);
super.onPause(); // required
}
private void startCameraThread() {
cameraThread = new HandlerThread("CameraThread");
cameraThread.start();
cameraHandler = new Handler(cameraThread.getLooper());
}
private void stopCameraThread() {
cameraThread.quitSafely();
try {
cameraThread.join();
cameraThread = null;
cameraHandler = null;
} catch (InterruptedException e) {
Log.e(TAG, "Failed to stop camera thread", e);
}
}
@SuppressLint("MissingPermission")
private void openCamera() {
try {
Log.d(TAG, "resumeCamera");
StateCallback cameraStateCallback =
new StateCallback() {
@Override
public void onOpened(CameraDevice openedCameraDevice) {
cameraDevice = openedCameraDevice;
startSoftwareSync();
initCameraController();
configureCaptureSession(); // calls startPreview();
}
@Override
public void onDisconnected(CameraDevice cameraDevice) {
}
@Override
public void onError(CameraDevice cameraDevice, int i) {
}
};
cameraManager.openCamera(cameraId, cameraStateCallback, cameraHandler); // Starts chain.
} catch (CameraAccessException e) {
Log.e(TAG, "Cannot open camera!", e);
finish();
}
}
public void onTimestampNs(long timestampNs) {
periodCalculator.onFrameTimestamp(timestampNs);
}
/* Set up UI controls and listeners based on if device is currently a leader of client. */
private void setLeaderClientControls(boolean isLeader) {
getPeriodButton.setOnClickListener(
view -> {
Log.d(TAG, "Calculating frames period.");
FutureTask<Integer> periodTask = new FutureTask<Integer>(
() -> {
try {
long periodNs = periodCalculator.getPeriodNs();
Log.d(TAG, "Calculated period: " + periodNs);
if (latestToast != null) {
latestToast.cancel();
}
latestToast =
Toast.makeText(
this,
"Calculated period: " + periodNs,
Toast.LENGTH_LONG);
latestToast.show();
phaseAlignController.setPeriodNs(periodNs);
} catch (InterruptedException e) {
Log.d(TAG, "Failed calculating period");
e.printStackTrace();
}
return 0;
}
);
periodTask.run();
}
);
if (isLeader) {
// Leader, all controls visible and set.
captureStillButton.setVisibility(View.VISIBLE);
phaseAlignButton.setVisibility(View.VISIBLE);
getPeriodButton.setVisibility(View.VISIBLE);
exposureSeekBar.setVisibility(View.VISIBLE);
sensitivitySeekBar.setVisibility(View.VISIBLE);
captureStillButton.setOnClickListener(
view -> {
if (isVideoRecording) {
stopVideo();
((SoftwareSyncLeader) softwareSyncController.softwareSync)
.broadcastRpc(
SoftwareSyncController.METHOD_STOP_RECORDING,
"0");
} else {
startVideo(false);
((SoftwareSyncLeader) softwareSyncController.softwareSync)
.broadcastRpc(
SoftwareSyncController.METHOD_START_RECORDING,
"0");
}
/* if (cameraController.getOutputSurfaces().isEmpty()) {
Log.e(TAG, "No output surfaces found.");
Toast.makeText(this, R.string.error_msg_no_outputs, Toast.LENGTH_LONG).show();
return;
}
long currentTimestamp = softwareSyncController.softwareSync.getLeaderTimeNs();
// Trigger request some time in the future (~500ms for example) so all devices have time
// to receive the request (delayed due to network latency) and prepare for triggering.
// Note: If the user keeps a running circular buffer of images, they can select frames
// in the near past as well, allowing for 'instantaneous' captures on all devices.
long futureTimestamp = currentTimestamp + Constants.FUTURE_TRIGGER_DELAY_NS;
Log.d(
TAG,
String.format(
"Trigger button, sending timestamp %,d at %,d",
futureTimestamp, currentTimestamp));
// Broadcast desired synchronized capture time to all devices.
((SoftwareSyncLeader) softwareSyncController.softwareSync)
.broadcastRpc(
SoftwareSyncController.METHOD_SET_TRIGGER_TIME,
String.valueOf(futureTimestamp));*/
});
phaseAlignButton.setOnClickListener(
view -> {
Log.d(TAG, "Broadcasting phase alignment request.");
// Request phase alignment on all devices.
((SoftwareSyncLeader) softwareSyncController.softwareSync)
.broadcastRpc(SoftwareSyncController.METHOD_DO_PHASE_ALIGN, "");
});
exposureSeekBar.setOnSeekBarChangeListener(
new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int value, boolean fromUser) {
currentSensorExposureTimeNs = seekBarValueToExposureNs(value);
sensorExposureTextView.setText(
"Exposure: " + prettyExposureValue(currentSensorExposureTimeNs));
Log.i(
TAG,
"Exposure Seekbar "
+ value
+ " to set exposure "
+ currentSensorExposureTimeNs
+ " : "
+ prettyExposureValue(currentSensorExposureTimeNs));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Do it immediately on leader for immediate feedback, but doesn't update clients
// without
// clicking the 2A button.
startPreview();
scheduleBroadcast2a();
}
});
sensitivitySeekBar.setOnSeekBarChangeListener(
new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int value, boolean fromUser) {
currentSensorSensitivity = seekBarValueToSensitivity(value);
sensorSensitivityTextView.setText("Sensitivity: " + currentSensorSensitivity);
Log.i(
TAG,
"Sensitivity Seekbar "
+ value
+ " to set sensitivity "
+ currentSensorSensitivity);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Do it immediately on leader for immediate feedback, but doesn't update clients
// without
// clicking the 2A button.
startPreview();
scheduleBroadcast2a();
}
});
} else {
// Client. All controls invisible.
captureStillButton.setVisibility(View.INVISIBLE);
phaseAlignButton.setVisibility(View.INVISIBLE);
getPeriodButton.setVisibility(View.VISIBLE);
exposureSeekBar.setVisibility(View.INVISIBLE);
sensitivitySeekBar.setVisibility(View.INVISIBLE);
captureStillButton.setOnClickListener(null);
phaseAlignButton.setOnClickListener(null);
exposureSeekBar.setOnSeekBarChangeListener(null);
sensitivitySeekBar.setOnSeekBarChangeListener(null);
}
}
private void startSoftwareSync() {
// Start softwaresync, close it first if it's already running.
if (softwareSyncController != null) {
softwareSyncController.close();
softwareSyncController = null;
}
try {
softwareSyncController =
new SoftwareSyncController(this, phaseAlignController, softwaresyncStatusTextView);
setLeaderClientControls(softwareSyncController.isLeader());
} catch (IllegalStateException e) {
// If wifi is disabled, start pick wifi activity.
Log.e(
TAG,
"Couldn't start SoftwareSync due to " + e + ", requesting user pick a wifi network.");
finish(); // Close current app, expect user to restart.
startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
}
}
private PhaseConfig loadPhaseConfigFile() throws JSONException {
// Load phase config file and pass to phase aligner.
JSONObject json;
try {
InputStream inputStream = getResources().openRawResource(phaseConfigFile);
byte[] buffer = new byte[inputStream.available()];
//noinspection ResultOfMethodCallIgnored
inputStream.read(buffer);
inputStream.close();
json = new JSONObject(new String(buffer, StandardCharsets.UTF_8));
} catch (JSONException | IOException e) {
throw new IllegalArgumentException("Error reading JSON file: ", e);
}
return PhaseConfig.parseFromJSON(json);
}
private void closeCamera() {
stopPreview();
captureSession = null;
recorderSurface.release();
if (cameraController != null) {
cameraController.close();
cameraController = null;
}
if (cameraDevice != null) {
Log.d(TAG, "Closing camera...");
cameraDevice.close();
Log.d(TAG, "Camera closed.");
}
// Close softwaresync whenever camera is stopped.
if (softwareSyncController != null) {
softwareSyncController.close();
softwareSyncController = null;
}
}
/**
* Gathers useful camera characteristics like available resolutions and cache them so we don't
* have to query the CameraCharacteristics struct again.
*/
private void cacheCameraCharacteristics() throws CameraAccessException {
cameraId = null;
for (String id : cameraManager.getCameraIdList()) {
if (cameraManager.getCameraCharacteristics(id).get(CameraCharacteristics.LENS_FACING)
== Constants.DEFAULT_CAMERA_FACING) {
cameraId = id;
break;
}
}
cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
StreamConfigurationMap scm =
cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
// We always capture the viewfinder. Its resolution is special: it's set chosen in Constants.
List<Size> viewfinderOutputSizes = Arrays.stream(scm.getOutputSizes(SurfaceTexture.class)).filter(
size -> size.getHeight() <= 1920 && size.getWidth() <= 1080
).collect(Collectors.toList());
if (viewfinderOutputSizes.size() != 0) {
Log.i(TAG, "Available viewfinder resolutions:");
for (Size s : viewfinderOutputSizes) {
Log.i(TAG, s.toString());
}
} else {
throw new IllegalStateException("Viewfinder unavailable!");
}
// TODO: max
viewfinderResolution =
Collections.max(viewfinderOutputSizes, new CompareSizesByArea());
Size[] rawOutputSizes = scm.getOutputSizes(ImageFormat.RAW10);
// if (rawOutputSizes != null) {
// Log.i(TAG, "Available Bayer RAW resolutions:");
// for (Size s : rawOutputSizes) {
// Log.i(TAG, s.toString());
// }
// } else {
// Log.i(TAG, "Bayer RAW unavailable!");
// }
// rawImageResolution = Collections.max(Arrays.asList(rawOutputSizes), new CompareSizesByArea());
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
List<Size> yuvOutputSizes = Arrays.stream(scm.getOutputSizes(ImageFormat.YUV_420_888)).filter(
size -> size.getHeight() <= profile.videoFrameHeight && size.getWidth() <= profile.videoFrameWidth
).collect(Collectors.toList());
if (yuvOutputSizes.size() != 0) {
Log.i(TAG, "Available YUV resolutions:");
for (Size s : yuvOutputSizes) {
Log.i(TAG, s.toString());
}
} else {
Log.i(TAG, "YUV unavailable!");
}
yuvImageResolution = Collections.max(yuvOutputSizes, new CompareSizesByArea());
Log.i(TAG, "Chosen viewfinder resolution: " + viewfinderResolution);
// Log.i(TAG, "Chosen raw resolution: " + rawImageResolution);
Log.i(TAG, "Chosen yuv resolution: " + yuvImageResolution);
}
public void setUpcomingCaptureStill(long upcomingTriggerTimeNs) {
cameraController.setUpcomingCaptureStill(upcomingTriggerTimeNs);
double timeTillSec =
TimeUtils.nanosToSeconds(
(double)
(upcomingTriggerTimeNs - softwareSyncController.softwareSync.getLeaderTimeNs()));
runOnUiThread(
() -> {
if (latestToast != null) {
latestToast.cancel();
}
latestToast =
Toast.makeText(
this,
String.format("Capturing in %.2f seconds", timeTillSec),
Toast.LENGTH_SHORT);
latestToast.show();
});
}
public void notifyCapturing(String name) {
runOnUiThread(
() -> {
if (latestToast != null) {
latestToast.cancel();
}
latestToast = Toast.makeText(this, "Capturing " + name + "...", Toast.LENGTH_SHORT);
latestToast.show();
});
}
public void notifyCaptured(String name) {
numCaptures++;
runOnUiThread(
() -> {
if (latestToast != null) {
latestToast.cancel();
}
latestToast = Toast.makeText(this, "Captured " + name, Toast.LENGTH_LONG);
latestToast.show();
statusTextView.setText(String.format("%d captures", numCaptures));
});
}
/**
* Compares two {@code Size}s based on their areas.
*/
static class CompareSizesByArea implements Comparator<Size> {
@Override
public int compare(Size lhs, Size rhs) {
// We cast here to ensure the multiplications won't overflow
return Long.signum(
(long) lhs.getWidth() * lhs.getHeight() - (long) rhs.getWidth() * rhs.getHeight());
}
}
public void injectFrame(long desiredExposureTimeNs) {
try {
CaptureRequest.Builder builder =
cameraController
.getRequestFactory()
.makeFrameInjectionRequest(
desiredExposureTimeNs, cameraController.getOutputSurfaces());
captureSession.capture(
builder.build(), cameraController.getSynchronizerCaptureCallback(), cameraHandler);
} catch (CameraAccessException e) {
throw new IllegalStateException("Camera capture failure during frame injection.", e);
}
}
private void createUi() {
Window appWindow = getWindow();
appWindow.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Disable sleep / screen off.
appWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Create the SurfaceView.
surfaceView = findViewById(R.id.viewfinder_surface_view);
// TextViews.
statusTextView = findViewById(R.id.status_text);
softwaresyncStatusTextView = findViewById(R.id.softwaresync_text);
sensorExposureTextView = findViewById(R.id.sensor_exposure);
sensorSensitivityTextView = findViewById(R.id.sensor_sensitivity);
phaseTextView = findViewById(R.id.phase);
// Controls.
captureStillButton = findViewById(R.id.capture_still_button);
phaseAlignButton = findViewById(R.id.phase_align_button);
getPeriodButton = findViewById(R.id.get_period_button);
exposureSeekBar = findViewById(R.id.exposure_seekbar);
sensitivitySeekBar = findViewById(R.id.sensitivity_seekbar);
sensorExposureTextView.setText("Exposure: " + prettyExposureValue(currentSensorExposureTimeNs));
sensorSensitivityTextView.setText("Sensitivity: " + currentSensorSensitivity);
}
private void scheduleBroadcast2a() {
send2aHandler.removeCallbacks(null); // Replace delayed callback with latest 2a values.
send2aHandler.postDelayed(
() -> {
Log.d(TAG, "Broadcasting current 2A values.");
String payload =
String.format("%d,%d", currentSensorExposureTimeNs, currentSensorSensitivity);
// Send 2A values to all devices
((SoftwareSyncLeader) softwareSyncController.softwareSync)
.broadcastRpc(SoftwareSyncController.METHOD_SET_2A, payload);
},
500);
}
void set2aAndUpdatePreview(long sensorExposureTimeNs, int sensorSensitivity) {
currentSensorExposureTimeNs = sensorExposureTimeNs;
currentSensorSensitivity = sensorSensitivity;
sensorExposureTextView.setText("Exposure: " + prettyExposureValue(currentSensorExposureTimeNs));
sensorSensitivityTextView.setText("Sensitivity: " + currentSensorSensitivity);
Log.i(
TAG,
String.format(
" Updating 2A to Exposure %d (%s), Sensitivity %d",
currentSensorExposureTimeNs,
prettyExposureValue(currentSensorExposureTimeNs),
currentSensorSensitivity));
startPreview();
}
void updatePhaseTextView(long phaseErrorNs) {
phaseTextView.setText(
String.format("Phase Error: %.2f ms", TimeUtils.nanosToMillis((double) phaseErrorNs)));
}
private long seekBarValueToExposureNs(int value) {
// Convert 0-10 values ranging from 1/32 to 1/16,000 of a second.
int[] steps = {32, 60, 125, 250, 500, 1000, 2000, 4000, 8000, 12000, 16000};
int denominator = steps[10 - value];
double exposureSec = 1. / denominator;
return (long) (exposureSec * 1_000_000_000);
}
private String prettyExposureValue(long exposureNs) {
return String.format("1/%.0f", 1. / TimeUtils.nanosToSeconds((double) exposureNs));
}
private int seekBarValueToSensitivity(int value) {
// Convert 0-10 values to 0-800 sensor sensitivities.
return (value * 800) / 10;
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
if (Constants.USE_FULL_SCREEN_IMMERSIVE) {
findViewById(android.R.id.content)
.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
}
/**
* Create {@link #cameraController}, and subscribe to status change events.
*/
private void initCameraController() {
cameraController =
new CameraController(
cameraCharacteristics,
Constants.SAVE_RAW ? rawImageResolution : null,
Constants.SAVE_YUV ? yuvImageResolution : null,
phaseAlignController,
this,
softwareSyncController.softwareSync);
}
private void configureCaptureSession() {
Log.d(TAG, "Creating capture session.");
List<Surface> outputSurfaces = new ArrayList<>();
Log.d(TAG, "Surfaceview size: " + surfaceView.getWidth() + ", " + surfaceView.getHeight());
Log.d(TAG, "viewfinderSurface valid? " + viewfinderSurface.isValid());
outputSurfaces.add(viewfinderSurface);
// MROB. Added MediaRecorder surface
try {
createRecorderSurface();
outputSurfaces.add(recorderSurface);
} catch (IOException e) {
e.printStackTrace();
}
outputSurfaces.addAll(cameraController.getOutputSurfaces());
if (cameraController.getOutputSurfaces().isEmpty()) {
Log.e(TAG, "No output surfaces found.");
}
Log.d(TAG, "Outputs " + cameraController.getOutputSurfaces());
try {
CameraCaptureSession.StateCallback sessionCallback =
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession cameraCaptureSession) {
Log.d(TAG, "camera capture configured.");
captureSession = cameraCaptureSession;
cameraController.configure(cameraDevice); // pass in device.
startPreview();
}
@Override
public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
Log.d(TAG, "camera capture configure failed.");
}
};
cameraDevice.createCaptureSession(outputSurfaces, sessionCallback, cameraHandler);
} catch (CameraAccessException e) {
Log.e(TAG, "Unable to reconfigure capture request", e);
}
}
private void startPreview(boolean wantAutoExp) {
Log.d(TAG, "Starting preview.");
try {
CaptureRequest.Builder previewRequestBuilder =
cameraController
.getRequestFactory()
.makePreview(
viewfinderSurface,
cameraController.getOutputSurfaces(),
currentSensorExposureTimeNs,
currentSensorSensitivity, wantAutoExp);
captureSession.stopRepeating();
captureSession.setRepeatingRequest(
previewRequestBuilder.build(),
cameraController.getSynchronizerCaptureCallback(),
cameraHandler);
} catch (CameraAccessException e) {
Log.w(TAG, "Unable to create preview.");
}
}
private void startPreview() {
startPreview(false);
}
/**
* Create directory and return file
* returning video file
*/
private String getOutputMediaFilePath() throws IOException {
// // External sdcard file location
// File mediaStorageDir = new File(Environment.getExternalStorageDirectory(),
// "MROB_VID");
// // Create storage directory if it does not exist
// if (!mediaStorageDir.exists()) {
// if (!mediaStorageDir.mkdirs()) {
// Log.d(TAG, "Oops! Failed create "
// + "MROB_VID" + " directory");
// return null;
// }
// }
File sdcard = Environment.getExternalStorageDirectory();
Path dir = Files.createDirectories(Paths.get(sdcard.getAbsolutePath(), SUBDIR_NAME, "VID"));
lastTimeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
String mediaFile;
mediaFile = dir.toString() + File.separator + "VID_" + lastTimeStamp + ".mp4";
return mediaFile;
}
private void createRecorderSurface() throws IOException {
recorderSurface = MediaCodec.createPersistentInputSurface();
MediaRecorder recorder = setUpMediaRecorder(recorderSurface, false);
recorder.prepare();
recorder.release();
deleteUnusedVideo();
}
private MediaRecorder setUpMediaRecorder(Surface surface) throws IOException {
return setUpMediaRecorder(surface, true);
}
private MediaRecorder setUpMediaRecorder(Surface surface, boolean specifyOutput) throws IOException {
MediaRecorder recorder = new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
/*
* create video output file
*/
/*
* set output file in media recorder
*/
lastVideoPath = getOutputMediaFilePath();
recorder.setOutputFile(lastVideoPath);
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_1080P);
recorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
recorder.setVideoEncodingBitRate(profile.videoBitRate);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
// int rotation = getWindowManager().getDefaultDisplay().getRotation();
/* switch (mSensorOrientation) {
case SENSOR_ORIENTATION_INVERSE_DEGREES:
mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
break;
}*/
recorder.setInputSurface(surface);