@@ -65,6 +65,7 @@ import kotlinx.coroutines.flow.filterNotNull
6565import kotlinx.coroutines.flow.first
6666import kotlinx.coroutines.flow.map
6767import kotlinx.coroutines.launch
68+ import kotlinx.coroutines.runBlocking
6869import kotlinx.coroutines.sync.Mutex
6970import kotlinx.coroutines.sync.withLock
7071
@@ -74,7 +75,12 @@ class PreviewViewModel(private val application: Application) : ObservableViewMod
7475
7576 private val buildStreamerUseCase = BuildStreamerUseCase (application, storageRepository)
7677
77- private val streamerFlow = MutableStateFlow (buildStreamerUseCase())
78+ private val streamerFlow =
79+ MutableStateFlow (
80+ SingleStreamer (
81+ application,
82+ runBlocking { storageRepository.isAudioEnableFlow.first() }) // TODO avoid runBlocking
83+ )
7884 private val streamer: SingleStreamer
7985 get() = streamerFlow.value
8086 val streamerLiveData = streamerFlow.asLiveData()
@@ -119,8 +125,9 @@ class PreviewViewModel(private val application: Application) : ObservableViewMod
119125 val endpointErrorLiveData: LiveData <String > = _endpointErrorLiveData
120126
121127 // Streamer states
128+ private val _isStreamingFlow = MutableStateFlow (false )
122129 val isStreamingLiveData: LiveData <Boolean >
123- get() = streamer.isStreamingFlow .asLiveData()
130+ get() = _isStreamingFlow .asLiveData()
124131 private val _isTryingConnectionLiveData = MutableLiveData <Boolean >()
125132 val isTryingConnectionLiveData: LiveData <Boolean > = _isTryingConnectionLiveData
126133
@@ -148,6 +155,7 @@ class PreviewViewModel(private val application: Application) : ObservableViewMod
148155 Log .i(TAG , " Video source is disabled" )
149156 }
150157
158+ // TODO: cancel jobs linked to previous streamer
151159 viewModelScope.launch {
152160 streamer.videoInput?.sourceFlow?.collect {
153161 notifySourceChanged()
@@ -174,8 +182,9 @@ class PreviewViewModel(private val application: Application) : ObservableViewMod
174182 }
175183 viewModelScope.launch {
176184 streamer.isStreamingFlow
177- .collect {
178- Log .i(TAG , " Streamer is streaming: $it " )
185+ .collect { isStreaming ->
186+ _isStreamingFlow .emit(isStreaming)
187+ Log .i(TAG , " Streamer is streaming: $isStreaming " )
179188 }
180189 }
181190 }
@@ -190,34 +199,30 @@ class PreviewViewModel(private val application: Application) : ObservableViewMod
190199 viewModelScope.launch {
191200 storageRepository.isAudioEnableFlow.combine(storageRepository.isVideoEnableFlow) { isAudioEnable, isVideoEnable ->
192201 Pair (isAudioEnable, isVideoEnable)
193- }.drop(1 ).collect { (_, _) ->
194- val previousStreamer = streamer
195- streamerFlow.emit(buildStreamerUseCase(previousStreamer))
196- if (previousStreamer != streamer) {
197- previousStreamer.release()
198- }
202+ }.drop(1 ).collect { (isAudioEnable, _) ->
203+ streamerFlow.emit(buildStreamerUseCase(streamer, isAudioEnable))
199204 }
200205 }
201206 viewModelScope.launch {
202- storageRepository.audioConfigFlow
207+ storageRepository.audioConfigFlow.filterNotNull()
203208 .collect { config ->
209+ if (! streamer.withAudio) {
210+ Log .i(TAG , " Audio is disabled. Skip setting audio config" )
211+ return @collect
212+ }
204213 if (ActivityCompat .checkSelfPermission(
205214 application,
206215 Manifest .permission.RECORD_AUDIO
207216 ) == PackageManager .PERMISSION_GRANTED
208217 ) {
209- config?.let {
210- streamer.setAudioConfig(it)
211- } ? : Log .i(TAG , " Audio is disabled" )
218+ streamer.setAudioConfig(config)
212219 }
213220 }
214221 }
215222 viewModelScope.launch {
216- storageRepository.videoConfigFlow
223+ storageRepository.videoConfigFlow.filterNotNull()
217224 .collect { config ->
218- config?.let {
219- streamer.setVideoConfig(it)
220- } ? : Log .i(TAG , " Video is disabled" )
225+ streamer.setVideoConfig(config)
221226 }
222227 }
223228 }
0 commit comments