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

+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;