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

28
#include <cassert>
29

30
void
31
playlist::TagModified(DetachedSong &&song) noexcept
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
inline void
65
playlist::QueueSongOrder(PlayerControl &pc, unsigned order) noexcept
66

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
void
81
playlist::SongStarted() noexcept
82 83
{
	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
inline void
91
playlist::QueuedSongStarted(PlayerControl &pc) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
92
{
93
	assert(!pc.LockGetSyncInfo().has_next_song);
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 122
playlist::UpdateQueuedSong(PlayerControl &pc,
			   const DetachedSong *prev) noexcept
123
{
124
	if (!playing)
125 126
		return;

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

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

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

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

147
		queue.ShuffleOrder();
148

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

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

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

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

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

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

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

183
	current = order;
184

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

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

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

198
	const auto i = pc.LockGetSyncInfo();
199

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

		/* make sure the queued song is always set (if
		   possible) */
214
		if (!pc.LockGetSyncInfo().has_next_song && queued < 0)
215
			UpdateQueuedSong(pc, nullptr);
216
	}
Warren Dukes's avatar
Warren Dukes committed
217 218
}

219
inline void
220
playlist::ResumePlayback(PlayerControl &pc) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
221
{
222
	assert(playing);
223
	assert(pc.GetState() == PlayerState::STOP);
Warren Dukes's avatar
Warren Dukes committed
224

225
	const auto error = pc.GetErrorType();
226
	if (error == PlayerError::NONE)
227
		error_count = 0;
228
	else
229
		++error_count;
230

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

246
void
247
playlist::SetRepeat(PlayerControl &pc, bool status) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
248
{
249
	if (status == queue.repeat)
250 251
		return;

252
	queue.repeat = status;
253

254
	pc.LockSetBorderPause(queue.single != SingleMode::OFF && !queue.repeat);
255

256 257
	/* if the last song is currently being played, the "next song"
	   might change when repeat mode is toggled */
258
	UpdateQueuedSong(pc, GetQueuedSong());
259

260
	listener.OnQueueOptionsChanged();
Warren Dukes's avatar
Warren Dukes committed
261 262
}

263
static void
264
playlist_order(playlist &playlist) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
265
{
266
	if (playlist.current >= 0)
267
		/* update playlist.current, order==position now */
268
		playlist.current = playlist.queue.OrderToPosition(playlist.current);
Warren Dukes's avatar
Warren Dukes committed
269

270
	playlist.queue.RestoreOrder();
Warren Dukes's avatar
Warren Dukes committed
271 272
}

273
void
274
playlist::SetSingle(PlayerControl &pc, SingleMode status) noexcept
275
{
276
	if (status == queue.single)
277 278
		return;

279
	queue.single = status;
280

281 282

	pc.LockSetBorderPause(queue.single != SingleMode::OFF && !queue.repeat);
283 284

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

288
	listener.OnQueueOptionsChanged();
289 290
}

291
void
292
playlist::SetConsume(bool status) noexcept
293
{
294
	if (status == queue.consume)
295 296
		return;

297
	queue.consume = status;
298
	listener.OnQueueOptionsChanged();
299 300
}

301
void
302
playlist::SetRandom(PlayerControl &pc, bool status) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
303
{
304
	if (status == queue.random)
305
		return;
Warren Dukes's avatar
Warren Dukes committed
306

307
	const DetachedSong *const queued_song = GetQueuedSong();
308

309
	queue.random = status;
Warren Dukes's avatar
Warren Dukes committed
310

311 312
	if (queue.random) {
		/* shuffle the queue order, but preserve current */
313

314 315 316
		const int current_position = playing
			? GetCurrentPosition()
			: -1;
317

318
		queue.ShuffleOrder();
319 320

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

332
	UpdateQueuedSong(pc, queued_song);
333

334
	listener.OnQueueOptionsChanged();
Warren Dukes's avatar
Warren Dukes committed
335 336
}

337
int
338
playlist::GetCurrentPosition() const noexcept
Avuton Olrich's avatar
Avuton Olrich committed
339
{
340 341 342
	return current >= 0
		? queue.OrderToPosition(current)
		: -1;
343 344
}

345
int
346
playlist::GetNextPosition() const noexcept
Avuton Olrich's avatar
Avuton Olrich committed
347
{
348 349
	if (current < 0)
		return -1;
350

351
	if (queue.single != SingleMode::OFF && queue.repeat)
352 353 354 355 356
		return queue.OrderToPosition(current);
	else if (queue.IsValidOrder(current + 1))
		return queue.OrderToPosition(current + 1);
	else if (queue.repeat)
		return queue.OrderToPosition(0);
357

358
	return -1;
359
}
360 361

void
362
playlist::BorderPause(PlayerControl &pc) noexcept
363 364 365 366 367 368 369 370
{
	if (queue.single == SingleMode::ONE_SHOT) {
		queue.single = SingleMode::OFF;
		pc.LockSetBorderPause(false);

		listener.OnQueueOptionsChanged();
	}
}