Skip to content

C++17 API — RAII Streams, Targets & Discovery

Source of truth: crates/capture-ffi/include/cspa.hpp (namespace cspa, Cspa::cspa_ffi CMake target). All RAII handle classes are move-only (= delete copy constructor) and release native handles deterministically on destruction.


class ManagedStream {
public:
ManagedStream() noexcept : handle_(nullptr);
explicit ManagedStream(cspa_manager *handle) noexcept : handle_(handle);
static Result<ManagedStream> open(const CaptureRequest &request);
#if CSPA_HAS_EXCEPTIONS
static ManagedStream open_or_throw(const CaptureRequest &request);
#endif
ManagedStream(const ManagedStream &) = delete;
ManagedStream &operator=(const ManagedStream &) = delete;
ManagedStream(ManagedStream &&other) noexcept : handle_(other.handle_);
ManagedStream &operator=(ManagedStream &&other) noexcept;
~ManagedStream() noexcept { reset(); }
void reset() noexcept;
Result<cspa_pcm_format_record> output_format() const noexcept;
Result<uint64_t> update_targets(const std::vector<Target> &new_targets);
#if CSPA_HAS_EXCEPTIONS
uint64_t update_targets_or_throw(const std::vector<Target> &new_targets);
#endif
Result<size_t> input_count() const noexcept;
Result<cspa_input_status_record> input_status(size_t index) const noexcept;
Result<ReadOutcome> read(
uint64_t requested_frames,
uint64_t timeout_ms = 200,
CancelToken *cancel = nullptr
) noexcept;
Result<ReadOutcome> read_into(
uint8_t *destination,
size_t destination_bytes,
uint64_t requested_frames,
uint64_t timeout_ms = 200,
CancelToken *cancel = nullptr
) noexcept;
Status stop(bool abort_mode = false) noexcept;
bool wait_cleanup(uint64_t timeout_ms = 2000) noexcept;
cspa_manager *raw() const noexcept;
};
class AudioStream {
public:
AudioStream() noexcept : handle_(nullptr);
explicit AudioStream(cspa_stream *handle) noexcept : handle_(handle);
static Result<AudioStream> open(
const Target &target,
Overflow overflow = Overflow::DropOldest,
PermissionPolicy permission_policy = PermissionPolicy::AutoRequest
) noexcept;
#if CSPA_HAS_EXCEPTIONS
static AudioStream open_or_throw(
const Target &target,
Overflow overflow = Overflow::DropOldest,
PermissionPolicy permission_policy = PermissionPolicy::AutoRequest
);
#endif
AudioStream(const AudioStream &) = delete;
AudioStream &operator=(const AudioStream &) = delete;
AudioStream(AudioStream &&other) noexcept : handle_(other.handle_);
AudioStream &operator=(AudioStream &&other) noexcept;
~AudioStream() noexcept { reset(); }
void reset() noexcept;
Result<cspa_pcm_format_record> format() const noexcept;
Result<ReadOutcome> read_into(
uint8_t *destination,
size_t destination_bytes,
uint64_t requested_frames,
uint64_t timeout_ms = 100,
CancelToken *cancel = nullptr
) noexcept;
Status stop(bool abort_mode = false) noexcept;
bool wait_cleanup(uint64_t timeout_ms = 2000) noexcept;
};
class CaptureRequest {
public:
explicit CaptureRequest(std::vector<Target> targets)
: targets_(std::move(targets));
CaptureRequest &set_tree_strategy(ProcessTreeRange range) noexcept;
CaptureRequest &set_combine_mode(CombineMode mode) noexcept;
CaptureRequest &set_input_conversion(InputConversionPolicy policy) noexcept;
CaptureRequest &set_on_format_change(OnFormatChange policy) noexcept;
CaptureRequest &set_backends(Backends backends) noexcept;
CaptureRequest &set_output_converter(OutputConverter converter) noexcept;
CaptureRequest &set_mixer_config(MixerConfig config) noexcept;
CaptureRequest &set_allow_reroute(bool allow) noexcept;
CaptureRequest &set_require_precise_timing(bool require) noexcept;
CaptureRequest &set_overflow(Overflow overflow) noexcept;
CaptureRequest &set_permission_policy(PermissionPolicy policy) noexcept;
void build_raw(
std::vector<cspa_capture_target_record> &raw_targets,
cspa_capture_request_record &out_req
) const;
Result<PermissionReport> check_permissions() const;
Result<PermissionReport> request_permissions(
uint64_t timeout_ms = 30000,
bool open_settings_if_denied = false
) const;
Result<PlanReport> inspect() const;
Result<PlanReport> inspect_for_os(PlatformOs os) const;
#if CSPA_HAS_EXCEPTIONS
PlanReport inspect_or_throw() const { return unwrap_or_throw(inspect()); }
PlanReport inspect_for_os_or_throw(PlatformOs os) const;
PermissionReport check_permissions_or_throw() const;
PermissionReport request_permissions_or_throw(
uint64_t timeout_ms = 30000,
bool open_settings_if_denied = false
) const;
#endif
static PlanReport decode_plan_report(
const cspa_plan_report_record &rep,
std::vector<cspa_target_plan_record> &tplans
);
};
class Target {
public:
Target() noexcept;
explicit Target(cspa_capture_target_record record) noexcept : record_(record);
static Result<Target> process(
uint32_t pid,
uint64_t start_time = 0,
ProcessScope scope = ProcessScope::Tree,
OnRootExitMode on_root_exit = OnRootExitMode::Stop,
uint64_t wait_timeout_ns = 0,
float gain = 1.0f
) noexcept;
static Result<Target> application(
uint32_t pid,
uint64_t start_time = 0,
const char *app_id = nullptr,
const char *name = nullptr,
const char *executable_path = nullptr,
ProcessScope scope = ProcessScope::Tree,
OnRootExitMode on_root_exit = OnRootExitMode::Stop,
uint64_t wait_timeout_ns = 0,
float gain = 1.0f
) noexcept;
static Result<Target> device(const char *device_id, float gain = 1.0f) noexcept;
static Result<Target> input_device(const char *device_id, float gain = 1.0f) noexcept;
static Result<Target> default_output(
DefaultDeviceMode mode = DefaultDeviceMode::Follow,
float gain = 1.0f
) noexcept;
static Result<Target> microphone(
DefaultDeviceMode mode = DefaultDeviceMode::Follow,
float gain = 1.0f
) noexcept;
Result<Target> with_backend(BackendId backend) const noexcept;
Result<Target> with_gain(float gain) const noexcept;
Result<Target> lower_to_process() const noexcept;
#if CSPA_HAS_EXCEPTIONS
static Target process_or_throw(
uint32_t pid,
uint64_t start_time = 0,
ProcessScope scope = ProcessScope::Tree,
OnRootExitMode on_root_exit = OnRootExitMode::Stop,
uint64_t wait_timeout_ns = 0,
float gain = 1.0f
);
static Target device_or_throw(const char *device_id, float gain = 1.0f);
static Target input_device_or_throw(const char *device_id, float gain = 1.0f);
static Target default_output_or_throw(
DefaultDeviceMode mode = DefaultDeviceMode::Follow,
float gain = 1.0f
);
static Target microphone_or_throw(
DefaultDeviceMode mode = DefaultDeviceMode::Follow,
float gain = 1.0f
);
Target with_backend_or_throw(BackendId backend) const;
Target with_gain_or_throw(float gain) const;
Target lower_to_process_or_throw() const;
#endif
TargetKind kind() const noexcept { return static_cast<TargetKind>(record_.kind); }
BackendId backend() const noexcept { return static_cast<BackendId>(record_.backend); }
std::vector<BackendId> available_backends() const;
uint32_t pid() const noexcept;
const char *device_id() const noexcept;
const char *app_id() const noexcept;
const char *name() const noexcept;
const cspa_capture_target_record &raw() const noexcept;
};
class Backends {
public:
Backends() noexcept;
static uint32_t pack_list(const std::vector<BackendId> &list) noexcept;
static std::vector<BackendId> unpack_list(uint32_t packed);
Backends &set_windows(const std::vector<BackendId> &list) noexcept;
Backends &set_linux(const std::vector<BackendId> &list) noexcept;
Backends &set_macos(const std::vector<BackendId> &list) noexcept;
const cspa_backends_record &raw() const noexcept;
};
class Discovery {
public:
Discovery() noexcept : handle_(nullptr);
explicit Discovery(cspa_discovery *handle) noexcept : handle_(handle);
static Result<Discovery> create() noexcept;
static Result<Discovery> with_backends(const Backends &backends) noexcept;
#if CSPA_HAS_EXCEPTIONS
static Discovery create_or_throw() { return unwrap_or_throw(create()); }
#endif
Discovery(const Discovery &) = delete;
Discovery &operator=(const Discovery &) = delete;
Discovery(Discovery &&other) noexcept : handle_(other.handle_);
Discovery &operator=(Discovery &&other) noexcept;
~Discovery() noexcept { reset(); }
void reset() noexcept;
Result<std::vector<Target>> query(const DiscoveryQuery &q) const;
Result<std::vector<Target>> output_devices() const;
Result<std::vector<Target>> input_devices() const;
Result<DiscoverySubscription> subscribe_query(
const DiscoveryQuery &q,
size_t max_queue = 256
) const noexcept;
cspa_discovery *raw() const noexcept;
};
class DiscoveryQuery {
public:
DiscoveryQuery() noexcept;
DiscoveryQuery &set_backends(const Backends &backends) noexcept;
DiscoveryQuery &set_dedupe(DiscoveryDedupe dedupe) noexcept;
DiscoveryQuery &set_order(DiscoveryOrder order) noexcept;
DiscoveryQuery &set_active_only(bool active_only) noexcept;
DiscoveryQuery &set_include_non_audio(bool include_non_audio) noexcept;
DiscoveryQuery &set_kinds(
bool apps,
bool procs,
bool output_devs,
bool input_devs
) noexcept;
const cspa_discovery_query_record &raw() const noexcept;
};
class DiscoverySubscription {
public:
DiscoverySubscription() noexcept : handle_(nullptr);
explicit DiscoverySubscription(cspa_discovery_subscription *handle) noexcept
: handle_(handle);
DiscoverySubscription(const DiscoverySubscription &) = delete;
DiscoverySubscription &operator=(const DiscoverySubscription &) = delete;
DiscoverySubscription(DiscoverySubscription &&other) noexcept : handle_(other.handle_);
DiscoverySubscription &operator=(DiscoverySubscription &&other) noexcept;
~DiscoverySubscription() noexcept { reset(); }
void reset() noexcept;
Result<bool> is_overflowed() const noexcept;
Result<bool> poll(DiscoveryEvent &out_event) const;
Result<std::vector<Target>> resnapshot(uint64_t *out_revision = nullptr) const;
cspa_discovery_subscription *raw() const noexcept;
};
struct DiscoveryEvent {
DiscoveryEventKind kind = DiscoveryEventKind::Added;
uint32_t visibility = 0;
uint64_t revision = 0;
uint64_t minimum_revision = 0;
std::string subject;
Target target;
};
class PcmBlock {
public:
PcmBlock() noexcept { std::memset(&record_, 0, sizeof(record_)); }
explicit PcmBlock(cspa_block_record record) noexcept : record_(record);
PcmBlock(const PcmBlock &) = delete;
PcmBlock &operator=(const PcmBlock &) = delete;
PcmBlock(PcmBlock &&other) noexcept : record_(other.record_);
PcmBlock &operator=(PcmBlock &&other) noexcept;
~PcmBlock() noexcept { release(); }
void release() noexcept;
const uint8_t *data() const noexcept;
uint64_t byte_len() const noexcept;
uint64_t frames() const noexcept;
uint64_t sequence() const noexcept;
uint64_t segment() const noexcept;
bool valid() const noexcept;
};
struct ReadOutcome {
PartialReason reason = PartialReason::None;
uint32_t boundary_kind = 0;
bool has_mixed_metadata = false;
uint64_t session = 0;
uint64_t segment = 0;
uint64_t frames = 0;
uint64_t copy_bytes = 0;
uint64_t copy_destinations = 0;
PcmBlock block;
cspa_mixed_block_metadata_record mixed_metadata{};
};
class CancelToken {
public:
CancelToken() noexcept : handle_(nullptr);
explicit CancelToken(cspa_cancel *handle) noexcept : handle_(handle);
static Result<CancelToken> create() noexcept;
CancelToken(const CancelToken &) = delete;
CancelToken &operator=(const CancelToken &) = delete;
CancelToken(CancelToken &&other) noexcept : handle_(other.handle_);
CancelToken &operator=(CancelToken &&other) noexcept;
~CancelToken() noexcept;
void cancel() noexcept;
bool is_cancelled() const noexcept;
cspa_cancel *raw() const noexcept;
};