Skip to content

Commit 238e4bb

Browse files
committed
Modify the alsa help to use the hdmi: prefix for the HDMI devices it finds.
1 parent 19556bf commit 238e4bb

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

audio_alsa.c

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,70 @@ uint64_t frames_sent_for_playing;
279279
// frames_sent_for_playing (which Shairport Sync might hold) would be invalid.
280280
int frames_sent_break_occurred;
281281

282+
// if a device name ends in ",DEV=0", drop it. Then if it also begins with "CARD=", drop that too.
283+
static void simplify_and_printf_mutable_device_name(char *device_name) {
284+
if (strstr(device_name, ",DEV=0") == device_name + strlen(device_name) - strlen(",DEV=0")) {
285+
char *shortened_device_name = str_replace(device_name, ",DEV=0", "");
286+
char *simplified_device_name = str_replace(shortened_device_name, "CARD=", "");
287+
printf(" \"%s\"\n", simplified_device_name);
288+
free(simplified_device_name);
289+
free(shortened_device_name);
290+
} else {
291+
printf(" \"%s\"\n", device_name);
292+
}
293+
}
294+
282295
static void help(void) {
296+
283297
printf(" -d output-device set the output device, default is \"default\".\n"
284298
" -c mixer-control set the mixer control name, default is to use no mixer.\n"
285299
" -m mixer-device set the mixer device, default is the output device.\n"
286300
" -i mixer-index set the mixer index, default is 0.\n");
287-
int r = system("if [ -d /proc/asound ] ; then echo \" hardware output devices:\" ; ls -al "
288-
"/proc/asound/ 2>/dev/null | grep '\\->' | tr -s ' ' | cut -d ' ' -f 9 | while "
289-
"read line; do echo \" \\\"hw:$line\\\"\" ; done ; fi");
290-
if (r != 0)
291-
debug(2, "error %d executing a script to list alsa hardware device names", r);
301+
// look for devices with a name prefix of hw: or hdmi:
302+
int card_number = -1;
303+
snd_card_next(&card_number);
304+
305+
if (card_number < 0) {
306+
printf(" no hardware output devices found.\n");
307+
}
308+
309+
int at_least_one_device_found = 0;
310+
while (card_number >= 0) {
311+
void **hints;
312+
char *hdmi_str = NULL;
313+
char *hw_str = NULL;
314+
if (snd_device_name_hint(card_number, "pcm", &hints) == 0) {
315+
void **device_on_card_hints = hints;
316+
while (*device_on_card_hints != NULL) {
317+
char *device_on_card_name = snd_device_name_get_hint(*device_on_card_hints, "NAME");
318+
if ((strstr(device_on_card_name, "hw:") == device_on_card_name) && (hw_str == NULL))
319+
hw_str = strdup(device_on_card_name);
320+
if ((strstr(device_on_card_name, "hdmi:") == device_on_card_name) && (hdmi_str == NULL))
321+
hdmi_str = strdup(device_on_card_name);
322+
free(device_on_card_name);
323+
device_on_card_hints++;
324+
}
325+
snd_device_name_free_hint(hints);
326+
if ((hdmi_str != NULL) || (hw_str != NULL)) {
327+
if (at_least_one_device_found == 0) {
328+
printf(" hardware output devices:\n");
329+
at_least_one_device_found = 1;
330+
}
331+
}
332+
if (hdmi_str != NULL) {
333+
simplify_and_printf_mutable_device_name(hdmi_str);
334+
} else if (hw_str != NULL) {
335+
simplify_and_printf_mutable_device_name(hw_str);
336+
}
337+
if (hdmi_str != NULL)
338+
free(hdmi_str);
339+
if (hw_str != NULL)
340+
free(hw_str);
341+
}
342+
snd_card_next(&card_number);
343+
}
344+
if (at_least_one_device_found == 0)
345+
printf(" no hardware output devices found.\n");
292346
}
293347

294348
void set_alsa_out_dev(char *dev) { alsa_out_dev = dev; } // ugh -- not static!

0 commit comments

Comments
 (0)