feat(ncm): 首页顶栏「已缓存」入口 + 已缓存列表页(歌名/歌手/实际档位/大小,点播/单曲删除/清空)
入口:首页 topbar 新增动作 3(MEOW_ICON_DOWNLOAD 位图,原「回播放页」顺延动作 4)→ push ACT_CACHE;左缘返回弹回首页(app.c 路由补齐 tap/swipe/touch/tick)。 列表页 app/ui/cache.c(对齐 queue.c 惯例:圆角卡/红 mini-vinyl/按压红条/当前播放行高亮): - 行 = 歌名 / 歌手 · 实际档位 · 大小;空态/页脚(N 首 · 合计 X MB)。 - 点行 = 点播(NCM_QUEUE_CACHE 队列,上/下一首在缓存列表内循环);行右「删除」=单曲删除; 顶栏「清空」=确认框 + ncm_cache_clear(与设置页「清理缓存」同语义)。 - 数据全在 worker:cache_reload → appstate_cache_list_load → ncm_cache_scan(读目录 + 文件内 tag + 音频头探测)→ 快照;UI 线程零磁盘 IO。快照上限 512 条(最近访问优先, ≈116KB bss),更多条目按 mtime 保留最近的。 core: - ncm_cache_scan / ncm_cache_remove(songs/ 池,song_<id>_<请求档>.<实际格式> 解析, 跳过 .tmp/.part 与非数字 id;按 mtime 降序;历史双点名删除兜底)。 - ncm_api_level_from_key(缓存键后缀 ↔ 档位枚举,与 ncm_api_level_key 互逆)。 - **档位显示实际档而非请求档**:真机存在 song_171650_hires.mp3(无损请求被降级链落到 exhigh mp3),故 scan 用 ncm_audio_probe_file 读实际容器参数后只降不升地推断 (FLAC 16bit→无损 / 24bit→Hi-Res+,MP3 ≤128k→标准 / >128k→极高)。 播放:player_manager 对 NCM_QUEUE_CACHE 用条目真实键(id+请求档+实际扩展名)直读缓存 文件,不按用户当前档键重下;song_local_file/play_is_same_song/queue 页均支持第三种队列。 宿主 mock(NCM_PAGE=cache)造 3 条缓存 + 嵌入 tag + 同步扫描。
This commit is contained in:
1 parent
184ebd0ac9
commit
f392ee5c48
16 files changed
+829
-18
No files matched your search
@@ -39,6 +39,22 @@ const char *ncm_api_level_key(int level) {
|
||||
return ncm_api_level_str(level);
|
||||
}
|
||||
|
||||
/* 键后缀 → 档位枚举(缓存扫描/「已缓存列表」用;与 ncm_api_level_key 严格互逆)。 */
|
||||
int ncm_api_level_from_key(const char *key) {
|
||||
if (!key || !key[0]) return -1;
|
||||
if (!strcmp(key, "sky_c51")) return NCM_LEVEL_SURROUND;
|
||||
if (!strcmp(key, "sky")) return NCM_LEVEL_SKY;
|
||||
if (!strcmp(key, "jymaster")) return NCM_LEVEL_JYMASTER;
|
||||
if (!strcmp(key, "jyeffect")) return NCM_LEVEL_JYEFFECT;
|
||||
if (!strcmp(key, "dolby")) return NCM_LEVEL_DOLBY;
|
||||
if (!strcmp(key, "hires")) return NCM_LEVEL_HIRES;
|
||||
if (!strcmp(key, "lossless")) return NCM_LEVEL_LOSSLESS;
|
||||
if (!strcmp(key, "exhigh")) return NCM_LEVEL_EXHIGH;
|
||||
if (!strcmp(key, "higher")) return NCM_LEVEL_HIGHER;
|
||||
if (!strcmp(key, "standard")) return NCM_LEVEL_STANDARD;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ncm_api_level_gate(int level) {
|
||||
switch (level) {
|
||||
case NCM_LEVEL_LOSSLESS:
|
||||
|
||||
@@ -39,6 +39,8 @@ enum {
|
||||
};
|
||||
const char *ncm_api_level_str(int level); /* API level 串(SKY/SURROUND 同为 "sky") */
|
||||
const char *ncm_api_level_key(int level); /* 缓存键唯一后缀(SKY="sky"/SURROUND="sky_c51") */
|
||||
/* 缓存键后缀 → 档位枚举(ncm_api_level_key 的逆;未知/空返回 -1)。 */
|
||||
int ncm_api_level_from_key(const char *key);
|
||||
|
||||
/* 会员等级(vip/info 判定,BIZ_NOTES §1):用户语义 0=非会员 / 1=畅听会员(VIP) /
|
||||
* 2=黑胶会员(CVIP) / 3=超级会员(SVIP)。枚举名保留旧名(CVIP/SVIP)避免大改,新增
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#include "ncm_cache.h"
|
||||
#include "ncm_common.h"
|
||||
#include "ncm_store.h"
|
||||
#include "ncm_tag.h"
|
||||
#include "ncm_api.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
@@ -461,3 +463,134 @@ int ncm_cache_clear(const ncm_cache_t *c) {
|
||||
}
|
||||
return NCM_OK;
|
||||
}
|
||||
|
||||
/* ---- 「已缓存列表」:扫描/删除(键=歌曲ID+档位,NCM_CACHE_DESIGN §2.5)----
|
||||
* 文件名 song_<id>_<请求档位>.<实际格式>;**档位段只是请求档**,实际档由文件头探测得出
|
||||
* (真机 song_<id>_hires.mp3 = 无损请求被降级链落到 exhigh mp3)。 */
|
||||
|
||||
/* 解析条目名 → id/请求档位键/实际扩展名。song_ 后必须是数字 id(排除 song_cov* 封面)。
|
||||
* 返回 0=是歌曲条目。 */
|
||||
static int parse_item_name(const char *nm, ncm_cache_item_t *it)
|
||||
{
|
||||
const char *p;
|
||||
size_t n = 0;
|
||||
if (!nm || strncmp(nm, "song_", 5) != 0) return -1;
|
||||
p = nm + 5;
|
||||
while (p[n] >= '0' && p[n] <= '9' && n + 1 < sizeof it->id) { it->id[n] = p[n]; n++; }
|
||||
if (n == 0 || (p[n] >= '0' && p[n] <= '9')) return -1;
|
||||
it->id[n] = 0;
|
||||
p += n;
|
||||
it->level[0] = 0;
|
||||
if (*p == '_') {
|
||||
size_t m = 0;
|
||||
p++;
|
||||
while (*p && *p != '.' && m + 1 < sizeof it->level) it->level[m++] = *p++;
|
||||
it->level[m] = 0;
|
||||
}
|
||||
while (*p == '.') p++; /* 历史双点名 song_<id>..mp3 归一 */
|
||||
if (!*p) return -1;
|
||||
ncm_strlcpy(it->ext, p, sizeof it->ext);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 请求档 + 实际容器参数 → 实际档(只降不升:显示「已降级至 X」语义)。 */
|
||||
static int actual_level_of(int req, const ncm_audio_info_t *ai)
|
||||
{
|
||||
int cap;
|
||||
if (!ai || !ai->container[0]) return req; /* 探测失败:保留请求档 */
|
||||
if (strcmp(ai->container, "flac") == 0) {
|
||||
if (ai->bits_per_sample <= 16) cap = NCM_LEVEL_LOSSLESS;
|
||||
else if (ai->sample_rate > 96000) cap = NCM_LEVEL_JYMASTER; /* 192k/24 */
|
||||
else if (ai->sample_rate > 48000) cap = NCM_LEVEL_JYEFFECT; /* 96k/24 */
|
||||
else cap = NCM_LEVEL_HIRES; /* 48k/24 */
|
||||
} else if (strcmp(ai->container, "mp3") == 0) {
|
||||
cap = (ai->bitrate_kbps > 128) ? NCM_LEVEL_EXHIGH : NCM_LEVEL_STANDARD;
|
||||
} else {
|
||||
return req;
|
||||
}
|
||||
if (req < 0) return cap;
|
||||
return (req < cap) ? req : cap;
|
||||
}
|
||||
|
||||
static int cmp_item_mtime_desc(const void *a, const void *b)
|
||||
{
|
||||
const ncm_cache_item_t *x = (const ncm_cache_item_t *)a;
|
||||
const ncm_cache_item_t *y = (const ncm_cache_item_t *)b;
|
||||
if (x->mtime > y->mtime) return -1;
|
||||
if (x->mtime < y->mtime) return 1;
|
||||
return strcmp(x->id, y->id);
|
||||
}
|
||||
|
||||
int ncm_cache_scan(const ncm_cache_t *c, ncm_cache_item_t *out, int max)
|
||||
{
|
||||
const char *store;
|
||||
DIR *d;
|
||||
struct dirent *e;
|
||||
int n = 0;
|
||||
|
||||
if (!c || !out || max <= 0) return NCM_ERR_ARG;
|
||||
store = (c->songs_dir[0]) ? c->songs_dir : c->dir;
|
||||
d = opendir(store);
|
||||
if (!d) return NCM_ERR_IO;
|
||||
while ((e = readdir(d))) {
|
||||
ncm_cache_item_t it;
|
||||
char p[NCM_CACHE_DIR_MAX + NAME_MAX + 8];
|
||||
struct stat st;
|
||||
ncm_audio_info_t ai;
|
||||
ncm_tag_meta_t tag;
|
||||
int slot;
|
||||
|
||||
if (strncmp(e->d_name, "song_", 5) != 0) continue;
|
||||
if (is_partial_name(e->d_name)) continue; /* .tmp/.part:写者管理域 */
|
||||
memset(&it, 0, sizeof it);
|
||||
if (parse_item_name(e->d_name, &it) != 0) continue;
|
||||
snprintf(p, sizeof p, "%s/%s", store, e->d_name);
|
||||
if (stat(p, &st) != 0 || !S_ISREG(st.st_mode)) continue;
|
||||
it.size = (uint64_t)st.st_size;
|
||||
it.mtime = (int64_t)st.st_mtim.tv_sec;
|
||||
if (n < max) {
|
||||
slot = n;
|
||||
} else {
|
||||
/* 池内条目多于展示上限:只保留最近访问的 max 条 */
|
||||
int oldest = 0;
|
||||
for (int i = 1; i < n; i++)
|
||||
if (out[i].mtime < out[oldest].mtime) oldest = i;
|
||||
if (it.mtime <= out[oldest].mtime) continue;
|
||||
slot = oldest;
|
||||
}
|
||||
memset(&ai, 0, sizeof ai);
|
||||
it.actual_level = actual_level_of(ncm_api_level_from_key(it.level),
|
||||
(ncm_audio_probe_file(p, &ai) == NCM_OK) ? &ai : NULL);
|
||||
if (ncm_tag_read_file(p, &tag) == NCM_OK) { /* 文件内 tag 优先(外部播放器同源) */
|
||||
ncm_strlcpy(it.name, tag.title, sizeof it.name);
|
||||
ncm_strlcpy(it.artist, tag.artist, sizeof it.artist);
|
||||
}
|
||||
out[slot] = it;
|
||||
if (n < max) n++;
|
||||
}
|
||||
closedir(d);
|
||||
qsort(out, (size_t)n, sizeof *out, cmp_item_mtime_desc);
|
||||
return n;
|
||||
}
|
||||
|
||||
int ncm_cache_remove(const ncm_cache_t *c, const char *id, const char *level, const char *ext)
|
||||
{
|
||||
char key[48];
|
||||
char path[NCM_CACHE_DIR_MAX + 64];
|
||||
|
||||
if (!c || !id || !id[0]) return NCM_ERR_ARG;
|
||||
if (level && level[0]) snprintf(key, sizeof key, "%s_%s", id, level);
|
||||
else ncm_strlcpy(key, id, sizeof key);
|
||||
if (ncm_cache_path(c, key, (ext && ext[0]) ? ext : "mp3", path, sizeof path) != NCM_OK)
|
||||
return NCM_ERR_FULL;
|
||||
if (unlink(path) == 0) return NCM_OK;
|
||||
/* §19 历史双点名 song_<id>..<ext>(scan 已归一成单点 ext)兜底删除 */
|
||||
{
|
||||
char legacy[NCM_CACHE_DIR_MAX + 64];
|
||||
const char *e = (ext && ext[0]) ? ext : "mp3";
|
||||
const char *base = cache_base_dir(c, key);
|
||||
int k = snprintf(legacy, sizeof legacy, "%s/song_%s..%s", base, key, e);
|
||||
if (k > 0 && (size_t)k < sizeof legacy && unlink(legacy) == 0) return NCM_OK;
|
||||
}
|
||||
return NCM_ERR_NOENT;
|
||||
}
|
||||
@@ -87,6 +87,30 @@ uint64_t ncm_cache_total_bytes(const ncm_cache_t *c);
|
||||
/* 立即执行 LRU 淘汰(扫描 songs/;超上限则删最旧,直到 <= 上限;跳过 .tmp/.part 半成品)。 */
|
||||
int ncm_cache_evict(const ncm_cache_t *c);
|
||||
|
||||
/* 缓存条目(ncm_cache_scan 输出,供「已缓存列表」展示/点播/删除)。
|
||||
* 命名约定 song_<id>_<请求档位>.<实际格式>;**档位段是请求档,不是实际档**——真机存在
|
||||
* song_171650_hires.mp3(无损请求被降级链落到 exhigh mp3)。故 scan 用 ncm_audio_probe_file
|
||||
* 读实际容器参数,actual_level 给**实际档**(-1=无法判定);播放/删除仍用 id+level+ext 定位。 */
|
||||
typedef struct ncm_cache_item {
|
||||
char id[24]; /* 歌曲 id(数字串) */
|
||||
char level[20]; /* 文件名里的请求档位键(""=旧无档位键条目) */
|
||||
char ext[8]; /* 实际容器扩展名(mp3/flac,去点) */
|
||||
int actual_level; /* 实际档 NCM_LEVEL_*(按容器/位深/码率推断;-1=未知) */
|
||||
uint64_t size; /* 文件字节数(含嵌入 tag) */
|
||||
int64_t mtime; /* 最后访问/写入(LRU 序) */
|
||||
char name[96]; /* 文件内 tag 标题(无 tag 为空,调用方可回退旁路元数据) */
|
||||
char artist[64]; /* 文件内 tag 歌手 */
|
||||
} ncm_cache_item_t;
|
||||
|
||||
/* 扫描歌曲缓存池(songs/,跳过 .tmp/.part 半成品与非 song_<数字 id> 条目),按 mtime 降序
|
||||
* (最近播放/写入在前)写入 out(最多 max 条;池内更多时保留最近的 max 条)。返回条目数或负错误码。
|
||||
* 每条会读文件头(tag + 音频参数探测),属磁盘 IO——**必须在 worker 线程调用**。 */
|
||||
int ncm_cache_scan(const ncm_cache_t *c, ncm_cache_item_t *out, int max);
|
||||
|
||||
/* 删除单条缓存:id+level(level 可为 "")+ext 定位 song_<id>[_<level>].<ext>。
|
||||
* 返回 0=已删除;NCM_ERR_NOENT=不存在。 */
|
||||
int ncm_cache_remove(const ncm_cache_t *c, const char *id, const char *level, const char *ext);
|
||||
|
||||
/* 清空缓存池:删除 songs/ 与 covers/ 的 song_* 全部文件(音频 + 封面条目 + 残留 .tmp/.part)。 */
|
||||
int ncm_cache_clear(const ncm_cache_t *c);
|
||||
|
||||
|
||||
Reference in new issue
Block a user