summaryrefslogtreecommitdiff
path: root/voctocore/lib/commands.py
blob: 664a6caa07b40aaac7cb58807afddfd6e423ac13 (plain)
  1. #!/usr/bin/python3
  2. import logging
  3. import json
  4. from lib.config import Config
  5. from lib.videomix import CompositeModes
  6. class ControlServerCommands():
  7. def __init__(self, pipeline):
  8. self.log = logging.getLogger('ControlServerCommands')
  9. self.pipeline = pipeline
  10. self.sources = Config.getlist('mix', 'sources')
  11. self.blankersources = Config.getlist('stream-blanker', 'sources')
  12. def decodeSourceName(self, src_name_or_id):
  13. if isinstance(src_name_or_id, str):
  14. try:
  15. return self.sources.index(src_name_or_id)
  16. except Exception as e:
  17. raise IndexError("source %s unknown" % src_name_or_id)
  18. if src_name_or_id < 0 or src_name_or_id >= len(self.sources):
  19. raise IndexError("source %s unknown" % src_name_or_id)
  20. def encodeSourceName(self, src_id):
  21. try:
  22. return self.sources[src_id]
  23. except Exception as e:
  24. raise IndexError("source %s unknown" % src_id)
  25. def decodeBlankerSourceName(self, src_name_or_id):
  26. if isinstance(src_name_or_id, str):
  27. try:
  28. return self.blankersources.index(src_name_or_id)
  29. except Exception as e:
  30. raise IndexError("source %s unknown" % src_name_or_id)
  31. if src_name_or_id < 0 or src_name_or_id >= len(self.blankersources):
  32. raise IndexError("source %s unknown" % src_name_or_id)
  33. def encodeBlankerSourceName(self, src_id):
  34. try:
  35. return self.blankersources[src_id]
  36. except Exception as e:
  37. raise IndexError("source %s unknown" % src_id)
  38. def message(self, *args):
  39. return True
  40. def set_video_a(self, src_name_or_id):
  41. src_id = self.decodeSourceName(src_name_or_id)
  42. self.pipeline.vmix.setVideoSourceA(src_id)
  43. return True
  44. def get_video_a(self):
  45. src_id = self.pipeline.vmix.getVideoSourceA()
  46. return (True, self.encodeSourceName(src_id))
  47. def set_video_b(self, src_name_or_id):
  48. src_id = self.decodeSourceName(src_name_or_id)
  49. self.pipeline.vmix.setVideoSourceB(src_id)
  50. return True
  51. def get_video_b(self):
  52. src_id = self.pipeline.vmix.getVideoSourceB()
  53. return (True, self.encodeSourceName(src_id))
  54. def set_audio(self, src_name_or_id):
  55. src_id = self.decodeSourceName(src_name_or_id)
  56. self.pipeline.amix.setAudioSource(src_id)
  57. return True
  58. def get_audio(self):
  59. src_id = self.pipeline.amix.getAudioSource()
  60. return (True, self.encodeSourceName(src_id))
  61. def set_composite_mode(self, composite_mode):
  62. try:
  63. mode = CompositeModes[composite_mode]
  64. except KeyError as e:
  65. raise KeyError("composite-mode %s unknown" % composite_mode)
  66. self.pipeline.vmix.setCompositeMode(mode)
  67. return True
  68. def get_composite_mode(self):
  69. try:
  70. mode = self.pipeline.vmix.getCompositeMode()
  71. return (True, mode.name)
  72. except Exception as e:
  73. raise KeyError("composite-mode %s unknown" % mode)
  74. def set_stream_status(self, *args):
  75. try:
  76. if args[0] == "live":
  77. self.pipeline.streamblanker.setBlankSource(None)
  78. return True
  79. elif args [0] == "blank":
  80. src_id = self.decodeBlankerSourceName(args[1])
  81. self.pipeline.streamblanker.setBlankSource(src_id)
  82. return True
  83. else:
  84. return (False, "invocation: set_stream_status (live | blank <mode>)")
  85. except IndexError as e:
  86. return (False, "invocation: set_stream_status (live | blank <mode>)")
  87. def get_stream_status(self):
  88. if self.pipeline.streamblanker.blankSource is None:
  89. return (True, "live")
  90. name = self.encodeBlankerSourceName(self.pipeline.streamblanker.blankSource)
  91. return (True, "blank " + name)
  92. def get_config(self):
  93. confdict = {k: dict(v) for k, v in dict(Config).items()}
  94. return (True, json.dumps(confdict))