Playlist.hxx 9.19 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 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"
Warren Dukes's avatar
Warren Dukes committed
25

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

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

42 43
	QueueListener &listener;

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

50 51 52 53 54 55 56
	/**
	 * 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;

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

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

69 70 71 72 73 74 75
	/**
	 * 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;

76
	/**
77 78 79
	 * 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.
80
	 */
81
	int current;
82 83

	/**
84 85 86 87
	 * 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.
88 89
	 *
	 * This variable is only valid if #playing is true.
90
	 */
91 92
	int queued;

93 94 95 96 97
	playlist(unsigned max_length,
		 QueueListener &_listener)
		:queue(max_length),
		 listener(_listener),
		 playing(false),
98 99
		 bulk_edit(false),
		 current(-1), queued(-1) {
100
	}
Warren Dukes's avatar
Warren Dukes committed
101

102 103
	~playlist() {
	}
104

105 106 107
	uint32_t GetVersion() const {
		return queue.version;
	}
108

109 110 111
	unsigned GetLength() const {
		return queue.GetLength();
	}
112

113 114 115
	unsigned PositionToId(unsigned position) const {
		return queue.PositionToId(position);
	}
116

117
	gcc_pure
118
	int GetCurrentPosition() const noexcept;
Warren Dukes's avatar
Warren Dukes committed
119

120
	gcc_pure
121
	int GetNextPosition() const noexcept;
122

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

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

137 138 139 140 141 142
	/**
	 * This is the "BORDER_PAUSE" event handler.  It is invoked by
	 * the player thread whenever playback goes into border pause.
	 */
	void BorderPause(PlayerControl &pc);

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

151 152 153 154 155 156 157 158 159 160 161
	/**
	 * 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.
	 */
	void SongStarted();

162 163 164 165 166 167 168 169 170
	/**
	 * 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()
	 */
171
	void UpdateQueuedSong(PlayerControl &pc, const DetachedSong *prev);
172

173 174 175 176 177
	/**
	 * Queue a song, addressed by its order number.
	 */
	void QueueSongOrder(PlayerControl &pc, unsigned order);

178 179
	/**
	 * Called when the player thread has started playing the
180 181
	 * "queued" song, i.e. it has switched from one song to the
	 * next automatically.
182 183 184
	 */
	void QueuedSongStarted(PlayerControl &pc);

185 186 187 188 189 190
	/**
	 * The player has stopped for some reason.  Check the error,
	 * and decide whether to re-start playback.
	 */
	void ResumePlayback(PlayerControl &pc);

191
public:
192 193 194
	void BeginBulk();
	void CommitBulk(PlayerControl &pc);

195
	void Clear(PlayerControl &pc);
196

197 198 199 200 201
	/**
	 * 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.
	 */
202
	void TagModified(DetachedSong &&song);
203
	void TagModified(const char *uri, const Tag &tag) noexcept;
Warren Dukes's avatar
Warren Dukes committed
204

205
#ifdef ENABLE_DATABASE
206 207 208
	/**
	 * The database has been modified.  Pull all updates.
	 */
209
	void DatabaseModified(const Database &db);
210
#endif
211

212
	/**
213 214 215
	 * Throws PlaylistError if the queue would be too large.
	 *
	 * @return the new song id
216
	 */
217
	unsigned AppendSong(PlayerControl &pc, DetachedSong &&song);
218 219

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

228
protected:
229
	void DeleteInternal(PlayerControl &pc,
230
			    unsigned song, const DetachedSong **queued_p);
Warren Dukes's avatar
Warren Dukes committed
231

232
public:
233
	void DeletePosition(PlayerControl &pc, unsigned position);
Warren Dukes's avatar
Warren Dukes committed
234

235 236
	void DeleteOrder(PlayerControl &pc, unsigned order) {
		DeletePosition(pc, queue.OrderToPosition(order));
237
	}
Warren Dukes's avatar
Warren Dukes committed
238

239
	void DeleteId(PlayerControl &pc, unsigned id);
240 241 242 243 244 245 246

	/**
	 * 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
	 */
247
	void DeleteRange(PlayerControl &pc, unsigned start, unsigned end);
Warren Dukes's avatar
Warren Dukes committed
248

249 250 251 252 253 254 255
	/**
	 * 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.
	 */
	void StaleSong(PlayerControl &pc, const char *uri);
Warren Dukes's avatar
Warren Dukes committed
256

257
	void Shuffle(PlayerControl &pc, unsigned start, unsigned end);
258

259 260
	void MoveRange(PlayerControl &pc, unsigned start,
		       unsigned end, int to);
Warren Dukes's avatar
Warren Dukes committed
261

262
	void MoveId(PlayerControl &pc, unsigned id, int to);
263

264
	void SwapPositions(PlayerControl &pc, unsigned song1, unsigned song2);
265

266
	void SwapIds(PlayerControl &pc, unsigned id1, unsigned id2);
267

268 269 270
	void SetPriorityRange(PlayerControl &pc,
			      unsigned start_position, unsigned end_position,
			      uint8_t priority);
Warren Dukes's avatar
Warren Dukes committed
271

272 273
	void SetPriorityId(PlayerControl &pc,
			   unsigned song_id, uint8_t priority);
Warren Dukes's avatar
Warren Dukes committed
274

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

282 283
	void AddSongIdTag(unsigned id, TagType tag_type, const char *value);
	void ClearSongIdTag(unsigned id, TagType tag_type);
284

285
	void Stop(PlayerControl &pc);
Warren Dukes's avatar
Warren Dukes committed
286

287
	/**
288
	 * Throws on error.
289 290
	 */
	void PlayPosition(PlayerControl &pc, int position);
Warren Dukes's avatar
Warren Dukes committed
291

292
	/**
293
	 * Throws on error.
294 295
	 */
	void PlayOrder(PlayerControl &pc, unsigned order);
296

297
	/**
298
	 * Throws on error.
299 300
	 */
	void PlayId(PlayerControl &pc, int id);
301

302
	/**
303
	 * Throws on error.
304 305
	 */
	void PlayNext(PlayerControl &pc);
306

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

312
	/**
313
	 * Throws on error.
314 315
	 */
	void SeekSongOrder(PlayerControl &pc,
316
			   unsigned song_order,
317
			   SongTime seek_time);
318

319
	/**
320
	 * Throws on error.
321 322
	 */
	void SeekSongPosition(PlayerControl &pc,
323
			      unsigned sonag_position,
324
			      SongTime seek_time);
325

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

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

345 346 347
	bool GetRepeat() const {
		return queue.repeat;
	}
348

349
	void SetRepeat(PlayerControl &pc, bool new_value);
350

351 352 353
	bool GetRandom() const {
		return queue.random;
	}
Warren Dukes's avatar
Warren Dukes committed
354

355
	void SetRandom(PlayerControl &pc, bool new_value);
Warren Dukes's avatar
Warren Dukes committed
356

357
	SingleMode GetSingle() const {
358 359
		return queue.single;
	}
Warren Dukes's avatar
Warren Dukes committed
360

361
	void SetSingle(PlayerControl &pc, SingleMode new_value);
362

363 364 365 366 367
	bool GetConsume() const {
		return queue.consume;
	}

	void SetConsume(bool new_value);
368 369 370 371 372 373 374 375 376 377 378 379

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
	 */
	unsigned MoveOrderToCurrent(unsigned old_order);
380 381
};

Warren Dukes's avatar
Warren Dukes committed
382
#endif