python, appsrc, signal "need-data" emitted only once

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

python, appsrc, signal "need-data" emitted only once

Jan Martinek-2
Hi everyone,

some time ago (2009?) I wrote a couple of Python scripts playing some
generated waveform on the speakers. It used to work fine.

Now my programs are obsolete and do not work. I made a very simplified
example:

------------------------
#!/usr/bin/python
import gobject
gobject.threads_init()
import gst, gtk
import random

class Sound:
     def __init__(self):
         self.player = gst.parse_launch('''appsrc name=source !
capsfilter
caps=audio/x-raw-int,rate=44100,channels=2,depth=16,signed=true !
gconfaudiosink''')
         playersrc = self.player.get_by_name('source')
         playersrc.connect('need-data', self.needdata)
         self.player.set_state(gst.STATE_PLAYING)
         gtk.main()

     def needdata(self, src, length):
         print 'need-data:', length
         data = ''
         for i in range(length):
             data = data + chr(random.randint(0, 255))
         src.emit('push-buffer', gst.Buffer(data))

sa = Sound()
-----------------

This should produce a noise from random data. It does not work because
"need-data" signal is called only once.

On the old system it plays a noise. Is is possible to make it work?

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

Re: python, appsrc, signal "need-data" emitted only once

Nicolas Dufresne-5
Le mardi 30 mai 2017 à 12:02 +0200, Jan Martinek a écrit :
> Hi everyone,
>
> some time ago (2009?) I wrote a couple of Python scripts playing
> some 
> generated waveform on the speakers. It used to work fine.
>
> Now my programs are obsolete and do not work. I made a very
> simplified 
> example:

All this code is written for GStreamer 0.10, which isn't supported
anymore.

>
> ------------------------
> #!/usr/bin/python
> import gobject
> gobject.threads_init()
> import gst, gtk

With 1.0 move, we now use gobject-instrospection.

> import gi
> gi.require_version("Gst", "1.0")
> gi.require_version('Gtk', '3.0')
> from gi.repository import Gst, Gtk

Notice the you need to convert a bit, gst to Gst, gtk to Gtk.

> import random
>
> class Sound:
>      def __init__(self):
>          self.player = gst.parse_launch('''appsrc name=source ! 
> capsfilter 
> caps=audio/x-raw-int,rate=44100,channels=2,depth=16,signed=true ! 
> gconfaudiosink''')

Caps filters have changed a bit. This would be "audio/x-
raw,rate=44100,channels=2,format=S16LE". gconfaudiosink no longer
exist, as gconf no longer exist. Use autoaudiosink instead.

>          playersrc = self.player.get_by_name('source')
>          playersrc.connect('need-data', self.needdata)
>          self.player.set_state(gst.STATE_PLAYING)
>          gtk.main()
>
>      def needdata(self, src, length):
>          print 'need-data:', length
>          data = ''
>          for i in range(length):
>              data = data + chr(random.randint(0, 255))
>          src.emit('push-buffer', gst.Buffer(data))
Buffers have considerably changed now. GstBuffer are containers for
GstMemory objects. GstMemory object need to be map/unmap in order to
access the data. This is a bit incompatible with python. For your use
case though, you can use Gst.Buffer.new_wrapped(). This will input a
python bytearray(), wrap it into a GstMemory and append it to a newly
allocated GstBuffer. Something like this:

> data = bytearray()
> for i in range(length):
>   data.append(random.randint(0, 255))
> src.emit('push-buffer', Gst.Buffer.new_wrapped(data))

>
> sa = Sound()

> -----------------
>
> This should produce a noise from random data. It does not work
> because 
> "need-data" signal is called only once.
>
> On the old system it plays a noise. Is is possible to make it work?
>
> Thank you
> Jan
> _______________________________________________
> gstreamer-devel mailing list
> [hidden email]
> https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel

signature.asc (188 bytes) Download Attachment