Appsrc produces silent audio
dingoegret
sephvelut at gmail.com
Sun Jun 25 22:28:13 UTC 2017
I have a simple application using appsrc. The produced file is blank. No
sound. How do timestamps work for buffers? If I set a buffer's pts to time
now() then wont that expire immediately after?
#include <gst/gst.h>
#include <string.h>
#include <ios>
#include <vector>
#include <fstream>
#include <gst/app/gstappsrc.h>
#include <iostream>
#define CHUNK_SIZE 10 * 1024 /* Amount of bytes we are sending in each
buffer */
#define SAMPLE_RATE 8000 /* Samples per second we are sending */
/* Structure to contain all our information, so we can pass it to callbacks
*/
typedef struct _CustomData {
GstElement *pipeline;
GstElement *app_source;
GstElement *filesink;
guint64 num_samples; /* Number of samples generated so far (for
timestamp generation) */
guint sourceid; /* To control the GSource */
GMainLoop *main_loop; /* GLib's Main Loop */
} CustomData;
std::ifstream file_stream("/Users/seph/Desktop/intro.wav");
std::vector<char> chunk(CHUNK_SIZE);
unsigned long timestamp = 0;
/* This method is called by the idle GSource in the mainloop, to feed
CHUNK_SIZE bytes into appsrc.
* The ide handler is added to the mainloop when appsrc requests us to start
sending data (need-data signal)
* and is removed when appsrc has enough data (enough-data signal).
*/
int c = 0;
static gboolean push_data(CustomData *data) {
GstBuffer *buffer;
GstFlowReturn ret;
GstMapInfo map;
unsigned long duration;
/* Create a new empty buffer */
buffer = gst_buffer_new_and_alloc (CHUNK_SIZE);
GST_BUFFER_TIMESTAMP(buffer) = timestamp;
duration = (unsigned long) (((double) CHUNK_SIZE / (SAMPLE_RATE * 1 *
(16 / 8))) * GST_SECOND);
GST_BUFFER_DURATION (buffer) = duration;
timestamp += duration;
std::cout << timestamp << std::endl;
gst_buffer_map(buffer, &map, GST_MAP_WRITE);
file_stream.rdbuf()->sgetn(&chunk[0], chunk.size());
map.data = (guint8 *) chunk.data();
gst_buffer_unmap(buffer, &map);
/* Push the buffer into the appsrc */
g_signal_emit_by_name(data->app_source, "push-buffer", buffer, &ret);
/* Free the buffer now that we are done with it */
gst_buffer_unref(buffer);
if (ret != GST_FLOW_OK) {
/* We got some error, stop sending data */
return FALSE;
}
// ending prematurely. no reason really
if(c == 4)
gst_app_src_end_of_stream((GstAppSrc *) data->app_source);
c++;
return TRUE;
}
/* This signal callback triggers when appsrc needs data. Here, we add an
idle handler
* to the mainloop to start pushing data into the appsrc */
static void start_feed(GstElement *source, guint size, CustomData *data) {
if (data->sourceid == 0) {
g_print("Start feeding\n");
data->sourceid = g_idle_add((GSourceFunc) push_data, data);
}
}
/* This callback triggers when appsrc has enough data and we can stop
sending.
* We remove the idle handler from the mainloop */
static void stop_feed(GstElement *source, CustomData *data) {
if (data->sourceid != 0) {
g_print("Stop feeding\n");
g_source_remove(data->sourceid);
data->sourceid = 0;
}
}
/* This function is called when an error message is posted on the bus */
static void error_cb(GstBus *bus, GstMessage *msg, CustomData *data) {
GError *err;
gchar *debug_info;
/* Print error details on the screen */
gst_message_parse_error(msg, &err, &debug_info);
g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME
(msg->src), err->message);
g_printerr("Debugging information: %s\n", debug_info ? debug_info :
"none");
g_clear_error(&err);
g_free(debug_info);
g_main_loop_quit(data->main_loop);
}
int main(int argc, char *argv[]) {
CustomData data;
GstBus *bus;
/* Initialize cumstom data structure */
memset(&data, 0, sizeof(data));
/* Initialize GStreamer */
gst_init(&argc, &argv);
/* setup pipeline */
data.pipeline = gst_pipeline_new("pipeline");
data.app_source = gst_element_factory_make("appsrc", NULL);
data.filesink = gst_element_factory_make("filesink", NULL);
g_assert(data.app_source);
g_assert(data.filesink);
g_assert(data.pipeline);
gst_bin_add_many(GST_BIN(data.pipeline), data.app_source, data.filesink,
NULL);
gst_element_link_many(data.app_source, data.filesink, NULL);
g_object_set(G_OBJECT (data.filesink), "location",
"/Users/seph/Desktop/test.wav", NULL);
//g_object_set (data.app_source, "caps", caps, "format",
GST_FORMAT_TIME, NULL);
g_signal_connect (data.app_source, "need-data", G_CALLBACK(start_feed),
&data);
g_signal_connect (data.app_source, "enough-data", G_CALLBACK(stop_feed),
&data);
/* Instruct the bus to emit signals for each received message, and
connect to the interesting signals */
bus = gst_element_get_bus(data.pipeline);
gst_bus_add_signal_watch(bus);
g_signal_connect (G_OBJECT(bus), "message::error", (GCallback) error_cb,
&data);
gst_object_unref(bus);
/* Start playing the pipeline */
gst_element_set_state(data.pipeline, GST_STATE_PLAYING);
/* Create a GLib Main Loop and set it to run */
data.main_loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(data.main_loop);
/* Free resources */
gst_element_set_state(data.pipeline, GST_STATE_NULL);
gst_object_unref(data.pipeline);
return 0;
}
--
View this message in context: http://gstreamer-devel.966125.n4.nabble.com/Appsrc-produces-silent-audio-tp4683530.html
Sent from the GStreamer-devel mailing list archive at Nabble.com.
More information about the gstreamer-devel
mailing list