ncm: FLAC 解码落地(dr_flac 0.13.4 单头入库) + 缓存键=歌曲ID+音质档位

- 解码分发 decoder.c:magic(fLaC→dr_flac / 其它→minimp3);flac ≤16bit→S16、
  >16bit→S32 满幅送 audiod(AUDIO_FMT_S32),rate/ch/bits 按实际流,零 SRC 零裁剪
- song_url_auto 接受 flac(type 权威/url 扩展名兜底),降级语义不变;flac 下载限幅 1GB
- 缓存键 song_<id>_<level>.<ext>:同歌同档命中不重下,换档新条目旧档留 LRU;
  ext 前导点正名(修双点名),lookup 回退历史双点名+旧无档键温和兼容
- audio_out S32 通路(AUDIOD/ALSA/WAV)+ 流死重开按 bits 还原格式
- 验证(0 真实请求):44.1k/16 与 96k/24 自制样本宿主+qemu 帧数/时长精确、
  逐样本零误差;mp3 回归逐帧一致;player_flow_sim P1-P6(新 P6 档位键/flac ext)、
  cache_evict/cover/pagination/window/audiod_chain sim 全 PASS;mipsel 0 warning
- REPORT §19 / VENDOR_NOTES dr_flac 条目;未部署真机(留主会话)
This commit is contained in:
MeowOS committed 2026-09-01 14:14:38 +08:00
1 parent 80bc191593
commit 70aec677ce
23 files changed
+13377 -110

No files matched your search

+6 -2
View File
@@ -435,8 +435,12 @@ static int autoplay_try_playlist(int pl)
for (int i = 0; i < g_st.track_ids_count; i++) { /* 滑窗模型:锁内拷贝窗内有效槽 */
ncm_song_t s;
if (appstate_song_copy(i, &s) != 0) break; /* 出窗即停(首页之后未物化) */
if (ncm_cache_lookup(&g_st.ctx.cache, s.id, ".mp3",
cpath, sizeof cpath) == NCM_OK) {
/* §19 缓存键 = id+档位:先试当前档位键(flac/mp3),再回退旧无档位键(存量 mp3) */
char key[64];
snprintf(key, sizeof key, "%s_%s", s.id, ncm_api_level_str(g_st.level));
if (ncm_cache_lookup(&g_st.ctx.cache, key, "flac", cpath, sizeof cpath) == NCM_OK ||
ncm_cache_lookup(&g_st.ctx.cache, key, "mp3", cpath, sizeof cpath) == NCM_OK ||
ncm_cache_lookup(&g_st.ctx.cache, s.id, "mp3", cpath, sizeof cpath) == NCM_OK) {
g_st.active_playlist = pl;
meow_log(MEOW_LOG_INFO, "autoplay: playlist[%d] song[%d] id=%s cached -> play",
pl, i, s.id);
+2 -2
View File
@@ -315,8 +315,8 @@ void cover_cache_reset(void);
/* ---- play(app/play/)---- */
/* audio_out(后端:默认 SINK_AUDIOD=meow_audiod 独占服务;env NCM_AUDIO_SINK=file/null/alsa
* 或 MEOW_AUDIO_DIRECT=1 选调试后端,见 audio_out.c 文件头) */
int audio_out_open(int sample_rate, int channels, int bits); /* 0=成功 */
int audio_out_write(const short *samples, int frames); /* <0=错误/结束 */
int audio_out_open(int sample_rate, int channels, int bits); /* 0=成功;bits∈{16,32}(§19 flac>16bit→32) */
int audio_out_write(const void *samples, int frames); /* <0=错误/结束;样本位宽=open 的 bits */
int audio_out_pause(int paused); /* 1=暂停(停 PCM 时钟不丢流) 0=恢复(流死自动重开);非 AUDIOD 后端恒 0 */
void audio_out_close(void);
void audio_out_set_target(int ms); /* 预留:缓冲目标 */
+17 -11
View File
@@ -62,7 +62,7 @@ static void pivset(struct snd_interval *iv, unsigned val)
}
static int alsa_open(int rate, int ch, int bits)
{
if (bits != 16) return -1;
if (bits != 16 && bits != 32) return -1; /* §19:S16 + S32(flac >16bit 满幅) */
int fd = open("/dev/snd/pcmC0D0p", O_RDWR);
if (fd < 0) {
meow_log(MEOW_LOG_ERROR, "audio: alsa open /dev/snd/pcmC0D0p failed errno=%d", errno);
@@ -84,7 +84,8 @@ static int alsa_open(int rate, int ch, int bits)
struct snd_mask *am = &p.masks[SNDRV_PCM_HW_PARAM_ACCESS - SNDRV_PCM_HW_PARAM_FIRST_MASK];
memset(am, 0, sizeof *am); pmask(am, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
struct snd_mask *fm = &p.masks[SNDRV_PCM_HW_PARAM_FORMAT - SNDRV_PCM_HW_PARAM_FIRST_MASK];
memset(fm, 0, sizeof *fm); pmask(fm, SNDRV_PCM_FORMAT_S16_LE);
memset(fm, 0, sizeof *fm);
pmask(fm, bits == 32 ? SNDRV_PCM_FORMAT_S32_LE : SNDRV_PCM_FORMAT_S16_LE);
struct snd_mask *sm = &p.masks[SNDRV_PCM_HW_PARAM_SUBFORMAT - SNDRV_PCM_HW_PARAM_FIRST_MASK];
memset(sm, 0, sizeof *sm); pmask(sm, SNDRV_PCM_SUBFORMAT_STD);
pivset(&p.intervals[SNDRV_PCM_HW_PARAM_CHANNELS - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL],
@@ -99,13 +100,14 @@ static int alsa_open(int rate, int ch, int bits)
return -1;
}
ioctl(fd, SNDRV_PCM_IOCTL_PREPARE, 0);
meow_log(MEOW_LOG_INFO, "audio: alsa open ok rate=%d ch=%d (direct debug)", rate, ch);
meow_log(MEOW_LOG_INFO, "audio: alsa open ok rate=%d ch=%d bits=%d (direct debug)", rate, ch, bits);
return fd;
}
#endif
/* 写 WAV 头(44 字节标准 PCM 布局:RIFF/WAVE/fmt(16)/data)。
* fmt(16) = format(2) ch(2) sample_rate(4) byte_rate(4) block_align(2) bits(2)。 */
* fmt(16) = format(2) ch(2) sample_rate(4) byte_rate(4) block_align(2) bits(2)。
* §19:bits 16/32 均合法(32=flac >16bit 的 S32 满幅样本)。 */
static void file_write_header(int fd, int rate, int ch, int bits, uint32_t data_len)
{
unsigned char h[44];
@@ -174,12 +176,16 @@ int audio_out_open(int sample_rate, int channels, int bits)
#endif
}
/* 默认:SINK_AUDIOD(meow_audiod 独占服务)。v1 SDK 声源格式 S16(minimp3 输出即 S16_LE)。 */
if (bits != 16) {
meow_log(MEOW_LOG_ERROR, "audio: audiod open: bits=%d unsupported (v1 S16 only)", bits);
/* 默认:SINK_AUDIOD(meow_audiod 独占服务)。§19:bits 16→S16 / 32→S32
* (flac >16bit 由 dr_flac 出 S32 满幅;格式归一/DAC 原生率跟随在 audiod 侧)。 */
int fmt;
if (bits == 16) fmt = AUDIO_FMT_S16;
else if (bits == 32) fmt = AUDIO_FMT_S32;
else {
meow_log(MEOW_LOG_ERROR, "audio: audiod open: bits=%d unsupported (16/32)", bits);
return -1;
}
int sid = meow_audio_open(sample_rate, channels, AUDIO_FMT_S16);
int sid = meow_audio_open(sample_rate, channels, fmt);
if (sid < 0) {
meow_log(MEOW_LOG_ERROR, "audio: audiod open failed rc=%d rate=%d ch=%d (audiod 不在?)",
sid, sample_rate, channels);
@@ -199,7 +205,7 @@ int audio_out_open(int sample_rate, int channels, int bits)
return 0;
}
int audio_out_write(const short *samples, int frames)
int audio_out_write(const void *samples, int frames)
{
if (g_app_quit) return -1;
if (g_sink == SINK_NULL || g_sink < 0) return frames;
@@ -252,11 +258,11 @@ int audio_out_pause(int paused)
meow_log(MEOW_LOG_WARN, "audio: audiod pause sid=%d rc=%d (stream dead?)", g_sid, rc);
return 0;
}
/* 恢复失败 = 流已死 → 重开 */
/* 恢复失败 = 流已死 → 重开(格式按本次 open 的 bits 还原:16→S16 / 32→S32) */
meow_log(MEOW_LOG_WARN, "audio: audiod resume sid=%d rc=%d -> reopen", g_sid, rc);
meow_audio_close(g_sid);
g_sid = 0;
int sid = meow_audio_open(g_rate, g_ch, AUDIO_FMT_S16);
int sid = meow_audio_open(g_rate, g_ch, g_bits == 32 ? AUDIO_FMT_S32 : AUDIO_FMT_S16);
if (sid < 0) {
meow_log(MEOW_LOG_ERROR, "audio: audiod reopen failed rc=%d rate=%d ch=%d", sid, g_rate, g_ch);
return -1;
+141
View File
@@ -0,0 +1,141 @@
/* play/decoder.c — 格式分发解码实现(接口见 decoder.h,REPORT §19)。
*
* mp3 细节:minimp3_ex 的 dec.info(hz/channels)在首次 mp3dec_ex_read 后才保证有效,
* 而调用方开 audio_out 前就要 rate/ch —— open 时先预读一批进 stash,read 先吐 stash。
* seek(mp3):关+重开(MP3D_SEEK_TO_BYTE 索引在 open 时重建)再按字节近似 seek,同旧语义。
*/
#include "decoder.h"
#include "minimp3_ex.h"
#include "dr_flac.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEC_STASH_FRAMES 4096 /* open 预读上限(帧),与播放循环单块读同量级 */
enum { DEC_MP3 = 0, DEC_FLAC = 1 };
struct ncm_decoder {
int kind;
char path[512];
int rate, ch, bits;
int64_t total_frames; /* 0=未知 */
/* mp3 */
mp3dec_ex_t mp3;
int16_t *stash; /* 预读样本(交织 s16),stash_n 个样本 */
size_t stash_n, stash_off;
/* flac */
drflac *flac;
};
/* magic 探测:首 4 字节 "fLaC" → flac;其余(ID3 / 0xFF 帧同步 / 未知)→ mp3
*(未知格式留给 minimp3 在 open 时报错,不在本层加第三种分支)。 */
static int sniff_kind(const char *path)
{
unsigned char m[4] = {0};
FILE *f = fopen(path, "rb");
if (f) { size_t n = fread(m, 1, 4, f); fclose(f); if (n < 4) return DEC_MP3; }
if (m[0] == 'f' && m[1] == 'L' && m[2] == 'a' && m[3] == 'C') return DEC_FLAC;
return DEC_MP3;
}
int ncm_dec_open(ncm_decoder_t **out, const char *path,
int *rate, int *ch, int *bits, int64_t *total_frames)
{
if (!out || !path) return -1;
ncm_decoder_t *d = (ncm_decoder_t *)calloc(1, sizeof *d);
if (!d) return -1;
d->kind = sniff_kind(path);
snprintf(d->path, sizeof d->path, "%s", path);
if (d->kind == DEC_FLAC) {
d->flac = drflac_open_file(path, NULL);
if (!d->flac) { free(d); return -1; }
d->rate = (int)d->flac->sampleRate;
d->ch = (int)d->flac->channels;
/* 位宽映射(AUDIOD §4 归一交给 SDK/audiod):≤16 → S16;>16 → S32 满幅 */
d->bits = (d->flac->bitsPerSample <= 16) ? 16 : 32;
d->total_frames = (int64_t)d->flac->totalPCMFrameCount;
} else {
if (mp3dec_ex_open(&d->mp3, path, MP3D_SEEK_TO_BYTE) != 0) { free(d); return -1; }
d->bits = 16;
d->stash = (int16_t *)malloc(DEC_STASH_FRAMES * 2 * sizeof(int16_t));
if (!d->stash) { mp3dec_ex_close(&d->mp3); free(d); return -1; }
/* 预读首块:填 dec.info(hz/channels)+ 预热 stash */
size_t got = mp3dec_ex_read(&d->mp3, d->stash, DEC_STASH_FRAMES * 2);
d->stash_n = got;
d->rate = d->mp3.info.hz;
d->ch = d->mp3.info.channels;
if (d->rate <= 0 || d->ch <= 0) { /* 首块即失败/空流 */
mp3dec_ex_close(&d->mp3); free(d->stash); free(d); return -1;
}
d->total_frames = (d->ch > 0) ? (int64_t)(d->mp3.samples / (uint64_t)d->ch) : 0;
}
if (d->ch < 1 || d->ch > 2) { ncm_dec_close(d); return -1; } /* v1 只接 单/双声道 */
*out = d;
if (rate) *rate = d->rate;
if (ch) *ch = d->ch;
if (bits) *bits = d->bits;
if (total_frames) *total_frames = d->total_frames;
return 0;
}
int64_t ncm_dec_read(ncm_decoder_t *d, void *buf, int64_t cap_frames)
{
if (!d || !buf || cap_frames <= 0) return 0;
if (d->kind == DEC_FLAC) {
if (d->bits == 16)
return (int64_t)drflac_read_pcm_frames_s16(d->flac, (drflac_uint64)cap_frames,
(drflac_int16 *)buf);
return (int64_t)drflac_read_pcm_frames_s32(d->flac, (drflac_uint64)cap_frames,
(drflac_int32 *)buf);
}
/* mp3:先吐 stash,再直读 */
int16_t *out = (int16_t *)buf;
int64_t cap_samples = cap_frames * d->ch;
int64_t done = 0;
if (d->stash_off < d->stash_n) {
size_t left = d->stash_n - d->stash_off;
size_t take = left < (size_t)cap_samples ? left : (size_t)cap_samples;
memcpy(out, d->stash + d->stash_off, take * sizeof(int16_t));
d->stash_off += take;
done += (int64_t)take;
}
if (done < cap_samples) {
size_t got = mp3dec_ex_read(&d->mp3, out + done, (size_t)(cap_samples - done));
done += (int64_t)got;
}
return done / d->ch;
}
int ncm_dec_seek_ms(ncm_decoder_t *d, int64_t ms, int64_t file_bytes, int64_t dur_ms)
{
if (!d || ms < 0) return -1;
if (d->kind == DEC_FLAC) {
drflac_uint64 frame = (drflac_uint64)(ms * d->rate / 1000);
return drflac_seek_to_pcm_frame(d->flac, frame) ? 0 : -1;
}
/* mp3:关+重开(重建字节索引)再按字节近似 */
mp3dec_ex_close(&d->mp3);
memset(&d->mp3, 0, sizeof d->mp3);
if (mp3dec_ex_open(&d->mp3, d->path, MP3D_SEEK_TO_BYTE) != 0) return -1;
d->stash_n = d->stash_off = 0;
int64_t target = (file_bytes > 0 && dur_ms > 0) ? file_bytes * ms / dur_ms : 0;
return mp3dec_ex_seek(&d->mp3, (uint64_t)(target > 0 ? target : 0));
}
void ncm_dec_close(ncm_decoder_t *d)
{
if (!d) return;
if (d->kind == DEC_FLAC) { if (d->flac) drflac_close(d->flac); }
else { mp3dec_ex_close(&d->mp3); free(d->stash); }
free(d);
}
const char *ncm_dec_format(const ncm_decoder_t *d)
{
if (!d) return "?";
return d->kind == DEC_FLAC ? "flac" : "mp3";
}
+34
View File
@@ -0,0 +1,34 @@
/* play/decoder.h — 格式分发解码(REPORT §19):按文件 magic 选后端。
* "fLaC" → dr_flac(无损 SQ / Hi-Res,vendor/dr_flac.h)
* 其它(ID3 / 0xFF 帧同步 / 未知)→ minimp3(既有 mp3 路径,未知格式由 minimp3 兜底报错)
* 输出约定:bits<=16 → 交织 S16;bits>16 → 交织 S32(dr_flac s32 左对齐满幅,送 audiod
* AUDIO_FMT_S32,AUDIOD_DESIGN.md §4 格式归一由 SDK/audiod 完成,本层不做 SRC/裁剪)。
*/
#ifndef NCM_DECODER_H
#define NCM_DECODER_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ncm_decoder ncm_decoder_t;
/* 打开并探测流参数:rate/ch/bits/total_frames 经指针回填实际值(mp3 恒 16bit;
* total_frames 0=未知)。返回 0=成功。 */
int ncm_dec_open(ncm_decoder_t **out, const char *path,
int *rate, int *ch, int *bits, int64_t *total_frames);
/* 读最多 cap_frames 帧到 buf(交织,样本位宽=open 返回的 bits;cap 以帧计,
* buf 容量须 ≥ cap_frames*ch*(bits/8))。返回实际帧数,0=EOF。 */
int64_t ncm_dec_read(ncm_decoder_t *d, void *buf, int64_t cap_frames);
/* seek 到 ms。flac=精确 PCM 帧;mp3=按 file_bytes*ms/dur_ms 字节近似(同旧语义)。 */
int ncm_dec_seek_ms(ncm_decoder_t *d, int64_t ms, int64_t file_bytes, int64_t dur_ms);
void ncm_dec_close(ncm_decoder_t *d);
/* "mp3" / "flac"(日志/UI 用)。 */
const char *ncm_dec_format(const ncm_decoder_t *d);
#ifdef __cplusplus
}
#endif
#endif /* NCM_DECODER_H */
+7
View File
@@ -0,0 +1,7 @@
/* play/dr_flac_impl.c — dr_flac 唯一实现 TU(header-only:define 实现一次,与
* minimp3_impl.c 同款)。编译时 -I<vendor>/dr_flac;vendor 豁免告警(同 minimp3 口径)。
* DR_FLAC_NO_OGG:ogg 封装的 flac 网易云不会下发(song_url type=flac 恒原生 flac),
* 裁掉 ogg 容器代码省 text。 */
#define DR_FLAC_NO_OGG
#define DR_FLAC_IMPLEMENTATION
#include "dr_flac.h"
+66 -34
View File
@@ -3,9 +3,12 @@
*
* 模型:一个持久的播放线程。外层循环取「目标曲目」,run_song() 负责一首歌的
* 「下载(缓冲) → 解码 → 输出 → 结束/被指令打断」。暂停=冻结(停读停写停推 pos,状态置 PAUSED);
* seek=重开解码器+输出并 seek 到目标 ms(字节 offset 近似);下一/上一首=打断当前 run_song 返回外层。
* seek=seek 解码器+重开输出到目标 ms(flac 精确帧/mp3 字节 offset 近似);下一/上一首=打断当前 run_song 返回外层。
* 所有跨线程共享状态在 g_play (mu)。g_st.ctx 的 device_id/cookie/cache 在播放期间稳定(登出会先停播)。
* 返回码/错误经 g_play.err/errmsg 供 UI 提示。
* §19:解码经 decoder.c 按 magic 分发(fLaC→dr_flac / 其它→minimp3);rate/ch/bits 按实际流
* 给 audio_out(flac ≤16bit→S16,>16bit→S32;audiod 格式归一/DAC 原生率跟随在服务端);
* 音频缓存键 = 歌曲ID+音质档位(用户定案 NCM_CACHE_DESIGN §2.5),旧无档位键温和兼容。
*/
#include "../ncm_app_internal.h"
#include <stdio.h>
@@ -13,15 +16,15 @@
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "minimp3_ex.h"
#include "decoder.h"
static pthread_t g_thr;
static int g_thr_on;
static int g_want_run = 1; /* 线程运行标志(quit=0) */
#define PCM_CHUNK 8192 /* 每块交织样本数(双声道 → 4096 帧) */
#define PCM_CHUNK_FRAMES 4096 /* 每块帧数(≈旧 8192 样本/双声道) */
/* ---- 线程内部一次性歌的状态 ---- */
static char g_path[512]; /* 本地 mp3 路径(缓存) */
static char g_path[512]; /* 本地音频路径(缓存,mp3/flac) */
static int64_t g_file_bytes;
static int g_level; /* 音质档(来自设置) */
static int g_cache_gb;
@@ -142,21 +145,42 @@ static int download_cb(void *ctx, const unsigned char *data, size_t len)
return ncm_cache_write((ncm_cache_writer_t *)ctx, data, len);
}
/* 得到本地 mp3 路径 + 文件大小。命中缓存直接返回;否则 URL 降级 + 流式落缓存。
/* 缓存键(用户定案 NCM_CACHE_DESIGN §2.5):歌曲ID + 音质档位 → "song_<id>_<level>.<ext>"。
* 同歌同档命中不重复下载;换档=新条目,旧档留给 LRU 自然淘汰。 */
static void audio_cache_key(char *out, size_t sz, const char *id, int level)
{
snprintf(out, sz, "%s_%s", id, ncm_api_level_str(level));
}
/* 得到本地音频路径 + 文件大小。命中缓存直接返回;否则 URL 降级 + 流式落缓存。
* 返回 NCM_OK 或负错误码。id 由调用方(run_song)从播放曲快照取,与窗口解耦(§17)。 */
static int song_local_file(const char *id, char *path, size_t psz, int64_t *bytes)
{
/* 缓存命中 → 离线直接播 */
if (ncm_cache_lookup(&g_st.ctx.cache, id, ".mp3", path, psz) == NCM_OK) {
char key[64];
audio_cache_key(key, sizeof key, id, g_level);
/* 缓存命中 → 离线直接播(新键两扩展名都试;档位=用户设定档,降级产物也归该档键) */
if (ncm_cache_lookup(&g_st.ctx.cache, key, "flac", path, psz) == NCM_OK ||
ncm_cache_lookup(&g_st.ctx.cache, key, "mp3", path, psz) == NCM_OK) {
struct stat st;
if (stat(path, &st) == 0) { *bytes = (int64_t)st.st_size; return NCM_OK; }
}
/* 旧键温和兼容(§19 前存量:无档位后缀的 song_<id>[.]mp3,含历史双点名):命中即播,
* 不主动改名/删除,由 LRU 自然淘汰。 */
if (ncm_cache_lookup(&g_st.ctx.cache, id, "mp3", path, psz) == NCM_OK) {
struct stat st;
if (stat(path, &st) == 0) { *bytes = (int64_t)st.st_size; return NCM_OK; }
}
/* 播放地址:从 g_level 降级,只收 mp3 */
/* 播放地址:从 g_level 降级,接受 mp3 与 flac(§19:dr_flac 落地后无损/Hi-Res 真实生效) */
ncm_play_url_t u;
int rc = ncm_api_song_url_auto(g_st.ctx.device_id, g_st.ctx.cookie, id, g_level, &u);
if (rc != NCM_OK) return rc;
if (!u.url[0]) return NCM_ERR_NOTFOUND;
/* 落盘扩展名:type 权威;type 缺失时看 url 扩展名,默认 mp3 */
const char *ext = "mp3";
if (ncm_streq(u.type, "flac")) ext = "flac";
else if (!u.type[0] && strstr(u.url, ".flac")) ext = "flac";
pthread_mutex_lock(&g_play.mu);
g_play.dl_total = (u.size > 0) ? u.size : 0; /* 缓冲进度% 分母(0=未知只转菊花) */
pthread_mutex_unlock(&g_play.mu);
@@ -166,7 +190,7 @@ static int song_local_file(const char *id, char *path, size_t psz, int64_t *byte
/* 流式下载到缓存 temp → commit(稳妥者:先落盘再播) */
ncm_cache_writer_t *w = NULL;
rc = ncm_cache_open(&g_st.ctx.cache, id, ".mp3", &w);
rc = ncm_cache_open(&g_st.ctx.cache, key, ext, &w);
if (rc != NCM_OK) return rc;
ncm_http_req_t req;
@@ -179,7 +203,10 @@ static int song_local_file(const char *id, char *path, size_t psz, int64_t *byte
req.timeout_ms = 30000;
ncm_http_resp_t resp;
size_t maxb = (u.size > 0 && u.size < (64LL << 20)) ? (size_t)u.size : (64u << 20);
/* 上限:Hi-Res flac 单曲可达数百 MB(192k/24bit 5min ≈345MB),flac 上限 1GB;mp3 维持 64MB。
* size 可信时按 size 限幅(防超额),未知按格式默认上限。 */
size_t cap = (ext[0] == 'f') ? (1024u << 20) : (64u << 20);
size_t maxb = (u.size > 0 && (uint64_t)u.size < cap) ? (size_t)u.size : cap;
rc = ncm_http_download(&req, download_cb, w, maxb, &resp);
if (rc != NCM_OK || resp.status != 200) {
ncm_cache_abort(w);
@@ -189,7 +216,7 @@ static int song_local_file(const char *id, char *path, size_t psz, int64_t *byte
if (rc != 0) return rc;
/* 写后检查:新落盘可能把池推过上限 → 再按 LRU 淘汰最旧(刚写的 mtime 最新,存活) */
ncm_cache_evict(&g_st.ctx.cache);
if (ncm_cache_lookup(&g_st.ctx.cache, id, ".mp3", path, psz) != NCM_OK)
if (ncm_cache_lookup(&g_st.ctx.cache, key, ext, path, psz) != NCM_OK)
return NCM_ERR_NOENT;
struct stat st;
if (stat(path, &st) == 0) *bytes = (int64_t)st.st_size;
@@ -247,12 +274,19 @@ static void run_song(int idx)
int64_t dur = s.duration_ms > 0 ? s.duration_ms : 0;
mp3dec_ex_t dec;
memset(&dec, 0, sizeof dec);
if (mp3dec_ex_open(&dec, path, MP3D_SEEK_TO_BYTE) != 0) { set_err(NCM_ERR_IO, "decode open failed"); return; }
/* 解码分发(§19):magic 探测 fLaC→dr_flac / 其它→minimp3;rate/ch/bits 按实际流。 */
ncm_decoder_t *dec = NULL;
int rate = 0, ch = 0, bits = 0;
int64_t total_frames = 0;
if (ncm_dec_open(&dec, path, &rate, &ch, &bits, &total_frames) != 0) {
set_err(NCM_ERR_IO, "decode open failed"); return;
}
if (dur <= 0 && total_frames > 0 && rate > 0) dur = total_frames * 1000 / rate;
/* 音频输出:解码器输出 S16_LE(minimp3 默认);用 44100/2ch 直开 */
if (audio_out_open(44100, 2, 16) != 0) { mp3dec_ex_close(&dec); set_err(NCM_ERR_IO, "audio open failed"); return; }
/* 音频输出:按实际流参数开(flac ≤16bit→S16,>16bit→S32;audiod 侧格式归一+原生率跟随) */
if (audio_out_open(rate, ch, bits) != 0) {
ncm_dec_close(dec); set_err(NCM_ERR_IO, "audio open failed"); return;
}
/* 状态→播放 */
pthread_mutex_lock(&g_play.mu);
@@ -261,9 +295,11 @@ static void run_song(int idx)
g_play.dur_ms = dur;
g_play.err = 0;
pthread_mutex_unlock(&g_play.mu);
meow_log(MEOW_LOG_INFO, "play: start idx=%d id=%s dur=%lldms", idx, s.id, (long long)dur);
meow_log(MEOW_LOG_INFO, "play: start idx=%d id=%s fmt=%s %dHz/%dch/%dbit dur=%lldms",
idx, s.id, ncm_dec_format(dec), rate, ch, bits, (long long)dur);
mp3d_sample_t pcm[PCM_CHUNK];
/* 单块缓冲:4096 帧 × ≤2ch × 4B(S32 上限)= 32KB,静态(播放线程单实例,省 32KB 栈)。 */
static int32_t pcm[PCM_CHUNK_FRAMES * 2];
int64_t frames_done = 0; /* 已输出帧(每通道) */
int need_seek_ms = -1;
int paused_edge = 0; /* 暂停沿:进暂停已向 audiod 发 pause */
@@ -288,7 +324,7 @@ static void run_song(int idx)
if (!paused_edge) { /* 进暂停:停 PCM 时钟但不丢流(audiod MSG_AUDIO_PAUSE) */
paused_edge = 1;
meow_log(MEOW_LOG_INFO, "play: paused idx=%d pos=%lldms", idx,
(long long)(frames_done * 1000 / 44100));
rate > 0 ? (long long)(frames_done * 1000 / rate) : 0);
audio_out_pause(1);
}
usleep(40 * 1000);
@@ -301,36 +337,32 @@ static void run_song(int idx)
}
if (need_seek_ms >= 0) {
/* 重开并 seek:关闭后重开(字节 offset 近似) */
/* seek:flac 精确帧 / mp3 关+重开按字节近似(decoder 内处理);输出重开 */
meow_log(MEOW_LOG_INFO, "play: seek idx=%d -> %dms", idx, need_seek_ms);
audio_out_close();
mp3dec_ex_close(&dec);
memset(&dec, 0, sizeof dec);
if (mp3dec_ex_open(&dec, path, MP3D_SEEK_TO_BYTE) != 0) { set_err(NCM_ERR_IO, "seek reopen failed"); break; }
if (audio_out_open(44100, 2, 16) != 0) { set_err(NCM_ERR_IO, "audio reopen failed"); break; }
int64_t target = (g_file_bytes > 0 && dur > 0)
? (int64_t)g_file_bytes * need_seek_ms / dur : 0;
mp3dec_ex_seek(&dec, (uint64_t)(target > 0 ? target : 0));
frames_done = (int64_t)need_seek_ms * 44100 / 1000;
if (ncm_dec_seek_ms(dec, need_seek_ms, g_file_bytes, dur) != 0) {
set_err(NCM_ERR_IO, "seek failed"); break;
}
if (audio_out_open(rate, ch, bits) != 0) { set_err(NCM_ERR_IO, "audio reopen failed"); break; }
frames_done = (int64_t)need_seek_ms * rate / 1000;
need_seek_ms = -1;
continue;
}
size_t got = mp3dec_ex_read(&dec, pcm, PCM_CHUNK);
if (got == 0) { /* 播完 */
int64_t frames = ncm_dec_read(dec, pcm, PCM_CHUNK_FRAMES);
if (frames == 0) { /* 播完 */
meow_log(MEOW_LOG_INFO, "play: done idx=%d frames=%lld", idx, (long long)frames_done);
break;
}
int frames = (int)(got / 2);
/* 写失败(流死/audiod 断链):置错停播——旧版静默 break 会被外层当「自然播完」
* 自动顺延下一首,流死时连环跳歌并狂发 song_url 请求(§18 修)。 */
if (audio_out_write(pcm, frames) != frames) {
if (audio_out_write(pcm, (int)frames) != (int)frames) {
if (!g_app_quit) set_err(NCM_ERR_IO, "audio write failed");
break;
}
frames_done += frames;
/* 更新进度(~200ms 节流)*/
int64_t pos = frames_done * 1000 / 44100;
int64_t pos = rate > 0 ? frames_done * 1000 / rate : 0;
pthread_mutex_lock(&g_play.mu);
g_play.pos_ms = pos;
if (dur > 0 && pos >= dur) g_play.pos_ms = dur;
@@ -338,7 +370,7 @@ static void run_song(int idx)
}
audio_out_close();
mp3dec_ex_close(&dec);
ncm_dec_close(dec);
/* 若应继续播下一首且非因命令打断,由外层处理(外层见 cur==idx 且需自然结束) */
}
+53 -30
View File
@@ -1,13 +1,26 @@
/* test/test_decode.c — 播放解码链路验证(宿主):minimp3 解码 mp3 → audio_out(FILE sink) 写 WAV。
* 不依赖网络/登录(输入本地 mp3)。验证:解码无错、WAV 非空、时长合理、RT 因子。
* 运行:NCM_AUDIO_SINK=file NCM_AUDIO_OUT=/tmp/ncm_decode.wav ./test_decode <path.mp3>
/* test/test_decode.c — 播放解码链路验证(宿主/mipsel+qemu):decoder(magic 分发
* dr_flac/minimp3,§19)→ audio_out(FILE sink) 写 WAV。不依赖网络/登录(输入本地文件)。
* 验证:格式探测正确、rate/ch/bits 按实际流、解码无错、WAV 非空、帧数/时长对、RT 因子。
*
* 运行:NCM_AUDIO_SINK=file NCM_AUDIO_OUT=/tmp/ncm_decode.wav \
* ./test_decode <path> [expect_frames] [expect_rate] [expect_bits]
* expect_* 给定时校验(不一致 exit 1);不给则只要求 frames>0。
*
* 编译(宿主,ncm_app 目录下):
* gcc -O2 -Wall -Wextra -DJSMN_HEADER -I. -Icore -I../sdk -I../meow_launcherd \
* -Ivendor/minimp3 -Ivendor/dr_flac \
* app/test/test_decode.c app/play/decoder.c app/play/audio_out.c \
* ../sdk/meow_ui_audio.c ../sdk/meow_ui_log.c -o /tmp/td.o-chain # 见下两行豁免 TU
* gcc -O2 -Ivendor/minimp3 -c app/play/minimp3_impl.c -o /tmp/td_mm.o # vendor 豁免
* gcc -O2 -Ivendor/dr_flac -c app/play/dr_flac_impl.c -o /tmp/td_df.o # vendor 豁免
* (首行改为 -c 逐个或把两个豁免 .o 一并链接:gcc ... -o out/test_decode /tmp/td_mm.o /tmp/td_df.o -lpthread -lm)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../ncm_app_internal.h" /* 引 audio_out / minimp3 声明 */
#include "minimp3_ex.h"
#include "../ncm_app_internal.h" /* audio_out 声明 + 全局态 */
#include "../play/decoder.h"
ncm_app_state_t g_st;
ncm_play_run_t g_play = { .mu = PTHREAD_MUTEX_INITIALIZER };
@@ -15,6 +28,14 @@ volatile int g_app_quit = 0;
volatile int g_net_ready = 0;
int g_host_mock = 1;
/* SDK meow_ui_log.c 需要(本测试不链 meow_ui_sys.c,同 audiod_chain_sim 口径打桩) */
uint64_t meow_ui_time_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}
static double now_s(void)
{
struct timespec ts;
@@ -25,43 +46,45 @@ static double now_s(void)
int main(int argc, char **argv)
{
const char *path = (argc > 1) ? argv[1] : "vendor/minimp3/vectors/performance/MIPSTest.mp3";
mp3dec_ex_t dec;
memset(&dec, 0, sizeof dec);
if (mp3dec_ex_open(&dec, path, MP3D_SEEK_TO_BYTE) != 0) { fprintf(stderr, "open failed\n"); return 1; }
int64_t exp_frames = (argc > 2) ? atoll(argv[2]) : -1;
int exp_rate = (argc > 3) ? atoi(argv[3]) : -1;
int exp_bits = (argc > 4) ? atoi(argv[4]) : -1;
static mp3d_sample_t pcm[8192 * 2];
/* 读首帧确定采样率/声道(read 会填 dec.info)*/
size_t got = mp3dec_ex_read(&dec, pcm, 8192 * 2);
int sr = dec.info.hz;
int ch = dec.info.channels;
if (sr <= 0 || ch <= 0) { fprintf(stderr, "bad stream\n"); return 1; }
printf("mp3: %s %dHz x%d detected_samples=%lld bitrate=%dkbps\n",
path, sr, ch, (long long)dec.samples, dec.info.bitrate_kbps);
if (audio_out_open(sr, ch, 16) != 0) { fprintf(stderr, "audio_out open failed\n"); return 1; }
ncm_decoder_t *dec = NULL;
int rate = 0, ch = 0, bits = 0;
int64_t total_frames = 0;
if (ncm_dec_open(&dec, path, &rate, &ch, &bits, &total_frames) != 0) {
fprintf(stderr, "open failed\n"); return 1;
}
printf("%s: %s %dHz x%d %dbit total_frames=%lld\n",
path, ncm_dec_format(dec), rate, ch, bits, (long long)total_frames);
if (exp_rate > 0 && rate != exp_rate) { fprintf(stderr, "rate mismatch %d != %d\n", rate, exp_rate); return 1; }
if (exp_bits > 0 && bits != exp_bits) { fprintf(stderr, "bits mismatch %d != %d\n", bits, exp_bits); return 1; }
if (audio_out_open(rate, ch, bits) != 0) { fprintf(stderr, "audio_out open failed\n"); return 1; }
static int32_t pcm[4096 * 2];
long long frames = 0;
double t0 = now_s();
if (got > 0) {
int fr = (int)(got / (size_t)ch);
if (audio_out_write(pcm, fr) != fr) { fprintf(stderr, "write failed\n"); return 1; }
frames += fr;
}
for (;;) {
if (g_app_quit) break;
got = mp3dec_ex_read(&dec, pcm, 8192 * 2);
int64_t got = ncm_dec_read(dec, pcm, 4096);
if (got == 0) break;
int fr = (int)(got / (size_t)ch);
if (audio_out_write(pcm, fr) != fr) { fprintf(stderr, "write failed\n"); break; }
frames += fr;
if (audio_out_write(pcm, (int)got) != (int)got) { fprintf(stderr, "write failed\n"); break; }
frames += got;
}
double dt = now_s() - t0;
audio_out_close();
mp3dec_ex_close(&dec);
ncm_dec_close(dec);
double secs = (double)frames / (double)sr;
double secs = rate > 0 ? (double)frames / (double)rate : 0;
double rt = (dt > 0) ? secs / dt : 0;
printf("decoded %lld frames = %.2f s audio (%d Hz) in %.3f s => %.1fx RT\n",
frames, secs, sr, dt, rt);
printf("decoded %lld frames = %.3f s audio (%d Hz) in %.3f s => %.1fx RT\n",
frames, secs, rate, dt, rt);
if (exp_frames >= 0 && frames != exp_frames) {
fprintf(stderr, "frames mismatch %lld != %lld\n", frames, (long long)exp_frames);
return 1;
}
printf("OK: %s\n", (frames > 0) ? "decode chain works" : "EMPTY");
return (frames > 0) ? 0 : 1;
}