Skip to content

Commit 8bea211

Browse files
committed
fix(core): update source size of OutputSurface when resolution changes in setVideoCodecConfig
1 parent 8260c16 commit 8bea211

File tree

2 files changed

+16
-36
lines changed

2 files changed

+16
-36
lines changed

core/src/main/java/io/github/thibaultbee/streampack/core/pipelines/StreamerPipeline.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ open class StreamerPipeline(
8989
surfaceProcessorFactory: ISurfaceProcessorInternal.Factory = DefaultSurfaceProcessorFactory(),
9090
protected val dispatcherProvider: IDispatcherProvider = DispatcherProvider()
9191
) : IWithVideoSource, IWithVideoRotation, IWithAudioSource, IStreamer {
92-
private val coroutineScope: CoroutineScope =
93-
CoroutineScope(dispatcherProvider.default)
92+
private val coroutineScope = CoroutineScope(dispatcherProvider.default)
9493
private val isReleaseRequested = AtomicBoolean(false)
9594

9695
private val _throwableFlow = MutableStateFlow<Throwable?>(null)
@@ -160,9 +159,9 @@ open class StreamerPipeline(
160159

161160
_videoInput?.let { input ->
162161
coroutineScope.launch {
163-
input.infoProviderFlow.collect {
162+
input.inputConfigChanged.collect {
164163
if (isReleaseRequested.get()) {
165-
Logger.w(TAG, "Pipeline is released, dropping info provider update")
164+
Logger.w(TAG, "Pipeline is released, dropping video input config changed")
166165
return@collect
167166
}
168167

core/src/main/java/io/github/thibaultbee/streampack/core/pipelines/inputs/VideoInput.kt

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import io.github.thibaultbee.streampack.core.elements.processing.video.ISurfaceP
2323
import io.github.thibaultbee.streampack.core.elements.processing.video.outputs.ISurfaceOutput
2424
import io.github.thibaultbee.streampack.core.elements.processing.video.outputs.SurfaceOutput
2525
import io.github.thibaultbee.streampack.core.elements.processing.video.source.DefaultSourceInfoProvider
26-
import io.github.thibaultbee.streampack.core.elements.processing.video.source.ISourceInfoProvider
2726
import io.github.thibaultbee.streampack.core.elements.sources.video.IPreviewableSource
2827
import io.github.thibaultbee.streampack.core.elements.sources.video.ISurfaceSourceInternal
2928
import io.github.thibaultbee.streampack.core.elements.sources.video.IVideoSource
@@ -184,8 +183,8 @@ internal class VideoInput(
184183
private val source: IVideoSourceInternal?
185184
get() = sourceInternalFlow.value
186185

187-
private val _infoProviderFlow = MutableStateFlow<ISourceInfoProvider?>(null)
188-
val infoProviderFlow = _infoProviderFlow.asStateFlow()
186+
private val _inputConfigChanged = MutableStateFlow(Unit)
187+
val inputConfigChanged: StateFlow<Unit> = _inputConfigChanged.asStateFlow()
189188

190189
// CONFIG
191190
private val _sourceConfigFlow = MutableStateFlow<VideoSourceConfig?>(null)
@@ -208,10 +207,6 @@ internal class VideoInput(
208207
private val _isStreamingFlow = MutableStateFlow(false)
209208
override val isStreamingFlow = _isStreamingFlow.asStateFlow()
210209

211-
// OUTPUT
212-
private val outputMutex = Mutex()
213-
private val surfaceOutput = mutableListOf<ISurfaceOutput>()
214-
215210
override suspend fun setSource(videoSourceFactory: IVideoSourceInternal.Factory) {
216211
if (isReleaseRequested.get()) {
217212
throw IllegalStateException("Input is released")
@@ -261,7 +256,7 @@ internal class VideoInput(
261256

262257
infoProviderJob += coroutineScope.launch {
263258
newVideoSource.infoProviderFlow.collect {
264-
_infoProviderFlow.emit(it)
259+
_inputConfigChanged.emit(Unit)
265260
}
266261
}
267262

@@ -375,6 +370,7 @@ internal class VideoInput(
375370
videoSourceInternal.resetOutput()
376371
}
377372
currentSurfaceProcessor.removeInputSurface(it)
373+
_inputConfigChanged.emit(Unit)
378374
addSourceSurface(
379375
videoConfig,
380376
currentSurfaceProcessor,
@@ -403,19 +399,16 @@ internal class VideoInput(
403399
}
404400
}
405401

406-
private suspend fun buildSurfaceProcessor(
407-
videoSourceConfig: VideoSourceConfig
408-
): ISurfaceProcessorInternal {
402+
private suspend fun buildSurfaceProcessor(videoSourceConfig: VideoSourceConfig): ISurfaceProcessorInternal {
409403
val newSurfaceProcessor =
410404
surfaceProcessorFactory.create(
411405
videoSourceConfig.dynamicRangeProfile,
412406
dispatcherProvider
413407
)
414408
addSourceSurface(videoSourceConfig, newSurfaceProcessor)
415409

416-
outputMutex.withLock {
417-
surfaceOutput.forEach { newSurfaceProcessor.addOutputSurface(it) }
418-
}
410+
// Re-adds output surfaces
411+
_inputConfigChanged.emit(Unit)
419412

420413
return newSurfaceProcessor
421414
}
@@ -451,7 +444,7 @@ internal class VideoInput(
451444
isMirroringRequired: Boolean,
452445
isStreaming: () -> Boolean
453446
): ISurfaceOutput {
454-
val infoProvider = infoProviderFlow.value ?: DefaultSourceInfoProvider()
447+
val infoProvider = source?.infoProviderFlow?.value ?: DefaultSourceInfoProvider()
455448
val sourceResolution = infoProvider.getSurfaceSize(
456449
sourceConfig!!.resolution // build surface output is only called after config is set
457450
)
@@ -482,24 +475,16 @@ internal class VideoInput(
482475
)
483476
}
484477

485-
internal suspend fun addOutputSurface(output: ISurfaceOutput) {
478+
internal fun addOutputSurface(output: ISurfaceOutput) {
486479
if (isReleaseRequested.get()) {
487480
throw IllegalStateException("Input is released")
488481
}
489482

490-
outputMutex.withLock {
491-
surfaceOutput.add(output)
492-
processor.addOutputSurface(output)
493-
}
483+
processor.addOutputSurface(output)
494484
}
495485

496-
internal suspend fun removeOutputSurface(output: Surface) {
497-
outputMutex.withLock {
498-
surfaceOutput.firstOrNull { it.targetSurface == output }?.let {
499-
surfaceOutput.remove(it)
500-
}
501-
processor.removeOutputSurface(output)
502-
}
486+
internal fun removeOutputSurface(output: Surface) {
487+
processor.removeOutputSurface(output)
503488
}
504489

505490
private suspend fun startStreamUnsafe() {
@@ -580,11 +565,7 @@ internal class VideoInput(
580565
processor.removeInputSurface(it)
581566
}
582567
}
583-
outputMutex.withLock {
584-
surfaceOutput.clear()
585-
processor.removeAllOutputSurfaces()
586-
}
587-
568+
processor.removeAllOutputSurfaces()
588569
processor.release()
589570
}
590571

0 commit comments

Comments
 (0)