Skip to content

Commit

Permalink
[fix] update resolution failed
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangdg committed Feb 13, 2023
1 parent 72f01ac commit 8256038
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class MultiCameraClient(ctx: Context, callback: IDeviceConnectCallBack?) {
*/
override fun onDisconnect(device: UsbDevice?, ctrlBlock: USBMonitor.UsbControlBlock?) {
if (Utils.debugCamera) {
Logger.i(TAG, "disconnct device name/pid/vid:${device?.deviceName}&${device?.productId}&${device?.vendorId} ")
Logger.i(TAG, "disconnect device name/pid/vid:${device?.deviceName}&${device?.productId}&${device?.vendorId} ")
}
device ?: return
if (!isUsbCamera(device) && !isFilterDevice(ctx, device)) {
Expand Down
21 changes: 18 additions & 3 deletions libausbc/src/main/java/com/jiangdg/ausbc/base/CameraFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ abstract class CameraFragment : BaseFragment(), ICameraStateCallBack {
setUsbControlBlock(null)
}
mRequestPermission.set(false)
mCurrentCamera = null
try {
mCurrentCamera?.cancel(true)
mCurrentCamera = null
} catch (e: Exception) {
e.printStackTrace()
}
}

override fun onConnectDev(device: UsbDevice?, ctrlBlock: USBMonitor.UsbControlBlock?) {
Expand All @@ -125,6 +130,12 @@ abstract class CameraFragment : BaseFragment(), ICameraStateCallBack {
mCameraMap[device.deviceId]?.apply {
setUsbControlBlock(ctrlBlock)
}?.also { camera ->
try {
mCurrentCamera?.cancel(true)
mCurrentCamera = null
} catch (e: Exception) {
e.printStackTrace()
}
mCurrentCamera = SettableFuture()
mCurrentCamera?.set(camera)
openCamera(mCameraView)
Expand All @@ -135,12 +146,16 @@ abstract class CameraFragment : BaseFragment(), ICameraStateCallBack {
override fun onDisConnectDec(device: UsbDevice?, ctrlBlock: USBMonitor.UsbControlBlock?) {
closeCamera()
mRequestPermission.set(false)
mCurrentCamera = null
}

override fun onCancelDev(device: UsbDevice?) {
mRequestPermission.set(false)
mCurrentCamera = null
try {
mCurrentCamera?.cancel(true)
mCurrentCamera = null
} catch (e: Exception) {
e.printStackTrace()
}
}
})
mCameraClient?.register()
Expand Down
37 changes: 26 additions & 11 deletions libausbc/src/main/java/com/jiangdg/ausbc/camera/CameraUVC.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ import java.util.concurrent.TimeUnit
*/
class CameraUVC(ctx: Context, device: UsbDevice) : MultiCameraClient.ICamera(ctx, device) {
private var mUvcCamera: UVCCamera? = null
private val mCameraPreviewSize by lazy {
arrayListOf<PreviewSize>()
}

private val frameCallBack = IFrameCallback { frame ->
frame?.apply {
Expand Down Expand Up @@ -78,18 +81,28 @@ class CameraUVC(ctx: Context, device: UsbDevice) : MultiCameraClient.ICamera(ctx
mUvcCamera?.supportedSizeList
} else {
mUvcCamera?.getSupportedSizeList(UVCCamera.FRAME_FORMAT_YUYV)
}.also { sizeList ->
sizeList?.forEach { size ->
val width = size.width
val height = size.height
val ratio = width.toDouble() / height
if (aspectRatio == null || aspectRatio == ratio) {
previewSizeList.add(PreviewSize(width, height))
}?.let { sizeList ->
if (mCameraPreviewSize.isEmpty()) {
mCameraPreviewSize.clear()
sizeList.forEach { size->
val width = size.width
val height = size.height
mCameraPreviewSize.add(PreviewSize(width, height))
}
}
mCameraPreviewSize
}?.onEach { size ->
val width = size.width
val height = size.height
val ratio = width.toDouble() / height
if (aspectRatio == null || aspectRatio == ratio) {
previewSizeList.add(PreviewSize(width, height))
}
}
if (Utils.debugCamera)
if (Utils.debugCamera) {
Logger.i(TAG, "aspect ratio = $aspectRatio, getAllPreviewSizes = $previewSizeList, ")
}

return previewSizeList
}

Expand Down Expand Up @@ -131,11 +144,13 @@ class CameraUVC(ctx: Context, device: UsbDevice) : MultiCameraClient.ICamera(ctx
return
}
initEncodeProcessor(previewSize.width, previewSize.height)
// if give custom minFps or maxFps or unsupported preview size
// this method will fail
mUvcCamera?.setPreviewSize(
previewSize.width,
previewSize.height,
MIN_FS,
MAX_FS,
MAX_FPS,
UVCCamera.FRAME_FORMAT_MJPEG,
UVCCamera.DEFAULT_BANDWIDTH
)
Expand All @@ -156,7 +171,7 @@ class CameraUVC(ctx: Context, device: UsbDevice) : MultiCameraClient.ICamera(ctx
previewSize.width,
previewSize.height,
MIN_FS,
MAX_FS,
MAX_FPS,
UVCCamera.FRAME_FORMAT_YUYV,
UVCCamera.DEFAULT_BANDWIDTH
)
Expand Down Expand Up @@ -501,6 +516,6 @@ class CameraUVC(ctx: Context, device: UsbDevice) : MultiCameraClient.ICamera(ctx
companion object {
private const val TAG = "CameraUVC"
private const val MIN_FS = 10
private const val MAX_FS = 60
private const val MAX_FPS = 60
}
}

0 comments on commit 8256038

Please sign in to comment.