ALSA上的操作不允许获取hw参数功能

时间:2014-04-03 07:21:11

标签: c alsa

我正在尝试从服务器播放pcm音频缓冲区。我下载了一个示例alsa播放文件,将recoreded文件作为输入工作正常,但我在SIP客户端应用程序中添加的相同代码正在获取操作不允许错误。< / p>

设备的打开设备和设置配置是正常的,但尝试获取我配置的参数,给出操作不允许错误。   有谁能告诉我为什么会收到这个错误?

/* Open the PCM device in playback mode */
    if (pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE,
                                    SND_PCM_STREAM_PLAYBACK, 0) < 0)
            printf("ERROR: Can't open \"%s\" PCM device. %s\n",
                                    PCM_DEVICE, snd_strerror(pcm));

    /* Allocate parameters object and fill it with default values*/
    snd_pcm_hw_params_alloca(&params);

    snd_pcm_hw_params_any(pcm_handle, params);

    /* Set parameters */
    if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params,
                                    SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
            printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));

    if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params,
                                            //SND_PCM_FORMAT_S16_LE) < 0) 
                                            SND_PCM_FORMAT_MU_LAW) < 0)
            printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));

    if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0)
            printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));

    if (pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0)
            printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));

    /* Write parameters */
    if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0)
            printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));


   /* Resume information */
    printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));

    printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));

    pcm =  snd_pcm_hw_params_get_channels(params, &tmp);
    printf("channels: %i  %d", tmp, pcm);

    if (tmp == 1)
            printf("(mono)\n");
    else if (tmp == 2)
            printf("(stereo)\n");

    snd_pcm_hw_params_get_rate(params, &tmp, 0);
    printf("rate: %d bps\n", tmp);

    printf("seconds: %d\n", seconds);

    /* Allocate buffer to hold single period */
    snd_pcm_hw_params_get_period_size(params, &frames, 0);

    buff_size = frames * channels * 2 /* 2 -> sample size */;
    buff = (char *) malloc(buff_size);

    printf("buffsize: %d\n", buff_size);
    snd_pcm_hw_params_get_period_time(params, &tmp, NULL);

1 个答案:

答案 0 :(得分:1)

您的所有错误检查都是错误的。

<运算符绑定强于=,所以在这样的行中:

if (err = snd_something(...) < 0)

将函数的返回值与零进行比较,并将该比较的结果(false或true,0或1)分配给变量。

要使其正常工作,您必须在作业周围添加括号:

if ((err = snd_something(...)) < 0)

但不要尝试将所有内容都放在一个表达式中,这是一个更好的主意:

err = snd_something(...);
if (err < 0)