git-svn-id: svn://elaine/murooma/trunk@321 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
(no author) 2014-12-24 13:48:23 +00:00
parent 56f8c9927e
commit f3a2486560

View file

@ -6,51 +6,54 @@
using namespace std;
Player::Player(const PcmDevice& pcmDevice, Stream* stream) : active_(false), stream_(stream), pcmDevice_(pcmDevice)
{
Player::Player(const PcmDevice& pcmDevice, Stream* stream) :
active_(false), stream_(stream), pcmDevice_(pcmDevice) {
}
void Player::start()
{
void Player::start() {
unsigned int pcm, tmp, rate;
int channels;
snd_pcm_hw_params_t *params;
int buff_size;
rate = stream_->format.rate;
rate = stream_->format.rate;
channels = stream_->format.channels;
/* Open the PCM device in playback mode */
if ((pcm = snd_pcm_open(&pcm_handle, pcmDevice_.name.c_str(), SND_PCM_STREAM_PLAYBACK, 0)) < 0)
cout << "ERROR: Can't open " << pcmDevice_.name << " PCM device. " << snd_strerror(pcm) << "\n";
if ((pcm = snd_pcm_open(&pcm_handle, pcmDevice_.name.c_str(),
SND_PCM_STREAM_PLAYBACK, 0)) < 0)
cout << "ERROR: Can't open " << pcmDevice_.name << " PCM device. "
<< snd_strerror(pcm) << "\n";
/* struct snd_pcm_playback_info_t pinfo;
if ( (pcm = snd_pcm_playback_info( pcm_handle, &pinfo )) < 0 )
fprintf( stderr, "Error: playback info error: %s\n", snd_strerror( err ) );
printf("buffer: '%d'\n", pinfo.buffer_size);
*/
if ( (pcm = snd_pcm_playback_info( pcm_handle, &pinfo )) < 0 )
fprintf( stderr, "Error: playback info error: %s\n", snd_strerror( err ) );
printf("buffer: '%d'\n", pinfo.buffer_size);
*/
/* Allocate parameters object and fill it with default values*/
snd_pcm_hw_params_alloca(&params);
snd_pcm_hw_params_any(pcm_handle, params);
/* Set parameters */
if ((pcm = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
cout << "ERROR: Can't set interleaved mode. " << snd_strerror(pcm) << "\n";
if ((pcm = snd_pcm_hw_params_set_access(pcm_handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
cout << "ERROR: Can't set interleaved mode. " << snd_strerror(pcm)
<< "\n";
if ((pcm = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_S16_LE)) < 0)
if ((pcm = snd_pcm_hw_params_set_format(pcm_handle, params,
SND_PCM_FORMAT_S16_LE)) < 0)
cout << "ERROR: Can't set format. " << snd_strerror(pcm) << "\n";
if ((pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels)) < 0)
cout << "ERROR: Can't set channels number. " << snd_strerror(pcm) << "\n";
if ((pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels))
< 0)
cout << "ERROR: Can't set channels number. " << snd_strerror(pcm)
<< "\n";
if ((pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0)) < 0)
if ((pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0))
< 0)
cout << "ERROR: Can't set rate. " << snd_strerror(pcm) << "\n";
unsigned int buffer_time;
snd_pcm_hw_params_get_buffer_time_max(params, &buffer_time, 0);
if (buffer_time > BUFFER_TIME)
@ -67,12 +70,13 @@ void Player::start()
/* Write parameters */
if ((pcm = snd_pcm_hw_params(pcm_handle, params)) < 0)
cout << "ERROR: Can't set harware parameters. " << snd_strerror(pcm) << "\n";
cout << "ERROR: Can't set harware parameters. " << snd_strerror(pcm)
<< "\n";
/* Resume information */
cout << "PCM name: " << snd_pcm_name(pcm_handle) << "\n";
cout << "PCM state: " << snd_pcm_state_name(snd_pcm_state(pcm_handle)) << "\n";
cout << "PCM state: " << snd_pcm_state_name(snd_pcm_state(pcm_handle))
<< "\n";
snd_pcm_hw_params_get_channels(params, &tmp);
cout << "channels: " << tmp << "\n";
@ -94,8 +98,6 @@ void Player::start()
snd_pcm_hw_params_get_period_time(params, &tmp, NULL);
cout << "period time: " << tmp << "\n";
snd_pcm_sw_params_t *swparams;
snd_pcm_sw_params_alloca(&swparams);
snd_pcm_sw_params_current(pcm_handle, swparams);
@ -105,40 +107,33 @@ void Player::start()
// snd_pcm_sw_params_set_stop_threshold(pcm_handle, swparams, frames);
snd_pcm_sw_params(pcm_handle, swparams);
playerThread = new thread(&Player::worker, this);
}
void Player::stop()
{
void Player::stop() {
active_ = false;
playerThread->join();
delete playerThread;
}
void Player::worker()
{
void Player::worker() {
unsigned int pcm;
snd_pcm_sframes_t framesAvail;
snd_pcm_sframes_t framesDelay;
active_ = true;
while (active_)
{
while (active_) {
snd_pcm_avail_delay(pcm_handle, &framesAvail, &framesDelay);
chronos::usec delay((chronos::usec::rep)(1000 * (double)framesDelay / stream_->format.msRate()));
if (stream_->getPlayerChunk(buff, delay, frames))
{
if ((pcm = snd_pcm_writei(pcm_handle, buff, frames)) == -EPIPE)
{
chronos::usec delay(
(chronos::usec::rep) (1000 * (double) framesDelay
/ stream_->format.msRate()));
if (stream_->getPlayerChunk(buff, delay, frames)) {
if ((pcm = snd_pcm_writei(pcm_handle, buff, frames)) == -EPIPE) {
printf("XRUN.\n");
snd_pcm_prepare(pcm_handle);
}
else if (pcm < 0)
{
printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
} else if (pcm < 0) {
printf("ERROR. Can't write to PCM device. %s\n",
snd_strerror(pcm));
}
}
}
@ -148,10 +143,7 @@ void Player::worker()
free(buff);
}
vector<PcmDevice> Player::pcm_list(void)
{
vector<PcmDevice> Player::pcm_list(void) {
void **hints, **n;
char *name, *descr, *io;
vector<PcmDevice> result;
@ -174,22 +166,21 @@ vector<PcmDevice> Player::pcm_list(void)
// printf("%s\n", name);
//cout << "Name: " << name << "\n";
//cout << "Desc: " << descr << "\n";
/*
if ((descr1 = descr) != NULL) {
printf(" ");
while (*descr1) {
if (*descr1 == '\n')
printf("\n ");
else
putchar(*descr1);
descr1++;
}
putchar('\n');
}
*/
__end:
if (name != NULL)
free(name);
/*
if ((descr1 = descr) != NULL) {
printf(" ");
while (*descr1) {
if (*descr1 == '\n')
printf("\n ");
else
putchar(*descr1);
descr1++;
}
putchar('\n');
}
*/
__end: if (name != NULL)
free(name);
if (descr != NULL)
free(descr);
if (io != NULL)
@ -200,4 +191,3 @@ vector<PcmDevice> Player::pcm_list(void)
return result;
}