整理代码
This commit is contained in:
commit
c7b4e178ec
364 changed files with 59691 additions and 0 deletions
144
video/alsa/PCM.cpp
Normal file
144
video/alsa/PCM.cpp
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
/*
|
||||
*******************************************************************************
|
||||
* INCLUDE
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include "PCM.h"
|
||||
#include <alsa/asoundlib.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace Video
|
||||
{
|
||||
namespace ALSA
|
||||
{
|
||||
/*
|
||||
*******************************************************************************
|
||||
* CLASS DEFINE
|
||||
*******************************************************************************
|
||||
*/
|
||||
PCM::PCM()
|
||||
{
|
||||
}
|
||||
|
||||
PCM::~PCM()
|
||||
{
|
||||
}
|
||||
|
||||
int PCM::open(SoundCard::Ptr pSoundCard, snd_pcm_stream_t stream)
|
||||
{
|
||||
_stream = stream;
|
||||
return snd_pcm_open((snd_pcm_t**)&_pPCM, pSoundCard->info().device.c_str(), stream, 0);
|
||||
}
|
||||
|
||||
int PCM::close()
|
||||
{
|
||||
snd_pcm_drain((snd_pcm_t*)_pPCM);
|
||||
snd_pcm_close((snd_pcm_t*)_pPCM);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PCM::config(snd_pcm_access_t access, snd_pcm_format_t format, int channels, int rate, int framesz, int samplesz)
|
||||
{
|
||||
#define __check_error(x) \
|
||||
rc = x; \
|
||||
if (rc < 0) \
|
||||
{ \
|
||||
std::cout << "Error: " << snd_strerror(rc) << "@" << #x << std::endl; \
|
||||
goto out; \
|
||||
}
|
||||
|
||||
snd_pcm_hw_params_t* hwParams = nullptr;
|
||||
snd_pcm_sw_params_t* swParams = nullptr;
|
||||
int rc;
|
||||
|
||||
__check_error(snd_pcm_hw_params_malloc(&hwParams));
|
||||
|
||||
__check_error(snd_pcm_hw_params_any((snd_pcm_t*)_pPCM, hwParams));
|
||||
|
||||
__check_error(snd_pcm_hw_params_set_access((snd_pcm_t*)_pPCM, hwParams, access));
|
||||
|
||||
__check_error(snd_pcm_hw_params_set_format((snd_pcm_t*)_pPCM, hwParams, format));
|
||||
|
||||
__check_error(snd_pcm_hw_params_set_channels((snd_pcm_t*)_pPCM, hwParams, channels));
|
||||
|
||||
__check_error(snd_pcm_hw_params_set_rate((snd_pcm_t*)_pPCM, hwParams, rate, 0));
|
||||
|
||||
__check_error(snd_pcm_hw_params_set_period_size((snd_pcm_t*)_pPCM, hwParams, framesz, 0));
|
||||
|
||||
__check_error(snd_pcm_hw_params_set_periods((snd_pcm_t*)_pPCM, hwParams, samplesz, 0));
|
||||
|
||||
__check_error(snd_pcm_hw_params_set_buffer_size((snd_pcm_t*)_pPCM, hwParams, samplesz * framesz));
|
||||
|
||||
__check_error(snd_pcm_hw_params((snd_pcm_t*)_pPCM, hwParams));
|
||||
|
||||
if (_stream == SND_PCM_STREAM_PLAYBACK)
|
||||
{
|
||||
__check_error(snd_pcm_sw_params_malloc(&swParams));
|
||||
__check_error(snd_pcm_sw_params_current((snd_pcm_t*)_pPCM, swParams));
|
||||
__check_error(snd_pcm_sw_params_set_avail_min((snd_pcm_t*)_pPCM, swParams, framesz));
|
||||
|
||||
__check_error(snd_pcm_sw_params_set_start_threshold((snd_pcm_t*)_pPCM, swParams, framesz * samplesz));
|
||||
__check_error(snd_pcm_sw_params((snd_pcm_t*)_pPCM, swParams));
|
||||
}
|
||||
|
||||
out:
|
||||
if (hwParams)
|
||||
snd_pcm_hw_params_free(hwParams);
|
||||
|
||||
if (swParams)
|
||||
snd_pcm_sw_params_free(swParams);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int PCM::write(const void* data, uint32_t framesz)
|
||||
{
|
||||
int rc = snd_pcm_writei((snd_pcm_t*)_pPCM, data, framesz);
|
||||
if (rc < 0)
|
||||
{
|
||||
if (rc == -EPIPE)
|
||||
rc = snd_pcm_prepare((snd_pcm_t*)_pPCM);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int PCM::write(void** datas, uint32_t framesz)
|
||||
{
|
||||
int rc = snd_pcm_writen((snd_pcm_t*)_pPCM, datas, framesz);
|
||||
if (rc < 0)
|
||||
{
|
||||
if (rc == -EPIPE)
|
||||
rc = snd_pcm_prepare((snd_pcm_t*)_pPCM);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int PCM::read(void* buffer, uint32_t framesz)
|
||||
{
|
||||
int rc = snd_pcm_readi((snd_pcm_t*)_pPCM, buffer, (snd_pcm_uframes_t)framesz);
|
||||
if (rc < 0)
|
||||
{
|
||||
std::cout << "ALSA error: " << rc << " -> " << snd_strerror(rc) << "@" << framesz << std::endl;
|
||||
if (rc == -EPIPE)
|
||||
rc = snd_pcm_prepare((snd_pcm_t*)_pPCM);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int PCM::read(void** buffers, uint32_t framesz)
|
||||
{
|
||||
int rc = snd_pcm_readn((snd_pcm_t*)_pPCM, buffers, (snd_pcm_uframes_t)framesz);
|
||||
if (rc < 0)
|
||||
{
|
||||
std::cout << "ALSA error: " << rc << " -> " << snd_strerror(rc) << "@" << framesz << std::endl;
|
||||
if (rc == -EPIPE)
|
||||
rc = snd_pcm_prepare((snd_pcm_t*)_pPCM);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue