improved alsa error handling

This commit is contained in:
badaix 2015-04-07 19:44:57 +02:00
parent 74cdd5ea64
commit 80eb832eac
4 changed files with 212 additions and 68 deletions

169
bug.txt
View file

@ -25,12 +25,15 @@
2015-02-11 06-53-30 [out] sleep > chunk_->getDuration(): 342585 > 92, chunks: 12, out: 100000, needed: 25034013
2015-02-11 06-53-30 [out] sleep > chunk_->getDuration(): 250190 > 92, chunks: 11, out: 100000, needed: 25034013
2015-02-11 06-53-30 [out] sleep > chunk_->getDuration(): 157787 > 92, chunks: 10, out: 100000, needed: 25034013
2015-02-11 06-53-30 [out] correction: 4, factor: 1.00362
2015-02-11 06-53-30 [out] Sleep 65289, age: -27457, bufferDuration: 25034
2015-02-11 06-53-30 [out] Sleep 65289, age: -27.457, bufferDuration: 25034
2015-02-11 06-53-30 [out] Chunk: -274 -274 -274 -274 1 100000
2015-02-11 06-53-40 [out] correction: 4, factor: 1.00362
2015-02-11 06-53-40 [out] Sleep 65189, age: 9939282, bufferDuration: 25034
2015-02-11 06-53-40 [out] Sleep 65189, age: 9.939.282, bufferDuration: 25034
2015-02-11 06-53-40 [out] Chunk: 99392 99392 99392 99392 1 100000
2015-02-11 06-53-50 [out] correction: 4, factor: 1.00362
2015-02-11 06-53-50 [out] Sleep 65089, age: 19914052, bufferDuration: 25034
2015-02-11 06-53-50 [out] Chunk: 199140 199140 199140 199140 1 100000
@ -98,3 +101,165 @@
2015-03-18 06-07-45 [out] sleep > chunkDuration: 1592 > 92, chunks: 25, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 1499 > 92, chunks: 24, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 1407 > 92, chunks: 23, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 1315 > 92, chunks: 22, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 1222 > 92, chunks: 21, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 1130 > 92, chunks: 20, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 1037 > 92, chunks: 19, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 945 > 92, chunks: 18, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 853 > 92, chunks: 17, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 760 > 92, chunks: 16, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 668 > 92, chunks: 15, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 576 > 92, chunks: 14, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 483 > 92, chunks: 13, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 391 > 92, chunks: 12, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 298 > 92, chunks: 11, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 206 > 92, chunks: 10, out: 100, needed: 25
2015-03-18 06-07-45 [out] sleep > chunkDuration: 113 > 92, chunks: 9, out: 100, needed: 25
2015-03-18 06-07-45 [out] correction: 4, factor: 1.00362
2015-03-18 06-07-45 [out] Sleep 21, age: -71, bufferDuration: 25
2015-03-18 06-07-45 [out] Chunk: -712 -712 -712 -712 1 100
2015-03-18 06-07-55 [out] sleep > bufferDuration/2: 21 > 12
2015-03-18 06-07-55 [out] correction: 4, factor: 1.00362
2015-03-18 06-07-55 [out] Sleep 21, age: 9899, bufferDuration: 25
2015-03-18 06-07-55 [out] Chunk: 98996 98996 98996 98996 1 100
2015-03-18 06-08-05 [out] sleep > bufferDuration/2: 21 > 12
2015-03-18 06-08-05 [out] correction: 4, factor: 1.00362
2015-03-18 06-08-05 [out] Sleep 21, age: 19873, bufferDuration: 25
2015-03-18 06-08-05 [out] Chunk: 198738 198738 198738 198738 1 100
2015-03-18 06-08-15 [out] sleep > bufferDuration/2: 21 > 12
2015-03-18 06-08-15 [out] correction: 4, factor: 1.00362
2015-03-18 06-08-15 [out] Sleep 21, age: 29849, bufferDuration: 25
2015-03-18 06-08-15 [out] Chunk: 298499 298499 298499 298499 1 100
2015-03-18 06-08-25 [out] sleep > bufferDuration/2: 21 > 12
2015-03-18 06-08-25 [out] correction: 4, factor: 1.00362
2015-03-18 06-08-25 [out] Sleep 21, age: 19205, bufferDuration: 25
2015-03-18 06-08-25 [out] Chunk: 192055 192055 192055 192055 1 100
2015-03-20 06-28-34 [out] correction: 4, factor: 1.00362
2015-03-20 06-28-34 [out] Sleep 5, age: 29230, bufferDuration: 25
2015-03-20 06-28-34 [out] Chunk: 292302 292302 292302 292302 1 100
2015-03-20 06-28-44 [out] correction: 4, factor: 1.00362
2015-03-20 06-28-44 [out] Sleep 5, age: 39205, bufferDuration: 25
2015-03-20 06-28-44 [out] Chunk: 392051 392051 392051 392051 1 100
2015-03-20 06-28-54 [out] correction: 4, factor: 1.00362
2015-03-20 06-28-54 [out] Sleep 5, age: 49179, bufferDuration: 25
2015-03-20 06-28-54 [out] Chunk: 491799 491799 491799 491799 1 100
2015-03-20 06-29-04 [out] correction: 4, factor: 1.00362
2015-03-20 06-29-04 [out] Sleep 5, age: 19216, bufferDuration: 25
2015-03-20 06-29-04 [out] Chunk: 192169 192169 192169 192169 1 100
2015-03-20 06-29-14 [out] correction: 4, factor: 1.00362
2015-03-20 06-29-14 [out] Sleep 5, age: 29192, bufferDuration: 25
2015-03-20 06-29-14 [out] Chunk: 291920 291920 291920 291920 1 100
2015-03-20 06-29-24 [out] correction: 4, factor: 1.00362
2015-03-20 06-29-24 [out] Sleep 5, age: 39167, bufferDuration: 25
2015-03-20 06-29-24 [out] Chunk: 391670 391670 391670 391670 1 100
2015-03-20 06-29-34 [out] correction: 4, factor: 1.00362
2015-03-20 06-29-34 [out] Sleep 5, age: 49141, bufferDuration: 25
2015-03-20 06-29-34 [out] Chunk: 491414 491414 491414 491414 1 100
2015-03-20 06-29-44 [out] correction: 4, factor: 1.00362
2015-03-20 06-29-44 [out] Sleep 5, age: 19178, bufferDuration: 25
2015-03-20 06-29-44 [out] Chunk: 191789 191789 191789 191789 1 100
2015-03-31 05-48-22 [out] Chunk: 391708 391708 391708 391708 1 100
36.122015-03-31 05-48-32 [out] correction: 4, factor: 1.00362
2015-03-31 05-48-32 [out] Sleep 1, age: 49145, bufferDuration: 25
2015-03-31 05-48-32 [out] Chunk: 491455 491455 491455 491455 1 100
36.122015-03-31 05-48-42 [out] correction: 4, factor: 1.00362
2015-03-31 05-48-42 [out] Sleep 1, age: 19182, bufferDuration: 25
2015-03-31 05-48-42 [out] Chunk: 191827 191827 191827 191827 1 100
36.122015-03-31 05-48-52 [out] correction: 4, factor: 1.00362
2015-03-31 05-48-52 [out] Sleep 1, age: 29157, bufferDuration: 25
2015-03-31 05-48-52 [out] Chunk: 291573 291573 291573 291573 1 100
36.122015-03-31 05-49-02 [out] correction: 4, factor: 1.00362
2015-03-31 05-49-02 [out] Sleep 0, age: 39135, bufferDuration: 25
2015-03-31 05-49-02 [out] Chunk: 391354 391354 391354 391354 1 100
3
2015-04-05 10-41-35 [out] Chunk: 192776 192776 192776 192776 1 100
36(4294967291).122015-04-05 10-41-45 [out] correction: 4, factor: 1.00362
2015-04-05 10-41-45 [out] Sleep 1, age: 29252, bufferDuration: 25
2015-04-05 10-41-45 [out] Chunk: 292525 292525 292525 292525 1 100
36(4294967291).122015-04-05 10-41-55 [out] correction: 4, factor: 1.00362
2015-04-05 10-41-55 [out] Sleep 1, age: 39227, bufferDuration: 25
2015-04-05 10-41-55 [out] Chunk: 392274 392274 392274 392274 1 100
36(4294967291).122015-04-05 10-42-05 [out] correction: 4, factor: 1.00362
2015-04-05 10-42-05 [out] Sleep 1, age: 19206, bufferDuration: 25
2015-04-05 10-42-05 [out] Chunk: 192066 192066 192066 192066 1 100
36(4294967291).122015-04-05 10-42-15 [out] correction: 4, factor: 1.00362
2015-04-05 10-42-15 [out] Sleep 1, age: 29184, bufferDuration: 25
2015-04-05 10-42-15 [out] Chunk: 291847 291847 291847 291847 1 100
36(4294967291).122015-04-05 10-42-25 [out] correction: 4, factor: 1.00362
2015-04-05 10-42-25 [out] Sleep 1, age: 39169, bufferDuration: 25
2015-04-05 10-42-25 [out] Chunk: 391699 391699 391699 391699 1 100
36(4294967291).122015-04-05 10-42-35 [out] correction: 4, factor: 1.00362
2015-04-05 10-42-35 [out] Sleep 1, age: 49156, bufferDuration: 25
2015-04-05 10-42-35 [out] Chunk: 491562 491562 491562 491562 1 100
3
2015-04-05 10-44-55 [out] sleep > chunkDuration: 932 > 92, chunks: 18, out: 100, needed: 25
2015-04-05 10-44-55 [out] sleep > chunkDuration: 839 > 92, chunks: 17, out: 100, needed: 25
2015-04-05 10-44-55 [out] sleep > chunkDuration: 747 > 92, chunks: 16, out: 100, needed: 25
2015-04-05 10-44-55 [out] sleep > chunkDuration: 654 > 92, chunks: 15, out: 100, needed: 25
2015-04-05 10-44-55 [out] sleep > chunkDuration: 561 > 92, chunks: 14, out: 100, needed: 25
2015-04-05 10-44-55 [out] sleep > chunkDuration: 469 > 92, chunks: 13, out: 100, needed: 25
2015-04-05 10-44-55 [out] sleep > chunkDuration: 376 > 92, chunks: 12, out: 100, needed: 25
2015-04-05 10-44-55 [out] sleep > chunkDuration: 283 > 92, chunks: 11, out: 100, needed: 25
2015-04-05 10-44-55 [out] sleep > chunkDuration: 191 > 92, chunks: 10, out: 100, needed: 25
2015-04-05 10-44-55 [out] sleep > chunkDuration: 98 > 92, chunks: 9, out: 100, needed: 25
2015-04-05 10-44-55 [out] correction: 4, factor: 1.00362
2015-04-05 10-44-55 [out] Sleep 5, age: -86, bufferDuration: 25
2015-04-05 10-44-55 [out] Chunk: -869 -869 -869 -869 1 100
36(4294967291).122015-04-05 10-45-05 [out] correction: 4, factor: 1.00362
2015-04-05 10-45-05 [out] Sleep 5, age: 9907, bufferDuration: 25
2015-04-05 10-45-05 [out] Chunk: 99070 99070 99070 99070 1 100
36(4294967291).122015-04-05 10-45-15 [out] correction: 4, factor: 1.00362
2015-04-05 10-45-15 [out] Sleep 5, age: 19891, bufferDuration: 25
2015-04-05 10-45-15 [out] Chunk: 198919 198919 198919 198919 1 100
36(4294967291).122015-04-05 10-45-25 [out] correction: 4, factor: 1.00362
2015-04-05 10-45-25 [out] Sleep 5, age: 29876, bufferDuration: 25
2015-04-05 10-45-25 [out] Chunk: 298767 298767 298767 298767 1 100
36(4294967291).122015-04-05 10-45-35 [out] correction: 4, factor: 1.00362
2015-04-05 10-45-35 [out] Sleep 5, age: 19150, bufferDuration: 25
2015-04-05 10-45-35 [out] Chunk: 191500 191500 191500 191500 1 100
36(4294967291).122015-04-05 10-45-45 [out] correction: 4, factor: 1.00362
2015-04-05 10-45-45 [out] Sleep 5, age: 29134, bufferDuration: 25
2015-04-05 10-45-45 [out] Chunk: 291345 291345 291345 291345 1 100
36(4294967291).122015-04-05 10-45-55 [out] correction: 4, factor: 1.00362
2015-04-05 10-45-55 [out] Sleep 5, age: 39119, bufferDuration: 25
2015-04-05 10-45-55 [out] Chunk: 391194 391194 391194 391194 1 100
36(4294967291).122015-04-05 10-46-05 [out] correction: 4, factor: 1.00362
2015-04-05 10-46-05 [out] Sleep 5, age: 49104, bufferDuration: 25
2015-04-05 10-46-05 [out] Chunk: 491044 491044 491044 491044 1 100
36(4294967291).122015-04-05 10-46-15 [out] correction: 4, factor: 1.00362
2015-04-05 10-46-15 [out] Sleep 4, age: 19151, bufferDuration: 25
2015-04-05 10-46-15 [out] Chunk: 191513 191513 191513 191513 1 100
36(4294967291).122015-04-05 10-46-25 [out] correction: 4, factor: 1.00362
2015-04-05 10-46-25 [out] Sleep 4, age: 29136, bufferDuration: 25
2015-04-05 10-46-25 [out] Chunk: 291369 291369 291369 291369 1 100
3

View file

@ -18,6 +18,7 @@
#include "alsaPlayer.h"
#include "common/log.h"
#include "common/snapException.h"
#include <alsa/asoundlib.h>
#include <iostream>
@ -25,12 +26,12 @@
using namespace std;
Player::Player(const PcmDevice& pcmDevice, Stream* stream) : buff_(NULL), active_(false), stream_(stream), pcmDevice_(pcmDevice)
Player::Player(const PcmDevice& pcmDevice, Stream* stream) : handle_(NULL), buff_(NULL), active_(false), stream_(stream), pcmDevice_(pcmDevice)
{
}
snd_pcm_t* Player::initAlsa()
void Player::initAlsa()
{
unsigned int tmp, rate;
int pcm, channels;
@ -41,14 +42,10 @@ snd_pcm_t* Player::initAlsa()
rate = format.rate;
channels = format.channels;
snd_pcm_t* pcm_handle(NULL);
/* Open the PCM device in playback mode */
if ((pcm = snd_pcm_open(&pcm_handle, pcmDevice_.name.c_str(), SND_PCM_STREAM_PLAYBACK, 0)) < 0)
{
logE << "ERROR: Can't open " << pcmDevice_.name << " PCM device. " << snd_strerror(pcm) << "\n";
return NULL;
}
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));
/* 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 ) );
@ -57,32 +54,20 @@ snd_pcm_t* Player::initAlsa()
/* Allocate parameters object and fill it with default values*/
snd_pcm_hw_params_alloca(&params);
snd_pcm_hw_params_any(pcm_handle, params);
snd_pcm_hw_params_any(handle_, params);
/* Set parameters */
if ((pcm = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{
logE << "ERROR: Can't set interleaved mode. " << snd_strerror(pcm) << "\n";
return NULL;
}
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)));
if ((pcm = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_S16_LE)) < 0)
{
logE << "ERROR: Can't set format. " << snd_strerror(pcm) << "\n";
return NULL;
}
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)));
if ((pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels)) < 0)
{
logE << "ERROR: Can't set channels number. " << snd_strerror(pcm) << "\n";
return NULL;
}
if ((pcm = snd_pcm_hw_params_set_channels(handle_, params, channels)) < 0)
throw new SnapException("Can't set channels number. " + string(snd_strerror(pcm)));
if ((pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0)) < 0)
{
logE << "ERROR: Can't set rate. " << snd_strerror(pcm) << "\n";
return NULL;
}
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)));
unsigned int buffer_time;
snd_pcm_hw_params_get_buffer_time_max(params, &buffer_time, 0);
@ -91,23 +76,20 @@ snd_pcm_t* Player::initAlsa()
unsigned int period_time = buffer_time / 4;
snd_pcm_hw_params_set_period_time_near(pcm_handle, params, &period_time, 0);
snd_pcm_hw_params_set_buffer_time_near(pcm_handle, params, &buffer_time, 0);
snd_pcm_hw_params_set_period_time_near(handle_, params, &period_time, 0);
snd_pcm_hw_params_set_buffer_time_near(handle_, params, &buffer_time, 0);
// long unsigned int periodsize = stream_->format.msRate() * 50;//2*rate/50;
// if ((pcm = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, params, &periodsize)) < 0)
// logE << "Unable to set buffer size " << (long int)periodsize << ": " << snd_strerror(pcm) << "\n";
/* Write parameters */
if ((pcm = snd_pcm_hw_params(pcm_handle, params)) < 0)
{
logE << "ERROR: Can't set harware parameters. " << snd_strerror(pcm) << "\n";
return NULL;
}
if ((pcm = snd_pcm_hw_params(handle_, params)) < 0)
throw new SnapException("Can't set harware parameters. " + string(snd_strerror(pcm)));
/* Resume information */
logD << "PCM name: " << snd_pcm_name(pcm_handle) << "\n";
logD << "PCM state: " << snd_pcm_state_name(snd_pcm_state(pcm_handle)) << "\n";
logD << "PCM name: " << snd_pcm_name(handle_) << "\n";
logD << "PCM state: " << snd_pcm_state_name(snd_pcm_state(handle_)) << "\n";
snd_pcm_hw_params_get_channels(params, &tmp);
logD << "channels: " << tmp << "\n";
@ -126,22 +108,21 @@ snd_pcm_t* Player::initAlsa()
snd_pcm_sw_params_t *swparams;
snd_pcm_sw_params_alloca(&swparams);
snd_pcm_sw_params_current(pcm_handle, swparams);
snd_pcm_sw_params_current(handle_, swparams);
snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, frames_);
snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, frames_);
snd_pcm_sw_params_set_avail_min(handle_, swparams, frames_);
snd_pcm_sw_params_set_start_threshold(handle_, swparams, frames_);
// snd_pcm_sw_params_set_stop_threshold(pcm_handle, swparams, frames_);
snd_pcm_sw_params(pcm_handle, swparams);
return pcm_handle;
snd_pcm_sw_params(handle_, swparams);
}
void Player::uninitAlsa(snd_pcm_t* handle)
void Player::uninitAlsa()
{
if (handle != NULL)
if (handle_ != NULL)
{
snd_pcm_drain(handle);
snd_pcm_close(handle);
snd_pcm_drain(handle_);
snd_pcm_close(handle_);
}
if (buff_ != NULL)
@ -154,6 +135,7 @@ void Player::uninitAlsa(snd_pcm_t* handle)
void Player::start()
{
initAlsa();
active_ = true;
playerThread_ = thread(&Player::worker, this);
}
@ -172,42 +154,33 @@ void Player::stop()
active_ = false;
playerThread_.join();
}
uninitAlsa();
}
void Player::worker()
{
snd_pcm_t* handle = NULL;
snd_pcm_sframes_t pcm;
snd_pcm_sframes_t framesAvail;
snd_pcm_sframes_t framesDelay;
while (active_)
{
if (handle == NULL)
{
if ((handle = initAlsa()) == NULL)
{
usleep(100*1000);
continue;
}
}
snd_pcm_avail_delay(handle, &framesAvail, &framesDelay);
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";
if (stream_->getPlayerChunk(buff_, delay, frames_))
{
if ((pcm = snd_pcm_writei(handle, buff_, frames_)) == -EPIPE)
if ((pcm = snd_pcm_writei(handle_, buff_, frames_)) == -EPIPE)
{
logE << "XRUN\n";
snd_pcm_prepare(handle);
snd_pcm_prepare(handle_);
}
else if (pcm < 0)
{
logE << "ERROR. Can't write to PCM device: " << snd_strerror(pcm) << "\n";
uninitAlsa(handle);
handle = NULL;
uninitAlsa();
initAlsa();
}
}
else
@ -216,7 +189,6 @@ void Player::worker()
usleep(100*1000);
}
}
uninitAlsa(handle);
}

View file

@ -38,9 +38,10 @@ public:
static std::vector<PcmDevice> pcm_list(void);
private:
snd_pcm_t* initAlsa();
void uninitAlsa(snd_pcm_t* handle);
void initAlsa();
void uninitAlsa();
void worker();
snd_pcm_t* handle_;
snd_pcm_uframes_t frames_;
char *buff_;
std::atomic<bool> active_;

View file

@ -33,6 +33,12 @@ public:
std::strcpy(text_, text);
}
SnapException(const std::string& text)
{
text_ = new char[text.size()];
std::strcpy(text_, text.c_str());
}
SnapException(const SnapException& e)
{
text_ = new char[std::strlen(e.text_)];