PlayerCommands.cxx 9.67 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

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, gcc_unused Response &r)
61
{
62
	int song = args.ParseOptional(0, -1);
63 64 65 66 67

	Error error;
	return client.partition.PlayPosition(song, error)
		? CommandResult::OK
		: print_error(r, error);
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 77 78

	Error error;
	return client.partition.PlayId(id, error)
		? CommandResult::OK
		: print_error(r, error);
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, gcc_unused Response &r)
97
{
98
	if (!args.IsEmpty()) {
99
		bool pause_flag = args.ParseBool(0);
100
		client.player_control.LockSetPause(pause_flag);
101
	} else
102
		client.player_control.LockPause();
103

104
	return CommandResult::OK;
105 106
}

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

113
	const auto player_status = client.player_control.LockGetStatus();
114 115

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

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

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

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

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

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

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

177
		if (player_status.audio_format.IsDefined()) {
178 179
			struct audio_format_string af_string;

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

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

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

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

208
	return CommandResult::OK;
209 210
}

211
CommandResult
212
handle_next(Client &client, gcc_unused Request args, gcc_unused Response &r)
213
{
214
	playlist &playlist = client.playlist;
215

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

221 222
	Error error;
	bool success = client.partition.PlayNext(error);
223

224
	playlist.queue.single = single;
225 226 227 228

	return success
		? CommandResult::OK
		: print_error(r, error);
229 230
}

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

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

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

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

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

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

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

288 289 290 291
	Error error;
	return client.partition.SeekSongPosition(song, seek_time, error)
		? CommandResult::OK
		: print_error(r, error);
292 293
}

294
CommandResult
295
handle_seekid(Client &client, Request args, Response &r)
296
{
297 298
	unsigned id = args.ParseUnsigned(0);
	SongTime seek_time = args.ParseSongTime(1);
299

300 301 302 303
	Error error;
	return client.partition.SeekSongId(id, seek_time, error)
		? CommandResult::OK
		: print_error(r, error);
304 305
}

306
CommandResult
307
handle_seekcur(Client &client, Request args, Response &r)
308
{
309
	const char *p = args.front();
310
	bool relative = *p == '+' || *p == '-';
311
	SignedSongTime seek_time = ParseCommandArgSignedSongTime(p);
312

313 314 315 316
	Error error;
	return client.partition.SeekCurrent(seek_time, relative, error)
		? CommandResult::OK
		: print_error(r, error);
317 318
}

319
CommandResult
320
handle_crossfade(Client &client, Request args, gcc_unused Response &r)
321
{
322
	unsigned xfade_time = args.ParseUnsigned(0);
323
	client.player_control.SetCrossFade(xfade_time);
324
	return CommandResult::OK;
325 326
}

327
CommandResult
328
handle_mixrampdb(Client &client, Request args, gcc_unused Response &r)
329
{
330
	float db = args.ParseFloat(0);
331
	client.player_control.SetMixRampDb(db);
332
	return CommandResult::OK;
333 334
}

335
CommandResult
336
handle_mixrampdelay(Client &client, Request args, gcc_unused Response &r)
337
{
338
	float delay_secs = args.ParseFloat(0);
339
	client.player_control.SetMixRampDelay(delay_secs);
340
	return CommandResult::OK;
341 342
}

343
CommandResult
344
handle_replay_gain_mode(Client &client, Request args, Response &r)
345
{
346
	if (!replay_gain_set_mode_string(args.front())) {
347
		r.Error(ACK_ERROR_ARG, "Unrecognized replay gain mode");
348
		return CommandResult::ERROR;
349 350
	}

351
	client.partition.outputs.SetReplayGainMode(replay_gain_get_real_mode(client.playlist.queue.random));
352
	client.partition.EmitIdle(IDLE_OPTIONS);
353
	return CommandResult::OK;
354 355
}

356
CommandResult
357 358
handle_replay_gain_status(gcc_unused Client &client, gcc_unused Request args,
			  Response &r)
359
{
360
	r.Format("replay_gain_mode: %s\n", replay_gain_get_mode_string());
361
	return CommandResult::OK;
362
}