aboutsummaryrefslogtreecommitdiff
path: root/patches/0011-yp3-stop-a-long-book-s-load-at-once-for-USB.patch
blob: 8fabc8fd304bd471263b347f3df7e0fa5f4cf36a (plain)
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
From a79946f739128362227ab71864ada22a9908ea52 Mon Sep 17 00:00:00 2001
From: YP3 Audiobook <noreply@example.invalid>
Date: Fri, 27 Nov 2026 00:31:28 +0000
Subject: [PATCH 11/11] yp3: stop a long book's load at once for USB

Plugging in USB while a long M4B loaded (also the flasher's reboot
into Rockbox, which resumes the book) left the computer without the
player's disk: USB storage needs playback stopped, and the stop waited
for the codec, which takes commands only once the index is read
(~6 s). The computer gave up on the unresponsive device and the
flasher reported "NO USB DISK".

load_progress now returns whether to go on: false when a stop is
waiting in the codec queue, and libm4a then ends the load at once with
an error. Seeks and pauses still wait for the load, as before.

---
 apps/codec_thread.c               |  7 ++++++-
 lib/rbcodec/codecs/codecs.h       | 10 ++++++----
 lib/rbcodec/codecs/libm4a/demux.c | 19 +++++++++++++------
 3 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/apps/codec_thread.c b/apps/codec_thread.c
index fc6eeecf..90c76192 100644
--- a/apps/codec_thread.c
+++ b/apps/codec_thread.c
@@ -591,11 +591,16 @@ static void load_codec(const struct codec_load_info *ev_data)
 static volatile int load_percent = -1;
 static long load_start_tick;
 
-static void codec_load_progress_callback(int percent)
+static bool codec_load_progress_callback(int percent)
 {
     if (load_percent < 0)
         load_start_tick = current_tick;
     load_percent = percent >= 100 ? -1 : percent;
+
+    /* Stop waiting (audio_stop_playback(): USB, another track): end the
+       load rather than make it wait for the whole index */
+    struct queue_event ev;
+    return !(queue_peek(&codec_queue, &ev) && ev.id == Q_CODEC_STOP);
 }
 
 /* A track start is reading its index (load_progress), however briefly */
diff --git a/lib/rbcodec/codecs/codecs.h b/lib/rbcodec/codecs/codecs.h
index 28f5a0f8..32568ad9 100644
--- a/lib/rbcodec/codecs/codecs.h
+++ b/lib/rbcodec/codecs/codecs.h
@@ -89,9 +89,9 @@
  * any new functions "waiting" at the end of the list.
  */
 #ifdef HAVE_CODEC_EXTRA_BUFFER
-#define CODEC_API_VERSION 52 /* load_progress */
+#define CODEC_API_VERSION 53 /* load_progress returns bool */
 #else
-#define CODEC_API_VERSION 51 /* load_progress */
+#define CODEC_API_VERSION 52 /* load_progress returns bool */
 #endif
 
 /* reasons for calling codec main entrypoint */
@@ -243,8 +243,10 @@ struct codec_api {
     void *(*codec_get_extra_buffer)(size_t *size);
 #endif
     /* How far a slow track start (a long M4B's index) has got, 0-100; the
-       loading screen shows it (codec_load_progress()) */
-    void (*load_progress)(int percent);
+       loading screen shows it (codec_load_progress()). Called often (each
+       block read): false means the codec is to stop, so the load should
+       end now (return an error) */
+    bool (*load_progress)(int percent);
 };
 
 /* codec header */
diff --git a/lib/rbcodec/codecs/libm4a/demux.c b/lib/rbcodec/codecs/libm4a/demux.c
index c4cfd92a..b4c08234 100644
--- a/lib/rbcodec/codecs/libm4a/demux.c
+++ b/lib/rbcodec/codecs/libm4a/demux.c
@@ -447,7 +447,10 @@ static bool read_chunk_stsc(qtmovie_t *qtmovie, size_t chunk_len)
  * the two tables took ~280k calls (a 4.5 h M4B), seconds of CPU that never
  * yielded: the UI, at the codec's priority and never preempted, froze until
  * the book had loaded. Now one call per block, a yield after each, and the
- * progress reported (ci->load_progress) for the loading screen. */
+ * progress reported (ci->load_progress) for the loading screen. A stop
+ * (USB, another book) ends the load at the next block: the codec cannot
+ * take commands until it returns, and USB waited ~6 s, long enough for
+ * the computer to give up on the player's disk. */
 #define TABLE_BLOCK_BYTES 1024
 
 static struct table_reader
@@ -457,6 +460,7 @@ static struct table_reader
     unsigned len, i;            /* bytes in buf, next to use */
     uint32_t done, total;       /* table bytes, both tables: progress */
     int percent;
+    bool stop;                  /* load_progress said stop: read no more */
     unsigned char buf[TABLE_BLOCK_BYTES];
 } rd;
 
@@ -470,8 +474,8 @@ static void table_open(stream_t *stream, uint32_t bytes)
 static void table_progress(int percent)
 {
     struct codec_api *api = rd.stream->ci;
-    if (percent != rd.percent && api->load_progress)
-        api->load_progress(percent);
+    if (api->load_progress && !api->load_progress(percent))
+        rd.stop = true;
     rd.percent = percent;
 }
 
@@ -481,7 +485,7 @@ static uint32_t table_u32(void)
     {
         /* tables are whole words, so a block holds whole values */
         unsigned n = MIN(rd.bytes_left, TABLE_BLOCK_BYTES);
-        if (n < 4)
+        if (n < 4 || rd.stop)
             return 0;
         unsigned got = rd.stream->ci->read_filebuf(rd.buf, n);
         if (got < n)
@@ -575,10 +579,11 @@ static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len)
     rd.done = 0;
     rd.total = numentries * 4 + qtmovie->res->num_sample_to_chunks * 12;
     rd.percent = -1;
+    rd.stop = false;
     table_open(qtmovie->stream, numentries * 4);
 
     uint32_t idx = 0;
-    for (i = 0; i < numentries; ++i)
+    for (i = 0; i < numentries && !rd.stop; ++i)
     {
         uint32_t offset = table_u32();
         if (i % accuracy_divider == 0)
@@ -597,7 +602,7 @@ static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len)
     stream_seek(qtmovie->stream, qtmovie->res->sample_to_chunk_offset);
     table_open(qtmovie->stream, qtmovie->res->num_sample_to_chunks * 12);
     stream_read_sample_to_chunk(&new_first, &new_frame);
-    for (k = 1; k <= numentries; ++k)
+    for (k = 1; k <= numentries && !rd.stop; ++k)
     {
         for (; i <= qtmovie->res->num_sample_to_chunks; ++i)
         {
@@ -628,6 +633,8 @@ static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len)
         qtmovie->res->lookup_table[idx].sample = 0;
     }
 
+    if (rd.stop)
+        return false;       /* the codec is to stop: no use finishing */
     table_progress(100);
     stream_seek(qtmovie->stream, current_offset);
     if (size_remaining)
-- 
2.56.1