【发布时间】:2015-03-08 20:44:57
【问题描述】:
我正在编写一个使用 gstreamer 从电视调谐器录制音频的 C 应用程序。
我有一个电视调谐器卡,它有一个像 hw:1,0 这样的声卡。 当应用程序开始使用 ALSA API 时,音频源默认作为播放源访问。
我想编写一个函数来使用 GStreamer 后端记录来自该源的声音。
我的下一个问题似乎涉及一个微小的竞争条件,涉及我的 hw:1,0 已经在使用中并且需要能够记录一些东西。
我可以做些什么来完成这项工作?
更新:播放声音不使用 GStreamer API,如果 alsa 和 libv4l2util 可用,它将使用 ALSA 函数在 V4L2 设备 (hw:1.0) 和音频输出设备 (hw:0.0) 之间自动启动音频流。
我尝试停止 ALSA 流式传输并使用 Gstreamer 元素 tee:
int main (int argc, char *argv[])
{
GstCaps *filter;
GMainLoop *loop;
GstBus *bus;
GstElement *pipeline, *audiosource, *audiosink, *audioresample, *audiobin, *audiotee, *encodebin, *filesink, *savebin;
GstEncodingProfile *profile;
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
pipeline = gst_pipeline_new("pipeline");
audiosource = gst_element_factory_make("alsasrc", NULL);
audiosink = gst_element_factory_make("alsasink", NULL);
audioresample = gst_element_factory_make("audioresample", NULL);
audiotee = gst_element_factory_make("tee", NULL);
encodebin = gst_element_factory_make("encodebin", NULL);
filesink = gst_element_factory_make("filesink", NULL);
audiobin = gst_bin_new ("abin");
savebin = gst_bin_new ("sbin");
profile = gst_get_encoding_profile(my.settings.profile);
if (!pipeline || !audiosource || !audiosink || !audioresample || !audiobin)
return -1;
g_object_set(G_OBJECT(audiosource), "device", "hw:1,0", NULL);
g_object_set(G_OBJECT(filesink), "location", "save.ogg", NULL);
g_object_set(encodebin, "profile", profile, NULL);
gst_encoding_profile_unref(profile);
g_object_set(G_OBJECT(audiobin), "async-handling", TRUE, NULL);
g_object_set(G_OBJECT(savebin), "async-handling", TRUE, NULL);
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
gst_bin_add_many (GST_BIN (audiobin), audiosource, audioresample, audiotee, audiosink, NULL);
gst_bin_add_many (GST_BIN (savebin), encodebin, filesink, NULL);
gst_bin_add_many (GST_BIN (pipeline), audiobin, savebin, NULL);
filter = gst_caps_new_simple("audio/x-raw", "rate", G_TYPE_INT, 32000, NULL);
if (!gst_element_link_filtered(audiosource, audioresample, filter))
return -1;
if (!gst_element_link_many(audioresample, audiotee, audiosink, NULL))
return -1;
gst_element_set_state (pipeline, GST_STATE_PLAYING);
g_main_loop_run (loop);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (GST_OBJECT (pipeline));
return 0;
}
现在我可以听到声音,开始录音但创建的录音文件没有数据。
【问题讨论】:
-
这当然不是完整的代码,这些元素甚至没有链接并添加到管道中,而且我没有看到任何实际播放声音的元素.. 请用实际代码更新您的问题。