-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathashuffle.cc
371 lines (337 loc) · 13.5 KB
/
ashuffle.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <sys/types.h>
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <optional>
#include <string>
#include <string_view>
#include <thread>
#include <absl/strings/str_format.h>
#include <absl/time/clock.h>
#include <absl/time/time.h>
#include <mpd/idle.h>
#include "args.h"
#include "ashuffle.h"
#include "load.h"
#include "log.h"
#include "mpd.h"
#include "mpd_client.h"
#include "rule.h"
#include "shuffle.h"
#include "util.h"
namespace ashuffle {
namespace {
/* These MPD commands are required for ashuffle to run */
constexpr std::array<std::string_view, 5> kRequiredCommands = {
"add", "status", "play", "pause", "idle",
};
absl::Status TryFirst(mpd::MPD *mpd, ShuffleChain *songs) {
absl::StatusOr<std::unique_ptr<mpd::Status>> status = mpd->CurrentStatus();
if (!status.ok()) {
Log().Error("Failed to query current MPD Status: %s",
status.status().ToString());
return status.status();
}
// No need to do anything if the player is already going.
if ((*status)->IsPlaying()) {
return absl::OkStatus();
}
// If we're not playing, then add a song, and start playing it.
if (auto s = mpd->Add(songs->Pick()); !s.ok()) {
Log().Error("Failed to add song to MPD: %s", s.ToString());
return s;
}
// Passing the former queue length, because PlayAt is zero-indexed.
if (auto s = mpd->PlayAt((*status)->QueueLength()); !s.ok()) {
Log().Error("Failed to play newly added song: %s", s.ToString());
return s;
}
return absl::OkStatus();
}
absl::Status TryEnqueue(mpd::MPD *mpd, ShuffleChain *songs,
const Options &options) {
absl::StatusOr<std::unique_ptr<mpd::Status>> status_or =
mpd->CurrentStatus();
if (!status_or.ok()) {
Log().Error("Failed to fetch MPD status");
return status_or.status();
}
std::unique_ptr<mpd::Status> status = std::move(*status_or);
// We're "past" the last song, if there is no current song position.
bool past_last = !status->SongPosition().has_value();
bool queue_empty = status->QueueLength() == 0;
unsigned queue_songs_remaining = 0;
if (!past_last) {
/* +1 on song_pos because it is zero-indexed */
queue_songs_remaining =
(status->QueueLength() - (*status->SongPosition() + 1));
}
bool should_add = false;
if (past_last) {
/* Always add if we've progressed past the last song. Even if
* --queue_buffer, we should have already enqueued a song by now. */
should_add = true;
} else if (queue_songs_remaining < options.queue_buffer) {
/* If a queue buffer is set, check to see how any songs are left. If
* we're past the end of our queue buffer, allow enquing a song. */
should_add = true;
} else if (queue_empty) {
/* If the queue is totally empty, enqueue. */
should_add = true;
}
/* Add another song to the list and restart the player */
if (should_add) {
if (options.queue_buffer != 0) {
int needed = static_cast<int>(options.queue_buffer) -
static_cast<int>(queue_songs_remaining);
// If we're not currently "on" a song, then we need to not only
// enqueue options->queue_buffer songs, but also the song we're
// about to play, so increment the `to_enqueue' count by one.
if (past_last || queue_empty) {
needed += 1;
}
while (needed > 0) {
std::vector<std::string> picked = songs->Pick();
needed -= static_cast<int>(picked.size());
if (auto status = mpd->Add(picked); !status.ok()) {
Log().Error("Failed to add picked song: %s",
status.ToString());
return status;
}
}
} else {
if (auto status = mpd->Add(songs->Pick()); !status.ok()) {
Log().Error("Failed to add picked song: %s", status.ToString());
return status;
}
}
}
/* If we added a song, and the player was not already playing, we need
* to re-start it. */
if (should_add && (past_last || queue_empty)) {
/* Since the 'status' was before we added our song, and the queue
* is zero-indexed, the length will be the position of the song we
* just added. Play that song */
if (auto s = mpd->PlayAt(status->QueueLength()); !s.ok()) {
Log().Error("Failed to start newly enqueued song: %s",
s.ToString());
return s;
}
/* Immediately pause playback if mpd single mode is on */
if (status->Single()) {
if (auto status = mpd->Pause(); !status.ok()) {
Log().Error("Failed to stop playback in single mode: %s",
status.ToString());
return status;
}
}
}
return absl::OkStatus();
}
void PromptPassword(mpd::MPD *mpd, std::function<std::string()> &getpass_f) {
/* keep looping till we get a bad error, or we get a good password. */
while (true) {
using status = mpd::MPD::PasswordStatus;
std::string pass = getpass_f();
absl::StatusOr<status> result = mpd->ApplyPassword(pass);
if (!result.ok()) {
std::cerr << "Failed to apply password:" << result.status()
<< std::endl;
continue;
}
if (*result == status::kAccepted) {
return;
}
fputs("incorrect password.\n", stderr);
}
}
struct MPDHost {
std::string host;
std::optional<std::string> password;
MPDHost(std::string_view in) {
std::size_t idx = in.find("@");
if (idx != std::string_view::npos) {
password = in.substr(0, idx);
host = in.substr(idx + 1, in.size() - idx);
} else {
host = in;
}
}
};
} // namespace
std::optional<std::unique_ptr<Loader>> Reloader(mpd::MPD *mpd,
const Options &options) {
// Nothing we can do when `--file` is provided. The user is just stuck
// with whatever URIs we parsed the first time.
if (options.file_in != nullptr) {
return std::nullopt;
}
return std::make_unique<MPDLoader>(mpd, options.ruleset, options.group_by);
}
/* Keep adding songs when the queue runs out */
absl::Status Loop(mpd::MPD *mpd, ShuffleChain *songs, const Options &options,
TestDelegate test_d) {
static_assert(MPD_IDLE_QUEUE == MPD_IDLE_PLAYLIST,
"QUEUE Now different signal.");
mpd::IdleEventSet set(MPD_IDLE_DATABASE, MPD_IDLE_QUEUE, MPD_IDLE_PLAYER);
// If the test delegate's `skip_init` is set to true, then skip the
// initializer.
if (options.tweak.play_on_startup) {
if (auto status = TryFirst(mpd, songs); !status.ok()) {
return status;
}
if (auto status = TryEnqueue(mpd, songs, options); !status.ok()) {
return status;
}
}
// Tracks if we should be enqueuing new songs.
bool active = true;
// Loop forever if test delegates are not set.
while (test_d.until_f == nullptr || test_d.until_f()) {
/* wait till the player state changes */
absl::StatusOr<mpd::IdleEventSet> events = mpd->Idle(set);
if (!events.ok()) {
Log().Error("Failed to idle for MPD events: %s",
events.status().ToString());
return events.status();
}
if (events->Has(MPD_IDLE_DATABASE) && options.tweak.exit_on_db_update) {
std::cout << "Database updated, exiting." << std::endl;
std::exit(0);
}
/* Only update the database if our original list was built from
* MPD. */
if (events->Has(MPD_IDLE_DATABASE) && options.file_in == nullptr) {
std::optional<std::unique_ptr<Loader>> reloader =
Reloader(mpd, options);
if (reloader.has_value()) {
songs->Clear();
(*reloader)->Load(songs);
PrintChainLength(std::cout, *songs);
}
} else if (events->Has(MPD_IDLE_QUEUE) ||
events->Has(MPD_IDLE_PLAYER)) {
if (options.tweak.suspend_timeout != absl::ZeroDuration()) {
auto status_or = mpd->CurrentStatus();
if (!status_or.ok()) {
Log().Error("Failed to fetch status in suspend handler");
return status_or.status();
}
std::unique_ptr<mpd::Status> status = std::move(*status_or);
if (status->QueueLength() == 0) {
test_d.sleep_f(options.tweak.suspend_timeout);
auto status_or = mpd->CurrentStatus();
if (!status_or.ok()) {
Log().Error(
"Failed to fetch status in suspend handler");
return status_or.status();
}
status = std::move(*status_or);
active = status->QueueLength() == 0;
}
}
if (!active) {
continue;
}
if (auto status = TryEnqueue(mpd, songs, options); !status.ok()) {
Log().Error("Failed regular enqueue");
return status;
}
}
}
return absl::OkStatus();
}
absl::StatusOr<std::unique_ptr<mpd::MPD>> Connect(
const mpd::Dialer &d, const Options &options,
std::function<std::string()> *getpass_f) {
/* Attempt to get host from command line if available. Otherwise use
* MPD_HOST variable if available. Otherwise use 'localhost'. */
const char *env_host =
getenv("MPD_HOST") != nullptr ? getenv("MPD_HOST") : "localhost";
std::string mpd_host_raw =
options.host.has_value() ? *options.host : env_host;
MPDHost mpd_host(mpd_host_raw);
/* Same thing for the port, use the command line defined port, environment
* defined, or the default port */
unsigned mpd_port =
options.port
? options.port
: (unsigned)(getenv("MPD_PORT") ? atoi(getenv("MPD_PORT")) : 6600);
mpd::Address addr = {
.host = mpd_host.host,
.port = mpd_port,
};
absl::StatusOr<std::unique_ptr<mpd::MPD>> r = d.Dial(addr);
if (!r.ok()) {
Log().Error("Failed to connect to mpd: %s", r.status().ToString());
return r.status();
}
std::unique_ptr<mpd::MPD> mpd = std::move(*r);
/* Password Workflow:
* 1. If the user supplied a password, then apply it. No matter what.
* 2. Check if we can execute all required commands. If not then:
* 2.a Fail if the user gave us a password that didn't work.
* 2.b Prompt the user to enter a password, and try again.
* 3. If the user successfully entered a password, then check that all
* required commands can be executed again. If we still can't execute
* all required commands, then fail. */
if (mpd_host.password) {
// We don't actually care if the password was accepted here. We still
// need to check the available commands either way.
(void)mpd->ApplyPassword(*mpd_host.password);
}
// Need a vector for the types to match up.
const std::vector<std::string_view> required(kRequiredCommands.begin(),
kRequiredCommands.end());
absl::StatusOr<mpd::MPD::Authorization> auth = mpd->CheckCommands(required);
if (!auth.ok()) {
Log().Error("Failed to check required commands: %s",
auth.status().ToString());
return auth.status();
}
if (!mpd_host.password && !auth->authorized && getpass_f != nullptr) {
// If the user did *not* supply a password, and we are missing a
// required command, and we're in an interactive mode (where we have
// the ability to prompt for a password) then try to prompt the user to
// provide a password. Once we get/apply a password, try the required
// commands again...
PromptPassword(mpd.get(), *getpass_f);
auth = mpd->CheckCommands(required);
if (!auth.ok()) {
Log().Error("Failed to check required commands: %s",
auth.status().ToString());
return auth.status();
}
}
// If we still can't connect, inform the user which commands are missing,
// and exit.
if (!auth->authorized) {
std::cerr << "Missing MPD Commands:" << std::endl;
for (std::string &cmd : auth->missing) {
std::cerr << " " << cmd << std::endl;
}
Die("password applied, but required command still not allowed.");
}
return mpd;
}
// Print the size of the database to the given stream, accounting for grouping.
void PrintChainLength(std::ostream &stream, const ShuffleChain &songs) {
if (songs.Len() == 0) {
stream << "Song pool is empty." << std::endl;
return;
}
// If we're grouping.
if (songs.Len() != songs.LenURIs()) {
stream << absl::StrFormat("Picking from %u groups (%u songs).",
songs.Len(), songs.LenURIs())
<< std::endl;
} else {
stream << "Picking random songs out of a pool of " << songs.Len() << "."
<< std::endl;
}
}
} // namespace ashuffle