PlaylistState.cxx 7.72 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 31
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
32
#include "PlayerControl.hxx"
33 34
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
35
#include "fs/Limits.hxx"
36
#include "util/CharUtil.hxx"
37
#include "util/StringUtil.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 61 62
#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
63
playlist_state_save(BufferedOutputStream &os, const struct playlist &playlist,
64
		    PlayerControl &pc)
65
{
66
	const auto player_status = pc.GetStatus();
67

68
	os.Write(PLAYLIST_STATE_FILE_STATE);
69

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

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

90 91 92 93 94 95 96 97 98 99 100 101
	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");
102 103 104
}

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

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

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

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

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

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

	line += sizeof(PLAYLIST_STATE_FILE_STATE) - 1;

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

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

191
	playlist.SetRandom(pc, random_mode);
192

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

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

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

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

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

	return true;
222
}
223 224

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

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