예제 #1
0
파일: node.py 프로젝트: leodenale/OpenOB
 def run_link(self, link_config, audio_interface):
     """
       Run a new TX or RX node.
     """
     # We're now entering the realm where we should desperately try and
     # maintain a link under all circumstances forever.
     self.logger.info("Link %s initial setup start on %s" %
                      (link_config.name, self.node_name))
     link_logger = self.logger_factory.getLogger(
         'node.%s.link.%s' % (self.node_name, link_config.name))
     while True:
         try:
             if audio_interface.mode == 'tx':
                 try:
                     link_logger.info("Starting up transmitter")
                     transmitter = RTPTransmitter(self.node_name,
                                                  link_config,
                                                  audio_interface)
                     transmitter.run()
                     caps = transmitter.get_caps()
                     link_logger.debug(
                         "Got caps from transmitter, setting config")
                     link_config.set("caps", caps)
                     transmitter.loop()
                 except Exception as e:
                     link_logger.exception(
                         "Transmitter crashed for some reason! Restarting..."
                     )
                     time.sleep(0.5)
             elif audio_interface.mode == 'rx':
                 link_logger.info("Waiting for transmitter capabilities...")
                 caps = link_config.blocking_get("caps")
                 link_logger.info("Got caps from transmitter")
                 try:
                     link_logger.info("Starting up receiver")
                     receiver = RTPReceiver(self.node_name, link_config,
                                            audio_interface)
                     receiver.run()
                     receiver.loop()
                 except Exception as e:
                     link_logger.exception(
                         "Receiver crashed for some reason! Restarting...")
                     time.sleep(0.1)
             else:
                 link_logger.critical("Unknown audio interface mode (%s)!" %
                                      audio_interface.mode)
                 sys.exit(1)
         except Exception as e:
             link_logger.exception(
                 "Unknown exception thrown - please report this as a bug! %s"
                 % e)
             raise
예제 #2
0
파일: node.py 프로젝트: deedos/openob
class Node(object):
    """
        OpenOB node instance.

        Nodes run links. Each Node looks after its end of a link, ensuring
        that it remains running and tries to recover from failures, as well as
        responding to configuration changes.

        Nodes have a name; everything else is link specific.

        For instance, a node might be the 'studio' node, which would run a
        'tx' end for the 'stl' link.

        Nodes have a config host which is where they store their inter-Node
        data and communicate with other Nodes.
    """
    def __init__(self, node_name):
        """Set up a new node."""
        self.node_name = node_name
        self.logger_factory = LoggerFactory()
        self.logger = self.logger_factory.getLogger('node.%s' % self.node_name)

    def run_link(self, link_config, audio_interface):
        """
          Run a new TX or RX node.
        """
        # We're now entering the realm where we should desperately try and
        # maintain a link under all circumstances forever.
        self.logger.info("Link %s initial setup start on %s" %
                         (link_config.name, self.node_name))
        link_logger = self.logger_factory.getLogger(
            'node.%s.link.%s.%s' %
            (self.node_name, link_config.name, audio_interface.mode))
        while True:
            try:
                if audio_interface.mode == 'tx':
                    try:
                        transmitter = RTPTransmitter(self.node_name,
                                                     link_config,
                                                     audio_interface)
                        link_logger.info("Starting up transmitter")
                        transmitter.run()
                        caps = transmitter.get_caps()
                        link_logger.info("Got caps, setting config - %s" %
                                         caps)
                        link_config.set("caps", caps)
                        transmitter.loop()
                    except ElementNotFoundError, e:
                        link_logger.critical(
                            "GStreamer element missing: %s - will now exit" %
                            e)
                        sys.exit(1)
                    except Exception, e:
                        link_logger.exception(
                            "Transmitter crashed for some reason! Restarting..."
                        )
                        time.sleep(0.5)
                elif audio_interface.mode == 'rx':
                    link_logger.info("Waiting for transmitter capabilities...")
                    caps = link_config.blocking_get("caps")
                    link_logger.info("Got caps from transmitters - %s" % caps)
                    try:
                        link_logger.info("Starting up receiver")
                        receiver = RTPReceiver(self.node_name, link_config,
                                               audio_interface)
                        receiver.run()
                        receiver.loop()
                    except ElementNotFoundError, e:
                        link_logger.critical(
                            "GStreamer element missing: %s - will now exit" %
                            e)
                        sys.exit(1)
예제 #3
0
파일: manager.py 프로젝트: sreimers/openob
    def run(self, opts):
        print("-- OpenOB Audio Link")
        print(" -- Starting Up")
        print(" -- Parameters: %s" % opts)
        # We're now entering the realm where we should desperately try and maintain a link under all circumstances forever.
        while True:
            try:
                # Set up redis and connect
                config = None
                while True:
                    try:
                        config = redis.Redis(opts.config_host)
                        print(" -- Connected to configuration server")
                        break
                    except Exception, e:
                        print(
                            Fore.BLACK + Back.RED +
                            " -- Couldn't connect to Redis! Ensure your configuration host is set properly, and you can connect to the default Redis port on that host from here (%s)."
                            % e)
                        print(
                            "    Waiting half a second and attempting to connect again."
                            + Fore.RESET + Back.RESET)
                        time.sleep(0.5)

                # So if we're a transmitter, let's set the options the receiver needs to know about
                link_key = "openob2:" + opts.link_name + ":"
                if opts.mode == 'tx':
                    if opts.encoding == 'celt' and int(opts.bitrate) > 192:
                        print(
                            Fore.BLACK + Back.YELLOW +
                            " -- WARNING: Can't use bitrates higher than 192kbps for CELT, limiting"
                            + Fore.RESET + Back.RESET)
                        opts.bitrate = 192
                    # We're a transmitter!
                    config.set(link_key + "port", opts.port)
                    config.set(link_key + "ipv6", opts.ipv6)
                    config.set(link_key + "jitter_buffer", opts.jitter_buffer)
                    config.set(link_key + "encoding", opts.encoding)
                    config.set(link_key + "bitrate", opts.bitrate)
                    print(" -- Configured receiver with:")
                    print("   - Base Port:     %s" %
                          config.get(link_key + "port"))
                    print("   - Jitter Buffer: %s ms" %
                          config.get(link_key + "jitter_buffer"))
                    print("   - Encoding:      %s" %
                          config.get(link_key + "encoding"))
                    print("   - Bitrate:       %s kbit/s" %
                          config.get(link_key + "bitrate"))
                    # Okay, we can't set caps yet - we need to configure ourselves first.
                    opus_opts = {
                        'audio': True,
                        'bandwidth': -1000,
                        'frame-size': opts.framesize,
                        'complexity': opts.complexity,
                        'constrained-vbr': True,
                        'inband-fec': opts.fec,
                        'packet-loss-percentage': opts.loss,
                        'dtx': opts.dtx
                    }
                    try:
                        transmitter = RTPTransmitter(
                            audio_input=opts.audio_input,
                            audio_device=opts.device,
                            base_port=opts.port,
                            ipv6=opts.ipv6,
                            encoding=opts.encoding,
                            bitrate=opts.bitrate,
                            jack_name=("openob_tx_%s" % opts.link_name),
                            receiver_address=opts.receiver_host,
                            opus_options=opus_opts)
                        # Set it up, get caps
                        try:
                            transmitter.run()
                            config.set(link_key + "caps",
                                       transmitter.get_caps())
                            print("   - Caps:          %s" %
                                  config.get(link_key + "caps"))
                            transmitter.loop()
                        except Exception, e:
                            print(
                                Fore.BLACK + Back.RED +
                                " -- Lost connection or otherwise had the transmitter fail on us, restarting (%s)"
                                % e)
                            time.sleep(0.5)
                    except gst.ElementNotFoundError, e:
                        print(Fore.BLACK + Back.RED + (
                            " -- Couldn't fulfill our gstreamer module dependencies! You don't have the following element available: %s"
                            % e) + Fore.RESET + Back.RESET)
                        sys.exit(1)
                else:
                    # We're a receiver!
                    # Default values.
                    port = 3000
                    caps = ''
                    jitter_buffer = 150
                    encoding = 'opus'
                    bitrate = '96'
                    while True:
                        try:
                            if config.get(link_key + "port") == None:
                                print(
                                    Fore.BLACK + Back.YELLOW +
                                    " -- Unable to configure myself from the configuration host; has the transmitter been started yet, and have you got the same link name on each end?"
                                )
                                print(
                                    "    Waiting half a second and attempting to reconfigure myself."
                                    + Fore.RESET + Back.RESET)
                                time.sleep(0.5)
                            port = int(config.get(link_key + "port"))
                            ipv6 = int(config.get(link_key + "ipv6"))
                            caps = config.get(link_key + "caps")
                            jitter_buffer = int(
                                config.get(link_key + "jitter_buffer"))
                            encoding = config.get(link_key + "encoding")
                            bitrate = int(config.get(link_key + "bitrate"))
                            print(" -- Configured from transmitter with:")
                            print("   - Base Port:     %s" % port)
                            print("   - Jitter Buffer: %s ms" % caps)
                            print("   - Encoding:      %s" % encoding)
                            print("   - Bitrate:       %s kbit/s" % bitrate)
                            print("   - Caps:          %s" % caps)
                            break
                        except Exception, e:
                            print(
                                Fore.BLACK + Back.YELLOW +
                                " -- Unable to configure myself from the configuration host; has the transmitter been started yet? (%s)"
                                % e)
                            print(
                                "    Waiting half a second and attempting to reconfigure myself."
                                + Fore.RESET + Back.RESET)
                            time.sleep(0.5)
                            #raise
                    # Okay, we can now configure ourself
                    receiver = RTPReceiver(audio_output=opts.audio_output,
                                           audio_device=opts.device,
                                           base_port=port,
                                           ipv6=ipv6,
                                           encoding=encoding,
                                           caps=caps,
                                           bitrate=bitrate,
                                           jitter_buffer=jitter_buffer,
                                           jack_name=("openob_tx_%s" %
                                                      opts.link_name))
                    try:
                        receiver.run()
                        receiver.loop()
                    except Exception, e:
                        print(Fore.BLACK + Back.RED + (
                            " -- Lost connection or otherwise had the receiver fail on us, restarting (%s)"
                            % e) + Fore.RESET + Back.RESET)
                        time.sleep(0.5)