Need an example for "How to play an audio data which is in memory as buffer" ?

Tim Müller tim at centricular.com
Wed Jul 22 11:30:19 PDT 2015


On Wed, 2015-07-22 at 08:25 -0700, Devesh Nagar wrote:

> How to insert stream data to GstBuffer and push it to the pipe line using
> appsrc 
> 
> I am able to play the audio using g_object_set (G_OBJECT (data.filesrc),
> "location", file name, NULL);
> then I inserted it to the pipeline. It works fine.
> 
> Now I am getting the stream data from trhe buffer. That is, I want to read
> the audio file and store it in buffer using fopen(). How to copy that buffer
> to GstBuffer using appsrc/memory_mapping  and push it to the pipeline?

If you know the file is fairly small, you could just do this to read it
into a single buffer:

GError *err = NULL;
gchar *contents = NULL;
gsize size = 0;

if (!g_file_get_contents ("/path/to/file", &contents, &size, &err
  g_printerr ("Error: %s\n", err->message);
  g_error_free (err);
  goto error;
}

GstBuffer *buf = gst_buffer_new_wrapped (contents, size);

Otherwise you could just do something like (untested):

#define BUF_SIZE (64*1024)

 do {
    GstMapInfo map;
    GstBuffer *buf;
    size_t ret;

    buf = gst_buffer_new_allocate (NULL, BUF_SIZE, NULL);
    gst_buffer_map (buf, &map, GST_MAP_WRITE);
    ret = fread (map.data, 1, map.size, data1.file);
    gst_buffer_unmap (buf, &map);
    if (ret > 0) {
      gst_buffer_resize (buf, ret);
      ... push buffer into appsrc...
    } else {
      gst_buffer_unref (buf);
    }
 } while (!feof (data1.file) && !ferror (data1.file));

But then you might just as well use filesrc instead...

 Cheers
  -Tim

> Here is my code snippet :
> 
> typedef struct _CustomData{
>   GstElement *pipeline;
>   GstAppSrc  *appsrc;
>   GstElement *convert;
>   GstElement *parser;
>   GstElement *decoder;
>   GstElement *sink;
>   GstElement *Sampler;
>   GstElement *typefind;
>   FILE *file;
>   guint sourceid;
> } CustomData;
> 
> main()
> {
>  CustomData data1;
> 
> //i am reading from file StreamData which contains stream data
> data1.file =
> fopen("/home/polaris/workspace/Gstreamer/myPrograms/StreamData", "r");
> 
> //then I createc elements and  and made a link to them and thus creating the
> pipeline. After that i am calling start_feed function as,
> 
>    g_signal_connect(data1.appsrc, "need-data", G_CALLBACK(start_feed),
> &data1);
>     g_signal_connect(data1.appsrc, "enough-data", G_CALLBACK(stop_feed),
> &data1);
> 
> //then we start playing by changing the state to play state using,
> gst_element_set_state ((GstElement *)data1.pipeline, GST_STATE_PLAYING)
> 
> }
> 
> 
> static void start_feed (GstElement * pipeline, guint size, CustomData *data)
> {
>  if (data->sourceid == 0) {
>   GST_DEBUG ("start feeding");
>   data->sourceid = g_idle_add ((GSourceFunc) read_data, data);
>  }
> }
> 
> static void stop_feed (GstElement * pipeline,CustomData *data)
> {
>  if (data->sourceid != 0) {
>   GST_DEBUG ("stop feeding");
>   g_source_remove (data->sourceid);
>   data->sourceid = 0;
>  }
> }
> 
> static gboolean read_data(CustomData *data)
> {
>  GstBuffer *buffer;
>  guint8 *ptr;
>  gint size;
>  GstFlowReturn ret;
>   int i;
> GstMemory *memory;
>  GstMapInfo info;
> 
>  ptr = g_malloc(BUFF_SIZE);
>  g_assert(ptr);
> 
>  size = fread(ptr, 1, BUFF_SIZE, data->file);
> 
>  if(size == 0){
>   ret = gst_app_src_end_of_stream(data->appsrc);
>   g_debug("eos returned %d at %d\n", ret, __LINE__);
>   return FALSE;
>  }
> 
>     buffer = gst_buffer_new ();
>     memory = gst_allocator_alloc (NULL, size, NULL);
>     gst_buffer_insert_memory (buffer, -1, memory);
>      gst_memory_map (memory, &info, GST_MAP_WRITE);
> 
>     for(i=0;i<info.size; i++)
>     {
>         info.data= ptr[i];
>     }
> 
>  ret = gst_app_src_push_buffer(data->appsrc, buffer);
> 
> }
> 
> 
> Is that mapping is proper? 
> Is copying the audio data to GstBuffer 
> proper?
> 
> Please Help
> 
> 
> 
> --
> View this message in context: http://gstreamer-devel.966125.n4.nabble.com/Need-an-example-for-How-to-play-an-audio-data-which-is-in-memory-as-buffer-tp4672763p4672792.html
> Sent from the GStreamer-devel mailing list archive at Nabble.com.
> _______________________________________________
> gstreamer-devel mailing list
> gstreamer-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/gstreamer-devel

-- 
Tim Müller, Centricular Ltd - http://www.centricular.com



More information about the gstreamer-devel mailing list