Skip to content

Commit c654e71

Browse files
committed
Disable error messages while a multithreaded scan is in progress
Fixes #121
1 parent 29a344a commit c654e71

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/scan.cpp

+29-12
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ extern "C" {
5555
#include "output.hpp"
5656
#include "tag.hpp"
5757

58-
#define output_fferror(e, msg) char errbuf[256]; av_strerror(e, errbuf, sizeof(errbuf)); output_error(msg ": {}", errbuf)
58+
template <typename T>
59+
constexpr void output_fferror(int error, T&& msg)
60+
{
61+
char errbuf[512];
62+
av_strerror(error, errbuf, sizeof(errbuf));
63+
output_error("{}: {}", msg, errbuf);
64+
}
5965
#define OLD_CHANNEL_LAYOUT LIBAVUTIL_VERSION_MAJOR < 57 || (LIBAVUTIL_VERSION_MAJOR == 57 && LIBAVUTIL_VERSION_MINOR < 18)
6066
#define OUTPUT_FORMAT AV_SAMPLE_FMT_S16
6167

@@ -223,7 +229,8 @@ bool ScanJob::Track::scan(const Config &config, std::mutex *m)
223229
lk->lock();
224230
rc = avformat_open_input(&format_ctx, rsgain::format("file:{}", path.string()).c_str(), nullptr, nullptr);
225231
if (rc < 0) {
226-
output_fferror(rc, "Could not open input");
232+
if (!multithread)
233+
output_fferror(rc, "Could not open input");
227234
goto end;
228235
}
229236

@@ -233,14 +240,16 @@ bool ScanJob::Track::scan(const Config &config, std::mutex *m)
233240

234241
rc = avformat_find_stream_info(format_ctx, nullptr);
235242
if (rc < 0) {
236-
output_fferror(rc, "Could not find stream info");
243+
if (!multithread)
244+
output_fferror(rc, "Could not find stream info");
237245
goto end;
238246
}
239247

240248
// Select the best audio stream
241249
stream_id = av_find_best_stream(format_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
242250
if (stream_id < 0) {
243-
output_error("Could not find audio stream");
251+
if (!multithread)
252+
output_error("Could not find audio stream");
244253
goto end;
245254
}
246255
stream = format_ctx->streams[stream_id];
@@ -250,7 +259,8 @@ bool ScanJob::Track::scan(const Config &config, std::mutex *m)
250259
do {
251260
codec_ctx = avcodec_alloc_context3(codec);
252261
if (!codec_ctx) {
253-
output_error("Could not allocate audio codec context");
262+
if (!multithread)
263+
output_error("Could not allocate audio codec context");
254264
goto end;
255265
}
256266
avcodec_parameters_to_context(codec_ctx, stream->codecpar);
@@ -274,7 +284,8 @@ bool ScanJob::Track::scan(const Config &config, std::mutex *m)
274284
}
275285
}
276286
}
277-
output_fferror(rc, "Could not open codec");
287+
if (!multithread)
288+
output_fferror(rc, "Could not open codec");
278289
goto end;
279290
}
280291
repeat = false;
@@ -324,13 +335,15 @@ bool ScanJob::Track::scan(const Config &config, std::mutex *m)
324335
);
325336
#endif
326337
if (!swr) {
327-
output_error("Could not allocate libswresample context");
338+
if (!multithread)
339+
output_error("Could not allocate libswresample context");
328340
goto end;
329341
}
330342

331343
rc = swr_init(swr);
332344
if (rc < 0) {
333-
output_fferror(rc, "Could not open libswresample context");
345+
if (!multithread)
346+
output_fferror(rc, "Could not open libswresample context");
334347
goto end;
335348
}
336349
}
@@ -345,21 +358,24 @@ bool ScanJob::Track::scan(const Config &config, std::mutex *m)
345358
EBUR128_MODE_I | peak_mode
346359
);
347360
if (!ebur128) {
348-
output_error("Could not initialize libebur128 scanner");
361+
if (!multithread)
362+
output_error("Could not initialize libebur128 scanner");
349363
goto end;
350364
}
351365

352366
// Allocate AVPacket structure
353367
packet = av_packet_alloc();
354368
if (!packet) {
355-
output_error("Could not allocate packet");
369+
if (!multithread)
370+
output_error("Could not allocate packet");
356371
goto end;
357372
}
358373

359374
// Alocate AVFrame structure
360375
frame = av_frame_alloc();
361376
if (!frame) {
362-
output_error("Could not allocate frame");
377+
if (!multithread)
378+
output_error("Could not allocate frame");
363379
goto end;
364380
}
365381

@@ -395,7 +411,8 @@ bool ScanJob::Track::scan(const Config &config, std::mutex *m)
395411
);
396412
swr_out_data[0] = (uint8_t*) av_malloc(out_size);
397413
if (swr_convert(swr, swr_out_data, frame->nb_samples, (const uint8_t**) frame->data, frame->nb_samples) < 0) {
398-
output_error("Could not convert audio frame");
414+
if (!multithread)
415+
output_error("Could not convert audio frame");
399416
av_free(swr_out_data[0]);
400417
goto end;
401418
}

0 commit comments

Comments
 (0)