PlaylistState.cxx 7.49 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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/StringAPI.hxx"
37
#include "util/StringCompare.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 90 91 92 93 94 95 96 97 98 99
	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");
100 101 102
}

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

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

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

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

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

136 137
	line = StringAfterPrefix(line, PLAYLIST_STATE_FILE_STATE);
	if (line == nullptr)
138 139
		return false;

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 150 151 152 153 154 155 156 157 158 159 160 161 162
		const char *p;
		if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_TIME))) {
			seek_time = SongTime::FromS(atof(p));
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_REPEAT))) {
			playlist.SetRepeat(pc, StringIsEqual(p, "1"));
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_SINGLE))) {
			playlist.SetSingle(pc, StringIsEqual(p, "1"));
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_CONSUME))) {
			playlist.SetConsume(StringIsEqual(p, "1"));
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_CROSSFADE))) {
			pc.SetCrossFade(atoi(p));
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_MIXRAMPDB))) {
			pc.SetMixRampDb(atof(p));
		} else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY))) {
163 164 165 166
			/* this check discards "nan" which was used
			   prior to MPD 0.18 */
			if (IsDigitASCII(*p))
				pc.SetMixRampDelay(atof(p));
167 168 169 170
		} 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);
171
		} else if (StringStartsWith(line,
172
					    PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
173
			playlist_state_load(file, song_loader, playlist);
174 175 176
		}
	}

177
	playlist.SetRandom(pc, random_mode);
178

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

183
		if (state == PlayerState::PLAY &&
184
		    config_get_bool(ConfigOption::RESTORE_PAUSED, false))
185 186 187
			/* the user doesn't want MPD to auto-start
			   playback after startup; fall back to
			   "pause" */
188
			state = PlayerState::PAUSE;
189

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

196
		if (state == PlayerState::STOP /* && config_option */)
197
			playlist.current = current;
198 199 200 201 202 203 204 205 206 207 208 209 210 211
		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? */
			}
		}
212

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

	return true;
218
}
219 220

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

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