import gobject
gobject.threads_init()
import gst

class Gst(object):
    def __init__(self):
        self.loop = gobject.MainLoop()
        self.pipeline = gst.Pipeline()
        bus = self.pipeline.get_bus()
        bus.add_signal_watch()
        bus.connect('message::eos', self.bus_message_eos_cb)
        bus.connect('message::error', self.bus_message_error_cb)

        audiocomposition = gst.element_factory_make('gnlcomposition', 'audio-outer')
        audiocomposition.connect('pad-added', self.composition_pad_added_cb)
        audiogroupcomposition = gst.element_factory_make('gnlcomposition', 'audio')
        audiosource = gst.element_factory_make('gnlsource')
        audiotestsrc = gst.element_factory_make('audiotestsrc')
        audiosource.add(audiotestsrc)
        audiosource.props.start = 0
        audiosource.props.duration = 10 * gst.SECOND
        self.aqueue = gst.element_factory_make('queue')
        self.audioconvert = gst.element_factory_make('audioconvert')
        self.alsasink = gst.element_factory_make('alsasink')
        audiogroupcomposition.add(audiosource)
        audiogroupcomposition.props.start = 0
        audiogroupcomposition.props.duration = 10 * gst.SECOND
        audiocomposition.add(audiogroupcomposition)
        self.pipeline.add(self.aqueue, self.audioconvert, self.alsasink)
        gst.element_link_many(self.aqueue, self.audioconvert,
                self.alsasink)

        videocomposition = gst.element_factory_make('gnlcomposition', 'video-outer')
        videocomposition.connect('pad-added', self.composition_pad_added_cb)
        videogroupcomposition = gst.element_factory_make('gnlcomposition', 'video')
        videosource = gst.element_factory_make('gnlsource')
        videotestsrc = gst.element_factory_make('videotestsrc')
        videosource.add(videotestsrc)
        videosource.props.start = 0
        videosource.props.duration = 10 * gst.SECOND
        self.vqueue = gst.element_factory_make('queue')
        self.ffmpegcolorspace = gst.element_factory_make('ffmpegcolorspace')
        self.xvimagesink = gst.element_factory_make('xvimagesink')
        videogroupcomposition.add(videosource)
        videogroupcomposition.props.start = 0
        videogroupcomposition.props.duration = 10 * gst.SECOND
        videocomposition.add(videogroupcomposition)
        self.pipeline.add(self.vqueue, self.ffmpegcolorspace, self.xvimagesink)
        gst.element_link_many(self.vqueue, self.ffmpegcolorspace,
                self.xvimagesink)

        self.pipeline.add(audiocomposition)
        self.pipeline.add(videocomposition)

    def bus_message_eos_cb(self, bus, message):
        print 'EOS'
        self.loop.quit()
    
    def bus_message_error_cb(self, bus, message):
        error, debug = message.parse_error()
        print 'ERROR', error.message, debug
        self.loop.quit()

    def composition_pad_added_cb(self, composition, pad):
        caps = str(pad.get_caps())

        if caps.startswith('audio'):
            pad.link(self.aqueue.get_pad('sink'))
        else:
            pad.link(self.vqueue.get_pad('sink'))

    def run(self):
        self.pipeline.set_state(gst.STATE_PLAYING)
        self.loop.run()
        self.pipeline.set_state(gst.STATE_NULL)


if __name__ == '__main__':
    Gst().run()
