Playlist.cxx 8 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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

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.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
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 == SingleMode::OFF) {
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
	const auto i = pc.LockGetSyncInfo();
198

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

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

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

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

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

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

251
	queue.repeat = status;
252

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

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

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

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

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

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

278
	queue.single = status;
279

280 281

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

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

287
	listener.OnQueueOptionsChanged();
288 289
}

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

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

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

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

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

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

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

317
		queue.ShuffleOrder();
318 319

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

331
	UpdateQueuedSong(pc, queued_song);
332

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

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

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

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

357
	return -1;
358
}
359 360 361 362 363 364 365 366 367 368 369

void
playlist::BorderPause(PlayerControl &pc)
{
	if (queue.single == SingleMode::ONE_SHOT) {
		queue.single = SingleMode::OFF;
		pc.LockSetBorderPause(false);

		listener.OnQueueOptionsChanged();
	}
}