2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_driver_alsa.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "audio_driver_alsa.h"
|
|
|
|
|
|
|
|
#ifdef ALSA_ENABLED
|
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/project_settings.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-16 18:19:45 +00:00
|
|
|
#include <errno.h>
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
Error AudioDriverALSA::init_device() {
|
2017-08-29 19:47:44 +00:00
|
|
|
mix_rate = GLOBAL_DEF("audio/mix_rate", DEFAULT_MIX_RATE);
|
2017-01-16 18:19:45 +00:00
|
|
|
speaker_mode = SPEAKER_MODE_STEREO;
|
2014-02-10 01:10:30 +00:00
|
|
|
channels = 2;
|
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
// If there is a specified device check that it is really present
|
|
|
|
if (device_name != "Default") {
|
|
|
|
Array list = get_device_list();
|
|
|
|
if (list.find(device_name) == -1) {
|
|
|
|
device_name = "Default";
|
|
|
|
new_device = "Default";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int status;
|
2014-02-10 01:10:30 +00:00
|
|
|
snd_pcm_hw_params_t *hwparams;
|
|
|
|
snd_pcm_sw_params_t *swparams;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#define CHECK_FAIL(m_cond) \
|
|
|
|
if (m_cond) { \
|
|
|
|
fprintf(stderr, "ALSA ERR: %s\n", snd_strerror(status)); \
|
2018-07-23 11:55:34 +00:00
|
|
|
if (pcm_handle) { \
|
|
|
|
snd_pcm_close(pcm_handle); \
|
|
|
|
pcm_handle = NULL; \
|
|
|
|
} \
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(m_cond, ERR_CANT_OPEN); \
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//todo, add
|
|
|
|
//6 chans - "plug:surround51"
|
|
|
|
//4 chans - "plug:surround40";
|
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
if (device_name == "Default") {
|
|
|
|
status = snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
|
|
|
|
} else {
|
|
|
|
String device = device_name;
|
|
|
|
int pos = device.find(";");
|
|
|
|
if (pos != -1) {
|
|
|
|
device = device.substr(0, pos);
|
|
|
|
}
|
|
|
|
status = snd_pcm_open(&pcm_handle, device.utf8().get_data(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(status < 0, ERR_CANT_OPEN);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
snd_pcm_hw_params_alloca(&hwparams);
|
|
|
|
|
2017-01-03 18:00:31 +00:00
|
|
|
status = snd_pcm_hw_params_any(pcm_handle, hwparams);
|
2017-03-05 15:44:50 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
status = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
|
2017-03-05 15:44:50 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//not interested in anything else
|
|
|
|
status = snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE);
|
2017-03-05 15:44:50 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//todo: support 4 and 6
|
|
|
|
status = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, 2);
|
2017-03-05 15:44:50 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
status = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &mix_rate, NULL);
|
2017-03-05 15:44:50 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-29 19:47:44 +00:00
|
|
|
// In ALSA the period size seems to be the one that will determine the actual latency
|
|
|
|
// Ref: https://www.alsa-project.org/main/index.php/FramesPeriods
|
|
|
|
unsigned int periods = 2;
|
|
|
|
int latency = GLOBAL_DEF("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
|
|
|
|
buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
|
|
|
|
buffer_size = buffer_frames * periods;
|
|
|
|
period_size = buffer_frames;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-03 18:00:31 +00:00
|
|
|
// set buffer size from project settings
|
|
|
|
status = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size);
|
2017-03-05 15:44:50 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-03 18:00:31 +00:00
|
|
|
status = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &period_size, NULL);
|
2017-03-05 15:44:50 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-08-24 06:47:34 +00:00
|
|
|
print_verbose("Audio buffer frames: " + itos(period_size) + " calculated latency: " + itos(period_size * 1000 / mix_rate) + "ms");
|
2017-08-29 19:47:44 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
status = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &periods, NULL);
|
2017-03-05 15:44:50 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
status = snd_pcm_hw_params(pcm_handle, hwparams);
|
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//snd_pcm_hw_params_free(&hwparams);
|
|
|
|
|
|
|
|
snd_pcm_sw_params_alloca(&swparams);
|
2017-01-03 18:00:31 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
status = snd_pcm_sw_params_current(pcm_handle, swparams);
|
2017-03-05 15:44:50 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-03 18:00:31 +00:00
|
|
|
status = snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, period_size);
|
2017-03-05 15:44:50 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
status = snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 1);
|
2017-03-05 15:44:50 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
status = snd_pcm_sw_params(pcm_handle, swparams);
|
2017-03-05 15:44:50 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
samples_in.resize(period_size * channels);
|
|
|
|
samples_out.resize(period_size * channels);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return OK;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Error AudioDriverALSA::init() {
|
|
|
|
|
|
|
|
active = false;
|
|
|
|
thread_exited = false;
|
|
|
|
exit_thread = false;
|
|
|
|
|
|
|
|
Error err = init_device();
|
|
|
|
if (err == OK) {
|
|
|
|
thread = Thread::create(AudioDriverALSA::thread_func, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2018-12-08 20:07:33 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AudioDriverALSA::thread_func(void *p_udata) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
AudioDriverALSA *ad = (AudioDriverALSA *)p_udata;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
while (!ad->exit_thread) {
|
2018-06-21 00:02:02 +00:00
|
|
|
|
|
|
|
ad->lock();
|
|
|
|
ad->start_counting_ticks();
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!ad->active) {
|
2019-09-22 16:45:08 +00:00
|
|
|
for (uint64_t i = 0; i < ad->period_size * ad->channels; i++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
ad->samples_out.write[i] = 0;
|
2018-06-21 00:02:02 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-06-21 00:02:02 +00:00
|
|
|
} else {
|
2018-03-25 03:43:51 +00:00
|
|
|
ad->audio_server_process(ad->period_size, ad->samples_in.ptrw());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-09-22 16:45:08 +00:00
|
|
|
for (uint64_t i = 0; i < ad->period_size * ad->channels; i++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
ad->samples_out.write[i] = ad->samples_in[i] >> 16;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2018-06-21 00:02:02 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-03 18:00:31 +00:00
|
|
|
int todo = ad->period_size;
|
2014-02-10 01:10:30 +00:00
|
|
|
int total = 0;
|
|
|
|
|
2018-06-21 00:02:02 +00:00
|
|
|
while (todo && !ad->exit_thread) {
|
2018-03-25 03:43:51 +00:00
|
|
|
uint8_t *src = (uint8_t *)ad->samples_out.ptr();
|
2017-03-05 15:44:50 +00:00
|
|
|
int wrote = snd_pcm_writei(ad->pcm_handle, (void *)(src + (total * ad->channels)), todo);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-06-21 00:02:02 +00:00
|
|
|
if (wrote > 0) {
|
|
|
|
total += wrote;
|
|
|
|
todo -= wrote;
|
|
|
|
} else if (wrote == -EAGAIN) {
|
|
|
|
ad->stop_counting_ticks();
|
|
|
|
ad->unlock();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-06-21 00:02:02 +00:00
|
|
|
OS::get_singleton()->delay_usec(1000);
|
|
|
|
|
|
|
|
ad->lock();
|
|
|
|
ad->start_counting_ticks();
|
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
wrote = snd_pcm_recover(ad->pcm_handle, wrote, 0);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (wrote < 0) {
|
2019-11-06 16:03:04 +00:00
|
|
|
ERR_PRINT("ALSA: Failed and can't recover: " + String(snd_strerror(wrote)));
|
2017-03-05 15:44:50 +00:00
|
|
|
ad->active = false;
|
|
|
|
ad->exit_thread = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2018-06-21 00:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-25 03:43:51 +00:00
|
|
|
|
|
|
|
// User selected a new device, finish the current one so we'll init the new device
|
|
|
|
if (ad->device_name != ad->new_device) {
|
|
|
|
ad->device_name = ad->new_device;
|
|
|
|
ad->finish_device();
|
|
|
|
|
|
|
|
Error err = ad->init_device();
|
|
|
|
if (err != OK) {
|
|
|
|
ERR_PRINT("ALSA: init_device error");
|
|
|
|
ad->device_name = "Default";
|
|
|
|
ad->new_device = "Default";
|
|
|
|
|
|
|
|
err = ad->init_device();
|
|
|
|
if (err != OK) {
|
|
|
|
ad->active = false;
|
|
|
|
ad->exit_thread = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 00:02:02 +00:00
|
|
|
|
|
|
|
ad->stop_counting_ticks();
|
|
|
|
ad->unlock();
|
2018-12-08 20:07:33 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ad->thread_exited = true;
|
2018-12-08 20:07:33 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void AudioDriverALSA::start() {
|
|
|
|
|
|
|
|
active = true;
|
2018-12-08 20:07:33 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int AudioDriverALSA::get_mix_rate() const {
|
|
|
|
|
|
|
|
return mix_rate;
|
2018-12-08 20:07:33 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-15 19:06:14 +00:00
|
|
|
AudioDriver::SpeakerMode AudioDriverALSA::get_speaker_mode() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-16 18:19:45 +00:00
|
|
|
return speaker_mode;
|
2018-12-08 20:07:33 +00:00
|
|
|
}
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
Array AudioDriverALSA::get_device_list() {
|
|
|
|
|
|
|
|
Array list;
|
|
|
|
|
|
|
|
list.push_back("Default");
|
|
|
|
|
|
|
|
void **hints;
|
|
|
|
|
|
|
|
if (snd_device_name_hint(-1, "pcm", &hints) < 0)
|
|
|
|
return list;
|
|
|
|
|
|
|
|
for (void **n = hints; *n != NULL; n++) {
|
|
|
|
char *name = snd_device_name_get_hint(*n, "NAME");
|
|
|
|
char *desc = snd_device_name_get_hint(*n, "DESC");
|
|
|
|
|
|
|
|
if (name != NULL && !strncmp(name, "plughw", 6)) {
|
|
|
|
if (desc) {
|
|
|
|
list.push_back(String(name) + ";" + String(desc));
|
|
|
|
} else {
|
|
|
|
list.push_back(String(name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc != NULL)
|
|
|
|
free(desc);
|
|
|
|
if (name != NULL)
|
|
|
|
free(name);
|
|
|
|
}
|
|
|
|
snd_device_name_free_hint(hints);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
String AudioDriverALSA::get_device() {
|
|
|
|
|
|
|
|
return device_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverALSA::set_device(String device) {
|
|
|
|
|
2018-06-21 00:02:02 +00:00
|
|
|
lock();
|
2018-03-25 03:43:51 +00:00
|
|
|
new_device = device;
|
2018-06-21 00:02:02 +00:00
|
|
|
unlock();
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void AudioDriverALSA::lock() {
|
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
if (!thread)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-02-26 10:28:13 +00:00
|
|
|
mutex.lock();
|
2018-12-08 20:07:33 +00:00
|
|
|
}
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void AudioDriverALSA::unlock() {
|
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
if (!thread)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-02-26 10:28:13 +00:00
|
|
|
mutex.unlock();
|
2018-12-08 20:07:33 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
void AudioDriverALSA::finish_device() {
|
|
|
|
|
2018-07-23 11:55:34 +00:00
|
|
|
if (pcm_handle) {
|
2018-03-25 03:43:51 +00:00
|
|
|
snd_pcm_close(pcm_handle);
|
2018-07-23 11:55:34 +00:00
|
|
|
pcm_handle = NULL;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void AudioDriverALSA::finish() {
|
|
|
|
|
2018-06-21 00:02:02 +00:00
|
|
|
if (thread) {
|
|
|
|
exit_thread = true;
|
|
|
|
Thread::wait_to_finish(thread);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-06-21 00:02:02 +00:00
|
|
|
memdelete(thread);
|
|
|
|
thread = NULL;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
2018-06-21 00:02:02 +00:00
|
|
|
|
|
|
|
finish_device();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-12-08 20:07:33 +00:00
|
|
|
AudioDriverALSA::AudioDriverALSA() :
|
|
|
|
thread(NULL),
|
|
|
|
pcm_handle(NULL),
|
|
|
|
device_name("Default"),
|
|
|
|
new_device("Default") {
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-12-08 20:07:33 +00:00
|
|
|
AudioDriverALSA::~AudioDriverALSA() {
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
#endif
|