c - Gstreamer:无法同时从 alsasrc 收听音频和录音?

标签 c gstreamer alsa

我正在编写一个 C 应用程序,它使用 gstreamer 来录制来自电视调谐器的音频。

我有一个电视调谐器卡,其中有一个声卡,例如 hw:1,0。 当应用程序使用 ALSA API 启动时,默认会访问音频源作为播放源。

我想编写一个函数来使用 GStreamer 后端录制来自该源的声音。

我的下一个问题似乎涉及一个微小的竞争条件,涉及我的 hw:1,0 已经在使用中并且需要能够记录一些内容。

我该怎么做才能让这项工作成功?

更新:播放声音不使用 GStreamer API,如果 alsa 和 libv4l2util 可用,它将使用 ALSA 功能自动在 V4L2 设备 (hw:1.0) 和音频输出设备 (hw:0.0) 之间启动音频流。

我尝试停止 ALSA 流并使用 Gstreamer element 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;
}

现在我可以听到声音,录音开始,但创建的录音文件不包含数据。

最佳答案

在更新的问题中,我可以看到encodebin已添加到管道中,但未链接到音频tee,这将是您的问题。在您当前的代码中,使用无用的 audiobin 和 savebin,我预计您在尝试执行此操作时也会收到错误,类似于“错误的元素层次结构”。摆脱这两个垃圾箱,它应该可以工作。

关于c - Gstreamer:无法同时从 alsasrc 收听音频和录音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27879037/

相关文章:

c - 使用SDL-C库,简单游戏中的鼠标运动错误

video - 如何将 360 视频从 PC 直播到 VR 耳机/护目镜/HMD?

python - 在 python 中导入 gtstreamer 时出现问题

c - snd_pcm_writei 延迟后不工作

audio - 无法播放已处理的音频数据(无符号字符)

c - C 中的按位运算

c - C 中 fgets() 和多个 strncmp() 的奇怪错误

c - timer_gettime 不适用于多个计时器

gstreamer:将录制从一个文件切换到另一个文件

c++ - 使用 ALSA 库的音频捕获 - snd_pcm_open => No such file or directory