#!/usr/bin/env python

from gi.repository import Gtk, Gst, GObject
from threading import Timer

GObject.threads_init()
Gst.init(None)
Gst.debug_set_active(True)

class Main(GObject.GObject):
    def __init__(self):
        GObject.GObject.__init__(self)

        self.videosrc = Gst.ElementFactory.make("ximagesrc", "video_src")
        self.videosrc.set_property("startx", 0)
        self.videosrc.set_property("starty", 0)
        self.videosrc.set_property("endx", 1919)
        self.videosrc.set_property("endy", 1079)
        self.videosrc.set_property("use-damage", False)
        self.videosrc.set_property("show-pointer", True)

        self.vid_caps = Gst.caps_from_string("video/x-raw,format=(x-raw-rgb),framerate=15/1")
        self.vid_caps_filter = Gst.ElementFactory.make("capsfilter", "vid_filter")
        self.vid_caps_filter.set_property("caps", self.vid_caps)

        self.videoconvert = Gst.ElementFactory.make("videoconvert", "videoconvert")
        self.videorate = Gst.ElementFactory.make("videorate", "video_rate")

        self.videnc = Gst.ElementFactory.make("vp8enc", "video_encoder")

        self.videnc.set_property("cpu-used", 2)
        self.videnc.set_property("end-usage", "vbr")
        self.videnc.set_property("target-bitrate", 800000000)
        self.videnc.set_property("static-threshold", 1000)
        self.videnc.set_property("token-partitions", 2)
        self.videnc.set_property("max-quantizer", 30)
        self.videnc.set_property("threads", 3)
        self.mux = Gst.ElementFactory.make("webmmux", "muxer")

        self.vid_in_queue = Gst.ElementFactory.make("queue", "queue_v1")
        self.vid_out_queue = Gst.ElementFactory.make("queue", "queue_v2")

        self.sink = Gst.ElementFactory.make("filesink", "sink")
        self.sink.set_property("location", "test-python.webm")
        self.file_queue = Gst.ElementFactory.make("queue", "queue_file")

        self.pipeline = Gst.Pipeline()

        self.pipeline.add(self.videosrc)
        self.pipeline.add(self.vid_in_queue)
        self.pipeline.add(self.videorate)
        self.pipeline.add(self.vid_caps_filter)
        self.pipeline.add(self.videoconvert)
        self.pipeline.add(self.vid_out_queue)
        self.pipeline.add(self.file_queue)
        self.pipeline.add(self.videnc)
        self.pipeline.add(self.mux)
        self.pipeline.add(self.sink)

        self.videosrc.link(self.vid_in_queue)
        self.vid_in_queue.link(self.videorate)
        self.videorate.link(self.vid_caps_filter)
        self.vid_caps_filter.link(self.videoconvert)
        self.videoconvert.link(self.videnc)
        self.videnc.link(self.vid_out_queue)
        self.vid_out_queue.link(self.mux)
        self.mux.link(self.file_queue)
        self.file_queue.link(self.sink)

        self.pipeline.set_state(Gst.State.PLAYING)

        t1 = Timer(700.0, self.stop_it)
        t2 = Timer(705.0, self.null_it)
        t1.start()
        t2.start()
        print "Pipeline playing."


    def stop_it(self):
        print "Stopping ..."
        self.pipeline.send_event(Gst.Event.new_eos())

    def null_it(self):
        print "Nulling ..."
        self.pipeline.set_state(Gst.State.NULL)
        Gtk.main_quit()

app = Main()
Gtk.main()
