doc: Remove superfluous comment already described in footnotes.
[mpd-mk.git] / src / decoder_internal.c
blob1b064d0aa4b9c14eb4d957dc4e17089e15646af4
1 /*
2 * Copyright (C) 2003-2009 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "decoder_internal.h"
21 #include "decoder_control.h"
22 #include "player_control.h"
23 #include "pipe.h"
24 #include "input_stream.h"
25 #include "buffer.h"
26 #include "chunk.h"
28 #include <assert.h>
30 /**
31 * This is a wrapper for input_stream_buffer(). It assumes that the
32 * decoder is currently locked, and temporarily unlocks it while
33 * calling input_stream_buffer(). We shouldn't hold the lock during a
34 * potentially blocking operation.
36 static int
37 decoder_input_buffer(struct input_stream *is)
39 int ret;
41 decoder_unlock();
42 ret = input_stream_buffer(is) > 0;
43 decoder_lock();
45 return ret;
48 /**
49 * All chunks are full of decoded data; wait for the player to free
50 * one.
52 static enum decoder_command
53 need_chunks(struct input_stream *is, bool do_wait)
55 if (dc.command == DECODE_COMMAND_STOP ||
56 dc.command == DECODE_COMMAND_SEEK)
57 return dc.command;
59 if ((is == NULL || decoder_input_buffer(is) <= 0) && do_wait) {
60 decoder_wait();
62 decoder_unlock();
63 notify_signal(&pc.notify);
64 decoder_lock();
66 return dc.command;
69 return DECODE_COMMAND_NONE;
72 struct music_chunk *
73 decoder_get_chunk(struct decoder *decoder, struct input_stream *is)
75 enum decoder_command cmd;
77 assert(decoder != NULL);
79 if (decoder->chunk != NULL)
80 return decoder->chunk;
82 do {
83 decoder->chunk = music_buffer_allocate(dc.buffer);
84 if (decoder->chunk != NULL)
85 return decoder->chunk;
87 decoder_lock();
88 cmd = need_chunks(is, true);
89 decoder_unlock();
90 } while (cmd == DECODE_COMMAND_NONE);
92 return NULL;
95 void
96 decoder_flush_chunk(struct decoder *decoder)
98 assert(decoder != NULL);
99 assert(decoder->chunk != NULL);
101 if (music_chunk_is_empty(decoder->chunk))
102 music_buffer_return(dc.buffer, decoder->chunk);
103 else
104 music_pipe_push(dc.pipe, decoder->chunk);
106 decoder->chunk = NULL;