Skip to content

Commit 8c5b603

Browse files
authored
perf: Use existing cameraQueue instead of yet another Thread (#2459)
* perf: Use existing `cameraQueue` instead of yet another Thread * fix: Use `coroutineScope` to avoid wrong use of isActive * fix: Do the same for `CameraSession` * Lint
1 parent af14f91 commit 8c5b603

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

package/android/src/main/java/com/mrousavy/camera/CameraView.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import com.mrousavy.camera.types.PixelFormat
2323
import com.mrousavy.camera.types.ResizeMode
2424
import com.mrousavy.camera.types.Torch
2525
import com.mrousavy.camera.types.VideoStabilizationMode
26-
import kotlin.coroutines.CoroutineContext
2726
import kotlinx.coroutines.CoroutineScope
2827
import kotlinx.coroutines.launch
2928

@@ -39,7 +38,6 @@ import kotlinx.coroutines.launch
3938
@SuppressLint("ClickableViewAccessibility", "ViewConstructor", "MissingPermission")
4039
class CameraView(context: Context) :
4140
FrameLayout(context),
42-
CoroutineScope,
4341
CameraSession.Callback {
4442
companion object {
4543
const val TAG = "CameraView"
@@ -98,7 +96,7 @@ class CameraView(context: Context) :
9896

9997
internal var frameProcessor: FrameProcessor? = null
10098

101-
override val coroutineContext: CoroutineContext = CameraQueues.cameraQueue.coroutineDispatcher
99+
private val coroutineScope = CoroutineScope(CameraQueues.cameraQueue.coroutineDispatcher)
102100

103101
init {
104102
this.installHierarchyFitter()
@@ -131,7 +129,7 @@ class CameraView(context: Context) :
131129
val now = System.currentTimeMillis()
132130
currentConfigureCall = now
133131

134-
launch {
132+
coroutineScope.launch {
135133
cameraSession.configure { config ->
136134
if (currentConfigureCall != now) {
137135
// configure waits for a lock, and if a new call to update() happens in the meantime we can drop this one.

package/android/src/main/java/com/mrousavy/camera/CameraViewModule.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.facebook.react.modules.core.PermissionAwareActivity
1010
import com.facebook.react.modules.core.PermissionListener
1111
import com.facebook.react.uimanager.UIManagerHelper
1212
import com.mrousavy.camera.core.CameraError
13+
import com.mrousavy.camera.core.CameraQueues
1314
import com.mrousavy.camera.core.ViewNotFoundError
1415
import com.mrousavy.camera.frameprocessor.VisionCameraInstaller
1516
import com.mrousavy.camera.frameprocessor.VisionCameraProxy
@@ -39,7 +40,7 @@ class CameraViewModule(reactContext: ReactApplicationContext) : ReactContextBase
3940
}
4041
}
4142

42-
private val coroutineScope = CoroutineScope(Dispatchers.Default) // TODO: or Dispatchers.Main?
43+
private val coroutineScope = CoroutineScope(CameraQueues.cameraQueue.coroutineDispatcher)
4344

4445
override fun invalidate() {
4546
super.invalidate()

package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt

+4-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ import com.mrousavy.camera.utils.ImageFormatUtils
4949
import java.io.Closeable
5050
import java.lang.IllegalStateException
5151
import java.util.concurrent.CancellationException
52-
import kotlin.coroutines.CoroutineContext
5352
import kotlinx.coroutines.CoroutineScope
5453
import kotlinx.coroutines.launch
5554
import kotlinx.coroutines.runBlocking
@@ -58,8 +57,7 @@ import kotlinx.coroutines.sync.withLock
5857

5958
class CameraSession(private val context: Context, private val cameraManager: CameraManager, private val callback: Callback) :
6059
CameraManager.AvailabilityCallback(),
61-
Closeable,
62-
CoroutineScope {
60+
Closeable {
6361
companion object {
6462
private const val TAG = "CameraSession"
6563
}
@@ -95,8 +93,7 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
9593
field = value
9694
}
9795

98-
override val coroutineContext: CoroutineContext
99-
get() = CameraQueues.cameraQueue.coroutineDispatcher
96+
private val coroutineScope = CoroutineScope(CameraQueues.cameraQueue.coroutineDispatcher)
10097

10198
// Video Outputs
10299
private var recording: RecordingSession? = null
@@ -139,7 +136,7 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
139136
super.onCameraAvailable(cameraId)
140137
if (this.configuration?.cameraId == cameraId && cameraDevice == null && configuration?.isActive == true) {
141138
Log.i(TAG, "Camera #$cameraId is now available again, trying to re-open it now...")
142-
launch {
139+
coroutineScope.launch {
143140
configure {
144141
// re-open CameraDevice if needed
145142
}
@@ -252,7 +249,7 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
252249

253250
private fun createPreviewOutput(surface: Surface) {
254251
Log.i(TAG, "Setting Preview Output...")
255-
launch {
252+
coroutineScope.launch {
256253
configure { config ->
257254
config.preview = CameraConfiguration.Output.Enabled.create(CameraConfiguration.Preview(surface))
258255
}

0 commit comments

Comments
 (0)