player_thread.c 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* the Music Player Daemon (MPD)
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
 * This project's homepage is: http://www.musicpd.org
 *
 * 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.
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "player_thread.h"
20
#include "player_control.h"
21
#include "decoder_control.h"
22 23 24 25 26 27
#include "audio.h"
#include "pcm_utils.h"
#include "path.h"
#include "log.h"
#include "main_notify.h"
#include "crossfade.h"
28
#include "song.h"
29 30 31 32 33 34 35

enum xfade_state {
	XFADE_DISABLED = -1,
	XFADE_UNKNOWN = 0,
	XFADE_ENABLED = 1
};

36 37
struct player {
	/**
38
	 * are we waiting for buffered_before_play?
39
	 */
40
	bool buffering;
41 42 43 44 45 46 47 48 49 50 51 52

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

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

53 54 55 56 57
	/**
	 * is there a new song in pc.next_song?
	 */
	bool queued;

58 59 60 61 62 63 64 65 66 67 68 69
	/**
	 * is cross fading enabled?
	 */
	enum xfade_state xfade;

	/**
	 * index of the first chunk of the next song, -1 if there is
	 * no next song
	 */
	int next_song_chunk;
};

70 71 72 73 74 75 76 77
static void player_command_finished(void)
{
	assert(pc.command != PLAYER_COMMAND_NONE);

	pc.command = PLAYER_COMMAND_NONE;
	wakeup_main_task();
}

78 79
static void quitDecode(void)
{
Max Kellermann's avatar
Max Kellermann committed
80
	dc_stop(&pc.notify);
81 82 83 84
	pc.state = PLAYER_STATE_STOP;
	wakeup_main_task();
}

85
static int waitOnDecode(struct player *player)
86
{
Max Kellermann's avatar
Max Kellermann committed
87
	dc_command_wait(&pc.notify);
88 89

	if (dc.error != DECODE_ERROR_NOERROR) {
90
		assert(dc.next_song == NULL || dc.next_song->url != NULL);
91 92 93 94 95
		pc.errored_song = dc.next_song;
		pc.error = PLAYER_ERROR_FILE;
		return -1;
	}

96 97
	pc.totalTime = pc.next_song->tag != NULL
		? pc.next_song->tag->time : 0;
98
	pc.bitRate = 0;
99
	audio_format_clear(&pc.audio_format);
100 101 102

	pc.next_song = NULL;
	player->queued = false;
103
	player->decoder_starting = true;
104 105 106 107

	return 0;
}

108
static bool decodeSeek(struct player *player)
109
{
Max Kellermann's avatar
Max Kellermann committed
110
	double where;
111
	bool ret;
112

113
	if (decoder_current_song() != pc.next_song) {
Max Kellermann's avatar
Max Kellermann committed
114
		dc_stop(&pc.notify);
115
		player->next_song_chunk = -1;
116
		ob_clear();
Max Kellermann's avatar
Max Kellermann committed
117
		dc_start_async(pc.next_song);
118
		waitOnDecode(player);
119
	}
Max Kellermann's avatar
Max Kellermann committed
120 121 122 123 124 125 126 127

	where = pc.seekWhere;
	if (where > pc.totalTime)
		where = pc.totalTime - 0.1;
	if (where < 0.0)
		where = 0.0;

	ret = dc_seek(&pc.notify, where);
128
	if (ret)
Max Kellermann's avatar
Max Kellermann committed
129
		pc.elapsedTime = where;
130 131 132 133 134 135

	player_command_finished();

	return ret;
}

136
static void processDecodeInput(struct player *player)
137 138 139 140 141
{
	switch (pc.command) {
	case PLAYER_COMMAND_NONE:
	case PLAYER_COMMAND_PLAY:
	case PLAYER_COMMAND_STOP:
Max Kellermann's avatar
Max Kellermann committed
142
	case PLAYER_COMMAND_EXIT:
143 144 145
	case PLAYER_COMMAND_CLOSE_AUDIO:
		break;

146 147 148
	case PLAYER_COMMAND_QUEUE:
		assert(pc.next_song != NULL);
		player->queued = true;
149 150 151 152
		player_command_finished();
		break;

	case PLAYER_COMMAND_PAUSE:
153 154 155
		player->paused = !player->paused;
		if (player->paused) {
			audio_output_pause_all();
156 157
			pc.state = PLAYER_STATE_PAUSE;
		} else {
158
			if (openAudioDevice(NULL)) {
159 160 161
				pc.state = PLAYER_STATE_PLAY;
			} else {
				char tmp[MPD_PATH_MAX];
162
				assert(dc.next_song == NULL || dc.next_song->url != NULL);
163 164 165 166
				pc.errored_song = dc.next_song;
				pc.error = PLAYER_ERROR_AUDIO;
				ERROR("problems opening audio device "
				      "while playing \"%s\"\n",
Max Kellermann's avatar
Max Kellermann committed
167
				      song_get_url(dc.next_song, tmp));
168
				player->paused = true;
169 170 171 172 173 174 175
			}
		}
		player_command_finished();
		break;

	case PLAYER_COMMAND_SEEK:
		dropBufferedAudio();
176
		if (decodeSeek(player)) {
177
			player->xfade = XFADE_UNKNOWN;
178 179 180 181

			/* abort buffering when the user has requested
			   a seek */
			player->buffering = false;
182 183
		}
		break;
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

	case PLAYER_COMMAND_CANCEL:
		if (pc.next_song == NULL) {
			/* the cancel request arrived too later, we're
			   already playing the queued song...  stop
			   everything now */
			pc.command = PLAYER_COMMAND_STOP;
			return;
		}

		if (player->next_song_chunk != -1) {
			/* the decoder is already decoding the song -
			   stop it and reset the position */
			dc_stop(&pc.notify);
			player->next_song_chunk = -1;
		}

		pc.next_song = NULL;
		player->queued = false;
		player_command_finished();
		break;
205 206 207 208
	}
}

static int playChunk(ob_chunk * chunk,
209
		     const struct audio_format *format, double sizeToTime)
210 211 212 213
{
	pc.elapsedTime = chunk->times;
	pc.bitRate = chunk->bitRate;

Max Kellermann's avatar
Max Kellermann committed
214 215
	pcm_volume(chunk->data, chunk->chunkSize,
		   format, pc.softwareVolume);
216

217
	if (!playAudio(chunk->data, chunk->chunkSize))
218 219 220 221 222 223
		return -1;

	pc.totalPlayTime += sizeToTime * chunk->chunkSize;
	return 0;
}

224
static void do_play(void)
225
{
226
	struct player player = {
227
		.buffering = true,
228 229
		.decoder_starting = false,
		.paused = false,
230
		.queued = false,
231 232 233
		.xfade = XFADE_UNKNOWN,
		.next_song_chunk = -1,
	};
234 235 236 237 238
	unsigned int crossFadeChunks = 0;
	/** the position of the next cross-faded chunk in the next
	    song */
	int nextChunk = 0;
	static const char silence[CHUNK_SIZE];
239
	struct audio_format play_audio_format;
240 241
	double sizeToTime = 0.0;

242
	ob_clear();
243
	ob_set_lazy(false);
244

245
	dc_start(&pc.notify, pc.next_song);
246
	if (waitOnDecode(&player) < 0) {
247
		quitDecode();
248
		player_command_finished();
249
		return;
250
	}
251 252 253 254 255 256

	pc.elapsedTime = 0;
	pc.state = PLAYER_STATE_PLAY;
	player_command_finished();

	while (1) {
257
		processDecodeInput(&player);
258
		if (pc.command == PLAYER_COMMAND_STOP ||
Max Kellermann's avatar
Max Kellermann committed
259
		    pc.command == PLAYER_COMMAND_EXIT ||
260
		    pc.command == PLAYER_COMMAND_CLOSE_AUDIO) {
261 262 263 264
			dropBufferedAudio();
			break;
		}

265
		if (player.buffering) {
266 267
			if (ob_available() < pc.buffered_before_play &&
			    !decoder_is_idle()) {
268 269 270 271 272
				/* not enough decoded buffer space yet */
				notify_wait(&pc.notify);
				continue;
			} else {
				/* buffering is complete */
273
				player.buffering = false;
274
				ob_set_lazy(true);
275 276 277
			}
		}

278
		if (player.decoder_starting) {
279 280 281 282 283 284 285
			if (dc.error != DECODE_ERROR_NOERROR) {
				/* the decoder failed */
				assert(dc.next_song == NULL || dc.next_song->url != NULL);
				pc.errored_song = dc.next_song;
				pc.error = PLAYER_ERROR_FILE;
				break;
			}
286
			else if (!decoder_is_starting()) {
287
				/* the decoder is ready and ok */
288
				player.decoder_starting = false;
289
				if (!openAudioDevice(&ob.audioFormat)) {
290
					char tmp[MPD_PATH_MAX];
291
					assert(dc.next_song == NULL || dc.next_song->url != NULL);
292 293 294 295
					pc.errored_song = dc.next_song;
					pc.error = PLAYER_ERROR_AUDIO;
					ERROR("problems opening audio device "
					      "while playing \"%s\"\n",
Max Kellermann's avatar
Max Kellermann committed
296
					      song_get_url(dc.next_song, tmp));
297 298 299
					break;
				}

300
				if (player.paused)
301
					closeAudioDevice();
302

303
				pc.totalTime = dc.totalTime;
304
				pc.audio_format = dc.audioFormat;
305
				play_audio_format = ob.audioFormat;
306 307 308 309 310 311 312 313 314 315
				sizeToTime = audioFormatSizeToTime(&ob.audioFormat);
			}
			else {
				/* the decoder is not yet ready; wait
				   some more */
				notify_wait(&pc.notify);
				continue;
			}
		}

316
		if (decoder_is_idle() && !player.queued &&
317 318
		    pc.next_song != NULL &&
		    pc.command == PLAYER_COMMAND_NONE) {
319 320 321 322 323 324 325
			/* the decoder has finished the current song;
			   request the next song from the playlist */
			pc.next_song = NULL;
			wakeup_main_task();
		}

		if (decoder_is_idle() && player.queued) {
326 327
			/* the decoder has finished the current song;
			   make it decode the next song */
328 329 330
			assert(pc.next_song != NULL);

			player.queued = false;
331
			player.next_song_chunk = ob.end;
Max Kellermann's avatar
Max Kellermann committed
332
			dc_start_async(pc.next_song);
333
		}
334 335
		if (player.next_song_chunk >= 0 &&
		    player.xfade == XFADE_UNKNOWN &&
336
		    !decoder_is_starting()) {
337 338 339 340 341 342 343
			/* enable cross fading in this song?  if yes,
			   calculate how many chunks will be required
			   for it */
			crossFadeChunks =
				cross_fade_calc(pc.crossFade, dc.totalTime,
						&(ob.audioFormat),
						ob.size -
344
						pc.buffered_before_play);
345
			if (crossFadeChunks > 0) {
346
				player.xfade = XFADE_ENABLED;
347 348 349 350
				nextChunk = -1;
			} else
				/* cross fading is disabled or the
				   next song is too short */
351
				player.xfade = XFADE_DISABLED;
352 353
		}

354
		if (player.paused)
355
			notify_wait(&pc.notify);
356 357
		else if (!ob_is_empty() &&
			 (int)ob.begin != player.next_song_chunk) {
358 359
			ob_chunk *beginChunk = ob_get_chunk(ob.begin);
			unsigned int fadePosition;
360 361 362
			if (player.xfade == XFADE_ENABLED &&
			    player.next_song_chunk >= 0 &&
			    (fadePosition = ob_relative(player.next_song_chunk))
363 364 365 366 367 368 369 370 371 372 373 374
			    <= crossFadeChunks) {
				/* perform cross fade */
				if (nextChunk < 0) {
					/* beginning of the cross fade
					   - adjust crossFadeChunks
					   which might be bigger than
					   the remaining number of
					   chunks in the old song */
					crossFadeChunks = fadePosition;
				}
				nextChunk = ob_absolute(crossFadeChunks);
				if (nextChunk >= 0) {
375
					ob_set_lazy(true);
376 377 378 379 380 381 382 383
					cross_fade_apply(beginChunk,
							 ob_get_chunk(nextChunk),
							 &(ob.audioFormat),
							 fadePosition,
							 crossFadeChunks);
				} else {
					/* there are not enough
					   decoded chunks yet */
384
					if (decoder_is_idle()) {
385 386 387
						/* the decoder isn't
						   running, abort
						   cross fading */
388
						player.xfade = XFADE_DISABLED;
389 390 391
					} else {
						/* wait for the
						   decoder */
392
						ob_set_lazy(false);
393 394 395 396 397 398 399
						notify_wait(&pc.notify);
						continue;
					}
				}
			}

			/* play the current chunk */
400
			if (playChunk(beginChunk, &play_audio_format,
401 402 403
				      sizeToTime) < 0)
				break;
			ob_shift();
404 405 406 407 408 409 410

			/* this formula should prevent that the
			   decoder gets woken up with each chunk; it
			   is more efficient to make it decode a
			   larger block at a time */
			if (ob_available() <= (pc.buffered_before_play + ob.size * 3) / 4)
				notify_signal(&dc.notify);
411 412
		} else if (!ob_is_empty() &&
			   (int)ob.begin == player.next_song_chunk) {
413 414
			/* at the beginning of a new song */

415
			if (player.xfade == XFADE_ENABLED && nextChunk >= 0) {
416 417 418 419 420 421
				/* the cross-fade is finished; skip
				   the section which was cross-faded
				   (and thus already played) */
				ob_skip(crossFadeChunks);
			}

422
			player.xfade = XFADE_UNKNOWN;
423

424 425
			player.next_song_chunk = -1;
			if (waitOnDecode(&player) < 0)
426 427 428
				return;

			wakeup_main_task();
429
		} else if (decoder_is_idle()) {
430 431
			break;
		} else {
432
			size_t frame_size =
433
				audio_format_frame_size(&play_audio_format);
434 435 436 437
			/* this formula ensures that we don't send
			   partial frames */
			unsigned num_frames = CHUNK_SIZE / frame_size;

438
			/*DEBUG("waiting for decoded audio, play silence\n");*/
439
			if (!playAudio(silence, num_frames * frame_size))
440 441 442 443 444 445 446 447 448 449 450 451
				break;
		}
	}

	quitDecode();
}

static void * player_task(mpd_unused void *arg)
{
	while (1) {
		switch (pc.command) {
		case PLAYER_COMMAND_PLAY:
452
		case PLAYER_COMMAND_QUEUE:
453
			do_play();
454 455 456 457 458 459 460 461 462 463 464 465 466
			break;

		case PLAYER_COMMAND_STOP:
		case PLAYER_COMMAND_SEEK:
		case PLAYER_COMMAND_PAUSE:
			player_command_finished();
			break;

		case PLAYER_COMMAND_CLOSE_AUDIO:
			closeAudioDevice();
			player_command_finished();
			break;

Max Kellermann's avatar
Max Kellermann committed
467 468 469 470 471 472
		case PLAYER_COMMAND_EXIT:
			closeAudioDevice();
			player_command_finished();
			pthread_exit(NULL);
			break;

473 474
		case PLAYER_COMMAND_CANCEL:
			pc.next_song = NULL;
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
			player_command_finished();
			break;

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

void player_create(void)
{
	pthread_attr_t attr;
	pthread_t player_thread;

	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	if (pthread_create(&player_thread, &attr, player_task, NULL))
		FATAL("Failed to spawn player task: %s\n", strerror(errno));
}