2 * Copyright (C) 2003-2010 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.
21 #include "decoder_api.h"
22 #include "decoder_internal.h"
23 #include "decoder_control.h"
24 #include "player_control.h"
37 #define G_LOG_DOMAIN "decoder"
40 decoder_initialized(struct decoder
*decoder
,
41 const struct audio_format
*audio_format
,
42 bool seekable
, float total_time
)
44 struct decoder_control
*dc
= decoder
->dc
;
45 struct audio_format_string af_string
;
47 assert(dc
->state
== DECODE_STATE_START
);
48 assert(dc
->pipe
!= NULL
);
49 assert(decoder
!= NULL
);
50 assert(decoder
->stream_tag
== NULL
);
51 assert(decoder
->decoder_tag
== NULL
);
52 assert(!decoder
->seeking
);
53 assert(audio_format
!= NULL
);
54 assert(audio_format_defined(audio_format
));
55 assert(audio_format_valid(audio_format
));
57 dc
->in_audio_format
= *audio_format
;
58 getOutputAudioFormat(audio_format
, &dc
->out_audio_format
);
60 dc
->seekable
= seekable
;
61 dc
->total_time
= total_time
;
64 dc
->state
= DECODE_STATE_DECODE
;
69 g_debug("audio_format=%s, seekable=%s",
70 audio_format_to_string(&dc
->in_audio_format
, &af_string
),
71 seekable
? "true" : "false");
73 if (!audio_format_equals(&dc
->in_audio_format
,
74 &dc
->out_audio_format
))
75 g_debug("converting to %s",
76 audio_format_to_string(&dc
->out_audio_format
,
80 enum decoder_command
decoder_get_command(G_GNUC_UNUSED
struct decoder
* decoder
)
82 const struct decoder_control
*dc
= decoder
->dc
;
84 assert(dc
->pipe
!= NULL
);
90 decoder_command_finished(struct decoder
*decoder
)
92 struct decoder_control
*dc
= decoder
->dc
;
96 assert(dc
->command
!= DECODE_COMMAND_NONE
);
97 assert(dc
->command
!= DECODE_COMMAND_SEEK
||
98 dc
->seek_error
|| decoder
->seeking
);
99 assert(dc
->pipe
!= NULL
);
101 if (decoder
->seeking
) {
102 decoder
->seeking
= false;
104 /* delete frames from the old song position */
106 if (decoder
->chunk
!= NULL
) {
107 music_buffer_return(dc
->buffer
, decoder
->chunk
);
108 decoder
->chunk
= NULL
;
111 music_pipe_clear(dc
->pipe
, dc
->buffer
);
113 decoder
->timestamp
= dc
->seek_where
;
116 dc
->command
= DECODE_COMMAND_NONE
;
119 player_lock_signal();
122 double decoder_seek_where(G_GNUC_UNUSED
struct decoder
* decoder
)
124 const struct decoder_control
*dc
= decoder
->dc
;
126 assert(dc
->command
== DECODE_COMMAND_SEEK
);
127 assert(dc
->pipe
!= NULL
);
129 decoder
->seeking
= true;
131 return dc
->seek_where
;
134 void decoder_seek_error(struct decoder
* decoder
)
136 struct decoder_control
*dc
= decoder
->dc
;
138 assert(dc
->command
== DECODE_COMMAND_SEEK
);
139 assert(dc
->pipe
!= NULL
);
141 dc
->seek_error
= true;
142 decoder
->seeking
= false;
144 decoder_command_finished(decoder
);
147 size_t decoder_read(struct decoder
*decoder
,
148 struct input_stream
*is
,
149 void *buffer
, size_t length
)
151 const struct decoder_control
*dc
=
152 decoder
!= NULL
? decoder
->dc
: NULL
;
153 GError
*error
= NULL
;
156 assert(decoder
== NULL
||
157 dc
->state
== DECODE_STATE_START
||
158 dc
->state
== DECODE_STATE_DECODE
);
160 assert(buffer
!= NULL
);
166 /* XXX don't allow decoder==NULL */
167 if (decoder
!= NULL
&&
168 /* ignore the SEEK command during initialization,
169 the plugin should handle that after it has
170 initialized successfully */
171 (dc
->command
!= DECODE_COMMAND_SEEK
||
172 (dc
->state
!= DECODE_STATE_START
&& !decoder
->seeking
)) &&
173 dc
->command
!= DECODE_COMMAND_NONE
)
176 nbytes
= input_stream_read(is
, buffer
, length
, &error
);
178 if (G_UNLIKELY(nbytes
== 0 && error
!= NULL
)) {
179 g_warning("%s", error
->message
);
184 if (nbytes
> 0 || input_stream_eof(is
))
187 /* sleep for a fraction of a second! */
188 /* XXX don't sleep, wait for an event instead */
194 decoder_timestamp(struct decoder
*decoder
, double t
)
196 assert(decoder
!= NULL
);
199 decoder
->timestamp
= t
;
203 * Sends a #tag as-is to the music pipe. Flushes the current chunk
204 * (decoder.chunk) if there is one.
206 static enum decoder_command
207 do_send_tag(struct decoder
*decoder
, struct input_stream
*is
,
208 const struct tag
*tag
)
210 struct music_chunk
*chunk
;
212 if (decoder
->chunk
!= NULL
) {
213 /* there is a partial chunk - flush it, we want the
214 tag in a new chunk */
215 decoder_flush_chunk(decoder
);
216 player_lock_signal();
219 assert(decoder
->chunk
== NULL
);
221 chunk
= decoder_get_chunk(decoder
, is
);
223 assert(decoder
->dc
->command
!= DECODE_COMMAND_NONE
);
224 return decoder
->dc
->command
;
227 chunk
->tag
= tag_dup(tag
);
228 return DECODE_COMMAND_NONE
;
232 update_stream_tag(struct decoder
*decoder
, struct input_stream
*is
)
237 ? input_stream_tag(is
)
240 tag
= decoder
->song_tag
;
244 /* no stream tag present - submit the song tag
246 decoder
->song_tag
= NULL
;
249 if (decoder
->stream_tag
!= NULL
)
250 tag_free(decoder
->stream_tag
);
252 decoder
->stream_tag
= tag
;
257 decoder_data(struct decoder
*decoder
,
258 struct input_stream
*is
,
259 const void *_data
, size_t length
,
262 struct decoder_control
*dc
= decoder
->dc
;
263 const char *data
= _data
;
264 GError
*error
= NULL
;
265 enum decoder_command cmd
;
267 assert(dc
->state
== DECODE_STATE_DECODE
);
268 assert(dc
->pipe
!= NULL
);
269 assert(length
% audio_format_frame_size(&dc
->in_audio_format
) == 0);
275 if (cmd
== DECODE_COMMAND_STOP
|| cmd
== DECODE_COMMAND_SEEK
||
279 /* send stream tags */
281 if (update_stream_tag(decoder
, is
)) {
282 if (decoder
->decoder_tag
!= NULL
) {
283 /* merge with tag from decoder plugin */
286 tag
= tag_merge(decoder
->decoder_tag
,
287 decoder
->stream_tag
);
288 cmd
= do_send_tag(decoder
, is
, tag
);
291 /* send only the stream tag */
292 cmd
= do_send_tag(decoder
, is
, decoder
->stream_tag
);
294 if (cmd
!= DECODE_COMMAND_NONE
)
298 if (!audio_format_equals(&dc
->in_audio_format
, &dc
->out_audio_format
)) {
299 data
= pcm_convert(&decoder
->conv_state
,
300 &dc
->in_audio_format
, data
, length
,
301 &dc
->out_audio_format
, &length
,
304 /* the PCM conversion has failed - stop
305 playback, since we have no better way to
307 g_warning("%s", error
->message
);
308 return DECODE_COMMAND_STOP
;
313 struct music_chunk
*chunk
;
318 chunk
= decoder_get_chunk(decoder
, is
);
320 assert(dc
->command
!= DECODE_COMMAND_NONE
);
324 dest
= music_chunk_write(chunk
, &dc
->out_audio_format
,
326 dc
->song
->start_ms
/ 1000.0,
329 /* the chunk is full, flush it */
330 decoder_flush_chunk(decoder
);
331 player_lock_signal();
340 /* copy the buffer */
342 memcpy(dest
, data
, nbytes
);
344 /* expand the music pipe chunk */
346 full
= music_chunk_expand(chunk
, &dc
->out_audio_format
, nbytes
);
348 /* the chunk is full, flush it */
349 decoder_flush_chunk(decoder
);
350 player_lock_signal();
356 decoder
->timestamp
+= (double)nbytes
/
357 audio_format_time_to_size(&dc
->out_audio_format
);
359 if (dc
->song
->end_ms
> 0 &&
360 decoder
->timestamp
>= dc
->song
->end_ms
/ 1000.0)
361 /* the end of this range has been reached:
363 return DECODE_COMMAND_STOP
;
366 return DECODE_COMMAND_NONE
;
370 decoder_tag(G_GNUC_UNUSED
struct decoder
*decoder
, struct input_stream
*is
,
371 const struct tag
*tag
)
373 G_GNUC_UNUSED
const struct decoder_control
*dc
= decoder
->dc
;
374 enum decoder_command cmd
;
376 assert(dc
->state
== DECODE_STATE_DECODE
);
377 assert(dc
->pipe
!= NULL
);
382 if (decoder
->decoder_tag
!= NULL
)
383 tag_free(decoder
->decoder_tag
);
384 decoder
->decoder_tag
= tag_dup(tag
);
386 /* check for a new stream tag */
388 update_stream_tag(decoder
, is
);
390 /* send tag to music pipe */
392 if (decoder
->stream_tag
!= NULL
) {
393 /* merge with tag from input stream */
396 merged
= tag_merge(decoder
->stream_tag
, decoder
->decoder_tag
);
397 cmd
= do_send_tag(decoder
, is
, merged
);
400 /* send only the decoder tag */
401 cmd
= do_send_tag(decoder
, is
, tag
);
407 decoder_replay_gain(struct decoder
*decoder
,
408 const struct replay_gain_info
*replay_gain_info
)
410 assert(decoder
!= NULL
);
412 if (replay_gain_info
!= NULL
) {
413 static unsigned serial
;
417 decoder
->replay_gain_info
= *replay_gain_info
;
418 decoder
->replay_gain_serial
= serial
;
420 if (decoder
->chunk
!= NULL
) {
421 /* flush the current chunk because the new
422 replay gain values affect the following
424 decoder_flush_chunk(decoder
);
425 player_lock_signal();
428 decoder
->replay_gain_serial
= 0;
432 decoder_mixramp(struct decoder
*decoder
,
433 char *mixramp_start
, char *mixramp_end
)
435 assert(decoder
!= NULL
);
436 struct decoder_control
*dc
= decoder
->dc
;
439 dc_mixramp_start(dc
, mixramp_start
);
440 dc_mixramp_end(dc
, mixramp_end
);