@@ -150,14 +150,24 @@ namespace jni
150150 if (!pa.pa_threaded_mainloop_in_thread (mainloop))
151151 iterate(mainloop, op);
152152
153- op = pa.pa_context_get_source_info_by_name (context, defaultCaptureName.c_str (), getSourceInfoCallback, this );
153+ // Without a source the server names no default, and an empty name is no query at all.
154+ if (defaultCaptureName.empty ()) {
155+ defaultCaptureDescName.clear ();
156+ }
157+ else {
158+ op = pa.pa_context_get_source_info_by_name (context, defaultCaptureName.c_str (), getSourceInfoCallback, this );
154159
155- if (!pa.pa_threaded_mainloop_in_thread (mainloop))
156- iterate(mainloop, op);
160+ if (!pa.pa_threaded_mainloop_in_thread (mainloop))
161+ iterate(mainloop, op);
162+ }
157163
158164 if (!pa.pa_threaded_mainloop_in_thread (mainloop))
159165 pa.pa_threaded_mainloop_unlock (mainloop);
160166
167+ if (defaultCaptureName.empty () || defaultCaptureDescName.empty ()) {
168+ return nullptr ;
169+ }
170+
161171 AudioDevicePtr defaultDevice = std::make_shared<AudioDevice>(defaultCaptureDescName, defaultCaptureName);
162172 defaultDevice->directionType = AudioDeviceDirectionType::adtCapture;
163173 return defaultDevice;
@@ -191,14 +201,24 @@ namespace jni
191201 if (!pa.pa_threaded_mainloop_in_thread (mainloop))
192202 iterate(mainloop, op);
193203
194- op = pa.pa_context_get_sink_info_by_name (context, defaultPlaybackName.c_str (), getSinkInfoCallback, this );
204+ // Without a sink the server names no default, and an empty name is no query at all.
205+ if (defaultPlaybackName.empty ()) {
206+ defaultPlaybackDescName.clear ();
207+ }
208+ else {
209+ op = pa.pa_context_get_sink_info_by_name (context, defaultPlaybackName.c_str (), getSinkInfoCallback, this );
195210
196- if (!pa.pa_threaded_mainloop_in_thread (mainloop))
197- iterate(mainloop, op);
211+ if (!pa.pa_threaded_mainloop_in_thread (mainloop))
212+ iterate(mainloop, op);
213+ }
198214
199215 if (!pa.pa_threaded_mainloop_in_thread (mainloop))
200216 pa.pa_threaded_mainloop_unlock (mainloop);
201217
218+ if (defaultPlaybackName.empty () || defaultPlaybackDescName.empty ()) {
219+ return nullptr ;
220+ }
221+
202222 AudioDevicePtr defaultDevice = std::make_shared<AudioDevice>(defaultPlaybackDescName, defaultPlaybackName);
203223 defaultDevice->directionType = AudioDeviceDirectionType::adtRender;
204224 return defaultDevice;
@@ -226,12 +246,17 @@ namespace jni
226246
227247 auto & pa = PulseAudioLoader::instance ();
228248
229- if (last > 0 ) {
230- pa.pa_threaded_mainloop_signal (engine->mainloop , 0 );
249+ // A failed query, e.g. for a source removed after the server named it the default,
250+ // ends with last < 0 and no info, and nothing follows it.
251+ if (last < 0 ) {
252+ engine->defaultCaptureDescName .clear ();
253+ }
254+ else if (last == 0 && info) {
255+ engine->defaultCaptureDescName = info->description ? info->description : " " ;
231256 return ;
232257 }
233258
234- engine->defaultCaptureDescName = info-> description ;
259+ pa. pa_threaded_mainloop_signal ( engine->mainloop , 0 ) ;
235260 }
236261
237262 void PulseAudioDeviceManager::getSourceCallback (pa_context * ctx, const pa_source_info * info, int last, void * userdata)
@@ -266,13 +291,18 @@ namespace jni
266291 {
267292 PulseAudioDeviceManager * engine = reinterpret_cast <PulseAudioDeviceManager *>(userdata);
268293
269- if (last > 0 ) {
270- auto & pa = PulseAudioLoader::instance ();
271- pa.pa_threaded_mainloop_signal (engine->mainloop , 0 );
294+ // A failed query, e.g. for a sink removed after the server named it the default,
295+ // ends with last < 0 and no info, and nothing follows it.
296+ if (last < 0 ) {
297+ engine->defaultPlaybackDescName .clear ();
298+ }
299+ else if (last == 0 && info) {
300+ engine->defaultPlaybackDescName = info->description ? info->description : " " ;
272301 return ;
273302 }
274303
275- engine->defaultPlaybackDescName = info->description ;
304+ auto & pa = PulseAudioLoader::instance ();
305+ pa.pa_threaded_mainloop_signal (engine->mainloop , 0 );
276306 }
277307
278308 void PulseAudioDeviceManager::getSinkCallback (pa_context * ctx, const pa_sink_info * info, int last, void * userdata)
@@ -350,8 +380,9 @@ namespace jni
350380 auto & pa = PulseAudioLoader::instance ();
351381
352382 PulseAudioDeviceManager * engine = reinterpret_cast <PulseAudioDeviceManager *>(userdata);
353- engine->defaultCaptureName = info->default_source_name ;
354- engine->defaultPlaybackName = info->default_sink_name ;
383+ // A failed query has no info, and a server without sources or sinks names no default.
384+ engine->defaultCaptureName = info && info->default_source_name ? info->default_source_name : " " ;
385+ engine->defaultPlaybackName = info && info->default_sink_name ? info->default_sink_name : " " ;
355386
356387 pa.pa_threaded_mainloop_signal (engine->mainloop , 0 );
357388 }
0 commit comments