Partition.cxx 4.78 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
 * 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.
 */

#include "config.h"
#include "Partition.hxx"
22
#include "Instance.hxx"
23
#include "Log.hxx"
24
#include "song/DetachedSong.hxx"
25
#include "mixer/Volume.hxx"
26
#include "IdleFlags.hxx"
27
#include "client/Listener.hxx"
28
#include "client/Client.hxx"
29 30 31 32
#include "input/cache/Manager.hxx"
#include "util/Domain.hxx"

static constexpr Domain cache_domain("cache");
33

34
Partition::Partition(Instance &_instance,
35
		     const char *_name,
36 37
		     unsigned max_length,
		     unsigned buffer_chunks,
38
		     AudioFormat configured_audio_format,
39
		     const ReplayGainConfig &replay_gain_config) noexcept
40
	:instance(_instance),
41
	 name(_name),
42
	 listener(new ClientListener(instance.event_loop, *this)),
43
	 idle_monitor(instance.event_loop, BIND_THIS_METHOD(OnIdleMonitor)),
44
	 global_events(instance.event_loop, BIND_THIS_METHOD(OnGlobalEvent)),
45
	 playlist(max_length, *this),
46
	 outputs(*this),
47 48 49
	 pc(*this, outputs,
	    instance.input_cache.get(),
	    buffer_chunks,
50
	    configured_audio_format, replay_gain_config)
51
{
52
	UpdateEffectiveReplayGainMode();
53 54
}

55 56
Partition::~Partition() noexcept = default;

57 58 59 60 61 62 63
void
Partition::BeginShutdown() noexcept
{
	pc.Kill();
	listener.reset();
}

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static void
PrefetchSong(InputCacheManager &cache, const char *uri) noexcept
{
	if (cache.Contains(uri))
		return;

	FormatDebug(cache_domain, "Prefetch '%s'", uri);

	try {
		cache.Prefetch(uri);
	} catch (...) {
		FormatError(std::current_exception(),
			    "Prefetch '%s' failed", uri);
	}
}

static void
PrefetchSong(InputCacheManager &cache, const DetachedSong &song) noexcept
{
	PrefetchSong(cache, song.GetURI());
}

inline void
Partition::PrefetchQueue() noexcept
{
	if (!instance.input_cache)
		return;

	auto &cache = *instance.input_cache;

	int next = playlist.GetNextPosition();
	if (next >= 0)
		PrefetchSong(cache, playlist.queue.Get(next));

	// TODO: prefetch more songs
}

101
void
102
Partition::UpdateEffectiveReplayGainMode() noexcept
103
{
104
	auto mode = replay_gain_mode;
105
	if (mode == ReplayGainMode::AUTO)
106
	    mode = playlist.queue.random
107 108
		    ? ReplayGainMode::TRACK
		    : ReplayGainMode::ALBUM;
109

110
	pc.LockSetReplayGainMode(mode);
111

112 113 114
	outputs.SetReplayGainMode(mode);
}

115 116
#ifdef ENABLE_DATABASE

117
const Database *
118
Partition::GetDatabase() const noexcept
119
{
120
	return instance.GetDatabase();
121 122
}

123 124 125 126 127 128
const Database &
Partition::GetDatabaseOrThrow() const
{
	return instance.GetDatabaseOrThrow();
}

129
void
130
Partition::DatabaseModified(const Database &db) noexcept
131
{
132
	playlist.DatabaseModified(db);
133
	EmitIdle(IDLE_DATABASE);
134 135
}

136 137
#endif

138
void
139
Partition::TagModified() noexcept
140
{
141 142
	auto song = pc.LockReadTaggedSong();
	if (song)
143
		playlist.TagModified(std::move(*song));
144 145
}

146 147 148 149 150 151
void
Partition::TagModified(const char *uri, const Tag &tag) noexcept
{
	playlist.TagModified(uri, tag);
}

152
void
153
Partition::SyncWithPlayer() noexcept
154 155
{
	playlist.SyncWithPlayer(pc);
156 157 158 159

	/* TODO: invoke this function in batches, to let the hard disk
	   spin down in between */
	PrefetchQueue();
160
}
161

162
void
163
Partition::BorderPause() noexcept
164 165 166 167
{
	playlist.BorderPause(pc);
}

168
void
169
Partition::OnQueueModified() noexcept
170 171 172 173 174
{
	EmitIdle(IDLE_PLAYLIST);
}

void
175
Partition::OnQueueOptionsChanged() noexcept
176 177 178 179 180
{
	EmitIdle(IDLE_OPTIONS);
}

void
181
Partition::OnQueueSongStarted() noexcept
182 183 184 185
{
	EmitIdle(IDLE_PLAYER);
}

186
void
187
Partition::OnPlayerSync() noexcept
188
{
189
	EmitGlobalEvent(SYNC_WITH_PLAYER);
190 191 192
}

void
193
Partition::OnPlayerTagModified() noexcept
194
{
195
	EmitGlobalEvent(TAG_MODIFIED);
196 197
}

198 199 200 201 202 203
void
Partition::OnBorderPause() noexcept
{
	EmitGlobalEvent(BORDER_PAUSE);
}

204
void
205
Partition::OnMixerVolumeChanged(Mixer &, int) noexcept
206 207 208 209
{
	InvalidateHardwareVolume();

	/* notify clients */
210
	EmitIdle(IDLE_MIXER);
211
}
212

213 214 215 216 217 218 219 220 221 222 223 224
void
Partition::OnIdleMonitor(unsigned mask) noexcept
{
	/* send "idle" notifications to all subscribed
	   clients */
	for (auto &client : clients)
		client.IdleAdd(mask);

	if (mask & (IDLE_PLAYLIST|IDLE_PLAYER|IDLE_MIXER|IDLE_OUTPUT))
		instance.OnStateModified();
}

225
void
226
Partition::OnGlobalEvent(unsigned mask) noexcept
227
{
228
	if ((mask & SYNC_WITH_PLAYER) != 0)
229
		SyncWithPlayer();
230 231 232

	if ((mask & TAG_MODIFIED) != 0)
		TagModified();
233 234 235

	if ((mask & BORDER_PAUSE) != 0)
		BorderPause();
236
}