2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* audio_driver_pulseaudio.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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-12-19 12:44:34 +00:00
|
|
|
#include "audio_driver_pulseaudio.h"
|
|
|
|
|
|
|
|
#ifdef PULSEAUDIO_ENABLED
|
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/config/project_settings.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/os.h"
|
2021-11-03 09:03:54 +00:00
|
|
|
#include "core/version.h"
|
2014-12-19 12:44:34 +00:00
|
|
|
|
2021-06-03 08:09:18 +00:00
|
|
|
#ifdef ALSAMIDI_ENABLED
|
2023-02-15 12:13:56 +00:00
|
|
|
#ifdef SOWRAP_ENABLED
|
2021-06-03 08:09:18 +00:00
|
|
|
#include "drivers/alsa/asound-so_wrap.h"
|
2023-02-15 12:13:56 +00:00
|
|
|
#else
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
#endif
|
2021-06-03 08:09:18 +00:00
|
|
|
#endif
|
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
void AudioDriverPulseAudio::pa_state_cb(pa_context *c, void *userdata) {
|
2022-04-05 10:40:26 +00:00
|
|
|
AudioDriverPulseAudio *ad = static_cast<AudioDriverPulseAudio *>(userdata);
|
2017-08-22 21:27:17 +00:00
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
switch (pa_context_get_state(c)) {
|
2017-08-22 21:27:17 +00:00
|
|
|
case PA_CONTEXT_TERMINATED:
|
2021-05-13 22:28:22 +00:00
|
|
|
print_verbose("PulseAudio: context terminated");
|
|
|
|
ad->pa_ready = -1;
|
|
|
|
break;
|
2018-03-25 03:43:51 +00:00
|
|
|
case PA_CONTEXT_FAILED:
|
2021-05-13 22:28:22 +00:00
|
|
|
print_verbose("PulseAudio: context failed");
|
2018-03-25 03:43:51 +00:00
|
|
|
ad->pa_ready = -1;
|
2017-08-22 21:27:17 +00:00
|
|
|
break;
|
|
|
|
case PA_CONTEXT_READY:
|
2021-05-13 22:28:22 +00:00
|
|
|
print_verbose("PulseAudio: context ready");
|
2018-03-25 03:43:51 +00:00
|
|
|
ad->pa_ready = 1;
|
2017-08-22 21:27:17 +00:00
|
|
|
break;
|
2018-09-26 11:13:56 +00:00
|
|
|
default:
|
2021-05-13 22:28:22 +00:00
|
|
|
print_verbose("PulseAudio: context other");
|
2018-09-26 11:13:56 +00:00
|
|
|
// TODO: Check if we want to handle some of the other
|
|
|
|
// PA context states like PA_CONTEXT_UNCONNECTED.
|
|
|
|
break;
|
2017-08-22 21:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
void AudioDriverPulseAudio::pa_sink_info_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata) {
|
2022-04-05 10:40:26 +00:00
|
|
|
AudioDriverPulseAudio *ad = static_cast<AudioDriverPulseAudio *>(userdata);
|
2017-08-22 21:27:17 +00:00
|
|
|
|
|
|
|
// If eol is set to a positive number, you're at the end of the list
|
|
|
|
if (eol > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-13 22:28:22 +00:00
|
|
|
// If eol is set to a negative number there's an error.
|
|
|
|
if (eol < 0) {
|
|
|
|
ERR_PRINT("PulseAudio: sink info error: " + String(pa_strerror(pa_context_errno(c))));
|
|
|
|
ad->pa_status--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-12 15:01:23 +00:00
|
|
|
ad->pa_map = l->channel_map;
|
2018-03-25 03:43:51 +00:00
|
|
|
ad->pa_status++;
|
2017-08-22 21:27:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 20:28:41 +00:00
|
|
|
void AudioDriverPulseAudio::pa_source_info_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata) {
|
2022-04-05 10:40:26 +00:00
|
|
|
AudioDriverPulseAudio *ad = static_cast<AudioDriverPulseAudio *>(userdata);
|
2018-07-27 20:28:41 +00:00
|
|
|
|
|
|
|
// If eol is set to a positive number, you're at the end of the list
|
|
|
|
if (eol > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-13 22:28:22 +00:00
|
|
|
// If eol is set to a negative number there's an error.
|
|
|
|
if (eol < 0) {
|
|
|
|
ERR_PRINT("PulseAudio: sink info error: " + String(pa_strerror(pa_context_errno(c))));
|
|
|
|
ad->pa_status--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-27 20:28:41 +00:00
|
|
|
ad->pa_rec_map = l->channel_map;
|
|
|
|
ad->pa_status++;
|
|
|
|
}
|
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
void AudioDriverPulseAudio::pa_server_info_cb(pa_context *c, const pa_server_info *i, void *userdata) {
|
2019-12-28 07:38:29 +00:00
|
|
|
ERR_FAIL_COND_MSG(!i, "PulseAudio server info is null.");
|
2022-04-05 10:40:26 +00:00
|
|
|
AudioDriverPulseAudio *ad = static_cast<AudioDriverPulseAudio *>(userdata);
|
2017-08-22 21:27:17 +00:00
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
ad->default_input_device = i->default_source_name;
|
|
|
|
ad->default_output_device = i->default_sink_name;
|
2018-03-25 03:43:51 +00:00
|
|
|
ad->pa_status++;
|
2017-08-22 21:27:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
Error AudioDriverPulseAudio::detect_channels(bool input) {
|
|
|
|
pa_channel_map_init_stereo(input ? &pa_rec_map : &pa_map);
|
2017-08-22 21:27:17 +00:00
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
String device = input ? input_device_name : output_device_name;
|
2018-07-27 20:28:41 +00:00
|
|
|
if (device == "Default") {
|
2018-03-25 03:43:51 +00:00
|
|
|
// Get the default output device name
|
|
|
|
pa_status = 0;
|
|
|
|
pa_operation *pa_op = pa_context_get_server_info(pa_ctx, &AudioDriverPulseAudio::pa_server_info_cb, (void *)this);
|
|
|
|
if (pa_op) {
|
|
|
|
while (pa_status == 0) {
|
2020-04-01 23:20:12 +00:00
|
|
|
int ret = pa_mainloop_iterate(pa_ml, 1, nullptr);
|
2018-03-25 03:43:51 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
ERR_PRINT("pa_mainloop_iterate error");
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 21:27:17 +00:00
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
pa_operation_unref(pa_op);
|
|
|
|
} else {
|
2021-05-13 22:28:22 +00:00
|
|
|
ERR_PRINT("pa_context_get_server_info error: " + String(pa_strerror(pa_context_errno(pa_ctx))));
|
|
|
|
return FAILED;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
2017-08-22 21:27:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 20:28:41 +00:00
|
|
|
char dev[1024];
|
|
|
|
if (device == "Default") {
|
2022-11-24 14:41:40 +00:00
|
|
|
strcpy(dev, input ? default_input_device.utf8().get_data() : default_output_device.utf8().get_data());
|
2018-03-25 03:43:51 +00:00
|
|
|
} else {
|
2018-07-27 20:28:41 +00:00
|
|
|
strcpy(dev, device.utf8().get_data());
|
2017-08-22 21:27:17 +00:00
|
|
|
}
|
2021-05-13 22:28:22 +00:00
|
|
|
print_verbose("PulseAudio: Detecting channels for device: " + String(dev));
|
2017-08-22 21:27:17 +00:00
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
// Now using the device name get the amount of channels
|
|
|
|
pa_status = 0;
|
2018-07-27 20:28:41 +00:00
|
|
|
pa_operation *pa_op;
|
2022-11-24 14:41:40 +00:00
|
|
|
if (input) {
|
2018-07-27 20:28:41 +00:00
|
|
|
pa_op = pa_context_get_source_info_by_name(pa_ctx, dev, &AudioDriverPulseAudio::pa_source_info_cb, (void *)this);
|
|
|
|
} else {
|
|
|
|
pa_op = pa_context_get_sink_info_by_name(pa_ctx, dev, &AudioDriverPulseAudio::pa_sink_info_cb, (void *)this);
|
|
|
|
}
|
|
|
|
|
2017-08-22 21:27:17 +00:00
|
|
|
if (pa_op) {
|
2018-03-25 03:43:51 +00:00
|
|
|
while (pa_status == 0) {
|
2020-04-01 23:20:12 +00:00
|
|
|
int ret = pa_mainloop_iterate(pa_ml, 1, nullptr);
|
2017-08-22 21:27:17 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
ERR_PRINT("pa_mainloop_iterate error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_operation_unref(pa_op);
|
2021-05-13 22:28:22 +00:00
|
|
|
|
|
|
|
if (pa_status == -1) {
|
|
|
|
return FAILED;
|
|
|
|
}
|
2017-08-22 21:27:17 +00:00
|
|
|
} else {
|
2022-11-24 14:41:40 +00:00
|
|
|
if (input) {
|
2018-07-27 20:28:41 +00:00
|
|
|
ERR_PRINT("pa_context_get_source_info_by_name error");
|
|
|
|
} else {
|
|
|
|
ERR_PRINT("pa_context_get_sink_info_by_name error");
|
|
|
|
}
|
2017-08-22 21:27:17 +00:00
|
|
|
}
|
2021-05-13 22:28:22 +00:00
|
|
|
|
|
|
|
return OK;
|
2017-08-22 21:27:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
Error AudioDriverPulseAudio::init_output_device() {
|
|
|
|
// If there is a specified output device, check that it is really present
|
|
|
|
if (output_device_name != "Default") {
|
|
|
|
PackedStringArray list = get_output_device_list();
|
|
|
|
if (list.find(output_device_name) == -1) {
|
|
|
|
output_device_name = "Default";
|
|
|
|
new_output_device = "Default";
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-22 21:27:17 +00:00
|
|
|
|
2018-03-01 01:37:42 +00:00
|
|
|
// Detect the amount of channels PulseAudio is using
|
2018-04-12 15:01:23 +00:00
|
|
|
// Note: If using an even amount of channels (2, 4, etc) channels and pa_map.channels will be equal,
|
|
|
|
// if not then pa_map.channels will have the real amount of channels PulseAudio is using and channels
|
|
|
|
// will have the amount of channels Godot is using (in this case it's pa_map.channels + 1)
|
2021-05-13 22:28:22 +00:00
|
|
|
Error err = detect_channels();
|
|
|
|
if (err != OK) {
|
|
|
|
// This most likely means there are no sinks.
|
2022-11-24 14:41:40 +00:00
|
|
|
ERR_PRINT("PulseAudio: init_output_device failed to detect number of output channels");
|
2021-05-13 22:28:22 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-04-12 15:01:23 +00:00
|
|
|
switch (pa_map.channels) {
|
2018-03-01 01:37:42 +00:00
|
|
|
case 1: // Mono
|
|
|
|
case 3: // Surround 2.1
|
|
|
|
case 5: // Surround 5.0
|
|
|
|
case 7: // Surround 7.0
|
2018-04-12 15:01:23 +00:00
|
|
|
channels = pa_map.channels + 1;
|
2018-03-01 01:37:42 +00:00
|
|
|
break;
|
|
|
|
|
2017-08-22 21:27:17 +00:00
|
|
|
case 2: // Stereo
|
2018-03-01 01:37:42 +00:00
|
|
|
case 4: // Surround 4.0
|
2017-08-22 21:27:17 +00:00
|
|
|
case 6: // Surround 5.1
|
|
|
|
case 8: // Surround 7.1
|
2018-04-12 15:01:23 +00:00
|
|
|
channels = pa_map.channels;
|
2017-08-22 21:27:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-11-08 23:44:11 +00:00
|
|
|
WARN_PRINT("PulseAudio: Unsupported number of output channels: " + itos(pa_map.channels));
|
2018-04-12 15:01:23 +00:00
|
|
|
pa_channel_map_init_stereo(&pa_map);
|
2018-04-11 15:04:23 +00:00
|
|
|
channels = 2;
|
2017-08-22 21:27:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-12-19 12:44:34 +00:00
|
|
|
|
2023-05-11 10:32:23 +00:00
|
|
|
int tmp_latency = GLOBAL_GET("audio/driver/output_latency");
|
|
|
|
buffer_frames = closest_power_of_2(tmp_latency * mix_rate / 1000);
|
2018-04-12 15:01:23 +00:00
|
|
|
pa_buffer_size = buffer_frames * pa_map.channels;
|
2017-08-29 19:47:44 +00:00
|
|
|
|
2021-11-08 23:44:11 +00:00
|
|
|
print_verbose("PulseAudio: detected " + itos(pa_map.channels) + " output channels");
|
|
|
|
print_verbose("PulseAudio: audio buffer frames: " + itos(buffer_frames) + " calculated output latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms");
|
2017-01-03 18:00:31 +00:00
|
|
|
|
2018-04-12 15:01:23 +00:00
|
|
|
pa_sample_spec spec;
|
|
|
|
spec.format = PA_SAMPLE_S16LE;
|
|
|
|
spec.channels = pa_map.channels;
|
|
|
|
spec.rate = mix_rate;
|
2019-07-01 20:03:02 +00:00
|
|
|
pa_map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
|
|
|
|
pa_map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
|
|
|
|
pa_map.map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
|
|
|
|
pa_map.map[3] = PA_CHANNEL_POSITION_LFE;
|
|
|
|
pa_map.map[4] = PA_CHANNEL_POSITION_REAR_LEFT;
|
|
|
|
pa_map.map[5] = PA_CHANNEL_POSITION_REAR_RIGHT;
|
|
|
|
pa_map.map[6] = PA_CHANNEL_POSITION_SIDE_LEFT;
|
|
|
|
pa_map.map[7] = PA_CHANNEL_POSITION_SIDE_RIGHT;
|
2018-04-12 15:01:23 +00:00
|
|
|
|
|
|
|
pa_str = pa_stream_new(pa_ctx, "Sound", &spec, &pa_map);
|
2020-04-01 23:20:12 +00:00
|
|
|
if (pa_str == nullptr) {
|
2019-11-06 16:03:04 +00:00
|
|
|
ERR_PRINT("PulseAudio: pa_stream_new error: " + String(pa_strerror(pa_context_errno(pa_ctx))));
|
2018-04-12 15:01:23 +00:00
|
|
|
ERR_FAIL_V(ERR_CANT_OPEN);
|
|
|
|
}
|
|
|
|
|
2017-01-03 18:00:31 +00:00
|
|
|
pa_buffer_attr attr;
|
2017-08-29 19:47:44 +00:00
|
|
|
// set to appropriate buffer length (in bytes) from global settings
|
2018-04-15 14:29:16 +00:00
|
|
|
// Note: PulseAudio defaults to 4 fragments, which means that the actual
|
|
|
|
// latency is tlength / fragments. It seems that the PulseAudio has no way
|
|
|
|
// to get the fragments number so we're hardcoding this to the default of 4
|
|
|
|
const int fragments = 4;
|
|
|
|
attr.tlength = pa_buffer_size * sizeof(int16_t) * fragments;
|
2017-01-03 18:00:31 +00:00
|
|
|
// set them to be automatically chosen
|
|
|
|
attr.prebuf = (uint32_t)-1;
|
|
|
|
attr.maxlength = (uint32_t)-1;
|
|
|
|
attr.minreq = (uint32_t)-1;
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
const char *dev = output_device_name == "Default" ? nullptr : output_device_name.utf8().get_data();
|
2018-03-25 03:43:51 +00:00
|
|
|
pa_stream_flags flags = pa_stream_flags(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE);
|
2020-04-01 23:20:12 +00:00
|
|
|
int error_code = pa_stream_connect_playback(pa_str, dev, &attr, flags, nullptr, nullptr);
|
2018-03-25 03:43:51 +00:00
|
|
|
ERR_FAIL_COND_V(error_code < 0, ERR_CANT_OPEN);
|
2014-12-19 12:44:34 +00:00
|
|
|
|
2018-03-01 01:37:42 +00:00
|
|
|
samples_in.resize(buffer_frames * channels);
|
|
|
|
samples_out.resize(pa_buffer_size);
|
2014-12-19 12:44:34 +00:00
|
|
|
|
2023-01-21 11:25:29 +00:00
|
|
|
// Reset audio input to keep synchronization.
|
2020-01-20 12:11:47 +00:00
|
|
|
input_position = 0;
|
|
|
|
input_size = 0;
|
2018-07-27 20:28:41 +00:00
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error AudioDriverPulseAudio::init() {
|
2023-02-15 12:13:56 +00:00
|
|
|
#ifdef SOWRAP_ENABLED
|
2021-02-19 23:13:33 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
int dylibloader_verbose = 1;
|
|
|
|
#else
|
|
|
|
int dylibloader_verbose = 0;
|
2021-06-03 08:09:18 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ALSAMIDI_ENABLED
|
|
|
|
// If using PulseAudio with ALSA MIDI, we need to initialize ALSA as well
|
|
|
|
initialize_asound(dylibloader_verbose);
|
2021-02-19 23:13:33 +00:00
|
|
|
#endif
|
|
|
|
if (initialize_pulse(dylibloader_verbose)) {
|
2021-02-16 15:19:03 +00:00
|
|
|
return ERR_CANT_OPEN;
|
|
|
|
}
|
2023-02-15 12:13:56 +00:00
|
|
|
#endif
|
2022-08-19 08:38:13 +00:00
|
|
|
active.clear();
|
|
|
|
exit_thread.clear();
|
2018-03-25 03:43:51 +00:00
|
|
|
|
2022-12-10 06:39:14 +00:00
|
|
|
mix_rate = _get_configured_mix_rate();
|
2018-03-25 03:43:51 +00:00
|
|
|
|
|
|
|
pa_ml = pa_mainloop_new();
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_COND_V(pa_ml == nullptr, ERR_CANT_OPEN);
|
2018-03-25 03:43:51 +00:00
|
|
|
|
2021-11-03 09:03:54 +00:00
|
|
|
String context_name;
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
context_name = VERSION_NAME " Editor";
|
|
|
|
} else {
|
|
|
|
context_name = GLOBAL_GET("application/config/name");
|
|
|
|
if (context_name.is_empty()) {
|
|
|
|
context_name = VERSION_NAME " Project";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_ctx = pa_context_new(pa_mainloop_get_api(pa_ml), context_name.utf8().ptr());
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_COND_V(pa_ctx == nullptr, ERR_CANT_OPEN);
|
2018-03-25 03:43:51 +00:00
|
|
|
|
|
|
|
pa_ready = 0;
|
|
|
|
pa_context_set_state_callback(pa_ctx, pa_state_cb, (void *)this);
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
int ret = pa_context_connect(pa_ctx, nullptr, PA_CONTEXT_NOFLAGS, nullptr);
|
2018-03-25 03:43:51 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (pa_ctx) {
|
|
|
|
pa_context_unref(pa_ctx);
|
2020-04-01 23:20:12 +00:00
|
|
|
pa_ctx = nullptr;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pa_ml) {
|
|
|
|
pa_mainloop_free(pa_ml);
|
2020-04-01 23:20:12 +00:00
|
|
|
pa_ml = nullptr;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_CANT_OPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (pa_ready == 0) {
|
2020-04-01 23:20:12 +00:00
|
|
|
ret = pa_mainloop_iterate(pa_ml, 1, nullptr);
|
2019-08-07 10:54:30 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
ERR_PRINT("pa_mainloop_iterate error");
|
|
|
|
}
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pa_ready < 0) {
|
|
|
|
if (pa_ctx) {
|
|
|
|
pa_context_disconnect(pa_ctx);
|
|
|
|
pa_context_unref(pa_ctx);
|
2020-04-01 23:20:12 +00:00
|
|
|
pa_ctx = nullptr;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pa_ml) {
|
|
|
|
pa_mainloop_free(pa_ml);
|
2020-04-01 23:20:12 +00:00
|
|
|
pa_ml = nullptr;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_CANT_OPEN;
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
init_output_device();
|
2021-05-13 22:28:22 +00:00
|
|
|
thread.start(AudioDriverPulseAudio::thread_func, this);
|
2014-12-19 12:44:34 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-09-26 17:50:42 +00:00
|
|
|
float AudioDriverPulseAudio::get_latency() {
|
|
|
|
if (latency == 0) { //only do this once since it's approximate anyway
|
2018-03-25 03:43:51 +00:00
|
|
|
lock();
|
|
|
|
|
|
|
|
pa_usec_t palat = 0;
|
|
|
|
if (pa_stream_get_state(pa_str) == PA_STREAM_READY) {
|
|
|
|
int negative = 0;
|
|
|
|
|
|
|
|
if (pa_stream_get_latency(pa_str, &palat, &negative) >= 0) {
|
|
|
|
if (negative) {
|
|
|
|
palat = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (palat > 0) {
|
|
|
|
latency = double(palat) / 1000000.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock();
|
2015-09-26 17:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return latency;
|
|
|
|
}
|
|
|
|
|
2014-12-19 12:44:34 +00:00
|
|
|
void AudioDriverPulseAudio::thread_func(void *p_udata) {
|
2022-04-07 10:23:40 +00:00
|
|
|
AudioDriverPulseAudio *ad = static_cast<AudioDriverPulseAudio *>(p_udata);
|
2018-07-04 01:08:43 +00:00
|
|
|
unsigned int write_ofs = 0;
|
|
|
|
size_t avail_bytes = 0;
|
2021-10-26 06:44:50 +00:00
|
|
|
uint64_t default_device_msec = OS::get_singleton()->get_ticks_msec();
|
2014-12-19 12:44:34 +00:00
|
|
|
|
2022-08-19 08:38:13 +00:00
|
|
|
while (!ad->exit_thread.is_set()) {
|
2018-07-04 01:08:43 +00:00
|
|
|
size_t read_bytes = 0;
|
|
|
|
size_t written_bytes = 0;
|
2018-06-21 00:02:02 +00:00
|
|
|
|
2018-07-04 01:08:43 +00:00
|
|
|
if (avail_bytes == 0) {
|
|
|
|
ad->lock();
|
|
|
|
ad->start_counting_ticks();
|
2014-12-19 12:44:34 +00:00
|
|
|
|
2022-08-19 08:38:13 +00:00
|
|
|
if (!ad->active.is_set()) {
|
2021-10-29 21:37:49 +00:00
|
|
|
ad->samples_out.fill(0);
|
2018-03-01 01:37:42 +00:00
|
|
|
} else {
|
2018-07-04 01:08:43 +00:00
|
|
|
ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
|
2018-03-01 01:37:42 +00:00
|
|
|
|
2021-10-29 21:37:49 +00:00
|
|
|
int16_t *out_ptr = ad->samples_out.ptrw();
|
|
|
|
|
2018-07-04 01:08:43 +00:00
|
|
|
if (ad->channels == ad->pa_map.channels) {
|
|
|
|
for (unsigned int i = 0; i < ad->pa_buffer_size; i++) {
|
2021-10-29 21:37:49 +00:00
|
|
|
out_ptr[i] = ad->samples_in[i] >> 16;
|
2018-07-04 01:08:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Uneven amount of channels
|
|
|
|
unsigned int in_idx = 0;
|
|
|
|
unsigned int out_idx = 0;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < ad->buffer_frames; i++) {
|
2018-09-26 15:38:02 +00:00
|
|
|
for (int j = 0; j < ad->pa_map.channels - 1; j++) {
|
2021-10-29 21:37:49 +00:00
|
|
|
out_ptr[out_idx++] = ad->samples_in[in_idx++] >> 16;
|
2018-07-04 01:08:43 +00:00
|
|
|
}
|
2018-11-18 00:16:06 +00:00
|
|
|
uint32_t l = ad->samples_in[in_idx++] >> 16;
|
|
|
|
uint32_t r = ad->samples_in[in_idx++] >> 16;
|
2021-10-29 21:37:49 +00:00
|
|
|
out_ptr[out_idx++] = (l + r) / 2;
|
2018-03-01 01:37:42 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-19 12:44:34 +00:00
|
|
|
}
|
2018-07-04 01:08:43 +00:00
|
|
|
|
|
|
|
avail_bytes = ad->pa_buffer_size * sizeof(int16_t);
|
|
|
|
write_ofs = 0;
|
|
|
|
ad->stop_counting_ticks();
|
|
|
|
ad->unlock();
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
2014-12-19 12:44:34 +00:00
|
|
|
|
2018-07-04 01:08:43 +00:00
|
|
|
ad->lock();
|
|
|
|
ad->start_counting_ticks();
|
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
int ret;
|
|
|
|
do {
|
2020-04-01 23:20:12 +00:00
|
|
|
ret = pa_mainloop_iterate(ad->pa_ml, 0, nullptr);
|
2018-03-25 03:43:51 +00:00
|
|
|
} while (ret > 0);
|
|
|
|
|
2018-07-04 01:08:43 +00:00
|
|
|
if (avail_bytes > 0 && pa_stream_get_state(ad->pa_str) == PA_STREAM_READY) {
|
|
|
|
size_t bytes = pa_stream_writable_size(ad->pa_str);
|
|
|
|
if (bytes > 0) {
|
|
|
|
size_t bytes_to_write = MIN(bytes, avail_bytes);
|
|
|
|
const void *ptr = ad->samples_out.ptr();
|
2020-04-01 23:20:12 +00:00
|
|
|
ret = pa_stream_write(ad->pa_str, (char *)ptr + write_ofs, bytes_to_write, nullptr, 0LL, PA_SEEK_RELATIVE);
|
2018-07-04 01:08:43 +00:00
|
|
|
if (ret != 0) {
|
2019-11-06 16:03:04 +00:00
|
|
|
ERR_PRINT("PulseAudio: pa_stream_write error: " + String(pa_strerror(ret)));
|
2018-03-25 03:43:51 +00:00
|
|
|
} else {
|
2018-07-04 01:08:43 +00:00
|
|
|
avail_bytes -= bytes_to_write;
|
|
|
|
write_ofs += bytes_to_write;
|
|
|
|
written_bytes += bytes_to_write;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-03 18:00:31 +00:00
|
|
|
}
|
2018-03-25 03:43:51 +00:00
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
// User selected a new output device, finish the current one so we'll init the new output device
|
|
|
|
if (ad->output_device_name != ad->new_output_device) {
|
|
|
|
ad->output_device_name = ad->new_output_device;
|
|
|
|
ad->finish_output_device();
|
2018-03-25 03:43:51 +00:00
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
Error err = ad->init_output_device();
|
2018-03-25 03:43:51 +00:00
|
|
|
if (err != OK) {
|
2022-11-24 14:41:40 +00:00
|
|
|
ERR_PRINT("PulseAudio: init_output_device error");
|
|
|
|
ad->output_device_name = "Default";
|
|
|
|
ad->new_output_device = "Default";
|
2018-03-25 03:43:51 +00:00
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
err = ad->init_output_device();
|
2018-03-25 03:43:51 +00:00
|
|
|
if (err != OK) {
|
2022-08-19 08:38:13 +00:00
|
|
|
ad->active.clear();
|
|
|
|
ad->exit_thread.set();
|
2018-03-25 03:43:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-25 17:57:31 +00:00
|
|
|
|
|
|
|
avail_bytes = 0;
|
|
|
|
write_ofs = 0;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
// If we're using the default output device, check that the current output device is still the default
|
|
|
|
if (ad->output_device_name == "Default") {
|
2021-10-26 06:44:50 +00:00
|
|
|
uint64_t msec = OS::get_singleton()->get_ticks_msec();
|
2018-12-12 14:17:46 +00:00
|
|
|
if (msec > (default_device_msec + 1000)) {
|
2022-11-24 14:41:40 +00:00
|
|
|
String old_default_device = ad->default_output_device;
|
2018-12-12 14:17:46 +00:00
|
|
|
|
|
|
|
default_device_msec = msec;
|
|
|
|
|
|
|
|
ad->pa_status = 0;
|
|
|
|
pa_operation *pa_op = pa_context_get_server_info(ad->pa_ctx, &AudioDriverPulseAudio::pa_server_info_cb, (void *)ad);
|
|
|
|
if (pa_op) {
|
|
|
|
while (ad->pa_status == 0) {
|
2020-04-01 23:20:12 +00:00
|
|
|
ret = pa_mainloop_iterate(ad->pa_ml, 1, nullptr);
|
2018-12-12 14:17:46 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
ERR_PRINT("pa_mainloop_iterate error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_operation_unref(pa_op);
|
|
|
|
} else {
|
2021-05-13 22:28:22 +00:00
|
|
|
ERR_PRINT("pa_context_get_server_info error: " + String(pa_strerror(pa_context_errno(ad->pa_ctx))));
|
2018-12-12 14:17:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
if (old_default_device != ad->default_output_device) {
|
|
|
|
ad->finish_output_device();
|
2018-12-12 14:17:46 +00:00
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
Error err = ad->init_output_device();
|
2018-12-12 14:17:46 +00:00
|
|
|
if (err != OK) {
|
2022-11-24 14:41:40 +00:00
|
|
|
ERR_PRINT("PulseAudio: init_output_device error");
|
2022-08-19 08:38:13 +00:00
|
|
|
ad->active.clear();
|
|
|
|
ad->exit_thread.set();
|
2018-12-12 14:17:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
avail_bytes = 0;
|
|
|
|
write_ofs = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 01:08:43 +00:00
|
|
|
if (ad->pa_rec_str && pa_stream_get_state(ad->pa_rec_str) == PA_STREAM_READY) {
|
|
|
|
size_t bytes = pa_stream_readable_size(ad->pa_rec_str);
|
|
|
|
if (bytes > 0) {
|
2020-04-01 23:20:12 +00:00
|
|
|
const void *ptr = nullptr;
|
2020-01-20 12:11:47 +00:00
|
|
|
size_t maxbytes = ad->input_buffer.size() * sizeof(int16_t);
|
2018-07-04 01:08:43 +00:00
|
|
|
|
|
|
|
bytes = MIN(bytes, maxbytes);
|
|
|
|
ret = pa_stream_peek(ad->pa_rec_str, &ptr, &bytes);
|
|
|
|
if (ret != 0) {
|
|
|
|
ERR_PRINT("pa_stream_peek error");
|
|
|
|
} else {
|
|
|
|
int16_t *srcptr = (int16_t *)ptr;
|
|
|
|
for (size_t i = bytes >> 1; i > 0; i--) {
|
2018-07-27 20:28:41 +00:00
|
|
|
int32_t sample = int32_t(*srcptr++) << 16;
|
2020-01-20 12:11:47 +00:00
|
|
|
ad->input_buffer_write(sample);
|
2018-07-27 20:28:41 +00:00
|
|
|
|
|
|
|
if (ad->pa_rec_map.channels == 1) {
|
2020-01-20 12:11:47 +00:00
|
|
|
// In case input device is single channel convert it to Stereo
|
|
|
|
ad->input_buffer_write(sample);
|
2018-07-04 01:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
read_bytes += bytes;
|
|
|
|
ret = pa_stream_drop(ad->pa_rec_str);
|
|
|
|
if (ret != 0) {
|
|
|
|
ERR_PRINT("pa_stream_drop error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 15:15:24 +00:00
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
// User selected a new input device, finish the current one so we'll init the new input device
|
|
|
|
if (ad->input_device_name != ad->new_input_device) {
|
|
|
|
ad->input_device_name = ad->new_input_device;
|
|
|
|
ad->finish_input_device();
|
2018-07-27 15:15:24 +00:00
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
Error err = ad->init_input_device();
|
2018-07-27 15:15:24 +00:00
|
|
|
if (err != OK) {
|
2022-11-24 14:41:40 +00:00
|
|
|
ERR_PRINT("PulseAudio: init_input_device error");
|
|
|
|
ad->input_device_name = "Default";
|
|
|
|
ad->new_input_device = "Default";
|
2018-07-27 15:15:24 +00:00
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
err = ad->init_input_device();
|
2018-07-27 15:15:24 +00:00
|
|
|
if (err != OK) {
|
2022-08-19 08:38:13 +00:00
|
|
|
ad->active.clear();
|
|
|
|
ad->exit_thread.set();
|
2018-07-27 15:15:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-04 01:08:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 00:02:02 +00:00
|
|
|
ad->stop_counting_ticks();
|
2018-03-25 03:43:51 +00:00
|
|
|
ad->unlock();
|
2018-07-04 01:08:43 +00:00
|
|
|
|
|
|
|
// Let the thread rest a while if we haven't read or write anything
|
|
|
|
if (written_bytes == 0 && read_bytes == 0) {
|
|
|
|
OS::get_singleton()->delay_usec(1000);
|
|
|
|
}
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
2014-12-19 12:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverPulseAudio::start() {
|
2022-08-19 08:38:13 +00:00
|
|
|
active.set();
|
2014-12-19 12:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AudioDriverPulseAudio::get_mix_rate() const {
|
|
|
|
return mix_rate;
|
|
|
|
}
|
|
|
|
|
2017-01-15 19:06:14 +00:00
|
|
|
AudioDriver::SpeakerMode AudioDriverPulseAudio::get_speaker_mode() const {
|
2017-08-22 21:27:17 +00:00
|
|
|
return get_speaker_mode_by_total_channels(channels);
|
2014-12-19 12:44:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 03:43:51 +00:00
|
|
|
void AudioDriverPulseAudio::pa_sinklist_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata) {
|
2022-04-05 10:40:26 +00:00
|
|
|
AudioDriverPulseAudio *ad = static_cast<AudioDriverPulseAudio *>(userdata);
|
2018-03-25 03:43:51 +00:00
|
|
|
|
|
|
|
// If eol is set to a positive number, you're at the end of the list
|
|
|
|
if (eol > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ad->pa_devices.push_back(l->name);
|
|
|
|
ad->pa_status++;
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
PackedStringArray AudioDriverPulseAudio::get_output_device_list() {
|
2018-03-25 03:43:51 +00:00
|
|
|
pa_devices.clear();
|
|
|
|
pa_devices.push_back("Default");
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
if (pa_ctx == nullptr) {
|
2018-03-25 03:43:51 +00:00
|
|
|
return pa_devices;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock();
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
// Get the output device list
|
2018-03-25 03:43:51 +00:00
|
|
|
pa_status = 0;
|
|
|
|
pa_operation *pa_op = pa_context_get_sink_info_list(pa_ctx, pa_sinklist_cb, (void *)this);
|
|
|
|
if (pa_op) {
|
|
|
|
while (pa_status == 0) {
|
2020-04-01 23:20:12 +00:00
|
|
|
int ret = pa_mainloop_iterate(pa_ml, 1, nullptr);
|
2018-03-25 03:43:51 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
ERR_PRINT("pa_mainloop_iterate error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_operation_unref(pa_op);
|
|
|
|
} else {
|
|
|
|
ERR_PRINT("pa_context_get_server_info error");
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock();
|
|
|
|
|
|
|
|
return pa_devices;
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
String AudioDriverPulseAudio::get_output_device() {
|
|
|
|
return output_device_name;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 15:40:15 +00:00
|
|
|
void AudioDriverPulseAudio::set_output_device(const String &p_name) {
|
2018-06-21 00:02:02 +00:00
|
|
|
lock();
|
2023-02-08 15:40:15 +00:00
|
|
|
new_output_device = p_name;
|
2018-06-21 00:02:02 +00:00
|
|
|
unlock();
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 12:44:34 +00:00
|
|
|
void AudioDriverPulseAudio::lock() {
|
2020-02-26 10:28:13 +00:00
|
|
|
mutex.lock();
|
2014-12-19 12:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverPulseAudio::unlock() {
|
2020-02-26 10:28:13 +00:00
|
|
|
mutex.unlock();
|
2014-12-19 12:44:34 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
void AudioDriverPulseAudio::finish_output_device() {
|
2018-03-25 03:43:51 +00:00
|
|
|
if (pa_str) {
|
|
|
|
pa_stream_disconnect(pa_str);
|
|
|
|
pa_stream_unref(pa_str);
|
2020-04-01 23:20:12 +00:00
|
|
|
pa_str = nullptr;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 12:44:34 +00:00
|
|
|
void AudioDriverPulseAudio::finish() {
|
2021-01-19 12:29:41 +00:00
|
|
|
if (!thread.is_started()) {
|
2014-12-19 12:44:34 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-12-19 12:44:34 +00:00
|
|
|
|
2022-08-19 08:38:13 +00:00
|
|
|
exit_thread.set();
|
2021-01-19 12:29:41 +00:00
|
|
|
thread.wait_to_finish();
|
2014-12-19 12:44:34 +00:00
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
finish_output_device();
|
2018-03-25 03:43:51 +00:00
|
|
|
|
|
|
|
if (pa_ctx) {
|
|
|
|
pa_context_disconnect(pa_ctx);
|
|
|
|
pa_context_unref(pa_ctx);
|
2020-04-01 23:20:12 +00:00
|
|
|
pa_ctx = nullptr;
|
2018-03-25 03:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pa_ml) {
|
|
|
|
pa_mainloop_free(pa_ml);
|
2020-04-01 23:20:12 +00:00
|
|
|
pa_ml = nullptr;
|
2017-08-29 19:47:44 +00:00
|
|
|
}
|
2014-12-19 12:44:34 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
Error AudioDriverPulseAudio::init_input_device() {
|
|
|
|
// If there is a specified input device, check that it is really present
|
|
|
|
if (input_device_name != "Default") {
|
|
|
|
PackedStringArray list = get_input_device_list();
|
|
|
|
if (list.find(input_device_name) == -1) {
|
|
|
|
input_device_name = "Default";
|
|
|
|
new_input_device = "Default";
|
2018-07-27 15:15:24 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-27 07:54:56 +00:00
|
|
|
|
2018-07-27 20:28:41 +00:00
|
|
|
detect_channels(true);
|
|
|
|
switch (pa_rec_map.channels) {
|
|
|
|
case 1: // Mono
|
|
|
|
case 2: // Stereo
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2019-11-07 08:44:15 +00:00
|
|
|
WARN_PRINT("PulseAudio: Unsupported number of input channels: " + itos(pa_rec_map.channels));
|
2018-07-27 20:28:41 +00:00
|
|
|
pa_channel_map_init_stereo(&pa_rec_map);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-11-08 23:44:11 +00:00
|
|
|
print_verbose("PulseAudio: detected " + itos(pa_rec_map.channels) + " input channels");
|
|
|
|
|
2018-07-04 01:08:43 +00:00
|
|
|
pa_sample_spec spec;
|
2018-02-27 07:54:56 +00:00
|
|
|
|
2018-07-04 01:08:43 +00:00
|
|
|
spec.format = PA_SAMPLE_S16LE;
|
2018-07-27 20:28:41 +00:00
|
|
|
spec.channels = pa_rec_map.channels;
|
2018-07-04 01:08:43 +00:00
|
|
|
spec.rate = mix_rate;
|
|
|
|
|
2018-10-19 19:32:03 +00:00
|
|
|
int input_latency = 30;
|
|
|
|
int input_buffer_frames = closest_power_of_2(input_latency * mix_rate / 1000);
|
|
|
|
int input_buffer_size = input_buffer_frames * spec.channels;
|
2018-02-27 07:54:56 +00:00
|
|
|
|
2018-07-04 01:08:43 +00:00
|
|
|
pa_buffer_attr attr;
|
2018-10-19 19:32:03 +00:00
|
|
|
attr.fragsize = input_buffer_size * sizeof(int16_t);
|
2018-07-04 01:08:43 +00:00
|
|
|
|
|
|
|
pa_rec_str = pa_stream_new(pa_ctx, "Record", &spec, &pa_rec_map);
|
2020-04-01 23:20:12 +00:00
|
|
|
if (pa_rec_str == nullptr) {
|
2019-11-06 16:03:04 +00:00
|
|
|
ERR_PRINT("PulseAudio: pa_stream_new error: " + String(pa_strerror(pa_context_errno(pa_ctx))));
|
2018-07-04 01:08:43 +00:00
|
|
|
ERR_FAIL_V(ERR_CANT_OPEN);
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
const char *dev = input_device_name == "Default" ? nullptr : input_device_name.utf8().get_data();
|
2018-07-04 01:08:43 +00:00
|
|
|
pa_stream_flags flags = pa_stream_flags(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE);
|
2018-07-27 15:15:24 +00:00
|
|
|
int error_code = pa_stream_connect_record(pa_rec_str, dev, &attr, flags);
|
2018-07-04 01:08:43 +00:00
|
|
|
if (error_code < 0) {
|
2019-11-06 16:03:04 +00:00
|
|
|
ERR_PRINT("PulseAudio: pa_stream_connect_record error: " + String(pa_strerror(error_code)));
|
2018-07-27 15:15:24 +00:00
|
|
|
ERR_FAIL_V(ERR_CANT_OPEN);
|
2018-07-04 01:08:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 12:11:47 +00:00
|
|
|
input_buffer_init(input_buffer_frames);
|
2018-10-19 19:32:03 +00:00
|
|
|
|
2020-01-20 12:11:47 +00:00
|
|
|
print_verbose("PulseAudio: detected " + itos(pa_rec_map.channels) + " input channels");
|
|
|
|
print_verbose("PulseAudio: input buffer frames: " + itos(input_buffer_frames) + " calculated latency: " + itos(input_buffer_frames * 1000 / mix_rate) + "ms");
|
2018-07-04 01:08:43 +00:00
|
|
|
|
2018-07-27 15:15:24 +00:00
|
|
|
return OK;
|
2018-02-27 07:54:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
void AudioDriverPulseAudio::finish_input_device() {
|
2018-07-04 01:08:43 +00:00
|
|
|
if (pa_rec_str) {
|
2018-07-27 15:15:24 +00:00
|
|
|
int ret = pa_stream_disconnect(pa_rec_str);
|
|
|
|
if (ret != 0) {
|
2019-11-06 16:03:04 +00:00
|
|
|
ERR_PRINT("PulseAudio: pa_stream_disconnect error: " + String(pa_strerror(ret)));
|
2018-07-27 15:15:24 +00:00
|
|
|
}
|
2018-07-04 01:08:43 +00:00
|
|
|
pa_stream_unref(pa_rec_str);
|
2020-04-01 23:20:12 +00:00
|
|
|
pa_rec_str = nullptr;
|
2018-07-04 01:08:43 +00:00
|
|
|
}
|
2018-07-27 15:15:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
Error AudioDriverPulseAudio::input_start() {
|
2018-07-27 15:15:24 +00:00
|
|
|
lock();
|
2022-11-24 14:41:40 +00:00
|
|
|
Error err = init_input_device();
|
2018-07-27 15:15:24 +00:00
|
|
|
unlock();
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
Error AudioDriverPulseAudio::input_stop() {
|
2018-07-27 15:15:24 +00:00
|
|
|
lock();
|
2022-11-24 14:41:40 +00:00
|
|
|
finish_input_device();
|
2018-07-27 15:15:24 +00:00
|
|
|
unlock();
|
2018-02-27 07:54:56 +00:00
|
|
|
|
2018-07-04 01:08:43 +00:00
|
|
|
return OK;
|
2018-02-27 07:54:56 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 15:15:24 +00:00
|
|
|
void AudioDriverPulseAudio::pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata) {
|
2022-04-05 10:40:26 +00:00
|
|
|
AudioDriverPulseAudio *ad = static_cast<AudioDriverPulseAudio *>(userdata);
|
2018-07-27 15:15:24 +00:00
|
|
|
|
|
|
|
// If eol is set to a positive number, you're at the end of the list
|
|
|
|
if (eol > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l->monitor_of_sink == PA_INVALID_INDEX) {
|
|
|
|
ad->pa_rec_devices.push_back(l->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
ad->pa_status++;
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
PackedStringArray AudioDriverPulseAudio::get_input_device_list() {
|
2018-07-27 15:15:24 +00:00
|
|
|
pa_rec_devices.clear();
|
|
|
|
pa_rec_devices.push_back("Default");
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
if (pa_ctx == nullptr) {
|
2018-07-27 15:15:24 +00:00
|
|
|
return pa_rec_devices;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock();
|
|
|
|
|
|
|
|
// Get the device list
|
|
|
|
pa_status = 0;
|
|
|
|
pa_operation *pa_op = pa_context_get_source_info_list(pa_ctx, pa_sourcelist_cb, (void *)this);
|
|
|
|
if (pa_op) {
|
|
|
|
while (pa_status == 0) {
|
2020-04-01 23:20:12 +00:00
|
|
|
int ret = pa_mainloop_iterate(pa_ml, 1, nullptr);
|
2018-07-27 15:15:24 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
ERR_PRINT("pa_mainloop_iterate error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_operation_unref(pa_op);
|
|
|
|
} else {
|
|
|
|
ERR_PRINT("pa_context_get_server_info error");
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock();
|
|
|
|
|
|
|
|
return pa_rec_devices;
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:41:40 +00:00
|
|
|
String AudioDriverPulseAudio::get_input_device() {
|
2018-07-27 15:15:24 +00:00
|
|
|
lock();
|
2022-11-24 14:41:40 +00:00
|
|
|
String name = input_device_name;
|
2018-07-27 15:15:24 +00:00
|
|
|
unlock();
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2023-02-08 15:40:15 +00:00
|
|
|
void AudioDriverPulseAudio::set_input_device(const String &p_name) {
|
|
|
|
lock();
|
|
|
|
new_input_device = p_name;
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
AudioDriverPulseAudio::AudioDriverPulseAudio() {
|
2017-08-22 21:27:17 +00:00
|
|
|
samples_in.clear();
|
|
|
|
samples_out.clear();
|
2014-12-19 12:44:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
#endif // PULSEAUDIO_ENABLED
|