obs.py 8.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright (C) 2023 Stefy Lanza <stefy@nexlab.net> and SexHack.me
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import obsws_python as obs
import time
18 19 20
from contextlib import suppress
import websocket
import queue
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
import logging
obws_logger = logging.getLogger("obsws_python.baseclient")
obws_logger.setLevel(logging.CRITICAL)

logger = logging.getLogger(__name__)


class OBSOutput:

   def __init__(self, output, config_section, obss):
      self.config_section = config_section
      self.output = output
      self.status = False
      self.obss = obss
      self.config_options = config.options(config_section)
      self.config_section = config_section
      self.statuses = {}
      self.inputs = {}
      for c in self.config_options:
         if 'status.' in c:
            for inp in config.get(config_section, c).split(','):
               if inp not in self.inputs.keys():
                  self.inputs[inp] = False
            if c.split('.')[1] not in self.statuses.keys():
               enabled=[]
               disanled=[]
               if config.get(config_section, "status."+c.split(".")[1]+".disable", fallback=False):
                  disabled=config.get(config_section, "status."+c.split(".")[1]+".disable", fallback=False).split(',')
               if config.get(config_section, "status."+c.split(".")[1]+".enable", fallback=False):
                  enabled=config.get(config_section, "status."+c.split(".")[1]+".enable", fallback="").split(',')
               self.statuses[c.split('.')[1]] = {'disable': disabled,
                                                 'enable': enabled,
                                                 }


         setattr(self, c, config.get(config_section, c))
      #print(getattr(self, 'source.closed'))
      #print(dir(self))

   def disable(self, sources):
      pass

   def enable(self, sources):
      pass

   def setStatus(self, status):
      if hasattr(self, 'status.'+status+'.disable'):
         self.disable(getattr(self, 'status.'+status+'.disable').split(','))

      if hasattr(self, 'status.'+status+'.enable'):
         self.enable(getattr(self, 'status.'+status+'.enable').split(','))

   def updateStatus(self):
      if not self.obss.online:
         self.status = False
         logging.info('FOUND False')
         return False
      for inp in self.inputs.keys():
         self.inputs[inp] = self.obss.getInputStatus(self.scene, inp)
      found=False
      for status  in self.statuses.keys():
         if not found:
            found = status
            for enabled in self.statuses[status]['enable']:
               if not self.inputs[enabled]:
                  found = False
            if found:
               for disabled in self.statuses[status]['disable']:
                  if self.inputs[disabled]:
                     found = False
            if found:
               self.status = found
               logging.info("FOUND "+found)
               return found
      logging.info('FOUND False')
      self.status = found
      return found

   def getStatus(self):
      if not self.status:
         self.status = self.updateStatus()
      return self.status
nextime's avatar
nextime committed
104 105 106 107 108 109 110 111

class OBSControl:

   def __init__(self, obs_server, config_section):
      self.online = False
      self.last_try = time.time()-11
      self.lastping = time.time()-20
      self.server = obs_server
112
      self.queue = queue.Queue()
nextime's avatar
nextime committed
113 114
      self.config_section = config_section
      self.config_options = config.options(config_section)
115 116
      self.cl = False
      self.cr = False
nextime's avatar
nextime committed
117 118 119 120 121 122 123 124 125 126 127 128
      for c in self.config_options:
         setattr(self, c, config.get(config_section, c))

   def start(self):
      if self.online:
         return
      self.last_try = time.time()
      try:
         self.cl = obs.EventClient(host=self.host, port=self.port, password=self.password)
         self.cr = obs.ReqClient(host=self.host, port=self.port, password=self.password)
         self.cl.callback.register(self.on_scene_item_enable_state_changed)
         self.setonline()
129
      except:
nextime's avatar
nextime committed
130 131
         pass

132 133 134 135 136
   def getInputStatus(self, scene, inp):
      if self.cr:
         return self.cr.get_scene_item_enabled(scene, int(inp)).scene_item_enabled
      return False

nextime's avatar
nextime committed
137
   def setonline(self):
138
      logging.info('OBS '+self.server+' ONLINE')
nextime's avatar
nextime committed
139
      self.online = True
140
      self.queue.put({ 'event': 'SETONLINE', 'data': {'server': self.server}})
nextime's avatar
nextime committed
141 142 143

   def setoffline(self):
      self.online = False
144
      self.queue.put({'event': 'SETOFFLINE', 'data': {'server': self.server}})
nextime's avatar
nextime committed
145 146 147

   def run_tasks(self):
      if not self.online and time.time()-self.last_try > 10:
148
         logging.info("OBS "+self.server+" starting... ")
nextime's avatar
nextime committed
149 150 151 152 153 154 155
         self.start()

      if self.online:
         if time.time()-self.lastping > 20:
            try:
               self.cr.broadcast_custom_event({'eventData': {'eventType': 'Ping', 'time': time.time()}})
               self.lastping = time.time()
156
               logging.info("SENT Ping to "+self.server)
nextime's avatar
nextime committed
157 158 159 160 161
            except:
               self.setoffline()


   def on_scene_item_enable_state_changed(self, data):
162 163 164 165
      logging.info("scscene_item_enable_state_changed")
      logging.info(data.attrs())
      logging.info([data.scene_item_enabled, data.scene_item_id, data.scene_name, data.scene_uuid])
      self.queue.put({'event': 'INPUTCHANGE', 'data': {'server': self.server, 'status': data.scene_item_enabled, 'scene':  data.scene_name, 'source': data.scene_item_id}})
166 167 168


def run_obs_controller():
nextime's avatar
nextime committed
169 170
    obs_servers = {}
    for k in [x for x in config.sections() if 'OBS:' in x]:
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        #if not config.get(k, 'active', fallback=False):
        obs_servers[k.split(":",1)[1]] = OBSControl(k.split(":",1)[1], k)


    obs_outputs = {}
    for k in [x for x in config.sections() if 'OUTPUT:' in x]:
        obss = False
        if config.get(k, 'obs') in obs_servers.keys():
           obss = obs_servers[config.get(k, 'obs')]
        obs_outputs[k.split(":",1)[1]] = OBSOutput(k.split(":",1)[1], k, obss )
	    


        logging.info('OUTPUT '+config.get(k, 'obs')+" -> "+k.split(":",1)[1])
        logging.info(obs_outputs[k.split(":",1)[1]].getStatus())
nextime's avatar
nextime committed
186 187 188

    #cl = obs.EventClient(host='192.168.42.115', port=4455, password='motorol4')
    #cr = obs.ReqClient(host='192.168.42.115', port=4455, password='motorol4')
189

nextime's avatar
nextime committed
190
    #cl.callback.register(on_scene_item_enable_state_changed)
191
    
nextime's avatar
nextime committed
192
    #print(cl.callback.get())
193 194 195 196 197

    #cl.callback.deregister(on_input_mute_state_changed)

    now=time.time()-30
    while True:
198 199 200 201 202
        if not qobs.empty():
            task=qobs.get(block=True)
            if task:
                logging.info('TASK INCOMING FOR OBS')
                logging.info(task)
203 204
        if (time.time() - now) > 30:
            now = time.time()
nextime's avatar
nextime committed
205 206 207
            #cr.broadcast_custom_event({'eventData': {'eventType': 'Ping', 'time': time.time()}})
        for o in obs_servers.keys():
            obs_servers[o].run_tasks()
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
            if not obs_servers[o].queue.empty():
               task=obs_servers[o].queue.get(block=True)
               if task:
                  logging.info('EVENT COMING FROM OBS '+o)
                  logging.info(task)
                  event = task['event']
                  data = task['data']
                  if event == 'SETOFFLINE' or event == 'SETONLINE':
                     for output in obs_outputs.values():
                        if output.obss.server == data['server']:
                           output.updateStatus()
                  if event == 'INPUTCHANGE':
                     #'data': {'server': 'leeloo', 'status': False, 'scene': 'LIVE SFW', 'source': 5}}
                     for output in obs_outputs.values():
                        if output.obss.server == data['server'] and output.scene==data['scene'] and str(data['source']) in output.inputs.keys():
                           output.updateStatus()


226 227
        time.sleep(.01)