提交 5bb29138 编写于 作者: T twwang

modify some bad coding style

modify some bad coding style
上级 ad842aa0
......@@ -14,10 +14,9 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include"alsa_helper.h"
#include<vector>
#include<cstdio>
#include"alsa_helper.h"
namespace anbox {
namespace audio {
......@@ -40,14 +39,13 @@ AlsaHelper::AlsaHelper()
AlsaHelper::~AlsaHelper()
{
DEBUG("Alsa helper disconstructor is called");
close_pcm_device();
pcm_name = nullptr;
}
int AlsaHelper::open_pcm_device(const char *name, snd_pcm_stream_t stream, int open_mode)
{
int res;
int res = -1;
res = snd_pcm_open(&mhandle, name, stream, open_mode);
if (res < 0) {
ERROR("alsa helper open pcm device fail and device name = %s and error code = %d", name, res);
......@@ -82,7 +80,9 @@ int AlsaHelper::set_pcm_params(hwparams params)
significant_bits_per_sample = snd_pcm_format_width(mhwparams.format);
bits_per_sample = snd_pcm_format_physical_width(mhwparams.format);
bits_per_frame = bits_per_sample * mhwparams.channels;
chunk_bytes = period_frames * bits_per_frame / bits_per_sample;
if (bits_per_sample != 0) {
chunk_bytes = period_frames * bits_per_frame / bits_per_sample;
}
DEBUG("alsa helper set_pcm_params successful, chunk_bytes = %lu, period_size = %lu", chunk_bytes, period_frames);
return error;
}
......@@ -113,8 +113,7 @@ int AlsaHelper::set_params()
}
rate = mhwparams.rate;
if (rate < DEFAULT_SAMPLE_RATE_LOWEST || rate > DEFAULT_SAMPLE_RATE_HIGHEST) {
err = -1;
return err;
return -1;
}
err = snd_pcm_hw_params_set_rate_near(mhandle, params, &mhwparams.rate, 0);
rate = mhwparams.rate;
......@@ -131,14 +130,13 @@ int AlsaHelper::set_params()
snd_pcm_hw_params_get_period_size(params, &period_frames, 0);
snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
if (period_frames == buffer_size) {
ERROR("Can't use period equal to buffer size (%lu == %lu)",period_frames, buffer_size);
ERROR("Can't use period equal to buffer size (%lu == %lu)", period_frames, buffer_size);
err = -1;
return err;
}
return err;
}
size_t AlsaHelper::pcm_read (char *readBuf, size_t length)
size_t AlsaHelper::pcm_read(char *readBuf, size_t length)
{
ssize_t r;
size_t count = length;
......@@ -156,15 +154,15 @@ size_t AlsaHelper::pcm_read (char *readBuf, size_t length)
snd_pcm_wait(mhandle, wait_times);
} else if (r == -EPIPE) {
if ((error = xrun()) < 0) {
goto end;
return 0;
}
} else if (r == -ESTRPIPE) {
if ((error = suspend()) < 0) {
goto end;
return 0;
}
} else if (r < 0) {
ERROR("pcm read has fail,error code = %zu", r);
goto end;
return 0;
}
if (r > 0) {
if (mhwparams.channels) {
......@@ -176,19 +174,15 @@ size_t AlsaHelper::pcm_read (char *readBuf, size_t length)
ERROR("after has read data r = %d count = %d", r, count);
}
return length;
end:
length = 0;
return length;
}
bool AlsaHelper::get_device_config (hwparams& device_config) const
bool AlsaHelper::get_device_config(hwparams& device_config) const
{
bool res = false; // it wil read from config file
// it wil read from config file
device_config.format = SND_PCM_FORMAT_S16_LE;
device_config.channels = CHANELTYPE_MONO;
device_config.rate = DEFAULT_SAMPLE_RATE;
res = true;
return res;
return true;
}
/* peak handler */
......@@ -197,16 +191,14 @@ void AlsaHelper::compute_max_peak(char *data, size_t count) const
const size_t size = 2;
signed int max, perc[size], max_peak[size];
static int run = 0;
int ichans, c;
int vumeter = CHANELTYPE_MONO; // channel is 1
ichans = 1;
int c = 0;
int vumeter = CHANELTYPE_MONO;
memset(max_peak, 0, sizeof(max_peak));
if (bits_per_sample == PER_SAMPLE_16BITS) {
signed short *valp = reinterpret_cast<signed short *> (data);
signed short *valp = reinterpret_cast<signed short *>(data);
signed short mask = snd_pcm_format_silence_16(mhwparams.format);
signed short sval;
signed short sval = 0;
count = count >> 1;
c = 0;
while (count-- > 0) {
sval = le16toh(*valp);
sval = abs(sval) ^ mask;
......@@ -229,6 +221,7 @@ void AlsaHelper::compute_max_peak(char *data, size_t count) const
max = 0x7fffffff;
}
int unit = 100;
int ichans = 1;
for (c = 0; c < ichans; c++) {
if (bits_per_sample > PER_SAMPLE_16BITS) {
perc[c] = max_peak[c] / (max / unit);
......@@ -240,8 +233,8 @@ void AlsaHelper::compute_max_peak(char *data, size_t count) const
int AlsaHelper::suspend(void)
{
ERROR("pcm read data has suspend");
int res = 0;
ERROR ("pcm read data has suspend ");
while ((res = snd_pcm_resume(mhandle)) == -EAGAIN) {
sleep(1);
}
......@@ -255,8 +248,8 @@ int AlsaHelper::suspend(void)
int AlsaHelper::xrun()
{
snd_pcm_status_t *status;
int res;
snd_pcm_status_t *status = nullptr;
int res = -1;
snd_pcm_status_alloca(&status);
if ((res = snd_pcm_status(mhandle, status)) < 0) {
return res;
......@@ -314,14 +307,14 @@ snd_pcm_uframes_t AlsaHelper::get_period_frames_bytes() const
return period_frames * (bits_per_frame / BYTE2BITS);
}
std::string AlsaHelper::get_usb_audio_device_name () const
std::string AlsaHelper::get_usb_audio_device_name() const
{
std::string deviceName = "default";
snd_ctl_t *handle = nullptr;
int card, err, dev;
int err = -1;
snd_ctl_card_info_t *info = nullptr;
snd_ctl_card_info_alloca(&info);
card = -1;
int card = -1;
if (snd_card_next(&card) < 0 || card < 0) {
return deviceName;
}
......@@ -340,20 +333,22 @@ std::string AlsaHelper::get_usb_audio_device_name () const
break;
}
}
dev = -1;
int dev = -1;
while (true) {
snd_ctl_pcm_next_device(handle, &dev);
if (dev < 0) {
break;
}
std::string card_info_id(snd_ctl_card_info_get_id(info));
std::string card_info_name(snd_ctl_card_info_get_name(info));
if ((card_info_id == "Audio") && (card_info_name == "USB Audio")) {
deviceName = "hw:" + std::to_string(card) + "," + std::to_string(dev);
if (validateAudioDevice(deviceName)) {
snd_ctl_close(handle);
return deviceName;
}
if ((snd_ctl_card_info_get_id(info) != nullptr) && (snd_ctl_card_info_get_name(info)!= nullptr)) {
std::string card_info_id(snd_ctl_card_info_get_id(info));
std::string card_info_name(snd_ctl_card_info_get_name(info));
if ((card_info_id == "Audio") && (card_info_name == "USB Audio")) {
deviceName = "hw:" + std::to_string(card) + "," + std::to_string(dev);
if (validateAudioDevice(deviceName)) {
snd_ctl_close(handle);
return deviceName;
}
}
}
}
snd_ctl_close(handle);
......@@ -366,9 +361,9 @@ std::string AlsaHelper::get_usb_audio_device_name () const
bool AlsaHelper::validateAudioDevice(std::string name) const
{
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int rate;
snd_pcm_t *handle = nullptr;
snd_pcm_hw_params_t *params = nullptr;
unsigned int rate = 0;
float lowLimit = 0.95;
float upLimit = 1.05;
hwparams specHwparams = {
......@@ -402,6 +397,5 @@ bool AlsaHelper::validateAudioDevice(std::string name) const
snd_pcm_close(handle);
return true;
}
} // audio
} // anbox
}
}
......@@ -16,14 +16,14 @@
*/
#include "anbox/platform/alsa/audio_source.h"
#include "anbox/logger.h"
#include<alsa/asoundlib.h>
#include<stdio.h>
#include<time.h>
#include <sys/select.h>
#include <stdio.h>
#include <time.h>
#include <stdexcept>
#include<string>
#include <string>
#include <boost/throw_exception.hpp>
#include <alsa/asoundlib.h>
#include <sys/select.h>
#include "anbox/logger.h"
#include "anbox/audio/client_info.h"
namespace anbox{
......@@ -37,18 +37,13 @@ AudioSource::AudioSource()
is_opened = false;
}
AudioSource::~AudioSource()
{
DEBUG("audio source disconstructor is called");
}
bool AudioSource::connect_audio()
{
clock_t start = clock();
int res;
int res = 0;
std::string audioName = malsahelper.get_usb_audio_device_name();
if (audioName == "default") {
return false;
return false;
}
res = malsahelper.open_pcm_device(audioName.c_str(), SND_PCM_STREAM_CAPTURE, 0);
if (res == 0) {
......@@ -88,8 +83,10 @@ void AudioSource::set_socket_connection(std::shared_ptr<network::SocketConnectio
void AudioSource::process_pcm_data()
{
std::string fail = "fail";
if (!is_opened) {
ERROR("pcm devices isn't opened");
mConnection->send(fail.c_str(), fail.size());
return;
}
snd_pcm_uframes_t period_frames = malsahelper.get_period_frames();
......@@ -97,11 +94,11 @@ void AudioSource::process_pcm_data()
char* dataBuf = new (std::nothrow) char [period_frames_bytes];
if (dataBuf == nullptr) {
ERROR("get memory fail !!!");
mConnection->send(fail.c_str(), fail.size());
return;
}
memset(dataBuf, 0, period_frames_bytes);
const char* tmp = nullptr;
std::string fail = "fail";
if ((malsahelper.pcm_read(dataBuf, period_frames)) != period_frames) {
mConnection->send(fail.c_str(), fail.size());
ERROR("read pcm data failed !!!");
......@@ -117,11 +114,11 @@ void AudioSource::process_pcm_data()
/**
* response recording request from android audio hal
*/
void AudioSource::read_data (const std::vector<std::uint8_t> &data)
void AudioSource::read_data(const std::vector<std::uint8_t> &data)
{
DEBUG("recording ,pid = %d,tid = %d", pid, tid);
enum audio::RecordCommand cmd = static_cast<enum audio::RecordCommand>(data[0]); // parse request type from hal
std::unique_lock<std::mutex> l(lock_);
DEBUG("recording ,pid = %d,tid = %d", pid, tid);
int res = 0;
std::string str;
switch (cmd) {
......@@ -140,7 +137,7 @@ void AudioSource::read_data (const std::vector<std::uint8_t> &data)
}
}
void AudioSource::sleep_ms (unsigned int secs) const
void AudioSource::sleep_ms(unsigned int secs) const
{
const long secs2usec = 1000000;
struct timeval tval;
......@@ -148,7 +145,6 @@ void AudioSource::sleep_ms (unsigned int secs) const
tval.tv_usec = (secs * CLOCKS_PER_SEC % secs2usec);
select(0, NULL, NULL, NULL, &tval);
}
} // alsa
} // platform
} // anbox
}
}
}
......@@ -33,7 +33,7 @@ namespace sdl {
class AudioSource:public audio::Source {
public:
AudioSource();
~AudioSource();
~AudioSource() {}
void read_data(const std::vector<std::uint8_t> &data) override;
void set_socket_connection(std::shared_ptr<network::SocketConnection> const& connection) override;
bool connect_audio() override;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册