c - Pulseaudio C API:创建一个空接收器
问题描述
我正在尝试创建 2 个简单的程序,它们基本上是pulseaudio 文档中的 parec-simple 和 pacat-simple 示例。唯一的区别是我想创建一个空接收器,相当于
pactl load-module module-null-sink sink_name=steam
(请参见此处的示例)然后我将使用它来代替两端的默认设备 - 播放和录制。
我的问题是:如何使用 pulseaudio 的 C API 创建这个空接收器?从我所见,pulse/simple.h 不包含任何函数定义来执行此操作,所以我想我必须使用 libpulse。
解决方案
我找不到它的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;
}
推荐阅读
- groovy - How to compare values in groovy
- javascript - Javascript convert date and time today and save .txt file using its format
- java - TarsosDSP pitch shifting artifacts
- java - CriteriaBuilder - Debug - NamedParameterDescriptor and query.getParameters() - Console IDE / log - Show name parameters and values
- c - Embedded system: How to identify required tasks/threads?
- oracle - Oracle HCM 云 API - 按 HireDate 获取员工
- html - 使用flex居中动态div数
- spring-boot - 使用 kotlin 的 webflux 项目中的 WebExceptionHandler 实现不响应任何请求
- google-sheets - 谷歌表格时间计算;总结不起作用
- c - STM32 UART2 BRR 未给出预期结果