Partition.cxx 4.41 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 29 30 31
#include "input/cache/Manager.hxx"
#include "util/Domain.hxx"

static constexpr Domain cache_domain("cache");
32

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

53 54
Partition::~Partition() noexcept = default;

55
void
56
Partition::EmitIdle(unsigned mask) noexcept
57
{
58
	instance.EmitIdle(mask);
59 60
}

61 62 63 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
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
}

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

107
	pc.LockSetReplayGainMode(mode);
108

109 110 111
	outputs.SetReplayGainMode(mode);
}

112 113
#ifdef ENABLE_DATABASE

114
const Database *
115
Partition::GetDatabase() const noexcept
116
{
117
	return instance.GetDatabase();
118 119
}

120 121 122 123 124 125
const Database &
Partition::GetDatabaseOrThrow() const
{
	return instance.GetDatabaseOrThrow();
}

126
void
127
Partition::DatabaseModified(const Database &db) noexcept
128
{
129
	playlist.DatabaseModified(db);
130
	EmitIdle(IDLE_DATABASE);
131 132
}

133 134
#endif

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

143 144 145 146 147 148
void
Partition::TagModified(const char *uri, const Tag &tag) noexcept
{
	playlist.TagModified(uri, tag);
}

149
void
150
Partition::SyncWithPlayer() noexcept
151 152
{
	playlist.SyncWithPlayer(pc);
153 154 155 156

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

159
void
160
Partition::BorderPause() noexcept
161 162 163 164
{
	playlist.BorderPause(pc);
}

165
void
166
Partition::OnQueueModified() noexcept
167 168 169 170 171
{
	EmitIdle(IDLE_PLAYLIST);
}

void
172
Partition::OnQueueOptionsChanged() noexcept
173 174 175 176 177
{
	EmitIdle(IDLE_OPTIONS);
}

void
178
Partition::OnQueueSongStarted() noexcept
179 180 181 182
{
	EmitIdle(IDLE_PLAYER);
}

183
void
184
Partition::OnPlayerSync() noexcept
185
{
186
	EmitGlobalEvent(SYNC_WITH_PLAYER);
187 188 189
}

void
190
Partition::OnPlayerTagModified() noexcept
191
{
192
	EmitGlobalEvent(TAG_MODIFIED);
193 194
}

195 196 197 198 199 200
void
Partition::OnBorderPause() noexcept
{
	EmitGlobalEvent(BORDER_PAUSE);
}

201
void
202
Partition::OnMixerVolumeChanged(Mixer &, int) noexcept
203 204 205 206
{
	InvalidateHardwareVolume();

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

void
Partition::OnGlobalEvent(unsigned mask)
{
213
	if ((mask & SYNC_WITH_PLAYER) != 0)
214
		SyncWithPlayer();
215 216 217

	if ((mask & TAG_MODIFIED) != 0)
		TagModified();
218 219 220

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