Play audio from a memory buffer?

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

Play audio from a memory buffer?

bcg
Hi All,

This should almost be an FAQ but it's not in there so here goes:

I have a pipeline thus:

file-source ->  decodebin -> audioresample -> converter -> audio_output

Using location (or fd if I change file_source to a fdsrc) I can happily play most audio I throw at it (including mp3s) providing its in a file. What I would like to do is play from a memory location with the mp3 data already in it.

I am assuming I would need to use a fakesrc with a callback to get my data into the pipeline - I've tried lots of variations on this and gotten nowhere.

Can I just replace the file-source with the fakesrc, load the data and go (if this is possible couple someone provide an example)? Or do I have to worry about queues and caps? If so, can someone provide an example? Will decodebin suffice or will I have to be more specific?

Great software by the way, just tricky to use with mp3s (I know why...).

Thanks,
Brad
Reply | Threaded
Open this post in threaded view
|

Re: Play audio from a memory buffer?

Tim-Philipp Müller-2
On Tue, 2011-01-04 at 04:35 -0800, bcg wrote:

Hi,

> This should almost be an FAQ but it's not in there so here goes:
>
> I have a pipeline thus:
>
> file-source ->  decodebin -> audioresample -> converter -> audio_output
>
> Using location (or fd if I change file_source to a fdsrc) I can happily play
> most audio I throw at it (including mp3s) providing its in a file. What I
> would like to do is play from a memory location with the mp3 data already in
> it.
>
> I am assuming I would need to use a fakesrc with a callback to get my data
> into the pipeline - I've tried lots of variations on this and gotten
> nowhere.
>
> Can I just replace the file-source with the fakesrc, load the data and go
> (if this is possible couple someone provide an example)? Or do I have to
> worry about queues and caps? If so, can someone provide an example? Will
> decodebin suffice or will I have to be more specific?

Use appsrc.

Cheers
 -Tim



------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re: Play audio from a memory buffer?

Brad Goldsmith
Can appsrc be put in that pipeline in place of filesrc without any additional elements?

Cheers,
Brad


On 5 January 2011 01:55, Tim-Philipp Müller <[hidden email]> wrote:
On Tue, 2011-01-04 at 04:35 -0800, bcg wrote:

Hi,

> This should almost be an FAQ but it's not in there so here goes:
>
> I have a pipeline thus:
>
> file-source ->  decodebin -> audioresample -> converter -> audio_output
>
> Using location (or fd if I change file_source to a fdsrc) I can happily play
> most audio I throw at it (including mp3s) providing its in a file. What I
> would like to do is play from a memory location with the mp3 data already in
> it.
>
> I am assuming I would need to use a fakesrc with a callback to get my data
> into the pipeline - I've tried lots of variations on this and gotten
> nowhere.
>
> Can I just replace the file-source with the fakesrc, load the data and go
> (if this is possible couple someone provide an example)? Or do I have to
> worry about queues and caps? If so, can someone provide an example? Will
> decodebin suffice or will I have to be more specific?

Use appsrc.

Cheers
 -Tim



------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel


------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web.   Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl 
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
bcg
Reply | Threaded
Open this post in threaded view
|

Re: Play audio from a memory buffer?

bcg
In reply to this post by Tim-Philipp Müller-2
Can appsrc be put in that pipeline, in place of filesrc, without any
additional elements?

Cheers,
Brad

On Wed, Jan 5, 2011 at 1:55 AM, Tim-Philipp Müller <[hidden email]> wrote:

> On Tue, 2011-01-04 at 04:35 -0800, bcg wrote:
>
> Hi,
>
>> This should almost be an FAQ but it's not in there so here goes:
>>
>> I have a pipeline thus:
>>
>> file-source ->  decodebin -> audioresample -> converter -> audio_output
>>
>> Using location (or fd if I change file_source to a fdsrc) I can happily play
>> most audio I throw at it (including mp3s) providing its in a file. What I
>> would like to do is play from a memory location with the mp3 data already in
>> it.
>>
>> I am assuming I would need to use a fakesrc with a callback to get my data
>> into the pipeline - I've tried lots of variations on this and gotten
>> nowhere.
>>
>> Can I just replace the file-source with the fakesrc, load the data and go
>> (if this is possible couple someone provide an example)? Or do I have to
>> worry about queues and caps? If so, can someone provide an example? Will
>> decodebin suffice or will I have to be more specific?
>
> Use appsrc.
>
> Cheers
>  -Tim
>
>
>
> ------------------------------------------------------------------------------
> Learn how Oracle Real Application Clusters (RAC) One Node allows customers
> to consolidate database storage, standardize their database environment, and,
> should the need arise, upgrade to a full multi-node Oracle RAC database
> without downtime or disruption
> http://p.sf.net/sfu/oracle-sfdevnl
> _______________________________________________
> gstreamer-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
>

------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re: Play audio from a memory buffer?

rohitratri@gmail.com
It can be put in the pipeline. But you need to pump data into it from your application. Your application would be (sort of) the 'source' and appsrc is like a cheat code to make the following elements think that there is actually a source element in there at the start of the pipeline feeding them gstbuffers.
Try looking up the output format of filesrc element and package your data similarly and feed it to appsrc. Rest will be taken care by the bin/pipeline.

Rohit

On Wed, Jan 5, 2011 at 11:34 AM, Brad Goldsmith <[hidden email]> wrote:
Can appsrc be put in that pipeline, in place of filesrc, without any
additional elements?

Cheers,
Brad

On Wed, Jan 5, 2011 at 1:55 AM, Tim-Philipp Müller <[hidden email]> wrote:
> On Tue, 2011-01-04 at 04:35 -0800, bcg wrote:
>
> Hi,
>
>> This should almost be an FAQ but it's not in there so here goes:
>>
>> I have a pipeline thus:
>>
>> file-source ->  decodebin -> audioresample -> converter -> audio_output
>>
>> Using location (or fd if I change file_source to a fdsrc) I can happily play
>> most audio I throw at it (including mp3s) providing its in a file. What I
>> would like to do is play from a memory location with the mp3 data already in
>> it.
>>
>> I am assuming I would need to use a fakesrc with a callback to get my data
>> into the pipeline - I've tried lots of variations on this and gotten
>> nowhere.
>>
>> Can I just replace the file-source with the fakesrc, load the data and go
>> (if this is possible couple someone provide an example)? Or do I have to
>> worry about queues and caps? If so, can someone provide an example? Will
>> decodebin suffice or will I have to be more specific?
>
> Use appsrc.
>
> Cheers
>  -Tim
>
>
>
> ------------------------------------------------------------------------------
> Learn how Oracle Real Application Clusters (RAC) One Node allows customers
> to consolidate database storage, standardize their database environment, and,
> should the need arise, upgrade to a full multi-node Oracle RAC database
> without downtime or disruption
> http://p.sf.net/sfu/oracle-sfdevnl
> _______________________________________________
> gstreamer-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
>

------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel


------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
bcg
Reply | Threaded
Open this post in threaded view
|

Re: Play audio from a memory buffer?

bcg
The example:

appsrc-stream.c: example for using appsrc in streaming mode.

Fits the bill. It loads a file as a memory map but if you shoehorn in
your memory location and size at lines:

207  app->length = g_mapped_file_get_length (app->file);
208  app->data = (guint8 *) g_mapped_file_get_contents (app->file);

You'll have a working example of what I was after.

Cheers,
Brad




1 /* GStreamer
2 *
3 * appsrc-stream.c: example for using appsrc in streaming mode.
4 *
5 * Copyright (C) 2008 Wim Taymans <[hidden email]>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/gst.h>
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 GST_DEBUG_CATEGORY (appsrc_playbin_debug);
34 #define GST_CAT_DEFAULT appsrc_playbin_debug
35
36 /*
37 * an example application of using appsrc in streaming push mode.
We simply push
38 * buffers into appsrc. The size of the buffers we push can be any size we
39 * choose.
40 *
41 * This example is very close to how one would deal with a
streaming webserver
42 * that does not support range requests or does not report the
total file size.
43 *
44 * Some optimisations are done so that we don't push too much
data. We connect
45 * to the need-data and enough-data signals to start/stop sending buffers.
46 *
47 * Appsrc in streaming mode (the default) does not support seeking
so we don't
48 * have to handle any seek callbacks.
49 *
50 * Some formats are able to estimate the duration of the media
file based on the
51 * file length (mp3, mpeg,..), others report an unknown length (ogg,..).
52 */
53 typedef struct _App App;
54
55 struct _App
56 {
57  GstElement *playbin;
58  GstElement *appsrc;
59
60  GMainLoop *loop;
61  guint sourceid;
62
63  GMappedFile *file;
64  guint8 *data;
65  gsize length;
66  guint64 offset;
67 };
68
69 App s_app;
70
71 #define CHUNK_SIZE  4096
72
73 /* This method is called by the idle GSource in the mainloop. We
feed CHUNK_SIZE
74 * bytes into appsrc.
75 * The ide handler is added to the mainloop when appsrc requests us to start
76 * sending data (need-data signal) and is removed when appsrc has
enough data
77 * (enough-data signal).
78 */
79 static gboolean
80 read_data (App * app)
81 {
82  GstBuffer *buffer;
83  guint len;
84  GstFlowReturn ret;
85
86  buffer = gst_buffer_new ();
87
88  if (app->offset >= app->length) {
89    /* we are EOS, send end-of-stream and remove the source */
90    g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret);
91    return FALSE;
92  }
93
94  /* read the next chunk */
95  len = CHUNK_SIZE;
96  if (app->offset + len > app->length)
97    len = app->length - app->offset;
98
99  GST_BUFFER_DATA (buffer) = app->data + app->offset;
100  GST_BUFFER_SIZE (buffer) = len;
101
102  GST_DEBUG ("feed buffer %p, offset %" G_GUINT64_FORMAT "-%u", buffer,
103      app->offset, len);
104  g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret);
105  if (ret != GST_FLOW_OK) {
106    /* some error, stop sending data */
107    return FALSE;
108  }
109
110  app->offset += len;
111
112  return TRUE;
113 }
114
115 /* This signal callback is called when appsrc needs data, we add
an idle handler
116 * to the mainloop to start pushing data into the appsrc */
117 static void
118 start_feed (GstElement * playbin, guint size, App * app)
119 {
120  if (app->sourceid == 0) {
121    GST_DEBUG ("start feeding");
122    app->sourceid = g_idle_add ((GSourceFunc) read_data, app);
123  }
124 }
125
126 /* This callback is called when appsrc has enough data and we can
stop sending.
127 * We remove the idle handler from the mainloop */
128 static void
129 stop_feed (GstElement * playbin, App * app)
130 {
131  if (app->sourceid != 0) {
132    GST_DEBUG ("stop feeding");
133    g_source_remove (app->sourceid);
134    app->sourceid = 0;
135  }
136 }
137
138 /* this callback is called when playbin2 has constructed a source
object to read
139 * from. Since we provided the appsrc:// uri to playbin2, this will be the
140 * appsrc that we must handle. We set up some signals to start
and stop pushing
141 * data into appsrc */
142 static void
143 found_source (GObject * object, GObject * orig, GParamSpec *
pspec, App * app)
144 {
145  /* get a handle to the appsrc */
146  g_object_get (orig, pspec->name, &app->appsrc, NULL);
147
148  GST_DEBUG ("got appsrc %p", app->appsrc);
149
150  /* we can set the length in appsrc. This allows some elements
to estimate the
151   * total duration of the stream. It's a good idea to set the
property when you
152   * can but it's not required. */
153  g_object_set (app->appsrc, "size", app->length, NULL);
154
155  /* configure the appsrc, we will push data into the appsrc from the
156   * mainloop. */
157  g_signal_connect (app->appsrc, "need-data", G_CALLBACK
(start_feed), app);
158  g_signal_connect (app->appsrc, "enough-data", G_CALLBACK
(stop_feed), app);
159 }
160
161 static gboolean
162 bus_message (GstBus * bus, GstMessage * message, App * app)
163 {
164  GST_DEBUG ("got message %s",
165      gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
166
167  switch (GST_MESSAGE_TYPE (message)) {
168    case GST_MESSAGE_ERROR:
169      g_error ("received error");
170      g_main_loop_quit (app->loop);
171      break;
172    case GST_MESSAGE_EOS:
173      g_main_loop_quit (app->loop);
174      break;
175    default:
176      break;
177  }
178  return TRUE;
179 }
180
181 int
182 main (int argc, char *argv[])
183 {
184  App *app = &s_app;
185  GError *error = NULL;
186  GstBus *bus;
187
188  gst_init (&argc, &argv);
189
190  GST_DEBUG_CATEGORY_INIT (appsrc_playbin_debug, "appsrc-playbin", 0,
191      "appsrc playbin example");
192
193  if (argc < 2) {
194    g_print ("usage: %s <filename>\n", argv[0]);
195    return -1;
196  }
197
198  /* try to open the file as an mmapped file */
199  app->file = g_mapped_file_new (argv[1], FALSE, &error);
200  if (error) {
201    g_print ("failed to open file: %s\n", error->message);
202    g_error_free (error);
203    return -2;
204  }
205  /* get some vitals, this will be used to read data from the
mmapped file and
206   * feed it to appsrc. */
207  app->length = g_mapped_file_get_length (app->file);
208  app->data = (guint8 *) g_mapped_file_get_contents (app->file);
209  app->offset = 0;
210
211  /* create a mainloop to get messages and to handle the idle
handler that will
212   * feed data to appsrc. */
213  app->loop = g_main_loop_new (NULL, TRUE);
214
215  app->playbin = gst_element_factory_make ("playbin2", NULL);
216  g_assert (app->playbin);
217
218  bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin));
219
220  /* add watch for messages */
221  gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
222
223  /* set to read from appsrc */
224  g_object_set (app->playbin, "uri", "appsrc://", NULL);
225
226  /* get notification when the source is created so that we get a
handle to it
227   * and can configure it */
228  g_signal_connect (app->playbin, "deep-notify::source",
229      (GCallback) found_source, app);
230
231  /* go to playing and wait in a mainloop. */
232  gst_element_set_state (app->playbin, GST_STATE_PLAYING);
233
234  /* this mainloop is stopped when we receive an error or EOS */
235  g_main_loop_run (app->loop);
236
237  GST_DEBUG ("stopping");
238
239  gst_element_set_state (app->playbin, GST_STATE_NULL);
240
241  /* free the file */
242  g_mapped_file_free (app->file);
243
244  gst_object_unref (bus);
245  g_main_loop_unref (app->loop);
246
247  return 0;
248 }


On Wed, Jan 5, 2011 at 5:30 PM, [hidden email] [via
GStreamer-devel] <[hidden email]>
wrote:

> It can be put in the pipeline. But you need to pump data into it from your
> application. Your application would be (sort of) the 'source' and appsrc is
> like a cheat code to make the following elements think that there is
> actually a source element in there at the start of the pipeline feeding them
> gstbuffers.
> Try looking up the output format of filesrc element and package your data
> similarly and feed it to appsrc. Rest will be taken care by the
> bin/pipeline.
> Rohit
> On Wed, Jan 5, 2011 at 11:34 AM, Brad Goldsmith <[hidden email]> wrote:
>>
>> Can appsrc be put in that pipeline, in place of filesrc, without any
>> additional elements?
>>
>> Cheers,
>> Brad
>>
>> On Wed, Jan 5, 2011 at 1:55 AM, Tim-Philipp Müller <[hidden email]> wrote:
>> > On Tue, 2011-01-04 at 04:35 -0800, bcg wrote:
>> >
>> > Hi,
>> >
>> >> This should almost be an FAQ but it's not in there so here goes:
>> >>
>> >> I have a pipeline thus:
>> >>
>> >> file-source ->  decodebin -> audioresample -> converter -> audio_output
>> >>
>> >> Using location (or fd if I change file_source to a fdsrc) I can happily
>> >> play
>> >> most audio I throw at it (including mp3s) providing its in a file. What
>> >> I
>> >> would like to do is play from a memory location with the mp3 data
>> >> already in
>> >> it.
>> >>
>> >> I am assuming I would need to use a fakesrc with a callback to get my
>> >> data
>> >> into the pipeline - I've tried lots of variations on this and gotten
>> >> nowhere.
>> >>
>> >> Can I just replace the file-source with the fakesrc, load the data and
>> >> go
>> >> (if this is possible couple someone provide an example)? Or do I have
>> >> to
>> >> worry about queues and caps? If so, can someone provide an example?
>> >> Will
>> >> decodebin suffice or will I have to be more specific?
>> >
>> > Use appsrc.
>> >
>> > Cheers
>> >  -Tim
>> >
>> >
>> >
>> >
>> > ------------------------------------------------------------------------------
>> > Learn how Oracle Real Application Clusters (RAC) One Node allows
>> > customers
>> > to consolidate database storage, standardize their database environment,
>> > and,
>> > should the need arise, upgrade to a full multi-node Oracle RAC database
>> > without downtime or disruption
>> > http://p.sf.net/sfu/oracle-sfdevnl
>> > _______________________________________________
>> > gstreamer-devel mailing list
>> > [hidden email]
>> > https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
>> >
>>
>>
>> ------------------------------------------------------------------------------
>> Learn how Oracle Real Application Clusters (RAC) One Node allows customers
>> to consolidate database storage, standardize their database environment,
>> and,
>> should the need arise, upgrade to a full multi-node Oracle RAC database
>> without downtime or disruption
>> http://p.sf.net/sfu/oracle-sfdevnl
>> _______________________________________________
>> gstreamer-devel mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
>
>
> ------------------------------------------------------------------------------
> Learn how Oracle Real Application Clusters (RAC) One Node allows customers
> to consolidate database storage, standardize their database environment,
> and,
> should the need arise, upgrade to a full multi-node Oracle RAC database
> without downtime or disruption
> http://p.sf.net/sfu/oracle-sfdevnl
> _______________________________________________
> gstreamer-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
>
>
> ________________________________
> View message @
> http://gstreamer-devel.966125.n4.nabble.com/Play-audio-from-a-memory-buffer-tp3173448p3174884.html
> To unsubscribe from Play audio from a memory buffer?, click here.