PipeWire device selection and persistence by ID. TODO: still need to resolve the name.

This commit is contained in:
casey langen 2021-03-18 00:41:41 -07:00
parent 0a0a192bbf
commit c8867a3b96
2 changed files with 35 additions and 4 deletions

View File

@ -47,6 +47,7 @@
constexpr size_t SAMPLES_PER_BUFFER = 2048;
constexpr size_t SAMPLE_SIZE_BYTES = sizeof(float);
constexpr size_t MAX_BUFFERS = 16;
constexpr const char* PREF_DEVICE_ID = "device_id";
static std::atomic<bool> pipeWireInitialized(false);
@ -56,6 +57,8 @@ static IPreferences* prefs = nullptr;
extern "C" void SetPreferences(IPreferences* prefs) {
::prefs = prefs;
prefs->GetString(PREF_DEVICE_ID, nullptr, 0, "");
prefs->Save();
}
extern "C" musik::core::sdk::ISchema* GetSchema() {
@ -63,6 +66,10 @@ extern "C" musik::core::sdk::ISchema* GetSchema() {
return schema;
}
static std::string getDeviceId() {
return getPreferenceString<std::string>(prefs, PREF_DEVICE_ID, "");
}
void PipeWireOut::OnStreamStateChanged(void* data, enum pw_stream_state old, enum pw_stream_state state, const char* error) {
std::cerr << "[PipeWire] state changed from " << old << " to " << state << "\n";
}
@ -249,13 +256,16 @@ IDeviceList* PipeWireOut::GetDeviceList() {
}
bool PipeWireOut::SetDefaultDevice(const char* deviceId) {
/* CAL TODO */
return false;
if (getDeviceId() != deviceId) {
setDefaultDevice<IPreferences, Device, IOutput>(prefs, this, PREF_DEVICE_ID, deviceId);
}
return true;
}
IDevice* PipeWireOut::GetDefaultDevice() {
std::unique_lock<std::recursive_mutex> lock(this->mutex);
return this->deviceList.Default();
this->RefreshDeviceList();
return this->deviceList.ResolveDevice(getDeviceId());
}
void PipeWireOut::StopPipeWire() {
@ -350,7 +360,7 @@ bool PipeWireOut::StartPipeWire(IBuffer* buffer) {
result = pw_stream_connect(
this->pwStream,
PW_DIRECTION_OUTPUT,
PW_ID_ANY,
this->deviceList.ResolveId(getDeviceId()),
streamFlags,
params,
1);

View File

@ -213,6 +213,27 @@ class PipeWireOut : public IOutput {
void Reset() {
this->devices.clear();
}
uint32_t ResolveId(const std::string& id) {
for (auto device: this->devices) {
if (device.Id() == id) {
try {
return (uint32_t) std::stoi(id);
}
catch(...) {
/* return default below... */
}
}
}
return PW_ID_ANY;
}
Device* ResolveDevice(const std::string& id) {
for (auto device: this->devices) {
if (device.Id() == id) {
return device.Clone();
}
}
return nullptr;
}
DeviceList* Clone() {
auto result = new DeviceList();
result->devices = this->devices;