#ifndef IAUDIOCAPTURER_H
#define IAUDIOCAPTURER_H

#include <cstdint>
#include <functional>

#include "basic/basic.h"

// 平台无关的音频格式结构
struct AudioFormat
{
    uint32_t sampleRate;     // 采样率 (Hz)
    uint16_t channels;       // 通道数
    uint16_t bitsPerSample;  // 每个样本的位数
    uint32_t avgBytesPerSec; // 平均每秒字节数
    uint16_t blockAlign;     // 数据块对齐大小

    // 默认构造函数
    AudioFormat()
        : sampleRate(0)
        , channels(0)
        , bitsPerSample(0)
        , avgBytesPerSec(0)
        , blockAlign(0)
    {}

    // 带参数的构造函数
    AudioFormat(uint32_t rate, uint16_t ch, uint16_t bits)
        : sampleRate(rate)
        , channels(ch)
        , bitsPerSample(bits)
    {
        blockAlign = channels * bitsPerSample / 8;
        avgBytesPerSec = sampleRate * blockAlign;
    }
};

class IAudioCapturer
{
public:
    // 默认音频格式
    static AudioFormat GetDefaultFormat()
    {
        return AudioFormat(AUDIO_SAMPLE_RATE, AUDIO_CHANNEL, AUDIO_CAPTURE_BITS_PER_SAMPLE);
    }

    enum Type { Microphone, Speaker };

    virtual ~IAudioCapturer() = default;

    virtual bool Init(Type deviceType) = 0;
    virtual bool Start() = 0;
    virtual void Stop() = 0;

    // 获取音频格式
    virtual const AudioFormat& GetFormat() const = 0;

    // 主动拉取音频数据，返回实际读取字节数
    virtual int readAudioData(char* buf, int maxLen) = 0;
};

#endif // IAUDIOCAPTURER_H
