Playlist.cxx 7.83 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
void
playlist::TagModified(const char *uri, const Tag &tag) noexcept
{
	bool modified = false;

	for (unsigned i = 0; i < queue.length; ++i) {
		auto &song = *queue.items[i].song;
		if (song.IsURI(uri)) {
			song.SetTag(tag);
			queue.ModifyAtPosition(i);
			modified = true;
		}
	}

	if (modified)
		OnModified();
}

64 65 66
inline void
playlist::QueueSongOrder(PlayerControl &pc, unsigned order)

Avuton Olrich's avatar
Avuton Olrich committed
67
{
68
	assert(queue.IsValidOrder(order));
69

70
	queued = order;
71

72
	const DetachedSong &song = queue.GetOrder(order);
73

74
	FormatDebug(playlist_domain, "queue song %i:\"%s\"",
75
		    queued, song.GetURI());
76

77
	pc.LockEnqueueSong(std::make_unique<DetachedSong>(song));
78
}
79

80 81 82 83
void
playlist::SongStarted()
{
	assert(current >= 0);
84 85 86 87

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

90 91
inline void
playlist::QueuedSongStarted(PlayerControl &pc)
Avuton Olrich's avatar
Avuton Olrich committed
92
{
93
	assert(pc.next_song == nullptr);
94
	assert(queued >= -1);
95
	assert(current >= 0);
96

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

100 101 102
	const int old_current = current;
	current = queued;
	queued = -1;
103

104 105
	if (queue.consume)
		DeleteOrder(pc, old_current);
106

107
	listener.OnQueueSongStarted();
108 109

	SongStarted();
Warren Dukes's avatar
Warren Dukes committed
110 111
}

112
const DetachedSong *
113
playlist::GetQueuedSong() const noexcept
Avuton Olrich's avatar
Avuton Olrich committed
114
{
115
	return playing && queued >= 0
116
		? &queue.GetOrder(queued)
117
		: nullptr;
118 119
}

120
void
121
playlist::UpdateQueuedSong(PlayerControl &pc, const DetachedSong *prev)
122
{
123
	if (!playing)
124 125
		return;

126 127 128 129 130 131
	if (prev == nullptr && bulk_edit)
		/* postponed until CommitBulk() to avoid always
		   queueing the first song that is being added (in
		   random mode) */
		return;

132
	assert(!queue.IsEmpty());
133
	assert((queued < 0) == (prev == nullptr));
134

135 136
	const int next_order = current >= 0
		? queue.GetNextOrder(current)
137 138
		: 0;

139
	if (next_order == 0 && queue.random && !queue.single) {
140 141 142
		/* shuffle the song order again, so we get a different
		   order each time the playlist is played
		   completely */
143 144
		const unsigned current_position =
			queue.OrderToPosition(current);
145

146
		queue.ShuffleOrder();
147

148
		/* make sure that the current still points to
149 150
		   the current song, after the song order has been
		   shuffled */
151
		current = queue.PositionToOrder(current_position);
152 153
	}

154
	const DetachedSong *const next_song = next_order >= 0
155
		? &queue.GetOrder(next_order)
156
		: nullptr;
157

158
	if (prev != nullptr && next_song != prev) {
159
		/* clear the currently queued song */
160
		pc.LockCancel();
161
		queued = -1;
162
	}
Warren Dukes's avatar
Warren Dukes committed
163

164 165
	if (next_order >= 0) {
		if (next_song != prev)
166
			QueueSongOrder(pc, next_order);
167
		else
168
			queued = next_order;
169
	}
170 171
}

172 173
void
playlist::PlayOrder(PlayerControl &pc, unsigned order)
Avuton Olrich's avatar
Avuton Olrich committed
174
{
175 176
	playing = true;
	queued = -1;
Eric Wong's avatar
Eric Wong committed
177

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

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

182
	current = order;
183

184
	pc.Play(std::make_unique<DetachedSong>(song));
185

186
	SongStarted();
Warren Dukes's avatar
Warren Dukes committed
187 188
}

189
void
190
playlist::SyncWithPlayer(PlayerControl &pc)
Avuton Olrich's avatar
Avuton Olrich committed
191
{
192
	if (!playing)
193 194
		/* this event has reached us out of sync: we aren't
		   playing anymore; ignore the event */
Avuton Olrich's avatar
Avuton Olrich committed
195
		return;
Warren Dukes's avatar
Warren Dukes committed
196

197
	pc.Lock();
198
	const PlayerState pc_state = pc.GetState();
199
	bool pc_has_next_song = pc.next_song != nullptr;
200
	pc.Unlock();
201

202
	if (pc_state == PlayerState::STOP)
203 204 205 206
		/* 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 */
207
		ResumePlayback(pc);
208
	else {
209 210
		/* check if the player thread has already started
		   playing the queued song */
211
		if (!pc_has_next_song && queued != -1)
212
			QueuedSongStarted(pc);
213

214
		pc.Lock();
215
		pc_has_next_song = pc.next_song != nullptr;
216
		pc.Unlock();
217

218 219
		/* make sure the queued song is always set (if
		   possible) */
220
		if (!pc_has_next_song && queued < 0)
221
			UpdateQueuedSong(pc, nullptr);
222
	}
Warren Dukes's avatar
Warren Dukes committed
223 224
}

225 226
inline void
playlist::ResumePlayback(PlayerControl &pc)
Avuton Olrich's avatar
Avuton Olrich committed
227
{
228
	assert(playing);
229
	assert(pc.GetState() == PlayerState::STOP);
Warren Dukes's avatar
Warren Dukes committed
230

231
	const auto error = pc.GetErrorType();
232
	if (error == PlayerError::NONE)
233
		error_count = 0;
234
	else
235
		++error_count;
236

237
	if ((stop_on_error && error != PlayerError::NONE) ||
238
	    error == PlayerError::OUTPUT ||
239
	    error_count >= queue.GetLength())
240 241
		/* too many errors, or critical error: stop
		   playback */
242
		Stop(pc);
243
	else
244
		/* continue playback at the next song */
245 246 247 248 249
		try {
			PlayNext(pc);
		} catch (...) {
			/* TODO: log error? */
		}
250 251
}

252
void
253
playlist::SetRepeat(PlayerControl &pc, bool status)
Avuton Olrich's avatar
Avuton Olrich committed
254
{
255
	if (status == queue.repeat)
256 257
		return;

258
	queue.repeat = status;
259

260
	pc.LockSetBorderPause(queue.single && !queue.repeat);
261

262 263
	/* if the last song is currently being played, the "next song"
	   might change when repeat mode is toggled */
264
	UpdateQueuedSong(pc, GetQueuedSong());
265

266
	listener.OnQueueOptionsChanged();
Warren Dukes's avatar
Warren Dukes committed
267 268
}

269
static void
270
playlist_order(playlist &playlist)
Avuton Olrich's avatar
Avuton Olrich committed
271
{
272
	if (playlist.current >= 0)
273
		/* update playlist.current, order==position now */
274
		playlist.current = playlist.queue.OrderToPosition(playlist.current);
Warren Dukes's avatar
Warren Dukes committed
275

276
	playlist.queue.RestoreOrder();
Warren Dukes's avatar
Warren Dukes committed
277 278
}

279
void
280
playlist::SetSingle(PlayerControl &pc, bool status)
281
{
282
	if (status == queue.single)
283 284
		return;

285
	queue.single = status;
286

287
	pc.LockSetBorderPause(queue.single && !queue.repeat);
288 289

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

293
	listener.OnQueueOptionsChanged();
294 295
}

296
void
297
playlist::SetConsume(bool status)
298
{
299
	if (status == queue.consume)
300 301
		return;

302
	queue.consume = status;
303
	listener.OnQueueOptionsChanged();
304 305
}

306
void
307
playlist::SetRandom(PlayerControl &pc, bool status)
Avuton Olrich's avatar
Avuton Olrich committed
308
{
309
	if (status == queue.random)
310
		return;
Warren Dukes's avatar
Warren Dukes committed
311

312
	const DetachedSong *const queued_song = GetQueuedSong();
313

314
	queue.random = status;
Warren Dukes's avatar
Warren Dukes committed
315

316 317
	if (queue.random) {
		/* shuffle the queue order, but preserve current */
318

319 320 321
		const int current_position = playing
			? GetCurrentPosition()
			: -1;
322

323
		queue.ShuffleOrder();
324 325

		if (current_position >= 0) {
326 327 328
			/* make sure the current song is the first in
			   the order list, so the whole rest of the
			   playlist is played after that */
329
			unsigned current_order =
330
				queue.PositionToOrder(current_position);
331
			current = queue.MoveOrder(current_order, 0);
332
		} else
333
			current = -1;
334
	} else
335
		playlist_order(*this);
336

337
	UpdateQueuedSong(pc, queued_song);
338

339
	listener.OnQueueOptionsChanged();
Warren Dukes's avatar
Warren Dukes committed
340 341
}

342
int
343
playlist::GetCurrentPosition() const noexcept
Avuton Olrich's avatar
Avuton Olrich committed
344
{
345 346 347
	return current >= 0
		? queue.OrderToPosition(current)
		: -1;
348 349
}

350
int
351
playlist::GetNextPosition() const noexcept
Avuton Olrich's avatar
Avuton Olrich committed
352
{
353 354
	if (current < 0)
		return -1;
355

356 357 358 359 360 361
	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);
362

363
	return -1;
364
}