Partition.cxx 4.84 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "lib/fmt/ExceptionFormatter.hxx"
25
#include "song/DetachedSong.hxx"
26
#include "mixer/Volume.hxx"
27
#include "IdleFlags.hxx"
28
#include "client/Listener.hxx"
29
#include "client/Client.hxx"
30 31 32 33
#include "input/cache/Manager.hxx"
#include "util/Domain.hxx"

static constexpr Domain cache_domain("cache");
34

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

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

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

65 66 67 68 69 70
static void
PrefetchSong(InputCacheManager &cache, const char *uri) noexcept
{
	if (cache.Contains(uri))
		return;

71
	FmtDebug(cache_domain, "Prefetch '{}'", uri);
72 73 74 75

	try {
		cache.Prefetch(uri);
	} catch (...) {
76 77 78
		FmtError(cache_domain,
			 "Prefetch '{}' failed: {}",
			 uri, std::current_exception());
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	}
}

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
}

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

112
	pc.LockSetReplayGainMode(mode);
113

114 115 116
	outputs.SetReplayGainMode(mode);
}

117 118
#ifdef ENABLE_DATABASE

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

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

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

138 139
#endif

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

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

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

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

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

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

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

void
183
Partition::OnQueueSongStarted() noexcept
184 185 186 187
{
	EmitIdle(IDLE_PLAYER);
}

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

void
195
Partition::OnPlayerTagModified() noexcept
196
{
197
	EmitGlobalEvent(TAG_MODIFIED);
198 199
}

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

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

	/* notify clients */
212
	EmitIdle(IDLE_MIXER);
213
}
214

215 216 217 218 219 220 221 222 223 224 225 226
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();
}

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

	if ((mask & TAG_MODIFIED) != 0)
		TagModified();
235 236 237

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