PlaylistState.cxx 7.61 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
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.
18 19 20 21 22 23 24
 */

/*
 * Saving and loading the playlist to/from the state file.
 *
 */

25
#include "PlaylistState.hxx"
26
#include "PlaylistError.hxx"
27
#include "Playlist.hxx"
28
#include "SingleMode.hxx"
29
#include "StateFileConfig.hxx"
Max Kellermann's avatar
Max Kellermann committed
30
#include "queue/QueueSave.hxx"
31 32
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
33
#include "player/Control.hxx"
34
#include "util/CharUtil.hxx"
35
#include "util/StringAPI.hxx"
36
#include "util/StringCompare.hxx"
37
#include "util/NumberParser.hxx"
38
#include "Log.hxx"
39 40 41 42 43 44 45

#include <string.h>
#include <stdlib.h>

#define PLAYLIST_STATE_FILE_STATE		"state: "
#define PLAYLIST_STATE_FILE_RANDOM		"random: "
#define PLAYLIST_STATE_FILE_REPEAT		"repeat: "
46
#define PLAYLIST_STATE_FILE_SINGLE		"single: "
47
#define PLAYLIST_STATE_FILE_CONSUME		"consume: "
48 49 50
#define PLAYLIST_STATE_FILE_CURRENT		"current: "
#define PLAYLIST_STATE_FILE_TIME		"time: "
#define PLAYLIST_STATE_FILE_CROSSFADE		"crossfade: "
51 52
#define PLAYLIST_STATE_FILE_MIXRAMPDB		"mixrampdb: "
#define PLAYLIST_STATE_FILE_MIXRAMPDELAY	"mixrampdelay: "
53 54 55 56 57 58 59 60
#define PLAYLIST_STATE_FILE_PLAYLIST_BEGIN	"playlist_begin"
#define PLAYLIST_STATE_FILE_PLAYLIST_END	"playlist_end"

#define PLAYLIST_STATE_FILE_STATE_PLAY		"play"
#define PLAYLIST_STATE_FILE_STATE_PAUSE		"pause"
#define PLAYLIST_STATE_FILE_STATE_STOP		"stop"

void
61
playlist_state_save(BufferedOutputStream &os, const struct playlist &playlist,
62
		    PlayerControl &pc)
63
{
64
	const auto player_status = pc.LockGetStatus();
65

66
	os.Write(PLAYLIST_STATE_FILE_STATE);
67

68
	if (playlist.playing) {
69
		switch (player_status.state) {
70
		case PlayerState::PAUSE:
71
			os.Write(PLAYLIST_STATE_FILE_STATE_PAUSE "\n");
72 73
			break;
		default:
74
			os.Write(PLAYLIST_STATE_FILE_STATE_PLAY "\n");
75
		}
76 77
		os.Format(PLAYLIST_STATE_FILE_CURRENT "%i\n",
			  playlist.queue.OrderToPosition(playlist.current));
78 79
		os.Format(PLAYLIST_STATE_FILE_TIME "%f\n",
			  player_status.elapsed_time.ToDoubleS());
80
	} else {
81
		os.Write(PLAYLIST_STATE_FILE_STATE_STOP "\n");
82

83
		if (playlist.current >= 0)
84
			os.Format(PLAYLIST_STATE_FILE_CURRENT "%i\n",
85
				playlist.queue.OrderToPosition(playlist.current));
86
	}
87

88 89
	os.Format(PLAYLIST_STATE_FILE_RANDOM "%i\n", playlist.queue.random);
	os.Format(PLAYLIST_STATE_FILE_REPEAT "%i\n", playlist.queue.repeat);
90 91
	os.Format(PLAYLIST_STATE_FILE_SINGLE "%i\n",
			  (int)playlist.queue.single);
92 93
	os.Format(PLAYLIST_STATE_FILE_CONSUME "%i\n", playlist.queue.consume);
	os.Format(PLAYLIST_STATE_FILE_CROSSFADE "%i\n",
94
		  (int)pc.GetCrossFade().count());
95 96
	os.Format(PLAYLIST_STATE_FILE_MIXRAMPDB "%f\n", pc.GetMixRampDb());
	os.Format(PLAYLIST_STATE_FILE_MIXRAMPDELAY "%f\n",
97
		  pc.GetMixRampDelay().count());
98 99 100
	os.Write(PLAYLIST_STATE_FILE_PLAYLIST_BEGIN "\n");
	queue_save(os, playlist.queue);
	os.Write(PLAYLIST_STATE_FILE_PLAYLIST_END "\n");
101 102 103
}

static void
104 105
playlist_state_load(TextFile &file, const SongLoader &song_loader,
		    struct playlist &playlist)
106
{
107
	const char *line = file.ReadLine();
108
	if (line == nullptr) {
109
		LogWarning(playlist_domain, "No playlist in state file");
110 111 112
		return;
	}

113
	while (!StringStartsWith(line, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
114
		queue_load_song(file, song_loader, line, playlist.queue);
115

116
		line = file.ReadLine();
117
		if (line == nullptr) {
118 119 120
			LogWarning(playlist_domain,
				   "'" PLAYLIST_STATE_FILE_PLAYLIST_END
				   "' not found in state file");
121 122 123
			break;
		}
	}
124

125
	playlist.queue.IncrementVersion();
126 127
}

128
bool
129 130
playlist_state_restore(const StateFileConfig &config,
		       const char *line, TextFile &file,
131
		       const SongLoader &song_loader,
132
		       struct playlist &playlist, PlayerControl &pc)
133 134
{
	int current = -1;
135
	SongTime seek_time = SongTime::zero();
136 137
	bool random_mode = false;

138 139
	line = StringAfterPrefix(line, PLAYLIST_STATE_FILE_STATE);
	if (line == nullptr)
140 141
		return false;

142
	PlayerState state;
143
	if (strcmp(line, PLAYLIST_STATE_FILE_STATE_PLAY) == 0)
144
		state = PlayerState::PLAY;
145
	else if (strcmp(line, PLAYLIST_STATE_FILE_STATE_PAUSE) == 0)
146 147 148
		state = PlayerState::PAUSE;
	else
		state = PlayerState::STOP;
149

150
	while ((line = file.ReadLine()) != nullptr) {
151 152
		const char *p;
		if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_TIME))) {
153
			seek_time = SongTime::FromS(ParseDouble(p));
154 155 156
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_REPEAT))) {
			playlist.SetRepeat(pc, StringIsEqual(p, "1"));
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_SINGLE))) {
157
			playlist.SetSingle(pc, SingleFromString(p));
158 159 160
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_CONSUME))) {
			playlist.SetConsume(StringIsEqual(p, "1"));
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_CROSSFADE))) {
161
			pc.SetCrossFade(FloatDuration(atoi(p)));
162
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_MIXRAMPDB))) {
163
			pc.SetMixRampDb(ParseFloat(p));
164
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY))) {
165 166 167
			/* this check discards "nan" which was used
			   prior to MPD 0.18 */
			if (IsDigitASCII(*p))
168
				pc.SetMixRampDelay(FloatDuration(ParseFloat(p)));
169 170 171 172
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_RANDOM))) {
			random_mode = StringIsEqual(p, "1");
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_CURRENT))) {
			current = atoi(p);
173
		} else if (StringStartsWith(line,
174
					    PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
175
			playlist_state_load(file, song_loader, playlist);
176 177 178
		}
	}

179
	playlist.SetRandom(pc, random_mode);
180

181 182
	if (!playlist.queue.IsEmpty()) {
		if (!playlist.queue.IsValidPosition(current))
183 184
			current = 0;

185
		if (state == PlayerState::PLAY && config.restore_paused)
186 187 188
			/* the user doesn't want MPD to auto-start
			   playback after startup; fall back to
			   "pause" */
189
			state = PlayerState::PAUSE;
190

191 192 193
		/* enable all devices for the first time; this must be
		   called here, after the audio output states were
		   restored, before playback begins */
194
		if (state != PlayerState::STOP)
195
			pc.LockUpdateAudio();
196

197
		if (state == PlayerState::STOP /* && config_option */)
198
			playlist.current = current;
199 200 201 202 203 204 205 206 207 208 209 210 211 212
		else if (seek_time.count() == 0) {
			try {
				playlist.PlayPosition(pc, current);
			} catch (...) {
				/* TODO: log error? */
			}
		} else {
			try {
				playlist.SeekSongPosition(pc, current,
							  seek_time);
			} catch (...) {
				/* TODO: log error? */
			}
		}
213

214
		if (state == PlayerState::PAUSE)
215
			pc.LockPause();
216
	}
217 218

	return true;
219
}
220 221

unsigned
222
playlist_state_get_hash(const playlist &playlist,
223
			PlayerControl &pc)
224
{
225
	const auto player_status = pc.LockGetStatus();
226

227
	return playlist.queue.version ^
228
		(player_status.state != PlayerState::STOP
229
		 ? (player_status.elapsed_time.ToS() << 8)
230
		 : 0) ^
231 232
		(playlist.current >= 0
		 ? (playlist.queue.OrderToPosition(playlist.current) << 16)
233
		 : 0) ^
234
		((int)pc.GetCrossFade().count() << 20) ^
235
		(unsigned(player_status.state) << 24) ^
236 237
		/* note that this takes 2 bits */
		((int)playlist.queue.single << 25) ^
238 239 240 241
		(playlist.queue.random << 27) ^
		(playlist.queue.repeat << 28) ^
		(playlist.queue.consume << 30) ^
		(playlist.queue.random << 31);
242
}