PlaylistState.cxx 7.68 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "config.h"
26
#include "PlaylistState.hxx"
27
#include "PlaylistError.hxx"
28
#include "Playlist.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "queue/QueueSave.hxx"
30
#include "fs/TextFile.hxx"
31
#include "PlayerControl.hxx"
32 33
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
34
#include "fs/Limits.hxx"
35
#include "util/CharUtil.hxx"
36
#include "util/StringUtil.hxx"
37
#include "Log.hxx"
38 39 40 41 42 43 44

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

#define PLAYLIST_STATE_FILE_STATE		"state: "
#define PLAYLIST_STATE_FILE_RANDOM		"random: "
#define PLAYLIST_STATE_FILE_REPEAT		"repeat: "
45
#define PLAYLIST_STATE_FILE_SINGLE		"single: "
46
#define PLAYLIST_STATE_FILE_CONSUME		"consume: "
47 48 49
#define PLAYLIST_STATE_FILE_CURRENT		"current: "
#define PLAYLIST_STATE_FILE_TIME		"time: "
#define PLAYLIST_STATE_FILE_CROSSFADE		"crossfade: "
50 51
#define PLAYLIST_STATE_FILE_MIXRAMPDB		"mixrampdb: "
#define PLAYLIST_STATE_FILE_MIXRAMPDELAY	"mixrampdelay: "
52 53 54 55 56 57 58 59 60 61
#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"

#define PLAYLIST_BUFFER_SIZE	2*MPD_PATH_MAX

void
62
playlist_state_save(FILE *fp, const struct playlist &playlist,
63
		    PlayerControl &pc)
64
{
65
	const auto player_status = pc.GetStatus();
66

67
	fputs(PLAYLIST_STATE_FILE_STATE, fp);
68

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

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

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

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

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

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

127
	playlist.queue.IncrementVersion();
128 129
}

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

139
	if (!StringStartsWith(line, PLAYLIST_STATE_FILE_STATE))
140 141 142 143
		return false;

	line += sizeof(PLAYLIST_STATE_FILE_STATE) - 1;

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

152
	while ((line = file.ReadLine()) != nullptr) {
153
		if (StringStartsWith(line, PLAYLIST_STATE_FILE_TIME)) {
154
			seek_time =
155
				atoi(&(line[strlen(PLAYLIST_STATE_FILE_TIME)]));
156
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_REPEAT)) {
157 158 159
			playlist.SetRepeat(pc,
					   strcmp(&(line[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
						  "1") == 0);
160
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_SINGLE)) {
161 162 163
			playlist.SetSingle(pc,
					   strcmp(&(line[strlen(PLAYLIST_STATE_FILE_SINGLE)]),
						  "1") == 0);
164
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CONSUME)) {
165 166
			playlist.SetConsume(strcmp(&(line[strlen(PLAYLIST_STATE_FILE_CONSUME)]),
						   "1") == 0);
167
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CROSSFADE)) {
168
			pc.SetCrossFade(atoi(line + strlen(PLAYLIST_STATE_FILE_CROSSFADE)));
169
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_MIXRAMPDB)) {
170
			pc.SetMixRampDb(atof(line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDB)));
171
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) {
172 173 174 175 176 177
			const char *p = line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDELAY);

			/* this check discards "nan" which was used
			   prior to MPD 0.18 */
			if (IsDigitASCII(*p))
				pc.SetMixRampDelay(atof(p));
178
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_RANDOM)) {
179
			random_mode =
180
				strcmp(line + strlen(PLAYLIST_STATE_FILE_RANDOM),
181
				       "1") == 0;
182
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CURRENT)) {
183
			current = atoi(&(line
184 185
					 [strlen
					  (PLAYLIST_STATE_FILE_CURRENT)]));
186
		} else if (StringStartsWith(line,
187
					    PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
188
			playlist_state_load(file, song_loader, playlist);
189 190 191
		}
	}

192
	playlist.SetRandom(pc, random_mode);
193

194 195
	if (!playlist.queue.IsEmpty()) {
		if (!playlist.queue.IsValidPosition(current))
196 197
			current = 0;

198
		if (state == PlayerState::PLAY &&
199
		    config_get_bool(CONF_RESTORE_PAUSED, false))
200 201 202
			/* the user doesn't want MPD to auto-start
			   playback after startup; fall back to
			   "pause" */
203
			state = PlayerState::PAUSE;
204

205 206 207
		/* enable all devices for the first time; this must be
		   called here, after the audio output states were
		   restored, before playback begins */
208
		if (state != PlayerState::STOP)
209
			pc.UpdateAudio();
210

211
		if (state == PlayerState::STOP /* && config_option */)
212
			playlist.current = current;
213
		else if (seek_time == 0)
214
			playlist.PlayPosition(pc, current);
215
		else
216
			playlist.SeekSongPosition(pc, current, seek_time);
217

218
		if (state == PlayerState::PAUSE)
219
			pc.Pause();
220
	}
221 222

	return true;
223
}
224 225

unsigned
226
playlist_state_get_hash(const playlist &playlist,
227
			PlayerControl &pc)
228
{
229
	const auto player_status = pc.GetStatus();
230

231
	return playlist.queue.version ^
232
		(player_status.state != PlayerState::STOP
233 234
		 ? ((int)player_status.elapsed_time << 8)
		 : 0) ^
235 236
		(playlist.current >= 0
		 ? (playlist.queue.OrderToPosition(playlist.current) << 16)
237
		 : 0) ^
238
		((int)pc.GetCrossFade() << 20) ^
239
		(unsigned(player_status.state) << 24) ^
240 241 242 243 244
		(playlist.queue.random << 27) ^
		(playlist.queue.repeat << 28) ^
		(playlist.queue.single << 29) ^
		(playlist.queue.consume << 30) ^
		(playlist.queue.random << 31);
245
}