Skip to content

Commit f203a75

Browse files
committed
feat(core): make output queueAudioFrame suspendable
1 parent 3c4e836 commit f203a75

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

core/src/androidTest/java/io/github/thibaultbee/streampack/core/pipelines/outputs/StubPipelineOutput.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ open class StubAudioAsyncPipelineOutput :
4343
private val _audioFrameFlow = MutableStateFlow<RawFrame?>(null)
4444
val audioFrameFlow = _audioFrameFlow.asStateFlow()
4545

46-
override fun queueAudioFrame(frame: RawFrame) {
47-
_audioFrameFlow.value = frame
46+
override suspend fun queueAudioFrame(frame: RawFrame) {
47+
_audioFrameFlow.emit(frame)
4848
}
4949
}
5050

@@ -84,8 +84,8 @@ class StubAudioSyncVideoSurfacePipelineOutput(resolution: Size) :
8484
private val _audioFrameFlow = MutableStateFlow<RawFrame?>(null)
8585
val audioFrameFlow = _audioFrameFlow.asStateFlow()
8686

87-
override fun queueAudioFrame(frame: RawFrame) {
88-
_audioFrameFlow.value = frame
87+
override suspend fun queueAudioFrame(frame: RawFrame) {
88+
_audioFrameFlow.emit(frame)
8989
}
9090
}
9191

@@ -150,8 +150,8 @@ class StubAudioSyncVideoSurfacePipelineOutputInternal(resolution: Size) :
150150
private val _audioFrameFlow = MutableStateFlow<RawFrame?>(null)
151151
val audioFrameFlow = _audioFrameFlow.asStateFlow()
152152

153-
override fun queueAudioFrame(frame: RawFrame) {
154-
_audioFrameFlow.value = frame
153+
override suspend fun queueAudioFrame(frame: RawFrame) {
154+
_audioFrameFlow.emit(frame)
155155
}
156156
}
157157

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ interface IAudioSyncPipelineOutputInternal : IAudioPipelineOutputInternal {
244244
*
245245
* @param frame The audio [RawFrame] to queue.
246246
*/
247-
fun queueAudioFrame(frame: RawFrame)
247+
suspend fun queueAudioFrame(frame: RawFrame)
248248
}
249249

250250
/**

core/src/main/java/io/github/thibaultbee/streampack/core/pipelines/outputs/encoding/EncodingPipelineOutput.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import kotlinx.coroutines.CoroutineDispatcher
5252
import kotlinx.coroutines.CoroutineScope
5353
import kotlinx.coroutines.Dispatchers
5454
import kotlinx.coroutines.cancel
55-
import kotlinx.coroutines.cancelChildren
5655
import kotlinx.coroutines.channels.Channel
5756
import kotlinx.coroutines.channels.consumeEach
5857
import kotlinx.coroutines.flow.MutableStateFlow
@@ -114,7 +113,13 @@ internal class EncodingPipelineOutput(
114113
override val surfaceFlow = _surfaceFlow.asStateFlow()
115114

116115
// ENCODERS
116+
private var audioInput: IEncoderInternal.ISyncByteBufferInput? = null
117+
117118
private var audioEncoderInternal: IEncoderInternal? = null
119+
set(value) {
120+
audioInput = value?.input as? IEncoderInternal.ISyncByteBufferInput
121+
field = value
122+
}
118123
override val audioEncoder: IEncoder?
119124
get() = audioEncoderInternal
120125

@@ -281,9 +286,8 @@ internal class EncodingPipelineOutput(
281286
}
282287
}
283288

284-
override fun queueAudioFrame(frame: RawFrame) {
285-
val encoder = requireNotNull(audioEncoderInternal) { "Audio is not configured" }
286-
val input = encoder.input as IEncoderInternal.ISyncByteBufferInput
289+
override suspend fun queueAudioFrame(frame: RawFrame) = mutex.withLock {
290+
val input = requireNotNull(audioInput) { "Audio input is null" }
287291
input.queueInputFrame(
288292
frame
289293
)

0 commit comments

Comments
 (0)