Commit 667481c3 authored by Max Kellermann's avatar Max Kellermann

OutputThread: move more functions into the AudioOutput class

parent af716057
...@@ -368,6 +368,51 @@ struct AudioOutput { ...@@ -368,6 +368,51 @@ struct AudioOutput {
* Set the "allow_play" and signal the thread. * Set the "allow_play" and signal the thread.
*/ */
void LockAllowPlay(); void LockAllowPlay();
private:
void CommandFinished();
bool Enable();
void Disable();
void Open();
void Close(bool drain);
void Reopen();
AudioFormat OpenFilter(AudioFormat &format, Error &error_r);
void CloseFilter();
void ReopenFilter();
/**
* Wait until the output's delay reaches zero.
*
* @return true if playback should be continued, false if a
* command was issued
*/
bool WaitForDelay();
gcc_pure
const music_chunk *GetNextChunk() const;
bool PlayChunk(const music_chunk *chunk);
/**
* Plays all remaining chunks, until the tail of the pipe has
* been reached (and no more chunks are queued), or until a
* command is received.
*
* @return true if at least one chunk has been available,
* false if the tail of the pipe was already reached
*/
bool Play();
void Pause();
/**
* The OutputThread.
*/
void Task();
static void Task(void *arg);
}; };
/** /**
......
...@@ -39,254 +39,249 @@ ...@@ -39,254 +39,249 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
static void ao_command_finished(AudioOutput *ao) void
AudioOutput::CommandFinished()
{ {
assert(ao->command != AO_COMMAND_NONE); assert(command != AO_COMMAND_NONE);
ao->command = AO_COMMAND_NONE; command = AO_COMMAND_NONE;
ao->mutex.unlock(); mutex.unlock();
audio_output_client_notify.Signal(); audio_output_client_notify.Signal();
ao->mutex.lock(); mutex.lock();
} }
static bool inline bool
ao_enable(AudioOutput *ao) AudioOutput::Enable()
{ {
Error error; if (really_enabled)
bool success;
if (ao->really_enabled)
return true; return true;
ao->mutex.unlock(); mutex.unlock();
success = ao_plugin_enable(ao, error); Error error;
ao->mutex.lock(); bool success = ao_plugin_enable(this, error);
mutex.lock();
if (!success) { if (!success) {
FormatError(error, FormatError(error,
"Failed to enable \"%s\" [%s]", "Failed to enable \"%s\" [%s]",
ao->name, ao->plugin.name); name, plugin.name);
return false; return false;
} }
ao->really_enabled = true; really_enabled = true;
return true; return true;
} }
static void inline void
ao_close(AudioOutput *ao, bool drain); AudioOutput::Disable()
static void
ao_disable(AudioOutput *ao)
{ {
if (ao->open) if (open)
ao_close(ao, false); Close(false);
if (ao->really_enabled) { if (really_enabled) {
ao->really_enabled = false; really_enabled = false;
ao->mutex.unlock(); mutex.unlock();
ao_plugin_disable(ao); ao_plugin_disable(this);
ao->mutex.lock(); mutex.lock();
} }
} }
static AudioFormat inline AudioFormat
ao_filter_open(AudioOutput *ao, AudioFormat &format, AudioOutput::OpenFilter(AudioFormat &format, Error &error_r)
Error &error_r)
{ {
assert(format.IsValid()); assert(format.IsValid());
/* the replay_gain filter cannot fail here */ /* the replay_gain filter cannot fail here */
if (ao->replay_gain_filter != nullptr && if (replay_gain_filter != nullptr &&
!ao->replay_gain_filter->Open(format, error_r).IsDefined()) !replay_gain_filter->Open(format, error_r).IsDefined())
return AudioFormat::Undefined(); return AudioFormat::Undefined();
if (ao->other_replay_gain_filter != nullptr && if (other_replay_gain_filter != nullptr &&
!ao->other_replay_gain_filter->Open(format, error_r).IsDefined()) { !other_replay_gain_filter->Open(format, error_r).IsDefined()) {
if (ao->replay_gain_filter != nullptr) if (replay_gain_filter != nullptr)
ao->replay_gain_filter->Close(); replay_gain_filter->Close();
return AudioFormat::Undefined(); return AudioFormat::Undefined();
} }
const AudioFormat af = ao->filter->Open(format, error_r); const AudioFormat af = filter->Open(format, error_r);
if (!af.IsDefined()) { if (!af.IsDefined()) {
if (ao->replay_gain_filter != nullptr) if (replay_gain_filter != nullptr)
ao->replay_gain_filter->Close(); replay_gain_filter->Close();
if (ao->other_replay_gain_filter != nullptr) if (other_replay_gain_filter != nullptr)
ao->other_replay_gain_filter->Close(); other_replay_gain_filter->Close();
} }
return af; return af;
} }
static void void
ao_filter_close(AudioOutput *ao) AudioOutput::CloseFilter()
{ {
if (ao->replay_gain_filter != nullptr) if (replay_gain_filter != nullptr)
ao->replay_gain_filter->Close(); replay_gain_filter->Close();
if (ao->other_replay_gain_filter != nullptr) if (other_replay_gain_filter != nullptr)
ao->other_replay_gain_filter->Close(); other_replay_gain_filter->Close();
ao->filter->Close(); filter->Close();
} }
static void inline void
ao_open(AudioOutput *ao) AudioOutput::Open()
{ {
bool success; bool success;
Error error; Error error;
struct audio_format_string af_string; struct audio_format_string af_string;
assert(!ao->open); assert(!open);
assert(ao->pipe != nullptr); assert(pipe != nullptr);
assert(ao->current_chunk == nullptr); assert(current_chunk == nullptr);
assert(ao->in_audio_format.IsValid()); assert(in_audio_format.IsValid());
ao->fail_timer.Reset(); fail_timer.Reset();
/* enable the device (just in case the last enable has failed) */ /* enable the device (just in case the last enable has failed) */
if (!ao_enable(ao)) if (!Enable())
/* still no luck */ /* still no luck */
return; return;
/* open the filter */ /* open the filter */
const AudioFormat filter_audio_format = const AudioFormat filter_audio_format =
ao_filter_open(ao, ao->in_audio_format, error); OpenFilter(in_audio_format, error);
if (!filter_audio_format.IsDefined()) { if (!filter_audio_format.IsDefined()) {
FormatError(error, "Failed to open filter for \"%s\" [%s]", FormatError(error, "Failed to open filter for \"%s\" [%s]",
ao->name, ao->plugin.name); name, plugin.name);
ao->fail_timer.Update(); fail_timer.Update();
return; return;
} }
assert(filter_audio_format.IsValid()); assert(filter_audio_format.IsValid());
ao->out_audio_format = filter_audio_format; out_audio_format = filter_audio_format;
ao->out_audio_format.ApplyMask(ao->config_audio_format); out_audio_format.ApplyMask(config_audio_format);
ao->mutex.unlock(); mutex.unlock();
success = ao_plugin_open(ao, ao->out_audio_format, error); success = ao_plugin_open(this, out_audio_format, error);
ao->mutex.lock(); mutex.lock();
assert(!ao->open); assert(!open);
if (!success) { if (!success) {
FormatError(error, "Failed to open \"%s\" [%s]", FormatError(error, "Failed to open \"%s\" [%s]",
ao->name, ao->plugin.name); name, plugin.name);
ao_filter_close(ao); CloseFilter();
ao->fail_timer.Update(); fail_timer.Update();
return; return;
} }
if (!convert_filter_set(ao->convert_filter, ao->out_audio_format, if (!convert_filter_set(convert_filter, out_audio_format,
error)) { error)) {
FormatError(error, "Failed to convert for \"%s\" [%s]", FormatError(error, "Failed to convert for \"%s\" [%s]",
ao->name, ao->plugin.name); name, plugin.name);
ao_filter_close(ao); CloseFilter();
ao->fail_timer.Update(); fail_timer.Update();
return; return;
} }
ao->open = true; open = true;
FormatDebug(output_domain, FormatDebug(output_domain,
"opened plugin=%s name=\"%s\" audio_format=%s", "opened plugin=%s name=\"%s\" audio_format=%s",
ao->plugin.name, ao->name, plugin.name, name,
audio_format_to_string(ao->out_audio_format, &af_string)); audio_format_to_string(out_audio_format, &af_string));
if (ao->in_audio_format != ao->out_audio_format) if (in_audio_format != out_audio_format)
FormatDebug(output_domain, "converting from %s", FormatDebug(output_domain, "converting from %s",
audio_format_to_string(ao->in_audio_format, audio_format_to_string(in_audio_format,
&af_string)); &af_string));
} }
static void void
ao_close(AudioOutput *ao, bool drain) AudioOutput::Close(bool drain)
{ {
assert(ao->open); assert(open);
ao->pipe = nullptr; pipe = nullptr;
ao->current_chunk = nullptr; current_chunk = nullptr;
ao->open = false; open = false;
ao->mutex.unlock(); mutex.unlock();
if (drain) if (drain)
ao_plugin_drain(ao); ao_plugin_drain(this);
else else
ao_plugin_cancel(ao); ao_plugin_cancel(this);
ao_plugin_close(ao); ao_plugin_close(this);
ao_filter_close(ao); CloseFilter();
ao->mutex.lock(); mutex.lock();
FormatDebug(output_domain, "closed plugin=%s name=\"%s\"", FormatDebug(output_domain, "closed plugin=%s name=\"%s\"",
ao->plugin.name, ao->name); plugin.name, name);
} }
static void void
ao_reopen_filter(AudioOutput *ao) AudioOutput::ReopenFilter()
{ {
Error error; Error error;
ao_filter_close(ao); CloseFilter();
const AudioFormat filter_audio_format = const AudioFormat filter_audio_format =
ao_filter_open(ao, ao->in_audio_format, error); OpenFilter(in_audio_format, error);
if (!filter_audio_format.IsDefined() || if (!filter_audio_format.IsDefined() ||
!convert_filter_set(ao->convert_filter, ao->out_audio_format, !convert_filter_set(convert_filter, out_audio_format,
error)) { error)) {
FormatError(error, FormatError(error,
"Failed to open filter for \"%s\" [%s]", "Failed to open filter for \"%s\" [%s]",
ao->name, ao->plugin.name); name, plugin.name);
/* this is a little code duplication fro ao_close(), /* this is a little code duplication from Close(),
but we cannot call this function because we must but we cannot call this function because we must
not call filter_close(ao->filter) again */ not call filter_close(filter) again */
ao->pipe = nullptr; pipe = nullptr;
ao->current_chunk = nullptr; current_chunk = nullptr;
ao->open = false; open = false;
ao->fail_timer.Update(); fail_timer.Update();
ao->mutex.unlock(); mutex.unlock();
ao_plugin_close(ao); ao_plugin_close(this);
ao->mutex.lock(); mutex.lock();
return; return;
} }
} }
static void void
ao_reopen(AudioOutput *ao) AudioOutput::Reopen()
{ {
if (!ao->config_audio_format.IsFullyDefined()) { if (!config_audio_format.IsFullyDefined()) {
if (ao->open) { if (open) {
const MusicPipe *mp = ao->pipe; const MusicPipe *mp = pipe;
ao_close(ao, true); Close(true);
ao->pipe = mp; pipe = mp;
} }
/* no audio format is configured: copy in->out, let /* no audio format is configured: copy in->out, let
the output's open() method determine the effective the output's open() method determine the effective
out_audio_format */ out_audio_format */
ao->out_audio_format = ao->in_audio_format; out_audio_format = in_audio_format;
ao->out_audio_format.ApplyMask(ao->config_audio_format); out_audio_format.ApplyMask(config_audio_format);
} }
if (ao->open) if (open)
/* the audio format has changed, and all filters have /* the audio format has changed, and all filters have
to be reconfigured */ to be reconfigured */
ao_reopen_filter(ao); ReopenFilter();
else else
ao_open(ao); Open();
} }
/** /**
...@@ -295,17 +290,17 @@ ao_reopen(AudioOutput *ao) ...@@ -295,17 +290,17 @@ ao_reopen(AudioOutput *ao)
* @return true if playback should be continued, false if a command * @return true if playback should be continued, false if a command
* was issued * was issued
*/ */
static bool inline bool
ao_wait(AudioOutput *ao) AudioOutput::WaitForDelay()
{ {
while (true) { while (true) {
unsigned delay = ao_plugin_delay(ao); unsigned delay = ao_plugin_delay(this);
if (delay == 0) if (delay == 0)
return true; return true;
(void)ao->cond.timed_wait(ao->mutex, delay); (void)cond.timed_wait(mutex, delay);
if (ao->command != AO_COMMAND_NONE) if (command != AO_COMMAND_NONE)
return false; return false;
} }
} }
...@@ -420,16 +415,15 @@ ao_filter_chunk(AudioOutput *ao, const struct music_chunk *chunk, ...@@ -420,16 +415,15 @@ ao_filter_chunk(AudioOutput *ao, const struct music_chunk *chunk,
return data; return data;
} }
static bool inline bool
ao_play_chunk(AudioOutput *ao, const struct music_chunk *chunk) AudioOutput::PlayChunk(const music_chunk *chunk)
{ {
assert(ao != nullptr); assert(filter != nullptr);
assert(ao->filter != nullptr);
if (ao->tags && gcc_unlikely(chunk->tag != nullptr)) { if (tags && gcc_unlikely(chunk->tag != nullptr)) {
ao->mutex.unlock(); mutex.unlock();
ao_plugin_send_tag(ao, chunk->tag); ao_plugin_send_tag(this, chunk->tag);
ao->mutex.lock(); mutex.lock();
} }
size_t size; size_t size;
...@@ -437,44 +431,44 @@ ao_play_chunk(AudioOutput *ao, const struct music_chunk *chunk) ...@@ -437,44 +431,44 @@ ao_play_chunk(AudioOutput *ao, const struct music_chunk *chunk)
/* workaround -Wmaybe-uninitialized false positive */ /* workaround -Wmaybe-uninitialized false positive */
size = 0; size = 0;
#endif #endif
const char *data = (const char *)ao_filter_chunk(ao, chunk, &size); const char *data = (const char *)ao_filter_chunk(this, chunk, &size);
if (data == nullptr) { if (data == nullptr) {
ao_close(ao, false); Close(false);
/* don't automatically reopen this device for 10 /* don't automatically reopen this device for 10
seconds */ seconds */
ao->fail_timer.Update(); fail_timer.Update();
return false; return false;
} }
Error error; Error error;
while (size > 0 && ao->command == AO_COMMAND_NONE) { while (size > 0 && command == AO_COMMAND_NONE) {
size_t nbytes; size_t nbytes;
if (!ao_wait(ao)) if (!WaitForDelay())
break; break;
ao->mutex.unlock(); mutex.unlock();
nbytes = ao_plugin_play(ao, data, size, error); nbytes = ao_plugin_play(this, data, size, error);
ao->mutex.lock(); mutex.lock();
if (nbytes == 0) { if (nbytes == 0) {
/* play()==0 means failure */ /* play()==0 means failure */
FormatError(error, "\"%s\" [%s] failed to play", FormatError(error, "\"%s\" [%s] failed to play",
ao->name, ao->plugin.name); name, plugin.name);
ao_close(ao, false); Close(false);
/* don't automatically reopen this device for /* don't automatically reopen this device for
10 seconds */ 10 seconds */
assert(!ao->fail_timer.IsDefined()); assert(!fail_timer.IsDefined());
ao->fail_timer.Update(); fail_timer.Update();
return false; return false;
} }
assert(nbytes <= size); assert(nbytes <= size);
assert(nbytes % ao->out_audio_format.GetFrameSize() == 0); assert(nbytes % out_audio_format.GetFrameSize() == 0);
data += nbytes; data += nbytes;
size -= nbytes; size -= nbytes;
...@@ -483,208 +477,200 @@ ao_play_chunk(AudioOutput *ao, const struct music_chunk *chunk) ...@@ -483,208 +477,200 @@ ao_play_chunk(AudioOutput *ao, const struct music_chunk *chunk)
return true; return true;
} }
static const struct music_chunk * inline const music_chunk *
ao_next_chunk(AudioOutput *ao) AudioOutput::GetNextChunk() const
{ {
return ao->current_chunk != nullptr return current_chunk != nullptr
/* continue the previous play() call */ /* continue the previous play() call */
? ao->current_chunk->next ? current_chunk->next
/* get the first chunk from the pipe */ /* get the first chunk from the pipe */
: ao->pipe->Peek(); : pipe->Peek();
} }
/** inline bool
* Plays all remaining chunks, until the tail of the pipe has been AudioOutput::Play()
* reached (and no more chunks are queued), or until a command is
* received.
*
* @return true if at least one chunk has been available, false if the
* tail of the pipe was already reached
*/
static bool
ao_play(AudioOutput *ao)
{ {
bool success; assert(pipe != nullptr);
const struct music_chunk *chunk;
assert(ao->pipe != nullptr); const music_chunk *chunk = GetNextChunk();
chunk = ao_next_chunk(ao);
if (chunk == nullptr) if (chunk == nullptr)
/* no chunk available */ /* no chunk available */
return false; return false;
ao->current_chunk_finished = false; current_chunk_finished = false;
assert(!ao->in_playback_loop); assert(!in_playback_loop);
ao->in_playback_loop = true; in_playback_loop = true;
while (chunk != nullptr && ao->command == AO_COMMAND_NONE) { while (chunk != nullptr && command == AO_COMMAND_NONE) {
assert(!ao->current_chunk_finished); assert(!current_chunk_finished);
ao->current_chunk = chunk; current_chunk = chunk;
success = ao_play_chunk(ao, chunk); if (!PlayChunk(chunk)) {
if (!success) { assert(current_chunk == nullptr);
assert(ao->current_chunk == nullptr);
break; break;
} }
assert(ao->current_chunk == chunk); assert(current_chunk == chunk);
chunk = chunk->next; chunk = chunk->next;
} }
assert(ao->in_playback_loop); assert(in_playback_loop);
ao->in_playback_loop = false; in_playback_loop = false;
ao->current_chunk_finished = true; current_chunk_finished = true;
ao->mutex.unlock(); mutex.unlock();
ao->player_control->LockSignal(); player_control->LockSignal();
ao->mutex.lock(); mutex.lock();
return true; return true;
} }
static void ao_pause(AudioOutput *ao) inline void
AudioOutput::Pause()
{ {
bool ret; mutex.unlock();
ao_plugin_cancel(this);
mutex.lock();
ao->mutex.unlock(); pause = true;
ao_plugin_cancel(ao); CommandFinished();
ao->mutex.lock();
ao->pause = true;
ao_command_finished(ao);
do { do {
if (!ao_wait(ao)) if (!WaitForDelay())
break; break;
ao->mutex.unlock(); mutex.unlock();
ret = ao_plugin_pause(ao); bool success = ao_plugin_pause(this);
ao->mutex.lock(); mutex.lock();
if (!ret) { if (!success) {
ao_close(ao, false); Close(false);
break; break;
} }
} while (ao->command == AO_COMMAND_NONE); } while (command == AO_COMMAND_NONE);
ao->pause = false; pause = false;
} }
static void inline void
audio_output_task(void *arg) AudioOutput::Task()
{ {
AudioOutput *ao = (AudioOutput *)arg; FormatThreadName("output:%s", name);
FormatThreadName("output:%s", ao->name);
SetThreadRealtime(); SetThreadRealtime();
ao->mutex.lock(); mutex.lock();
while (1) { while (1) {
switch (ao->command) { switch (command) {
case AO_COMMAND_NONE: case AO_COMMAND_NONE:
break; break;
case AO_COMMAND_ENABLE: case AO_COMMAND_ENABLE:
ao_enable(ao); Enable();
ao_command_finished(ao); CommandFinished();
break; break;
case AO_COMMAND_DISABLE: case AO_COMMAND_DISABLE:
ao_disable(ao); Disable();
ao_command_finished(ao); CommandFinished();
break; break;
case AO_COMMAND_OPEN: case AO_COMMAND_OPEN:
ao_open(ao); Open();
ao_command_finished(ao); CommandFinished();
break; break;
case AO_COMMAND_REOPEN: case AO_COMMAND_REOPEN:
ao_reopen(ao); Reopen();
ao_command_finished(ao); CommandFinished();
break; break;
case AO_COMMAND_CLOSE: case AO_COMMAND_CLOSE:
assert(ao->open); assert(open);
assert(ao->pipe != nullptr); assert(pipe != nullptr);
ao_close(ao, false); Close(false);
ao_command_finished(ao); CommandFinished();
break; break;
case AO_COMMAND_PAUSE: case AO_COMMAND_PAUSE:
if (!ao->open) { if (!open) {
/* the output has failed after /* the output has failed after
audio_output_all_pause() has audio_output_all_pause() has
submitted the PAUSE command; bail submitted the PAUSE command; bail
out */ out */
ao_command_finished(ao); CommandFinished();
break; break;
} }
ao_pause(ao); Pause();
/* don't "break" here: this might cause /* don't "break" here: this might cause
ao_play() to be called when command==CLOSE Play() to be called when command==CLOSE
ends the paused state - "continue" checks ends the paused state - "continue" checks
the new command first */ the new command first */
continue; continue;
case AO_COMMAND_DRAIN: case AO_COMMAND_DRAIN:
if (ao->open) { if (open) {
assert(ao->current_chunk == nullptr); assert(current_chunk == nullptr);
assert(ao->pipe->Peek() == nullptr); assert(pipe->Peek() == nullptr);
ao->mutex.unlock(); mutex.unlock();
ao_plugin_drain(ao); ao_plugin_drain(this);
ao->mutex.lock(); mutex.lock();
} }
ao_command_finished(ao); CommandFinished();
continue; continue;
case AO_COMMAND_CANCEL: case AO_COMMAND_CANCEL:
ao->current_chunk = nullptr; current_chunk = nullptr;
if (ao->open) { if (open) {
ao->mutex.unlock(); mutex.unlock();
ao_plugin_cancel(ao); ao_plugin_cancel(this);
ao->mutex.lock(); mutex.lock();
} }
ao_command_finished(ao); CommandFinished();
continue; continue;
case AO_COMMAND_KILL: case AO_COMMAND_KILL:
ao->current_chunk = nullptr; current_chunk = nullptr;
ao_command_finished(ao); CommandFinished();
ao->mutex.unlock(); mutex.unlock();
return; return;
} }
if (ao->open && ao->allow_play && ao_play(ao)) if (open && allow_play && Play())
/* don't wait for an event if there are more /* don't wait for an event if there are more
chunks in the pipe */ chunks in the pipe */
continue; continue;
if (ao->command == AO_COMMAND_NONE) { if (command == AO_COMMAND_NONE) {
ao->woken_for_play = false; woken_for_play = false;
ao->cond.wait(ao->mutex); cond.wait(mutex);
} }
} }
} }
void void
AudioOutput::Task(void *arg)
{
AudioOutput *ao = (AudioOutput *)arg;
ao->Task();
}
void
AudioOutput::StartThread() AudioOutput::StartThread()
{ {
assert(command == AO_COMMAND_NONE); assert(command == AO_COMMAND_NONE);
Error error; Error error;
if (!thread.Start(audio_output_task, this, error)) if (!thread.Start(Task, this, error))
FatalError(error); FatalError(error);
} }
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