116
116
117
117
#include < limits.h>
118
118
119
- static constexpr unsigned DEFAULT_BUFFER_SIZE = 4096 ;
119
+ static constexpr size_t KILOBYTE = 1024 ;
120
+ static constexpr size_t MEGABYTE = 1024 * KILOBYTE;
121
+
122
+ static constexpr size_t DEFAULT_BUFFER_SIZE = 4 * MEGABYTE;
123
+ static constexpr size_t MIN_BUFFER_SIZE = std::max(CHUNK_SIZE * 32 ,
124
+ 64 * KILOBYTE);
125
+
120
126
static constexpr unsigned DEFAULT_BUFFER_BEFORE_PLAY = 10 ;
121
127
122
128
#ifdef ANDROID
@@ -129,7 +135,6 @@ struct Config {
129
135
ReplayGainConfig replay_gain;
130
136
};
131
137
132
- gcc_const
133
138
static Config
134
139
LoadConfig ()
135
140
{
@@ -306,12 +311,17 @@ initialize_decoder_and_player(const ReplayGainConfig &replay_gain_config)
306
311
FormatFatalError (" buffer size \" %s\" is not a "
307
312
" positive integer, line %i" ,
308
313
param->value .c_str (), param->line );
309
- buffer_size = tmp;
314
+ buffer_size = tmp * KILOBYTE;
315
+
316
+ if (buffer_size < MIN_BUFFER_SIZE) {
317
+ FormatWarning (config_domain, " buffer size %lu is too small, using %lu bytes instead" ,
318
+ (unsigned long )buffer_size,
319
+ (unsigned long )MIN_BUFFER_SIZE);
320
+ buffer_size = MIN_BUFFER_SIZE;
321
+ }
310
322
} else
311
323
buffer_size = DEFAULT_BUFFER_SIZE;
312
324
313
- buffer_size *= 1024 ;
314
-
315
325
const unsigned buffered_chunks = buffer_size / CHUNK_SIZE;
316
326
317
327
if (buffered_chunks >= 1 << 15 )
@@ -329,6 +339,19 @@ initialize_decoder_and_player(const ReplayGainConfig &replay_gain_config)
329
339
" than 100 percent, line %i" ,
330
340
param->value .c_str (), param->line );
331
341
}
342
+
343
+ if (perc > 80 ) {
344
+ /* this upper limit should avoid deadlocks
345
+ which can occur because the DecoderThread
346
+ cannot ever fill the music buffer to
347
+ exactly 100%; a few chunks always need to
348
+ be available to generate silence in
349
+ Player::SendSilence() */
350
+ FormatError (config_domain,
351
+ " buffer_before_play is too large (%f%%), capping at 80%%; please fix your configuration" ,
352
+ perc);
353
+ perc = 80 ;
354
+ }
332
355
} else
333
356
perc = DEFAULT_BUFFER_BEFORE_PLAY;
334
357
0 commit comments