playlist.c 10.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
/*
 * Copyright (C) 2003-2011 The Music Player Daemon Project
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "playlist_internal.h"
#include "playlist_save.h"
#include "player_control.h"
#include "command.h"
#include "tag.h"
#include "song.h"
#include "conf.h"
#include "stored_playlist.h"
#include "idle.h"

#include <glib.h>

#include <assert.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "playlist"

void
playlist_increment_version_all(struct playlist *playlist)
{
	queue_modify_all(&playlist->queue);
	idle_add(IDLE_PLAYLIST);
}

void
playlist_tag_changed(struct playlist *playlist)
{
	if (!playlist->playing)
		return;

	assert(playlist->current >= 0);

	queue_modify(&playlist->queue, playlist->current);
	idle_add(IDLE_PLAYLIST);
}

void
playlist_init(struct playlist *playlist)
{
	queue_init(&playlist->queue,
		   config_get_positive(CONF_MAX_PLAYLIST_LENGTH,
				       DEFAULT_PLAYLIST_MAX_LENGTH));

	playlist->queued = -1;
	playlist->current = -1;
}

void
playlist_finish(struct playlist *playlist)
{
	queue_finish(&playlist->queue);
}

/**
 * Queue a song, addressed by its order number.
 */
static void
playlist_queue_song_order(struct playlist *playlist, struct player_control *pc,
			  unsigned order)
{
	struct song *song;
	char *uri;

	assert(queue_valid_order(&playlist->queue, order));

	playlist->queued = order;

	song = queue_get_order(&playlist->queue, order);
	uri = song_get_uri(song);
	g_debug("queue song %i:\"%s\"", playlist->queued, uri);
	g_free(uri);

	pc_enqueue_song(pc, song);
}

/**
 * Called if the player thread has started playing the "queued" song.
 */
static void
playlist_song_started(struct playlist *playlist, struct player_control *pc)
{
	assert(pc->next_song == NULL);
	assert(playlist->queued >= -1);

	/* queued song has started: copy queued to current,
	   and notify the clients */

	int current = playlist->current;
	playlist->current = playlist->queued;
	playlist->queued = -1;

	/* Pause if we are in single mode. */
	if(playlist->queue.single && !playlist->queue.repeat) {
		pc_set_pause(pc, true);
	}

	if(playlist->queue.consume)
		playlist_delete(playlist, pc,
				queue_order_to_position(&playlist->queue,
							current));

	idle_add(IDLE_PLAYER);
}

const struct song *
playlist_get_queued_song(struct playlist *playlist)
{
	if (!playlist->playing || playlist->queued < 0)
		return NULL;

	return queue_get_order(&playlist->queue, playlist->queued);
}

void
playlist_update_queued_song(struct playlist *playlist,
			    struct player_control *pc,
			    const struct song *prev)
{
	int next_order;
	const struct song *next_song;

	if (!playlist->playing)
		return;

	assert(!queue_is_empty(&playlist->queue));
	assert((playlist->queued < 0) == (prev == NULL));

	next_order = playlist->current >= 0
		? queue_next_order(&playlist->queue, playlist->current)
		: 0;

	if (next_order == 0 && playlist->queue.random &&
	    !playlist->queue.single) {
		/* shuffle the song order again, so we get a different
		   order each time the playlist is played
		   completely */
		unsigned current_position =
			queue_order_to_position(&playlist->queue,
						playlist->current);

		queue_shuffle_order(&playlist->queue);

		/* make sure that the playlist->current still points to
		   the current song, after the song order has been
		   shuffled */
		playlist->current =
			queue_position_to_order(&playlist->queue,
						current_position);
	}

	if (next_order >= 0)
		next_song = queue_get_order(&playlist->queue, next_order);
	else
		next_song = NULL;

	if (prev != NULL && next_song != prev) {
		/* clear the currently queued song */
		pc_cancel(pc);
		playlist->queued = -1;
	}

	if (next_order >= 0) {
		if (next_song != prev)
			playlist_queue_song_order(playlist, pc, next_order);
		else
			playlist->queued = next_order;
	}
}

void
playlist_play_order(struct playlist *playlist, struct player_control *pc,
		    int orderNum)
{
	struct song *song;
	char *uri;

	playlist->playing = true;
	playlist->queued = -1;

	song = queue_get_order(&playlist->queue, orderNum);

	uri = song_get_uri(song);
	g_debug("play %i:\"%s\"", orderNum, uri);
	g_free(uri);

	pc_play(pc, song);
	playlist->current = orderNum;
}

static void
playlist_resume_playback(struct playlist *playlist, struct player_control *pc);

/**
 * This is the "PLAYLIST" event handler.  It is invoked by the player
 * thread whenever it requests a new queued song, or when it exits.
 */
void
playlist_sync(struct playlist *playlist, struct player_control *pc)
{
	if (!playlist->playing)
		/* this event has reached us out of sync: we aren't
		   playing anymore; ignore the event */
		return;

	player_lock(pc);
	enum player_state pc_state = pc_get_state(pc);
	const struct song *pc_next_song = pc->next_song;
	player_unlock(pc);

	if (pc_state == PLAYER_STATE_STOP)
		/* the player thread has stopped: check if playback
		   should be restarted with the next song.  That can
		   happen if the playlist isn't filling the queue fast
		   enough */
		playlist_resume_playback(playlist, pc);
	else {
		/* check if the player thread has already started
		   playing the queued song */
		if (pc_next_song == NULL && playlist->queued != -1)
			playlist_song_started(playlist, pc);

		/* make sure the queued song is always set (if
		   possible) */
		if (pc->next_song == NULL && playlist->queued < 0)
			playlist_update_queued_song(playlist, pc, NULL);
	}
}

/**
 * The player has stopped for some reason.  Check the error, and
 * decide whether to re-start playback
 */
static void
playlist_resume_playback(struct playlist *playlist, struct player_control *pc)
{
	enum player_error error;

	assert(playlist->playing);
	assert(pc_get_state(pc) == PLAYER_STATE_STOP);

	error = pc_get_error(pc);
	if (error == PLAYER_ERROR_NOERROR)
		playlist->error_count = 0;
	else
		++playlist->error_count;

	if ((playlist->stop_on_error && error != PLAYER_ERROR_NOERROR) ||
	    error == PLAYER_ERROR_AUDIO || error == PLAYER_ERROR_SYSTEM ||
	    playlist->error_count >= queue_length(&playlist->queue))
		/* too many errors, or critical error: stop
		   playback */
		playlist_stop(playlist, pc);
	else
		/* continue playback at the next song */
		playlist_next(playlist, pc);
}

bool
playlist_get_repeat(const struct playlist *playlist)
{
	return playlist->queue.repeat;
}

bool
playlist_get_random(const struct playlist *playlist)
{
	return playlist->queue.random;
}

bool
playlist_get_single(const struct playlist *playlist)
{
	return playlist->queue.single;
}

bool
playlist_get_consume(const struct playlist *playlist)
{
	return playlist->queue.consume;
}

void
playlist_set_repeat(struct playlist *playlist, struct player_control *pc,
		    bool status)
{
	if (status == playlist->queue.repeat)
		return;

	playlist->queue.repeat = status;

	/* if the last song is currently being played, the "next song"
	   might change when repeat mode is toggled */
	playlist_update_queued_song(playlist, pc,
				    playlist_get_queued_song(playlist));

	idle_add(IDLE_OPTIONS);
}

static void
playlist_order(struct playlist *playlist)
{
	if (playlist->current >= 0)
		/* update playlist.current, order==position now */
		playlist->current = queue_order_to_position(&playlist->queue,
							    playlist->current);

	queue_restore_order(&playlist->queue);
}

void
playlist_set_single(struct playlist *playlist, struct player_control *pc,
		    bool status)
{
	if (status == playlist->queue.single)
		return;

	playlist->queue.single = status;

	/* if the last song is currently being played, the "next song"
	   might change when single mode is toggled */
	playlist_update_queued_song(playlist, pc,
				    playlist_get_queued_song(playlist));

	idle_add(IDLE_OPTIONS);
}

void
playlist_set_consume(struct playlist *playlist, bool status)
{
	if (status == playlist->queue.consume)
		return;

	playlist->queue.consume = status;
	idle_add(IDLE_OPTIONS);
}

void
playlist_set_random(struct playlist *playlist, struct player_control *pc,
		    bool status)
{
	const struct song *queued;

	if (status == playlist->queue.random)
		return;

	queued = playlist_get_queued_song(playlist);

	playlist->queue.random = status;

	if (playlist->queue.random) {
		/* shuffle the queue order, but preserve
		   playlist->current */

		int current_position =
			playlist->playing && playlist->current >= 0
			? (int)queue_order_to_position(&playlist->queue,
						       playlist->current)
			: -1;

		queue_shuffle_order(&playlist->queue);

		if (current_position >= 0) {
			/* make sure the current song is the first in
			   the order list, so the whole rest of the
			   playlist is played after that */
			unsigned current_order =
				queue_position_to_order(&playlist->queue,
							current_position);
			queue_swap_order(&playlist->queue, 0, current_order);
			playlist->current = 0;
		} else
			playlist->current = -1;
	} else
		playlist_order(playlist);

	playlist_update_queued_song(playlist, pc, queued);

	idle_add(IDLE_OPTIONS);
}

int
playlist_get_current_song(const struct playlist *playlist)
{
	if (playlist->current >= 0)
		return queue_order_to_position(&playlist->queue,
					       playlist->current);

	return -1;
}

int
playlist_get_next_song(const struct playlist *playlist)
{
	if (playlist->current >= 0)
	{
		if (playlist->queue.single == 1 && playlist->queue.repeat == 1)
			return queue_order_to_position(&playlist->queue,
			                               playlist->current);
		else if (playlist->current + 1 < (int)queue_length(&playlist->queue))
			return queue_order_to_position(&playlist->queue,
					       playlist->current + 1);
		else if (playlist->queue.repeat == 1)
			return queue_order_to_position(&playlist->queue, 0);
	}

	return -1;
}

unsigned long
playlist_get_version(const struct playlist *playlist)
{
	return playlist->queue.version;
}

int
playlist_get_length(const struct playlist *playlist)
{
	return queue_length(&playlist->queue);
}

unsigned
playlist_get_song_id(const struct playlist *playlist, unsigned song)
{
	return queue_position_to_id(&playlist->queue, song);
}