# Path: vocto/port.py
# class Port(object):
# 
#     NONE = 0
# 
#     IN = 1
#     OUT = 2
# 
#     OFFSET_PREVIEW = 100
#     # core listening port
#     CORE_LISTENING = 9999
#     # input ports
#     SOURCES_IN = 10000
#     SOURCES_BACKGROUND = 16000
#     SOURCE_OVERLAY= 14000
#     SOURCES_BLANK = 17000
#     AUDIO_SOURCE_BLANK = 18000
#     # output ports
#     MIX_OUT = 11000
#     MIX_PREVIEW = MIX_OUT+OFFSET_PREVIEW
#     SOURCES_OUT = 13000
#     SOURCES_PREVIEW = SOURCES_OUT+OFFSET_PREVIEW
#     LIVE_OUT = 15000
#     LIVE_PREVIEW = LIVE_OUT+OFFSET_PREVIEW
# 
#     def __init__(self, name, source=None, audio=None, video=None):
#         self.name = name
#         self.source = source
#         self.audio = audio
#         self.video = video
#         self.update()
# 
#     def todict(self):
#         return {
#             'name': self.name,
#             'port': self.port,
#             'audio': self.audio,
#             'video': self.video,
#             'io': self.io,
#             'connections': self.connections
#         }
# 
#     def update(self):
#         if self.source:
#             self.port = self.source.port()
#             self.audio = self.source.audio_channels()
#             self.video = self.source.video_channels()
#             self.io = self.IN if self.source.is_input() else self.OUT
#             self.connections = self.source.num_connections()
# 
#     def from_str(_str):
#         p = Port(_str['name'])
#         p.port = _str['port']
#         p.audio = _str['audio']
#         p.video = _str['video']
#         p.io = _str['io']
#         p.connections = _str['connections']
#         return p
# 
#     def is_input(self):
#         return self.io == Port.IN
# 
#     def is_output(self):
#         return self.io == Port.OUT
# 
# Path: vocto/debug.py
# def gst_generate_dot(pipeline, name):
#     from lib.args import Args
#     dotfile = os.path.join(os.environ['GST_DEBUG_DUMP_DOT_DIR'], "%s.dot" % name)
#     log.debug("Generating DOT image of pipeline '{name}' into '{file}'".format(name=name, file=dotfile))
#     Gst.debug_bin_to_dot_file(pipeline, Gst.DebugGraphDetails(Args.gst_debug_details), name)
# 
# Path: vocto/pretty.py
# def pretty(pipe):
#     result = ""
#     for line in pipe.splitlines():
#         line = line.strip()
#         r = re.match(r'^(!\s.*)$', line)
#         if r:
#             result += "    " + r.group(1)
#         else:
#             r = re.match(r'^([\w\-_]*\s*\=\s*.*)$', line)
#             if r:
#                 result += "        " + r.group(1)
#             else:
#                 r = re.match(r'^(\))$', line)
#                 if r:
#                     result += r.group(1)
#                 else:
#                     r = re.match(r'^bin.\($', line)
#                     if r:
#                         result += line
#                     else:
#                         result += "    " + line
#         result += "\n"
#     return result


import logging
import sys
from gi.repository import Gst, Gdk
from lib.args import Args
from lib.config import Config
from lib.clock import Clock
from vocto.port import Port
from vocto.debug import gst_generate_dot
from vocto.pretty import pretty
            ! level
                name=lvl
                interval=50000000
            ! audioconvert
            """

        # If Playback is requested, push fo pulseaudio
        if play_audio:
            pipe += """ ! pulsesink
                            name=audiosink-{name}
                        """
        else:
            pipe += """ ! fakesink
                        """
        pipe = pipe.format(name=name,
                           acaps=Config.getAudioCaps(),
                           port=port,
                           )

        self.log.info("Creating Display-Pipeline:\n%s",  pretty(pipe))
        try:
            # launch gstreamer pipeline
            self.pipeline = Gst.parse_launch(pipe)
            self.log.info("pipeline launched successfuly")
        except:
            self.log.error("Can not launch pipeline")
            sys.exit(-1)

        if Args.dot:
            self.log.debug("Generating DOT image of videodisplay pipeline")

# Path: vocto/port.py
# class Port(object):
# 
#     NONE = 0
# 
#     IN = 1
#     OUT = 2
# 
#     OFFSET_PREVIEW = 100
#     # core listening port
#     CORE_LISTENING = 9999
#     # input ports
#     SOURCES_IN = 10000
#     SOURCES_BACKGROUND = 16000
#     SOURCE_OVERLAY= 14000
#     SOURCES_BLANK = 17000
#     AUDIO_SOURCE_BLANK = 18000
#     # output ports
#     MIX_OUT = 11000
#     MIX_PREVIEW = MIX_OUT+OFFSET_PREVIEW
#     SOURCES_OUT = 13000
#     SOURCES_PREVIEW = SOURCES_OUT+OFFSET_PREVIEW
#     LIVE_OUT = 15000
#     LIVE_PREVIEW = LIVE_OUT+OFFSET_PREVIEW
# 
#     def __init__(self, name, source=None, audio=None, video=None):
#         self.name = name
#         self.source = source
#         self.audio = audio
#         self.video = video
#         self.update()
# 
#     def todict(self):
#         return {
#             'name': self.name,
#             'port': self.port,
#             'audio': self.audio,
#             'video': self.video,
#             'io': self.io,
#             'connections': self.connections
#         }
# 
#     def update(self):
#         if self.source:
#             self.port = self.source.port()
#             self.audio = self.source.audio_channels()
#             self.video = self.source.video_channels()
#             self.io = self.IN if self.source.is_input() else self.OUT
#             self.connections = self.source.num_connections()
# 
#     def from_str(_str):
#         p = Port(_str['name'])
#         p.port = _str['port']
#         p.audio = _str['audio']
#         p.video = _str['video']
#         p.io = _str['io']
#         p.connections = _str['connections']
#         return p
# 
#     def is_input(self):
#         return self.io == Port.IN
# 
#     def is_output(self):
#         return self.io == Port.OUT
# 
# Path: vocto/debug.py
# def gst_generate_dot(pipeline, name):
#     from lib.args import Args
#     dotfile = os.path.join(os.environ['GST_DEBUG_DUMP_DOT_DIR'], "%s.dot" % name)
#     log.debug("Generating DOT image of pipeline '{name}' into '{file}'".format(name=name, file=dotfile))
#     Gst.debug_bin_to_dot_file(pipeline, Gst.DebugGraphDetails(Args.gst_debug_details), name)
# 
# Path: vocto/pretty.py
# def pretty(pipe):
#     result = ""
#     for line in pipe.splitlines():
#         line = line.strip()
#         r = re.match(r'^(!\s.*)$', line)
#         if r:
#             result += "    " + r.group(1)
#         else:
#             r = re.match(r'^([\w\-_]*\s*\=\s*.*)$', line)
#             if r:
#                 result += "        " + r.group(1)
#             else:
#                 r = re.match(r'^(\))$', line)
#                 if r:
#                     result += r.group(1)
#                 else:
#                     r = re.match(r'^bin.\($', line)
#                     if r:
#                         result += line
#                     else:
#                         result += "    " + line
#         result += "\n"
#     return result


# Path: voctogui/lib/videodisplay.py
import logging
import sys
from gi.repository import Gst, Gdk
from lib.args import Args
from lib.config import Config
from lib.clock import Clock
from vocto.port import Port
from vocto.debug import gst_generate_dot
from vocto.pretty import pretty

            ! level
                name=lvl
                interval=50000000
            ! audioconvert
            """

        # If Playback is requested, push fo pulseaudio
        if play_audio:
            pipe += """ ! pulsesink
                            name=audiosink-{name}
                        """
        else:
            pipe += """ ! fakesink
                        """
        pipe = pipe.format(name=name,
                           acaps=Config.getAudioCaps(),
                           port=port,
                           )

        self.log.info("Creating Display-Pipeline:\n%s",  pretty(pipe))
        try:
            # launch gstreamer pipeline
            self.pipeline = Gst.parse_launch(pipe)
            self.log.info("pipeline launched successfuly")
        except:
            self.log.error("Can not launch pipeline")
            sys.exit(-1)

        if Args.dot:
            self.log.debug("Generating DOT image of videodisplay pipeline")

            gst_generate_dot(self.pipeline, "gui.videodisplay.{}".format(name))