Add Audio Server profiling time to the profiler

This commit is contained in:
Marcelo Fernandez 2018-06-20 21:02:02 -03:00
parent b64bf118f1
commit 9f6536bd0a
8 changed files with 198 additions and 84 deletions

View file

@ -142,8 +142,6 @@ Error AudioDriverALSA::init_device() {
samples_in.resize(period_size * channels);
samples_out.resize(period_size * channels);
snd_pcm_nonblock(pcm_handle, 0);
return OK;
}
@ -168,54 +166,50 @@ void AudioDriverALSA::thread_func(void *p_udata) {
AudioDriverALSA *ad = (AudioDriverALSA *)p_udata;
while (!ad->exit_thread) {
ad->lock();
ad->start_counting_ticks();
if (!ad->active) {
for (unsigned int i = 0; i < ad->period_size * ad->channels; i++) {
ad->samples_out[i] = 0;
};
}
} else {
ad->lock();
ad->audio_server_process(ad->period_size, ad->samples_in.ptrw());
ad->unlock();
for (unsigned int i = 0; i < ad->period_size * ad->channels; i++) {
ad->samples_out[i] = ad->samples_in[i] >> 16;
}
};
}
int todo = ad->period_size;
int total = 0;
while (todo) {
if (ad->exit_thread)
break;
while (todo && !ad->exit_thread) {
uint8_t *src = (uint8_t *)ad->samples_out.ptr();
int wrote = snd_pcm_writei(ad->pcm_handle, (void *)(src + (total * ad->channels)), todo);
if (wrote < 0) {
if (ad->exit_thread)
break;
if (wrote > 0) {
total += wrote;
todo -= wrote;
} else if (wrote == -EAGAIN) {
ad->stop_counting_ticks();
ad->unlock();
if (wrote == -EAGAIN) {
//can't write yet (though this is blocking..)
usleep(1000);
continue;
}
OS::get_singleton()->delay_usec(1000);
ad->lock();
ad->start_counting_ticks();
} else {
wrote = snd_pcm_recover(ad->pcm_handle, wrote, 0);
if (wrote < 0) {
//absolute fail
fprintf(stderr, "ALSA failed and can't recover: %s\n", snd_strerror(wrote));
ERR_PRINTS("ALSA: Failed and can't recover: " + String(snd_strerror(wrote)));
ad->active = false;
ad->exit_thread = true;
break;
}
continue;
};
total += wrote;
todo -= wrote;
};
}
}
// User selected a new device, finish the current one so we'll init the new device
if (ad->device_name != ad->new_device) {
@ -232,10 +226,12 @@ void AudioDriverALSA::thread_func(void *p_udata) {
if (err != OK) {
ad->active = false;
ad->exit_thread = true;
break;
}
}
}
ad->stop_counting_ticks();
ad->unlock();
};
ad->thread_exited = true;
@ -296,7 +292,9 @@ String AudioDriverALSA::get_device() {
void AudioDriverALSA::set_device(String device) {
lock();
new_device = device;
unlock();
}
void AudioDriverALSA::lock() {
@ -323,21 +321,21 @@ void AudioDriverALSA::finish_device() {
void AudioDriverALSA::finish() {
if (!thread)
return;
if (thread) {
exit_thread = true;
Thread::wait_to_finish(thread);
exit_thread = true;
Thread::wait_to_finish(thread);
memdelete(thread);
thread = NULL;
if (mutex) {
memdelete(mutex);
mutex = NULL;
}
}
finish_device();
memdelete(thread);
if (mutex) {
memdelete(mutex);
mutex = NULL;
}
thread = NULL;
};
}
AudioDriverALSA::AudioDriverALSA() {