decoder/ffmpeg: FfmpegOpenInput() throws exception on error

This commit is contained in:
Max Kellermann 2016-09-16 18:38:16 +02:00
parent 8c744efd56
commit 2a2ac35b98

View File

@ -60,22 +60,18 @@ extern "C" {
static AVFormatContext * static AVFormatContext *
FfmpegOpenInput(AVIOContext *pb, FfmpegOpenInput(AVIOContext *pb,
const char *filename, const char *filename,
AVInputFormat *fmt, AVInputFormat *fmt)
Error &error)
{ {
AVFormatContext *context = avformat_alloc_context(); AVFormatContext *context = avformat_alloc_context();
if (context == nullptr) { if (context == nullptr)
error.Set(ffmpeg_domain, "Out of memory"); throw std::runtime_error("avformat_alloc_context() failed");
return nullptr;
}
context->pb = pb; context->pb = pb;
int err = avformat_open_input(&context, filename, fmt, nullptr); int err = avformat_open_input(&context, filename, fmt, nullptr);
if (err < 0) { if (err < 0) {
avformat_free_context(context); avformat_free_context(context);
SetFfmpegError(error, err, "avformat_open_input() failed"); throw MakeFfmpegError(err, "avformat_open_input() failed");
return nullptr;
} }
return context; return context;
@ -797,11 +793,12 @@ ffmpeg_decode(Decoder &decoder, InputStream &input)
return; return;
} }
Error error; AVFormatContext *format_context;
AVFormatContext *format_context = try {
FfmpegOpenInput(stream.io, input.GetURI(), input_format, error); format_context =FfmpegOpenInput(stream.io, input.GetURI(),
if (format_context == nullptr) { input_format);
LogError(error); } catch (const std::runtime_error &e) {
LogError(e);
return; return;
} }
@ -848,11 +845,12 @@ ffmpeg_scan_stream(InputStream &is,
if (!stream.Open()) if (!stream.Open())
return false; return false;
AVFormatContext *f = AVFormatContext *f;
FfmpegOpenInput(stream.io, is.GetURI(), input_format, try {
IgnoreError()); f = FfmpegOpenInput(stream.io, is.GetURI(), input_format);
if (f == nullptr) } catch (const std::runtime_error &) {
return false; return false;
}
AtScopeExit(&f) { AtScopeExit(&f) {
avformat_close_input(&f); avformat_close_input(&f);