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

Nicolas Dufresne nicolas at ndufresne.ca
Wed May 31 15:14:18 UTC 2017


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
> gstreamer-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: This is a digitally signed message part
URL: <https://lists.freedesktop.org/archives/gstreamer-devel/attachments/20170531/c22966ee/attachment.sig>


More information about the gstreamer-devel mailing list