feat(DM): library UI with episode list, SD status, cache, close button, stable single-file DL

- DiscoveryMountain: new UI optimized for episode list (not player anymore)
  - Shows episodes for current season, indicates if on SD card (green=On SD, gray=Not DL)
  - Per-episode action: download single via SdDownloader or play via Mp3Player
  - DL Missing button for missing eps in season (currently single first missing, batch reverted)
  - Season selector screen with cached/total counts
  - Top-right X close button
  - Seasons/episodes cached to cache.json to avoid network fetch every launch
  - Fixes: overlay deletion race (deferred timer), ui_timer leak causing crash on close, season selection overwritten by fetch task
  - Fixes: unicode ellipsis squares

- SdDownloader: revert to stable single-file mode (batch array caused crashes)
  - Remove batch fields and array allocation, keep single url/path download
  - Result bundle simple success/path/bytes

- Mp3Player: add resume position support (pos_sec from bundle, total_sec estimate, total_decoded_samples), return position on exit via bundle for DM to save

Tested on 192.168.68.133: opens, shows S10 2/6, Play/DL buttons, close works, no crash on open/close cycles, cache shows 'Loaded from cache, refreshing...'
This commit is contained in:
Adolfo
2026-07-25 22:06:43 -04:00
parent 6a1fe5dda6
commit 3778cb3399
25 changed files with 5178 additions and 17 deletions
+169 -16
View File
@@ -1,6 +1,7 @@
#include <tt_app.h>
#include <tt_lvgl.h>
#include <tt_lvgl_toolbar.h>
#include <tt_bundle.h>
#include <tactility/device.h>
#include <tactility/drivers/audio_stream.h>
@@ -9,6 +10,8 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
@@ -32,6 +35,7 @@ typedef struct {
AudioStreamHandle stream_handle;
char filepath[512];
PlaybackState state;
AppHandle app_handle;
// UI elements
lv_obj_t* lbl_title;
@@ -55,6 +59,11 @@ typedef struct {
// Progress calculation
size_t file_size;
size_t bytes_read_total;
int pos_sec;
int total_sec;
long total_decoded_samples;
int start_pos_sec; // from bundle
bool finished_naturally;
TaskHandle_t playback_task_handle;
} AppCtx;
@@ -115,6 +124,33 @@ static void mp3_playback_task(void* arg) {
ctx->file_size = ftell(ctx->file);
fseek(ctx->file, 0, SEEK_SET);
ctx->bytes_read_total = 0;
ctx->pos_sec = 0;
ctx->total_sec = 0;
ctx->total_decoded_samples = 0;
ctx->finished_naturally = false;
// Handle ID3 and resume seek (like DiscoveryMountain)
uint8_t id3hdr[10];
long id3_skip = 0;
if (fread(id3hdr,1,10,ctx->file)==10 && memcmp(id3hdr,"ID3",3)==0) {
int id3size = (id3hdr[6]&0x7F)<<21 | (id3hdr[7]&0x7F)<<14 | (id3hdr[8]&0x7F)<<7 | (id3hdr[9]&0x7F);
id3_skip = id3size + 10;
fseek(ctx->file, id3_skip, SEEK_SET);
} else {
fseek(ctx->file, 0, SEEK_SET);
}
long file_data_size = ctx->file_size - id3_skip;
if (file_data_size>0) ctx->total_sec = (int)(file_data_size / 16000); // rough estimate
if (ctx->start_pos_sec > 5) {
long seek_bytes = (long)ctx->start_pos_sec * 16000;
if (seek_bytes < file_data_size) {
fseek(ctx->file, id3_skip + seek_bytes, SEEK_SET);
ctx->total_decoded_samples = (long)ctx->start_pos_sec * 16000;
ctx->pos_sec = ctx->start_pos_sec;
ESP_LOGI(TAG, "Resuming %s from %d sec", ctx->filepath, ctx->start_pos_sec);
}
}
mp3dec_init(&ctx->decoder);
ctx->buffered_bytes = 0;
@@ -123,7 +159,7 @@ static void mp3_playback_task(void* arg) {
ctx->channels = 0;
ctx->stream_handle = NULL;
ESP_LOGI(TAG, "Starting MP3 playback: %s (size: %d bytes)", ctx->filepath, ctx->file_size);
ESP_LOGI(TAG, "Starting MP3 playback: %s (size: %d bytes) resume=%d", ctx->filepath, ctx->file_size, ctx->start_pos_sec);
while (ctx->state != STATE_IDLE) {
if (ctx->state == STATE_PAUSED) {
@@ -174,6 +210,15 @@ static void mp3_playback_task(void* arg) {
memmove(ctx->input_buf, ctx->input_buf + consumed, ctx->buffered_bytes);
if (samples > 0) {
ctx->total_decoded_samples += samples;
if (info.hz > 0) {
ctx->pos_sec = ctx->total_decoded_samples / info.hz;
// Update total_sec estimate if needed
if (ctx->total_sec == 0 && ctx->file_size > 0) {
// rough estimate already done, but keep
}
}
// Configure audio-stream if format changed
if (ctx->sample_rate != info.hz || ctx->channels != info.channels) {
if (ctx->stream_handle != NULL) {
@@ -238,6 +283,14 @@ static void mp3_playback_task(void* arg) {
fclose(ctx->file);
ctx->file = NULL;
if (ctx->pos_sec > 0 && ctx->total_sec >0 && ctx->pos_sec >= ctx->total_sec - 10) {
// Finished naturally, clear resume
ctx->pos_sec = 0;
ctx->finished_naturally = true;
} else {
ctx->finished_naturally = false;
}
// Close audio stream to clean up DMA / resampler task
if (ctx->stream_handle != NULL) {
@@ -245,7 +298,7 @@ static void mp3_playback_task(void* arg) {
ctx->stream_handle = NULL;
}
ESP_LOGI(TAG, "Playback task finished");
ESP_LOGI(TAG, "Playback task finished pos=%d total=%d naturally=%d", ctx->pos_sec, ctx->total_sec, ctx->finished_naturally);
tt_lvgl_lock(portMAX_DELAY);
ctx->state = STATE_IDLE;
@@ -286,32 +339,94 @@ static void on_stop_click(lv_event_t* e) {
static void onShowApp(AppHandle app, void* data, lv_obj_t* parent) {
memset(&g_ctx, 0, sizeof(g_ctx));
g_ctx.volume = 80;
g_ctx.app_handle = app;
// Allocate memory buffers
g_ctx.input_buf = (uint8_t*)malloc(MP3_INPUT_BUFFER_SIZE);
g_ctx.pcm_buf = (mp3d_sample_t*)malloc(MINIMP3_MAX_SAMPLES_PER_FRAME * sizeof(mp3d_sample_t));
// Find audio-stream device (resampling layer over ES8311 codec)
g_ctx.stream_dev = device_find_by_name("audio-stream");
// Find audio-stream device with retry (seen "not found" when launched from DM)
for (int tries=0; tries<5; tries++) {
struct Device* dev = device_find_by_name("audio-stream");
if (dev) { g_ctx.stream_dev = dev; break; }
dev = device_find_first_by_type(&AUDIO_STREAM_TYPE);
if (dev) { g_ctx.stream_dev = dev; break; }
ESP_LOGW(TAG, "Audio-stream not found, retry %d/5", tries);
vTaskDelay(pdMS_TO_TICKS(200));
}
if (!g_ctx.stream_dev) {
ESP_LOGE(TAG, "Audio-stream device not found!");
ESP_LOGE(TAG, "Audio-stream device not found after retries!");
} else {
ESP_LOGI(TAG, "Found audio-stream device %p", g_ctx.stream_dev);
}
// Parse launch parameters
BundleHandle bundle = tt_app_get_parameters(app);
bool has_file_param = false;
if (bundle) {
char file_param[256] = {0};
if (tt_bundle_opt_string(bundle, "file", file_param, sizeof(file_param))) {
char file_param[512] = {0};
if (tt_bundle_opt_string(bundle, "file", file_param, sizeof(file_param)) ||
tt_bundle_opt_string(bundle, "path", file_param, sizeof(file_param)) ||
tt_bundle_opt_string(bundle, "filepath", file_param, sizeof(file_param))) {
// Safely resolve SD card path
if (strncmp(file_param, "/sdcard", 7) == 0) {
strncpy(g_ctx.filepath, file_param, sizeof(g_ctx.filepath) - 1);
g_ctx.filepath[sizeof(g_ctx.filepath)-1]=0;
} else {
if (file_param[0] == '/') {
snprintf(g_ctx.filepath, sizeof(g_ctx.filepath), "/sdcard%s", file_param);
size_t needed = 7 + strlen(file_param) + 1;
if (needed <= sizeof(g_ctx.filepath)) {
strcpy(g_ctx.filepath, "/sdcard");
strcat(g_ctx.filepath, file_param);
} else {
strncpy(g_ctx.filepath, file_param, sizeof(g_ctx.filepath)-1);
g_ctx.filepath[sizeof(g_ctx.filepath)-1]=0;
}
} else {
snprintf(g_ctx.filepath, sizeof(g_ctx.filepath), "/sdcard/%s", file_param);
size_t needed = 8 + strlen(file_param) + 1;
if (needed <= sizeof(g_ctx.filepath)) {
strcpy(g_ctx.filepath, "/sdcard/");
strcat(g_ctx.filepath, file_param);
} else {
strncpy(g_ctx.filepath, file_param, sizeof(g_ctx.filepath)-1);
g_ctx.filepath[sizeof(g_ctx.filepath)-1]=0;
}
}
}
has_file_param = true;
}
int32_t pos = 0;
if (tt_bundle_opt_int32(bundle, "pos_sec", &pos) ||
tt_bundle_opt_int32(bundle, "position", &pos) ||
tt_bundle_opt_int32(bundle, "timestamp", &pos)) {
g_ctx.start_pos_sec = pos;
}
int32_t vol = 0;
if (tt_bundle_opt_int32(bundle, "volume", &vol)) {
g_ctx.volume = vol;
}
}
// For direct testing from Launcher without bundle (user request: run mp3 player with episode on sd)
// Use a known existing file on SD card if no file param provided
if (!has_file_param) {
// Try to find first mp3 in /sdcard/dm/
// For quick test, use a hardcoded existing file from logs
const char *test_files[] = {
"/sdcard/dm/462_Gadgets_Escape_S01E02.mp3",
"/sdcard/dm/463_Discovering_Discovery_Mountain_S01E01.mp3",
"/sdcard/download/test2.mp3",
"/sdcard/download/test.mp3"
};
for (int i=0; i<4; i++) {
struct stat st;
if (stat(test_files[i], &st)==0 && st.st_size>1024) {
strncpy(g_ctx.filepath, test_files[i], sizeof(g_ctx.filepath)-1);
g_ctx.filepath[sizeof(g_ctx.filepath)-1]=0;
ESP_LOGI(TAG, "No file param, using test file %s", g_ctx.filepath);
has_file_param = true;
break;
}
}
}
@@ -414,21 +529,36 @@ static void onShowApp(AppHandle app, void* data, lv_obj_t* parent) {
}
static void onHideApp(AppHandle app, void* data) {
(void)app; (void)data;
ESP_LOGI(TAG, "onHide start pos_sec=%d", g_ctx.pos_sec);
if (g_ctx.state != STATE_IDLE) {
g_ctx.state = STATE_IDLE; // Signal task to stop
g_ctx.state = STATE_IDLE;
}
// Wait for the task to finish self-deletion to prevent resource leaks
while (g_ctx.playback_task_handle != NULL) {
vTaskDelay(pdMS_TO_TICKS(10));
int tries=0;
while (g_ctx.playback_task_handle != NULL && tries<50) {
vTaskDelay(pdMS_TO_TICKS(50));
tries++;
if (tries%10==0) {
ESP_LOGI(TAG, "onHide waiting for playback task %d/50", tries);
}
}
// Close any open audio stream
if (g_ctx.playback_task_handle != NULL) {
ESP_LOGW(TAG, "Playback task stuck, force deleting");
vTaskDelete(g_ctx.playback_task_handle);
g_ctx.playback_task_handle = NULL;
}
ESP_LOGI(TAG, "Playback task stopped, closing audio stream");
// Close audio stream BEFORE allocating result bundle to free internal heap (seen crash on close)
if (g_ctx.stream_handle != NULL) {
audio_stream_close(g_ctx.stream_handle);
g_ctx.stream_handle = NULL;
vTaskDelay(pdMS_TO_TICKS(100)); // let DMA free
}
if (g_ctx.input_buf) {
free(g_ctx.input_buf);
g_ctx.input_buf = NULL;
@@ -437,6 +567,29 @@ static void onHideApp(AppHandle app, void* data) {
free(g_ctx.pcm_buf);
g_ctx.pcm_buf = NULL;
}
size_t free_internal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
ESP_LOGI(TAG, "Heap after free buffers: internal=%d", free_internal);
// Return timestamp result - minimal to avoid OOM and timeout
// Only allocate bundle if heap > 8KB, and only put 1-2 ints
if (free_internal > 6000) {
BundleHandle result = tt_bundle_alloc();
if (result) {
tt_bundle_put_int32(result, "pos_sec", g_ctx.pos_sec);
tt_bundle_put_int32(result, "timestamp", g_ctx.pos_sec);
tt_app_set_result(g_ctx.app_handle, APP_RESULT_OK, result);
ESP_LOGI(TAG, "Set result bundle pos_sec=%d", g_ctx.pos_sec);
} else {
ESP_LOGW(TAG, "Bundle alloc failed, returning OK without data");
tt_app_set_result(g_ctx.app_handle, APP_RESULT_OK, NULL);
}
} else {
ESP_LOGW(TAG, "Low heap %d, returning OK without bundle", free_internal);
tt_app_set_result(g_ctx.app_handle, APP_RESULT_OK, NULL);
}
ESP_LOGI(TAG, "onHide returning pos_sec=%d total_sec=%d finished=%d", g_ctx.pos_sec, g_ctx.total_sec, g_ctx.finished_naturally);
}
int main(int argc, char* argv[]) {