1
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject #,Gtk
from gi.repository import Gst as gst


class TakePhoto:

  def __init__(self):
    GObject.threads_init()
    gst.init(None)
    self.pipeline = gst.Pipeline()
    self.video_source = gst.ElementFactory.make('v4l2src', 'video_source')
    self.video_source.set_property("num-buffers",1)
    self.vconvert = gst.ElementFactory.make('videoconvert', 'vconvert')
    self.png = gst.ElementFactory.make('pngenc', 'png')
    self.multisink = gst.ElementFactory.make('multifilesink', 'multisink')
    self.multisink_pad = self.multisink.get_static_pad("sink")
    self.multisink_pad.add_probe(gst.PadProbeType.EVENT_UPSTREAM,self.probe_callback)

    self.caps = gst.caps_from_string("video/x-raw,format=RGB,width=800,height=600,framerate=5/1")
    self.png.set_property('snapshot',True)
    self.multisink.set_property('location','/home/pi/frame.png')
    self.filter = gst.ElementFactory.make("capsfilter", "filter")
    self.filter.set_property("caps", self.caps)


    self.pipeline.add(self.video_source)
    self.pipeline.add(self.vconvert)
    self.pipeline.add(self.filter)
    self.pipeline.add(self.png)
    self.pipeline.add(self.multisink)

    self.video_source.link(self.filter)
    self.filter.link(self.vconvert)
    self.vconvert.link(self.png)
    self.png.link(self.multisink)

def probe_callback(multisink_pad,info):
    info_event = info.get_event()
    info_structure = info_event.get_structure()
    do_something_with_this_info
    return Gst.PadProbeReturn.PASS


def take_photo(self): #this is reusable
    bus = self.pipeline.get_bus()
    self.pipeline.set_state(gst.State.PLAYING)
    print "Capture started"
    msg = bus.timed_pop_filtered(gst.CLOCK_TIME_NONE,gst.MessageType.ERROR | gst.MessageType.EOS)
    print msg

    self.pipeline.set_state(gst.State.NULL)

So what i want to do is that every time there is an event on either a source pad or sink pad, the probe callback function will be called in the main thread.

For the function "def probe_callback(multisink_pad,info):" When i tried to run this function, it given me an error that stated this:

(TypeError: probe_callback() takes exactly 2 arguments (1 given) )

I follow those code from this website: Gstreamer message to signal new frame from video source (webcam)

i tried and it didnt seem to work for me. Anybody can help me with this?

Community
  • 1
  • 1
Allen.Kan
  • 61
  • 1
  • 7
  • not exactly a python expert, but what about defining the callback function outside the class (maybe I am totally wrong).. in C++ when using classes I always define callbacks as plain C functions outside a class.. event callback is supposed to take 3 parameters: pad, info, user_data .. in user_data a pass usually `this` pointer or element pointer and use that later.. – nayana Jun 29 '16 at 12:21
  • check [this example](https://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/section-dynamic-pipelines.html) - search for GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM – nayana Jun 29 '16 at 12:23
  • Your indentation is a little awkward, `probe_callback` is supposed to be a method of your class `TakePhoto`? – mvarge Jun 29 '16 at 12:54

0 Answers0