PlaylistState.cxx 7.74 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 31
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
32
#include "player/Control.hxx"
33 34
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.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
#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
60
playlist_state_save(BufferedOutputStream &os, const struct playlist &playlist,
61
		    PlayerControl &pc)
62
{
63
	const auto player_status = pc.GetStatus();
64

65
	os.Write(PLAYLIST_STATE_FILE_STATE);
66

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

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

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

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

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

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

123
	playlist.queue.IncrementVersion();
124 125
}

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

135
	if (!StringStartsWith(line, PLAYLIST_STATE_FILE_STATE))
136 137 138 139
		return false;

	line += sizeof(PLAYLIST_STATE_FILE_STATE) - 1;

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

148
	while ((line = file.ReadLine()) != nullptr) {
149
		if (StringStartsWith(line, PLAYLIST_STATE_FILE_TIME)) {
150
			double seconds = atof(line + strlen(PLAYLIST_STATE_FILE_TIME));
151
			seek_time = SongTime::FromS(seconds);
152
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_REPEAT)) {
153 154 155
			playlist.SetRepeat(pc,
					   strcmp(&(line[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
						  "1") == 0);
156
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_SINGLE)) {
157 158 159
			playlist.SetSingle(pc,
					   strcmp(&(line[strlen(PLAYLIST_STATE_FILE_SINGLE)]),
						  "1") == 0);
160
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CONSUME)) {
161 162
			playlist.SetConsume(strcmp(&(line[strlen(PLAYLIST_STATE_FILE_CONSUME)]),
						   "1") == 0);
163
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CROSSFADE)) {
164
			pc.SetCrossFade(atoi(line + strlen(PLAYLIST_STATE_FILE_CROSSFADE)));
165
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_MIXRAMPDB)) {
166
			pc.SetMixRampDb(atof(line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDB)));
167
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) {
168 169 170 171 172 173
			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));
174
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_RANDOM)) {
175
			random_mode =
176
				strcmp(line + strlen(PLAYLIST_STATE_FILE_RANDOM),
177
				       "1") == 0;
178
		} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CURRENT)) {
179
			current = atoi(&(line
180 181
					 [strlen
					  (PLAYLIST_STATE_FILE_CURRENT)]));
182
		} else if (StringStartsWith(line,
183
					    PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
184
			playlist_state_load(file, song_loader, playlist);
185 186 187
		}
	}

188
	playlist.SetRandom(pc, random_mode);
189

190 191
	if (!playlist.queue.IsEmpty()) {
		if (!playlist.queue.IsValidPosition(current))
192 193
			current = 0;

194
		if (state == PlayerState::PLAY &&
195
		    config_get_bool(ConfigOption::RESTORE_PAUSED, false))
196 197 198
			/* the user doesn't want MPD to auto-start
			   playback after startup; fall back to
			   "pause" */
199
			state = PlayerState::PAUSE;
200

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

207
		if (state == PlayerState::STOP /* && config_option */)
208
			playlist.current = current;
209
		else if (seek_time.count() == 0)
210
			playlist.PlayPosition(pc, current);
211
		else
212
			playlist.SeekSongPosition(pc, current, seek_time);
213

214
		if (state == PlayerState::PAUSE)
215
			pc.Pause();
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.GetStatus();
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() << 20) ^
235
		(unsigned(player_status.state) << 24) ^
236 237 238 239 240
		(playlist.queue.random << 27) ^
		(playlist.queue.repeat << 28) ^
		(playlist.queue.single << 29) ^
		(playlist.queue.consume << 30) ^
		(playlist.queue.random << 31);
241
}