Playlist.cxx 7.54 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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.hxx"
22
#include "PlayerControl.hxx"
23
#include "song.h"
Max Kellermann's avatar
Max Kellermann committed
24
#include "Idle.hxx"
Warren Dukes's avatar
Warren Dukes committed
25

26 27
#include <glib.h>

Max Kellermann's avatar
Max Kellermann committed
28
#include <assert.h>
29

30 31 32
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "playlist"

33
void
34
playlist::FullIncrementVersions()
Avuton Olrich's avatar
Avuton Olrich committed
35
{
36
	queue.ModifyAll();
37
	idle_add(IDLE_PLAYLIST);
Warren Dukes's avatar
Warren Dukes committed
38 39
}

40
void
41
playlist::TagChanged()
42
{
43
	if (!playing)
44 45
		return;

46
	assert(current >= 0);
47

48
	queue.ModifyAtOrder(current);
49
	idle_add(IDLE_PLAYLIST);
50 51
}

52 53 54
/**
 * Queue a song, addressed by its order number.
 */
55
static void
56 57
playlist_queue_song_order(struct playlist *playlist, struct player_control *pc,
			  unsigned order)
Avuton Olrich's avatar
Avuton Olrich committed
58
{
59
	char *uri;
60

61
	assert(playlist->queue.IsValidOrder(order));
62

63
	playlist->queued = order;
64

65
	struct song *song =
66
		song_dup_detached(playlist->queue.GetOrder(order));
67

68
	uri = song_get_uri(song);
69
	g_debug("queue song %i:\"%s\"", playlist->queued, uri);
70 71
	g_free(uri);

72
	pc->EnqueueSong(song);
73
}
74

75
/**
76
 * Called if the player thread has started playing the "queued" song.
77
 */
78
static void
79
playlist_song_started(struct playlist *playlist, struct player_control *pc)
Avuton Olrich's avatar
Avuton Olrich committed
80
{
81
	assert(pc->next_song == NULL);
82
	assert(playlist->queued >= -1);
83

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

87 88 89
	int current = playlist->current;
	playlist->current = playlist->queued;
	playlist->queued = -1;
90

91
	if(playlist->queue.consume)
92
		playlist->DeleteOrder(*pc, current);
93 94

	idle_add(IDLE_PLAYER);
Warren Dukes's avatar
Warren Dukes committed
95 96
}

97
const struct song *
98
playlist::GetQueuedSong() const
Avuton Olrich's avatar
Avuton Olrich committed
99
{
100 101 102
	return playing && queued >= 0
		? queue.GetOrder(queued)
		: nullptr;
103 104
}

105
void
106
playlist::UpdateQueuedSong(player_control &pc, const song *prev)
107
{
108
	if (!playing)
109 110
		return;

111 112
	assert(!queue.IsEmpty());
	assert((queued < 0) == (prev == NULL));
113

114 115
	const int next_order = current >= 0
		? queue.GetNextOrder(current)
116 117
		: 0;

118
	if (next_order == 0 && queue.random && !queue.single) {
119 120 121
		/* shuffle the song order again, so we get a different
		   order each time the playlist is played
		   completely */
122 123
		const unsigned current_position =
			queue.OrderToPosition(current);
124

125
		queue.ShuffleOrder();
126

127
		/* make sure that the current still points to
128 129
		   the current song, after the song order has been
		   shuffled */
130
		current = queue.PositionToOrder(current_position);
131 132
	}

133 134 135
	const struct song *const next_song = next_order >= 0
		? queue.GetOrder(next_order)
		: nullptr;
136 137 138

	if (prev != NULL && next_song != prev) {
		/* clear the currently queued song */
139
		pc.Cancel();
140
		queued = -1;
141
	}
Warren Dukes's avatar
Warren Dukes committed
142

143 144
	if (next_order >= 0) {
		if (next_song != prev)
145
			playlist_queue_song_order(this, &pc, next_order);
146
		else
147
			queued = next_order;
148
	}
149 150
}

151
void
152
playlist::PlayOrder(player_control &pc, int order)
Avuton Olrich's avatar
Avuton Olrich committed
153
{
154 155
	playing = true;
	queued = -1;
Eric Wong's avatar
Eric Wong committed
156

157
	struct song *song = song_dup_detached(queue.GetOrder(order));
Warren Dukes's avatar
Warren Dukes committed
158

159 160
	char *uri = song_get_uri(song);
	g_debug("play %i:\"%s\"", order, uri);
161
	g_free(uri);
Warren Dukes's avatar
Warren Dukes committed
162

163
	pc.Play(song);
164
	current = order;
Warren Dukes's avatar
Warren Dukes committed
165 166
}

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

170
void
171
playlist::SyncWithPlayer(player_control &pc)
Avuton Olrich's avatar
Avuton Olrich committed
172
{
173
	if (!playing)
174 175
		/* this event has reached us out of sync: we aren't
		   playing anymore; ignore the event */
Avuton Olrich's avatar
Avuton Olrich committed
176
		return;
Warren Dukes's avatar
Warren Dukes committed
177

178 179
	pc.Lock();
	const player_state pc_state = pc.GetState();
180
	const song *pc_next_song = pc.next_song;
181
	pc.Unlock();
182 183

	if (pc_state == PLAYER_STATE_STOP)
184 185 186 187
		/* 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 */
188
		playlist_resume_playback(this, &pc);
189
	else {
190 191
		/* check if the player thread has already started
		   playing the queued song */
192 193
		if (pc_next_song == nullptr && queued != -1)
			playlist_song_started(this, &pc);
194

195
		pc.Lock();
196
		pc_next_song = pc.next_song;
197
		pc.Unlock();
198

199 200
		/* make sure the queued song is always set (if
		   possible) */
201 202
		if (pc_next_song == nullptr && queued < 0)
			UpdateQueuedSong(pc, nullptr);
203
	}
Warren Dukes's avatar
Warren Dukes committed
204 205
}

206 207 208 209
/**
 * The player has stopped for some reason.  Check the error, and
 * decide whether to re-start playback
 */
210
static void
211
playlist_resume_playback(struct playlist *playlist, struct player_control *pc)
Avuton Olrich's avatar
Avuton Olrich committed
212
{
213
	assert(playlist->playing);
214
	assert(pc->GetState() == PLAYER_STATE_STOP);
Warren Dukes's avatar
Warren Dukes committed
215

216
	const auto error = pc->GetErrorType();
217
	if (error == PLAYER_ERROR_NONE)
218
		playlist->error_count = 0;
219
	else
220
		++playlist->error_count;
221

222 223
	if ((playlist->stop_on_error && error != PLAYER_ERROR_NONE) ||
	    error == PLAYER_ERROR_OUTPUT ||
224
	    playlist->error_count >= playlist->queue.GetLength())
225 226
		/* too many errors, or critical error: stop
		   playback */
227
		playlist->Stop(*pc);
228
	else
229
		/* continue playback at the next song */
230
		playlist->PlayNext(*pc);
231 232
}

233
void
234
playlist::SetRepeat(player_control &pc, bool status)
Avuton Olrich's avatar
Avuton Olrich committed
235
{
236
	if (status == queue.repeat)
237 238
		return;

239
	queue.repeat = status;
240

241
	pc.SetBorderPause(queue.single && !queue.repeat);
242

243 244
	/* if the last song is currently being played, the "next song"
	   might change when repeat mode is toggled */
245
	UpdateQueuedSong(pc, GetQueuedSong());
246

247
	idle_add(IDLE_OPTIONS);
Warren Dukes's avatar
Warren Dukes committed
248 249
}

250 251
static void
playlist_order(struct playlist *playlist)
Avuton Olrich's avatar
Avuton Olrich committed
252
{
253
	if (playlist->current >= 0)
254
		/* update playlist.current, order==position now */
255
		playlist->current = playlist->queue.OrderToPosition(playlist->current);
Warren Dukes's avatar
Warren Dukes committed
256

257
	playlist->queue.RestoreOrder();
Warren Dukes's avatar
Warren Dukes committed
258 259
}

260
void
261
playlist::SetSingle(player_control &pc, bool status)
262
{
263
	if (status == queue.single)
264 265
		return;

266
	queue.single = status;
267

268
	pc.SetBorderPause(queue.single && !queue.repeat);
269 270

	/* if the last song is currently being played, the "next song"
271
	   might change when single mode is toggled */
272
	UpdateQueuedSong(pc, GetQueuedSong());
273 274 275 276

	idle_add(IDLE_OPTIONS);
}

277
void
278
playlist::SetConsume(bool status)
279
{
280
	if (status == queue.consume)
281 282
		return;

283
	queue.consume = status;
284 285 286
	idle_add(IDLE_OPTIONS);
}

287
void
288
playlist::SetRandom(player_control &pc, bool status)
Avuton Olrich's avatar
Avuton Olrich committed
289
{
290
	if (status == queue.random)
291
		return;
Warren Dukes's avatar
Warren Dukes committed
292

293
	const struct song *const queued_song = GetQueuedSong();
294

295
	queue.random = status;
Warren Dukes's avatar
Warren Dukes committed
296

297 298
	if (queue.random) {
		/* shuffle the queue order, but preserve current */
299

300
		const int current_position = GetCurrentPosition();
301

302
		queue.ShuffleOrder();
303 304

		if (current_position >= 0) {
305 306 307
			/* make sure the current song is the first in
			   the order list, so the whole rest of the
			   playlist is played after that */
308
			unsigned current_order =
309 310 311
				queue.PositionToOrder(current_position);
			queue.SwapOrders(0, current_order);
			current = 0;
312
		} else
313
			current = -1;
314
	} else
315
		playlist_order(this);
316

317
	UpdateQueuedSong(pc, queued_song);
318

319
	idle_add(IDLE_OPTIONS);
Warren Dukes's avatar
Warren Dukes committed
320 321
}

322
int
323
playlist::GetCurrentPosition() const
Avuton Olrich's avatar
Avuton Olrich committed
324
{
325 326 327
	return current >= 0
		? queue.OrderToPosition(current)
		: -1;
328 329
}

330
int
331
playlist::GetNextPosition() const
Avuton Olrich's avatar
Avuton Olrich committed
332
{
333 334
	if (current < 0)
		return -1;
335

336 337 338 339 340 341
	if (queue.single && queue.repeat)
		return queue.OrderToPosition(current);
	else if (queue.IsValidOrder(current + 1))
		return queue.OrderToPosition(current + 1);
	else if (queue.repeat)
		return queue.OrderToPosition(0);
342

343
	return -1;
344
}