Skip to content

Commit

Permalink
fix: Throw correct errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Mar 18, 2024
1 parent be251f5 commit 5fe7608
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ class NoCameraDeviceError :
)
class PixelFormatNotSupportedError(format: String) :
CameraError("device", "pixel-format-not-supported", "The pixelFormat $format is not supported on the given Camera Device!")
class LowLightBoostNotSupportedError :
CameraError(
"device",
"low-light-boost-not-supported",
"The currently selected camera device does not support low-light boost! Select a device where `device.supportsLowLightBoost` is true."
)
class FlashUnavailableError :
CameraError(
"device",
Expand Down Expand Up @@ -183,7 +177,6 @@ class NoRecordingInProgressError :
class RecordingCanceledError : CameraError("capture", "recording-canceled", "The active recording was canceled.")
class FileIOError(throwable: Throwable) :
CameraError("capture", "file-io-error", "An unexpected File IO error occurred! Error: ${throwable.message}.", throwable)
class InsufficientStorageError : CameraError("capture", "insufficient-storage", "There is not enough storage space available.")
class RecordingInProgressError :
CameraError(
"capture",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,19 @@ class CameraSession(private val context: Context, private val callback: Callback
}
}

private fun assetFormatRequirement(propName: String, format: CameraDeviceFormat?, requirement: (format: CameraDeviceFormat) -> Boolean) {
private fun assertFormatRequirement(
propName: String,
format: CameraDeviceFormat?,
throwIfNotMet: CameraError,
requirement: (format: CameraDeviceFormat) -> Boolean
) {
if (format == null) {
// we need a format for this to work.
throw PropRequiresFormatToBeNonNullError(propName)
}
val isSupported = requirement(format)
if (!isSupported) {
// TODO: Throw actual Camera Device error
throw Error("$propName is not supported!")
throw throwIfNotMet
}
}

Expand All @@ -225,13 +229,13 @@ class CameraSession(private val context: Context, private val callback: Callback
val preview = Preview.Builder().also { preview ->
// Configure Preview Output
if (configuration.videoStabilizationMode.isAtLeast(VideoStabilizationMode.CINEMATIC)) {
assetFormatRequirement("videoStabilizationMode", format) {
assertFormatRequirement("videoStabilizationMode", format, InvalidVideoStabilizationMode(configuration.videoStabilizationMode)) {
it.videoStabilizationModes.contains(configuration.videoStabilizationMode)
}
preview.setPreviewStabilizationEnabled(true)
}
if (fpsRange != null) {
assetFormatRequirement("fps", format) {
assertFormatRequirement("fps", format, InvalidFpsError(fpsRange.upper)) {
fpsRange.lower >= it.minFps && fpsRange.upper <= it.maxFps
}
preview.setTargetFrameRate(fpsRange)
Expand Down Expand Up @@ -287,17 +291,20 @@ class CameraSession(private val context: Context, private val callback: Callback
// Configure Video Output
video.setMirrorMode(MirrorMode.MIRROR_MODE_ON_FRONT_ONLY)
if (configuration.videoStabilizationMode.isAtLeast(VideoStabilizationMode.STANDARD)) {
assetFormatRequirement("videoStabilizationMode", format) {
assertFormatRequirement("videoStabilizationMode", format, InvalidVideoStabilizationMode(configuration.videoStabilizationMode)) {
it.videoStabilizationModes.contains(configuration.videoStabilizationMode)
}
video.setVideoStabilizationEnabled(true)
}
if (fpsRange != null) {
assetFormatRequirement("fps", format) { fpsRange.lower >= it.minFps && fpsRange.upper <= it.maxFps }
assertFormatRequirement("fps", format, InvalidFpsError(fpsRange.upper)) {
fpsRange.lower >= it.minFps &&
fpsRange.upper <= it.maxFps
}
video.setTargetFrameRate(fpsRange)
}
if (videoConfig.config.enableHdr) {
assetFormatRequirement("videoHdr", format) { it.supportsVideoHdr }
assertFormatRequirement("videoHdr", format, InvalidVideoHdrError()) { it.supportsVideoHdr }
video.setDynamicRange(DynamicRange.HDR_UNSPECIFIED_10_BIT)
}
if (format != null) {
Expand Down

0 comments on commit 5fe7608

Please sign in to comment.