This commit is contained in:
badaix 2015-04-07 20:33:54 +02:00
parent 80eb832eac
commit 52b0e92a5c

View file

@ -44,7 +44,7 @@ void Player::initAlsa()
/* Open the PCM device in playback mode */
if ((pcm = snd_pcm_open(&handle_, pcmDevice_.name.c_str(), SND_PCM_STREAM_PLAYBACK, 0)) < 0)
throw new SnapException("Can't open " + pcmDevice_.name + " PCM device. " + snd_strerror(pcm));
throw SnapException("Can't open " + pcmDevice_.name + " PCM device. " + snd_strerror(pcm));
/* struct snd_pcm_playback_info_t pinfo;
if ( (pcm = snd_pcm_playback_info( pcm_handle, &pinfo )) < 0 )
@ -58,16 +58,16 @@ void Player::initAlsa()
/* Set parameters */
if ((pcm = snd_pcm_hw_params_set_access(handle_, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
throw new SnapException("Can't set interleaved mode. " + string(snd_strerror(pcm)));
throw SnapException("Can't set interleaved mode. " + string(snd_strerror(pcm)));
if ((pcm = snd_pcm_hw_params_set_format(handle_, params, SND_PCM_FORMAT_S16_LE)) < 0)
throw new SnapException("Can't set format. " + string(snd_strerror(pcm)));
throw SnapException("Can't set format. " + string(snd_strerror(pcm)));
if ((pcm = snd_pcm_hw_params_set_channels(handle_, params, channels)) < 0)
throw new SnapException("Can't set channels number. " + string(snd_strerror(pcm)));
throw SnapException("Can't set channels number. " + string(snd_strerror(pcm)));
if ((pcm = snd_pcm_hw_params_set_rate_near(handle_, params, &rate, 0)) < 0)
throw new SnapException("Can't set rate. " + string(snd_strerror(pcm)));
throw SnapException("Can't set rate. " + string(snd_strerror(pcm)));
unsigned int buffer_time;
snd_pcm_hw_params_get_buffer_time_max(params, &buffer_time, 0);
@ -85,7 +85,7 @@ void Player::initAlsa()
/* Write parameters */
if ((pcm = snd_pcm_hw_params(handle_, params)) < 0)
throw new SnapException("Can't set harware parameters. " + string(snd_strerror(pcm)));
throw SnapException("Can't set harware parameters. " + string(snd_strerror(pcm)));
/* Resume information */
logD << "PCM name: " << snd_pcm_name(handle_) << "\n";
@ -123,6 +123,7 @@ void Player::uninitAlsa()
{
snd_pcm_drain(handle_);
snd_pcm_close(handle_);
handle_ = NULL;
}
if (buff_ != NULL)
@ -165,6 +166,19 @@ void Player::worker()
snd_pcm_sframes_t framesDelay;
while (active_)
{
if (handle_ == NULL)
{
try
{
initAlsa();
}
catch (const std::exception& e)
{
logE << "Exception in initAlsa: " << e.what() << endl;
usleep(100*1000);
}
}
snd_pcm_avail_delay(handle_, &framesAvail, &framesDelay);
chronos::usec delay((chronos::usec::rep) (1000 * (double) framesDelay / stream_->getFormat().msRate()));
logD << "Avail: " << framesAvail << ", delay: " << framesDelay << ", delay[ms]: " << delay.count() / 1000 << "\n";
@ -180,7 +194,6 @@ void Player::worker()
{
logE << "ERROR. Can't write to PCM device: " << snd_strerror(pcm) << "\n";
uninitAlsa();
initAlsa();
}
}
else