PlayerCommands.cxx 9.6 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "Idle.hxx"
32
#include "AudioFormat.hxx"
33
#include "ReplayGainConfig.hxx"
34
#include "util/ScopeExit.hxx"
35
#include "util/Error.hxx"
36

37 38 39 40
#ifdef ENABLE_DATABASE
#include "db/update/Service.hxx"
#endif

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#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"

61
CommandResult
62
handle_play(Client &client, Request args, gcc_unused Response &r)
63
{
64
	int song = args.ParseOptional(0, -1);
65

66 67
	client.partition.PlayPosition(song);
	return CommandResult::OK;
68 69
}

70
CommandResult
71
handle_playid(Client &client, Request args, gcc_unused Response &r)
72
{
73
	int id = args.ParseOptional(0, -1);
74

75 76
	client.partition.PlayId(id);
	return CommandResult::OK;
77 78
}

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

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

93
CommandResult
94
handle_pause(Client &client, Request args, gcc_unused Response &r)
95
{
96
	if (!args.IsEmpty()) {
97
		bool pause_flag = args.ParseBool(0);
98
		client.player_control.LockSetPause(pause_flag);
99
	} else
100
		client.player_control.LockPause();
101

102
	return CommandResult::OK;
103 104
}

105
CommandResult
106
handle_status(Client &client, gcc_unused Request args, Response &r)
107
{
108
	const char *state = nullptr;
109 110
	int song;

111
	const auto player_status = client.player_control.LockGetStatus();
112 113

	switch (player_status.state) {
114
	case PlayerState::STOP:
115 116
		state = "stop";
		break;
117
	case PlayerState::PAUSE:
118 119
		state = "pause";
		break;
120
	case PlayerState::PLAY:
121 122 123 124
		state = "play";
		break;
	}

125
	const playlist &playlist = client.playlist;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	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);
144

145
	if (client.player_control.GetCrossFade() > 0)
146 147
		r.Format(COMMAND_STATUS_CROSSFADE ": %i\n",
			 int(client.player_control.GetCrossFade() + 0.5));
148 149

	if (client.player_control.GetMixRampDelay() > 0)
150 151
		r.Format(COMMAND_STATUS_MIXRAMPDELAY ": %f\n",
			 client.player_control.GetMixRampDelay());
152

153
	song = playlist.GetCurrentPosition();
154
	if (song >= 0) {
155 156 157
		r.Format(COMMAND_STATUS_SONG ": %i\n"
			 COMMAND_STATUS_SONGID ": %u\n",
			 song, playlist.PositionToId(song));
158 159
	}

160
	if (player_status.state != PlayerState::STOP) {
161 162 163 164 165 166 167 168 169
		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);
170

171
		if (!player_status.total_time.IsNegative())
172 173
			r.Format("duration: %1.3f\n",
				 player_status.total_time.ToDoubleS());
174

175
		if (player_status.audio_format.IsDefined()) {
176 177
			struct audio_format_string af_string;

178 179 180
			r.Format(COMMAND_STATUS_AUDIO ": %s\n",
				 audio_format_to_string(player_status.audio_format,
							&af_string));
181
		}
182 183
	}

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

195 196 197 198 199 200 201 202 203
	try {
		client.player_control.LockCheckRethrowError();
	} catch (const std::exception &e) {
		r.Format(COMMAND_STATUS_ERROR ": %s\n", e.what());
	} catch (const Error &error) {
		r.Format(COMMAND_STATUS_ERROR ": %s\n", error.GetMessage());
	} catch (...) {
		r.Format(COMMAND_STATUS_ERROR ": unknown\n");
	}
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 225 226
	AtScopeExit(&playlist, single) {
		playlist.queue.single = single;
	};
227

228 229
	client.partition.PlayNext();
	return CommandResult::OK;
230 231
}

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

240
CommandResult
241
handle_repeat(Client &client, Request args, gcc_unused Response &r)
242
{
243
	bool status = args.ParseBool(0);
244
	client.partition.SetRepeat(status);
245
	return CommandResult::OK;
246 247
}

248
CommandResult
249
handle_single(Client &client, Request args, gcc_unused Response &r)
250
{
251
	bool status = args.ParseBool(0);
252
	client.partition.SetSingle(status);
253
	return CommandResult::OK;
254 255
}

256
CommandResult
257
handle_consume(Client &client, Request args, gcc_unused Response &r)
258
{
259
	bool status = args.ParseBool(0);
260
	client.partition.SetConsume(status);
261
	return CommandResult::OK;
262 263
}

264
CommandResult
265
handle_random(Client &client, Request args, gcc_unused Response &r)
266
{
267
	bool status = args.ParseBool(0);
268
	client.partition.SetRandom(status);
269
	client.partition.outputs.SetReplayGainMode(replay_gain_get_real_mode(client.partition.GetRandom()));
270
	return CommandResult::OK;
271 272
}

273
CommandResult
274 275
handle_clearerror(Client &client, gcc_unused Request args,
		  gcc_unused Response &r)
276
{
277
	client.player_control.LockClearError();
278
	return CommandResult::OK;
279 280
}

281
CommandResult
282
handle_seek(Client &client, Request args, gcc_unused Response &r)
283
{
284 285
	unsigned song = args.ParseUnsigned(0);
	SongTime seek_time = args.ParseSongTime(1);
286

287 288
	client.partition.SeekSongPosition(song, seek_time);
	return CommandResult::OK;
289 290
}

291
CommandResult
292
handle_seekid(Client &client, Request args, gcc_unused Response &r)
293
{
294 295
	unsigned id = args.ParseUnsigned(0);
	SongTime seek_time = args.ParseSongTime(1);
296

297 298
	client.partition.SeekSongId(id, seek_time);
	return CommandResult::OK;
299 300
}

301
CommandResult
302
handle_seekcur(Client &client, Request args, gcc_unused Response &r)
303
{
304
	const char *p = args.front();
305
	bool relative = *p == '+' || *p == '-';
306
	SignedSongTime seek_time = ParseCommandArgSignedSongTime(p);
307

308 309
	client.partition.SeekCurrent(seek_time, relative);
	return CommandResult::OK;
310 311
}

312
CommandResult
313
handle_crossfade(Client &client, Request args, gcc_unused Response &r)
314
{
315
	unsigned xfade_time = args.ParseUnsigned(0);
316
	client.player_control.SetCrossFade(xfade_time);
317
	return CommandResult::OK;
318 319
}

320
CommandResult
321
handle_mixrampdb(Client &client, Request args, gcc_unused Response &r)
322
{
323
	float db = args.ParseFloat(0);
324
	client.player_control.SetMixRampDb(db);
325
	return CommandResult::OK;
326 327
}

328
CommandResult
329
handle_mixrampdelay(Client &client, Request args, gcc_unused Response &r)
330
{
331
	float delay_secs = args.ParseFloat(0);
332
	client.player_control.SetMixRampDelay(delay_secs);
333
	return CommandResult::OK;
334 335
}

336
CommandResult
337
handle_replay_gain_mode(Client &client, Request args, Response &r)
338
{
339
	if (!replay_gain_set_mode_string(args.front())) {
340
		r.Error(ACK_ERROR_ARG, "Unrecognized replay gain mode");
341
		return CommandResult::ERROR;
342 343
	}

344
	client.partition.outputs.SetReplayGainMode(replay_gain_get_real_mode(client.playlist.queue.random));
345
	client.partition.EmitIdle(IDLE_OPTIONS);
346
	return CommandResult::OK;
347 348
}

349
CommandResult
350 351
handle_replay_gain_status(gcc_unused Client &client, gcc_unused Request args,
			  Response &r)
352
{
353
	r.Format("replay_gain_mode: %s\n", replay_gain_get_mode_string());
354
	return CommandResult::OK;
355
}