diff options
author | Max Kellermann <max@duempel.org> | 2009-03-06 00:42:01 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2009-03-06 00:42:01 +0100 |
commit | 000b2d4f3a9c4f761ab918aaff4705621bb8559f (patch) | |
tree | 5009a5548c99155e00b4b14d52f50728a337960f /src/decoder | |
parent | 10be8a8714b49bec5e6f869975962cbc26cebe61 (diff) | |
download | mpd-000b2d4f3a9c4f761ab918aaff4705621bb8559f.tar.gz mpd-000b2d4f3a9c4f761ab918aaff4705621bb8559f.tar.xz mpd-000b2d4f3a9c4f761ab918aaff4705621bb8559f.zip |
music_pipe: added music_pipe_push()
Added music_pipe_allocate(), music_pipe_push() and
music_pipe_cancel(). Those functions allow the caller (decoder thread
in this case) to do its own chunk management. The functions
music_pipe_flush() and music_pipe_tag() can now be removed.
Diffstat (limited to '')
-rw-r--r-- | src/decoder_api.c | 78 | ||||
-rw-r--r-- | src/decoder_internal.c | 80 | ||||
-rw-r--r-- | src/decoder_internal.h | 22 | ||||
-rw-r--r-- | src/decoder_thread.c | 6 |
4 files changed, 152 insertions, 34 deletions
diff --git a/src/decoder_api.c b/src/decoder_api.c index e6ea44088..88864befa 100644 --- a/src/decoder_api.c +++ b/src/decoder_api.c @@ -26,6 +26,7 @@ #include "normalize.h" #include "pipe.h" +#include "chunk.h" #include <glib.h> @@ -85,9 +86,16 @@ void decoder_command_finished(G_GNUC_UNUSED struct decoder * decoder) assert(dc.command != DECODE_COMMAND_SEEK || dc.seek_error || decoder->seeking); - if (dc.command == DECODE_COMMAND_SEEK) + if (dc.command == DECODE_COMMAND_SEEK) { /* delete frames from the old song position */ + + if (decoder->chunk != NULL) { + music_pipe_cancel(decoder->chunk); + decoder->chunk = NULL; + } + music_pipe_clear(); + } dc.command = DECODE_COMMAND_NONE; notify_signal(&pc.notify); @@ -147,35 +155,28 @@ size_t decoder_read(struct decoder *decoder, } /** - * All chunks are full of decoded data; wait for the player to free - * one. + * Sends a #tag as-is to the music pipe. Flushes the current chunk + * (decoder.chunk) if there is one. */ static enum decoder_command -need_chunks(struct input_stream *is, bool do_wait) +do_send_tag(struct decoder *decoder, struct input_stream *is, + const struct tag *tag) { - if (dc.command == DECODE_COMMAND_STOP || - dc.command == DECODE_COMMAND_SEEK) - return dc.command; - - if ((is == NULL || input_stream_buffer(is) <= 0) && do_wait) { - notify_wait(&dc.notify); - notify_signal(&pc.notify); - - return dc.command; - } + struct music_chunk *chunk; - return DECODE_COMMAND_NONE; -} - -static enum decoder_command -do_send_tag(struct input_stream *is, const struct tag *tag) -{ - while (!music_pipe_tag(tag)) { - enum decoder_command cmd = need_chunks(is, true); + if (decoder->chunk != NULL) { + /* there is a partial chunk - flush it, we want the + tag in a new chunk */ + enum decoder_command cmd = + decoder_flush_chunk(decoder, is); if (cmd != DECODE_COMMAND_NONE) return cmd; } + assert(decoder->chunk == NULL); + + chunk = decoder_get_chunk(decoder); + chunk->tag = tag_dup(tag); return DECODE_COMMAND_NONE; } @@ -226,11 +227,11 @@ decoder_data(struct decoder *decoder, tag = tag_merge(decoder->stream_tag, decoder->decoder_tag); - cmd = do_send_tag(is, tag); + cmd = do_send_tag(decoder, is, tag); tag_free(tag); } else /* send only the stream tag */ - cmd = do_send_tag(is, decoder->stream_tag); + cmd = do_send_tag(decoder, is, decoder->stream_tag); if (cmd != DECODE_COMMAND_NONE) return cmd; @@ -250,14 +251,18 @@ decoder_data(struct decoder *decoder, } while (length > 0) { + struct music_chunk *chunk; + char *dest; size_t nbytes; - char *dest = music_pipe_write(&dc.out_audio_format, - data_time, bitRate, - &nbytes); + bool full; + + chunk = decoder_get_chunk(decoder); + dest = music_chunk_write(chunk, &dc.out_audio_format, + data_time, bitRate, &nbytes); if (dest == NULL) { - /* the music pipe is full: wait for more - room */ - enum decoder_command cmd = need_chunks(is, true); + /* the chunk is full, flush it */ + enum decoder_command cmd = + decoder_flush_chunk(decoder, is); if (cmd != DECODE_COMMAND_NONE) return cmd; continue; @@ -283,7 +288,14 @@ decoder_data(struct decoder *decoder, /* expand the music pipe chunk */ - music_pipe_expand(&dc.out_audio_format, nbytes); + full = music_chunk_expand(chunk, &dc.out_audio_format, nbytes); + if (full) { + /* the chunk is full, flush it */ + enum decoder_command cmd = + decoder_flush_chunk(decoder, is); + if (cmd != DECODE_COMMAND_NONE) + return cmd; + } data += nbytes; length -= nbytes; @@ -318,11 +330,11 @@ decoder_tag(G_GNUC_UNUSED struct decoder *decoder, struct input_stream *is, struct tag *merged; merged = tag_merge(decoder->stream_tag, decoder->decoder_tag); - cmd = do_send_tag(is, merged); + cmd = do_send_tag(decoder, is, merged); tag_free(merged); } else /* send only the decoder tag */ - cmd = do_send_tag(is, tag); + cmd = do_send_tag(decoder, is, tag); return cmd; } diff --git a/src/decoder_internal.c b/src/decoder_internal.c new file mode 100644 index 000000000..93ad80e5e --- /dev/null +++ b/src/decoder_internal.c @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2003-2009 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "decoder_internal.h" +#include "decoder_control.h" +#include "player_control.h" +#include "pipe.h" +#include "input_stream.h" + +#include <assert.h> + +/** + * All chunks are full of decoded data; wait for the player to free + * one. + */ +static enum decoder_command +need_chunks(struct input_stream *is, bool do_wait) +{ + if (dc.command == DECODE_COMMAND_STOP || + dc.command == DECODE_COMMAND_SEEK) + return dc.command; + + if ((is == NULL || input_stream_buffer(is) <= 0) && do_wait) { + notify_wait(&dc.notify); + notify_signal(&pc.notify); + + return dc.command; + } + + return DECODE_COMMAND_NONE; +} + +struct music_chunk * +decoder_get_chunk(struct decoder *decoder) +{ + assert(decoder != NULL); + + if (decoder->chunk != NULL) + return decoder->chunk; + + decoder->chunk = music_pipe_allocate(); + return decoder->chunk; +} + +enum decoder_command +decoder_flush_chunk(struct decoder *decoder, struct input_stream *is) +{ + bool success; + enum decoder_command cmd; + + assert(decoder != NULL); + assert(decoder->chunk != NULL); + + while (true) { + success = music_pipe_push(decoder->chunk); + if (success) { + decoder->chunk = NULL; + return DECODE_COMMAND_NONE; + } + + cmd = need_chunks(is, true); + if (cmd != DECODE_COMMAND_NONE) + return cmd; + } +} diff --git a/src/decoder_internal.h b/src/decoder_internal.h index 53b6b5cf3..ab52ab037 100644 --- a/src/decoder_internal.h +++ b/src/decoder_internal.h @@ -19,8 +19,11 @@ #ifndef MPD_DECODER_INTERNAL_H #define MPD_DECODER_INTERNAL_H +#include "decoder_command.h" #include "pcm_convert.h" +struct input_stream; + struct decoder { struct pcm_convert_state conv_state; @@ -31,6 +34,25 @@ struct decoder { /** the last tag received from the decoder plugin */ struct tag *decoder_tag; + + /** the chunk currently being written to */ + struct music_chunk *chunk; }; +/** + * Returns the current chunk the decoder writes to, or allocates a new + * chunk if there is none. + */ +struct music_chunk * +decoder_get_chunk(struct decoder *decoder); + +/** + * Flushes a chunk. Waits for room in the music pipe if required. + * + * @return DECODE_COMMAND_NONE on success, any other command if we + * have received a decoder command while waiting + */ +enum decoder_command +decoder_flush_chunk(struct decoder *decoder, struct input_stream *is); + #endif diff --git a/src/decoder_thread.c b/src/decoder_thread.c index b35683e6e..97e92d295 100644 --- a/src/decoder_thread.c +++ b/src/decoder_thread.c @@ -98,6 +98,7 @@ static void decoder_run_song(const struct song *song, const char *uri) decoder.seeking = false; decoder.stream_tag = NULL; decoder.decoder_tag = NULL; + decoder.chunk = NULL; dc.state = DECODE_STATE_START; dc.command = DECODE_COMMAND_NONE; @@ -194,7 +195,10 @@ static void decoder_run_song(const struct song *song, const char *uri) pcm_convert_deinit(&decoder.conv_state); - music_pipe_flush(); + /* flush the last chunk */ + if (decoder.chunk != NULL && + decoder_flush_chunk(&decoder, NULL) != DECODE_COMMAND_NONE) + music_pipe_cancel(decoder.chunk); if (close_instream) input_stream_close(&input_stream); |