import sys
from gi.repository import Gtk
from gi.repository import Cheese
from gi.repository import Clutter
from gi.repository import Gst
from gi.repository import GObject
GObject.threads_init()
Gst.init(None)

import os

class VideoBox():
   def __init__(self):
       mainloop = GObject.MainLoop()
       # Create transcoding pipeline
       self.pipeline = Gst.Pipeline()

       self.videotest1 = Gst.ElementFactory.make('videotestsrc', None)
       self.videotest1.set_property("do-timestamp",True)
       self.pipeline.add(self.videotest1)

       self.videotest2 = Gst.ElementFactory.make('videotestsrc', None)
       self.videotest2.set_property("do-timestamp",True)
       self.pipeline.add(self.videotest2)

       self.audiotest1 = Gst.ElementFactory.make('audiotestsrc', None)
       self.pipeline.add(self.audiotest1)

       self.audiotest2 = Gst.ElementFactory.make('audiotestsrc', None)
       self.audiotest2.set_property("wave",6)
       self.pipeline.add(self.audiotest2)

       camera1caps = Gst.Caps.from_string("video/x-raw, width=320,height=240")
       self.camerafilter1 = Gst.ElementFactory.make("capsfilter", "filter1") 
       self.camerafilter1.set_property("caps", camera1caps)
       self.pipeline.add(self.camerafilter1)

       camera2caps = Gst.Caps.from_string("video/x-raw, width=320,height=240")
       self.camerafilter2 = Gst.ElementFactory.make("capsfilter", "filter2") 
       self.camerafilter2.set_property("caps", camera2caps)
       self.pipeline.add(self.camerafilter2)

       self.mediamuxer = Gst.ElementFactory.make("matroskamux", None)
       self.pipeline.add(self.mediamuxer)

       self.videoenc1 = Gst.ElementFactory.make("theoraenc", None)
       self.pipeline.add(self.videoenc1)

       self.videoenc2 = Gst.ElementFactory.make("theoraenc", None)
       self.pipeline.add(self.videoenc2)

       self.audioenc1 = Gst.ElementFactory.make("speexenc", None)
       self.pipeline.add(self.audioenc1)

       self.audioenc2 = Gst.ElementFactory.make("speexenc", None)
       self.pipeline.add(self.audioenc2)

       self.filesink = Gst.ElementFactory.make("filesink", None)
       self.filesink.set_property("location","/tmp/test.mkv")
       self.pipeline.add(self.filesink)

       self.videotest1.link(self.videoenc1)
       self.videoenc1.link(self.mediamuxer)
       
       self.videotest2.link(self.videoenc2)
       self.videoenc2.link(self.mediamuxer)
       
       self.audiotest1.link(self.audioenc1)
       self.audioenc1.link(self.mediamuxer)

       self.audiotest2.link(self.audioenc2)
       self.audioenc2.link(self.mediamuxer)

       self.mediamuxer.link(self.filesink)

       self.pipeline.set_state(Gst.State.PLAYING)
       mainloop.run()
   
if __name__ == "__main__":
    app = VideoBox()
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    exit_status = app.run(sys.argv)
    sys.exit(exit_status)
