rustduck-sys 1.1.0

Raw Rust FFI bindings to the C ABI used by TSDuck's Python API
Documentation
#include "rustduck-sys/native/bridge/input_switcher.h"

#include "rustduck-sys/native/bridge/test_hooks.h"
#include "rustduck-sys/native/tspy.h"
#include "rustduck-sys/src/bridge.rs.h"

#include <cstdint>
#include <limits>
#include <memory>
#include <stdexcept>
#include <utility>

namespace {

long checked_long(std::int64_t value) {
  if (value < std::numeric_limits<long>::min() ||
      value > std::numeric_limits<long>::max()) {
    throw std::invalid_argument(
        "input switcher numeric option is outside native long range");
  }
  return static_cast<long>(value);
}

const std::uint8_t *buffer_data(const rust::Vec<std::uint8_t> &buffer) {
  return buffer.empty() ? nullptr : buffer.data();
}

void validate_utf16(const rust::Vec<std::uint8_t> &buffer) {
  if (buffer.size() % sizeof(std::uint16_t) != 0) {
    throw std::invalid_argument("input switcher UTF-16 buffer is not aligned");
  }
}

} // namespace

RustDuckInputSwitcher::RustDuckInputSwitcher(
    std::shared_ptr<void> switcher) noexcept
    : switcher_(std::move(switcher)) {}

RustDuckInputSwitcher::~RustDuckInputSwitcher() noexcept = default;

void *RustDuckInputSwitcher::raw() const noexcept { return switcher_.get(); }

void RustDuckInputSwitcher::retain_handler(
    const RustDuckMemoryHandler &handler) {
  handlers_.push_back(handler.retain());
}

std::unique_ptr<RustDuckInputSwitcher>
rustduck_input_switcher_new(const RustDuckReport &report) {
  rustduck_test_maybe_fail(RustDuckFailurePoint::SwitcherConstruct);
  auto switcher =
      std::shared_ptr<void>(tspyNewInputSwitcher(report.raw()),
                            [report = report.retain()](void *pointer) {
                              if (pointer != nullptr) {
                                tspyDeleteInputSwitcher(pointer);
                              }
                            });
  if (!switcher) {
    throw std::runtime_error("native input switcher construction failed");
  }
  return std::make_unique<RustDuckInputSwitcher>(std::move(switcher));
}

void rustduck_input_switcher_register_input(
    RustDuckInputSwitcher &switcher, const RustDuckMemoryHandler &handler) {
  rustduck_test_maybe_fail(RustDuckFailurePoint::SwitcherRegisterHandler);
  switcher.retain_handler(handler);
  tspyPluginEventHandlerRegisterInput(switcher.raw(), handler.raw());
}

void rustduck_input_switcher_register_output(
    RustDuckInputSwitcher &switcher, const RustDuckMemoryHandler &handler) {
  rustduck_test_maybe_fail(RustDuckFailurePoint::SwitcherRegisterHandler);
  switcher.retain_handler(handler);
  tspyPluginEventHandlerRegisterOutput(switcher.raw(), handler.raw());
}

bool rustduck_input_switcher_start(RustDuckInputSwitcher &switcher,
                                   const RustDuckInputSwitcherConfig &config) {
  rustduck_test_maybe_fail(RustDuckFailurePoint::SwitcherStart);
  validate_utf16(config.plugins);
  validate_utf16(config.event_command);
  validate_utf16(config.event_udp_address);
  validate_utf16(config.event_local_address);
  const tspyInputSwitcherArgs args{
      config.fast_switch ? 1L : 0L,
      config.delayed_switch ? 1L : 0L,
      config.terminate ? 1L : 0L,
      config.reuse_port ? 1L : 0L,
      checked_long(config.first_input),
      checked_long(config.primary_input),
      checked_long(config.cycle_count),
      checked_long(config.buffered_packets),
      checked_long(config.max_input_packets),
      checked_long(config.max_output_packets),
      checked_long(config.sock_buffer),
      checked_long(config.remote_server_port),
      checked_long(config.receive_timeout),
      const_cast<std::uint8_t *>(buffer_data(config.plugins)),
      config.plugins.size(),
      const_cast<std::uint8_t *>(buffer_data(config.event_command)),
      config.event_command.size(),
      const_cast<std::uint8_t *>(buffer_data(config.event_udp_address)),
      config.event_udp_address.size(),
      checked_long(config.event_udp_port),
      const_cast<std::uint8_t *>(buffer_data(config.event_local_address)),
      config.event_local_address.size(),
      checked_long(config.event_ttl),
  };
  return tspyStartInputSwitcher(switcher.raw(), &args);
}

void rustduck_input_switcher_stop(RustDuckInputSwitcher &switcher) {
  tspyStopInputSwitcher(switcher.raw());
}

void rustduck_input_switcher_wait(RustDuckInputSwitcher &switcher) {
  tspyWaitInputSwitcher(switcher.raw());
}