player_thread.c 18.7 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19 20
 */

#include "player_thread.h"
21
#include "player_control.h"
22
#include "decoder_control.h"
23
#include "decoder_thread.h"
24
#include "output_all.h"
25
#include "pcm_volume.h"
26
#include "path.h"
27
#include "event_pipe.h"
28
#include "crossfade.h"
29
#include "song.h"
30
#include "tag.h"
31
#include "pipe.h"
32
#include "chunk.h"
33
#include "idle.h"
34
#include "main.h"
35
#include "buffer.h"
36

37 38
#include <glib.h>

39 40 41
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "player_thread"

42 43 44 45 46 47
enum xfade_state {
	XFADE_DISABLED = -1,
	XFADE_UNKNOWN = 0,
	XFADE_ENABLED = 1
};

48
struct player {
49 50
	struct music_pipe *pipe;

51
	/**
52
	 * are we waiting for buffered_before_play?
53
	 */
54
	bool buffering;
55 56 57 58 59 60 61 62 63 64 65 66

	/**
	 * true if the decoder is starting and did not provide data
	 * yet
	 */
	bool decoder_starting;

	/**
	 * is the player paused?
	 */
	bool paused;

67 68 69 70 71
	/**
	 * is there a new song in pc.next_song?
	 */
	bool queued;

72 73 74 75 76
	/**
	 * the song currently being played
	 */
	struct song *song;

77 78 79 80
	/**
	 * is cross fading enabled?
	 */
	enum xfade_state xfade;
81

82 83 84 85 86 87 88 89 90 91
	/**
	 * has cross-fading begun?
	 */
	bool cross_fading;

	/**
	 * The number of chunks used for crossfading.
	 */
	unsigned cross_fade_chunks;

92 93 94 95 96 97 98 99 100 101
	/**
	 * The current audio format for the audio outputs.
	 */
	struct audio_format play_audio_format;

	/**
	 * Coefficient for converting a PCM buffer size into a time
	 * span.
	 */
	double size_to_time;
102 103
};

104 105
static struct music_buffer *player_buffer;

106 107 108 109 110
static void player_command_finished(void)
{
	assert(pc.command != PLAYER_COMMAND_NONE);

	pc.command = PLAYER_COMMAND_NONE;
111
	notify_signal(&main_notify);
112 113
}

114 115 116
/**
 * Stop the decoder and clears (and frees) its music pipe.
 */
117 118 119 120 121 122
static void
player_dc_stop(struct player *player)
{
	dc_stop(&pc.notify);

	if (dc.pipe != NULL) {
123 124
		/* clear and free the decoder pipe */

125
		music_pipe_clear(dc.pipe, player_buffer);
126 127 128 129 130 131 132 133

		if (dc.pipe != player->pipe)
			music_pipe_free(dc.pipe);

		dc.pipe = NULL;
	}
}

134 135 136 137 138
/**
 * After the decoder has been started asynchronously, wait for the
 * "START" command to finish.  The decoder may not be initialized yet,
 * i.e. there is no audio_format information yet.
 */
139 140
static bool
player_wait_for_decoder(struct player *player)
141
{
Max Kellermann's avatar
Max Kellermann committed
142
	dc_command_wait(&pc.notify);
143

144
	if (decoder_has_failed()) {
145
		assert(dc.next_song == NULL || dc.next_song->url != NULL);
146 147
		pc.errored_song = dc.next_song;
		pc.error = PLAYER_ERROR_FILE;
148
		pc.next_song = NULL;
149
		player->queued = false;
150
		return false;
151 152
	}

Max Kellermann's avatar
Max Kellermann committed
153
	pc.total_time = pc.next_song->tag != NULL
154
		? pc.next_song->tag->time : 0;
Max Kellermann's avatar
Max Kellermann committed
155
	pc.bit_rate = 0;
156
	audio_format_clear(&pc.audio_format);
157

158
	player->song = pc.next_song;
159
	pc.next_song = NULL;
160
	pc.elapsed_time = 0;
161
	player->queued = false;
162 163 164

	/* set the "starting" flag, which will be cleared by
	   player_check_decoder_startup() */
165
	player->decoder_starting = true;
166

167 168 169
	/* call syncPlaylistWithQueue() in the main thread */
	event_pipe_emit(PIPE_EVENT_PLAYLIST);

170
	return true;
171 172
}

173 174 175 176 177
/**
 * The decoder has acknowledged the "START" command (see
 * player_wait_for_decoder()).  This function checks if the decoder
 * initialization has completed yet.
 */
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
static bool
player_check_decoder_startup(struct player *player)
{
	assert(player->decoder_starting);

	if (decoder_has_failed()) {
		/* the decoder failed */
		assert(dc.next_song == NULL || dc.next_song->url != NULL);

		pc.errored_song = dc.next_song;
		pc.error = PLAYER_ERROR_FILE;

		return false;
	} else if (!decoder_is_starting()) {
		/* the decoder is ready and ok */
193 194

		if (audio_format_defined(&player->play_audio_format) &&
195
		    !audio_output_all_wait(1))
196 197 198 199
			/* the output devices havn't finished playing
			   all chunks yet - wait for that */
			return true;

200 201 202 203 204
		pc.total_time = dc.total_time;
		pc.audio_format = dc.in_audio_format;
		player->play_audio_format = dc.out_audio_format;
		player->size_to_time =
			audioFormatSizeToTime(&dc.out_audio_format);
205 206
		player->decoder_starting = false;

207
		if (!player->paused &&
208 209
		    !audio_output_all_open(&dc.out_audio_format,
					   player_buffer)) {
210 211 212 213 214 215 216 217 218
			char *uri = song_get_uri(dc.next_song);
			g_warning("problems opening audio device "
				  "while playing \"%s\"", uri);
			g_free(uri);

			assert(dc.next_song == NULL || dc.next_song->url != NULL);
			pc.errored_song = dc.next_song;
			pc.error = PLAYER_ERROR_AUDIO;

219 220 221 222 223 224
			/* pause: the user may resume playback as soon
			   as an audio output becomes available */
			pc.state = PLAYER_STATE_PAUSE;
			player->paused = true;
			return true;
		}
225 226 227 228 229 230 231 232 233 234 235

		return true;
	} else {
		/* the decoder is not yet ready; wait
		   some more */
		notify_wait(&pc.notify);

		return true;
	}
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
/**
 * Sends a chunk of silence to the audio outputs.  This is called when
 * there is not enough decoded data in the pipe yet, to prevent
 * underruns in the hardware buffers.
 */
static bool
player_send_silence(struct player *player)
{
	struct music_chunk *chunk;
	size_t frame_size =
		audio_format_frame_size(&player->play_audio_format);
	/* this formula ensures that we don't send
	   partial frames */
	unsigned num_frames = sizeof(chunk->data) / frame_size;

	assert(audio_format_defined(&player->play_audio_format));

	chunk = music_buffer_allocate(player_buffer);
	if (chunk == NULL) {
		g_warning("Failed to allocate silence buffer");
		return false;
	}

#ifndef NDEBUG
	chunk->audio_format = player->play_audio_format;
#endif

	chunk->length = num_frames * frame_size;
	memset(chunk->data, 0, chunk->length);

	if (!audio_output_all_play(chunk)) {
		music_buffer_return(player_buffer, chunk);
		return false;
	}

	return true;
}

274 275 276
/**
 * This is the handler for the #PLAYER_COMMAND_SEEK command.
 */
Max Kellermann's avatar
Max Kellermann committed
277
static bool player_seek_decoder(struct player *player)
278
{
Max Kellermann's avatar
Max Kellermann committed
279
	double where;
280
	bool ret;
281

282 283
	assert(pc.next_song != NULL);

284
	if (decoder_current_song() != pc.next_song) {
285 286 287
		/* the decoder is already decoding the "next" song -
		   stop it and start the previous song again */

288 289
		player_dc_stop(player);

290 291
		/* clear music chunks which might still reside in the
		   pipe */
292
		music_pipe_clear(player->pipe, player_buffer);
293
		dc.pipe = player->pipe;
294

295 296
		/* re-start the decoder */
		dc_start_async(pc.next_song);
297
		ret = player_wait_for_decoder(player);
298
		if (!ret) {
299
			/* decoder failure */
300
			player_command_finished();
301
			return false;
302
		}
303 304 305
	} else {
		pc.next_song = NULL;
		player->queued = false;
306
	}
Max Kellermann's avatar
Max Kellermann committed
307

308 309 310 311 312 313 314 315 316 317 318
	/* wait for the decoder to complete initialization */

	while (player->decoder_starting) {
		ret = player_check_decoder_startup(player);
		if (!ret) {
			/* decoder failure */
			player_command_finished();
			return false;
		}
	}

319 320
	/* send the SEEK command */

Max Kellermann's avatar
Max Kellermann committed
321 322 323
	where = pc.seek_where;
	if (where > pc.total_time)
		where = pc.total_time - 0.1;
Max Kellermann's avatar
Max Kellermann committed
324 325 326 327
	if (where < 0.0)
		where = 0.0;

	ret = dc_seek(&pc.notify, where);
328
	if (!ret) {
329
		/* decoder failure */
330 331 332
		player_command_finished();
		return false;
	}
333

334
	pc.elapsed_time = where;
335 336
	player_command_finished();

337 338
	player->xfade = XFADE_UNKNOWN;

339 340
	/* re-fill the buffer after seeking */
	player->buffering = true;
341 342 343 344

	audio_output_all_cancel();

	return true;
345 346
}

Max Kellermann's avatar
Max Kellermann committed
347
static void player_process_command(struct player *player)
348 349 350 351 352
{
	switch (pc.command) {
	case PLAYER_COMMAND_NONE:
	case PLAYER_COMMAND_PLAY:
	case PLAYER_COMMAND_STOP:
Max Kellermann's avatar
Max Kellermann committed
353
	case PLAYER_COMMAND_EXIT:
354 355 356
	case PLAYER_COMMAND_CLOSE_AUDIO:
		break;

357 358
	case PLAYER_COMMAND_QUEUE:
		assert(pc.next_song != NULL);
359
		assert(!player->queued);
360
		assert(dc.pipe == NULL || dc.pipe == player->pipe);
361

362
		player->queued = true;
363 364 365 366
		player_command_finished();
		break;

	case PLAYER_COMMAND_PAUSE:
367 368
		player->paused = !player->paused;
		if (player->paused) {
369
			audio_output_all_pause();
370
			pc.state = PLAYER_STATE_PAUSE;
371 372 373 374 375 376
		} else if (!audio_format_defined(&player->play_audio_format)) {
			/* the decoder hasn't provided an audio format
			   yet - don't open the audio device yet */

			pc.state = PLAYER_STATE_PLAY;
		} else if (audio_output_all_open(&player->play_audio_format, player_buffer)) {
377
			/* unpaused, continue playing */
378
			pc.state = PLAYER_STATE_PLAY;
379
		} else {
380 381
			/* the audio device has failed - rollback to
			   pause mode */
382 383 384 385 386
			assert(dc.next_song == NULL || dc.next_song->url != NULL);
			pc.errored_song = dc.next_song;
			pc.error = PLAYER_ERROR_AUDIO;

			player->paused = true;
387
		}
388

389 390 391 392
		player_command_finished();
		break;

	case PLAYER_COMMAND_SEEK:
393
		player_seek_decoder(player);
394
		break;
395 396 397

	case PLAYER_COMMAND_CANCEL:
		if (pc.next_song == NULL) {
398
			/* the cancel request arrived too late, we're
399 400 401 402 403 404
			   already playing the queued song...  stop
			   everything now */
			pc.command = PLAYER_COMMAND_STOP;
			return;
		}

405
		if (dc.pipe != NULL && dc.pipe != player->pipe)
406 407
			/* the decoder is already decoding the song -
			   stop it and reset the position */
408
			player_dc_stop(player);
409 410 411 412 413

		pc.next_song = NULL;
		player->queued = false;
		player_command_finished();
		break;
414 415 416
	}
}

417 418 419 420 421
/**
 * Plays a #music_chunk object (after applying software volume).  If
 * it contains a (stream) tag, copy it to the current song, so MPD's
 * playlist reflects the new stream tag.
 */
422
static bool
Max Kellermann's avatar
Max Kellermann committed
423 424
play_chunk(struct song *song, struct music_chunk *chunk,
	   const struct audio_format *format, double sizeToTime)
425
{
Max Kellermann's avatar
Max Kellermann committed
426 427
	bool success;

428 429
	assert(music_chunk_check_format(chunk, format));

430 431 432
	if (chunk->tag != NULL) {
		if (!song_is_file(song)) {
			/* always update the tag of remote streams */
433
			struct tag *old_tag = song->tag;
434 435 436

			song->tag = tag_dup(chunk->tag);

437 438 439
			if (old_tag != NULL)
				tag_free(old_tag);

440 441
			/* the main thread will update the playlist
			   version when he receives this event */
442
			event_pipe_emit(PIPE_EVENT_TAG);
443

444 445 446 447 448 449
			/* notify all clients that the tag of the
			   current song has changed */
			idle_add(IDLE_PLAYER);
		}
	}

450 451 452 453 454 455
	if (chunk->length == 0)
		return true;

	pc.elapsed_time = chunk->times;
	pc.bit_rate = chunk->bit_rate;

456 457
	/* apply software volume */

Max Kellermann's avatar
Max Kellermann committed
458 459 460 461 462 463 464 465 466
	success = pcm_volume(chunk->data, chunk->length,
			     format, pc.software_volume);
	if (!success) {
		g_warning("pcm_volume() failed on %u:%u:%u",
			  format->sample_rate, format->bits, format->channels);
		pc.errored_song = dc.current_song;
		pc.error = PLAYER_ERROR_AUDIO;
		return false;
	}
467

468 469
	/* send the chunk to the audio outputs */

470
	if (!audio_output_all_play(chunk)) {
471 472
		pc.errored_song = dc.current_song;
		pc.error = PLAYER_ERROR_AUDIO;
473
		return false;
474
	}
475

Max Kellermann's avatar
Max Kellermann committed
476
	pc.total_play_time += sizeToTime * chunk->length;
477
	return true;
478 479
}

480 481 482 483 484 485 486 487 488 489 490 491 492
/**
 * Obtains the next chunk from the music pipe, optionally applies
 * cross-fading, and sends it to all audio outputs.
 *
 * @return true on success, false on error (playback will be stopped)
 */
static bool
play_next_chunk(struct player *player)
{
	struct music_chunk *chunk = NULL;
	unsigned cross_fade_position;
	bool success;

493
	if (!audio_output_all_wait(64))
494 495
		/* the output pipe is still large enough, don't send
		   another chunk */
496 497
		return true;

498 499 500 501 502 503 504 505 506
	if (player->xfade == XFADE_ENABLED &&
	    dc.pipe != NULL && dc.pipe != player->pipe &&
	    (cross_fade_position = music_pipe_size(player->pipe))
	    <= player->cross_fade_chunks) {
		/* perform cross fade */
		struct music_chunk *other_chunk =
			music_pipe_shift(dc.pipe);

		if (!player->cross_fading) {
507 508 509 510
			/* beginning of the cross fade - adjust
			   crossFadeChunks which might be bigger than
			   the remaining number of chunks in the old
			   song */
511 512 513 514 515 516 517 518 519 520 521 522 523 524
			player->cross_fade_chunks = cross_fade_position;
			player->cross_fading = true;
		}

		if (other_chunk != NULL) {
			chunk = music_pipe_shift(player->pipe);
			assert(chunk != NULL);

			cross_fade_apply(chunk, other_chunk,
					 &dc.out_audio_format,
					 cross_fade_position,
					 player->cross_fade_chunks);
			music_buffer_return(player_buffer, other_chunk);
		} else {
525
			/* there are not enough decoded chunks yet */
526
			if (decoder_is_idle()) {
527
				/* the decoder isn't running, abort
528 529 530
				   cross fading */
				player->xfade = XFADE_DISABLED;
			} else {
531
				/* wait for the decoder */
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
				notify_signal(&dc.notify);
				notify_wait(&pc.notify);

				return true;
			}
		}
	}

	if (chunk == NULL)
		chunk = music_pipe_shift(player->pipe);

	assert(chunk != NULL);

	/* play the current chunk */

	success = play_chunk(player->song, chunk, &player->play_audio_format,
			     player->size_to_time);

550 551
	if (!success) {
		music_buffer_return(player_buffer, chunk);
552 553 554 555 556 557

		/* pause: the user may resume playback as soon as an
		   audio output becomes available */
		pc.state = PLAYER_STATE_PAUSE;
		player->paused = true;

558
		return false;
559
	}
560

561 562
	/* this formula should prevent that the decoder gets woken up
	   with each chunk; it is more efficient to make it decode a
563 564 565 566 567 568 569 570 571
	   larger block at a time */
	if (!decoder_is_idle() &&
	    music_pipe_size(dc.pipe) <= (pc.buffered_before_play +
					 music_buffer_size(player_buffer) * 3) / 4)
		notify_signal(&dc.notify);

	return true;
}

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
/**
 * This is called at the border between two songs: the audio output
 * has consumed all chunks of the current song, and we should start
 * sending chunks from the next one.
 *
 * @return true on success, false on error (playback will be stopped)
 */
static bool
player_song_border(struct player *player)
{
	player->xfade = XFADE_UNKNOWN;

	music_pipe_free(player->pipe);
	player->pipe = dc.pipe;

587 588 589 590
	if (!player_wait_for_decoder(player))
		return false;

	return true;
591 592
}

593 594 595 596 597
/*
 * The main loop of the player thread, during playback.  This is
 * basically a state machine, which multiplexes data between the
 * decoder thread and the output threads.
 */
598
static void do_play(void)
599
{
600
	struct player player = {
601
		.buffering = true,
602 603
		.decoder_starting = false,
		.paused = false,
604
		.queued = false,
605
		.song = NULL,
606
		.xfade = XFADE_UNKNOWN,
607 608
		.cross_fading = false,
		.cross_fade_chunks = 0,
609
		.size_to_time = 0.0,
610
	};
611

612
	player.pipe = music_pipe_new();
613

614
	dc.buffer = player_buffer;
615
	dc.pipe = player.pipe;
616
	dc_start(&pc.notify, pc.next_song);
617
	if (!player_wait_for_decoder(&player)) {
618
		player_dc_stop(&player);
619
		player_command_finished();
620
		music_pipe_free(player.pipe);
621
		event_pipe_emit(PIPE_EVENT_PLAYLIST);
622
		return;
623
	}
624

Max Kellermann's avatar
Max Kellermann committed
625
	pc.elapsed_time = 0;
626 627 628
	pc.state = PLAYER_STATE_PLAY;
	player_command_finished();

629
	while (true) {
Max Kellermann's avatar
Max Kellermann committed
630
		player_process_command(&player);
631
		if (pc.command == PLAYER_COMMAND_STOP ||
Max Kellermann's avatar
Max Kellermann committed
632
		    pc.command == PLAYER_COMMAND_EXIT ||
633
		    pc.command == PLAYER_COMMAND_CLOSE_AUDIO) {
634
			audio_output_all_cancel();
635 636 637
			break;
		}

638
		if (player.buffering) {
639 640 641 642
			/* buffering at the start of the song - wait
			   until the buffer is large enough, to
			   prevent stuttering on slow machines */

643
			if (music_pipe_size(player.pipe) < pc.buffered_before_play &&
644
			    !decoder_is_idle()) {
645
				/* not enough decoded buffer space yet */
646 647 648 649 650 651 652

				if (!player.paused &&
				    audio_format_defined(&player.play_audio_format) &&
				    audio_output_all_check() < 4 &&
				    !player_send_silence(&player))
					break;

653 654 655 656
				notify_wait(&pc.notify);
				continue;
			} else {
				/* buffering is complete */
657
				player.buffering = false;
658 659 660
			}
		}

661
		if (player.decoder_starting) {
662
			/* wait until the decoder is initialized completely */
663
			bool success;
664

665 666 667 668
			success = player_check_decoder_startup(&player);
			if (!success)
				break;
			continue;
669 670
		}

671
#ifndef NDEBUG
672
		/*
673 674 675
		music_pipe_check_format(&play_audio_format,
					player.next_song_chunk,
					&dc.out_audio_format);
676
		*/
677 678
#endif

679
		if (decoder_is_idle() && player.queued) {
680 681
			/* the decoder has finished the current song;
			   make it decode the next song */
682
			assert(pc.next_song != NULL);
683
			assert(dc.pipe == NULL || dc.pipe == player.pipe);
684 685

			player.queued = false;
686
			dc.pipe = music_pipe_new();
Max Kellermann's avatar
Max Kellermann committed
687
			dc_start_async(pc.next_song);
688
		}
689 690

		if (dc.pipe != NULL && dc.pipe != player.pipe &&
691
		    player.xfade == XFADE_UNKNOWN &&
692
		    !decoder_is_starting()) {
693 694 695
			/* enable cross fading in this song?  if yes,
			   calculate how many chunks will be required
			   for it */
696
			player.cross_fade_chunks =
Max Kellermann's avatar
Max Kellermann committed
697
				cross_fade_calc(pc.cross_fade_seconds, dc.total_time,
698
						&dc.out_audio_format,
699
						&player.play_audio_format,
700
						music_buffer_size(player_buffer) -
701
						pc.buffered_before_play);
702
			if (player.cross_fade_chunks > 0) {
703
				player.xfade = XFADE_ENABLED;
704
				player.cross_fading = false;
705 706 707
			} else
				/* cross fading is disabled or the
				   next song is too short */
708
				player.xfade = XFADE_DISABLED;
709 710
		}

711
		if (player.paused)
712
			notify_wait(&pc.notify);
713
		else if (music_pipe_size(player.pipe) > 0) {
714 715
			/* at least one music chunk is ready - send it
			   to the audio output */
716

717
			play_next_chunk(&player);
718 719 720 721 722 723 724
		} else if (audio_output_all_check() > 0) {
			/* not enough data from decoder, but the
			   output thread is still busy, so it's
			   okay */

			/* XXX synchronize in a better way */
			g_usleep(10000);
725
		} else if (dc.pipe != NULL && dc.pipe != player.pipe) {
726 727
			/* at the beginning of a new song */

728
			if (!player_song_border(&player))
729
				break;
730
		} else if (decoder_is_idle()) {
731 732
			break;
		} else {
733 734 735
			/* the decoder is too busy and hasn't provided
			   new PCM data in time: send silence (if the
			   output pipe is empty) */
736
			if (!player_send_silence(&player))
737 738 739 740
				break;
		}
	}

741 742 743 744 745
	if (player.queued) {
		assert(pc.next_song != NULL);
		pc.next_song = NULL;
	}

746
	player_dc_stop(&player);
747

748
	music_pipe_clear(player.pipe, player_buffer);
749
	music_pipe_free(player.pipe);
750 751 752

	pc.state = PLAYER_STATE_STOP;
	event_pipe_emit(PIPE_EVENT_PLAYLIST);
753 754
}

755
static gpointer player_task(G_GNUC_UNUSED gpointer arg)
756
{
757 758
	decoder_thread_start();

759 760
	player_buffer = music_buffer_new(pc.buffer_chunks);

761 762 763
	while (1) {
		switch (pc.command) {
		case PLAYER_COMMAND_PLAY:
764
		case PLAYER_COMMAND_QUEUE:
765 766
			assert(pc.next_song != NULL);

767
			do_play();
768 769 770
			break;

		case PLAYER_COMMAND_STOP:
771 772 773
			audio_output_all_cancel();
			/* fall through */

774 775
		case PLAYER_COMMAND_SEEK:
		case PLAYER_COMMAND_PAUSE:
776
			pc.next_song = NULL;
777 778 779 780
			player_command_finished();
			break;

		case PLAYER_COMMAND_CLOSE_AUDIO:
781
			audio_output_all_close();
782 783 784
			player_command_finished();
			break;

Max Kellermann's avatar
Max Kellermann committed
785
		case PLAYER_COMMAND_EXIT:
786
			dc_quit();
787
			audio_output_all_close();
788
			music_buffer_free(player_buffer);
Max Kellermann's avatar
Max Kellermann committed
789
			player_command_finished();
790
			g_thread_exit(NULL);
Max Kellermann's avatar
Max Kellermann committed
791 792
			break;

793 794
		case PLAYER_COMMAND_CANCEL:
			pc.next_song = NULL;
795 796 797 798 799 800 801 802 803 804 805 806 807
			player_command_finished();
			break;

		case PLAYER_COMMAND_NONE:
			notify_wait(&pc.notify);
			break;
		}
	}
	return NULL;
}

void player_create(void)
{
808
	GError *e = NULL;
809

810 811 812 813
	assert(pc.thread == NULL);

	pc.thread = g_thread_create(player_task, NULL, true, &e);
	if (pc.thread == NULL)
814
		g_error("Failed to spawn player task: %s", e->message);
815
}