

from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkX11
from gi.repository import GLib
from gi.repository import Gst
from gi.repository import GObject
from gi.repository import GstVideo


Gdk.threads_init ()
GObject.threads_init()
Gst.init(None)

class DialogExample(Gtk.Dialog):

    def __init__(self, parent):
        Gtk.Dialog.__init__(self, "My Dialog", parent, 0,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OK, Gtk.ResponseType.OK))

        self.set_default_size(150, 100)

        self.drawing_area = Gtk.DrawingArea()
        self.drawing_area.set_size_request(600,300)


        box = self.get_content_area()
        box.add(self.drawing_area)

        self.player = Gst.Pipeline()

        self.bus = self.player.get_bus()
        self.bus.add_signal_watch()
        self.bus.connect('message::error', self.on_error)

        self.bus.enable_sync_message_emission()
        self.bus.connect('sync-message::element', self.on_sync_message)

        self.src = Gst.ElementFactory.make('videotestsrc', None)
        self.sink = Gst.ElementFactory.make('ximagesink', None)
        self.audiosrc = Gst.ElementFactory.make('audiotestsrc', None)
        self.audiosrc.set_property('wave', 8)
        self.audiosink = Gst.ElementFactory.make('autoaudiosink', None)

        self.player.add (self.src)
        self.player.add(self.sink)
        self.player.add(self.audiosrc)
        self.player.add(self.audiosink)

        self.src.link (self.sink)
        self.audiosrc.link(self.audiosink)

        self.show_all()

        self.xid = self.drawing_area.get_property('window').get_xid()
        self.player.set_state(Gst.State.PLAYING)

    def on_sync_message (self, bus, msg):
        if msg.get_structure().get_name() == 'prepare-window-handle':
            print('prepare-window-handle')
            print self.xid
            msg.src.set_window_handle(self.xid)

    def on_error(self, bus, msg):
        print msg.parse_error()


class DialogWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Dialog Example")

        self.set_border_width(6)

        button = Gtk.Button("Open dialog")
        button.connect("clicked", self.on_button_clicked)

        self.drawing_area = Gtk.DrawingArea()
        self.drawing_area.set_size_request(600,300)

        box = Gtk.VBox ()
        box.set_spacing (8)
        box.pack_start (self.drawing_area, False, False, 0)
        box.pack_start (button, False, False, 0)


        self.add(box)
        self.player = Gst.Pipeline()

        self.bus = self.player.get_bus()
        self.bus.add_signal_watch()
        self.bus.connect('message::error', self.on_error)

        self.bus.enable_sync_message_emission()
        self.bus.connect('sync-message::element', self.on_sync_message)


        self.src = Gst.ElementFactory.make('videotestsrc', None)
        self.sink = Gst.ElementFactory.make('ximagesink', None)
        self.audiosrc = Gst.ElementFactory.make('audiotestsrc', None)
        self.audiosrc.set_property('wave', 8)
        self.audiosrc.set_property('freq', 220)
        self.audiosink = Gst.ElementFactory.make('autoaudiosink', None)

        self.player.add (self.src)
        self.player.add(self.sink)
        self.player.add(self.audiosrc)
        self.player.add(self.audiosink)

        self.src.link (self.sink)
        self.audiosrc.link(self.audiosink)

        self.show_all()

        self.xid = self.drawing_area.get_property('window').get_xid()
        self.player.set_state(Gst.State.PLAYING)

    def on_sync_message (self, bus, msg):
        if msg.get_structure().get_name() == 'prepare-window-handle':
            print('prepare-window-handle')
            print self.xid
            msg.src.set_window_handle(self.xid)

    def on_error(self, bus, msg):
        print msg.parse_error()


    def on_button_clicked(self, widget):
        dialog = DialogExample(self)
        response = dialog.run()

        dialog.destroy()

win = DialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()


