0
如果ALSA混合器未正确通过用户碰撞与 alsa_get_unmute_volume配置:断言`ELEM”失败防止崩溃,如果断言失败(ALSA配置不正确)
static const char alsa_core_devnames[] = "default";
static char *card, *channel;
static int muted = 0;
static int mutecount = 0;
static snd_mixer_t *handle = NULL;
static snd_mixer_elem_t *elem = NULL;
static long alsa_min, alsa_max, alsa_vol;
static int alsa_get_unmute_volume(void)
{
long val;
assert(elem);
if (snd_mixer_selem_is_playback_mono(elem)) {
snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &val);
return val;
} else {
int c, n = 0;
long sum = 0;
for (c = 0; c <= SND_MIXER_SCHN_LAST; c++) {
if (snd_mixer_selem_has_playback_channel(elem, c)) {
snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &val);
sum += val;
n++;
}
}
if (! n) {
return 0;
}
val = sum/n;
sum = (long)((double)(alsa_vol * (alsa_max - alsa_min))/100. + 0.5);
if (sum != val) {
alsa_vol = (long)(((val * 100.)/(alsa_max - alsa_min)) + 0.5);
}
return alsa_vol;
}
}
碰撞
alsa_get_unmute_volume: Assertion `elem' failed. Aborted (core dumped)
有可能在代码中防止这种情况?
我认为这个问题就在这里。当用户手动设置,在gtkentry中,设备和通道的值错误,例如/ dev/mixer:line,然后按回车,程序崩溃
设置混频器设备和通道的代码(正确的值,例如:hw: 0 /线,默认/ CD,......)是这样的:
static int alsa_set_device(const char *devname)
{
int i;
if (card) free(card);
card = strdup(devname);
if(!card) return -1;
i = strcspn(card, "/");
if(i == strlen(card)) {
channel = "Line";
} else {
card[i] = 0;
channel = card + i + 1;
}
alsa_open_mixer();
if (!handle) {
fprintf(stderr, "mixer: Can't open mixer %s, "
"mixer volume and mute unavailable.\n", card);
return -1;
}
return 0;
}
感谢
你可以用-DNDEBUG构建来禁用断言...但是如果elem在这里是NULL,我不会期望你发布的代码做任何明智的事情。 – FatalError