Skip to content

Commit

Permalink
Merge pull request #31 from MobileRoboticsSkoltech/blinking_flash
Browse files Browse the repository at this point in the history
Flash logging
  • Loading branch information
anastasiia-kornilova authored Apr 21, 2021
2 parents d7df21d + 7653f8b commit 0cfcde3
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This project is based on [Open Camera](https://opencamera.org.uk/) — a popul
- ```{VIDEO_NAME}_accel.csv```, data format: ```X-data, Y-data, Z-data, timestamp (ns)```
- ```{VIDEO_NAME}_magnetic.csv```, data format: ```X-data, Y-data, Z-data, timestamp (ns)```
- ```{VIDEO_NAME}_timestamps.csv```, data format: ```timestamp (ns)```
- ```{VIDEO_NAME}_flash.csv```, data format: ```timestamp (ns)``` (timestamps of when the flash fired)

### Remote recording

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.util.Log;

import net.sourceforge.opencamera.cameracontroller.YuvImageUtils;
import net.sourceforge.opencamera.sensorlogging.FlashController;
import net.sourceforge.opencamera.sensorlogging.RawSensorInfo;
import net.sourceforge.opencamera.sensorlogging.VideoFrameInfo;
import net.sourceforge.opencamera.sensorlogging.VideoPhaseInfo;
Expand All @@ -26,6 +27,12 @@ public class ExtendedAppInterface extends MyApplicationInterface {
private static final int SENSOR_FREQ_DEFAULT_PREF = 0;

private final RawSensorInfo mRawSensorInfo;

public FlashController getFlashController() {
return mFlashController;
}

private final FlashController mFlashController;
private final SharedPreferences mSharedPreferences;
private final MainActivity mMainActivity;
private final YuvImageUtils mYuvUtils;
Expand All @@ -40,6 +47,8 @@ public VideoFrameInfo setupFrameInfo() throws IOException {
super(mainActivity, savedInstanceState);
mRawSensorInfo = mainActivity.getRawSensorInfoManager();
mMainActivity = mainActivity;
mFlashController = new FlashController(mainActivity);

mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mainActivity);
// We create it only once here (not during the video) as it is a costly operation
// (instantiates RenderScript object)
Expand Down Expand Up @@ -152,6 +161,16 @@ public void startingVideo() {
mMainActivity.getPreview().showToast(null, "Requested IMU recording but no sensors were enabled");
mMainActivity.getPreview().stopVideo(false);
}

if (getVideoFlashPref()) {
try {
mFlashController.startRecording(mLastVideoDate);
} catch (IOException e) {
Log.e(TAG, "Failed to start flash controller");
e.printStackTrace();
}
}

super.startingVideo();
}

Expand All @@ -168,6 +187,10 @@ public void stoppingVideo() {
mMainActivity.getPreview().showToast("Stopping video with IMU recording...", true);
}

if (mFlashController.isRecording()) {
mFlashController.stopRecording();
}

super.stoppingVideo();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5145,6 +5145,7 @@ private void flashVideo() {
// turn on torch
cancelAutoFocus();
camera_controller.setFlashValue("flash_torch");
applicationInterface.getFlashController().onFlashFired();
try {
Thread.sleep(100);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package net.sourceforge.opencamera.sensorlogging;

import android.os.SystemClock;
import android.util.Log;

import net.sourceforge.opencamera.MainActivity;
import net.sourceforge.opencamera.StorageUtils;
import net.sourceforge.opencamera.StorageUtilsWrapper;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class FlashController {
private final static String TAG = "FlashController";
private final static String TIMESTAMP_FILE_SUFFIX = "_flash";
private BufferedWriter mFlashBufferedWriter;
private final MainActivity mContext;

public boolean isRecording() {
return mIsRecording;
}

private volatile boolean mIsRecording;


public FlashController(MainActivity context) {
mContext = context;
}

public void startRecording(Date currentVideoDate) throws IOException {
StorageUtilsWrapper storageUtils = mContext.getStorageUtils();

File frameTimestampFile = storageUtils.createOutputCaptureInfo(
StorageUtils.MEDIA_TYPE_RAW_SENSOR_INFO, "csv", TIMESTAMP_FILE_SUFFIX, currentVideoDate
);
mFlashBufferedWriter = new BufferedWriter(
new PrintWriter(frameTimestampFile)
);
mIsRecording = true;
}

public void onFlashFired() {
if (isRecording() && mFlashBufferedWriter != null) {
long timestamp = SystemClock.elapsedRealtimeNanos();
try {
mFlashBufferedWriter
.append(Long.toString(timestamp))
.append("\n");
} catch (IOException e) {
Log.d(TAG, "Failed to write flash timestamp");
}
}
}

public void stopRecording() {
mIsRecording = false;
try {
if (mFlashBufferedWriter != null) {
Log.d(TAG, "Before writer close()");
mFlashBufferedWriter.flush();
mFlashBufferedWriter.close();
mFlashBufferedWriter = null;
Log.d(TAG, "After writer close()");
}
} catch (IOException e) {
Log.d(TAG, "Exception occurred when attempting to close mFlashBufferedWriter");
e.printStackTrace();
}
}
}

0 comments on commit 0cfcde3

Please sign in to comment.