Streaming images using appsrc

fgfernandez0321 fgfernandez0321 at gmail.com
Fri Feb 23 20:43:13 UTC 2018


Hi Arjen,

Thank you for your help. I followed your steps however not working as
expected. At the end, what I would like to achieve is to show the images
using autovideosink as video playing but using images instead of a video
with frames... I send you a zip with the source just in case you need it. 
SenderImages.zip
<http://gstreamer-devel.966125.n4.nabble.com/file/t377167/SenderImages.zip>  

Regards

This the code based on your explanation:

from PIL import Image as PILImage
from random import randint
import time
import gi
import io

gi.require_version('Gst', '1.0')
gi.require_version('GstApp', '1.0')
gi.require_version('Gtk', '3.0')

# noinspection PyUnresolvedReferences
from gi.repository import GObject, Gst, Gtk

GObject.threads_init()
Gst.init(None)


class SenderImages:
    def __init__(self):
        # Control if it is allowed push buffer in queue using "need-data"
and "enough-data" signals
        self.is_push_buffer_allowed = None

        self._mainloop = GObject.MainLoop()
        auto_video_sink_pipeline = "appsrc name=source !
image/jpeg,framerate=30/1 ! queue ! decodebin ! imagefreeze ! autovideosink"
        self._pipeline = Gst.parse_launch(auto_video_sink_pipeline)

        self._src = self._pipeline.get_by_name('source')
        self._src.connect('need-data', self.start_feed)
        self._src.connect('enough-data', self.stop_feed)

        self._src.set_property('format', 'bytes')
        self._src.set_property('do-timestamp', 'true')  # GstBase.BaseSrc

    def start_feed(self, src, length):
        print('======================> need data length: %s' % length)
        self.is_push_buffer_allowed = True

    def stop_feed(self, src):
        print('======================> enough_data')
        self.is_push_buffer_allowed = False

    def play(self):
        self._pipeline.set_state(Gst.State.PLAYING)

    def stop(self):
        self._pipeline.set_state(Gst.State.NULL)

    def run(self):
        """ Run - blocking. """
        self._mainloop.run()

    def push(self):
        """ Push a buffer into the source. """
        if self.is_push_buffer_allowed:
            image_number = randint(1, 4)
            filename = 'images/%s.jpg' % image_number

            img = PILImage.open(filename, mode='r')
            img_byte_array = io.BytesIO()
            img.save(img_byte_array, format='JPEG')
            img_byte_array = img_byte_array.getvalue()

            buffer = Gst.Buffer.new_wrapped(img_byte_array)
            gst_sample = Gst.Sample.new(buffer)
            gst_flow_return = self._src.emit('push-sample', gst_sample)

            if gst_flow_return != Gst.FlowReturn.OK:
                print('We got some error, stop sending data')

        else:
            print('It is enough data for buffer....')


sender = SenderImages()
sender.play()

index = 0
while index < 1000:
    print('========================= Showing picture...')
    sender.push()

    time.sleep(1)
    index += 1




--
Sent from: http://gstreamer-devel.966125.n4.nabble.com/


More information about the gstreamer-devel mailing list