Skip to content

Commit 1270ae0

Browse files
committed
Record video with frame rate supported by camera
1 parent 1e2dce5 commit 1270ae0

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

‎app/src/main/java/org/thunderdog/challegram/ui/camera/legacy/CameraApiLegacy.java‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ protected void onStartVideoCapture () throws Throwable {
704704
throw new IllegalStateException("!isCameraActive");
705705
}
706706

707+
final int frameRate = getVideoFrameRate(camera.getParameters());
707708
camera.unlock();
708709

709710
videoRecorder = new MediaRecorder();
@@ -716,7 +717,7 @@ protected void onStartVideoCapture () throws Throwable {
716717
}
717718
videoRecorder.setOutputFile(videoFile.getAbsolutePath());
718719
videoRecorder.setMaxFileSize(ByteUnit.GIB.toBytes(1));
719-
videoRecorder.setVideoFrameRate(30);
720+
videoRecorder.setVideoFrameRate(frameRate);
720721
videoRecorder.setMaxDuration(0);
721722
videoRecorder.setVideoEncodingBitRate(900000 * 2);
722723
//videoRecorder.setAudioEncodingBitRate(32000);
@@ -757,6 +758,27 @@ private File configureRecorder (MediaRecorder recorder) {
757758
return manager.getOutputFile(true);
758759
}
759760

761+
private static final int VIDEO_FRAME_RATE = 30;
762+
763+
private static int getVideoFrameRate (Camera.Parameters parameters) {
764+
// MediaRecorder fails to start with a frame rate that the camera doesn't support,
765+
// e.g. 30 on a camera of 24: the closest supported one below 30 is taken instead,
766+
// or above it when there's none
767+
final List<Integer> frameRates = parameters.getSupportedPreviewFrameRates();
768+
if (frameRates == null || frameRates.isEmpty() || frameRates.contains(VIDEO_FRAME_RATE)) {
769+
return VIDEO_FRAME_RATE;
770+
}
771+
int below = 0, above = Integer.MAX_VALUE;
772+
for (int frameRate : frameRates) {
773+
if (frameRate < VIDEO_FRAME_RATE) {
774+
below = Math.max(below, frameRate);
775+
} else {
776+
above = Math.min(above, frameRate);
777+
}
778+
}
779+
return below != 0 ? below : above;
780+
}
781+
760782
private static int getHighCamcorderProfile () {
761783
switch (Device.PRODUCT) {
762784
case Device.LGE_G3:

0 commit comments

Comments
 (0)