configure.ac: Add subheaders to Input/Sticker/Autodiscovery/Metadata and Misc.
[mpd-mk.git] / src / player_thread.c
blob8fa089c1fb6a1ad0acc6fff6dfff0e99ee4ab36d
1 /*
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.
20 #include "config.h"
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"
27 #include "path.h"
28 #include "event_pipe.h"
29 #include "crossfade.h"
30 #include "song.h"
31 #include "tag.h"
32 #include "pipe.h"
33 #include "chunk.h"
34 #include "idle.h"
35 #include "main.h"
36 #include "buffer.h"
38 #include <glib.h>
40 #undef G_LOG_DOMAIN
41 #define G_LOG_DOMAIN "player_thread"
43 enum xfade_state {
44 XFADE_DISABLED = -1,
45 XFADE_UNKNOWN = 0,
46 XFADE_ENABLED = 1
49 struct player {
50 struct decoder_control *dc;
52 struct music_pipe *pipe;
54 /**
55 * are we waiting for buffered_before_play?
57 bool buffering;
59 /**
60 * true if the decoder is starting and did not provide data
61 * yet
63 bool decoder_starting;
65 /**
66 * is the player paused?
68 bool paused;
70 /**
71 * is there a new song in pc.next_song?
73 bool queued;
75 /**
76 * the song currently being played
78 struct song *song;
80 /**
81 * is cross fading enabled?
83 enum xfade_state xfade;
85 /**
86 * has cross-fading begun?
88 bool cross_fading;
90 /**
91 * The number of chunks used for crossfading.
93 unsigned cross_fade_chunks;
95 /**
96 * The tag of the "next" song during cross-fade. It is
97 * postponed, and sent to the output thread when the new song
98 * really begins.
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
112 * precisly.
114 float elapsed_time;
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)
129 player_lock();
130 player_command_finished_locked();
131 player_unlock();
135 * Start the decoder.
137 * Player lock is not held.
139 static void
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.
155 static void
156 player_dc_stop(struct player *player)
158 struct decoder_control *dc = player->dc;
160 dc_stop(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);
170 dc->pipe = NULL;
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.
179 static bool
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.
192 static bool
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)) {
203 player_lock();
204 pc.errored_song = dc->song;
205 pc.error = PLAYER_ERROR_FILE;
206 pc.next_song = NULL;
207 player_unlock();
209 return false;
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;
219 player_lock();
221 /* update player_control's song information */
222 pc.total_time = song_get_duration(pc.next_song);
223 pc.bit_rate = 0;
224 audio_format_clear(&pc.audio_format);
226 /* clear the queued song */
227 pc.next_song = NULL;
229 player_unlock();
231 /* call syncPlaylistWithQueue() in the main thread */
232 event_pipe_emit(PIPE_EVENT_PLAYLIST);
234 return true;
238 * Returns the real duration of the song, comprising the duration
239 * indicated by the decoder plugin.
241 static double
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.
264 static bool
265 player_check_decoder_startup(struct player *player)
267 struct decoder_control *dc = player->dc;
269 assert(player->decoder_starting);
271 decoder_lock(dc);
273 if (decoder_has_failed(dc)) {
274 /* the decoder failed */
275 decoder_unlock(dc);
277 player_lock();
278 pc.errored_song = dc->song;
279 pc.error = PLAYER_ERROR_FILE;
280 player_unlock();
282 return false;
283 } else if (!decoder_is_starting(dc)) {
284 /* the decoder is ready and ok */
286 decoder_unlock(dc);
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 */
292 return true;
294 player_lock();
295 pc.total_time = real_song_duration(dc->song, dc->total_time);
296 pc.audio_format = dc->in_audio_format;
297 player_unlock();
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,
304 player_buffer)) {
305 char *uri = song_get_uri(dc->song);
306 g_warning("problems opening audio device "
307 "while playing \"%s\"", uri);
308 g_free(uri);
310 player_lock();
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;
316 player_unlock();
318 player->paused = true;
319 return true;
322 return true;
323 } else {
324 /* the decoder is not yet ready; wait
325 some more */
326 player_wait_decoder(dc);
327 decoder_unlock(dc);
329 return true;
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.
340 static bool
341 player_send_silence(struct player *player)
343 struct music_chunk *chunk;
344 size_t frame_size =
345 audio_format_frame_size(&player->play_audio_format);
346 /* this formula ensures that we don't send
347 partial frames */
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);
353 if (chunk == NULL) {
354 g_warning("Failed to allocate silence buffer");
355 return false;
358 #ifndef NDEBUG
359 chunk->audio_format = player->play_audio_format;
360 #endif
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);
368 return false;
371 return true;
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;
383 double where;
384 bool ret;
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
395 pipe */
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);
401 if (!ret) {
402 /* decoder failure */
403 player_command_finished();
404 return false;
406 } else {
407 pc.next_song = NULL;
408 player->queued = false;
411 /* wait for the decoder to complete initialization */
413 while (player->decoder_starting) {
414 ret = player_check_decoder_startup(player);
415 if (!ret) {
416 /* decoder failure */
417 player_command_finished();
418 return false;
422 /* send the SEEK command */
424 where = pc.seek_where;
425 if (where > pc.total_time)
426 where = pc.total_time - 0.1;
427 if (where < 0.0)
428 where = 0.0;
430 ret = dc_seek(dc, where + song->start_ms / 1000.0);
431 if (!ret) {
432 /* decoder failure */
433 player_command_finished();
434 return false;
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();
448 return true;
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:
463 break;
465 case PLAYER_COMMAND_UPDATE_AUDIO:
466 player_unlock();
467 audio_output_all_enable_disable();
468 player_lock();
469 player_command_finished_locked();
470 break;
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();
479 break;
481 case PLAYER_COMMAND_PAUSE:
482 player_unlock();
484 player->paused = !player->paused;
485 if (player->paused) {
486 audio_output_all_pause();
487 player_lock();
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 */
493 player_lock();
495 pc.state = PLAYER_STATE_PLAY;
496 } else if (audio_output_all_open(&player->play_audio_format, player_buffer)) {
497 /* unpaused, continue playing */
498 player_lock();
500 pc.state = PLAYER_STATE_PLAY;
501 } else {
502 /* the audio device has failed - rollback to
503 pause mode */
504 pc.error = PLAYER_ERROR_AUDIO;
506 player->paused = true;
508 player_lock();
511 player_command_finished_locked();
512 break;
514 case PLAYER_COMMAND_SEEK:
515 player_unlock();
516 player_seek_decoder(player);
517 player_lock();
518 break;
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
524 everything now */
525 pc.command = PLAYER_COMMAND_STOP;
526 return;
529 if (decoding_next_song(player)) {
530 /* the decoder is already decoding the song -
531 stop it and reset the position */
532 player_unlock();
533 player_dc_stop(player);
534 player_lock();
537 pc.next_song = NULL;
538 player->queued = false;
539 player_command_finished_locked();
540 break;
542 case PLAYER_COMMAND_REFRESH:
543 if (audio_format_defined(&player->play_audio_format) &&
544 !player->paused) {
545 player_unlock();
546 audio_output_all_check();
547 player_lock();
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();
555 break;
559 static void
560 update_song_tag(struct song *song, const struct tag *new_tag)
562 struct tag *old_tag;
564 if (song_is_file(song))
565 /* don't update tags of local files, only remote
566 streams may change tags dynamically */
567 return;
569 old_tag = song->tag;
570 song->tag = tag_dup(new_tag);
572 if (old_tag != NULL)
573 tag_free(old_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
580 changed */
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.
591 static bool
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);
602 return true;
605 pc.bit_rate = chunk->bit_rate;
607 /* send the chunk to the audio outputs */
609 if (!audio_output_all_play(chunk))
610 return false;
612 pc.total_play_time += (double)chunk->length /
613 audio_format_time_to_size(format);
614 return true;
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)
623 static bool
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;
629 bool success;
631 if (!audio_output_all_wait(64))
632 /* the output pipe is still large enough, don't send
633 another chunk */
634 return true;
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
648 song */
649 player->cross_fade_chunks = cross_fade_position;
650 player->cross_fading = true;
653 if (other_chunk != NULL) {
654 float mix_ratio;
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,
664 other_chunk->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;
670 } else {
671 mix_ratio = nan("");
674 cross_fade_apply(chunk, other_chunk,
675 &dc->out_audio_format,
676 mix_ratio);
677 music_buffer_return(player_buffer, other_chunk);
678 } else {
679 /* there are not enough decoded chunks yet */
681 decoder_lock(dc);
683 if (decoder_is_idle(dc)) {
684 /* the decoder isn't running, abort
685 cross fading */
686 decoder_unlock(dc);
688 player->xfade = XFADE_DISABLED;
689 } else {
690 /* wait for the decoder */
691 decoder_signal(dc);
692 player_wait_decoder(dc);
693 decoder_unlock(dc);
695 return true;
700 if (chunk == NULL)
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);
717 if (!success) {
718 music_buffer_return(player_buffer, chunk);
720 player_lock();
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;
729 player_unlock();
731 return false;
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 */
737 decoder_lock(dc);
738 if (!decoder_is_idle(dc) &&
739 music_pipe_size(dc->pipe) <= (pc.buffered_before_play +
740 music_buffer_size(player_buffer) * 3) / 4)
741 decoder_signal(dc);
742 decoder_unlock(dc);
744 return true;
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)
756 static bool
757 player_song_border(struct player *player)
759 char *uri;
761 player->xfade = XFADE_UNKNOWN;
763 uri = song_get_uri(player->song);
764 g_message("played \"%s\"", uri);
765 g_free(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))
773 return false;
775 return true;
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 = {
786 .dc = dc,
787 .buffering = true,
788 .decoder_starting = false,
789 .paused = false,
790 .queued = true,
791 .song = NULL,
792 .xfade = XFADE_UNKNOWN,
793 .cross_fading = false,
794 .cross_fade_chunks = 0,
795 .cross_fade_tag = NULL,
796 .elapsed_time = 0.0,
799 player_unlock();
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);
809 player_lock();
810 return;
813 player_lock();
814 pc.state = PLAYER_STATE_PLAY;
815 player_command_finished_locked();
817 while (true) {
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) {
822 player_unlock();
823 audio_output_all_cancel();
824 break;
827 player_unlock();
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))
842 break;
844 decoder_lock(dc);
845 /* XXX race condition: check decoder again */
846 player_wait_decoder(dc);
847 decoder_unlock(dc);
848 player_lock();
849 continue;
850 } else {
851 /* buffering is complete */
852 player.buffering = false;
856 if (player.decoder_starting) {
857 /* wait until the decoder is initialized completely */
858 bool success;
859 const struct song *song;
861 success = player_check_decoder_startup(&player);
862 if (!success)
863 break;
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);
871 player_lock();
872 continue;
875 #ifndef NDEBUG
877 music_pipe_check_format(&play_audio_format,
878 player.next_song_chunk,
879 &dc->out_audio_format);
881 #endif
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
897 for it */
898 player.cross_fade_chunks =
899 cross_fade_calc(pc.cross_fade_seconds, dc->total_time,
900 pc.mixramp_db,
901 pc.mixramp_delay_seconds,
902 dc->mixramp_start,
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;
911 } else
912 /* cross fading is disabled or the
913 next song is too short */
914 player.xfade = XFADE_DISABLED;
917 if (player.paused) {
918 player_lock();
920 if (pc.command == PLAYER_COMMAND_NONE)
921 player_wait();
922 continue;
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
931 okay */
933 /* XXX synchronize in a better way */
934 g_usleep(10000);
935 } else if (decoding_next_song(&player)) {
936 /* at the beginning of a new song */
938 if (!player_song_border(&player))
939 break;
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
946 playback */
947 audio_output_all_drain();
948 break;
950 } else {
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))
955 break;
958 player_lock();
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);
969 player_lock();
971 if (player.queued) {
972 assert(pc.next_song != NULL);
973 pc.next_song = NULL;
976 pc.state = PLAYER_STATE_STOP;
978 player_unlock();
980 event_pipe_emit(PIPE_EVENT_PLAYLIST);
982 player_lock();
985 static gpointer player_task(G_GNUC_UNUSED gpointer arg)
987 struct decoder_control dc;
989 dc_init(&dc);
990 decoder_thread_start(&dc);
992 player_buffer = music_buffer_new(pc.buffer_chunks);
994 player_lock();
996 while (1) {
997 switch (pc.command) {
998 case PLAYER_COMMAND_QUEUE:
999 assert(pc.next_song != NULL);
1001 do_play(&dc);
1002 break;
1004 case PLAYER_COMMAND_STOP:
1005 player_unlock();
1006 audio_output_all_cancel();
1007 player_lock();
1009 /* fall through */
1011 case PLAYER_COMMAND_SEEK:
1012 case PLAYER_COMMAND_PAUSE:
1013 pc.next_song = NULL;
1014 player_command_finished_locked();
1015 break;
1017 case PLAYER_COMMAND_CLOSE_AUDIO:
1018 player_unlock();
1020 audio_output_all_release();
1022 player_lock();
1023 player_command_finished_locked();
1025 #ifndef NDEBUG
1026 /* in the DEBUG build, check for leaked
1027 music_chunk objects by freeing the
1028 music_buffer */
1029 music_buffer_free(player_buffer);
1030 player_buffer = music_buffer_new(pc.buffer_chunks);
1031 #endif
1033 break;
1035 case PLAYER_COMMAND_UPDATE_AUDIO:
1036 player_unlock();
1037 audio_output_all_enable_disable();
1038 player_lock();
1039 player_command_finished_locked();
1040 break;
1042 case PLAYER_COMMAND_EXIT:
1043 player_unlock();
1045 dc_quit(&dc);
1046 dc_deinit(&dc);
1047 audio_output_all_close();
1048 music_buffer_free(player_buffer);
1050 player_command_finished();
1051 return NULL;
1053 case PLAYER_COMMAND_CANCEL:
1054 pc.next_song = NULL;
1055 player_command_finished_locked();
1056 break;
1058 case PLAYER_COMMAND_REFRESH:
1059 /* no-op when not playing */
1060 player_command_finished_locked();
1061 break;
1063 case PLAYER_COMMAND_NONE:
1064 player_wait();
1065 break;
1070 void player_create(void)
1072 GError *e = NULL;
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);