playlist.c 10.1 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2010 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
21
#include "playlist_internal.h"
22
#include "playlist_save.h"
23
#include "player_control.h"
Warren Dukes's avatar
Warren Dukes committed
24 25
#include "command.h"
#include "tag.h"
26
#include "song.h"
Warren Dukes's avatar
Warren Dukes committed
27
#include "conf.h"
28
#include "stored_playlist.h"
29
#include "idle.h"
Warren Dukes's avatar
Warren Dukes committed
30

31 32
#include <glib.h>

Max Kellermann's avatar
Max Kellermann committed
33
#include <assert.h>
34

35 36 37
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "playlist"

38 39
void
playlist_increment_version_all(struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
40
{
41
	queue_modify_all(&playlist->queue);
42
	idle_add(IDLE_PLAYLIST);
Warren Dukes's avatar
Warren Dukes committed
43 44
}

45 46
void
playlist_tag_changed(struct playlist *playlist)
47
{
48
	if (!playlist->playing)
49 50
		return;

51
	assert(playlist->current >= 0);
52

53
	queue_modify(&playlist->queue, playlist->current);
54
	idle_add(IDLE_PLAYLIST);
55 56
}

57 58
void
playlist_init(struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
59
{
60
	queue_init(&playlist->queue,
61 62
		   config_get_positive(CONF_MAX_PLAYLIST_LENGTH,
				       DEFAULT_PLAYLIST_MAX_LENGTH));
63

64 65
	playlist->queued = -1;
	playlist->current = -1;
Warren Dukes's avatar
Warren Dukes committed
66 67
}

68 69
void
playlist_finish(struct playlist *playlist)
70
{
71
	queue_finish(&playlist->queue);
72 73
}

74 75 76
/**
 * Queue a song, addressed by its order number.
 */
77
static void
78
playlist_queue_song_order(struct playlist *playlist, unsigned order)
Avuton Olrich's avatar
Avuton Olrich committed
79
{
80 81
	struct song *song;
	char *uri;
82

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

85
	playlist->queued = order;
86

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

92
	pc_enqueue_song(song);
93
}
94

95
/**
96
 * Called if the player thread has started playing the "queued" song.
97
 */
98
static void
99
playlist_song_started(struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
100
{
101 102
	assert(pc.next_song == NULL);
	assert(playlist->queued >= -1);
103

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

107 108 109
	int current = playlist->current;
	playlist->current = playlist->queued;
	playlist->queued = -1;
110

111
	/* Pause if we are in single mode. */
112 113
	if(playlist->queue.single && !playlist->queue.repeat) {
		pc_set_pause(true);
Warren Dukes's avatar
Warren Dukes committed
114
	}
115 116 117 118 119

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

	idle_add(IDLE_PLAYER);
Warren Dukes's avatar
Warren Dukes committed
120 121
}

122
const struct song *
123
playlist_get_queued_song(struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
124
{
125
	if (!playlist->playing || playlist->queued < 0)
126
		return NULL;
127

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

131
void
132
playlist_update_queued_song(struct playlist *playlist, const struct song *prev)
133 134 135 136
{
	int next_order;
	const struct song *next_song;

137
	if (!playlist->playing)
138 139
		return;

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

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

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

156
		queue_shuffle_order(&playlist->queue);
157

158
		/* make sure that the playlist->current still points to
159 160
		   the current song, after the song order has been
		   shuffled */
161 162
		playlist->current =
			queue_position_to_order(&playlist->queue,
163 164 165 166
						current_position);
	}

	if (next_order >= 0)
167
		next_song = queue_get_order(&playlist->queue, next_order);
168 169 170 171 172 173
	else
		next_song = NULL;

	if (prev != NULL && next_song != prev) {
		/* clear the currently queued song */
		pc_cancel();
174
		playlist->queued = -1;
175
	}
Warren Dukes's avatar
Warren Dukes committed
176

177 178
	if (next_order >= 0) {
		if (next_song != prev)
179
			playlist_queue_song_order(playlist, next_order);
180
		else
181
			playlist->queued = next_order;
182
	}
183 184
}

185
void
186
playlist_play_order(struct playlist *playlist, int orderNum)
Avuton Olrich's avatar
Avuton Olrich committed
187
{
188
	struct song *song;
189
	char *uri;
Eric Wong's avatar
Eric Wong committed
190

191 192
	playlist->playing = true;
	playlist->queued = -1;
Warren Dukes's avatar
Warren Dukes committed
193

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

	uri = song_get_uri(song);
197
	g_debug("play %i:\"%s\"", orderNum, uri);
198
	g_free(uri);
Warren Dukes's avatar
Warren Dukes committed
199

200
	pc_play(song);
201
	playlist->current = orderNum;
Warren Dukes's avatar
Warren Dukes committed
202 203
}

204
static void
205
playlist_resume_playback(struct playlist *playlist);
206

207 208 209 210
/**
 * This is the "PLAYLIST" event handler.  It is invoked by the player
 * thread whenever it requests a new queued song, or when it exits.
 */
211 212
void
playlist_sync(struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
213
{
214
	if (!playlist->playing)
215 216
		/* this event has reached us out of sync: we aren't
		   playing anymore; ignore the event */
Avuton Olrich's avatar
Avuton Olrich committed
217
		return;
Warren Dukes's avatar
Warren Dukes committed
218

219 220 221 222 223 224
	player_lock();
	enum player_state pc_state = pc_get_state();
	const struct song *pc_next_song = pc.next_song;
	player_unlock();

	if (pc_state == PLAYER_STATE_STOP)
225 226 227 228
		/* 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 */
229
		playlist_resume_playback(playlist);
230
	else {
231 232
		/* check if the player thread has already started
		   playing the queued song */
233
		if (pc_next_song == NULL && playlist->queued != -1)
234
			playlist_song_started(playlist);
235 236 237

		/* make sure the queued song is always set (if
		   possible) */
238
		if (pc.next_song == NULL && playlist->queued < 0)
239
			playlist_update_queued_song(playlist, NULL);
240
	}
Warren Dukes's avatar
Warren Dukes committed
241 242
}

243 244 245 246
/**
 * The player has stopped for some reason.  Check the error, and
 * decide whether to re-start playback
 */
247
static void
248
playlist_resume_playback(struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
249
{
250
	enum player_error error;
Warren Dukes's avatar
Warren Dukes committed
251

252
	assert(playlist->playing);
253
	assert(pc_get_state() == PLAYER_STATE_STOP);
Warren Dukes's avatar
Warren Dukes committed
254

255
	error = pc_get_error();
256
	if (error == PLAYER_ERROR_NOERROR)
257
		playlist->error_count = 0;
258
	else
259
		++playlist->error_count;
260

261
	if ((playlist->stop_on_error && error != PLAYER_ERROR_NOERROR) ||
262
	    error == PLAYER_ERROR_AUDIO || error == PLAYER_ERROR_SYSTEM ||
263
	    playlist->error_count >= queue_length(&playlist->queue))
264 265
		/* too many errors, or critical error: stop
		   playback */
266
		playlist_stop(playlist);
267
	else
268
		/* continue playback at the next song */
269
		playlist_next(playlist);
Warren Dukes's avatar
Warren Dukes committed
270 271
}

272
bool
273
playlist_get_repeat(const struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
274
{
275
	return playlist->queue.repeat;
Warren Dukes's avatar
Warren Dukes committed
276 277
}

278
bool
279
playlist_get_random(const struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
280
{
281
	return playlist->queue.random;
Warren Dukes's avatar
Warren Dukes committed
282 283
}

284
bool
285
playlist_get_single(const struct playlist *playlist)
286
{
287
	return playlist->queue.single;
288 289
}

290
bool
291
playlist_get_consume(const struct playlist *playlist)
292 293 294 295
{
	return playlist->queue.consume;
}

296 297
void
playlist_set_repeat(struct playlist *playlist, bool status)
Avuton Olrich's avatar
Avuton Olrich committed
298
{
299
	if (status == playlist->queue.repeat)
300 301
		return;

302
	playlist->queue.repeat = status;
303

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

309
	idle_add(IDLE_OPTIONS);
Warren Dukes's avatar
Warren Dukes committed
310 311
}

312 313
static void
playlist_order(struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
314
{
315
	if (playlist->current >= 0)
316
		/* update playlist.current, order==position now */
317 318
		playlist->current = queue_order_to_position(&playlist->queue,
							    playlist->current);
Warren Dukes's avatar
Warren Dukes committed
319

320
	queue_restore_order(&playlist->queue);
Warren Dukes's avatar
Warren Dukes committed
321 322
}

323 324
void
playlist_set_single(struct playlist *playlist, bool status)
325
{
326 327 328 329
	if (status == playlist->queue.single)
		return;

	playlist->queue.single = status;
330 331

	/* if the last song is currently being played, the "next song"
332
	   might change when single mode is toggled */
333 334 335 336 337 338
	playlist_update_queued_song(playlist,
				    playlist_get_queued_song(playlist));

	idle_add(IDLE_OPTIONS);
}

339 340
void
playlist_set_consume(struct playlist *playlist, bool status)
341 342 343 344 345 346 347 348
{
	if (status == playlist->queue.consume)
		return;

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

349 350
void
playlist_set_random(struct playlist *playlist, bool status)
Avuton Olrich's avatar
Avuton Olrich committed
351
{
352 353
	const struct song *queued;

354
	if (status == playlist->queue.random)
355
		return;
Warren Dukes's avatar
Warren Dukes committed
356

357
	queued = playlist_get_queued_song(playlist);
358

359
	playlist->queue.random = status;
Warren Dukes's avatar
Warren Dukes committed
360

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

365 366
		int current_position =
			playlist->playing && playlist->current >= 0
367 368
			? (int)queue_order_to_position(&playlist->queue,
						       playlist->current)
369 370
			: -1;

371
		queue_shuffle_order(&playlist->queue);
372 373

		if (current_position >= 0) {
374 375 376
			/* make sure the current song is the first in
			   the order list, so the whole rest of the
			   playlist is played after that */
377
			unsigned current_order =
378
				queue_position_to_order(&playlist->queue,
379
							current_position);
380 381
			queue_swap_order(&playlist->queue, 0, current_order);
			playlist->current = 0;
382 383
		} else
			playlist->current = -1;
384
	} else
385
		playlist_order(playlist);
386

387
	playlist_update_queued_song(playlist, queued);
388

389
	idle_add(IDLE_OPTIONS);
Warren Dukes's avatar
Warren Dukes committed
390 391
}

392 393
int
playlist_get_current_song(const struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
394
{
395 396 397
	if (playlist->current >= 0)
		return queue_order_to_position(&playlist->queue,
					       playlist->current);
398

Avuton Olrich's avatar
Avuton Olrich committed
399
	return -1;
400 401
}

402 403
int
playlist_get_next_song(const struct playlist *playlist)
404 405 406
{
	if (playlist->current >= 0)
	{
407 408 409 410
		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))
411 412 413
			return queue_order_to_position(&playlist->queue,
					       playlist->current + 1);
		else if (playlist->queue.repeat == 1)
414
			return queue_order_to_position(&playlist->queue, 0);
415 416 417 418 419
	}

	return -1;
}

420
unsigned long
421
playlist_get_version(const struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
422
{
423
	return playlist->queue.version;
424 425
}

426
int
427
playlist_get_length(const struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
428
{
429
	return queue_length(&playlist->queue);
430 431
}

432
unsigned
433
playlist_get_song_id(const struct playlist *playlist, unsigned song)
Avuton Olrich's avatar
Avuton Olrich committed
434
{
435
	return queue_position_to_id(&playlist->queue, song);
436
}