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

+2 -13
View File
@@ -561,7 +561,7 @@ int callscreen_run_main(int argc, char** argv) {
alsa.setBoolean("PRI_MI2S_RX Voice Mixer VoiceMMode1", false);
alsa.setBoolean("VoiceMMode1 Capture Mixer TERT_MI2S_TX", false);
alsa.setBoolean("INT_BT_SCO_RX Voice Mixer VoiceMMode1", true);
alsa.setBoolean("VoiceMMode1 Capture Mixer INT_BT_SCO_TX", !micMuted);
alsa.setBoolean("VoiceMMode1 Capture Mixer INT_BT_SCO_TX", true);
} else {
alsa.setBoolean("PRI_MI2S_RX Audio Mixer MultiMedia1", false);
alsa.setBoolean("MultiMedia2 Mixer TERT_MI2S_TX", false);
@@ -589,7 +589,7 @@ int callscreen_run_main(int argc, char** argv) {
alsa.setBoolean("INT_BT_SCO_RX Voice Mixer VoiceMMode1", false);
alsa.setBoolean("VoiceMMode1 Capture Mixer INT_BT_SCO_TX", false);
alsa.setBoolean("PRI_MI2S_RX Voice Mixer VoiceMMode1", true);
alsa.setBoolean("VoiceMMode1 Capture Mixer TERT_MI2S_TX", !micMuted);
alsa.setBoolean("VoiceMMode1 Capture Mixer TERT_MI2S_TX", true);
} else {
alsa.setBoolean("INT_BT_SCO_RX Audio Mixer MultiMedia1", false);
alsa.setBoolean("MultiMedia2 Mixer INT_BT_SCO_TX", false);
@@ -1412,17 +1412,6 @@ int callscreen_run_main(int argc, char** argv) {
writeActiveStateFiles();
stopRingingJobs();
btAudioOn = false;
alsa.setBoolean("INT_BT_SCO_RX Voice Mixer VoiceMMode1", false);
alsa.setBoolean("VoiceMMode1 Capture Mixer INT_BT_SCO_TX", false);
alsa.setBoolean("PRI_MI2S_RX Voice Mixer VoiceMMode1", false);
alsa.setBoolean("VoiceMMode1 Capture Mixer TERT_MI2S_TX", false);
alsa.setEnum("EAR_S", 0);
alsa.setEnum("RX1 MIX1 INP1", 0);
alsa.setInteger("RX1 Digital Volume", 0);
alsa.setEnum("RX3 MIX1 INP1", 3);
alsa.setBoolean("SPK DAC Switch", true);
int spkPct = readIntFile(getHomeDirPath() + "/.config/sxmo/speaker_vol", 85);
alsa.setInteger("RX3 Digital Volume", pctToHwVol(spkPct, true, false));
runAsyncCmd("sxmo_jobs.sh stop ringing >/dev/null 2>&1; "
"pkill -TERM -f sxmo_hook_ring.sh >/dev/null 2>&1; "
"pkill -TERM -f 'pw-play.*ring' >/dev/null 2>&1; "
+24 -9
View File
@@ -824,15 +824,30 @@ int runFilesApp(int argc, char** argv) {
int rvol = std::clamp(readRingVolumePct(), 0, 100);
int rx3 = (rvol <= 0) ? 0 : (56 + (rvol * 32 + 50) / 100);
runAsyncCmd("amixer -c 0 cset \"name='EAR_S'\" 0 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='RX1 MIX1 INP1'\" 0 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='PRI_MI2S_RX Audio Mixer MultiMedia1'\" 1 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='SPK DAC Switch'\" 1 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='RX3 MIX1 INP1'\" 3 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='RX3 Digital Volume'\" " +
std::to_string(rx3) + " >/dev/null 2>&1; "
"wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 >/dev/null 2>&1; "
"wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1");
int hp = (rvol <= 0) ? 0 : (52 + (rvol * 32 + 50) / 100);
runAsyncCmd("outdev=$(cat /tmp/sxmo_audio_out_device 2>/dev/null); "
"if [ \"$outdev\" = \"Bluetooth\" ]; then "
" amixer -c 0 cset \"name='SPK DAC Switch'\" 0 >/dev/null 2>&1; "
" wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 >/dev/null 2>&1; "
" wpctl set-volume @DEFAULT_AUDIO_SINK@ " + std::to_string(rvol) + "% >/dev/null 2>&1; "
"elif [ \"$outdev\" = \"Headphones\" ]; then "
" amixer -c 0 cset \"name='SPK DAC Switch'\" 0 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='EAR_S'\" 0 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='PRI_MI2S_RX Audio Mixer MultiMedia1'\" 1 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='RX1 Digital Volume'\" " + std::to_string(hp) + " >/dev/null 2>&1; "
" amixer -c 0 cset \"name='RX2 Digital Volume'\" " + std::to_string(hp) + " >/dev/null 2>&1; "
" wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 >/dev/null 2>&1; "
" wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1; "
"else "
" amixer -c 0 cset \"name='EAR_S'\" 0 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='RX1 MIX1 INP1'\" 0 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='PRI_MI2S_RX Audio Mixer MultiMedia1'\" 1 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='SPK DAC Switch'\" 1 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='RX3 MIX1 INP1'\" 3 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='RX3 Digital Volume'\" " + std::to_string(rx3) + " >/dev/null 2>&1; "
" wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 >/dev/null 2>&1; "
" wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1; "
"fi");
pid_t pid = ::fork();
if (pid == 0) {
+16 -7
View File
@@ -266,7 +266,12 @@ int readSavedSpeakerVol() {
}
std::string vfile = "/speaker_vol";
int defv = 85;
if (outDev.find("Headphone") != std::string::npos || outDev.find("Headset") != std::string::npos) {
if (outDev.find("Bluetooth") != std::string::npos ||
outDev.find("bluetooth") != std::string::npos) {
vfile = "/bluetooth_vol";
defv = 80;
} else if (outDev.find("Headphone") != std::string::npos ||
outDev.find("Headset") != std::string::npos) {
vfile = "/headphones_vol";
defv = 80;
} else if (outDev.find("Earpiece") != std::string::npos) {
@@ -502,10 +507,14 @@ int runMusicApp(int argc, char** argv) {
isPaused = false;
ignoreEofUntilMs = nowMs() + 450;
runAsyncCmd("amixer -c 0 cset \"name='EAR_S'\" 0 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='SPK DAC Switch'\" 1 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='RX3 MIX1 INP1'\" 3 >/dev/null 2>&1; "
"wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 >/dev/null 2>&1");
runAsyncCmd("sxmo_audio.sh device apply >/dev/null 2>&1; "
"(sleep 0.25; "
"sink=$(pactl get-default-sink 2>/dev/null); "
"if [ -n \"$sink\" ]; then "
" for sid in $(pactl list short sink-inputs 2>/dev/null | awk '{print $1}'); do "
" pactl move-sink-input \"$sid\" \"$sink\" >/dev/null 2>&1 || true; "
" done; "
"fi) >/dev/null 2>&1 &");
std::string loadTarget = tr.path;
if (isMonotoneFile(tr.path)) {
@@ -1333,11 +1342,11 @@ int runMusicApp(int argc, char** argv) {
} else if (sym == XK_Right || sym == XK_KP_Right) {
seekRelative(5);
} else if (sym == XK_Up || sym == XK_KP_Up) {
volumePct = std::clamp(volumePct + 5, 0, 100);
volumePct = std::clamp(readSavedSpeakerVol() + 5, 0, 100);
applySystemSpeakerVol(volumePct);
setBanner("Volume: " + std::to_string(volumePct) + "%", 1200);
} else if (sym == XK_Down || sym == XK_KP_Down) {
volumePct = std::clamp(volumePct - 5, 0, 100);
volumePct = std::clamp(readSavedSpeakerVol() - 5, 0, 100);
applySystemSpeakerVol(volumePct);
setBanner("Volume: " + std::to_string(volumePct) + "%", 1200);
} else if (dch == '4') {
+8 -6
View File
@@ -581,8 +581,8 @@ std::vector<BtDeviceEntry> queryBtDevices() {
d.profileLabel = "HSP/HFP";
} else if (hasPaSink) {
d.profileLabel = "A2DP";
} else if (hasA2dp && hasHfp) {
d.profileLabel = "A2DP+HFP";
} else if (hasA2dp) {
d.profileLabel = "A2DP";
} else if (hasHfp) {
d.profileLabel = "HSP/HFP";
} else {
@@ -1561,8 +1561,9 @@ int runNetworksApp(int argc, char** argv) {
}
}
if (isConn && targetBt.kind == BtDevKind::Audio) {
runCaptureCmd("sxmo_audio.sh device set Bluetooth >/dev/null 2>&1");
refreshCurrentTab(false);
std::ofstream ofs("/tmp/sxmo_audio_out_device", std::ios::trunc);
if (ofs) ofs << "Bluetooth\n";
runAsyncCmd("sxmo_audio.sh device set Bluetooth >/dev/null 2>&1");
}
setBanner(isConn ? ("Connected: " + targetBt.name) : "Connect failed");
} else if (act == 1) {
@@ -1592,8 +1593,9 @@ int runNetworksApp(int argc, char** argv) {
}
}
if (isPairedOrConn && targetBt.kind == BtDevKind::Audio) {
runCaptureCmd("sxmo_audio.sh device set Bluetooth >/dev/null 2>&1");
refreshCurrentTab(false);
std::ofstream ofs("/tmp/sxmo_audio_out_device", std::ios::trunc);
if (ofs) ofs << "Bluetooth\n";
runAsyncCmd("sxmo_audio.sh device set Bluetooth >/dev/null 2>&1");
}
setBanner(isPairedOrConn ? ("Paired & connected: " + targetBt.name)
: ("Pairing failed: " + targetBt.name));
+39 -15
View File
@@ -500,20 +500,35 @@ int runRingtonesApp(int argc, char** argv) {
settings = loadRingSettings();
int rvol = std::clamp(settings.ringVol, 0, 100);
int rx3 = (rvol <= 0) ? 0 : (56 + (rvol * 32 + 50) / 100);
int hp = (rvol <= 0) ? 0 : (52 + (rvol * 32 + 50) / 100);
std::string volArg = "--volume=100";
std::string loopArg = singleShot ? "--loop=no" : "--loop=2";
runAsyncCmd("amixer -c 0 cset \"name='EAR_S'\" 0 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='RX1 MIX1 INP1'\" 0 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='PRI_MI2S_RX Audio Mixer MultiMedia1'\" 1 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='SPK DAC Switch'\" 1 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='RX3 MIX1 INP1'\" 3 >/dev/null 2>&1; "
"amixer -c 0 cset \"name='RX3 Digital Volume'\" " +
runAsyncCmd("outdev=$(cat /tmp/sxmo_audio_out_device 2>/dev/null); "
"if [ \"$outdev\" = \"Bluetooth\" ]; then "
" amixer -c 0 cset \"name='SPK DAC Switch'\" 0 >/dev/null 2>&1; "
" wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 >/dev/null 2>&1; "
" wpctl set-volume @DEFAULT_AUDIO_SINK@ " + std::to_string(rvol) + "% >/dev/null 2>&1; "
"elif [ \"$outdev\" = \"Headphones\" ]; then "
" amixer -c 0 cset \"name='SPK DAC Switch'\" 0 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='EAR_S'\" 0 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='PRI_MI2S_RX Audio Mixer MultiMedia1'\" 1 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='RX1 Digital Volume'\" " + std::to_string(hp) + " >/dev/null 2>&1; "
" amixer -c 0 cset \"name='RX2 Digital Volume'\" " + std::to_string(hp) + " >/dev/null 2>&1; "
" wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 >/dev/null 2>&1; "
" wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1; "
"else "
" amixer -c 0 cset \"name='EAR_S'\" 0 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='RX1 MIX1 INP1'\" 0 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='PRI_MI2S_RX Audio Mixer MultiMedia1'\" 1 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='SPK DAC Switch'\" 1 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='RX3 MIX1 INP1'\" 3 >/dev/null 2>&1; "
" amixer -c 0 cset \"name='RX3 Digital Volume'\" " + std::to_string(rx3) + " >/dev/null 2>&1; "
" wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 >/dev/null 2>&1; "
" wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1; "
" (sleep 0.25; amixer -c 0 cset \"name='RX3 Digital Volume'\" " +
std::to_string(rx3) + " >/dev/null 2>&1; "
"wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 >/dev/null 2>&1; "
"wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1; "
"(sleep 0.25; amixer -c 0 cset \"name='RX3 Digital Volume'\" " +
std::to_string(rx3) + " >/dev/null 2>&1; "
"wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1) &");
" wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1) & "
"fi");
pid_t pid = ::fork();
if (pid == 0) {
std::string runtimeDir = "/run/user/" + std::to_string(::getuid());
@@ -981,10 +996,19 @@ int runRingtonesApp(int argc, char** argv) {
settings.ringVol = std::clamp(settings.ringVol + delta * 5, 0, 100);
saveRingSettings(settings);
if (previewPid > 0) {
int rx3 = (settings.ringVol <= 0) ? 0 : (56 + (settings.ringVol * 32 + 50) / 100);
runAsyncCmd("amixer -c 0 cset \"name='RX3 Digital Volume'\" " +
std::to_string(rx3) + " >/dev/null 2>&1; "
"wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1");
int rvol = settings.ringVol;
int rx3 = (rvol <= 0) ? 0 : (56 + (rvol * 32 + 50) / 100);
int hp = (rvol <= 0) ? 0 : (52 + (rvol * 32 + 50) / 100);
runAsyncCmd("outdev=$(cat /tmp/sxmo_audio_out_device 2>/dev/null); "
"if [ \"$outdev\" = \"Bluetooth\" ]; then "
" wpctl set-volume @DEFAULT_AUDIO_SINK@ " + std::to_string(rvol) + "% >/dev/null 2>&1; "
"elif [ \"$outdev\" = \"Headphones\" ]; then "
" amixer -c 0 cset \"name='RX1 Digital Volume'\" " + std::to_string(hp) + " >/dev/null 2>&1; "
" amixer -c 0 cset \"name='RX2 Digital Volume'\" " + std::to_string(hp) + " >/dev/null 2>&1; "
"else "
" amixer -c 0 cset \"name='RX3 Digital Volume'\" " + std::to_string(rx3) + " >/dev/null 2>&1; "
" wpctl set-volume @DEFAULT_AUDIO_SINK@ 100% >/dev/null 2>&1; "
"fi");
}
setBanner("Ring volume: " + std::to_string(settings.ringVol) + "%");
} else if (settingsRow == 4) {
+6 -5
View File
@@ -809,15 +809,16 @@ void ensureAlsaMultimediaRouting() {
runAsyncCmd(
"outdev=$(cat /tmp/sxmo_audio_out_device 2>/dev/null); "
"if [ \"$outdev\" = \"Earpiece\" ] || [ -z \"$outdev\" ]; then "
" if [ \"$(cat /tmp/sxmo_jack_plugged 2>/dev/null)\" = \"1\" ]; then "
" if [ -n \"$(pactl list short sinks 2>/dev/null | awk '$2 ~ /^bluez_output\\./ { print $2; exit }')\" ]; then "
" sxmo_audio.sh out set Bluetooth >/dev/null 2>&1; "
" elif [ \"$(cat /tmp/sxmo_jack_plugged 2>/dev/null)\" = \"1\" ]; then "
" sxmo_audio.sh out set Headphones >/dev/null 2>&1; "
" else "
" sxmo_audio.sh out set Speaker >/dev/null 2>&1; "
" fi; "
"fi; "
"amixer -q -c 0 cset \"name='PRI_MI2S_RX Audio Mixer MultiMedia1'\" 1 >/dev/null 2>&1; "
"amixer -q -c 0 cset \"name='RX3 MIX1 INP1'\" 'RX1' >/dev/null 2>&1; "
"amixer -q -c 0 cset \"name='SPK DAC Switch'\" 1 >/dev/null 2>&1");
"else "
" sxmo_audio.sh device apply >/dev/null 2>&1; "
"fi");
}
bool initBrowserEngine(BrowserRuntimeState& st, const std::string& userAgent) {
+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;