Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pulseaudio: Retry until ready (w/ timeout) #417

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions src/print_volume.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ static char *apply_volume_format(const char *fmt, char *buffer, int ivolume, con
return buffer;
}

#if HAS_PULSEAUDIO
static bool description_pulseaudio_wait(uint32_t sink_idx,
const char *sink_name,
char buffer[MAX_SINK_DESCRIPTION_LEN]) {
static bool pulse_wait = true;
bool success = description_pulseaudio(sink_idx, sink_name, buffer);
if (success || !pulse_wait) {
return success;
}

pulse_wait = false;
for (int try = 0; try < 50 && !success; try ++) {
usleep(2000); /* Total maximum wait of 0.1 seconds */
success = description_pulseaudio(sink_idx, sink_name, buffer);
}
return success;
}
#endif

void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *fmt_muted, const char *device, const char *mixer, int mixer_idx) {
char *outwalk = buffer;
int pbval = 1;
Expand All @@ -85,20 +104,22 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *
* index of the PulseAudio sink then force PulseAudio, optionally
* overriding the default sink */
if (!strncasecmp(device, "pulse", strlen("pulse"))) {
if (!pulse_initialize()) {
/* Explicit pulseaudio device but initialization failure means we
* give up instead of fall-backs. */
goto out;
}

uint32_t sink_idx = device[strlen("pulse")] == ':' ? (uint32_t)atoi(device + strlen("pulse:")) : DEFAULT_SINK_INDEX;
const char *sink_name = device[strlen("pulse")] == ':' &&
!isdigit(device[strlen("pulse:")])
? device + strlen("pulse:")
: NULL;
int cvolume = 0;
char description[MAX_SINK_DESCRIPTION_LEN] = {'\0'};

if (pulse_initialize()) {
cvolume = volume_pulseaudio(sink_idx, sink_name);
/* false result means error, stick to empty-string */
if (!description_pulseaudio(sink_idx, sink_name, description)) {
description[0] = '\0';
}
bool success = description_pulseaudio_wait(sink_idx, sink_name, description);
int cvolume = volume_pulseaudio(sink_idx, sink_name);
if (!success || cvolume < 0) {
goto out;
}

int ivolume = DECOMPOSE_VOLUME(cvolume);
Expand All @@ -108,18 +129,15 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *
pbval = 0;
}

/* negative result means error, stick to 0 */
if (ivolume < 0)
ivolume = 0;
buffer = apply_volume_format(muted ? fmt_muted : fmt,
buffer,
ivolume,
description);
goto out_with_format;
} else if (!strcasecmp(device, "default") && pulse_initialize()) {
/* no device specified or "default" set */
char description[MAX_SINK_DESCRIPTION_LEN];
bool success = description_pulseaudio(DEFAULT_SINK_INDEX, NULL, description);
char description[MAX_SINK_DESCRIPTION_LEN] = {'\0'};
bool success = description_pulseaudio_wait(DEFAULT_SINK_INDEX, NULL, description);
int cvolume = volume_pulseaudio(DEFAULT_SINK_INDEX, NULL);
int ivolume = DECOMPOSE_VOLUME(cvolume);
bool muted = DECOMPOSE_MUTED(cvolume);
Expand Down
6 changes: 3 additions & 3 deletions src/pulse.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,16 @@ bool description_pulseaudio(uint32_t sink_idx, const char *sink_name, char buffe
buffer[sizeof(entry->description) - 1] = '\0';
return true;
}

pthread_mutex_unlock(&pulse_mutex);
/* first time requires a prime callback call because we only get updates
* when the description or volume actually changes, but we need it to be
* correct even if it never changes */
pa_threaded_mainloop_lock(main_loop);
get_sink_info(context, sink_idx, sink_name);
pa_threaded_mainloop_unlock(main_loop);
/* show empty string while we don't have this information */
buffer[0] = '\0';
return true;

return false;
}

/*
Expand Down