Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openeuler
anbox
提交
5bb29138
A
anbox
项目概览
openeuler
/
anbox
通知
24
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
anbox
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
5bb29138
编写于
7月 26, 2020
作者:
T
twwang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify some bad coding style
modify some bad coding style
上级
ad842aa0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
59 addition
and
69 deletion
+59
-69
src/anbox/audio/alsa_helper.cpp
src/anbox/audio/alsa_helper.cpp
+41
-47
src/anbox/platform/alsa/audio_source.cpp
src/anbox/platform/alsa/audio_source.cpp
+17
-21
src/anbox/platform/alsa/audio_source.h
src/anbox/platform/alsa/audio_source.h
+1
-1
未找到文件。
src/anbox/audio/alsa_helper.cpp
浏览文件 @
5bb29138
...
...
@@ -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
}
}
src/anbox/platform/alsa/audio_source.cpp
浏览文件 @
5bb29138
...
...
@@ -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
}
}
}
src/anbox/platform/alsa/audio_source.h
浏览文件 @
5bb29138
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录