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,14 +6,11 @@
using namespace std; using namespace std;
Player::Player(const PcmDevice& pcmDevice, Stream* stream) :
Player::Player(const PcmDevice& pcmDevice, Stream* stream) : active_(false), stream_(stream), pcmDevice_(pcmDevice) active_(false), stream_(stream), pcmDevice_(pcmDevice) {
{
} }
void Player::start() {
void Player::start()
{
unsigned int pcm, tmp, rate; unsigned int pcm, tmp, rate;
int channels; int channels;
snd_pcm_hw_params_t *params; snd_pcm_hw_params_t *params;
@ -22,10 +19,11 @@ void Player::start()
rate = stream_->format.rate; rate = stream_->format.rate;
channels = stream_->format.channels; channels = stream_->format.channels;
/* Open the PCM device in playback mode */ /* Open the PCM device in playback mode */
if ((pcm = snd_pcm_open(&pcm_handle, pcmDevice_.name.c_str(), SND_PCM_STREAM_PLAYBACK, 0)) < 0) if ((pcm = snd_pcm_open(&pcm_handle, pcmDevice_.name.c_str(),
cout << "ERROR: Can't open " << pcmDevice_.name << " PCM device. " << snd_strerror(pcm) << "\n"; 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; /* struct snd_pcm_playback_info_t pinfo;
if ( (pcm = snd_pcm_playback_info( pcm_handle, &pinfo )) < 0 ) if ( (pcm = snd_pcm_playback_info( pcm_handle, &pinfo )) < 0 )
@ -38,19 +36,24 @@ void Player::start()
snd_pcm_hw_params_any(pcm_handle, params); snd_pcm_hw_params_any(pcm_handle, params);
/* Set parameters */ /* Set parameters */
if ((pcm = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) if ((pcm = snd_pcm_hw_params_set_access(pcm_handle, params,
cout << "ERROR: Can't set interleaved mode. " << snd_strerror(pcm) << "\n"; 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"; cout << "ERROR: Can't set format. " << snd_strerror(pcm) << "\n";
if ((pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels)) < 0) if ((pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels))
cout << "ERROR: Can't set channels number. " << snd_strerror(pcm) << "\n"; < 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"; cout << "ERROR: Can't set rate. " << snd_strerror(pcm) << "\n";
unsigned int buffer_time; unsigned int buffer_time;
snd_pcm_hw_params_get_buffer_time_max(params, &buffer_time, 0); snd_pcm_hw_params_get_buffer_time_max(params, &buffer_time, 0);
if (buffer_time > BUFFER_TIME) if (buffer_time > BUFFER_TIME)
@ -67,12 +70,13 @@ void Player::start()
/* Write parameters */ /* Write parameters */
if ((pcm = snd_pcm_hw_params(pcm_handle, params)) < 0) 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 */ /* Resume information */
cout << "PCM name: " << snd_pcm_name(pcm_handle) << "\n"; 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); snd_pcm_hw_params_get_channels(params, &tmp);
cout << "channels: " << tmp << "\n"; cout << "channels: " << tmp << "\n";
@ -94,8 +98,6 @@ void Player::start()
snd_pcm_hw_params_get_period_time(params, &tmp, NULL); snd_pcm_hw_params_get_period_time(params, &tmp, NULL);
cout << "period time: " << tmp << "\n"; cout << "period time: " << tmp << "\n";
snd_pcm_sw_params_t *swparams; snd_pcm_sw_params_t *swparams;
snd_pcm_sw_params_alloca(&swparams); snd_pcm_sw_params_alloca(&swparams);
snd_pcm_sw_params_current(pcm_handle, 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_set_stop_threshold(pcm_handle, swparams, frames);
snd_pcm_sw_params(pcm_handle, swparams); snd_pcm_sw_params(pcm_handle, swparams);
playerThread = new thread(&Player::worker, this); playerThread = new thread(&Player::worker, this);
} }
void Player::stop() {
void Player::stop()
{
active_ = false; active_ = false;
playerThread->join(); playerThread->join();
delete playerThread; delete playerThread;
} }
void Player::worker() {
void Player::worker()
{
unsigned int pcm; unsigned int pcm;
snd_pcm_sframes_t framesAvail; snd_pcm_sframes_t framesAvail;
snd_pcm_sframes_t framesDelay; snd_pcm_sframes_t framesDelay;
active_ = true; active_ = true;
while (active_) while (active_) {
{
snd_pcm_avail_delay(pcm_handle, &framesAvail, &framesDelay); snd_pcm_avail_delay(pcm_handle, &framesAvail, &framesDelay);
chronos::usec delay((chronos::usec::rep)(1000 * (double)framesDelay / stream_->format.msRate())); chronos::usec delay(
if (stream_->getPlayerChunk(buff, delay, frames)) (chronos::usec::rep) (1000 * (double) framesDelay
{ / stream_->format.msRate()));
if ((pcm = snd_pcm_writei(pcm_handle, buff, frames)) == -EPIPE) if (stream_->getPlayerChunk(buff, delay, frames)) {
{ if ((pcm = snd_pcm_writei(pcm_handle, buff, frames)) == -EPIPE) {
printf("XRUN.\n"); printf("XRUN.\n");
snd_pcm_prepare(pcm_handle); snd_pcm_prepare(pcm_handle);
} } else if (pcm < 0) {
else if (pcm < 0) printf("ERROR. Can't write to PCM device. %s\n",
{ snd_strerror(pcm));
printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
} }
} }
} }
@ -148,10 +143,7 @@ void Player::worker()
free(buff); free(buff);
} }
vector<PcmDevice> Player::pcm_list(void) {
vector<PcmDevice> Player::pcm_list(void)
{
void **hints, **n; void **hints, **n;
char *name, *descr, *io; char *name, *descr, *io;
vector<PcmDevice> result; vector<PcmDevice> result;
@ -187,8 +179,7 @@ vector<PcmDevice> Player::pcm_list(void)
putchar('\n'); putchar('\n');
} }
*/ */
__end: __end: if (name != NULL)
if (name != NULL)
free(name); free(name);
if (descr != NULL) if (descr != NULL)
free(descr); free(descr);
@ -200,4 +191,3 @@ vector<PcmDevice> Player::pcm_list(void)
return result; return result;
} }