Playlist.cxx 7.49 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "Listener.hxx"
23
#include "PlaylistError.hxx"
24
#include "player/Control.hxx"
25
#include "DetachedSong.hxx"
26
#include "Log.hxx"
Warren Dukes's avatar
Warren Dukes committed
27

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

30
void
31
playlist::TagModified(DetachedSong &&song)
32
{
33
	if (!playing)
34 35
		return;

36
	assert(current >= 0);
37

38 39
	DetachedSong &current_song = queue.GetOrder(current);
	if (song.IsSame(current_song))
40
		current_song.MoveTagItemsFrom(std::move(song));
41

42
	queue.ModifyAtOrder(current);
43
	OnModified();
44 45
}

46 47 48
inline void
playlist::QueueSongOrder(PlayerControl &pc, unsigned order)

Avuton Olrich's avatar
Avuton Olrich committed
49
{
50
	assert(queue.IsValidOrder(order));
51

52
	queued = order;
53

54
	const DetachedSong &song = queue.GetOrder(order);
55

56
	FormatDebug(playlist_domain, "queue song %i:\"%s\"",
57
		    queued, song.GetURI());
58

59
	pc.LockEnqueueSong(new DetachedSong(song));
60
}
61

62 63 64 65
void
playlist::SongStarted()
{
	assert(current >= 0);
66 67 68 69

	/* reset a song's "priority" when playback starts */
	if (queue.SetPriority(queue.OrderToPosition(current), 0, -1, false))
		OnModified();
70 71
}

72 73
inline void
playlist::QueuedSongStarted(PlayerControl &pc)
Avuton Olrich's avatar
Avuton Olrich committed
74
{
75
	assert(pc.next_song == nullptr);
76
	assert(queued >= -1);
77
	assert(current >= 0);
78

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

82 83 84
	const int old_current = current;
	current = queued;
	queued = -1;
85

86 87
	if (queue.consume)
		DeleteOrder(pc, old_current);
88

89
	listener.OnQueueSongStarted();
90 91

	SongStarted();
Warren Dukes's avatar
Warren Dukes committed
92 93
}

94
const DetachedSong *
95
playlist::GetQueuedSong() const noexcept
Avuton Olrich's avatar
Avuton Olrich committed
96
{
97
	return playing && queued >= 0
98
		? &queue.GetOrder(queued)
99
		: nullptr;
100 101
}

102
void
103
playlist::UpdateQueuedSong(PlayerControl &pc, const DetachedSong *prev)
104
{
105
	if (!playing)
106 107
		return;

108 109 110 111 112 113
	if (prev == nullptr && bulk_edit)
		/* postponed until CommitBulk() to avoid always
		   queueing the first song that is being added (in
		   random mode) */
		return;

114
	assert(!queue.IsEmpty());
115
	assert((queued < 0) == (prev == nullptr));
116

117 118
	const int next_order = current >= 0
		? queue.GetNextOrder(current)
119 120
		: 0;

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

128
		queue.ShuffleOrder();
129

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

136
	const DetachedSong *const next_song = next_order >= 0
137
		? &queue.GetOrder(next_order)
138
		: nullptr;
139

140
	if (prev != nullptr && next_song != prev) {
141
		/* clear the currently queued song */
142
		pc.LockCancel();
143
		queued = -1;
144
	}
Warren Dukes's avatar
Warren Dukes committed
145

146 147
	if (next_order >= 0) {
		if (next_song != prev)
148
			QueueSongOrder(pc, next_order);
149
		else
150
			queued = next_order;
151
	}
152 153
}

154 155
void
playlist::PlayOrder(PlayerControl &pc, unsigned order)
Avuton Olrich's avatar
Avuton Olrich committed
156
{
157 158
	playing = true;
	queued = -1;
Eric Wong's avatar
Eric Wong committed
159

160
	const DetachedSong &song = queue.GetOrder(order);
Warren Dukes's avatar
Warren Dukes committed
161

162
	FormatDebug(playlist_domain, "play %u:\"%s\"", order, song.GetURI());
Warren Dukes's avatar
Warren Dukes committed
163

164
	current = order;
165

166
	pc.Play(new DetachedSong(song));
167

168
	SongStarted();
Warren Dukes's avatar
Warren Dukes committed
169 170
}

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

179
	pc.Lock();
180
	const PlayerState pc_state = pc.GetState();
181
	const DetachedSong *pc_next_song = pc.next_song;
182
	pc.Unlock();
183

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

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

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

207 208
inline void
playlist::ResumePlayback(PlayerControl &pc)
Avuton Olrich's avatar
Avuton Olrich committed
209
{
210
	assert(playing);
211
	assert(pc.GetState() == PlayerState::STOP);
Warren Dukes's avatar
Warren Dukes committed
212

213
	const auto error = pc.GetErrorType();
214
	if (error == PlayerError::NONE)
215
		error_count = 0;
216
	else
217
		++error_count;
218

219
	if ((stop_on_error && error != PlayerError::NONE) ||
220
	    error == PlayerError::OUTPUT ||
221
	    error_count >= queue.GetLength())
222 223
		/* too many errors, or critical error: stop
		   playback */
224
		Stop(pc);
225
	else
226
		/* continue playback at the next song */
227 228 229 230 231
		try {
			PlayNext(pc);
		} catch (...) {
			/* TODO: log error? */
		}
232 233
}

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

240
	queue.repeat = status;
241

242
	pc.LockSetBorderPause(queue.single && !queue.repeat);
243

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

248
	listener.OnQueueOptionsChanged();
Warren Dukes's avatar
Warren Dukes committed
249 250
}

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

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

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

267
	queue.single = status;
268

269
	pc.LockSetBorderPause(queue.single && !queue.repeat);
270 271

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

275
	listener.OnQueueOptionsChanged();
276 277
}

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

284
	queue.consume = status;
285
	listener.OnQueueOptionsChanged();
286 287
}

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

294
	const DetachedSong *const queued_song = GetQueuedSong();
295

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

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

301 302 303
		const int current_position = playing
			? GetCurrentPosition()
			: -1;
304

305
		queue.ShuffleOrder();
306 307

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

319
	UpdateQueuedSong(pc, queued_song);
320

321
	listener.OnQueueOptionsChanged();
Warren Dukes's avatar
Warren Dukes committed
322 323
}

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

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

338 339 340 341 342 343
	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);
344

345
	return -1;
346
}