Fix A2DP Bluetooth audio routing, stream migration, hotplug monitoring, and q6voice teardown

This commit is contained in:
Luxferre committed 2026-11-04 15:42:58 +02:00
1 parent b2ade2dc42
commit b8dea31b59
34 files changed
+533 -155

No files matched your search

+55 -26
View File
@@ -765,17 +765,46 @@ class PcmAudioOut {
return sysMuted_;
}
static std::string currentOutDev() {
std::ifstream dfs("/tmp/sxmo_audio_out_device");
std::string d = "Speaker";
if (dfs) std::getline(dfs, d);
if (d.find("Bluetooth") != std::string::npos ||
d.find("bluetooth") != std::string::npos) {
return "Bluetooth";
}
if (d.find("Headphone") != std::string::npos ||
d.find("Headset") != std::string::npos) {
return "Headphones";
}
if (d.find("Earpiece") != std::string::npos) {
return "Earpiece";
}
return "Speaker";
}
static std::string volFileForOutDev(const std::string& cfgDir,
const std::string& outDev) {
if (outDev == "Bluetooth") return cfgDir + "/bluetooth_vol";
if (outDev == "Headphones") return cfgDir + "/headphones_vol";
if (outDev == "Earpiece") return cfgDir + "/earpiece_vol";
return cfgDir + "/speaker_vol";
}
void refreshVolumeIfNeeded(int64_t tNow) {
if (tNow - lastVolCheckMs_ < 500) return;
lastVolCheckMs_ = tNow;
const char* xdg = std::getenv("XDG_CONFIG_HOME");
std::string path =
((xdg && xdg[0]) ? (std::string(xdg) + "/sxmo")
: (getHomeDir() + "/.config/sxmo")) +
"/speaker_vol";
std::string cfgDir =
(xdg && xdg[0]) ? (std::string(xdg) + "/sxmo")
: (getHomeDir() + "/.config/sxmo");
std::string outDev = currentOutDev();
std::string path = volFileForOutDev(cfgDir, outDev);
struct stat st {};
if (::stat(path.c_str(), &st) == 0 &&
st.st_mtim.tv_sec != lastVolMtime_) {
if (outDev != lastOutDev_ ||
(::stat(path.c_str(), &st) == 0 &&
st.st_mtim.tv_sec != lastVolMtime_)) {
lastOutDev_ = outDev;
lastVolMtime_ = st.st_mtim.tv_sec;
refreshVolume(false);
}
@@ -788,10 +817,12 @@ class PcmAudioOut {
std::string cfgDir =
(xdg && xdg[0]) ? (std::string(xdg) + "/sxmo")
: (getHomeDir() + "/.config/sxmo");
std::string outDev = currentOutDev();
lastOutDev_ = outDev;
int pct = -1;
{
std::string volPath = cfgDir + "/speaker_vol";
std::string volPath = volFileForOutDev(cfgDir, outDev);
struct stat st {};
if (::stat(volPath.c_str(), &st) == 0) {
lastVolMtime_ = st.st_mtim.tv_sec;
@@ -805,7 +836,7 @@ class PcmAudioOut {
bool spkSwitch = true;
long rx3Cur = 84;
snd_ctl_t* ctl = nullptr;
if (snd_ctl_open(&ctl, "hw:0", 0) == 0 && ctl) {
if (outDev == "Speaker" && snd_ctl_open(&ctl, "hw:0", 0) == 0 && ctl) {
spkSwitch = readCtlBool(ctl, "SPK DAC Switch", true);
rx3Cur = readCtlInt(ctl, "RX3 Digital Volume", 84);
if (pct < 0 || pct > 100) {
@@ -828,12 +859,19 @@ class PcmAudioOut {
}
snd_ctl_close(ctl);
} else {
if (pct < 0 || pct > 100) pct = 75;
if (pct < 0 || pct > 100) pct = (outDev == "Bluetooth" || outDev == "Headphones") ? 80 : 85;
pct = std::clamp((pct / 5) * 5, 0, 100);
}
bool outMutedFile = false;
{
std::ifstream mfs("/tmp/sxmo_out_muted");
int m = 0;
if (mfs && (mfs >> m) && m == 1) outMutedFile = true;
}
speakerPct_ = pct;
sysMuted_ = !spkSwitch || (pct <= 0);
sysMuted_ = outMutedFile || (outDev == "Speaker" && !spkSwitch) || (pct <= 0);
if (sysMuted_) {
volScale_ = 0;
} else {
@@ -849,8 +887,9 @@ class PcmAudioOut {
(xdg && xdg[0]) ? (std::string(xdg) + "/sxmo")
: (getHomeDir() + "/.config/sxmo");
ensureDir(cfgDir);
std::string outDev = currentOutDev();
{
std::ofstream ofs(cfgDir + "/speaker_vol", std::ios::trunc);
std::ofstream ofs(volFileForOutDev(cfgDir, outDev), std::ios::trunc);
if (ofs) ofs << nextPct << "\n";
}
char cmd[128];
@@ -1026,27 +1065,16 @@ class PcmAudioOut {
for (int di = 0; di < 3; ++di) {
const char* dev = devs[di];
if (snd_pcm_open(&h, dev, SND_PCM_STREAM_PLAYBACK, 0) >= 0) {
if (configureLowLatency(h)) {
if (configureLowLatency(h) ||
snd_pcm_set_params(h, SND_PCM_FORMAT_S16_LE,
SND_PCM_ACCESS_RW_INTERLEAVED, 2, 48000, 1,
25000) >= 0) {
break;
}
snd_pcm_close(h);
h = nullptr;
}
}
if (!h) {
for (int di = 0; di < 3; ++di) {
const char* dev = devs[di];
if (snd_pcm_open(&h, dev, SND_PCM_STREAM_PLAYBACK, 0) >= 0) {
if (snd_pcm_set_params(h, SND_PCM_FORMAT_S16_LE,
SND_PCM_ACCESS_RW_INTERLEAVED, 2, 48000, 1,
25000) >= 0) {
break;
}
snd_pcm_close(h);
h = nullptr;
}
}
}
if (!h) {
threadRunning_.store(false, std::memory_order_release);
return;
@@ -1214,6 +1242,7 @@ class PcmAudioOut {
std::atomic<int32_t> volScale_{21};
int64_t lastVolCheckMs_ = 0;
time_t lastVolMtime_ = 0;
std::string lastOutDev_;
unsigned accumSamples_ = 0;
int32_t currentL_ = 0;
int32_t currentR_ = 0;
+58 -21
View File
@@ -83,17 +83,51 @@ class AlsaSoundEngine {
// Cheap enough to call from the mixer thread every 500 ms: it stats the
// file and only re-reads it when its mtime changes, and samples the two
// ALSA controls directly.
static std::string currentOutDev() {
std::string d = "Speaker";
FILE* f = std::fopen("/tmp/sxmo_audio_out_device", "r");
if (f) {
char buf[64] = {};
if (std::fgets(buf, sizeof(buf), f)) {
d = buf;
}
std::fclose(f);
}
if (d.find("Bluetooth") != std::string::npos ||
d.find("bluetooth") != std::string::npos) {
return "Bluetooth";
}
if (d.find("Headphone") != std::string::npos ||
d.find("Headset") != std::string::npos) {
return "Headphones";
}
if (d.find("Earpiece") != std::string::npos) {
return "Earpiece";
}
return "Speaker";
}
static std::string volFileForOutDev(const std::string& cfgDir,
const std::string& outDev) {
if (outDev == "Bluetooth") return cfgDir + "/bluetooth_vol";
if (outDev == "Headphones") return cfgDir + "/headphones_vol";
if (outDev == "Earpiece") return cfgDir + "/earpiece_vol";
return cfgDir + "/speaker_vol";
}
void refreshSystemVolume(bool force) {
std::lock_guard<std::mutex> lk(volMu_);
std::string path = sxmoConfigDir() + "/speaker_vol";
std::string outDev = currentOutDev();
std::string path = volFileForOutDev(sxmoConfigDir(), outDev);
struct stat st {};
bool haveFile = (::stat(path.c_str(), &st) == 0);
int64_t mtimeNs = haveFile ? (static_cast<int64_t>(st.st_mtim.tv_sec) *
1000000000LL +
st.st_mtim.tv_nsec)
: -1;
if (force || mtimeNs != lastVolMtimeNs_) {
if (force || mtimeNs != lastVolMtimeNs_ || outDev != lastOutDev_) {
lastVolMtimeNs_ = mtimeNs;
lastOutDev_ = outDev;
filePct_ = -1;
if (haveFile) {
FILE* f = std::fopen(path.c_str(), "r");
@@ -107,26 +141,26 @@ class AlsaSoundEngine {
}
}
bool spkOn = true;
long rx3 = 92;
snd_ctl_t* ctl = nullptr;
if (snd_ctl_open(&ctl, "hw:0", 0) == 0 && ctl) {
spkOn = readCtlBool(ctl, "SPK DAC Switch", true);
rx3 = readCtlInt(ctl, "RX3 Digital Volume", 92);
snd_ctl_close(ctl);
}
int pct = filePct_;
if (pct < 0) {
// Same RX3 -> percentage mapping as xdrawer (60..92 => 0..100%)
if (rx3 <= 0) {
pct = 0;
} else if (rx3 >= 92) {
pct = 100;
if (outDev == "Speaker") {
long rx3 = 92;
snd_ctl_t* ctl = nullptr;
if (snd_ctl_open(&ctl, "hw:0", 0) == 0 && ctl) {
rx3 = readCtlInt(ctl, "RX3 Digital Volume", 92);
snd_ctl_close(ctl);
}
if (rx3 <= 0) {
pct = 0;
} else if (rx3 >= 92) {
pct = 100;
} else {
pct = std::clamp(static_cast<int>(std::lround(((rx3 - 60) * 100.0) /
32.0)),
5, 100);
}
} else {
pct = std::clamp(static_cast<int>(std::lround(((rx3 - 60) * 100.0) /
32.0)),
5, 100);
pct = (outDev == "Bluetooth" || outDev == "Headphones") ? 80 : 85;
}
}
volumePct.store(pct, std::memory_order_relaxed);
@@ -141,8 +175,9 @@ class AlsaSoundEngine {
pct = std::clamp(pct, 0, 100);
std::string dir = sxmoConfigDir();
::mkdir(dir.c_str(), 0755);
std::string outDev = currentOutDev();
{
FILE* f = std::fopen((dir + "/speaker_vol").c_str(), "w");
FILE* f = std::fopen(volFileForOutDev(dir, outDev).c_str(), "w");
if (f) {
std::fprintf(f, "%d\n", pct);
std::fclose(f);
@@ -374,6 +409,7 @@ class AlsaSoundEngine {
// System speaker volume tracking (see refreshSystemVolume)
std::mutex volMu_;
int64_t lastVolMtimeNs_ = -2;
std::string lastOutDev_;
int filePct_ = -1;
std::atomic<bool> sysMuted_{false};
std::atomic<float> sysGain_{1.0f};
@@ -1163,7 +1199,8 @@ class AlsaSoundEngine {
}
if (!pcm) {
bool preferDefault = (::access("/run/bluetoothd.pid", F_OK) == 0);
bool preferDefault = (currentOutDev() == "Bluetooth") ||
(::access("/run/bluetoothd.pid", F_OK) == 0);
const char* devsHwFirst[] = {"hw:0,0", "default", "plughw:0,0"};
const char* devsDefFirst[] = {"default", "hw:0,0", "plughw:0,0"};
const char** devs = preferDefault ? devsDefFirst : devsHwFirst;