from gi.repository import Gst, Gtk, GLib

Gst.init([])

uri = "file:///home/lazka/Desktop/a.ogg"

i = 0

"""

This leaks per iteration:

 * 1 GstFakeSink
 * 1 GstGhostPad
 * 1 GstProxyPad
 * 1 GstPad

"""

def create_playbin():
    global i

    i += 1
    print(i)

    sink_bin = Gst.Bin()
    sink = Gst.ElementFactory.make('fakesink', None)
    sink_bin.add(sink)

    static = sink.get_static_pad('sink')
    gpad = Gst.GhostPad.new('sink', static)
    sink_bin.add_pad(gpad)

    playbin = Gst.ElementFactory.make('playbin', None)
    playbin.set_property("audio-sink", sink_bin)
    playbin.set_property('uri', uri)
    playbin.set_state(Gst.State.PLAYING)
    playbin.get_state(2 ** 64 - 1)
    playbin.set_state(Gst.State.NULL)
    playbin.get_state(2 ** 64 - 1)

    return True

GLib.unix_signal_add(0, 2, Gtk.main_quit)
GLib.idle_add(create_playbin)
w = Gtk.Window()
w.show_all()
Gtk.main()
