【问题标题】:How can I get ALSA library functions to work in Apache CGI?如何让 ALSA 库函数在 Apache CGI 中工作?
【发布时间】:2021-10-23 22:49:17
【问题描述】:

以下代码打印出可用的 ALSA Midi 卡;默认情况下应该总是至少有一个。 但是,当通过 Apache CGI 访问时,snd_card_next() 函数无法正常工作,导致第一个 if 语句被激活并且找不到 ALSA 卡。

为什么 Apache CGI 无法处理这个函数?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <alsa/asoundlib.h>

#define NL  "\r\n"

void get_alsa_midi_cards(void)
{
    int card, status, cnt;
    int code;
    char *card_name;
    int i;
    
    card = -1;  // use -1 to prime the pump of iterating through card list
    status = snd_card_next(&card);
    if (status < 0 || card < 0) {
        printf ("Content-Type: text/html; charset=utf-8" NL "Cache-Control: no-cache" NL NL);
        printf("{\"alsa_midi_card\":[]}");
        //printf("Status: %i Card: %i", status, card);
        //printf("Status: 404 Not Found" NL NL);
        return;
    }   
    printf ("Content-Type: text/html; charset=utf-8" NL "Cache-Control: no-cache" NL NL);

    printf("{\"alsa_midi_card\":[");

    for (i = 0, cnt = 0; card >= 0; i++) {
        if ((code = snd_card_get_longname (card, &card_name)) < 0)
            continue;
        if (cnt++ > 0)
            printf(",");    
        printf("{\"longname\":\"");
        printf(card_name);
        printf("\"}");

        if ((status = snd_card_next (&card)) < 0)
            break;
    } 
    printf("]}");
} 

int main (int argc, char *argv[])
{
    get_alsa_midi_cards();

    return 0;
}

注意:要使用 gcc 编译代码,请添加 -lasound 标志。

【问题讨论】:

    标签: linux apache2 cgi


    【解决方案1】:

    需要将 Apache 用户(通常是 www-data)添加到“audio”组才能访问此功能。

    usermod -a -G audio www-data
    

    【讨论】:

      猜你喜欢
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多