I'm a newbie in gstreamer
and I can simply use gst-launch
to play a PCM file like this:
$ gst-launch-1.0 filesrc location=output.pcm ! audio/x-raw, format=S16LE,
channels=1, rate=16000 ! autoaudiosink
But In my application, what I got is a char array with PCM raw data. After google it, I know that I should use appsrc
as a source, but the test code have no output at all.
Is there any example code can tell me how to play this char array which contains one channel 16K PCM raw data?
Here attached my test code:
std::ifstream file("output.pcm");
char data[22120];
file.read(data, 22120);
file.close();
std::cout << "read success";
GstElement *m_pipeline;
GstElement *m_source;
GMainLoop *m_loop;
GstBuffer *m_last_buffer;
GstElement *m_sink;
GstCaps *audioCaps;
gst_init(NULL, NULL);
audioCaps = gst_caps_new_simple("audio/x-raw", "format", G_TYPE_STRING, "S16LE", "rate", G_TYPE_INT, 16000,
"channels", G_TYPE_INT, 1, nullptr);
if (audioCaps == nullptr)
{
std::cout << "error" << std::endl;
return 0;
}
m_source = gst_element_factory_make("appsrc", "source");
m_sink = gst_element_factory_make("autoaudiosink", "sink");
m_pipeline = gst_pipeline_new("audio-pipeline");
m_loop = g_main_loop_new(NULL, FALSE);
gst_bin_add_many(GST_BIN(m_pipeline), m_source, m_sink, NULL);
gst_element_link_many(m_source, m_sink, NULL);
gst_app_src_set_caps(GST_APP_SRC(m_source), audioCaps);
gst_caps_unref(audioCaps);
GstBuffer *buffer = gst_buffer_new_allocate(NULL, 22120, NULL);
gst_buffer_fill(buffer, 0, data, 22120);
gst_app_src_push_buffer(GST_APP_SRC(m_source), buffer);
gst_element_set_state(m_pipeline, GST_STATE_PLAYING);
g_main_loop_run(m_loop);
/* free resources */
gst_element_set_state(m_pipeline, GST_STATE_NULL);
gst_object_unref(m_pipeline);