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;