Skip to content

Commit 5508e5f

Browse files
committed
refactor(core): camera: pass a coroutine dispatcher instead of a coroutine scope
1 parent fc5d1c9 commit 5508e5f

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

core/src/main/java/io/github/thibaultbee/streampack/core/elements/sources/video/camera/controllers/CameraController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ internal class CameraController(
163163
return if (sessionController == null) {
164164
val deviceController = getDeviceController()
165165
CameraSessionController.create(
166-
coroutineScope,
166+
defaultDispatcher,
167167
deviceController,
168168
sessionCallback,
169169
sessionCompat,

core/src/main/java/io/github/thibaultbee/streampack/core/elements/sources/video/camera/controllers/CameraSessionController.kt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import io.github.thibaultbee.streampack.core.elements.sources.video.camera.utils
2828
import io.github.thibaultbee.streampack.core.elements.sources.video.camera.utils.CameraUtils
2929
import io.github.thibaultbee.streampack.core.elements.sources.video.camera.utils.CaptureRequestWithTargetsBuilder
3030
import io.github.thibaultbee.streampack.core.logger.Logger
31-
import kotlinx.coroutines.CoroutineScope
31+
import kotlinx.coroutines.CoroutineDispatcher
3232
import kotlinx.coroutines.flow.MutableStateFlow
3333
import kotlinx.coroutines.flow.StateFlow
3434
import kotlinx.coroutines.flow.asStateFlow
@@ -39,7 +39,7 @@ import kotlinx.coroutines.sync.withLock
3939
import kotlinx.coroutines.withContext
4040

4141
internal class CameraSessionController private constructor(
42-
private val coroutineScope: CoroutineScope,
42+
private val coroutineDispatcher: CoroutineDispatcher,
4343
private val captureSession: CameraCaptureSession,
4444
private val captureRequestBuilder: CaptureRequestWithTargetsBuilder,
4545
private val sessionCallback: CameraSessionCallback,
@@ -71,7 +71,7 @@ internal class CameraSessionController private constructor(
7171
private val captureCallbacks =
7272
setOf(captureCallback, sessionCallback)
7373

74-
suspend fun isEmpty() = withContext(coroutineScope.coroutineContext) {
74+
suspend fun isEmpty() = withContext(coroutineDispatcher) {
7575
requestTargetMutex.withLock { captureRequestBuilder.isEmpty() }
7676
}
7777

@@ -81,7 +81,7 @@ internal class CameraSessionController private constructor(
8181
* @param surface The target to check
8282
* @return true if the target is in the current capture request, false otherwise
8383
*/
84-
suspend fun hasTarget(surface: Surface) = withContext(coroutineScope.coroutineContext) {
84+
suspend fun hasTarget(surface: Surface) = withContext(coroutineDispatcher) {
8585
requestTargetMutex.withLock {
8686
captureRequestBuilder.hasTarget(surface)
8787
}
@@ -94,7 +94,7 @@ internal class CameraSessionController private constructor(
9494
* @return true if the target is in the current capture request, false otherwise
9595
*/
9696
suspend fun hasTarget(cameraSurface: CameraSurface) =
97-
withContext(coroutineScope.coroutineContext) {
97+
withContext(coroutineDispatcher) {
9898
requestTargetMutex.withLock {
9999
captureRequestBuilder.hasTarget(cameraSurface)
100100
}
@@ -110,7 +110,7 @@ internal class CameraSessionController private constructor(
110110
require(targets.all { it.surface.isValid }) { "All targets must be valid" }
111111
require(targets.all { outputs.contains(it) }) { "Targets must be in the current capture session: $targets ($outputs)" }
112112

113-
val res = withContext(coroutineScope.coroutineContext) {
113+
val res = withContext(coroutineDispatcher) {
114114
requestTargetMutex.withLock {
115115
val res = targets.map {
116116
captureRequestBuilder.addTarget(it)
@@ -131,7 +131,7 @@ internal class CameraSessionController private constructor(
131131
suspend fun addTarget(name: String): Boolean {
132132
require(outputs.any { it.name == name }) { "Target type must be in the current capture session: $name ($outputs)" }
133133

134-
val res = withContext(coroutineScope.coroutineContext) {
134+
val res = withContext(coroutineDispatcher) {
135135
requestTargetMutex.withLock {
136136
val target = outputs.first { it.name == name }
137137
val res = captureRequestBuilder.addTarget(target)
@@ -151,7 +151,7 @@ internal class CameraSessionController private constructor(
151151
require(target.surface.isValid) { "Target must be valid: $target" }
152152
require(outputs.contains(target)) { "Target must be in the current capture session: $target ($outputs)" }
153153

154-
val res = withContext(coroutineScope.coroutineContext) {
154+
val res = withContext(coroutineDispatcher) {
155155
requestTargetMutex.withLock {
156156
val res = captureRequestBuilder.addTarget(target)
157157
setRepeatingSession()
@@ -167,7 +167,7 @@ internal class CameraSessionController private constructor(
167167
* @param targets The targets to remove
168168
*/
169169
suspend fun removeTargets(targets: List<CameraSurface>) {
170-
withContext(coroutineScope.coroutineContext) {
170+
withContext(coroutineDispatcher) {
171171
requestTargetMutex.withLock {
172172
targets.forEach {
173173
captureRequestBuilder.removeTarget(it)
@@ -187,7 +187,7 @@ internal class CameraSessionController private constructor(
187187
* @param name The name of target to remove
188188
*/
189189
suspend fun removeTarget(name: String) {
190-
withContext(coroutineScope.coroutineContext) {
190+
withContext(coroutineDispatcher) {
191191
requestTargetMutex.withLock {
192192
val target = outputs.firstOrNull { it.name == name }
193193
target?.let {
@@ -212,7 +212,7 @@ internal class CameraSessionController private constructor(
212212
* @param target The target to remove
213213
*/
214214
suspend fun removeTarget(target: CameraSurface) {
215-
withContext(coroutineScope.coroutineContext) {
215+
withContext(coroutineDispatcher) {
216216
requestTargetMutex.withLock {
217217
captureRequestBuilder.removeTarget(target)
218218

@@ -226,7 +226,7 @@ internal class CameraSessionController private constructor(
226226
}
227227

228228
suspend fun close() {
229-
withContext(coroutineScope.coroutineContext) {
229+
withContext(coroutineDispatcher) {
230230
captureSessionMutex.withLock {
231231
if (isClosed) {
232232
Logger.w(TAG, "Session already closed")
@@ -254,7 +254,7 @@ internal class CameraSessionController private constructor(
254254
Logger.w(TAG, "Capture request is empty")
255255
return
256256
}
257-
withContext(coroutineScope.coroutineContext) {
257+
withContext(coroutineDispatcher) {
258258
captureSessionMutex.withLock {
259259
if (isClosed) {
260260
Logger.w(TAG, "Camera session controller is released")
@@ -273,7 +273,7 @@ internal class CameraSessionController private constructor(
273273
}
274274

275275
private suspend fun stopRepeatingSession() {
276-
withContext(coroutineScope.coroutineContext) {
276+
withContext(coroutineDispatcher) {
277277
captureSessionMutex.withLock {
278278
if (isClosed) {
279279
Logger.w(TAG, "Camera session controller is released")
@@ -316,7 +316,7 @@ internal class CameraSessionController private constructor(
316316
outputs: List<CameraSurface>,
317317
dynamicRange: Long,
318318
fpsRange: Range<Int>
319-
): CameraSessionController = withContext(coroutineScope.coroutineContext) {
319+
): CameraSessionController = withContext(coroutineDispatcher) {
320320
requestTargetMutex.withLock {
321321
require(outputs.isNotEmpty()) { "At least one output is required" }
322322
require(outputs.all { it.surface.isValid }) { "All outputs $outputs must be valid but ${outputs.filter { !it.surface.isValid }} is invalid" }
@@ -349,7 +349,7 @@ internal class CameraSessionController private constructor(
349349
)
350350

351351
val controller = CameraSessionController(
352-
coroutineScope,
352+
coroutineDispatcher,
353353
newCaptureSession,
354354
captureRequestBuilder,
355355
sessionCallback,
@@ -372,7 +372,7 @@ internal class CameraSessionController private constructor(
372372
private const val TAG = "CameraSessionController"
373373

374374
suspend fun create(
375-
coroutineScope: CoroutineScope,
375+
coroutineDispatcher: CoroutineDispatcher,
376376
cameraDeviceController: CameraDeviceController,
377377
sessionCallback: CameraSessionCallback,
378378
sessionCompat: ICameraCaptureSessionCompat,
@@ -402,7 +402,7 @@ internal class CameraSessionController private constructor(
402402
defaultRequestBuilder()
403403
}
404404
return CameraSessionController(
405-
coroutineScope,
405+
coroutineDispatcher,
406406
captureSession,
407407
captureRequestBuilder,
408408
sessionCallback,

0 commit comments

Comments
 (0)