summaryrefslogtreecommitdiff
path: root/voctogui/voctogui.py
blob: 085389897c2727bae36ca215cd5a701a438aa536 (plain)
  1. #!/usr/bin/env python3
  2. import gi, signal, logging, sys, os
  3. # import GStreamer and GLib-Helper classes
  4. gi.require_version('Gtk', '3.0')
  5. gi.require_version('Gst', '1.0')
  6. gi.require_version('GdkX11', '3.0')
  7. gi.require_version('GstVideo', '1.0')
  8. from gi.repository import Gtk, Gdk, Gst, GObject, GdkX11, GstVideo
  9. # check min-version
  10. minGst = (1, 5)
  11. minPy = (3, 0)
  12. Gst.init([])
  13. if Gst.version() < minGst:
  14. raise Exception("GStreamer version", Gst.version(), 'is too old, at least', minGst, 'is required')
  15. if sys.version_info < minPy:
  16. raise Exception("Python version", sys.version_info, 'is too old, at least', minPy, 'is required')
  17. # init GObject & Co. before importing local classes
  18. GObject.threads_init()
  19. Gdk.init([])
  20. Gtk.init([])
  21. # import local classes
  22. from lib.args import Args
  23. from lib.config import Config
  24. from lib.ui import Ui
  25. from lib.loghandler import LogHandler
  26. import lib.connection as Connection
  27. # main class
  28. class Voctogui(object):
  29. def __init__(self):
  30. self.log = logging.getLogger('Voctogui')
  31. # Uf a UI-File was specified on the Command-Line, load it
  32. if Args.ui_file:
  33. self.log.info('loading ui-file from file specified on command-line: %s', self.options.ui_file)
  34. self.ui = Ui(Args.ui_file)
  35. else:
  36. # Paths to look for the gst-switch UI-File
  37. paths = [
  38. os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ui/voctogui.ui'),
  39. '/usr/lib/voctogui/ui/voctogui.ui'
  40. ]
  41. # Look for a gst-switch UI-File and load it
  42. for path in paths:
  43. self.log.debug('trying to load ui-file from file %s', path)
  44. if os.path.isfile(path):
  45. self.log.info('loading ui-file from file %s', path)
  46. self.ui = Ui(path)
  47. break
  48. if self.ui is None:
  49. raise Exception("Can't find any .ui-Files to use (searched %s)" % (', '.join(paths)))
  50. self.ui.setup()
  51. def run(self):
  52. self.log.info('setting UI visible')
  53. self.ui.show()
  54. try:
  55. self.log.info('running Gtk-MainLoop')
  56. Gtk.main()
  57. self.log.info('Gtk-MainLoop ended')
  58. except KeyboardInterrupt:
  59. self.log.info('Terminated via Ctrl-C')
  60. def quit(self):
  61. self.log.info('quitting Gtk-MainLoop')
  62. Gtk.main_quit()
  63. # run mainclass
  64. def main():
  65. # configure logging
  66. docolor = (Args.color == 'always') or (Args.color == 'auto' and sys.stderr.isatty())
  67. handler = LogHandler(docolor, Args.timestamp)
  68. logging.root.addHandler(handler)
  69. if Args.verbose >= 2:
  70. level = logging.DEBUG
  71. elif Args.verbose == 1:
  72. level = logging.INFO
  73. else:
  74. level = logging.WARNING
  75. logging.root.setLevel(level)
  76. # make killable by ctrl-c
  77. logging.debug('setting SIGINT handler')
  78. signal.signal(signal.SIGINT, signal.SIG_DFL)
  79. logging.info('Python Version: %s', sys.version_info)
  80. logging.info('GStreamer Version: %s', Gst.version())
  81. # establish a synchronus connection to server
  82. Connection.establish(
  83. Config.get('server', 'host'))
  84. # fetch config from server
  85. Config.fetchServerConfig()
  86. # Warn when connecting to a non-local core without preview-encoders enabled
  87. # the list-comparison is not complete (one could use a local hostname or the local system ip)
  88. # but it's only here to warn that one might be making a mistake
  89. use_previews = Config.getboolean('previews', 'enabled') and Config.getboolean('previews', 'use')
  90. looks_like_localhost = Config.get('server', 'host') in ['::1', '127.0.0.1', 'localhost']
  91. if not use_previews and not looks_like_localhost:
  92. logging.warn(
  93. 'Connecting to `%s` (which looks like a remote host) might not work without enabeling '
  94. 'the preview encoders (set `[previews] enabled=true` on the core) or it might saturate '
  95. 'your ethernet link between the two machines.',
  96. Config.get('server', 'host')
  97. )
  98. # switch connection to nonblocking, event-driven mode
  99. Connection.enterNonblockingMode()
  100. # init main-class and main-loop
  101. # (this binds all event-hander on the Connection)
  102. logging.debug('initializing Voctogui')
  103. voctogui = Voctogui()
  104. # start the Mainloop and show the Window
  105. logging.debug('running Voctogui')
  106. voctogui.run()
  107. if __name__ == '__main__':
  108. main()