#!/usr/bin/python

import gobject
gobject.threads_init()

import time
import pygst
pygst.require("0.10")
import gst

class ChangeEffect():

    def __init__(self):
        self.pipeline = gst.Pipeline("pipeline")

        self.testsrc = gst.element_factory_make("videotestsrc", None)
        self.testsrc.set_property('is-live', True)
        self.pipeline.add(self.testsrc)

        self.colorspace = []
        self.colorspace.append(gst.element_factory_make("ffmpegcolorspace", None))
        self.colorspace.append(gst.element_factory_make("ffmpegcolorspace", None))
        for element in self.colorspace:
            self.pipeline.add(element)

        self.queue = gst.element_factory_make("queue", None)
        self.queuesink = gst.element_factory_make("queue", None)

        self.pipeline.add(self.queue)
        self.pipeline.add(self.queuesink)

        self.effect = []
        self.effect.append(gst.element_factory_make("identity", None))
        self.effect.append(gst.element_factory_make("navigationtest", None))
        self.effect.append(gst.element_factory_make("agingtv", None))
        self.effect.append(gst.element_factory_make("videoflip", None))
        self.effect.append(gst.element_factory_make("vertigotv", None))
        self.pipeline.add(self.effect[0])

        self.capsfilter1 = gst.element_factory_make("capsfilter", None)
        self.pipeline.add(self.capsfilter1)
        caps = gst.caps_from_string(
                "video/x-raw-yuv, width=320, height=240, format=(fourcc)I420"
        )
        self.capsfilter1.set_property("caps", caps)


        self.capsfilter2 = gst.element_factory_make("capsfilter", None)
        self.pipeline.add(self.capsfilter2)
        caps = gst.caps_from_string(
                "video/x-raw-rgb"
        )
        self.capsfilter2.set_property("caps", caps)


        self.videosink = gst.element_factory_make("ximagesink", None)
        self.pipeline.add(self.videosink)
        self.videosink.set_property("force-aspect-ratio", True)

        gst.element_link_many(
                self.testsrc, self.capsfilter1, self.queue,
                self.colorspace[0],
                self.effect[0], self.colorspace[1], self.capsfilter2,
                self.queuesink, self.videosink
        )

        bus = self.pipeline.get_bus()
        bus.add_signal_watch()
        bus.connect("message", self.on_message)
        self.pipeline.set_state(gst.STATE_PLAYING);

        self.current_effect = self.effect[0]
        for new_effect in self.effect[1:5]:
            self.change_effect(self.current_effect, new_effect)
            self.current_effect = new_effect

        time.sleep(5)
        print "Quitting"
        self.pipeline.set_state(gst.STATE_NULL);

    def swap_element(self, swap_bin, previous_element, next_element,
        old_element, new_element):

        #here was queue
        #previous_pad = self.testsrc.get_static_pad("src")
        previous_pad = self.queue.get_static_pad("src")

        previous_pad.set_blocked(True)

        old_element.set_state(gst.STATE_NULL)
        previous_element.unlink(old_element)
        old_element.unlink(next_element)
        swap_bin.remove(old_element)

        swap_bin.add(new_element)
        new_element.link(next_element)
        previous_element.link(new_element)
        new_element.set_state(gst.STATE_PLAYING)
        previous_pad.set_blocked(False)
        swap_bin.set_state(gst.STATE_PLAYING)
        
        gst.DEBUG_BIN_TO_DOT_FILE_WITH_TS (self.pipeline, gst.DEBUG_GRAPH_SHOW_ALL, "debugging")
        return

    def change_effect(self, previous_element, new_element):
        print "Waiting 3 seconds"
        time.sleep(3)

        print "Adding next effect"
        self.swap_element(self.pipeline, self.colorspace[0], self.colorspace[1], previous_element, new_element)

    def on_message(self, bus, message):
        t = message.type
        if t == gst.MESSAGE_EOS:
            pipeline.set_state(gst.STATE_NULL)
        elif t == gst.MESSAGE_ERROR:
            print message
            pipeline.set_state(gst.STATE_NULL)

ChangeEffect()

