@@ -95,6 +95,18 @@ static off_t stream_lseek(void *stream, off_t offset, int whence) {
95
95
#define INPUT_BUFFER_CONSUME (i , n ) ((i).read_off += (n))
96
96
#define INPUT_BUFFER_CLEAR (i ) ((i).read_off = (i).write_off = 0)
97
97
98
+ static void stream_set_blocking (audiomp3_mp3file_obj_t * self , bool block_ok ) {
99
+ if (!self -> settimeout_args [0 ]) {
100
+ return ;
101
+ }
102
+ if (block_ok == self -> block_ok ) {
103
+ return ;
104
+ }
105
+ self -> block_ok = block_ok ;
106
+ self -> settimeout_args [2 ] = block_ok ? mp_const_none : mp_obj_new_int (0 );
107
+ mp_call_method_n_kw (1 , 0 , self -> settimeout_args );
108
+ }
109
+
98
110
/** Fill the input buffer unconditionally.
99
111
*
100
112
* Returns true if the input buffer contains any useful data,
@@ -110,6 +122,8 @@ static bool mp3file_update_inbuf_always(audiomp3_mp3file_obj_t *self, bool block
110
122
return INPUT_BUFFER_AVAILABLE (self -> inbuf ) > 0 ;
111
123
}
112
124
125
+ stream_set_blocking (self , block_ok );
126
+
113
127
// We didn't previously reach EOF and we have input buffer space available
114
128
115
129
// Move the unconsumed portion of the buffer to the start
@@ -119,7 +133,7 @@ static bool mp3file_update_inbuf_always(audiomp3_mp3file_obj_t *self, bool block
119
133
self -> inbuf .read_off = 0 ;
120
134
}
121
135
122
- for (size_t to_read ; !self -> eof && (to_read = INPUT_BUFFER_SPACE (self -> inbuf )) > 0 && ( block_ok || stream_readable ( self -> stream )) ;) {
136
+ for (size_t to_read ; !self -> eof && (to_read = INPUT_BUFFER_SPACE (self -> inbuf )) > 0 ;) {
123
137
uint8_t * write_ptr = self -> inbuf .buf + self -> inbuf .write_off ;
124
138
ssize_t n_read = stream_read (self -> stream , write_ptr , to_read );
125
139
@@ -328,9 +342,14 @@ void common_hal_audiomp3_mp3file_set_file(audiomp3_mp3file_obj_t *self, mp_obj_t
328
342
background_callback_prevent ();
329
343
330
344
self -> stream = stream ;
345
+ mp_load_method_maybe (stream , MP_QSTR_settimeout , self -> settimeout_args );
331
346
332
347
INPUT_BUFFER_CLEAR (self -> inbuf );
333
348
self -> eof = 0 ;
349
+
350
+ self -> block_ok = false;
351
+ stream_set_blocking (self , true);
352
+
334
353
self -> other_channel = -1 ;
335
354
mp3file_update_inbuf_half (self , true);
336
355
mp3file_find_sync_word (self , true);
@@ -365,6 +384,7 @@ void common_hal_audiomp3_mp3file_deinit(audiomp3_mp3file_obj_t *self) {
365
384
self -> pcm_buffer [0 ] = NULL ;
366
385
self -> pcm_buffer [1 ] = NULL ;
367
386
self -> stream = mp_const_none ;
387
+ self -> settimeout_args [0 ] = MP_OBJ_NULL ;
368
388
self -> samples_decoded = 0 ;
369
389
}
370
390
0 commit comments