Android 音频系统基于 Linux 的 ALSA 驱动, tinyalsa 在 alsa 的驱动基础上封装音频接口,提供给 audio HAL, audio HAL 提供接口给 Android audioflinger 等 framework。 HAL 需要实现 audio 硬件的打开与关闭(这里是 android 认为的硬件)。
static inline int audio_hw_device_open(const struct hw_module_t* module,
struct audio_hw_device** device)
{
return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,
(struct hw_device_t**)device);
}
static inline int audio_hw_device_close(struct audio_hw_device* device)
{
return device->common.close(&device->common);
}
需要实现 in 和 out 的 数据流 struct audio_stream_out struct audio_stream_in
in 主要有 read 方法用于读取音频数据, out 主要有 write 方法,写入数据到设备,分别实现录音和放音。 详见: hardware/libhardware/include/hardware/audio.h
其中的方法又是调用的 tinyalsa 的接口,关于 pcm 的操作:
/* Open and close a stream */
struct pcm *pcm_open(unsigned int card, unsigned int device,
unsigned int flags, struct pcm_config *config);
int pcm_close(struct pcm *pcm);
int pcm_is_ready(struct pcm *pcm);
/* Obtain the parameters for a PCM */
struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
unsigned int flags);
void pcm_params_free(struct pcm_params *pcm_params);
unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
enum pcm_param param);
unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
enum pcm_param param);
/* Set and get config */
int pcm_get_config(struct pcm *pcm, struct pcm_config *config);
int pcm_set_config(struct pcm *pcm, struct pcm_config *config);
/* Returns a human readable reason for the last error */
const char *pcm_get_error(struct pcm *pcm);
/* Returns the sample size in bits for a PCM format.
- As with ALSA formats, this is the storage size for the format, whereas the
- format represents the number of significant bits. For example,
- PCM_FORMAT_S24_LE uses 32 bits of storage.
*/
unsigned int pcm_format_to_bits(enum pcm_format format);
/* Returns the buffer size (int frames) that should be used for pcm_write. */
unsigned int pcm_get_buffer_size(struct pcm *pcm);
unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames);
unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes);
/* Returns the pcm latency in ms */
unsigned int pcm_get_latency(struct pcm *pcm);
/* Returns available frames in pcm buffer and corresponding time stamp.
- The clock is CLOCK_MONOTONIC if flag PCM_MONOTONIC was specified in pcm_open,
- otherwise the clock is CLOCK_REALTIME.
- For an input stream, frames available are frames ready for the
- application to read.
- For an output stream, frames available are the number of empty frames available
- for the application to write.
*/
int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
struct timespec *tstamp);
/* Write data to the fifo.
- Will start playback on the first write or on a write that
- occurs after a fifo underrun.
*/
int pcm_write(struct pcm *pcm, const void *data, unsigned int count);
int pcm_read(struct pcm *pcm, void *data, unsigned int count);
/*
- mmap() support.
*/
int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count);
int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count);
int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
unsigned int *frames);
int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames);
/* Start and stop a PCM channel that doesn’t transfer data */
int pcm_start(struct pcm *pcm);
int pcm_stop(struct pcm *pcm);
/* Interrupt driven API */
int pcm_wait(struct pcm *pcm, int timeout);
/* Change avail_min after the stream has been opened with no need to stop the stream.
- Only accepted if opened with PCM_MMAP and PCM_NOIRQ flags
*/
int pcm_set_avail_min(struct pcm *pcm, int avail_min);