Playlist.hxx 9.5 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 21
#ifndef MPD_PLAYLIST_HXX
#define MPD_PLAYLIST_HXX
Warren Dukes's avatar
Warren Dukes committed
22

23
#include "SingleMode.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "queue/Queue.hxx"
25
#include "config.h"
Warren Dukes's avatar
Warren Dukes committed
26

27
enum TagType : uint8_t;
28
struct Tag;
29
class PlayerControl;
30
class DetachedSong;
31
class Database;
32
class SongLoader;
33 34
class SongTime;
class SignedSongTime;
35
class QueueListener;
36

37
struct playlist {
38 39 40
	/**
	 * The song queue - it contains the "real" playlist.
	 */
41
	Queue queue;
42

43 44
	QueueListener &listener;

45 46 47 48
	/**
	 * This value is true if the player is currently playing (or
	 * should be playing).
	 */
49
	bool playing = false;
50

51 52 53 54 55 56 57
	/**
	 * If true, then any error is fatal; if false, MPD will
	 * attempt to play the next song on non-fatal errors.  During
	 * seeking, this flag is set.
	 */
	bool stop_on_error;

58 59 60 61 62
	/**
	 * If true, then a bulk edit has been initiated by
	 * BeginBulk(), and UpdateQueuedSong() and OnModified() will
	 * be postponed until CommitBulk()
	 */
63
	bool bulk_edit = false;
64 65 66 67 68 69

	/**
	 * Has the queue been modified during bulk edit mode?
	 */
	bool bulk_modified;

70 71 72 73 74 75 76
	/**
	 * Number of errors since playback was started.  If this
	 * number exceeds the length of the playlist, MPD gives up,
	 * because all songs have been tried.
	 */
	unsigned error_count;

77
	/**
78 79 80
	 * The "current song pointer" (the order number).  This is the
	 * song which is played when we get the "play" command.  It is
	 * also the song which is currently being played.
81
	 */
82
	int current = -1;
83 84

	/**
85 86 87 88
	 * The "next" song to be played (the order number), when the
	 * current one finishes.  The decoder thread may start
	 * decoding and buffering it, while the "current" song is
	 * still playing.
89 90
	 *
	 * This variable is only valid if #playing is true.
91
	 */
92
	int queued = -1;
93

94
	playlist(unsigned max_length,
95
		 QueueListener &_listener) noexcept
96
		:queue(max_length),
97 98
		 listener(_listener)
	{
99
	}
Warren Dukes's avatar
Warren Dukes committed
100

101
	uint32_t GetVersion() const noexcept {
102 103
		return queue.version;
	}
104

105
	unsigned GetLength() const noexcept {
106 107
		return queue.GetLength();
	}
108

109
	unsigned PositionToId(unsigned position) const noexcept {
110 111
		return queue.PositionToId(position);
	}
112

113
	gcc_pure
114
	int GetCurrentPosition() const noexcept;
Warren Dukes's avatar
Warren Dukes committed
115

116
	gcc_pure
117
	int GetNextPosition() const noexcept;
118

119 120 121 122 123
	/**
	 * Returns the song object which is currently queued.  Returns
	 * none if there is none (yet?) or if MPD isn't playing.
	 */
	gcc_pure
124
	const DetachedSong *GetQueuedSong() const noexcept;
Warren Dukes's avatar
Warren Dukes committed
125

126 127 128 129 130
	/**
	 * This is the "PLAYLIST" event handler.  It is invoked by the
	 * player thread whenever it requests a new queued song, or
	 * when it exits.
	 */
131
	void SyncWithPlayer(PlayerControl &pc) noexcept;
Warren Dukes's avatar
Warren Dukes committed
132

133 134 135 136
	/**
	 * This is the "BORDER_PAUSE" event handler.  It is invoked by
	 * the player thread whenever playback goes into border pause.
	 */
137
	void BorderPause(PlayerControl &pc) noexcept;
138

139 140 141
protected:
	/**
	 * Called by all editing methods after a modification.
142 143
	 * Updates the queue version and invokes
	 * QueueListener::OnQueueModified().
144
	 */
145
	void OnModified() noexcept;
Warren Dukes's avatar
Warren Dukes committed
146

147 148 149 150 151 152 153 154 155
	/**
	 * Called when playback of a new song starts.  Unlike
	 * QueuedSongStarted(), this also gets called when the user
	 * manually switches to another song.  It may be used for
	 * playlist fixups.
	 *
	 * The song being started is specified by the #current
	 * attribute.
	 */
156
	void SongStarted() noexcept;
157

158 159 160 161 162 163 164 165 166
	/**
	 * Updates the "queued song".  Calculates the next song
	 * according to the current one (if MPD isn't playing, it
	 * takes the first song), and queues this song.  Clears the
	 * old queued song if there was one.
	 *
	 * @param prev the song which was previously queued, as
	 * determined by playlist_get_queued_song()
	 */
167
	void UpdateQueuedSong(PlayerControl &pc, const DetachedSong *prev) noexcept;
168

169 170 171
	/**
	 * Queue a song, addressed by its order number.
	 */
172
	void QueueSongOrder(PlayerControl &pc, unsigned order) noexcept;
173

174 175
	/**
	 * Called when the player thread has started playing the
176 177
	 * "queued" song, i.e. it has switched from one song to the
	 * next automatically.
178
	 */
179
	void QueuedSongStarted(PlayerControl &pc) noexcept;
180

181 182 183 184
	/**
	 * The player has stopped for some reason.  Check the error,
	 * and decide whether to re-start playback.
	 */
185
	void ResumePlayback(PlayerControl &pc) noexcept;
186

187
public:
188 189
	void BeginBulk() noexcept;
	void CommitBulk(PlayerControl &pc) noexcept;
190

191
	void Clear(PlayerControl &pc) noexcept;
192

193 194 195 196 197
	/**
	 * A tag in the play queue has been modified by the player
	 * thread.  Apply the given song's tag to the current song if
	 * the song matches.
	 */
198
	void TagModified(DetachedSong &&song) noexcept;
199
	void TagModified(const char *uri, const Tag &tag) noexcept;
Warren Dukes's avatar
Warren Dukes committed
200

201
#ifdef ENABLE_DATABASE
202 203 204
	/**
	 * The database has been modified.  Pull all updates.
	 */
205
	void DatabaseModified(const Database &db);
206
#endif
207

208
	/**
209 210 211
	 * Throws PlaylistError if the queue would be too large.
	 *
	 * @return the new song id
212
	 */
213
	unsigned AppendSong(PlayerControl &pc, DetachedSong &&song);
214 215

	/**
216 217 218
	 * Throws #std::runtime_error on error.
	 *
	 * @return the new song id
219 220 221
	 */
	unsigned AppendURI(PlayerControl &pc,
			   const SongLoader &loader,
222
			   const char *uri_utf8);
Warren Dukes's avatar
Warren Dukes committed
223

224
protected:
225
	void DeleteInternal(PlayerControl &pc,
226
			    unsigned song, const DetachedSong **queued_p) noexcept;
Warren Dukes's avatar
Warren Dukes committed
227

228
public:
229
	void DeletePosition(PlayerControl &pc, unsigned position);
Warren Dukes's avatar
Warren Dukes committed
230

231 232
	void DeleteOrder(PlayerControl &pc, unsigned order) {
		DeletePosition(pc, queue.OrderToPosition(order));
233
	}
Warren Dukes's avatar
Warren Dukes committed
234

235
	void DeleteId(PlayerControl &pc, unsigned id);
236 237 238 239 240 241 242

	/**
	 * Deletes a range of songs from the playlist.
	 *
	 * @param start the position of the first song to delete
	 * @param end the position after the last song to delete
	 */
243
	void DeleteRange(PlayerControl &pc, unsigned start, unsigned end);
Warren Dukes's avatar
Warren Dukes committed
244

245 246 247 248 249 250
	/**
	 * Mark the given song as "stale", i.e. as not being available
	 * anymore.  This gets called when a song is removed from the
	 * database.  The method attempts to remove all instances of
	 * this song from the queue.
	 */
251
	void StaleSong(PlayerControl &pc, const char *uri) noexcept;
Warren Dukes's avatar
Warren Dukes committed
252

253
	void Shuffle(PlayerControl &pc, unsigned start, unsigned end) noexcept;
254

255 256
	void MoveRange(PlayerControl &pc, unsigned start,
		       unsigned end, int to);
Warren Dukes's avatar
Warren Dukes committed
257

258
	void MoveId(PlayerControl &pc, unsigned id, int to);
259

260
	void SwapPositions(PlayerControl &pc, unsigned song1, unsigned song2);
261

262
	void SwapIds(PlayerControl &pc, unsigned id1, unsigned id2);
263

264 265 266
	void SetPriorityRange(PlayerControl &pc,
			      unsigned start_position, unsigned end_position,
			      uint8_t priority);
Warren Dukes's avatar
Warren Dukes committed
267

268 269
	void SetPriorityId(PlayerControl &pc,
			   unsigned song_id, uint8_t priority);
Warren Dukes's avatar
Warren Dukes committed
270

271
	/**
272
	 * Sets the start_time and end_time attributes on the song
273
	 * with the specified id.
274 275
	 *
	 * Throws on error.
276
	 */
277 278
	void SetSongIdRange(PlayerControl &pc, unsigned id,
			    SongTime start, SongTime end);
279

280 281 282 283 284 285 286 287 288
	/**
	 * Throws on error.
	 */
	void AddSongIdTag(unsigned id, TagType tag_type,
			  const char *value);

	/**
	 * Throws on error.
	 */
289
	void ClearSongIdTag(unsigned id, TagType tag_type);
290

291
	void Stop(PlayerControl &pc) noexcept;
Warren Dukes's avatar
Warren Dukes committed
292

293
	/**
294
	 * Throws on error.
295 296
	 */
	void PlayPosition(PlayerControl &pc, int position);
Warren Dukes's avatar
Warren Dukes committed
297

298
	/**
299
	 * Throws on error.
300 301
	 */
	void PlayOrder(PlayerControl &pc, unsigned order);
302

303
	/**
304
	 * Throws on error.
305 306
	 */
	void PlayId(PlayerControl &pc, int id);
307

308
	/**
309
	 * Throws on error.
310 311
	 */
	void PlayNext(PlayerControl &pc);
312

313
	/**
314
	 * Throws on error.
315 316
	 */
	void PlayPrevious(PlayerControl &pc);
317

318
	/**
319
	 * Throws on error.
320 321
	 */
	void SeekSongOrder(PlayerControl &pc,
322
			   unsigned song_order,
323
			   SongTime seek_time);
324

325
	/**
326
	 * Throws on error.
327 328
	 */
	void SeekSongPosition(PlayerControl &pc,
329
			      unsigned sonag_position,
330
			      SongTime seek_time);
331

332
	/**
333
	 * Throws on error.
334 335 336
	 */
	void SeekSongId(PlayerControl &pc,
			unsigned song_id, SongTime seek_time);
337

338 339 340 341
	/**
	 * Seek within the current song.  Fails if MPD is not currently
	 * playing.
	 *
342
	 * Throws on error.
343
	 *
Max Kellermann's avatar
Max Kellermann committed
344
	 * @param seek_time the time
345 346 347
	 * @param relative if true, then the specified time is relative to the
	 * current position
	 */
348 349
	void SeekCurrent(PlayerControl &pc,
			 SignedSongTime seek_time, bool relative);
Warren Dukes's avatar
Warren Dukes committed
350

351
	bool GetRepeat() const noexcept {
352 353
		return queue.repeat;
	}
354

355
	void SetRepeat(PlayerControl &pc, bool new_value) noexcept;
356

357
	bool GetRandom() const noexcept {
358 359
		return queue.random;
	}
Warren Dukes's avatar
Warren Dukes committed
360

361
	void SetRandom(PlayerControl &pc, bool new_value) noexcept;
Warren Dukes's avatar
Warren Dukes committed
362

363
	SingleMode GetSingle() const noexcept {
364 365
		return queue.single;
	}
Warren Dukes's avatar
Warren Dukes committed
366

367
	void SetSingle(PlayerControl &pc, SingleMode new_value) noexcept;
368

369
	bool GetConsume() const noexcept {
370 371 372
		return queue.consume;
	}

373
	void SetConsume(bool new_value) noexcept;
374 375 376 377 378 379 380 381 382 383 384

private:
	/**
	 * Prepare a manual song change: move the given song to the
	 * current playback order.  This is done to avoid skipping
	 * upcoming songs in the order list.  The newly selected song
	 * shall be inserted in the order list, and the rest shall be
	 * played after that as previously planned.
	 *
	 * @return the new order number of the given song
	 */
385
	unsigned MoveOrderToCurrent(unsigned old_order) noexcept;
386 387
};

Warren Dukes's avatar
Warren Dukes committed
388
#endif