Skip to content

Commit 8140fb2

Browse files
committed
Phase calculation setup
1 parent f76be0f commit 8140fb2

File tree

5 files changed

+170
-103
lines changed

5 files changed

+170
-103
lines changed

app/src/main/java/com/googleresearch/capturesync/CameraController.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.googleresearch.capturesync.ImageMetadataSynchronizer.CaptureRequestTag;
3232
import com.googleresearch.capturesync.softwaresync.TimeDomainConverter;
3333
import com.googleresearch.capturesync.softwaresync.TimeUtils;
34+
import com.googleresearch.capturesync.softwaresync.phasealign.PeriodCalculator;
3435

3536
import java.io.IOException;
3637
import java.text.SimpleDateFormat;
@@ -71,6 +72,8 @@ public CaptureCallback getSynchronizerCaptureCallback() {
7172

7273
private String goalOutputDirName;
7374

75+
private PeriodCalculator periodCalculator;
76+
7477
private CaptureRequestFactory requestFactory;
7578

7679
/**
@@ -88,8 +91,8 @@ public CameraController(
8891
Size yuvImageResolution,
8992
PhaseAlignController phaseAlignController,
9093
MainActivity context,
91-
TimeDomainConverter timeDomainConverter) {
92-
94+
TimeDomainConverter timeDomainConverter
95+
) {
9396
imageThread = new HandlerThread("ImageThread");
9497
imageThread.start();
9598
imageHandler = new Handler(imageThread.getLooper());
@@ -135,9 +138,11 @@ public CameraController(
135138
// }
136139

137140
int sequenceId = result.getSequenceId();
141+
long unSyncTimestampNs = result.get(CaptureResult.SENSOR_TIMESTAMP);
142+
context.onTimestampNs(unSyncTimestampNs);
138143
long synchronizedTimestampNs =
139144
timeDomainConverter.leaderTimeForLocalTimeNs(
140-
result.get(CaptureResult.SENSOR_TIMESTAMP));
145+
unSyncTimestampNs);
141146

142147
double timestampMs = TimeUtils.nanosToMillis((double) synchronizedTimestampNs);
143148
double frameDurationMs =

app/src/main/java/com/googleresearch/capturesync/MainActivity.java

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.googleresearch.capturesync.softwaresync.CSVLogger;
6161
import com.googleresearch.capturesync.softwaresync.SoftwareSyncLeader;
6262
import com.googleresearch.capturesync.softwaresync.TimeUtils;
63+
import com.googleresearch.capturesync.softwaresync.phasealign.PeriodCalculator;
6364
import com.googleresearch.capturesync.softwaresync.phasealign.PhaseConfig;
6465

6566
import java.io.File;
@@ -77,23 +78,29 @@
7778
import java.util.Date;
7879
import java.util.List;
7980
import java.util.Locale;
81+
import java.util.concurrent.Future;
82+
import java.util.concurrent.FutureTask;
8083
import java.util.stream.Collectors;
8184

8285
import org.json.JSONException;
8386
import org.json.JSONObject;
8487

85-
/** Main activity for the libsoftwaresync demo app using the camera 2 API. */
88+
/**
89+
* Main activity for the libsoftwaresync demo app using the camera 2 API.
90+
*/
8691
public class MainActivity extends Activity {
8792
private static final String TAG = "MainActivity";
8893
// private static final int EXP_LEN = 20_000;
8994
private static final int STATIC_LEN = 15_000;
9095
private String lastTimeStamp;
96+
private PeriodCalculator periodCalculator;
9197

9298
public Integer getLastVideoSeqId() {
9399
return lastVideoSeqId;
94100
}
95101

96102
private Integer lastVideoSeqId;
103+
97104
public int getCurSequence() {
98105
return curSequence;
99106
}
@@ -144,6 +151,7 @@ public MediaRecorder getMediaRecorder() {
144151

145152
// UI controls.
146153
private Button captureStillButton;
154+
private Button getPeriodButton;
147155
private Button phaseAlignButton;
148156
private SeekBar exposureSeekBar;
149157
private SeekBar sensitivitySeekBar;
@@ -200,7 +208,7 @@ public void surfaceDestroyed(SurfaceHolder holder) {
200208
protected void onCreate(Bundle savedInstanceState) {
201209
super.onCreate(savedInstanceState);
202210
Log.v(TAG, "onCreate");
203-
211+
periodCalculator = new PeriodCalculator();
204212
checkPermissions();
205213
if (permissionsGranted) {
206214
onCreateWithPermission();
@@ -263,7 +271,9 @@ private void maybeUpdateConfiguration(Configuration newConfig) {
263271
}
264272
}
265273

266-
/** Resize the SurfaceView to be centered on screen. */
274+
/**
275+
* Resize the SurfaceView to be centered on screen.
276+
*/
267277
private void updateViewfinderLayoutParams() {
268278
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) surfaceView.getLayoutParams();
269279

@@ -363,12 +373,17 @@ public void onError(CameraDevice cameraDevice, int i) {
363373
}
364374
}
365375

376+
public void onTimestampNs(long timestampNs) {
377+
periodCalculator.onFrameTimestamp(timestampNs);
378+
}
379+
366380
/* Set up UI controls and listeners based on if device is currently a leader of client. */
367381
private void setLeaderClientControls(boolean isLeader) {
368382
if (isLeader) {
369383
// Leader, all controls visible and set.
370384
captureStillButton.setVisibility(View.VISIBLE);
371385
phaseAlignButton.setVisibility(View.VISIBLE);
386+
getPeriodButton.setVisibility(View.VISIBLE);
372387
exposureSeekBar.setVisibility(View.VISIBLE);
373388
sensitivitySeekBar.setVisibility(View.VISIBLE);
374389

@@ -415,6 +430,26 @@ private void setLeaderClientControls(boolean isLeader) {
415430
String.valueOf(futureTimestamp));*/
416431
});
417432

433+
getPeriodButton.setOnClickListener(
434+
view -> {
435+
Log.d(TAG, "Calculating frames period.");
436+
437+
FutureTask<Integer> periodTask = new FutureTask<Integer>(
438+
() -> {
439+
try {
440+
long periodNs = periodCalculator.getPeriodNs();
441+
Log.d(TAG, "Calculated period: " + periodNs);
442+
phaseAlignController.setPeriodNs(periodNs);
443+
} catch (InterruptedException e) {
444+
Log.d(TAG, "Failed calculating period");
445+
e.printStackTrace();
446+
}
447+
return 0;
448+
}
449+
);
450+
periodTask.run();
451+
}
452+
);
418453

419454
phaseAlignButton.setOnClickListener(
420455
view -> {
@@ -486,6 +521,7 @@ public void onStopTrackingTouch(SeekBar seekBar) {
486521
// Client. All controls invisible.
487522
captureStillButton.setVisibility(View.INVISIBLE);
488523
phaseAlignButton.setVisibility(View.INVISIBLE);
524+
getPeriodButton.setVisibility(View.INVISIBLE);
489525
exposureSeekBar.setVisibility(View.INVISIBLE);
490526
sensitivitySeekBar.setVisibility(View.INVISIBLE);
491527

@@ -662,7 +698,9 @@ public void notifyCaptured(String name) {
662698
});
663699
}
664700

665-
/** Compares two {@code Size}s based on their areas. */
701+
/**
702+
* Compares two {@code Size}s based on their areas.
703+
*/
666704
static class CompareSizesByArea implements Comparator<Size> {
667705

668706
@Override
@@ -707,6 +745,8 @@ private void createUi() {
707745
// Controls.
708746
captureStillButton = findViewById(R.id.capture_still_button);
709747
phaseAlignButton = findViewById(R.id.phase_align_button);
748+
getPeriodButton = findViewById(R.id.get_period_button);
749+
710750
exposureSeekBar = findViewById(R.id.exposure_seekbar);
711751
sensitivitySeekBar = findViewById(R.id.sensitivity_seekbar);
712752
sensorExposureTextView.setText("Exposure: " + prettyExposureValue(currentSensorExposureTimeNs));
@@ -781,7 +821,9 @@ public void onWindowFocusChanged(boolean hasFocus) {
781821
}
782822
}
783823

784-
/** Create {@link #cameraController}, and subscribe to status change events. */
824+
/**
825+
* Create {@link #cameraController}, and subscribe to status change events.
826+
*/
785827
private void initCameraController() {
786828
cameraController =
787829
new CameraController(
@@ -1034,7 +1076,9 @@ private void checkPermissions() {
10341076
}
10351077
}
10361078

1037-
/** Wait for permissions to continue onCreate. */
1079+
/**
1080+
* Wait for permissions to continue onCreate.
1081+
*/
10381082
@Override
10391083
public void onRequestPermissionsResult(
10401084
int requestCode, String[] permissions, int[] grantResults) {

0 commit comments

Comments
 (0)