fix(pulseaudio): fix get_name(), cleanup

This commit is contained in:
NBonaparte 2017-09-25 16:42:42 -07:00
parent 5c6b56614e
commit a08c5c710a
3 changed files with 20 additions and 19 deletions

View File

@ -55,10 +55,12 @@ class pulseaudio {
inline void wait_loop(pa_operation *op, pa_threaded_mainloop *loop); inline void wait_loop(pa_operation *op, pa_threaded_mainloop *loop);
// used for temporary callback results // used for temporary callback results
int success; int success{0};
pa_cvolume cv; pa_cvolume cv;
bool muted; bool muted{false};
bool exists; bool exists{false};
// default sink name
string def_s_name;
pa_context* m_context{nullptr}; pa_context* m_context{nullptr};
pa_threaded_mainloop* m_mainloop{nullptr}; pa_threaded_mainloop* m_mainloop{nullptr};
@ -67,8 +69,7 @@ class pulseaudio {
// specified sink name // specified sink name
string spec_s_name; string spec_s_name;
// default sink name string s_name;
string def_s_name;
uint32_t m_index{0}; uint32_t m_index{0};
}; };

View File

@ -47,8 +47,11 @@ pulseaudio::pulseaudio(string&& sink_name) : spec_s_name(sink_name) {
throw pulseaudio_error("Could not connect to pulseaudio server."); throw pulseaudio_error("Could not connect to pulseaudio server.");
} }
pa_operation* op = pa_context_get_sink_info_by_name(m_context, sink_name.c_str(), sink_info_callback, this); pa_operation *op{nullptr};
if (!sink_name.empty()) {
op = pa_context_get_sink_info_by_name(m_context, sink_name.c_str(), sink_info_callback, this);
wait_loop(op, m_mainloop); wait_loop(op, m_mainloop);
}
if (!exists) { if (!exists) {
op = pa_context_get_server_info(m_context, get_default_sink_callback, this); op = pa_context_get_server_info(m_context, get_default_sink_callback, this);
if (!op) { if (!op) {
@ -87,7 +90,7 @@ pulseaudio::~pulseaudio() {
* Get sink name * Get sink name
*/ */
const string& pulseaudio::get_name() { const string& pulseaudio::get_name() {
return spec_s_name; return s_name;
} }
/** /**
@ -112,8 +115,10 @@ int pulseaudio::process_events() {
// try to get specified sink // try to get specified sink
case evtype::NEW: case evtype::NEW:
// redundant if already using specified sink // redundant if already using specified sink
if (!spec_s_name.empty()) {
o = pa_context_get_sink_info_by_name(m_context, spec_s_name.c_str(), sink_info_callback, this); o = pa_context_get_sink_info_by_name(m_context, spec_s_name.c_str(), sink_info_callback, this);
wait_loop(o, m_mainloop); wait_loop(o, m_mainloop);
}
break; break;
// get volume // get volume
case evtype::CHANGE: case evtype::CHANGE:
@ -145,8 +150,6 @@ int pulseaudio::get_volume() {
pa_operation *op = pa_context_get_sink_info_by_index(m_context, m_index, get_sink_volume_callback, this); pa_operation *op = pa_context_get_sink_info_by_index(m_context, m_index, get_sink_volume_callback, this);
wait_loop(op, m_mainloop); wait_loop(op, m_mainloop);
pa_threaded_mainloop_unlock(m_mainloop); pa_threaded_mainloop_unlock(m_mainloop);
//if (!pa_cvolume_valid(&cv))
// throw pulseaudio_error("Failed to get volume.");
// alternatively, user pa_cvolume_avg_mask() to average selected channels // alternatively, user pa_cvolume_avg_mask() to average selected channels
return static_cast<int>(pa_cvolume_max(&cv) * 100.0f / PA_VOLUME_NORM + 0.5f); return static_cast<int>(pa_cvolume_max(&cv) * 100.0f / PA_VOLUME_NORM + 0.5f);
} }
@ -158,8 +161,6 @@ void pulseaudio::set_volume(float percentage) {
pa_threaded_mainloop_lock(m_mainloop); pa_threaded_mainloop_lock(m_mainloop);
pa_operation *op = pa_context_get_sink_info_by_index(m_context, m_index, get_sink_volume_callback, this); pa_operation *op = pa_context_get_sink_info_by_index(m_context, m_index, get_sink_volume_callback, this);
wait_loop(op, m_mainloop); wait_loop(op, m_mainloop);
//if (!pa_cvolume_valid(&cv))
// throw pulseaudio_error("Failed to get volume.");
pa_volume_t vol = math_util::percentage_to_value<pa_volume_t>(percentage, PA_VOLUME_MUTED, PA_VOLUME_NORM); pa_volume_t vol = math_util::percentage_to_value<pa_volume_t>(percentage, PA_VOLUME_MUTED, PA_VOLUME_NORM);
pa_cvolume_scale(&cv, vol); pa_cvolume_scale(&cv, vol);
op = pa_context_set_sink_volume_by_index(m_context, m_index, &cv, simple_callback, this); op = pa_context_set_sink_volume_by_index(m_context, m_index, &cv, simple_callback, this);
@ -177,8 +178,6 @@ void pulseaudio::inc_volume(int delta_perc) {
pa_threaded_mainloop_lock(m_mainloop); pa_threaded_mainloop_lock(m_mainloop);
pa_operation *op = pa_context_get_sink_info_by_index(m_context, m_index, get_sink_volume_callback, this); pa_operation *op = pa_context_get_sink_info_by_index(m_context, m_index, get_sink_volume_callback, this);
wait_loop(op, m_mainloop); wait_loop(op, m_mainloop);
//if (!pa_cvolume_valid(&cv))
// throw pulseaudio_error("Failed to get volume.");
pa_volume_t vol = math_util::percentage_to_value<pa_volume_t>(abs(delta_perc), PA_VOLUME_NORM); pa_volume_t vol = math_util::percentage_to_value<pa_volume_t>(abs(delta_perc), PA_VOLUME_NORM);
if (delta_perc > 0) if (delta_perc > 0)
pa_cvolume_inc(&cv, vol); pa_cvolume_inc(&cv, vol);
@ -282,9 +281,10 @@ void pulseaudio::simple_callback(pa_context *, int success, void *userdata) {
*/ */
void pulseaudio::get_default_sink_callback(pa_context *, const pa_server_info *info, void *userdata) { void pulseaudio::get_default_sink_callback(pa_context *, const pa_server_info *info, void *userdata) {
pulseaudio *This = static_cast<pulseaudio *>(userdata); pulseaudio *This = static_cast<pulseaudio *>(userdata);
if (info->default_sink_name) if (info->default_sink_name) {
This->def_s_name = info->default_sink_name; This->def_s_name = info->default_sink_name;
else This->s_name = info->default_sink_name;
} else
This->def_s_name = ""s; This->def_s_name = ""s;
pa_threaded_mainloop_signal(This->m_mainloop, 0); pa_threaded_mainloop_signal(This->m_mainloop, 0);
} }
@ -299,6 +299,7 @@ void pulseaudio::sink_info_callback(pa_context *, const pa_sink_info *info, int
else { else {
This->exists = true; This->exists = true;
This->m_index = info->index; This->m_index = info->index;
This->s_name = info->name;
} }
pa_threaded_mainloop_signal(This->m_mainloop, 0); pa_threaded_mainloop_signal(This->m_mainloop, 0);
} }

View File

@ -77,7 +77,6 @@ namespace modules {
auto sink_name = m_conf.get(name(), "sink", ""s); auto sink_name = m_conf.get(name(), "sink", ""s);
try { try {
m_pulseaudio = factory_util::unique<pulseaudio>(move(sink_name)); m_pulseaudio = factory_util::unique<pulseaudio>(move(sink_name));
//} catch (const pulseaudio_error& err) {
} catch (const pulseaudio_error& err) { } catch (const pulseaudio_error& err) {
throw module_error(err.what()); throw module_error(err.what());
} }