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
|
//- commands
// NOTE: one vocabulary for everything that drives the player from outside the keyboard: the
// control socket (mp3_player ctl, waybar clicks) and the test scripts (MP3_PLAYER_SCRIPT). a
// verb added here works in all of them.
internal b32 command_fail(char *error, u32 error_size, const char *format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(error, error_size, format, args);
va_end(args);
return false;
}
// "<qari id> <surah>[,<surah>...]" -> track keys
internal u32 command_tracks(App *app, char *args, u32 *keys, u32 capacity, s32 *out_qari)
{
char *rest = NULL;
s32 qari = catalog_find_qari_by_id(app->catalog, (u32)strtoul(args, &rest, 10));
if(qari < 0 || rest == args) return 0;
u32 count = 0;
for(char *at = rest; *at && count < capacity;) {
while(*at == ' ' || *at == ',') ++at;
if(!*at) break;
char *end = NULL;
long surah = strtol(at, &end, 10);
if(end == at || surah < 1 || surah > SURAH_COUNT) return 0;
keys[count++] = TrackKey((u32)qari, (u32)surah - 1);
at = end;
}
*out_qari = qari;
return count;
}
internal void app_stop(App *app)
{
if(!app->audio_ok) return;
decoder_command(app->decoder, DECODER_STOP, 0, 0);
app->requested_key = TRACK_NONE;
app->paused = false;
}
internal b32 player_command(App *app, char *line, char *error, u32 error_size)
{
while(*line == ' ' || *line == '\t') ++line;
char *args = line;
while(*args && *args != ' ' && *args != '\t') ++args;
if(*args) *args++ = 0;
while(*args == ' ' || *args == '\t') ++args;
const char *verb = line;
u32 rate = Max(app->position.rate, 44100u);
error[0] = 0;
if(!strcmp(verb, "toggle")) {
app_toggle_pause(app);
} else if(!strcmp(verb, "play") || !strcmp(verb, "queue")) {
if(!args[0]) {
// NOTE: plain play: resume, or start the selected surah
if(app_current_key(app) == TRACK_NONE || app->paused) app_toggle_pause(app);
return true;
}
u32 keys[SURAH_COUNT];
s32 qari = -1;
u32 count = command_tracks(app, args, keys, ArrayCount(keys), &qari);
if(count == 0) return command_fail(error, error_size, "expected <qari id> <surah>[,<surah>...]");
app->qari = qari;
app->surah = (s32)TrackSurah(keys[0]);
app->keep_selection_visible = true;
if(count == 1) app_play(app, keys[0], 0);
else app_play_queue(app, keys, count);
} else if(!strcmp(verb, "pause") || !strcmp(verb, "resume")) {
if(app_current_key(app) != TRACK_NONE && app->paused != !strcmp(verb, "pause")) app_toggle_pause(app);
} else if(!strcmp(verb, "stop")) {
app_stop(app);
} else if(!strcmp(verb, "next") || !strcmp(verb, "prev")) {
app_step_track(app, !strcmp(verb, "next") ? 1 : -1);
} else if(!strcmp(verb, "seek") || !strcmp(verb, "seekend")) {
// seek 120 | seek +10 | seek -10 | seek end-3 (seekend 3: the scripts' spelling)
if(app_current_key(app) == TRACK_NONE) return command_fail(error, error_size, "nothing is playing");
b32 from_end = !strcmp(verb, "seekend") || !strncmp(args, "end", 3);
const char *number = !strncmp(args, "end", 3) ? args + 3 : args;
if(!*number && !from_end) return command_fail(error, error_size, "expected seconds: 120, +10, -10 or end-3");
f64 seconds = atof(number);
if(from_end) {
u64 total = app_track_total(app);
if(!total) return command_fail(error, error_size, "length not known yet");
decoder_command(app->decoder, DECODER_SEEK, 0, total - Min(total, (u64)(fabs(seconds) * rate)));
} else if(number[0] == '+' || number[0] == '-') {
app_seek_seconds(app, seconds);
} else {
decoder_command(app->decoder, DECODER_SEEK, 0, (u64)(Max(seconds, 0.0) * rate));
}
} else if(!strcmp(verb, "vol") || !strcmp(verb, "volume")) {
if(!args[0]) return command_fail(error, error_size, "expected 0-100, +n or -n");
f32 value = (f32)atof(args) / 100.0f;
app_set_volume(app, (args[0] == '+' || args[0] == '-') ? app->settings.volume + value : value);
} else if(!strcmp(verb, "download") || !strcmp(verb, "delete")) {
u32 keys[SURAH_COUNT];
s32 qari = -1;
u32 count = command_tracks(app, args, keys, ArrayCount(keys), &qari);
if(count == 0) return command_fail(error, error_size, "expected <qari id> <surah>[,<surah>...]");
for(u32 index = 0; index < count; ++index) {
if(!strcmp(verb, "download")) store_request(app->store, keys[index], true);
else app_delete(app, keys[index]);
}
} else if(!strcmp(verb, "view")) {
ViewType type;
if(!view_resolve(args, &type)) return command_fail(error, error_size, "no view called %s", args);
layout_open(&app->layout, (ViewKind)type.kind, type.group);
} else if(!strcmp(verb, "set")) {
if(!settings_set(&app->settings, args)) return command_fail(error, error_size, "unknown setting or value: %s", args);
app_apply_settings(app);
} else if(!strcmp(verb, "cmd")) {
// NOTE: anything the : line takes, exactly as if typed there
app->input_size = (u32)Min(strlen(args), sizeof(app->input) - 1);
MemCpy(app->input, args, app->input_size);
command_run(app);
app->input_size = 0;
} else if(!strcmp(verb, "quit")) {
app->quit = true;
} else if(!strcmp(verb, "ping")) {
} else {
return command_fail(error, error_size, "unknown command %s (try help)", verb);
}
return true;
}
//- status
typedef enum PlayerState {
PLAYER_STOPPED,
PLAYER_PLAYING,
PLAYER_PAUSED,
PLAYER_BUFFERING,
} PlayerState;
global_variable const char *player_state_names[] = {"stopped", "playing", "paused", "buffering"};
// NOTE: everything a status event carries, in the units it's shown in. two equal snapshots
// mean an identical event, so a compare decides whether subscribers hear about it.
typedef struct PlayerSnapshot PlayerSnapshot;
struct PlayerSnapshot {
u32 key;
u32 state;
u32 position_s;
u32 duration_s;
u32 volume;
u32 autoplay;
u32 queue_index; // 1 based, 0 = not in the queue
u32 queue_count;
u32 downloaded; // per mille of the current file on disk
};
internal PlayerSnapshot player_snapshot(App *app)
{
PlayerSnapshot snapshot = {};
u32 key = app_current_key(app);
snapshot.key = key;
snapshot.volume = (u32)(app->settings.volume * 100.0f + 0.5f);
snapshot.autoplay = app->settings.autoplay;
snapshot.queue_count = app->queue_count;
if(key == TRACK_NONE) {
snapshot.state = PLAYER_STOPPED;
return snapshot;
}
u32 status = AtomicLoadRelaxed(&app->decoder->status);
snapshot.state = app->paused ? PLAYER_PAUSED
: (status == DECODER_BUFFERING || (app->position.flags & PLAYBACK_STARVED)) ? PLAYER_BUFFERING
: PLAYER_PLAYING;
u32 rate = app->position.rate ? app->position.rate : 44100;
if((app->position.flags & PLAYBACK_HAS_TRACK) && app->position.track_key == key) snapshot.position_s = (u32)(app->position.frame / rate);
snapshot.duration_s = (u32)(app_track_total(app) / rate);
s32 at = queue_find(app, key);
snapshot.queue_index = (at >= 0) ? (u32)at + 1 : 0;
TrackEntry *entry = store_entry(app->store, key);
if(entry) {
u64 total = AtomicLoadRelaxed(&entry->bytes_total);
u64 done = AtomicLoadRelaxed(&entry->bytes_done);
b32 disk = AtomicLoadRelaxed(TrackStatePtr(app->store, entry)) == TRACK_DONE;
snapshot.downloaded = disk ? 1000 : (total ? (u32)(done * 1000 / total) : 0);
}
return snapshot;
}
internal u32 player_status_json(App *app, PlayerSnapshot *snapshot, char *buffer, u32 capacity)
{
JsonWriter json;
json_begin(&json, buffer, capacity);
json_string(&json, "event", str8("status"));
json_string(&json, "state", str8((char *)player_state_names[snapshot->state]));
if(snapshot->key != TRACK_NONE) {
Qari *qari = &app->catalog->qaris[TrackQari(snapshot->key)];
Surah *surah = &app->catalog->surahs[TrackSurah(snapshot->key)];
json_u64(&json, "qari_id", qari->id);
json_string(&json, "reciter", qari->name);
json_u64(&json, "surah", TrackSurah(snapshot->key) + 1);
json_string(&json, "surah_name", surah->name);
json_string(&json, "surah_english", surah->english);
json_u64(&json, "ayat", surah->ayah_count);
json_u64(&json, "position", snapshot->position_s);
json_u64(&json, "duration", snapshot->duration_s);
json_f64(&json, "downloaded", snapshot->downloaded / 1000.0);
json_bool(&json, "on_disk", snapshot->downloaded == 1000);
}
json_u64(&json, "volume", snapshot->volume);
json_string(&json, "autoplay", str8((char *)autoplay_name(snapshot->autoplay)));
json_u64(&json, "queue_index", snapshot->queue_index);
json_u64(&json, "queue_count", snapshot->queue_count);
json_end(&json);
return json.size;
}
//- server glue
typedef struct PlayerIpc PlayerIpc;
struct PlayerIpc {
App *app;
IpcServer server;
PlayerSnapshot published;
b32 any_published;
b32 changed; // a command ran: the caller redraws
};
internal void player_ipc_handle(void *user, IpcServer *server, u32 client, char *line)
{
PlayerIpc *ipc = (PlayerIpc *)user;
App *app = ipc->app;
char reply[2048];
if(!strcmp(line, "status") || !strcmp(line, "subscribe")) {
if(line[1] == 'u') ipc_subscribe(server, client);
PlayerSnapshot snapshot = player_snapshot(app);
ipc_reply(server, client, reply, player_status_json(app, &snapshot, reply, sizeof(reply)));
return;
}
if(!strcmp(line, "help")) {
const char *text = "{\"ok\":true,\"commands\":\"status subscribe toggle play pause resume stop next prev seek vol "
"play <qari> <surahs> download delete view set cmd ping quit\"}";
ipc_reply(server, client, text, (u32)strlen(text));
return;
}
char error[256];
b32 ok = player_command(app, line, error, sizeof(error));
ipc->changed = true;
JsonWriter json;
json_begin(&json, reply, sizeof(reply));
json_bool(&json, "ok", ok);
if(!ok) json_string(&json, "error", str8(error));
json_end(&json);
ipc_reply(server, client, reply, json.size);
}
// NOTE: once per loop pass: a struct compare, and only with subscribers. a playing track changes
// its snapshot once a second (whole seconds of position), so that's the event rate.
internal void player_ipc_publish(PlayerIpc *ipc)
{
if(ipc->server.subscribers == 0) {
ipc->any_published = false;
return;
}
PlayerSnapshot snapshot = player_snapshot(ipc->app);
if(ipc->any_published && memcmp(&snapshot, &ipc->published, sizeof(snapshot)) == 0) return;
ipc->published = snapshot;
ipc->any_published = true;
char line[2048];
ipc_broadcast(&ipc->server, line, player_status_json(ipc->app, &snapshot, line, sizeof(line)));
}
// NOTE: how long the loop may sleep before a subscriber is owed the next second of position.
// -1 = no deadline (no subscribers, or nothing moves).
internal s64 player_ipc_timeout(PlayerIpc *ipc)
{
App *app = ipc->app;
if(ipc->server.subscribers == 0 || app->paused || app_current_key(app) == TRACK_NONE) return -1;
u32 rate = app->position.rate;
if(!rate || !(app->position.flags & PLAYBACK_HAS_TRACK)) return 250000000ll; // starting up: check back soon
u64 left = rate - app->position.frame % rate;
return (s64)(left * 1000000000ull / rate) + 2000000ll; // just past the boundary
}
|