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 "player_thread.h"
22 #include "player_control.h"
23 #include "decoder_control.h"
24 #include "decoder_thread.h"
25 #include "output_all.h"
26 #include "pcm_volume.h"
28 #include "event_pipe.h"
29 #include "crossfade.h"
41 #define G_LOG_DOMAIN "player_thread"
50 struct decoder_control
*dc
;
52 struct music_pipe
*pipe
;
55 * are we waiting for buffered_before_play?
60 * true if the decoder is starting and did not provide data
63 bool decoder_starting
;
66 * is the player paused?
71 * is there a new song in pc.next_song?
76 * the song currently being played
81 * is cross fading enabled?
83 enum xfade_state xfade
;
86 * has cross-fading begun?
91 * The number of chunks used for crossfading.
93 unsigned cross_fade_chunks
;
96 * The tag of the "next" song during cross-fade. It is
97 * postponed, and sent to the output thread when the new song
100 struct tag
*cross_fade_tag
;
103 * The current audio format for the audio outputs.
105 struct audio_format play_audio_format
;
108 * The time stamp of the chunk most recently sent to the
109 * output thread. This attribute is only used if
110 * audio_output_all_get_elapsed_time() didn't return a usable
111 * value; the output thread can estimate the elapsed time more
117 static struct music_buffer
*player_buffer
;
119 static void player_command_finished_locked(void)
121 assert(pc
.command
!= PLAYER_COMMAND_NONE
);
123 pc
.command
= PLAYER_COMMAND_NONE
;
124 g_cond_signal(main_cond
);
127 static void player_command_finished(void)
130 player_command_finished_locked();
137 * Player lock is not held.
140 player_dc_start(struct player
*player
, struct music_pipe
*pipe
)
142 struct decoder_control
*dc
= player
->dc
;
144 assert(player
->queued
|| pc
.command
== PLAYER_COMMAND_SEEK
);
145 assert(pc
.next_song
!= NULL
);
147 dc_start(dc
, pc
.next_song
, player_buffer
, pipe
);
151 * Stop the decoder and clears (and frees) its music pipe.
153 * Player lock is not held.
156 player_dc_stop(struct player
*player
)
158 struct decoder_control
*dc
= player
->dc
;
162 if (dc
->pipe
!= NULL
) {
163 /* clear and free the decoder pipe */
165 music_pipe_clear(dc
->pipe
, player_buffer
);
167 if (dc
->pipe
!= player
->pipe
)
168 music_pipe_free(dc
->pipe
);
175 * Returns true if the decoder is decoding the next song (or has begun
176 * decoding it, or has finished doing it), and the player hasn't
177 * switched to that song yet.
180 decoding_next_song(const struct player
*player
)
182 return player
->dc
->pipe
!= NULL
&& player
->dc
->pipe
!= player
->pipe
;
186 * After the decoder has been started asynchronously, wait for the
187 * "START" command to finish. The decoder may not be initialized yet,
188 * i.e. there is no audio_format information yet.
190 * The player lock is not held.
193 player_wait_for_decoder(struct player
*player
)
195 struct decoder_control
*dc
= player
->dc
;
197 assert(player
->queued
|| pc
.command
== PLAYER_COMMAND_SEEK
);
198 assert(pc
.next_song
!= NULL
);
200 player
->queued
= false;
202 if (decoder_lock_has_failed(dc
)) {
204 pc
.errored_song
= dc
->song
;
205 pc
.error
= PLAYER_ERROR_FILE
;
212 player
->song
= pc
.next_song
;
213 player
->elapsed_time
= 0.0;
215 /* set the "starting" flag, which will be cleared by
216 player_check_decoder_startup() */
217 player
->decoder_starting
= true;
221 /* update player_control's song information */
222 pc
.total_time
= song_get_duration(pc
.next_song
);
224 audio_format_clear(&pc
.audio_format
);
226 /* clear the queued song */
231 /* call syncPlaylistWithQueue() in the main thread */
232 event_pipe_emit(PIPE_EVENT_PLAYLIST
);
238 * Returns the real duration of the song, comprising the duration
239 * indicated by the decoder plugin.
242 real_song_duration(const struct song
*song
, double decoder_duration
)
244 assert(song
!= NULL
);
246 if (decoder_duration
<= 0.0)
247 /* the decoder plugin didn't provide information; fall
248 back to song_get_duration() */
249 return song_get_duration(song
);
251 if (song
->end_ms
> 0 && song
->end_ms
/ 1000.0 < decoder_duration
)
252 return (song
->end_ms
- song
->start_ms
) / 1000.0;
254 return decoder_duration
- song
->start_ms
/ 1000.0;
258 * The decoder has acknowledged the "START" command (see
259 * player_wait_for_decoder()). This function checks if the decoder
260 * initialization has completed yet.
262 * The player lock is not held.
265 player_check_decoder_startup(struct player
*player
)
267 struct decoder_control
*dc
= player
->dc
;
269 assert(player
->decoder_starting
);
273 if (decoder_has_failed(dc
)) {
274 /* the decoder failed */
278 pc
.errored_song
= dc
->song
;
279 pc
.error
= PLAYER_ERROR_FILE
;
283 } else if (!decoder_is_starting(dc
)) {
284 /* the decoder is ready and ok */
288 if (audio_format_defined(&player
->play_audio_format
) &&
289 !audio_output_all_wait(1))
290 /* the output devices havn't finished playing
291 all chunks yet - wait for that */
295 pc
.total_time
= real_song_duration(dc
->song
, dc
->total_time
);
296 pc
.audio_format
= dc
->in_audio_format
;
299 player
->play_audio_format
= dc
->out_audio_format
;
300 player
->decoder_starting
= false;
302 if (!player
->paused
&&
303 !audio_output_all_open(&dc
->out_audio_format
,
305 char *uri
= song_get_uri(dc
->song
);
306 g_warning("problems opening audio device "
307 "while playing \"%s\"", uri
);
311 pc
.error
= PLAYER_ERROR_AUDIO
;
313 /* pause: the user may resume playback as soon
314 as an audio output becomes available */
315 pc
.state
= PLAYER_STATE_PAUSE
;
318 player
->paused
= true;
324 /* the decoder is not yet ready; wait
326 player_wait_decoder(dc
);
334 * Sends a chunk of silence to the audio outputs. This is called when
335 * there is not enough decoded data in the pipe yet, to prevent
336 * underruns in the hardware buffers.
338 * The player lock is not held.
341 player_send_silence(struct player
*player
)
343 struct music_chunk
*chunk
;
345 audio_format_frame_size(&player
->play_audio_format
);
346 /* this formula ensures that we don't send
348 unsigned num_frames
= sizeof(chunk
->data
) / frame_size
;
350 assert(audio_format_defined(&player
->play_audio_format
));
352 chunk
= music_buffer_allocate(player_buffer
);
354 g_warning("Failed to allocate silence buffer");
359 chunk
->audio_format
= player
->play_audio_format
;
362 chunk
->times
= -1.0; /* undefined time stamp */
363 chunk
->length
= num_frames
* frame_size
;
364 memset(chunk
->data
, 0, chunk
->length
);
366 if (!audio_output_all_play(chunk
)) {
367 music_buffer_return(player_buffer
, chunk
);
375 * This is the handler for the #PLAYER_COMMAND_SEEK command.
377 * The player lock is not held.
379 static bool player_seek_decoder(struct player
*player
)
381 struct song
*song
= pc
.next_song
;
382 struct decoder_control
*dc
= player
->dc
;
386 assert(pc
.next_song
!= NULL
);
388 if (decoder_current_song(dc
) != song
) {
389 /* the decoder is already decoding the "next" song -
390 stop it and start the previous song again */
392 player_dc_stop(player
);
394 /* clear music chunks which might still reside in the
396 music_pipe_clear(player
->pipe
, player_buffer
);
398 /* re-start the decoder */
399 player_dc_start(player
, player
->pipe
);
400 ret
= player_wait_for_decoder(player
);
402 /* decoder failure */
403 player_command_finished();
408 player
->queued
= false;
411 /* wait for the decoder to complete initialization */
413 while (player
->decoder_starting
) {
414 ret
= player_check_decoder_startup(player
);
416 /* decoder failure */
417 player_command_finished();
422 /* send the SEEK command */
424 where
= pc
.seek_where
;
425 if (where
> pc
.total_time
)
426 where
= pc
.total_time
- 0.1;
430 ret
= dc_seek(dc
, where
+ song
->start_ms
/ 1000.0);
432 /* decoder failure */
433 player_command_finished();
437 player
->elapsed_time
= where
;
439 player_command_finished();
441 player
->xfade
= XFADE_UNKNOWN
;
443 /* re-fill the buffer after seeking */
444 player
->buffering
= true;
446 audio_output_all_cancel();
452 * Player lock must be held before calling.
454 static void player_process_command(struct player
*player
)
456 G_GNUC_UNUSED
struct decoder_control
*dc
= player
->dc
;
458 switch (pc
.command
) {
459 case PLAYER_COMMAND_NONE
:
460 case PLAYER_COMMAND_STOP
:
461 case PLAYER_COMMAND_EXIT
:
462 case PLAYER_COMMAND_CLOSE_AUDIO
:
465 case PLAYER_COMMAND_UPDATE_AUDIO
:
467 audio_output_all_enable_disable();
469 player_command_finished_locked();
472 case PLAYER_COMMAND_QUEUE
:
473 assert(pc
.next_song
!= NULL
);
474 assert(!player
->queued
);
475 assert(dc
->pipe
== NULL
|| dc
->pipe
== player
->pipe
);
477 player
->queued
= true;
478 player_command_finished_locked();
481 case PLAYER_COMMAND_PAUSE
:
484 player
->paused
= !player
->paused
;
485 if (player
->paused
) {
486 audio_output_all_pause();
489 pc
.state
= PLAYER_STATE_PAUSE
;
490 } else if (!audio_format_defined(&player
->play_audio_format
)) {
491 /* the decoder hasn't provided an audio format
492 yet - don't open the audio device yet */
495 pc
.state
= PLAYER_STATE_PLAY
;
496 } else if (audio_output_all_open(&player
->play_audio_format
, player_buffer
)) {
497 /* unpaused, continue playing */
500 pc
.state
= PLAYER_STATE_PLAY
;
502 /* the audio device has failed - rollback to
504 pc
.error
= PLAYER_ERROR_AUDIO
;
506 player
->paused
= true;
511 player_command_finished_locked();
514 case PLAYER_COMMAND_SEEK
:
516 player_seek_decoder(player
);
520 case PLAYER_COMMAND_CANCEL
:
521 if (pc
.next_song
== NULL
) {
522 /* the cancel request arrived too late, we're
523 already playing the queued song... stop
525 pc
.command
= PLAYER_COMMAND_STOP
;
529 if (decoding_next_song(player
)) {
530 /* the decoder is already decoding the song -
531 stop it and reset the position */
533 player_dc_stop(player
);
538 player
->queued
= false;
539 player_command_finished_locked();
542 case PLAYER_COMMAND_REFRESH
:
543 if (audio_format_defined(&player
->play_audio_format
) &&
546 audio_output_all_check();
550 pc
.elapsed_time
= audio_output_all_get_elapsed_time();
551 if (pc
.elapsed_time
< 0.0)
552 pc
.elapsed_time
= player
->elapsed_time
;
554 player_command_finished_locked();
560 update_song_tag(struct song
*song
, const struct tag
*new_tag
)
564 if (song_is_file(song
))
565 /* don't update tags of local files, only remote
566 streams may change tags dynamically */
570 song
->tag
= tag_dup(new_tag
);
575 /* the main thread will update the playlist version when he
576 receives this event */
577 event_pipe_emit(PIPE_EVENT_TAG
);
579 /* notify all clients that the tag of the current song has
581 idle_add(IDLE_PLAYER
);
585 * Plays a #music_chunk object (after applying software volume). If
586 * it contains a (stream) tag, copy it to the current song, so MPD's
587 * playlist reflects the new stream tag.
589 * Player lock is not held.
592 play_chunk(struct song
*song
, struct music_chunk
*chunk
,
593 const struct audio_format
*format
)
595 assert(music_chunk_check_format(chunk
, format
));
597 if (chunk
->tag
!= NULL
)
598 update_song_tag(song
, chunk
->tag
);
600 if (chunk
->length
== 0) {
601 music_buffer_return(player_buffer
, chunk
);
605 pc
.bit_rate
= chunk
->bit_rate
;
607 /* send the chunk to the audio outputs */
609 if (!audio_output_all_play(chunk
))
612 pc
.total_play_time
+= (double)chunk
->length
/
613 audio_format_time_to_size(format
);
618 * Obtains the next chunk from the music pipe, optionally applies
619 * cross-fading, and sends it to all audio outputs.
621 * @return true on success, false on error (playback will be stopped)
624 play_next_chunk(struct player
*player
)
626 struct decoder_control
*dc
= player
->dc
;
627 struct music_chunk
*chunk
= NULL
;
628 unsigned cross_fade_position
;
631 if (!audio_output_all_wait(64))
632 /* the output pipe is still large enough, don't send
636 if (player
->xfade
== XFADE_ENABLED
&&
637 decoding_next_song(player
) &&
638 (cross_fade_position
= music_pipe_size(player
->pipe
))
639 <= player
->cross_fade_chunks
) {
640 /* perform cross fade */
641 struct music_chunk
*other_chunk
=
642 music_pipe_shift(dc
->pipe
);
644 if (!player
->cross_fading
) {
645 /* beginning of the cross fade - adjust
646 crossFadeChunks which might be bigger than
647 the remaining number of chunks in the old
649 player
->cross_fade_chunks
= cross_fade_position
;
650 player
->cross_fading
= true;
653 if (other_chunk
!= NULL
) {
656 chunk
= music_pipe_shift(player
->pipe
);
657 assert(chunk
!= NULL
);
659 /* don't send the tags of the new song (which
660 is being faded in) yet; postpone it until
661 the current song is faded out */
662 player
->cross_fade_tag
=
663 tag_merge_replace(player
->cross_fade_tag
,
665 other_chunk
->tag
= NULL
;
667 if (isnan(pc
.mixramp_delay_seconds
)) {
668 mix_ratio
= ((float)cross_fade_position
)
669 / player
->cross_fade_chunks
;
674 cross_fade_apply(chunk
, other_chunk
,
675 &dc
->out_audio_format
,
677 music_buffer_return(player_buffer
, other_chunk
);
679 /* there are not enough decoded chunks yet */
683 if (decoder_is_idle(dc
)) {
684 /* the decoder isn't running, abort
688 player
->xfade
= XFADE_DISABLED
;
690 /* wait for the decoder */
692 player_wait_decoder(dc
);
701 chunk
= music_pipe_shift(player
->pipe
);
703 assert(chunk
!= NULL
);
705 /* insert the postponed tag if cross-fading is finished */
707 if (player
->xfade
!= XFADE_ENABLED
&& player
->cross_fade_tag
!= NULL
) {
708 chunk
->tag
= tag_merge_replace(chunk
->tag
,
709 player
->cross_fade_tag
);
710 player
->cross_fade_tag
= NULL
;
713 /* play the current chunk */
715 success
= play_chunk(player
->song
, chunk
, &player
->play_audio_format
);
718 music_buffer_return(player_buffer
, chunk
);
722 pc
.error
= PLAYER_ERROR_AUDIO
;
724 /* pause: the user may resume playback as soon as an
725 audio output becomes available */
726 pc
.state
= PLAYER_STATE_PAUSE
;
727 player
->paused
= true;
734 /* this formula should prevent that the decoder gets woken up
735 with each chunk; it is more efficient to make it decode a
736 larger block at a time */
738 if (!decoder_is_idle(dc
) &&
739 music_pipe_size(dc
->pipe
) <= (pc
.buffered_before_play
+
740 music_buffer_size(player_buffer
) * 3) / 4)
748 * This is called at the border between two songs: the audio output
749 * has consumed all chunks of the current song, and we should start
750 * sending chunks from the next one.
752 * The player lock is not held.
754 * @return true on success, false on error (playback will be stopped)
757 player_song_border(struct player
*player
)
761 player
->xfade
= XFADE_UNKNOWN
;
763 uri
= song_get_uri(player
->song
);
764 g_message("played \"%s\"", uri
);
767 music_pipe_free(player
->pipe
);
768 player
->pipe
= player
->dc
->pipe
;
770 audio_output_all_song_border();
772 if (!player_wait_for_decoder(player
))
779 * The main loop of the player thread, during playback. This is
780 * basically a state machine, which multiplexes data between the
781 * decoder thread and the output threads.
783 static void do_play(struct decoder_control
*dc
)
785 struct player player
= {
788 .decoder_starting
= false,
792 .xfade
= XFADE_UNKNOWN
,
793 .cross_fading
= false,
794 .cross_fade_chunks
= 0,
795 .cross_fade_tag
= NULL
,
801 player
.pipe
= music_pipe_new();
803 player_dc_start(&player
, player
.pipe
);
804 if (!player_wait_for_decoder(&player
)) {
805 player_dc_stop(&player
);
806 player_command_finished();
807 music_pipe_free(player
.pipe
);
808 event_pipe_emit(PIPE_EVENT_PLAYLIST
);
814 pc
.state
= PLAYER_STATE_PLAY
;
815 player_command_finished_locked();
818 player_process_command(&player
);
819 if (pc
.command
== PLAYER_COMMAND_STOP
||
820 pc
.command
== PLAYER_COMMAND_EXIT
||
821 pc
.command
== PLAYER_COMMAND_CLOSE_AUDIO
) {
823 audio_output_all_cancel();
829 if (player
.buffering
) {
830 /* buffering at the start of the song - wait
831 until the buffer is large enough, to
832 prevent stuttering on slow machines */
834 if (music_pipe_size(player
.pipe
) < pc
.buffered_before_play
&&
835 !decoder_lock_is_idle(dc
)) {
836 /* not enough decoded buffer space yet */
838 if (!player
.paused
&&
839 audio_format_defined(&player
.play_audio_format
) &&
840 audio_output_all_check() < 4 &&
841 !player_send_silence(&player
))
845 /* XXX race condition: check decoder again */
846 player_wait_decoder(dc
);
851 /* buffering is complete */
852 player
.buffering
= false;
856 if (player
.decoder_starting
) {
857 /* wait until the decoder is initialized completely */
859 const struct song
*song
;
861 success
= player_check_decoder_startup(&player
);
865 /* seek to the beginning of the range */
866 song
= decoder_current_song(dc
);
867 if (song
!= NULL
&& song
->start_ms
> 0 &&
868 !dc_seek(dc
, song
->start_ms
/ 1000.0))
869 player_dc_stop(&player
);
877 music_pipe_check_format(&play_audio_format,
878 player.next_song_chunk,
879 &dc->out_audio_format);
883 if (decoder_lock_is_idle(dc
) && player
.queued
&&
884 dc
->pipe
== player
.pipe
) {
885 /* the decoder has finished the current song;
886 make it decode the next song */
887 assert(dc
->pipe
== NULL
|| dc
->pipe
== player
.pipe
);
889 player_dc_start(&player
, music_pipe_new());
892 if (decoding_next_song(&player
) &&
893 player
.xfade
== XFADE_UNKNOWN
&&
894 !decoder_lock_is_starting(dc
)) {
895 /* enable cross fading in this song? if yes,
896 calculate how many chunks will be required
898 player
.cross_fade_chunks
=
899 cross_fade_calc(pc
.cross_fade_seconds
, dc
->total_time
,
901 pc
.mixramp_delay_seconds
,
903 dc
->mixramp_prev_end
,
904 &dc
->out_audio_format
,
905 &player
.play_audio_format
,
906 music_buffer_size(player_buffer
) -
907 pc
.buffered_before_play
);
908 if (player
.cross_fade_chunks
> 0) {
909 player
.xfade
= XFADE_ENABLED
;
910 player
.cross_fading
= false;
912 /* cross fading is disabled or the
913 next song is too short */
914 player
.xfade
= XFADE_DISABLED
;
920 if (pc
.command
== PLAYER_COMMAND_NONE
)
923 } else if (music_pipe_size(player
.pipe
) > 0) {
924 /* at least one music chunk is ready - send it
925 to the audio output */
927 play_next_chunk(&player
);
928 } else if (audio_output_all_check() > 0) {
929 /* not enough data from decoder, but the
930 output thread is still busy, so it's
933 /* XXX synchronize in a better way */
935 } else if (decoding_next_song(&player
)) {
936 /* at the beginning of a new song */
938 if (!player_song_border(&player
))
940 } else if (decoder_lock_is_idle(dc
)) {
941 /* check the size of the pipe again, because
942 the decoder thread may have added something
943 since we last checked */
944 if (music_pipe_size(player
.pipe
) == 0) {
945 /* wait for the hardware to finish
947 audio_output_all_drain();
951 /* the decoder is too busy and hasn't provided
952 new PCM data in time: send silence (if the
953 output pipe is empty) */
954 if (!player_send_silence(&player
))
961 player_dc_stop(&player
);
963 music_pipe_clear(player
.pipe
, player_buffer
);
964 music_pipe_free(player
.pipe
);
966 if (player
.cross_fade_tag
!= NULL
)
967 tag_free(player
.cross_fade_tag
);
972 assert(pc
.next_song
!= NULL
);
976 pc
.state
= PLAYER_STATE_STOP
;
980 event_pipe_emit(PIPE_EVENT_PLAYLIST
);
985 static gpointer
player_task(G_GNUC_UNUSED gpointer arg
)
987 struct decoder_control dc
;
990 decoder_thread_start(&dc
);
992 player_buffer
= music_buffer_new(pc
.buffer_chunks
);
997 switch (pc
.command
) {
998 case PLAYER_COMMAND_QUEUE
:
999 assert(pc
.next_song
!= NULL
);
1004 case PLAYER_COMMAND_STOP
:
1006 audio_output_all_cancel();
1011 case PLAYER_COMMAND_SEEK
:
1012 case PLAYER_COMMAND_PAUSE
:
1013 pc
.next_song
= NULL
;
1014 player_command_finished_locked();
1017 case PLAYER_COMMAND_CLOSE_AUDIO
:
1020 audio_output_all_release();
1023 player_command_finished_locked();
1026 /* in the DEBUG build, check for leaked
1027 music_chunk objects by freeing the
1029 music_buffer_free(player_buffer
);
1030 player_buffer
= music_buffer_new(pc
.buffer_chunks
);
1035 case PLAYER_COMMAND_UPDATE_AUDIO
:
1037 audio_output_all_enable_disable();
1039 player_command_finished_locked();
1042 case PLAYER_COMMAND_EXIT
:
1047 audio_output_all_close();
1048 music_buffer_free(player_buffer
);
1050 player_command_finished();
1053 case PLAYER_COMMAND_CANCEL
:
1054 pc
.next_song
= NULL
;
1055 player_command_finished_locked();
1058 case PLAYER_COMMAND_REFRESH
:
1059 /* no-op when not playing */
1060 player_command_finished_locked();
1063 case PLAYER_COMMAND_NONE
:
1070 void player_create(void)
1074 assert(pc
.thread
== NULL
);
1076 pc
.thread
= g_thread_create(player_task
, NULL
, true, &e
);
1077 if (pc
.thread
== NULL
)
1078 g_error("Failed to spawn player task: %s", e
->message
);