C++ GStreamer 1.0-无法播放音频:错误:流格式错误

C++ GStreamer 1.0-无法播放音频:错误:流格式错误,c++,gstreamer,gstreamer-1.0,C++,Gstreamer,Gstreamer 1.0,我通过以下命令制作了一个.pcm音频文件: gst-launch-1.0 filesrc location=/home/pi/rawaudio/can-you-keep-a-secret.wav ! wavparse ! audioresample ! audioconvert ! audio/x-raw,format=S16BE,channels=1,rate=44100,layout=interleaved ! filesink location=/home/pi/rawaudio/test

我通过以下命令制作了一个.pcm音频文件:

gst-launch-1.0 filesrc location=/home/pi/rawaudio/can-you-keep-a-secret.wav ! wavparse ! audioresample ! audioconvert ! audio/x-raw,format=S16BE,channels=1,rate=44100,layout=interleaved ! filesink location=/home/pi/rawaudio/test.pcm
我可以用:

gst-launch-1.0 filesrc location=/home/pi/rawaudio/test.pcm ! audio/x-raw,format=S16BE,channels=1,rate=44100,layout=interleaved ! audioconvert ! audioresample ! alsasink
这是完美的工作。但是现在,我需要把它应用到我的C++应用程序中。这是我已经拥有的:

#include <string>
#include <stdio.h>
#include <gst/gst.h>
#include <gio/gio.h>
#include <boost/thread.hpp>

#define AUDIO_LOCATION "/home/pi/rawaudio/test.pcm"

static gboolean bus_call(GstBus *bus,
                         GstMessage *msg,
                         gpointer data) {
    GMainLoop *loop = (GMainLoop*)data;

    switch (GST_MESSAGE_TYPE(msg)) {
        case GST_MESSAGE_EOS:
            g_print ("End of stream\n");
            g_main_loop_quit(loop);
            break;
        case GST_MESSAGE_ERROR: {
            gchar  *debug;
            GError *error;

            gst_message_parse_error(msg, &error, &debug);
            g_free (debug);

            g_printerr("Error: %s\n", error->message);
            g_error_free(error);

            g_main_loop_quit(loop);
            break;
        }
        default:
            break;
    }
    return true;
}

int main (int argc, char **argv) {
    gst_init(&argc, &argv);

    GstElement *pipeline, *source, *parser, *sink, *convert;

    GMainLoop *loop;
    GstBus *bus;
    guint bus_watch_id;

    // loop
    loop = g_main_loop_new(NULL, false);
    // pipeline
    pipeline = gst_pipeline_new("test_pipeline");

    sink = gst_element_factory_make ("alsasink", "sink");
    source = gst_element_factory_make("filesrc", "source");
    g_object_set(G_OBJECT(source), "location", AUDIO_LOCATION, NULL);

    //convert = gst_element_factory_make("audioconvert", "convert");
    //parser = gst_element_factory_make("audioresample","parse");

    GstPad *sourcepad, *sinkpad;

    sourcepad = gst_element_get_static_pad(source, "src");
    gst_pad_set_caps(sourcepad,
        gst_caps_new_simple("audio/x-raw",
                            "rate", G_TYPE_INT, 44100,
                            "channels", G_TYPE_INT, 1,
                            "format", G_TYPE_STRING, "S16BE",
                            "layout", G_TYPE_STRING, "interleaved",
                            NULL));
    gst_object_unref(sourcepad);



    // bus
    bus = gst_pipeline_get_bus(GST_PIPELINE (pipeline));
    bus_watch_id = gst_bus_add_watch(bus, bus_call, loop);
    gst_object_unref(bus);

    // add elements into pipeline
    gst_bin_add_many(GST_BIN(pipeline), source, sink, NULL);
    // link source to decode
    gst_element_link_many(source, sink, NULL);

    // start playing
    gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING);

    // iterate
    g_print("Running...\n");
    g_main_loop_run(loop);

    // out of the main loop, clean up nicely
    g_print("Returned, stopping playback\n");
    gst_element_set_state(pipeline, GST_STATE_NULL);

    g_print("Deleting pipeline\n");
    gst_object_unref(GST_OBJECT(pipeline));
    g_source_remove (bus_watch_id);
    g_main_loop_unref(loop);

    return 0;
}
跑步的时候


我错过了什么?一些元素,如音频解析、音频转换、音频重采样。。。我还尝试在源代码之后将audioconvert和audioresample插入管道,但随后出现内部数据流错误异常。

您的编码版本与gst发布略有不同。启动行中的caps实际上是在创建一个capsfilter元素,而您的代码在pad上设置caps(推送一个粘性caps事件)。我猜它可能有不同的行为。您的编码版本与gst发布略有不同。启动行中的caps实际上是在创建一个capsfilter元素,而您的代码在pad上设置caps(推送一个粘性caps事件)。我想它可能有不同的行为。
Error: The stream is in the wrong format