Skip to content

Commit

Permalink
Utilize debouncer instead of animator timeout for video capture end t…
Browse files Browse the repository at this point in the history
…ime.
  • Loading branch information
alex-signal committed Jul 11, 2022
1 parent 6aa4706 commit 8f85b58
Showing 1 changed file with 19 additions and 34 deletions.
@@ -1,7 +1,6 @@
package org.thoughtcrime.securesms.mediasend;

import android.Manifest;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
Expand All @@ -23,11 +22,13 @@
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.permissions.Permissions;
import org.thoughtcrime.securesms.util.Debouncer;
import org.thoughtcrime.securesms.util.MemoryFileDescriptor;
import org.thoughtcrime.securesms.video.VideoUtil;

import java.io.FileDescriptor;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RequiresApi(26)
class CameraXVideoCaptureHelper implements CameraButtonView.VideoCaptureListener {
Expand All @@ -41,16 +42,18 @@ class CameraXVideoCaptureHelper implements CameraButtonView.VideoCaptureListener
private final @NonNull Callback callback;
private final @NonNull MemoryFileDescriptor memoryFileDescriptor;
private final @NonNull ValueAnimator updateProgressAnimator;
private final @NonNull Debouncer debouncer;

private boolean isRecording;
private ValueAnimator cameraMetricsAnimator;
private boolean isRecording;
private ValueAnimator cameraMetricsAnimator;

private final VideoCapture.OnVideoSavedCallback videoSavedListener = new VideoCapture.OnVideoSavedCallback() {
@SuppressLint("RestrictedApi")
@Override
public void onVideoSaved(@NonNull VideoCapture.OutputFileResults outputFileResults) {
try {
isRecording = false;
debouncer.clear();
camera.setZoomRatio(camera.getMinZoomRatio());
memoryFileDescriptor.seek(0);
callback.onVideoSaved(memoryFileDescriptor.getFileDescriptor());
Expand All @@ -63,6 +66,7 @@ public void onVideoSaved(@NonNull VideoCapture.OutputFileResults outputFileResul
@Override
public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
isRecording = false;
debouncer.clear();
callback.onVideoError(cause);
}
};
Expand All @@ -71,23 +75,18 @@ public void onError(int videoCaptureError, @NonNull String message, @Nullable Th
@NonNull CameraButtonView captureButton,
@NonNull SignalCameraView camera,
@NonNull MemoryFileDescriptor memoryFileDescriptor,
int maxVideoDurationSec,
int maxVideoDurationSec,
@NonNull Callback callback)
{
this.fragment = fragment;
this.camera = camera;
this.memoryFileDescriptor = memoryFileDescriptor;
this.callback = callback;
this.updateProgressAnimator = ValueAnimator.ofFloat(0f, 1f).setDuration(maxVideoDurationSec * 1000);
this.updateProgressAnimator = ValueAnimator.ofFloat(0f, 1f).setDuration(TimeUnit.SECONDS.toMillis(maxVideoDurationSec));
this.debouncer = new Debouncer(TimeUnit.SECONDS.toMillis(maxVideoDurationSec));

updateProgressAnimator.setInterpolator(new LinearInterpolator());
updateProgressAnimator.addUpdateListener(anim -> captureButton.setProgress(anim.getAnimatedFraction()));
updateProgressAnimator.addListener(new AnimationEndCallback() {
@Override
public void onAnimationEnd(Animator animation) {
if (isRecording) onVideoCaptureComplete();
}
});
}

@Override
Expand Down Expand Up @@ -126,14 +125,15 @@ private void beginCameraRecording() {

camera.startRecording(options, Executors.mainThreadExecutor(), videoSavedListener);
updateProgressAnimator.start();
debouncer.publish(this::onVideoCaptureComplete);
}

private void shrinkCaptureArea() {
Size screenSize = getScreenSize();
Size videoRecordingSize = VideoUtil.getVideoRecordingSize();
float scale = getSurfaceScaleForRecording();
float targetWidthForAnimation = videoRecordingSize.getWidth() * scale;
float scaleX = targetWidthForAnimation / screenSize.getWidth();
Size screenSize = getScreenSize();
Size videoRecordingSize = VideoUtil.getVideoRecordingSize();
float scale = getSurfaceScaleForRecording();
float targetWidthForAnimation = videoRecordingSize.getWidth() * scale;
float scaleX = targetWidthForAnimation / screenSize.getWidth();

if (scaleX == 1f) {
float targetHeightForAnimation = videoRecordingSize.getHeight() * scale;
Expand Down Expand Up @@ -190,6 +190,7 @@ public void onVideoCaptureComplete() {
}

updateProgressAnimator.cancel();
debouncer.clear();
}

@Override
Expand All @@ -206,27 +207,11 @@ static MemoryFileDescriptor createFileDescriptor(@NonNull Context context) throw
);
}

private static abstract class AnimationEndCallback implements Animator.AnimatorListener {

@Override
public final void onAnimationStart(Animator animation) {

}

@Override
public final void onAnimationCancel(Animator animation) {

}

@Override
public final void onAnimationRepeat(Animator animation) {

}
}

interface Callback {
void onVideoRecordStarted();

void onVideoSaved(@NonNull FileDescriptor fd);

void onVideoError(@Nullable Throwable cause);
}
}

0 comments on commit 8f85b58

Please sign in to comment.