aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/commands.py
blob: a5b6ab1e02b4e4e9f0b01d00902393af7c37eae9 (plain)
  1. import logging
  2. import json
  3. import inspect
  4. from lib.config import Config
  5. from lib.videomix import CompositeModes
  6. from lib.response import NotifyResponse, OkResponse
  7. def decodeName(items, name_or_id):
  8. try:
  9. name_or_id = int(name_or_id)
  10. if name_or_id < 0 or name_or_id >= len(items):
  11. raise IndexError("unknown index %d" % name_or_id)
  12. return name_or_id
  13. except ValueError as e:
  14. try:
  15. return items.index(name_or_id)
  16. except ValueError as e:
  17. raise IndexError("unknown name %s" % name_or_id)
  18. def decodeEnumName(enum, name_or_id):
  19. try:
  20. name_or_id = int(name_or_id)
  21. if name_or_id < 0 or name_or_id >= len(enum):
  22. raise IndexError("unknown index %d" % name_or_id)
  23. return name_or_id
  24. except ValueError as e:
  25. try:
  26. return enum[name_or_id]
  27. except KeyError as e:
  28. raise IndexError("unknown name %s" % name_or_id)
  29. def encodeName(items, id):
  30. try:
  31. return items[id]
  32. except IndexError as e:
  33. raise IndexError("unknown index %d" % id)
  34. def encodeEnumName(enum, id):
  35. try:
  36. return enum(id).name
  37. except ValueError as e:
  38. raise IndexError("unknown index %d" % id)
  39. class ControlServerCommands(object):
  40. def __init__(self, pipeline):
  41. self.log = logging.getLogger('ControlServerCommands')
  42. self.pipeline = pipeline
  43. self.sources = Config.getlist('mix', 'sources')
  44. self.blankerSources = Config.getlist('stream-blanker', 'sources')
  45. # Commands are defined below. Errors are sent to the clients by throwing
  46. # exceptions, they will be turned into messages outside.
  47. def message(self, *args):
  48. """sends a message through the control-server, which can be received by
  49. user-defined scripts. does not change the state of the voctocore."""
  50. return NotifyResponse('message', *args)
  51. def help(self):
  52. helplines = []
  53. helplines.append("Commands:")
  54. for name, func in ControlServerCommands.__dict__.items():
  55. if name[0] == '_':
  56. continue
  57. if not func.__code__:
  58. continue
  59. params = inspect.signature(func).parameters
  60. params = [str(info) for name, info in params.items()]
  61. params = ', '.join(params[1:])
  62. command_sig = '\t' + name
  63. if params:
  64. command_sig += ': '+params
  65. if func.__doc__:
  66. command_sig += '\n'+'\n'.join(
  67. ['\t\t'+line.strip() for line in func.__doc__.splitlines()])+'\n'
  68. helplines.append(command_sig)
  69. helplines.append('\t'+'quit / exit')
  70. helplines.append("\n")
  71. helplines.append("Source-Names:")
  72. for source in self.sources:
  73. helplines.append("\t"+source)
  74. helplines.append("\n")
  75. helplines.append("Stream-Blanker Sources-Names:")
  76. for source in self.blankerSources:
  77. helplines.append("\t"+source)
  78. helplines.append("\n")
  79. helplines.append("Composition-Modes:")
  80. for mode in CompositeModes:
  81. helplines.append("\t"+mode.name)
  82. return OkResponse("\n".join(helplines))
  83. def _get_video_status(self):
  84. a = encodeName( self.sources, self.pipeline.vmix.getVideoSourceA() )
  85. b = encodeName( self.sources, self.pipeline.vmix.getVideoSourceB() )
  86. return [a, b]
  87. def get_video(self):
  88. """gets the current video-status, consisting of the name of
  89. video-source A and video-source B"""
  90. status = self._get_video_status()
  91. return OkResponse('video_status', *status)
  92. def set_video_a(self, src_name_or_id):
  93. """sets the video-source A to the supplied source-name or source-id,
  94. swapping A and B if the supplied source is currently used as
  95. video-source B"""
  96. src_id = decodeName(self.sources, src_name_or_id)
  97. self.pipeline.vmix.setVideoSourceA(src_id)
  98. status = self._get_video_status()
  99. return NotifyResponse('video_status', *status)
  100. def set_video_b(self, src_name_or_id):
  101. """sets the video-source B to the supplied source-name or source-id,
  102. swapping A and B if the supplied source is currently used as
  103. video-source A"""
  104. src_id = decodeName(self.sources, src_name_or_id)
  105. self.pipeline.vmix.setVideoSourceB(src_id)
  106. status = self._get_video_status()
  107. return NotifyResponse('video_status', *status)
  108. def _get_audio_status(self):
  109. src_id = self.pipeline.amix.getAudioSource()
  110. return encodeName(self.sources, src_id)
  111. def get_audio(self):
  112. """gets the name of the current audio-source"""
  113. status = self._get_audio_status()
  114. return OkResponse('audio_status', status)
  115. def set_audio(self, src_name_or_id):
  116. """sets the audio-source to the supplied source-name or source-id"""
  117. src_id = decodeName(self.sources, src_name_or_id)
  118. self.pipeline.amix.setAudioSource(src_id)
  119. status = self._get_audio_status()
  120. return NotifyResponse('audio_status', status)
  121. def _get_composite_status(self):
  122. mode = self.pipeline.vmix.getCompositeMode()
  123. return encodeEnumName(CompositeModes, mode)
  124. def get_composite_mode(self):
  125. """gets the name of the current composite-mode"""
  126. status = self._get_composite_status()
  127. return OkResponse('composite_mode', status)
  128. def set_composite_mode(self, mode_name_or_id):
  129. """sets the name of the id of the composite-mode"""
  130. mode = decodeEnumName(CompositeModes, mode_name_or_id)
  131. self.pipeline.vmix.setCompositeMode(mode)
  132. composite_status = self._get_composite_status()
  133. video_status = self._get_video_status()
  134. return [
  135. NotifyResponse('composite_mode', composite_status),
  136. NotifyResponse('video_status', *video_status)
  137. ]
  138. def set_videos_and_composite(self, src_a_name_or_id, src_b_name_or_id, mode_name_or_id):
  139. """sets the A- and the B-source synchronously with the composition-mode
  140. all parametets can be set to "*" which will leave them unchanged."""
  141. if src_a_name_or_id != '*':
  142. src_a_id = decodeName(self.sources, src_a_name_or_id)
  143. self.pipeline.vmix.setVideoSourceA(src_a_id)
  144. if src_b_name_or_id != '*':
  145. src_b_id = decodeName(self.sources, src_b_name_or_id)
  146. self.pipeline.vmix.setVideoSourceB(src_b_id)
  147. if mode_name_or_id != '*':
  148. mode = decodeEnumName(CompositeModes, mode_name_or_id)
  149. self.pipeline.vmix.setCompositeMode(mode)
  150. composite_status = self._get_composite_status()
  151. video_status = self._get_video_status()
  152. return [
  153. NotifyResponse('composite_mode', composite_status),
  154. NotifyResponse('video_status', *video_status)
  155. ]
  156. def _get_stream_status(self):
  157. blankSource = self.pipeline.streamblanker.blankSource
  158. if blankSource is None:
  159. return ('live',)
  160. return 'blank', encodeName(self.blankerSources, blankSource)
  161. def get_stream_status(self):
  162. """gets the current streamblanker-status"""
  163. status = self._get_stream_status()
  164. return OkResponse('stream_status', *status)
  165. def set_stream_blank(self, source_name_or_id):
  166. """sets the streamblanker-status to blank with the specified
  167. blanker-source-name or -id"""
  168. src_id = decodeName(self.blankerSources, source_name_or_id)
  169. self.pipeline.streamblanker.setBlankSource(src_id)
  170. status = self._get_stream_status()
  171. return NotifyResponse('stream_status', *status)
  172. def set_stream_live(self):
  173. """sets the streamblanker-status to live"""
  174. self.pipeline.streamblanker.setBlankSource(None)
  175. status = self._get_stream_status()
  176. return NotifyResponse('stream_status', *status)
  177. def get_config(self):
  178. """returns the parsed server-config"""
  179. confdict = {header: dict(section) for header, section in dict(Config).items()}
  180. return OkResponse('server_config', json.dumps(confdict))