import gobject
gobject.threads_init()
import gst

caps = gst.Caps("video/x-raw-yuv,format=(fourcc)AYUV,width=320,height=240")

mixer = gst.element_factory_make("videomixer")
mixer.props.background = 1
mixop = gst.element_factory_make("gnloperation")
mixop.add(mixer)
mixop.props.expandable = True
mixop.props.priority = 0

def makeSrc(start, pattern, priority):
    tst = gst.element_factory_make("videotestsrc")
    flt = gst.element_factory_make("capsfilter")
    tst.props.pattern = pattern
    flt.props.caps = caps
    srcbin = gst.Bin()
    srcbin.add(tst, flt)
    gst.element_link_many(tst, flt)
    srcbin.add_pad(gst.GhostPad("src", flt.get_pad("src")))

    src = gst.element_factory_make("gnlsource")
    src.add(srcbin)
    src.props.priority = priority
    src.props.start = start
    src.props.media_start = 0
    src.props.duration = 5 * gst.SECOND
    src.props.media_duration = 5 * gst.SECOND
    return src

controllers = []

def makeOp(priority):
    flt = gst.element_factory_make("queue")
    alpha = gst.element_factory_make("alpha")
    controller = gst.Controller(alpha, "alpha")
    controller.set_interpolation_mode("alpha", gst.INTERPOLATE_LINEAR)
    controller.set("alpha", 0 * gst.SECOND, 1)
    controller.set("alpha", 2.5 * gst.SECOND, 0)
    controllers.append(controller)

    alphabin = gst.Bin()
    alphabin.add(alpha, flt)
    gst.element_link_many(alpha, flt)
    alphabin.add_pad(gst.GhostPad("sink", alpha.get_pad("sink")))
    alphabin.add_pad(gst.GhostPad("src", flt.get_pad("src")))

    alphaop = gst.element_factory_make("gnloperation")
    alphaop.add(alphabin)
    alphaop.props.priority = priority 
    alphaop.props.start = long(2.5 * gst.SECOND)
    alphaop.props.media_start = 0
    alphaop.props.duration = long(2.5 * gst.SECOND)
    alphaop.props.media_duration = long(2.5 * gst.SECOND)
    alphaop.props.active = True
    return alphaop

def comp_pad_added(comp, pad):
    tpad = flt.get_pad("sink")
    pad.link(tpad)

comp = gst.element_factory_make("gnlcomposition")
comp.add(mixop)
comp.add(makeOp(1))
comp.add(makeSrc(0, 0, 2))
comp.add(makeSrc(long(2.5 * gst.SECOND), 1, 2))

comp.connect("pad-added", comp_pad_added)
flt = gst.element_factory_make("capsfilter")
flt.props.caps = caps
csp = gst.element_factory_make("ffmpegcolorspace")
sink = gst.element_factory_make("ximagesink")

p = gst.Pipeline()
p.add(comp, flt, csp, sink)
gst.element_link_many(flt, csp, sink)
p.set_state(gst.STATE_PLAYING)
ml = gobject.MainLoop()
ml.run()
