//- 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; } // " [,...]" -> 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 [,...]"); 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 [,...]"); 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 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 }