【问题标题】:Pulseaudio C API: create a null sinkPulseaudio C API:创建一个空接收器
【发布时间】:2020-08-23 17:32:55
【问题描述】:

我正在尝试创建 2 个简单的程序,它们基本上是来自 pulseaudio documentation 的 parec-simple 和 pacat-simple 示例。唯一的区别是我想创建一个空接收器,相当于

pactl load-module module-null-sink sink_name=steam 

(参见示例 here)然后我将使用它来代替两端的默认设备 - 播放和录制。

我的问题是:如何使用 pulseaudio 的 C API 创建这个空接收器?从我所见,pulse/simple.h 不包含任何函数定义来执行此操作,所以我想我必须使用 libpulse。

【问题讨论】:

  • 嘿 Copil,你找到办法了吗?
  • @Barthy:是的,请看下面的答案。

标签: c pulseaudio


【解决方案1】:

我找不到它的pulseaudio API,所以我直接使用pactl接口。

对于任何感兴趣的人,这里有一个对我有用的示例代码。我刚刚使用popen 执行pactl 命令来创建一个(空)接收器:

static bool createNullPaSink(const std::string& sinkName, int* paHandleId) {

    // this is the command to create the null sink with some specific setup (format, channels, rate, etc.0
    std::string pacmd = "pactl load-module module-null-sink latency_msec=100 format=s16le rate=48000 channels=2 channel_map=front-left,front-right sink_properties=device.description=QM sink_name=" + sinkName;
    // execute the pactl command
    FILE* f = popen(pacmd.c_str(), "r");
    if (!f) {
        // creating the sink failed
        return false;
    }
    
    // now get the handle of the sink by reading the output of the popen command above
    std::array<char, 128> buffer;
    std::string spaHandleId;
    while (fgets(buffer.data(), 128, f) != NULL) {
        spaHandleId += buffer.data();
    }
    auto returnCode = pclose(f);
    if (returnCode) {
        return false;
    }

    *paHandleId = std::stoi(spaHandleId);
    
    return true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多