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

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Devesh Nagar
I have played audio files like mp3 and sbc format files by simply defining decoder/converter and giving that file to gstreamer.
But here I dont want to play directly with file.
I want to open the audio file and read the content in to buffer, and feed that buffer into gstreamer to play.

Need an example how to do that.
Please Help
Reply | Threaded
Open this post in threaded view
|

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

Tim Müller
On Tue, 2015-07-21 at 00:45 -0700, Devesh Nagar wrote:

Hi,

> I have played audio files like mp3 and sbc format files by simply defining
> decoder/converter and giving that file to gstreamer.
> But here I dont want to play directly with file.
> I want to open the audio file and read the content in to buffer, and feed
> that buffer into gstreamer to play.

You can either feed your memory to the pipeline via appsrc, or you can
use giostreamsrc with a GMemoryInputStream like in this code:
http://cgit.freedesktop.org/gstreamer/gst-plugins-base/tree/tests/check/pipelines/gio.c#n60

Cheers
 -Tim

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

_______________________________________________
gstreamer-devel mailing list
[hidden email]
http://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

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

Devesh Nagar
This post was updated on .
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?

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?

error is :
It  sets the play state but suddenly goes to failure state (i think crashing)

Please Help
Reply | Threaded
Open this post in threaded view
|

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

Tim Müller
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
> [hidden email]
> http://lists.freedesktop.org/mailman/listinfo/gstreamer-devel

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

_______________________________________________
gstreamer-devel mailing list
[hidden email]
http://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

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

karthik
I tried this code for feeding the Gstbuffer to the pipe line using appsrc. Its palying!!!
But after sometime audio is not able to here. But Pushing to pipe line is success. I am pushing the buffer for 50 miliseconds. Is it because of lateency ? Do we take care about latency when pushing the buffer. How to get or set the latency of gstbuffer?

Need help.....
Reply | Threaded
Open this post in threaded view
|

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

karthik
Also.....what is the maximum size of GstBuffer?
Reply | Threaded
Open this post in threaded view
|

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

Devesh Nagar
In reply to this post by Tim Müller
Thanks Tim for you support
Your second code snippet is working...


Regards
Devesh
Reply | Threaded
Open this post in threaded view
|

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

karthik
In reply to this post by Tim Müller
what is the replacement for gst_buffer_map () API in gstreamer 0.1 version.
or how to feed the data to the GstBuffer in 0.1 version of gstreamer
Reply | Threaded
Open this post in threaded view
|

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

Nicolas Dufresne-3
Le mardi 29 septembre 2015 à 10:23 -0700, karthik a écrit :
> what is the replacement for gst_buffer_map () API in gstreamer 0.1
> version.
> or how to feed the data to the GstBuffer in 0.1 version of gstreamer

You most likely mean GStreamer 0.10. We don't recommend or support new
development around this version of GStreamer (or any older versions).
These version are now abandoned and receive no fixes (not even security
fixes). So I hope this question is about maintaining an old legacy
software.

** WARNING to other people reading: This is all deprecated stuff, don't
follow these instructions **

In GStreamer 0.10, there was no memory locking mechanism. So you access
buffer->data pointer (or use GST_BUFFER_DATA macro). Only 1 data
pointer was allowed.

http://www.freedesktop.org/software/gstreamer-sdk/data/docs/2012.5/gstr
eamer-0.10/gstreamer-GstBuffer.html#GstBuffer
_______________________________________________
gstreamer-devel mailing list
[hidden email]
http://lists.freedesktop.org/mailman/listinfo/gstreamer-devel

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

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

karthik
In reply to this post by Tim Müller
Hi,

After some operation of play and pause, Gstreamer state is not changing to play or pause even though i am setting the state using gst_element_set_state(). What could be the reason?
Please help me to solve this issue.
Reply | Threaded
Open this post in threaded view
|

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

Sebastian Dröge-3
On Do, 2016-04-28 at 22:32 -0700, karthik wrote:
> Hi,
>
> After some operation of play and pause, Gstreamer state is not
> changing to play or pause even though i am setting the state using
> gst_element_set_state(). What could be the reason?
> Please help me to solve this issue.

Check if gst_element_set_state() returns an error, and if you get any
error messages on the bus. Also check in the debug logs if something
looks off.

Not being able to go to PAUSED without any error can also mean that not
all sinks receive data, as they only reach the PAUSED state then and
the pipeline will only reach the PAUSED state once all received data.
You can disable this behaviour with the async property on the sinks,
but this is usually a bad idea and in your case would probably be a
workaround for another problem.

What does your pipeline look like?

--
Sebastian Dröge, Centricular Ltd · http://www.centricular.com


_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel

signature.asc (968 bytes) Download Attachment