#!/usr/bin/env python
# Transmageddon
# Copyright (C) 2009 Christian Schaller <uraeus@gnome.org>
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This librarmy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
#b You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import sys
import os
import gobject; gobject.threads_init()
import gst
import pygst
import glib
import gst.pbutils
import gtk

class Transcoder:
   def __init__(self):
       # create GStreamer pipeline object
       self.pipeline = gst.Pipeline("TranscodingPipeline")
       self.pipeline.set_state(gst.STATE_PAUSED)

       self.uridecoder = gst.element_factory_make("uridecodebin", "uridecoder") 
       self.uridecoder.set_property("uri", "file:///home/cschalle/Videos/gravity.mpg")
       self.uridecoder.connect("pad-added", self.OnDynamicPad)
       self.pipeline.add(self.uridecoder)

       self.containerprofile = gst.pbutils.EncodingContainerProfile ("ogg", None , gst.Caps("application/ogg"), None)
       self.videoprofile = gst.pbutils.EncodingVideoProfile (gst.Caps("video/x-dirac"), None, gst.caps_new_any(), 0)
       self.audioprofile = gst.pbutils.EncodingAudioProfile (gst.Caps("audio/x-vorbis"), None, gst.caps_new_any(), 0)
       self.containerprofile.add_profile(self.videoprofile)
       self.containerprofile.add_profile(self.audioprofile)

       self.ebin = gst.element_factory_make ("encodebin", None)
       self.ebin.set_property("profile", self.containerprofile)
       self.pipeline.add(self.ebin)

       print "self.ebin is " + str(self.ebin)
       self.ebin.set_state(gst.STATE_PAUSED)
       self.uridecoder.set_state(gst.STATE_PAUSED)
       self.filesink = gst.element_factory_make("filesink", None)
       self.filesink.set_property("location", "/tmp/test.ogg")
       self.pipeline.add(self.filesink)
       self.filesink.set_state(gst.STATE_PAUSED)
       self.ebin.link(self.filesink)

       self.pipeline.set_state(gst.STATE_PLAYING)

   def OnDynamicPad(self, uridecodebin, src_pad):
       c = src_pad.get_caps().to_string()
       sinkpad = self.ebin.emit("request-pad", src_pad.get_caps())
       if c.startswith("audio/"):
          src_pad.link(sinkpad)
       elif c.startswith("video/"):
           self.videoflipper = gst.element_factory_make("videoflip")
           self.pipeline.add(self.videoflipper)
           src_pad.link(self.videoflipper.get_static_pad("sink"))
           self.videoflipper.get_static_pad("src").link(sinkpad)
           self.videoflipper.set_state(gst.STATE_PAUSED)

if __name__ == "__main__":
	hwg = Transcoder()
	gtk.main()
