399 lines
15 KiB
C++
399 lines
15 KiB
C++
// ================================================================================================
|
|
// Audio Backend - 硬件设备管理器
|
|
// ================================================================================================
|
|
// 描述: 音频设备的抽象层,支持跨平台设备管理
|
|
// 功能: 设备枚举、配置、热插拔检测
|
|
// ================================================================================================
|
|
|
|
#pragma once
|
|
|
|
#include "audio_buffer.h"
|
|
#include "error.h"
|
|
#include "logger.h"
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <unordered_map>
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <thread>
|
|
|
|
namespace audio_backend::frontend {
|
|
|
|
// ================================================================================================
|
|
// 设备类型和状态
|
|
// ================================================================================================
|
|
enum class DeviceType {
|
|
Unknown,
|
|
Input, // 输入设备
|
|
Output, // 输出设备
|
|
Duplex // 双工设备
|
|
};
|
|
|
|
enum class DeviceState {
|
|
Unknown,
|
|
Available, // 可用
|
|
Active, // 激活中
|
|
Busy, // 忙碌
|
|
Disabled, // 禁用
|
|
Disconnected // 断开连接
|
|
};
|
|
|
|
enum class DeviceDriver {
|
|
Unknown,
|
|
ASIO, // Windows ASIO
|
|
WASAPI, // Windows WASAPI
|
|
DirectSound, // Windows DirectSound
|
|
ALSA, // Linux ALSA
|
|
JACK, // Linux/Mac JACK
|
|
PulseAudio, // Linux PulseAudio
|
|
CoreAudio, // macOS Core Audio
|
|
PortAudio // 跨平台 PortAudio
|
|
};
|
|
|
|
// ================================================================================================
|
|
// 设备能力信息
|
|
// ================================================================================================
|
|
struct DeviceCapabilities {
|
|
std::vector<uint32_t> supported_sample_rates;
|
|
std::vector<uint16_t> supported_channel_counts;
|
|
std::vector<engine::AudioFormat> supported_formats;
|
|
std::pair<uint32_t, uint32_t> buffer_size_range{64, 8192};
|
|
uint32_t default_buffer_size = 512;
|
|
double minimum_latency_ms = 0.0;
|
|
double maximum_latency_ms = 1000.0;
|
|
bool supports_exclusive_mode = false;
|
|
bool supports_shared_mode = true;
|
|
};
|
|
|
|
// ================================================================================================
|
|
// 完整的设备信息
|
|
// ================================================================================================
|
|
struct AudioDeviceInfo {
|
|
// 基础信息
|
|
std::string id; // 设备唯一标识符
|
|
std::string name; // 设备显示名称
|
|
std::string description; // 设备描述
|
|
DeviceType type; // 设备类型
|
|
DeviceDriver driver; // 驱动类型
|
|
DeviceState state; // 设备状态
|
|
|
|
// 设备属性
|
|
bool is_default_input = false; // 是否为默认输入设备
|
|
bool is_default_output = false; // 是否为默认输出设备
|
|
bool is_system_default = false; // 是否为系统默认设备
|
|
bool supports_control_panel = false; // 是否支持控制面板
|
|
|
|
// 硬件信息
|
|
std::string manufacturer; // 制造商
|
|
std::string model; // 型号
|
|
std::string version; // 版本
|
|
std::string serial_number; // 序列号
|
|
|
|
// 能力信息
|
|
DeviceCapabilities capabilities;
|
|
|
|
// 当前配置
|
|
uint32_t current_sample_rate = 48000;
|
|
uint16_t current_channels = 2;
|
|
engine::AudioFormat current_format = engine::AudioFormat::FLOAT32;
|
|
uint32_t current_buffer_size = 512;
|
|
|
|
// 延迟信息
|
|
double input_latency_ms = 0.0;
|
|
double output_latency_ms = 0.0;
|
|
double total_latency_ms = 0.0;
|
|
|
|
// 时间戳
|
|
std::chrono::system_clock::time_point last_seen;
|
|
std::chrono::system_clock::time_point last_updated;
|
|
};
|
|
|
|
// ================================================================================================
|
|
// 设备配置
|
|
// ================================================================================================
|
|
struct DeviceConfiguration {
|
|
std::string device_id;
|
|
uint32_t sample_rate = 48000;
|
|
uint16_t channels = 2;
|
|
engine::AudioFormat format = engine::AudioFormat::FLOAT32;
|
|
uint32_t buffer_size = 512;
|
|
bool exclusive_mode = false;
|
|
bool enable_monitoring = false;
|
|
double volume = 1.0;
|
|
bool muted = false;
|
|
|
|
// 验证配置
|
|
bool is_valid() const {
|
|
return !device_id.empty() &&
|
|
sample_rate > 0 && sample_rate <= 192000 &&
|
|
channels > 0 && channels <= 32 &&
|
|
format != engine::AudioFormat::UNKNOWN &&
|
|
buffer_size >= 64 && buffer_size <= 8192 &&
|
|
volume >= 0.0 && volume <= 2.0;
|
|
}
|
|
};
|
|
|
|
// ================================================================================================
|
|
// 设备事件监听器
|
|
// ================================================================================================
|
|
class IDeviceEventListener {
|
|
public:
|
|
virtual ~IDeviceEventListener() = default;
|
|
|
|
// 设备生命周期事件
|
|
virtual void on_device_added(const AudioDeviceInfo& device) = 0;
|
|
virtual void on_device_removed(const std::string& device_id) = 0;
|
|
virtual void on_device_state_changed(const std::string& device_id,
|
|
DeviceState old_state,
|
|
DeviceState new_state) = 0;
|
|
|
|
// 默认设备变更
|
|
virtual void on_default_device_changed(DeviceType type, const std::string& new_device_id) = 0;
|
|
|
|
// 设备配置变更
|
|
virtual void on_device_config_changed(const std::string& device_id,
|
|
const DeviceConfiguration& new_config) = 0;
|
|
|
|
// 音频流事件
|
|
virtual void on_audio_data_available(const std::string& device_id,
|
|
const engine::AudioBuffer& buffer) = 0;
|
|
|
|
// 错误事件
|
|
virtual void on_device_error(const std::string& device_id,
|
|
common::ErrorCode error,
|
|
const std::string& message) = 0;
|
|
};
|
|
|
|
// ================================================================================================
|
|
// 设备管理器配置
|
|
// ================================================================================================
|
|
struct DeviceManagerConfig {
|
|
// 基础配置
|
|
bool auto_detect_devices = true;
|
|
bool enable_hot_plug_detection = true;
|
|
uint32_t device_scan_interval_ms = 1000;
|
|
uint32_t device_timeout_ms = 5000;
|
|
|
|
// 驱动配置
|
|
std::vector<DeviceDriver> preferred_drivers = {
|
|
#ifdef _WIN32
|
|
DeviceDriver::ASIO,
|
|
DeviceDriver::WASAPI,
|
|
DeviceDriver::DirectSound
|
|
#elif defined(__APPLE__)
|
|
DeviceDriver::CoreAudio,
|
|
DeviceDriver::JACK
|
|
#else
|
|
DeviceDriver::JACK,
|
|
DeviceDriver::ALSA,
|
|
DeviceDriver::PulseAudio
|
|
#endif
|
|
};
|
|
|
|
// 缓冲配置
|
|
uint32_t default_buffer_size = 512;
|
|
uint32_t min_buffer_size = 64;
|
|
uint32_t max_buffer_size = 8192;
|
|
|
|
// 性能配置
|
|
bool enable_exclusive_mode = false;
|
|
bool enable_zero_copy = true;
|
|
uint32_t io_thread_priority = 99; // 实时优先级
|
|
|
|
// 监控配置
|
|
bool enable_device_monitoring = true;
|
|
bool enable_performance_monitoring = true;
|
|
uint32_t monitoring_interval_ms = 1000;
|
|
};
|
|
|
|
// ================================================================================================
|
|
// 设备统计信息
|
|
// ================================================================================================
|
|
struct DeviceStatistics {
|
|
std::atomic<uint64_t> frames_processed{0};
|
|
std::atomic<uint64_t> buffer_underruns{0};
|
|
std::atomic<uint64_t> buffer_overruns{0};
|
|
std::atomic<uint64_t> device_errors{0};
|
|
std::atomic<double> average_cpu_usage{0.0};
|
|
std::atomic<double> peak_cpu_usage{0.0};
|
|
std::atomic<double> average_latency_ms{0.0};
|
|
std::atomic<double> peak_latency_ms{0.0};
|
|
std::chrono::steady_clock::time_point start_time;
|
|
};
|
|
|
|
// ================================================================================================
|
|
// 硬件设备管理器
|
|
// ================================================================================================
|
|
class DeviceManager {
|
|
public:
|
|
explicit DeviceManager(const DeviceManagerConfig& config);
|
|
~DeviceManager();
|
|
|
|
// 禁止拷贝和移动
|
|
DeviceManager(const DeviceManager&) = delete;
|
|
DeviceManager& operator=(const DeviceManager&) = delete;
|
|
DeviceManager(DeviceManager&&) = delete;
|
|
DeviceManager& operator=(DeviceManager&&) = delete;
|
|
|
|
// 生命周期管理
|
|
common::ErrorCode initialize();
|
|
common::ErrorCode shutdown();
|
|
bool is_initialized() const { return initialized_.load(); }
|
|
|
|
// 事件监听
|
|
void add_event_listener(std::shared_ptr<IDeviceEventListener> listener);
|
|
void remove_event_listener(std::shared_ptr<IDeviceEventListener> listener);
|
|
|
|
// 设备枚举和查询
|
|
std::vector<AudioDeviceInfo> get_all_devices() const;
|
|
std::vector<AudioDeviceInfo> get_input_devices() const;
|
|
std::vector<AudioDeviceInfo> get_output_devices() const;
|
|
std::optional<AudioDeviceInfo> get_device_info(const std::string& device_id) const;
|
|
|
|
// 默认设备管理
|
|
std::string get_default_input_device() const;
|
|
std::string get_default_output_device() const;
|
|
common::ErrorCode set_default_input_device(const std::string& device_id);
|
|
common::ErrorCode set_default_output_device(const std::string& device_id);
|
|
|
|
// 设备配置
|
|
common::ErrorCode configure_device(const DeviceConfiguration& config);
|
|
std::optional<DeviceConfiguration> get_device_configuration(const std::string& device_id) const;
|
|
|
|
// 音频流控制
|
|
common::ErrorCode start_input_stream(const std::string& device_id);
|
|
common::ErrorCode stop_input_stream(const std::string& device_id);
|
|
common::ErrorCode start_output_stream(const std::string& device_id);
|
|
common::ErrorCode stop_output_stream(const std::string& device_id);
|
|
|
|
// 音频数据处理
|
|
common::ErrorCode write_audio_data(const std::string& device_id,
|
|
const engine::AudioBuffer& buffer);
|
|
common::ErrorCode read_audio_data(const std::string& device_id,
|
|
engine::AudioBuffer& buffer,
|
|
std::chrono::milliseconds timeout = std::chrono::milliseconds{100});
|
|
|
|
// 设备控制
|
|
common::ErrorCode set_device_volume(const std::string& device_id, double volume);
|
|
common::ErrorCode get_device_volume(const std::string& device_id, double& volume) const;
|
|
common::ErrorCode set_device_mute(const std::string& device_id, bool muted);
|
|
common::ErrorCode get_device_mute(const std::string& device_id, bool& muted) const;
|
|
|
|
// 测试和校准
|
|
common::ErrorCode test_device(const std::string& device_id);
|
|
common::ErrorCode calibrate_latency(const std::string& device_id);
|
|
|
|
// 统计信息
|
|
const DeviceStatistics& get_statistics() const { return statistics_; }
|
|
std::optional<DeviceStatistics> get_device_statistics(const std::string& device_id) const;
|
|
void reset_statistics();
|
|
|
|
// 配置管理
|
|
const DeviceManagerConfig& config() const { return config_; }
|
|
common::ErrorCode update_config(const DeviceManagerConfig& new_config);
|
|
|
|
private:
|
|
// 前向声明
|
|
class DeviceEnumerator;
|
|
class DeviceController;
|
|
class AudioStream;
|
|
|
|
// 内部初始化
|
|
common::ErrorCode initialize_drivers();
|
|
common::ErrorCode initialize_device_detection();
|
|
common::ErrorCode shutdown_all_streams();
|
|
|
|
// 设备检测
|
|
void scan_devices();
|
|
void detect_device_changes();
|
|
void handle_device_arrival(const std::string& device_id);
|
|
void handle_device_removal(const std::string& device_id);
|
|
|
|
// 事件通知
|
|
void notify_device_added(const AudioDeviceInfo& device);
|
|
void notify_device_removed(const std::string& device_id);
|
|
void notify_device_state_changed(const std::string& device_id,
|
|
DeviceState old_state,
|
|
DeviceState new_state);
|
|
void notify_default_device_changed(DeviceType type, const std::string& new_device_id);
|
|
void notify_audio_data_available(const std::string& device_id,
|
|
const engine::AudioBuffer& buffer);
|
|
void notify_device_error(const std::string& device_id,
|
|
common::ErrorCode error,
|
|
const std::string& message);
|
|
|
|
// 工作线程
|
|
void device_monitor_worker();
|
|
void performance_monitor_worker();
|
|
void audio_io_worker();
|
|
|
|
// 驱动管理
|
|
common::ErrorCode initialize_driver(DeviceDriver driver);
|
|
void shutdown_driver(DeviceDriver driver);
|
|
|
|
private:
|
|
DeviceManagerConfig config_;
|
|
std::atomic<bool> initialized_{false};
|
|
std::atomic<bool> shutdown_requested_{false};
|
|
|
|
// 设备缓存
|
|
std::unordered_map<std::string, AudioDeviceInfo> cached_devices_;
|
|
mutable std::mutex devices_mutex_;
|
|
|
|
// 活动配置
|
|
std::unordered_map<std::string, DeviceConfiguration> active_configs_;
|
|
mutable std::mutex configs_mutex_;
|
|
|
|
// 默认设备
|
|
std::string default_input_device_;
|
|
std::string default_output_device_;
|
|
mutable std::mutex defaults_mutex_;
|
|
|
|
// 设备控制器
|
|
std::unordered_map<DeviceDriver, std::unique_ptr<DeviceController>> device_controllers_;
|
|
std::unique_ptr<DeviceEnumerator> device_enumerator_;
|
|
|
|
// 音频流
|
|
std::unordered_map<std::string, std::unique_ptr<AudioStream>> active_streams_;
|
|
mutable std::mutex streams_mutex_;
|
|
|
|
// 事件监听器
|
|
std::vector<std::weak_ptr<IDeviceEventListener>> event_listeners_;
|
|
mutable std::mutex listeners_mutex_;
|
|
|
|
// 工作线程
|
|
std::vector<std::thread> worker_threads_;
|
|
|
|
// 统计信息
|
|
DeviceStatistics statistics_;
|
|
std::unordered_map<std::string, DeviceStatistics> device_statistics_;
|
|
|
|
// 同步原语
|
|
mutable std::mutex state_mutex_;
|
|
std::condition_variable monitor_cv_;
|
|
};
|
|
|
|
// ================================================================================================
|
|
// 跨平台设备工厂
|
|
// ================================================================================================
|
|
namespace device_factory {
|
|
|
|
// 创建平台特定的设备控制器
|
|
std::unique_ptr<DeviceManager> create_device_manager(const DeviceManagerConfig& config = {});
|
|
|
|
// 创建针对特定驱动的设备管理器
|
|
std::unique_ptr<DeviceManager> create_driver_specific_manager(DeviceDriver driver);
|
|
|
|
// 获取系统推荐的设备配置
|
|
DeviceManagerConfig get_recommended_config();
|
|
|
|
// 检测系统支持的驱动
|
|
std::vector<DeviceDriver> detect_available_drivers();
|
|
|
|
} // namespace device_factory
|
|
|
|
} // namespace audio_backend::frontend
|