Commit 67659016 authored by Max Kellermann's avatar Max Kellermann

DecoderControl: convert "enum decoder_state" to strictly-typed enum

parent c5d05ac0
...@@ -47,7 +47,7 @@ decoder_initialized(struct decoder *decoder, ...@@ -47,7 +47,7 @@ decoder_initialized(struct decoder *decoder,
struct decoder_control *dc = decoder->dc; struct decoder_control *dc = decoder->dc;
struct audio_format_string af_string; struct audio_format_string af_string;
assert(dc->state == DECODE_STATE_START); assert(dc->state == DecoderState::START);
assert(dc->pipe != NULL); assert(dc->pipe != NULL);
assert(decoder != NULL); assert(decoder != NULL);
assert(decoder->stream_tag == NULL); assert(decoder->stream_tag == NULL);
...@@ -63,7 +63,7 @@ decoder_initialized(struct decoder *decoder, ...@@ -63,7 +63,7 @@ decoder_initialized(struct decoder *decoder,
dc->total_time = total_time; dc->total_time = total_time;
dc->Lock(); dc->Lock();
dc->state = DECODE_STATE_DECODE; dc->state = DecoderState::DECODE;
dc->client_cond.signal(); dc->client_cond.signal();
dc->Unlock(); dc->Unlock();
...@@ -88,7 +88,7 @@ decoder_prepare_initial_seek(struct decoder *decoder) ...@@ -88,7 +88,7 @@ decoder_prepare_initial_seek(struct decoder *decoder)
const struct decoder_control *dc = decoder->dc; const struct decoder_control *dc = decoder->dc;
assert(dc->pipe != NULL); assert(dc->pipe != NULL);
if (dc->state != DECODE_STATE_DECODE) if (dc->state != DecoderState::DECODE)
/* wait until the decoder has finished initialisation /* wait until the decoder has finished initialisation
(reading file headers etc.) before emitting the (reading file headers etc.) before emitting the
virtual "SEEK" command */ virtual "SEEK" command */
...@@ -247,7 +247,7 @@ decoder_check_cancel_read(const struct decoder *decoder) ...@@ -247,7 +247,7 @@ decoder_check_cancel_read(const struct decoder *decoder)
/* ignore the SEEK command during initialization, the plugin /* ignore the SEEK command during initialization, the plugin
should handle that after it has initialized successfully */ should handle that after it has initialized successfully */
if (dc->command == DecoderCommand::SEEK && if (dc->command == DecoderCommand::SEEK &&
(dc->state == DECODE_STATE_START || decoder->seeking)) (dc->state == DecoderState::START || decoder->seeking))
return false; return false;
return true; return true;
...@@ -260,8 +260,8 @@ size_t decoder_read(struct decoder *decoder, ...@@ -260,8 +260,8 @@ size_t decoder_read(struct decoder *decoder,
/* XXX don't allow decoder==NULL */ /* XXX don't allow decoder==NULL */
assert(decoder == NULL || assert(decoder == NULL ||
decoder->dc->state == DECODE_STATE_START || decoder->dc->state == DecoderState::START ||
decoder->dc->state == DECODE_STATE_DECODE); decoder->dc->state == DecoderState::DECODE);
assert(is != NULL); assert(is != NULL);
assert(buffer != NULL); assert(buffer != NULL);
...@@ -364,7 +364,7 @@ decoder_data(struct decoder *decoder, ...@@ -364,7 +364,7 @@ decoder_data(struct decoder *decoder,
struct decoder_control *dc = decoder->dc; struct decoder_control *dc = decoder->dc;
DecoderCommand cmd; DecoderCommand cmd;
assert(dc->state == DECODE_STATE_DECODE); assert(dc->state == DecoderState::DECODE);
assert(dc->pipe != NULL); assert(dc->pipe != NULL);
assert(length % dc->in_audio_format.GetFrameSize() == 0); assert(length % dc->in_audio_format.GetFrameSize() == 0);
...@@ -472,7 +472,7 @@ decoder_tag(gcc_unused struct decoder *decoder, struct input_stream *is, ...@@ -472,7 +472,7 @@ decoder_tag(gcc_unused struct decoder *decoder, struct input_stream *is,
gcc_unused const struct decoder_control *dc = decoder->dc; gcc_unused const struct decoder_control *dc = decoder->dc;
DecoderCommand cmd; DecoderCommand cmd;
assert(dc->state == DECODE_STATE_DECODE); assert(dc->state == DecoderState::DECODE);
assert(dc->pipe != NULL); assert(dc->pipe != NULL);
/* save the tag */ /* save the tag */
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
decoder_control::decoder_control() decoder_control::decoder_control()
:thread(nullptr), :thread(nullptr),
state(DECODE_STATE_STOP), state(DecoderState::STOP),
command(DecoderCommand::NONE), command(DecoderCommand::NONE),
song(nullptr), song(nullptr),
replay_gain_db(0), replay_gain_prev_db(0), replay_gain_db(0), replay_gain_prev_db(0),
...@@ -54,12 +54,12 @@ decoder_control::IsCurrentSong(const Song *_song) const ...@@ -54,12 +54,12 @@ decoder_control::IsCurrentSong(const Song *_song) const
assert(_song != NULL); assert(_song != NULL);
switch (state) { switch (state) {
case DECODE_STATE_STOP: case DecoderState::STOP:
case DECODE_STATE_ERROR: case DecoderState::ERROR:
return false; return false;
case DECODE_STATE_START: case DecoderState::START:
case DECODE_STATE_DECODE: case DecoderState::DECODE:
return song_equals(song, _song); return song_equals(song, _song);
} }
...@@ -99,7 +99,7 @@ decoder_control::Stop() ...@@ -99,7 +99,7 @@ decoder_control::Stop()
function (see below). */ function (see below). */
SynchronousCommandLocked(DecoderCommand::STOP); SynchronousCommandLocked(DecoderCommand::STOP);
if (state != DECODE_STATE_STOP && state != DECODE_STATE_ERROR) if (state != DecoderState::STOP && state != DecoderState::ERROR)
SynchronousCommandLocked(DecoderCommand::STOP); SynchronousCommandLocked(DecoderCommand::STOP);
Unlock(); Unlock();
...@@ -108,11 +108,11 @@ decoder_control::Stop() ...@@ -108,11 +108,11 @@ decoder_control::Stop()
bool bool
decoder_control::Seek(double where) decoder_control::Seek(double where)
{ {
assert(state != DECODE_STATE_START); assert(state != DecoderState::START);
assert(where >= 0.0); assert(where >= 0.0);
if (state == DECODE_STATE_STOP || if (state == DecoderState::STOP ||
state == DECODE_STATE_ERROR || !seekable) state == DecoderState::ERROR || !seekable)
return false; return false;
seek_where = where; seek_where = where;
......
...@@ -29,15 +29,21 @@ ...@@ -29,15 +29,21 @@
#include <glib.h> #include <glib.h>
#include <assert.h> #include <assert.h>
#include <stdint.h>
/* damn you, windows.h! */
#ifdef ERROR
#undef ERROR
#endif
struct Song; struct Song;
class MusicBuffer; class MusicBuffer;
class MusicPipe; class MusicPipe;
enum decoder_state { enum class DecoderState : uint8_t {
DECODE_STATE_STOP = 0, STOP = 0,
DECODE_STATE_START, START,
DECODE_STATE_DECODE, DECODE,
/** /**
* The last "START" command failed, because there was an I/O * The last "START" command failed, because there was an I/O
...@@ -45,7 +51,7 @@ enum decoder_state { ...@@ -45,7 +51,7 @@ enum decoder_state {
* This state will only come after START; once the state has * This state will only come after START; once the state has
* turned to DECODE, by definition no such error can occur. * turned to DECODE, by definition no such error can occur.
*/ */
DECODE_STATE_ERROR, ERROR,
}; };
struct decoder_control { struct decoder_control {
...@@ -71,14 +77,14 @@ struct decoder_control { ...@@ -71,14 +77,14 @@ struct decoder_control {
*/ */
Cond client_cond; Cond client_cond;
enum decoder_state state; DecoderState state;
DecoderCommand command; DecoderCommand command;
/** /**
* The error that occurred in the decoder thread. This * The error that occurred in the decoder thread. This
* attribute is only valid if #state is #DECODE_STATE_ERROR. * attribute is only valid if #state is #DecoderState::ERROR.
* The object must be freed when this object transitions to * The object must be freed when this object transitions to
* any other state (usually #DECODE_STATE_START). * any other state (usually #DecoderState::START).
*/ */
Error error; Error error;
...@@ -182,8 +188,8 @@ struct decoder_control { ...@@ -182,8 +188,8 @@ struct decoder_control {
} }
bool IsIdle() const { bool IsIdle() const {
return state == DECODE_STATE_STOP || return state == DecoderState::STOP ||
state == DECODE_STATE_ERROR; state == DecoderState::ERROR;
} }
gcc_pure gcc_pure
...@@ -195,7 +201,7 @@ struct decoder_control { ...@@ -195,7 +201,7 @@ struct decoder_control {
} }
bool IsStarting() const { bool IsStarting() const {
return state == DECODE_STATE_START; return state == DecoderState::START;
} }
gcc_pure gcc_pure
...@@ -209,7 +215,7 @@ struct decoder_control { ...@@ -209,7 +215,7 @@ struct decoder_control {
bool HasFailed() const { bool HasFailed() const {
assert(command == DecoderCommand::NONE); assert(command == DecoderCommand::NONE);
return state == DECODE_STATE_ERROR; return state == DecoderState::ERROR;
} }
gcc_pure gcc_pure
...@@ -229,10 +235,10 @@ struct decoder_control { ...@@ -229,10 +235,10 @@ struct decoder_control {
gcc_pure gcc_pure
Error GetError() const { Error GetError() const {
assert(command == DecoderCommand::NONE); assert(command == DecoderCommand::NONE);
assert(state != DECODE_STATE_ERROR || error.IsDefined()); assert(state != DecoderState::ERROR || error.IsDefined());
Error result; Error result;
if (state == DECODE_STATE_ERROR) if (state == DecoderState::ERROR)
result.Set(error); result.Set(error);
return result; return result;
} }
...@@ -254,9 +260,9 @@ struct decoder_control { ...@@ -254,9 +260,9 @@ struct decoder_control {
* Caller must lock the object. * Caller must lock the object.
*/ */
void ClearError() { void ClearError() {
if (state == DECODE_STATE_ERROR) { if (state == DecoderState::ERROR) {
error.Clear(); error.Clear();
state = DECODE_STATE_STOP; state = DecoderState::STOP;
} }
} }
......
...@@ -120,7 +120,7 @@ decoder_stream_decode(const struct decoder_plugin *plugin, ...@@ -120,7 +120,7 @@ decoder_stream_decode(const struct decoder_plugin *plugin,
assert(decoder->decoder_tag == NULL); assert(decoder->decoder_tag == NULL);
assert(input_stream != NULL); assert(input_stream != NULL);
assert(input_stream->ready); assert(input_stream->ready);
assert(decoder->dc->state == DECODE_STATE_START); assert(decoder->dc->state == DecoderState::START);
g_debug("probing plugin %s", plugin->name); g_debug("probing plugin %s", plugin->name);
...@@ -136,10 +136,10 @@ decoder_stream_decode(const struct decoder_plugin *plugin, ...@@ -136,10 +136,10 @@ decoder_stream_decode(const struct decoder_plugin *plugin,
decoder->dc->Lock(); decoder->dc->Lock();
assert(decoder->dc->state == DECODE_STATE_START || assert(decoder->dc->state == DecoderState::START ||
decoder->dc->state == DECODE_STATE_DECODE); decoder->dc->state == DecoderState::DECODE);
return decoder->dc->state != DECODE_STATE_START; return decoder->dc->state != DecoderState::START;
} }
static bool static bool
...@@ -153,7 +153,7 @@ decoder_file_decode(const struct decoder_plugin *plugin, ...@@ -153,7 +153,7 @@ decoder_file_decode(const struct decoder_plugin *plugin,
assert(decoder->decoder_tag == NULL); assert(decoder->decoder_tag == NULL);
assert(path != NULL); assert(path != NULL);
assert(g_path_is_absolute(path)); assert(g_path_is_absolute(path));
assert(decoder->dc->state == DECODE_STATE_START); assert(decoder->dc->state == DecoderState::START);
g_debug("probing plugin %s", plugin->name); g_debug("probing plugin %s", plugin->name);
...@@ -166,10 +166,10 @@ decoder_file_decode(const struct decoder_plugin *plugin, ...@@ -166,10 +166,10 @@ decoder_file_decode(const struct decoder_plugin *plugin,
decoder->dc->Lock(); decoder->dc->Lock();
assert(decoder->dc->state == DECODE_STATE_START || assert(decoder->dc->state == DecoderState::START ||
decoder->dc->state == DECODE_STATE_DECODE); decoder->dc->state == DecoderState::DECODE);
return decoder->dc->state != DECODE_STATE_START; return decoder->dc->state != DecoderState::START;
} }
/** /**
...@@ -380,7 +380,7 @@ decoder_run_song(struct decoder_control *dc, ...@@ -380,7 +380,7 @@ decoder_run_song(struct decoder_control *dc,
? new Tag(*song->tag) : nullptr); ? new Tag(*song->tag) : nullptr);
int ret; int ret;
dc->state = DECODE_STATE_START; dc->state = DecoderState::START;
decoder_command_finished_locked(dc); decoder_command_finished_locked(dc);
...@@ -398,9 +398,9 @@ decoder_run_song(struct decoder_control *dc, ...@@ -398,9 +398,9 @@ decoder_run_song(struct decoder_control *dc,
dc->Lock(); dc->Lock();
if (ret) if (ret)
dc->state = DECODE_STATE_STOP; dc->state = DecoderState::STOP;
else { else {
dc->state = DECODE_STATE_ERROR; dc->state = DecoderState::ERROR;
const char *error_uri = song->uri; const char *error_uri = song->uri;
char *allocated = uri_remove_auth(error_uri); char *allocated = uri_remove_auth(error_uri);
...@@ -431,7 +431,7 @@ decoder_run(struct decoder_control *dc) ...@@ -431,7 +431,7 @@ decoder_run(struct decoder_control *dc)
uri = song->GetURI(); uri = song->GetURI();
if (uri == NULL) { if (uri == NULL) {
dc->state = DECODE_STATE_ERROR; dc->state = DecoderState::ERROR;
dc->error.Set(decoder_domain, "Failed to map song"); dc->error.Set(decoder_domain, "Failed to map song");
decoder_command_finished_locked(dc); decoder_command_finished_locked(dc);
...@@ -451,8 +451,8 @@ decoder_task(gpointer arg) ...@@ -451,8 +451,8 @@ decoder_task(gpointer arg)
dc->Lock(); dc->Lock();
do { do {
assert(dc->state == DECODE_STATE_STOP || assert(dc->state == DecoderState::STOP ||
dc->state == DECODE_STATE_ERROR); dc->state == DecoderState::ERROR);
switch (dc->command) { switch (dc->command) {
case DecoderCommand::START: case DecoderCommand::START:
......
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