#ifndef _LINUX_ALSA_COMMON_H_
#define _LINUX_ALSA_COMMON_H_

#include <alsa/asoundlib.h>
#include <yunxin/util/stdbool.h>
#include <yunxin/util/nim_base_types.h>

#ifdef __cplusplus
extern"C"
{
#endif
#define FUNC_GET_NUM_OF_DEVICE 0
#define FUNC_GET_DEVICE_NAME 1
#define FUNC_GET_DEVICE_NAME_FOR_AN_ENUM 2
#define  kAdmMaxDeviceNameSize 100
typedef struct audio_alsa_mixer_s
{
	int32_t id_;
	pthread_mutex_t* mutex_;
	snd_mixer_t* mixer_output_handle_;
	snd_mixer_t* mixer_input_handle_;
	snd_mixer_elem_t* mixer_output_element_;
	snd_mixer_elem_t* mixer_input_element_;
	char mixer_input_str[kAdmMaxDeviceNameSize];
	char mixer_output_str[kAdmMaxDeviceNameSize];
}audio_alsa_mixer_t;

void* audio_alsa_mixer_create(int32_t id);
int32_t audio_mixer_open_speaker(audio_alsa_mixer_t* audio_mixer,char* device_name);
int32_t audio_mixer_open_microphone(audio_alsa_mixer_t* audio_mixer,char* device_name);
int32_t audio_mixer_set_speaker_volume(audio_alsa_mixer_t* audio_mixer,uint32_t volume);
uint32_t audio_mixer_get_speaker_volume(audio_alsa_mixer_t* audio_mixer);
uint32_t audio_mixer_get_max_speaker_volume(audio_alsa_mixer_t* audio_mixer);
uint32_t audio_mixer_get_min_speaker_volume(audio_alsa_mixer_t* audio_mixer);
int32_t audio_mixer_set_speaker_mute(audio_alsa_mixer_t* audio_mixer, bool mute);
bool audio_mixer_get_speaker_mute(audio_alsa_mixer_t* audio_mixer);
int32_t audio_mixer_set_microphone_mute(audio_alsa_mixer_t* audio_mixer, bool mute);
bool audio_mixer_get_microphone_mute(audio_alsa_mixer_t* audio_mixer);
int32_t audio_mixer_set_microphone_volume(audio_alsa_mixer_t* audio_mixer, uint32_t volume);
uint32_t audio_mixer_get_microphone_volume(audio_alsa_mixer_t* audio_mixer);
uint32_t audio_mixer_get_max_microphone_volume(audio_alsa_mixer_t* audio_mixer);
uint32_t audio_mixer_get_min_microphone_volume(audio_alsa_mixer_t* audio_mixer);
int32_t audio_mixer_close(audio_alsa_mixer_t* audio_mixer);
int32_t audio_mixer_close_speaker(audio_alsa_mixer_t* audio_mixer);
int32_t audio_mixer_close_microphone(audio_alsa_mixer_t* audio_mixer);
void audio_alsa_mixer_destroy(audio_alsa_mixer_t** audio_mixer);

typedef struct audio_alsa_core_s
{
	snd_pcm_t* pcm_handle_;
	audio_alsa_mixer_t alsa_mixer_;
	bool       capture_;
	bool	   capturing_;
	int		channel_;
	int   bits_per_channel_;
	int   bytes_per_frame_;
	int   samplerate_;
	uint32_t cache_size_;
}audio_alsa_core_t;

audio_alsa_core_t* nrtc_audio_alsa_core_create(bool input, int channel, int samplerate, int bit);

//打开设备
int nrtc_alsa_open_device(audio_alsa_core_t* core, const char* deviceid);

//关闭设备
void nrtc_alsa_close_device(audio_alsa_core_t** core);

//获取alsa设备
char* nrtc_alsa_available_devices(bool input);

//获取alsa设备
/*
* function 函数作用 FUNC_GET_NUM_OF_DEVICE FUNC_GET_DEVICE_NAME FUNC_GET_DEVICE_NAME_FOR_AN_ENUM
* playback 用于播放
* enum_device_no 设备index
* enum_device_name 存储设备名
* edn_len enum_device_name长度
*/
int32_t nrtc_alsa_get_devices_info(int32_t function, bool playback, int32_t enum_device_no, char* enum_device_name, int32_t edn_len);

//开始操作
int nrtc_alsa_start_opt(audio_alsa_core_t* core);

//停止操作
int nrtc_alsa_stop_opt(audio_alsa_core_t* core);

//往alsa中写入数据
int nrtc_alsa_write_data(audio_alsa_core_t* alsa, char* data, int len);

//从alsa中读取数据
int nrtc_alsa_read_data(audio_alsa_core_t* alsa, char* data, int len);

//音量操作
int nrtc_alsa_set_volume(audio_alsa_core_t* alsa,bool capture,int volume);

int nrtc_alsa_get_volume(audio_alsa_core_t* alsa, bool capture);




#ifdef __cplusplus
}
#endif// __cplusplus

#endif 


