PlayerCommands.cxx 9.95 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 14 15 16 17 18 19 20 21
 * 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.
 *
 * 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.
 */

#include "config.h"
#include "PlayerCommands.hxx"
22
#include "Request.hxx"
23
#include "CommandError.hxx"
24
#include "queue/Playlist.hxx"
25
#include "PlaylistPrint.hxx"
26
#include "client/Client.hxx"
27
#include "client/Response.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "mixer/Volume.hxx"
29
#include "Partition.hxx"
30
#include "Instance.hxx"
31
#include "AudioFormat.hxx"
32
#include "ReplayGainConfig.hxx"
33
#include "util/ConstBuffer.hxx"
34

35 36 37 38
#ifdef ENABLE_DATABASE
#include "db/update/Service.hxx"
#endif

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#define COMMAND_STATUS_STATE            "state"
#define COMMAND_STATUS_REPEAT           "repeat"
#define COMMAND_STATUS_SINGLE           "single"
#define COMMAND_STATUS_CONSUME          "consume"
#define COMMAND_STATUS_RANDOM           "random"
#define COMMAND_STATUS_PLAYLIST         "playlist"
#define COMMAND_STATUS_PLAYLIST_LENGTH  "playlistlength"
#define COMMAND_STATUS_SONG             "song"
#define COMMAND_STATUS_SONGID           "songid"
#define COMMAND_STATUS_NEXTSONG         "nextsong"
#define COMMAND_STATUS_NEXTSONGID       "nextsongid"
#define COMMAND_STATUS_TIME             "time"
#define COMMAND_STATUS_BITRATE          "bitrate"
#define COMMAND_STATUS_ERROR            "error"
#define COMMAND_STATUS_CROSSFADE	"xfade"
#define COMMAND_STATUS_MIXRAMPDB	"mixrampdb"
#define COMMAND_STATUS_MIXRAMPDELAY	"mixrampdelay"
#define COMMAND_STATUS_AUDIO		"audio"
#define COMMAND_STATUS_UPDATING_DB	"updating_db"

59
CommandResult
60
handle_play(Client &client, Request args, Response &r)
61 62
{
	int song = -1;
63
	if (!args.ParseOptional(0, song, r))
64
		return CommandResult::ERROR;
65

66
	PlaylistResult result = client.partition.PlayPosition(song);
67
	return print_playlist_result(r, result);
68 69
}

70
CommandResult
71
handle_playid(Client &client, Request args, Response &r)
72 73
{
	int id = -1;
74
	if (!args.ParseOptional(0, id, r))
75
		return CommandResult::ERROR;
76

77
	PlaylistResult result = client.partition.PlayId(id);
78
	return print_playlist_result(r, result);
79 80
}

81
CommandResult
82
handle_stop(Client &client, gcc_unused Request args, gcc_unused Response &r)
83
{
84
	client.partition.Stop();
85
	return CommandResult::OK;
86 87
}

88
CommandResult
89
handle_currentsong(Client &client, gcc_unused Request args, Response &r)
90
{
91
	playlist_print_current(r, client.partition, client.playlist);
92
	return CommandResult::OK;
93 94
}

95
CommandResult
96
handle_pause(Client &client, Request args, Response &r)
97
{
98
	if (!args.IsEmpty()) {
99
		bool pause_flag;
100
		if (!args.Parse(0, pause_flag, r))
101
			return CommandResult::ERROR;
102

103
		client.player_control.SetPause(pause_flag);
104
	} else
105
		client.player_control.Pause();
106

107
	return CommandResult::OK;
108 109
}

110
CommandResult
111
handle_status(Client &client, gcc_unused Request args, Response &r)
112
{
113
	const char *state = nullptr;
114 115
	int song;

116
	const auto player_status = client.player_control.GetStatus();
117 118

	switch (player_status.state) {
119
	case PlayerState::STOP:
120 121
		state = "stop";
		break;
122
	case PlayerState::PAUSE:
123 124
		state = "pause";
		break;
125
	case PlayerState::PLAY:
126 127 128 129
		state = "play";
		break;
	}

130
	const playlist &playlist = client.playlist;
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	r.Format("volume: %i\n"
		 COMMAND_STATUS_REPEAT ": %i\n"
		 COMMAND_STATUS_RANDOM ": %i\n"
		 COMMAND_STATUS_SINGLE ": %i\n"
		 COMMAND_STATUS_CONSUME ": %i\n"
		 COMMAND_STATUS_PLAYLIST ": %li\n"
		 COMMAND_STATUS_PLAYLIST_LENGTH ": %i\n"
		 COMMAND_STATUS_MIXRAMPDB ": %f\n"
		 COMMAND_STATUS_STATE ": %s\n",
		 volume_level_get(client.partition.outputs),
		 playlist.GetRepeat(),
		 playlist.GetRandom(),
		 playlist.GetSingle(),
		 playlist.GetConsume(),
		 (unsigned long)playlist.GetVersion(),
		 playlist.GetLength(),
		 client.player_control.GetMixRampDb(),
		 state);
149

150
	if (client.player_control.GetCrossFade() > 0)
151 152
		r.Format(COMMAND_STATUS_CROSSFADE ": %i\n",
			 int(client.player_control.GetCrossFade() + 0.5));
153 154

	if (client.player_control.GetMixRampDelay() > 0)
155 156
		r.Format(COMMAND_STATUS_MIXRAMPDELAY ": %f\n",
			 client.player_control.GetMixRampDelay());
157

158
	song = playlist.GetCurrentPosition();
159
	if (song >= 0) {
160 161 162
		r.Format(COMMAND_STATUS_SONG ": %i\n"
			 COMMAND_STATUS_SONGID ": %u\n",
			 song, playlist.PositionToId(song));
163 164
	}

165
	if (player_status.state != PlayerState::STOP) {
166 167 168 169 170 171 172 173 174
		r.Format(COMMAND_STATUS_TIME ": %i:%i\n"
			 "elapsed: %1.3f\n"
			 COMMAND_STATUS_BITRATE ": %u\n",
			 player_status.elapsed_time.RoundS(),
			 player_status.total_time.IsNegative()
			 ? 0u
			 : unsigned(player_status.total_time.RoundS()),
			 player_status.elapsed_time.ToDoubleS(),
			 player_status.bit_rate);
175

176
		if (!player_status.total_time.IsNegative())
177 178
			r.Format("duration: %1.3f\n",
				 player_status.total_time.ToDoubleS());
179

180
		if (player_status.audio_format.IsDefined()) {
181 182
			struct audio_format_string af_string;

183 184 185
			r.Format(COMMAND_STATUS_AUDIO ": %s\n",
				 audio_format_to_string(player_status.audio_format,
							&af_string));
186
		}
187 188
	}

189
#ifdef ENABLE_DATABASE
190 191 192 193 194
	const UpdateService *update_service = client.partition.instance.update;
	unsigned updateJobId = update_service != nullptr
		? update_service->GetId()
		: 0;
	if (updateJobId != 0) {
195 196
		r.Format(COMMAND_STATUS_UPDATING_DB ": %i\n",
			 updateJobId);
197
	}
198
#endif
199

200
	Error error = client.player_control.LockGetError();
201
	if (error.IsDefined())
202 203
		r.Format(COMMAND_STATUS_ERROR ": %s\n",
			 error.GetMessage());
204

205
	song = playlist.GetNextPosition();
206 207 208 209
	if (song >= 0)
		r.Format(COMMAND_STATUS_NEXTSONG ": %i\n"
			 COMMAND_STATUS_NEXTSONGID ": %u\n",
			 song, playlist.PositionToId(song));
210

211
	return CommandResult::OK;
212 213
}

214
CommandResult
215
handle_next(Client &client, gcc_unused Request args, gcc_unused Response &r)
216
{
217
	playlist &playlist = client.playlist;
218

219 220
	/* single mode is not considered when this is user who
	 * wants to change song. */
221 222
	const bool single = playlist.queue.single;
	playlist.queue.single = false;
223

224
	client.partition.PlayNext();
225

226
	playlist.queue.single = single;
227
	return CommandResult::OK;
228 229
}

230
CommandResult
231 232
handle_previous(Client &client, gcc_unused Request args,
		gcc_unused Response &r)
233
{
234
	client.partition.PlayPrevious();
235
	return CommandResult::OK;
236 237
}

238
CommandResult
239
handle_repeat(Client &client, Request args, Response &r)
240 241
{
	bool status;
242
	if (!args.Parse(0, status, r))
243
		return CommandResult::ERROR;
244

245
	client.partition.SetRepeat(status);
246
	return CommandResult::OK;
247 248
}

249
CommandResult
250
handle_single(Client &client, Request args, Response &r)
251 252
{
	bool status;
253
	if (!args.Parse(0, status, r))
254
		return CommandResult::ERROR;
255

256
	client.partition.SetSingle(status);
257
	return CommandResult::OK;
258 259
}

260
CommandResult
261
handle_consume(Client &client, Request args, Response &r)
262 263
{
	bool status;
264
	if (!args.Parse(0, status, r))
265
		return CommandResult::ERROR;
266

267
	client.partition.SetConsume(status);
268
	return CommandResult::OK;
269 270
}

271
CommandResult
272
handle_random(Client &client, Request args, Response &r)
273 274
{
	bool status;
275
	if (!args.Parse(0, status, r))
276
		return CommandResult::ERROR;
277

278
	client.partition.SetRandom(status);
279
	client.partition.outputs.SetReplayGainMode(replay_gain_get_real_mode(client.partition.GetRandom()));
280
	return CommandResult::OK;
281 282
}

283
CommandResult
284 285
handle_clearerror(Client &client, gcc_unused Request args,
		  gcc_unused Response &r)
286
{
287
	client.player_control.ClearError();
288
	return CommandResult::OK;
289 290
}

291
CommandResult
292
handle_seek(Client &client, Request args, Response &r)
293
{
294 295
	unsigned song;
	SongTime seek_time;
296
	if (!args.Parse(0, song, r) || !args.Parse(1, seek_time, r))
297
		return CommandResult::ERROR;
298

299
	PlaylistResult result =
300
		client.partition.SeekSongPosition(song, seek_time);
301
	return print_playlist_result(r, result);
302 303
}

304
CommandResult
305
handle_seekid(Client &client, Request args, Response &r)
306
{
307 308
	unsigned id;
	SongTime seek_time;
309
	if (!args.Parse(0, id, r))
310
		return CommandResult::ERROR;
311
	if (!args.Parse(1, seek_time, r))
312
		return CommandResult::ERROR;
313

314
	PlaylistResult result =
315
		client.partition.SeekSongId(id, seek_time);
316
	return print_playlist_result(r, result);
317 318
}

319
CommandResult
320
handle_seekcur(Client &client, Request args, Response &r)
321
{
322
	const char *p = args.front();
323
	bool relative = *p == '+' || *p == '-';
324
	SignedSongTime seek_time;
325
	if (!ParseCommandArg(r, seek_time, p))
326
		return CommandResult::ERROR;
327

328
	PlaylistResult result =
329
		client.partition.SeekCurrent(seek_time, relative);
330
	return print_playlist_result(r, result);
331 332
}

333
CommandResult
334
handle_crossfade(Client &client, Request args, Response &r)
335 336
{
	unsigned xfade_time;
337
	if (!args.Parse(0, xfade_time, r))
338
		return CommandResult::ERROR;
339

340
	client.player_control.SetCrossFade(xfade_time);
341
	return CommandResult::OK;
342 343
}

344
CommandResult
345
handle_mixrampdb(Client &client, Request args, Response &r)
346 347
{
	float db;
348
	if (!args.Parse(0, db, r))
349
		return CommandResult::ERROR;
350

351
	client.player_control.SetMixRampDb(db);
352
	return CommandResult::OK;
353 354
}

355
CommandResult
356
handle_mixrampdelay(Client &client, Request args, Response &r)
357 358
{
	float delay_secs;
359
	if (!args.Parse(0, delay_secs, r))
360
		return CommandResult::ERROR;
361

362
	client.player_control.SetMixRampDelay(delay_secs);
363

364
	return CommandResult::OK;
365 366
}

367
CommandResult
368
handle_replay_gain_mode(Client &client, Request args, Response &r)
369
{
370
	if (!replay_gain_set_mode_string(args.front())) {
371
		r.Error(ACK_ERROR_ARG, "Unrecognized replay gain mode");
372
		return CommandResult::ERROR;
373 374
	}

375
	client.partition.outputs.SetReplayGainMode(replay_gain_get_real_mode(client.playlist.queue.random));
376
	return CommandResult::OK;
377 378
}

379
CommandResult
380 381
handle_replay_gain_status(gcc_unused Client &client, gcc_unused Request args,
			  Response &r)
382
{
383
	r.Format("replay_gain_mode: %s\n", replay_gain_get_mode_string());
384
	return CommandResult::OK;
385
}