c++ - gstreamer audiomixer 命令到代码转换

标签 c++ c gstreamer-1.0

我想在我的应用程序中使用音频混合器,它接收来自不同来源的音频,并应在扬声器中一起播放它们。

我的最终应用程序应该执行类似以下命令的操作:

gst-launch-1.0 audiomixer name=mix ! autoaudiosink autoaudiosrc ! \
audioconvert ! mix. udpsrc port=5001 caps="application/x-rtp" ! queue !\
rtppcmudepay ! mulawdec ! audioconvert ! audioresample ! mix.

我已经编写了一个使用 tee 和队列的代码,并且知道如何在基于 this code 的代码中使用 tee 和队列。 。但我不知道如何在我的代码中使用混合器。

所以为了简单起见,我只想编写一段代码来像这个命令一样工作:

 gst-launch-1.0 audiotestsrc freq=100 ! audiomixer name=mix ! audioconvert ! autoaudiosink autoaudiosrc ! mix.

我没有找到任何有用的示例来实现此目标,我如何编写 C 代码来实现此目标?

最佳答案

第二部分:

 gst-launch-1.0 audiotestsrc freq=100 ! audiomixer name=mix ! audioconvert ! autoaudiosink autoaudiosrc ! mix.

此代码有效:

#include <gst/gst.h>
static GMainLoop *loop;
int bus_callback (GstBus *bus, GstMessage *message, gpointer data)
{
    g_print ("Got %s message\n", GST_MESSAGE_TYPE_NAME (message));
    switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR: {
        GError *err;
        gchar *debug;
        gst_message_parse_error (message, &err, &debug);
        g_print ("Error: %s\n", err->message);
        g_error_free (err);
        g_free (debug);
        g_main_loop_quit (loop);
        break;
    }
    case GST_MESSAGE_EOS:
        /* end-of-stream */
        g_main_loop_quit (loop);
        break;
    default:
        /* unhandled message */
        break;
    }
    /* we want to be notified again the next time there is a message
    * on the bus, so returning TRUE (FALSE means we want to stop watching
    * for messages on the bus and our callback should not be called again)
    */
    return TRUE;
}

int main(int argc, char *argv[])
{
    /* Initialize GStreamer */
    gst_init (nullptr, nullptr);
    GstElement *pipeline, *src1,*src2, *sink, *convert1,*convert2,*audiomixer;
    GstPad *conv_pad1, *conv_pad2, *mixer1_sinkpad,*mixer2_sinkpad;
    gint i;
    static GstBus *bus;
    static guint bus_watch_id;
    pipeline = gst_pipeline_new ("pipeline");
    audiomixer = gst_element_factory_make ("adder", "mixer");

    sink = gst_element_factory_make ("autoaudiosink", "sink");
    src1 = gst_element_factory_make ("audiotestsrc", "src1");
    convert1 = gst_element_factory_make ("audioconvert", "convert1");
    src2 = gst_element_factory_make ("autoaudiosrc", "src2");
    convert2 = gst_element_factory_make ("audioconvert", "convert2");
    //g_object_set (sink, "async-handling", TRUE, NULL);


    gst_bin_add_many (GST_BIN (pipeline), audiomixer ,sink, NULL);
    gst_bin_add_many (GST_BIN (pipeline), src1 , convert1 , NULL);
    gst_bin_add_many (GST_BIN (pipeline), src2 , convert2 , NULL);
    gst_element_link (src1, convert1 );
    gst_element_link (src2, convert2 );
    gst_element_link(audiomixer , sink);

    conv_pad1= gst_element_get_static_pad (convert1, "src");
    mixer1_sinkpad = gst_element_get_request_pad (audiomixer, "sink_%u");
    gst_pad_link (conv_pad1, mixer1_sinkpad);
    g_object_unref(mixer1_sinkpad);

    conv_pad2= gst_element_get_static_pad (convert2, "src");
    mixer2_sinkpad = gst_element_get_request_pad (audiomixer, "sink_%u");
    gst_pad_link (conv_pad2, mixer2_sinkpad);
    g_object_unref(mixer2_sinkpad);

    /* adds a watch for new message on our pipeline’s message bus to
    * the default GLib main context, which is the main context that our
    * GLib main loop is attached to below
    */
    bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
    bus_watch_id = gst_bus_add_watch (bus, bus_callback, NULL);
    gst_object_unref (bus);

    /* Start playing */
    gst_element_set_state (pipeline, GST_STATE_PLAYING);


    loop = g_main_loop_new (NULL, FALSE);

    g_main_loop_run (loop);
    g_object_unref(conv_pad1);
    g_object_unref(conv_pad2);
    gst_element_set_state (pipeline, GST_STATE_NULL);
    g_source_remove (bus_watch_id);
}

关于c++ - gstreamer audiomixer 命令到代码转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59928164/

相关文章:

c - 有没有关于如何在 C 中使用 GStreamer GstBaseTransform 的完整示例?

c++ - ftp 服务器的 makefile - 修改头文件时编译

c - 函数内的函数不起作用

c++ - 逻辑 CPU 计数返回 16 而不是 4

c - 使用非阻塞套接字时,recv 无响应

c - 套接字编程——connect()中参数的含义

ffmpeg - AVFrame : How to get/replace plane data buffer(s) and size?

gstreamer - 如何将数据从文件发送到 gstreamer 中的 webrtcbin 元素?

c++ - 可以删除继承的成员或阻止继承某些属性/方法吗?

c++ - 创建套接字 C++