2013-02-07 62 views
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; 
} 

感謝

+0

你可以用-DNDEBUG構建來禁用斷言...但是如果elem在這裏是NULL,我不會期望你發佈的代碼做任何明智的事情。 – FatalError

回答

1

你將不得不重新編譯ALSA禁用斷言。當你定義NDEBUG斷言被禁用。

請注意,如果斷言失敗,即使您禁用斷言,您的程序可能會崩潰或得到不希望的行爲。

如果某個斷言某人在某處犯了錯誤,斷言就應該始終爲真。