Partition.hxx 7.29 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

#ifndef MPD_PARTITION_HXX
#define MPD_PARTITION_HXX

23
#include "event/MaskMonitor.hxx"
24
#include "queue/Playlist.hxx"
25
#include "queue/Listener.hxx"
26
#include "output/MultipleOutputs.hxx"
27
#include "mixer/Listener.hxx"
28 29
#include "player/Control.hxx"
#include "player/Listener.hxx"
30
#include "ReplayGainMode.hxx"
31
#include "SingleMode.hxx"
32
#include "Chrono.hxx"
33
#include "config.h"
34

35 36
#include <boost/intrusive/list.hpp>

37
#include <string>
38
#include <memory>
39

40
struct Instance;
41
class MultipleOutputs;
42
class SongLoader;
43
class ClientListener;
44
class Client;
45

46 47 48 49
/**
 * A partition of the Music Player Daemon.  It is a separate unit with
 * a playlist, a player, outputs etc.
 */
50
struct Partition final : QueueListener, PlayerListener, MixerListener {
51 52
	static constexpr unsigned TAG_MODIFIED = 0x1;
	static constexpr unsigned SYNC_WITH_PLAYER = 0x2;
53
	static constexpr unsigned BORDER_PAUSE = 0x4;
54

55 56
	Instance &instance;

57 58
	const std::string name;

59 60
	std::unique_ptr<ClientListener> listener;

61 62 63 64 65
	boost::intrusive::list<Client,
			       boost::intrusive::base_hook<boost::intrusive::list_base_hook<boost::intrusive::tag<Partition>,
											    boost::intrusive::link_mode<boost::intrusive::normal_link>>>,
			       boost::intrusive::constant_time_size<false>> clients;

66 67 68 69 70
	/**
	 * Monitor for idle events local to this partition.
	 */
	MaskMonitor idle_monitor;

71
	MaskMonitor global_events;
72

73 74
	struct playlist playlist;

75 76
	MultipleOutputs outputs;

77
	PlayerControl pc;
78

79 80
	ReplayGainMode replay_gain_mode = ReplayGainMode::OFF;

81
	Partition(Instance &_instance,
82
		  const char *_name,
83
		  unsigned max_length,
84
		  unsigned buffer_chunks,
85
		  AudioFormat configured_audio_format,
86
		  const ReplayGainConfig &replay_gain_config) noexcept;
87

88 89
	~Partition() noexcept;

90 91
	void BeginShutdown() noexcept;

92
	void EmitGlobalEvent(unsigned mask) noexcept {
93 94
		global_events.OrMask(mask);
	}
95

96 97 98
	/**
	 * Emit an "idle" event to all clients of this partition.
	 *
99
	 * This method can be called from any thread.
100
	 */
101 102 103
	void EmitIdle(unsigned mask) noexcept {
		idle_monitor.OrMask(mask);
	}
104

105 106 107 108 109 110 111 112
	/**
	 * Populate the #InputCacheManager with soon-to-be-played song
	 * files.
	 *
	 * Errors will be logged.
	 */
	void PrefetchQueue() noexcept;

113
	void ClearQueue() noexcept {
114 115 116
		playlist.Clear(pc);
	}

117
	unsigned AppendURI(const SongLoader &loader,
118 119
			   const char *uri_utf8) {
		return playlist.AppendURI(pc, loader, uri_utf8);
120 121
	}

122 123
	void DeletePosition(unsigned position) {
		playlist.DeletePosition(pc, position);
124 125
	}

126 127
	void DeleteId(unsigned id) {
		playlist.DeleteId(pc, id);
128 129 130 131 132 133 134 135
	}

	/**
	 * 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
	 */
136 137
	void DeleteRange(unsigned start, unsigned end) {
		playlist.DeleteRange(pc, start, end);
138 139
	}

140
	void StaleSong(const char *uri) noexcept {
141
		playlist.StaleSong(pc, uri);
142 143
	}

144
	void Shuffle(unsigned start, unsigned end) noexcept {
145 146 147
		playlist.Shuffle(pc, start, end);
	}

148 149
	void MoveRange(unsigned start, unsigned end, int to) {
		playlist.MoveRange(pc, start, end, to);
150 151
	}

152 153
	void MoveId(unsigned id, int to) {
		playlist.MoveId(pc, id, to);
154 155
	}

156 157
	void SwapPositions(unsigned song1, unsigned song2) {
		playlist.SwapPositions(pc, song1, song2);
158 159
	}

160 161
	void SwapIds(unsigned id1, unsigned id2) {
		playlist.SwapIds(pc, id1, id2);
162 163
	}

164 165 166 167
	void SetPriorityRange(unsigned start_position, unsigned end_position,
			      uint8_t priority) {
		playlist.SetPriorityRange(pc, start_position, end_position,
					  priority);
168 169
	}

170 171
	void SetPriorityId(unsigned song_id, uint8_t priority) {
		playlist.SetPriorityId(pc, song_id, priority);
172 173
	}

174
	void Stop() noexcept {
175 176 177
		playlist.Stop(pc);
	}

178 179
	void PlayPosition(int position) {
		return playlist.PlayPosition(pc, position);
180 181
	}

182 183
	void PlayId(int id) {
		return playlist.PlayId(pc, id);
184 185
	}

186 187
	void PlayNext() {
		return playlist.PlayNext(pc);
188 189
	}

190 191
	void PlayPrevious() {
		return playlist.PlayPrevious(pc);
192 193
	}

194 195
	void SeekSongPosition(unsigned song_position, SongTime seek_time) {
		playlist.SeekSongPosition(pc, song_position, seek_time);
196 197
	}

198 199
	void SeekSongId(unsigned song_id, SongTime seek_time) {
		playlist.SeekSongId(pc, song_id, seek_time);
200 201
	}

202 203
	void SeekCurrent(SignedSongTime seek_time, bool relative) {
		playlist.SeekCurrent(pc, seek_time, relative);
204 205
	}

206
	void SetRepeat(bool new_value) noexcept {
207 208 209
		playlist.SetRepeat(pc, new_value);
	}

210
	bool GetRandom() const noexcept {
211 212 213
		return playlist.GetRandom();
	}

214
	void SetRandom(bool new_value) noexcept {
215 216 217
		playlist.SetRandom(pc, new_value);
	}

218
	void SetSingle(SingleMode new_value) noexcept {
219 220 221
		playlist.SetSingle(pc, new_value);
	}

222
	void SetConsume(bool new_value) noexcept {
223 224
		playlist.SetConsume(new_value);
	}
225

226
	void SetReplayGainMode(ReplayGainMode mode) noexcept {
227 228 229 230
		replay_gain_mode = mode;
		UpdateEffectiveReplayGainMode();
	}

231 232
	/**
	 * Publishes the effective #ReplayGainMode to all subsystems.
233
	 * #ReplayGainMode::AUTO is substituted.
234
	 */
235
	void UpdateEffectiveReplayGainMode() noexcept;
236

237
#ifdef ENABLE_DATABASE
238 239 240 241 242
	/**
	 * Returns the global #Database instance.  May return nullptr
	 * if this MPD configuration has no database (no
	 * music_directory was configured).
	 */
243
	const Database *GetDatabase() const noexcept;
244

245 246
	const Database &GetDatabaseOrThrow() const;

247 248 249 250
	/**
	 * The database has been modified.  Propagate the change to
	 * all subsystems.
	 */
251
	void DatabaseModified(const Database &db) noexcept;
252
#endif
253

254
	/**
255 256
	 * A tag in the play queue has been modified by the player
	 * thread.  Propagate the change to all subsystems.
257
	 */
258
	void TagModified() noexcept;
259

260 261 262 263 264 265
	/**
	 * The tag of the given song has been modified.  Propagate the
	 * change to all instances of this song in the queue.
	 */
	void TagModified(const char *uri, const Tag &tag) noexcept;

266 267 268
	/**
	 * Synchronize the player with the play queue.
	 */
269
	void SyncWithPlayer() noexcept;
270

271 272 273 274
	/**
	 * Border pause has just been enabled. Change single mode to off
	 * if it was one-shot.
	 */
275
	void BorderPause() noexcept;
276

277
private:
278
	/* virtual methods from class QueueListener */
279 280 281
	void OnQueueModified() noexcept override;
	void OnQueueOptionsChanged() noexcept override;
	void OnQueueSongStarted() noexcept override;
282

283
	/* virtual methods from class PlayerListener */
284 285
	void OnPlayerSync() noexcept override;
	void OnPlayerTagModified() noexcept override;
286
	void OnBorderPause() noexcept override;
287

288
	/* virtual methods from class MixerListener */
289
	void OnMixerVolumeChanged(Mixer &mixer, int volume) noexcept override;
290

291 292 293
	/* callback for #idle_monitor */
	void OnIdleMonitor(unsigned mask) noexcept;

294
	/* callback for #global_events */
295
	void OnGlobalEvent(unsigned mask) noexcept;
296 297 298
};

#endif