Skip to content

Commit

Permalink
fix(core): fix concurrency exception due to camera source
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee committed Jan 8, 2025
1 parent e395d6f commit 2e4db6c
Show file tree
Hide file tree
Showing 14 changed files with 729 additions and 384 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CameraStreamerStateTest(descriptor: MediaDescriptor) :
ConfigurationUtils.dummyValidAudioConfig(),
ConfigurationUtils.dummyValidVideoConfig()
)
streamer.startPreview(SurfaceUtils.createSurfaceView(activityScenarioRule.scenario))
streamer.startPreview(SurfaceUtils.getSurfaceView(activityScenarioRule.scenario))
streamer.startStream(descriptor)
streamer.stopStream()
streamer.stopPreview()
Expand All @@ -68,17 +68,17 @@ class CameraStreamerStateTest(descriptor: MediaDescriptor) :
// Single method calls
@Test
fun startPreviewTest() = runTest {
streamer.startPreview(SurfaceUtils.createSurfaceView(activityScenarioRule.scenario))
streamer.startPreview(SurfaceUtils.getSurfaceView(activityScenarioRule.scenario))
}

@Test
fun stopPreviewTest() {
fun stopPreviewTest() = runTest {
streamer.stopPreview()
}

// Multiple methods calls
@Test
fun configureStopPreviewTest() {
fun configureStopPreviewTest() = runTest {
streamer.setConfig(
ConfigurationUtils.dummyValidAudioConfig(),
ConfigurationUtils.dummyValidVideoConfig()
Expand All @@ -92,7 +92,7 @@ class CameraStreamerStateTest(descriptor: MediaDescriptor) :
ConfigurationUtils.dummyValidAudioConfig(),
ConfigurationUtils.dummyValidVideoConfig()
)
streamer.startPreview(SurfaceUtils.createSurfaceView(activityScenarioRule.scenario))
streamer.startPreview(SurfaceUtils.getSurfaceView(activityScenarioRule.scenario))
streamer.release()
}

Expand All @@ -102,7 +102,7 @@ class CameraStreamerStateTest(descriptor: MediaDescriptor) :
ConfigurationUtils.dummyValidAudioConfig(),
ConfigurationUtils.dummyValidVideoConfig()
)
streamer.startPreview(SurfaceUtils.createSurfaceView(activityScenarioRule.scenario))
streamer.startPreview(SurfaceUtils.getSurfaceView(activityScenarioRule.scenario))
streamer.stopPreview()
}

Expand All @@ -112,13 +112,13 @@ class CameraStreamerStateTest(descriptor: MediaDescriptor) :
ConfigurationUtils.dummyValidAudioConfig(),
ConfigurationUtils.dummyValidVideoConfig()
)
streamer.startPreview(SurfaceUtils.createSurfaceView(activityScenarioRule.scenario))
streamer.startPreview(SurfaceUtils.getSurfaceView(activityScenarioRule.scenario))
streamer.stopStream()
}

@Test
fun multipleStartPreviewStopPreviewTest() = runTest {
val surfaceView = SurfaceUtils.createSurfaceView(activityScenarioRule.scenario)
val surfaceView = SurfaceUtils.getSurfaceView(activityScenarioRule.scenario)
streamer.setConfig(
ConfigurationUtils.dummyValidAudioConfig(),
ConfigurationUtils.dummyValidVideoConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import kotlin.coroutines.suspendCoroutine
object SurfaceUtils {
private const val TAG = "SurfaceUtils"

private fun createSurfaceViewAsync(
private fun getSurfaceViewAsync(
scenario: ActivityScenario<SurfaceViewTestActivity>,
onSurfaceCreated: (SurfaceView) -> Unit
) {
Expand Down Expand Up @@ -54,9 +54,56 @@ object SurfaceUtils {
}
}

suspend fun createSurfaceView(scenario: ActivityScenario<SurfaceViewTestActivity>): SurfaceView {
/**
* Gets the [SurfaceView] from the [SurfaceViewTestActivity]
*/
suspend fun getSurfaceView(scenario: ActivityScenario<SurfaceViewTestActivity>): SurfaceView {
return suspendCoroutine {
createSurfaceViewAsync(scenario) { surfaceView ->
getSurfaceViewAsync(scenario) { surfaceView ->
it.resumeWith(Result.success(surfaceView))
}
}
}

private fun addsSurfaceViewAsync(
scenario: ActivityScenario<SurfaceViewTestActivity>,
onSurfaceCreated: (SurfaceView) -> Unit
) {
scenario.onActivity {
val surfaceView = SurfaceView(it)
val callback =
object : SurfaceHolder.Callback {
override fun surfaceCreated(surfaceHolder: SurfaceHolder) {
Logger.i(TAG, "Surface created")
onSurfaceCreated(surfaceView)
}

override fun surfaceChanged(
holder: SurfaceHolder,
format: Int,
width: Int,
height: Int
) {
Logger.i(TAG, "Surface changed")
}

override fun surfaceDestroyed(holder: SurfaceHolder) {
Logger.i(TAG, "Surface destroyed")
}
}


surfaceView.holder.setFixedSize(10, 10)
it.addSurface(surfaceView, callback)
}
}

/**
* Adds a [SurfaceView] in the [SurfaceViewTestActivity]
*/
suspend fun addSurfaceView(scenario: ActivityScenario<SurfaceViewTestActivity>): SurfaceView {
return suspendCoroutine {
addsSurfaceViewAsync(scenario) { surfaceView ->
it.resumeWith(Result.success(surfaceView))
}
}
Expand Down
Loading

0 comments on commit 2e4db6c

Please sign in to comment.