Commit 498491ec authored by Max Kellermann's avatar Max Kellermann

decoder/ffmpeg: convert struct mpd_ffmpeg_stream to a class

parent 85b77b81
...@@ -88,19 +88,29 @@ mpd_ffmpeg_log_callback(gcc_unused void *ptr, int level, ...@@ -88,19 +88,29 @@ mpd_ffmpeg_log_callback(gcc_unused void *ptr, int level,
} }
} }
struct mpd_ffmpeg_stream { struct AvioStream {
struct decoder *decoder; struct decoder *decoder;
struct input_stream *input; struct input_stream *input;
AVIOContext *io; AVIOContext *io;
unsigned char buffer[8192]; unsigned char buffer[8192];
AvioStream(struct decoder *_decoder, input_stream *_input)
:decoder(_decoder), input(_input), io(nullptr) {}
~AvioStream() {
if (io != nullptr)
av_free(io);
}
bool Open();
}; };
static int static int
mpd_ffmpeg_stream_read(void *opaque, uint8_t *buf, int size) mpd_ffmpeg_stream_read(void *opaque, uint8_t *buf, int size)
{ {
struct mpd_ffmpeg_stream *stream = (struct mpd_ffmpeg_stream *)opaque; AvioStream *stream = (AvioStream *)opaque;
return decoder_read(stream->decoder, stream->input, return decoder_read(stream->decoder, stream->input,
(void *)buf, size); (void *)buf, size);
...@@ -109,7 +119,7 @@ mpd_ffmpeg_stream_read(void *opaque, uint8_t *buf, int size) ...@@ -109,7 +119,7 @@ mpd_ffmpeg_stream_read(void *opaque, uint8_t *buf, int size)
static int64_t static int64_t
mpd_ffmpeg_stream_seek(void *opaque, int64_t pos, int whence) mpd_ffmpeg_stream_seek(void *opaque, int64_t pos, int whence)
{ {
struct mpd_ffmpeg_stream *stream = (struct mpd_ffmpeg_stream *)opaque; AvioStream *stream = (AvioStream *)opaque;
if (whence == AVSEEK_SIZE) if (whence == AVSEEK_SIZE)
return stream->input->size; return stream->input->size;
...@@ -120,23 +130,15 @@ mpd_ffmpeg_stream_seek(void *opaque, int64_t pos, int whence) ...@@ -120,23 +130,15 @@ mpd_ffmpeg_stream_seek(void *opaque, int64_t pos, int whence)
return stream->input->offset; return stream->input->offset;
} }
static struct mpd_ffmpeg_stream * bool
mpd_ffmpeg_stream_open(struct decoder *decoder, struct input_stream *input) AvioStream::Open()
{ {
struct mpd_ffmpeg_stream *stream = g_new(struct mpd_ffmpeg_stream, 1); io = avio_alloc_context(buffer, sizeof(buffer),
stream->decoder = decoder; false, this,
stream->input = input; mpd_ffmpeg_stream_read, nullptr,
stream->io = avio_alloc_context(stream->buffer, sizeof(stream->buffer), input->seekable
false, stream, ? mpd_ffmpeg_stream_seek : nullptr);
mpd_ffmpeg_stream_read, NULL, return io != nullptr;
input->seekable
? mpd_ffmpeg_stream_seek : NULL);
if (stream->io == NULL) {
g_free(stream);
return NULL;
}
return stream;
} }
/** /**
...@@ -158,13 +160,6 @@ mpd_ffmpeg_open_input(AVFormatContext **ic_ptr, ...@@ -158,13 +160,6 @@ mpd_ffmpeg_open_input(AVFormatContext **ic_ptr,
return avformat_open_input(ic_ptr, filename, fmt, NULL); return avformat_open_input(ic_ptr, filename, fmt, NULL);
} }
static void
mpd_ffmpeg_stream_close(struct mpd_ffmpeg_stream *stream)
{
av_free(stream->io);
g_free(stream);
}
static bool static bool
ffmpeg_init(gcc_unused const config_param &param) ffmpeg_init(gcc_unused const config_param &param)
{ {
...@@ -376,20 +371,18 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) ...@@ -376,20 +371,18 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input)
g_debug("detected input format '%s' (%s)", g_debug("detected input format '%s' (%s)",
input_format->name, input_format->long_name); input_format->name, input_format->long_name);
struct mpd_ffmpeg_stream *stream = AvioStream stream(decoder, input);
mpd_ffmpeg_stream_open(decoder, input); if (!stream.Open()) {
if (stream == NULL) {
g_warning("Failed to open stream"); g_warning("Failed to open stream");
return; return;
} }
//ffmpeg works with ours "fileops" helper //ffmpeg works with ours "fileops" helper
AVFormatContext *format_context = NULL; AVFormatContext *format_context = NULL;
if (mpd_ffmpeg_open_input(&format_context, stream->io, if (mpd_ffmpeg_open_input(&format_context, stream.io,
input->uri.c_str(), input->uri.c_str(),
input_format) != 0) { input_format) != 0) {
g_warning("Open failed\n"); g_warning("Open failed\n");
mpd_ffmpeg_stream_close(stream);
return; return;
} }
...@@ -398,7 +391,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) ...@@ -398,7 +391,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input)
if (find_result < 0) { if (find_result < 0) {
g_warning("Couldn't find stream info\n"); g_warning("Couldn't find stream info\n");
avformat_close_input(&format_context); avformat_close_input(&format_context);
mpd_ffmpeg_stream_close(stream);
return; return;
} }
...@@ -406,7 +398,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) ...@@ -406,7 +398,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input)
if (audio_stream == -1) { if (audio_stream == -1) {
g_warning("No audio stream inside\n"); g_warning("No audio stream inside\n");
avformat_close_input(&format_context); avformat_close_input(&format_context);
mpd_ffmpeg_stream_close(stream);
return; return;
} }
...@@ -421,7 +412,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) ...@@ -421,7 +412,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input)
if (!codec) { if (!codec) {
g_warning("Unsupported audio codec\n"); g_warning("Unsupported audio codec\n");
avformat_close_input(&format_context); avformat_close_input(&format_context);
mpd_ffmpeg_stream_close(stream);
return; return;
} }
...@@ -439,7 +429,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) ...@@ -439,7 +429,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input)
g_warning("%s", error->message); g_warning("%s", error->message);
g_error_free(error); g_error_free(error);
avformat_close_input(&format_context); avformat_close_input(&format_context);
mpd_ffmpeg_stream_close(stream);
return; return;
} }
...@@ -452,7 +441,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) ...@@ -452,7 +441,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input)
if (open_result < 0) { if (open_result < 0) {
g_warning("Could not open codec\n"); g_warning("Could not open codec\n");
avformat_close_input(&format_context); avformat_close_input(&format_context);
mpd_ffmpeg_stream_close(stream);
return; return;
} }
...@@ -467,7 +455,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) ...@@ -467,7 +455,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input)
if (!frame) { if (!frame) {
g_warning("Could not allocate frame\n"); g_warning("Could not allocate frame\n");
avformat_close_input(&format_context); avformat_close_input(&format_context);
mpd_ffmpeg_stream_close(stream);
return; return;
} }
...@@ -511,7 +498,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) ...@@ -511,7 +498,6 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input)
avcodec_close(codec_context); avcodec_close(codec_context);
avformat_close_input(&format_context); avformat_close_input(&format_context);
mpd_ffmpeg_stream_close(stream);
} }
//no tag reading in ffmpeg, check if playable //no tag reading in ffmpeg, check if playable
...@@ -523,22 +509,19 @@ ffmpeg_scan_stream(struct input_stream *is, ...@@ -523,22 +509,19 @@ ffmpeg_scan_stream(struct input_stream *is,
if (input_format == NULL) if (input_format == NULL)
return false; return false;
struct mpd_ffmpeg_stream *stream = mpd_ffmpeg_stream_open(NULL, is); AvioStream stream(nullptr, is);
if (stream == NULL) if (!stream.Open())
return false; return false;
AVFormatContext *f = NULL; AVFormatContext *f = NULL;
if (mpd_ffmpeg_open_input(&f, stream->io, is->uri.c_str(), if (mpd_ffmpeg_open_input(&f, stream.io, is->uri.c_str(),
input_format) != 0) { input_format) != 0)
mpd_ffmpeg_stream_close(stream);
return false; return false;
}
const int find_result = const int find_result =
avformat_find_stream_info(f, NULL); avformat_find_stream_info(f, NULL);
if (find_result < 0) { if (find_result < 0) {
avformat_close_input(&f); avformat_close_input(&f);
mpd_ffmpeg_stream_close(stream);
return false; return false;
} }
...@@ -553,8 +536,6 @@ ffmpeg_scan_stream(struct input_stream *is, ...@@ -553,8 +536,6 @@ ffmpeg_scan_stream(struct input_stream *is,
handler, handler_ctx); handler, handler_ctx);
avformat_close_input(&f); avformat_close_input(&f);
mpd_ffmpeg_stream_close(stream);
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment