Commit 0a0659d7 authored by Max Kellermann's avatar Max Kellermann

mixer/Plugin: pass AudioOutput reference to init()

Passing a void pointer is unsafe.
parent b6df4680
......@@ -26,7 +26,7 @@
Mixer *
mixer_new(EventLoop &event_loop,
const MixerPlugin &plugin, void *ao,
const MixerPlugin &plugin, AudioOutput &ao,
const config_param &param,
Error &error)
{
......
......@@ -28,11 +28,12 @@
class Error;
class Mixer;
class EventLoop;
struct AudioOutput;
struct MixerPlugin;
struct config_param;
Mixer *
mixer_new(EventLoop &event_loop, const MixerPlugin &plugin, void *ao,
mixer_new(EventLoop &event_loop, const MixerPlugin &plugin, AudioOutput &ao,
const config_param &param,
Error &error);
......
......@@ -28,6 +28,7 @@
#define MPD_MIXER_PLUGIN_HXX
struct config_param;
struct AudioOutput;
class Mixer;
class EventLoop;
class Error;
......@@ -36,13 +37,13 @@ struct MixerPlugin {
/**
* Alocates and configures a mixer device.
*
* @param ao the pointer returned by AudioOutputPlugin.init
* @param ao the associated AudioOutput
* @param param the configuration section
* @param error_r location to store the error occurring, or
* nullptr to ignore errors
* @return a mixer object, or nullptr on error
*/
Mixer *(*init)(EventLoop &event_loop, void *ao,
Mixer *(*init)(EventLoop &event_loop, AudioOutput &ao,
const config_param &param,
Error &error);
......
......@@ -167,7 +167,7 @@ AlsaMixer::Configure(const config_param &param)
}
static Mixer *
alsa_mixer_init(EventLoop &event_loop, gcc_unused void *ao,
alsa_mixer_init(EventLoop &event_loop, gcc_unused AudioOutput &ao,
const config_param &param,
gcc_unused Error &error)
{
......
......@@ -97,7 +97,7 @@ OssMixer::Configure(const config_param &param, Error &error)
}
static Mixer *
oss_mixer_init(gcc_unused EventLoop &event_loop, gcc_unused void *ao,
oss_mixer_init(gcc_unused EventLoop &event_loop, gcc_unused AudioOutput &ao,
const config_param &param,
Error &error)
{
......
......@@ -35,13 +35,13 @@
#include <assert.h>
class PulseMixer final : public Mixer {
PulseOutput *output;
PulseOutput &output;
bool online;
struct pa_cvolume volume;
public:
PulseMixer(PulseOutput *_output)
PulseMixer(PulseOutput &_output)
:Mixer(pulse_mixer_plugin),
output(_output), online(false)
{
......@@ -130,7 +130,7 @@ PulseMixer::Update(pa_context *context, pa_stream *stream)
}
void
pulse_mixer_on_connect(gcc_unused PulseMixer *pm,
pulse_mixer_on_connect(gcc_unused PulseMixer &pm,
struct pa_context *context)
{
pa_operation *o;
......@@ -151,41 +151,34 @@ pulse_mixer_on_connect(gcc_unused PulseMixer *pm,
}
void
pulse_mixer_on_disconnect(PulseMixer *pm)
pulse_mixer_on_disconnect(PulseMixer &pm)
{
pm->Offline();
pm.Offline();
}
void
pulse_mixer_on_change(PulseMixer *pm,
pulse_mixer_on_change(PulseMixer &pm,
struct pa_context *context, struct pa_stream *stream)
{
pm->Update(context, stream);
pm.Update(context, stream);
}
static Mixer *
pulse_mixer_init(gcc_unused EventLoop &event_loop, void *ao,
pulse_mixer_init(gcc_unused EventLoop &event_loop, AudioOutput &ao,
gcc_unused const config_param &param,
Error &error)
gcc_unused Error &error)
{
PulseOutput *po = (PulseOutput *)ao;
if (ao == nullptr) {
error.Set(pulse_mixer_domain,
"The pulse mixer cannot work without the audio output");
return nullptr;
}
PulseOutput &po = (PulseOutput &)ao;
PulseMixer *pm = new PulseMixer(po);
pulse_output_set_mixer(po, pm);
pulse_output_set_mixer(po, *pm);
return pm;
}
PulseMixer::~PulseMixer()
{
pulse_output_clear_mixer(output, this);
pulse_output_clear_mixer(output, *this);
}
int
......
......@@ -25,12 +25,12 @@ struct pa_context;
struct pa_stream;
void
pulse_mixer_on_connect(PulseMixer *pm, pa_context *context);
pulse_mixer_on_connect(PulseMixer &pm, pa_context *context);
void
pulse_mixer_on_disconnect(PulseMixer *pm);
pulse_mixer_on_disconnect(PulseMixer &pm);
void
pulse_mixer_on_change(PulseMixer *pm, pa_context *context, pa_stream *stream);
pulse_mixer_on_change(PulseMixer &pm, pa_context *context, pa_stream *stream);
#endif
......@@ -26,10 +26,10 @@
class RoarMixer final : public Mixer {
/** the base mixer class */
RoarOutput *self;
RoarOutput &self;
public:
RoarMixer(RoarOutput *_output)
RoarMixer(RoarOutput &_output)
:Mixer(roar_mixer_plugin),
self(_output) {}
......@@ -46,11 +46,11 @@ public:
};
static Mixer *
roar_mixer_init(gcc_unused EventLoop &event_loop, void *ao,
roar_mixer_init(gcc_unused EventLoop &event_loop, AudioOutput &ao,
gcc_unused const config_param &param,
gcc_unused Error &error)
{
return new RoarMixer((RoarOutput *)ao);
return new RoarMixer((RoarOutput &)ao);
}
int
......
......@@ -84,7 +84,8 @@ public:
};
static Mixer *
software_mixer_init(gcc_unused EventLoop &event_loop, gcc_unused void *ao,
software_mixer_init(gcc_unused EventLoop &event_loop,
gcc_unused AudioOutput &ao,
gcc_unused const config_param &param,
gcc_unused Error &error)
{
......
......@@ -31,10 +31,10 @@
#include <windows.h>
class WinmmMixer final : public Mixer {
WinmmOutput *output;
WinmmOutput &output;
public:
WinmmMixer(WinmmOutput *_output)
WinmmMixer(WinmmOutput &_output)
:Mixer(winmm_mixer_plugin),
output(_output) {
}
......@@ -67,13 +67,11 @@ winmm_volume_encode(int volume)
}
static Mixer *
winmm_mixer_init(gcc_unused EventLoop &event_loop, void *ao,
winmm_mixer_init(gcc_unused EventLoop &event_loop, AudioOutput &ao,
gcc_unused const config_param &param,
gcc_unused Error &error)
{
assert(ao != nullptr);
return new WinmmMixer((WinmmOutput *)ao);
return new WinmmMixer((WinmmOutput &)ao);
}
int
......
......@@ -129,10 +129,10 @@ audio_output_load_mixer(EventLoop &event_loop, AudioOutput *ao,
if (plugin == nullptr)
return nullptr;
return mixer_new(event_loop, *plugin, ao, param, error);
return mixer_new(event_loop, *plugin, *ao, param, error);
case MIXER_TYPE_SOFTWARE:
mixer = mixer_new(event_loop, software_mixer_plugin, nullptr,
mixer = mixer_new(event_loop, software_mixer_plugin, *ao,
config_param(),
IgnoreError());
assert(mixer != nullptr);
......
......@@ -70,70 +70,66 @@ SetError(Error &error, pa_context *context, const char *msg)
}
void
pulse_output_lock(PulseOutput *po)
pulse_output_lock(PulseOutput &po)
{
pa_threaded_mainloop_lock(po->mainloop);
pa_threaded_mainloop_lock(po.mainloop);
}
void
pulse_output_unlock(PulseOutput *po)
pulse_output_unlock(PulseOutput &po)
{
pa_threaded_mainloop_unlock(po->mainloop);
pa_threaded_mainloop_unlock(po.mainloop);
}
void
pulse_output_set_mixer(PulseOutput *po, PulseMixer *pm)
pulse_output_set_mixer(PulseOutput &po, PulseMixer &pm)
{
assert(po != nullptr);
assert(po->mixer == nullptr);
assert(pm != nullptr);
assert(po.mixer == nullptr);
po->mixer = pm;
po.mixer = &pm;
if (po->mainloop == nullptr)
if (po.mainloop == nullptr)
return;
pa_threaded_mainloop_lock(po->mainloop);
pa_threaded_mainloop_lock(po.mainloop);
if (po->context != nullptr &&
pa_context_get_state(po->context) == PA_CONTEXT_READY) {
pulse_mixer_on_connect(pm, po->context);
if (po.context != nullptr &&
pa_context_get_state(po.context) == PA_CONTEXT_READY) {
pulse_mixer_on_connect(pm, po.context);
if (po->stream != nullptr &&
pa_stream_get_state(po->stream) == PA_STREAM_READY)
pulse_mixer_on_change(pm, po->context, po->stream);
if (po.stream != nullptr &&
pa_stream_get_state(po.stream) == PA_STREAM_READY)
pulse_mixer_on_change(pm, po.context, po.stream);
}
pa_threaded_mainloop_unlock(po->mainloop);
pa_threaded_mainloop_unlock(po.mainloop);
}
void
pulse_output_clear_mixer(PulseOutput *po, gcc_unused PulseMixer *pm)
pulse_output_clear_mixer(PulseOutput &po, gcc_unused PulseMixer &pm)
{
assert(po != nullptr);
assert(pm != nullptr);
assert(po->mixer == pm);
assert(po.mixer == &pm);
po->mixer = nullptr;
po.mixer = nullptr;
}
bool
pulse_output_set_volume(PulseOutput *po, const pa_cvolume *volume,
pulse_output_set_volume(PulseOutput &po, const pa_cvolume *volume,
Error &error)
{
pa_operation *o;
if (po->context == nullptr || po->stream == nullptr ||
pa_stream_get_state(po->stream) != PA_STREAM_READY) {
if (po.context == nullptr || po.stream == nullptr ||
pa_stream_get_state(po.stream) != PA_STREAM_READY) {
error.Set(pulse_output_domain, "disconnected");
return false;
}
o = pa_context_set_sink_input_volume(po->context,
pa_stream_get_index(po->stream),
o = pa_context_set_sink_input_volume(po.context,
pa_stream_get_index(po.stream),
volume, nullptr, nullptr);
if (o == nullptr) {
SetError(error, po->context,
SetError(error, po.context,
"failed to set PulseAudio volume");
return false;
}
......@@ -190,7 +186,7 @@ pulse_output_context_state_cb(struct pa_context *context, void *userdata)
switch (pa_context_get_state(context)) {
case PA_CONTEXT_READY:
if (po->mixer != nullptr)
pulse_mixer_on_connect(po->mixer, context);
pulse_mixer_on_connect(*po->mixer, context);
pa_threaded_mainloop_signal(po->mainloop, 0);
break;
......@@ -198,7 +194,7 @@ pulse_output_context_state_cb(struct pa_context *context, void *userdata)
case PA_CONTEXT_TERMINATED:
case PA_CONTEXT_FAILED:
if (po->mixer != nullptr)
pulse_mixer_on_disconnect(po->mixer);
pulse_mixer_on_disconnect(*po->mixer);
/* the caller thread might be waiting for these
states */
......@@ -231,7 +227,7 @@ pulse_output_subscribe_cb(pa_context *context,
idx == pa_stream_get_index(po->stream) &&
(type == PA_SUBSCRIPTION_EVENT_NEW ||
type == PA_SUBSCRIPTION_EVENT_CHANGE))
pulse_mixer_on_change(po->mixer, context, po->stream);
pulse_mixer_on_change(*po->mixer, context, po->stream);
}
/**
......@@ -487,7 +483,7 @@ pulse_output_stream_state_cb(pa_stream *stream, void *userdata)
switch (pa_stream_get_state(stream)) {
case PA_STREAM_READY:
if (po->mixer != nullptr)
pulse_mixer_on_change(po->mixer, po->context, stream);
pulse_mixer_on_change(*po->mixer, po->context, stream);
pa_threaded_mainloop_signal(po->mainloop, 0);
break;
......@@ -495,7 +491,7 @@ pulse_output_stream_state_cb(pa_stream *stream, void *userdata)
case PA_STREAM_FAILED:
case PA_STREAM_TERMINATED:
if (po->mixer != nullptr)
pulse_mixer_on_disconnect(po->mixer);
pulse_mixer_on_disconnect(*po->mixer);
pa_threaded_mainloop_signal(po->mainloop, 0);
break;
......
......@@ -28,19 +28,19 @@ class Error;
extern const struct AudioOutputPlugin pulse_output_plugin;
void
pulse_output_lock(PulseOutput *po);
pulse_output_lock(PulseOutput &po);
void
pulse_output_unlock(PulseOutput *po);
pulse_output_unlock(PulseOutput &po);
void
pulse_output_set_mixer(PulseOutput *po, PulseMixer *pm);
pulse_output_set_mixer(PulseOutput &po, PulseMixer &pm);
void
pulse_output_clear_mixer(PulseOutput *po, PulseMixer *pm);
pulse_output_clear_mixer(PulseOutput &po, PulseMixer &pm);
bool
pulse_output_set_volume(PulseOutput *po,
pulse_output_set_volume(PulseOutput &po,
const pa_cvolume *volume, Error &error);
#endif
......@@ -93,9 +93,9 @@ RoarOutput::GetVolume() const
}
int
roar_output_get_volume(RoarOutput *roar)
roar_output_get_volume(RoarOutput &roar)
{
return roar->GetVolume();
return roar.GetVolume();
}
bool
......@@ -115,9 +115,9 @@ RoarOutput::SetVolume(unsigned volume)
}
bool
roar_output_set_volume(RoarOutput *roar, unsigned volume)
roar_output_set_volume(RoarOutput &roar, unsigned volume)
{
return roar->SetVolume(volume);
return roar.SetVolume(volume);
}
inline void
......
......@@ -25,9 +25,9 @@ class RoarOutput;
extern const struct AudioOutputPlugin roar_output_plugin;
int
roar_output_get_volume(RoarOutput *roar);
roar_output_get_volume(RoarOutput &roar);
bool
roar_output_set_volume(RoarOutput *roar, unsigned volume);
roar_output_set_volume(RoarOutput &roar, unsigned volume);
#endif
......@@ -59,9 +59,9 @@ struct WinmmOutput {
static constexpr Domain winmm_output_domain("winmm_output");
HWAVEOUT
winmm_output_get_handle(WinmmOutput *output)
winmm_output_get_handle(WinmmOutput &output)
{
return output->handle;
return output.handle;
}
static bool
......
......@@ -35,7 +35,7 @@ extern const struct AudioOutputPlugin winmm_output_plugin;
gcc_pure
HWAVEOUT
winmm_output_get_handle(WinmmOutput *);
winmm_output_get_handle(WinmmOutput &output);
#endif
......
......@@ -35,59 +35,6 @@
#include <string.h>
#include <unistd.h>
#ifdef HAVE_PULSE
#include "output/plugins/PulseOutputPlugin.hxx"
void
pulse_output_lock(gcc_unused PulseOutput *po)
{
}
void
pulse_output_unlock(gcc_unused PulseOutput *po)
{
}
void
pulse_output_set_mixer(gcc_unused PulseOutput *po,
gcc_unused PulseMixer *pm)
{
}
void
pulse_output_clear_mixer(gcc_unused PulseOutput *po,
gcc_unused PulseMixer *pm)
{
}
bool
pulse_output_set_volume(gcc_unused PulseOutput *po,
gcc_unused const struct pa_cvolume *volume,
gcc_unused Error &error)
{
return false;
}
#endif
#ifdef HAVE_ROAR
#include "output/plugins/RoarOutputPlugin.hxx"
int
roar_output_get_volume(gcc_unused RoarOutput *roar)
{
return -1;
}
bool
roar_output_set_volume(gcc_unused RoarOutput *roar,
gcc_unused unsigned volume)
{
return true;
}
#endif
void
GlobalEvents::Emit(gcc_unused Event event)
{
......@@ -116,7 +63,8 @@ int main(int argc, gcc_unused char **argv)
EventLoop event_loop;
Error error;
Mixer *mixer = mixer_new(event_loop, alsa_mixer_plugin, nullptr,
Mixer *mixer = mixer_new(event_loop, alsa_mixer_plugin,
*(AudioOutput *)nullptr,
config_param(), error);
if (mixer == NULL) {
LogError(error, "mixer_new() failed");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment