Expose decoder as packet sink

Make decoder implement the packet sink trait.

This will allow the stream to push packets without depending on the
concrete sink type.
This commit is contained in:
Romain Vimont 2021-04-11 15:01:05 +02:00
parent 5beb7d6c02
commit cbed38799e
2 changed files with 33 additions and 0 deletions

View file

@ -6,6 +6,9 @@
#include "video_buffer.h" #include "video_buffer.h"
#include "util/log.h" #include "util/log.h"
/** Downcast packet_sink to decoder */
#define DOWNCAST(SINK) container_of(SINK, struct decoder, packet_sink)
bool bool
decoder_open(struct decoder *decoder, const AVCodec *codec) { decoder_open(struct decoder *decoder, const AVCodec *codec) {
decoder->codec_ctx = avcodec_alloc_context3(codec); decoder->codec_ctx = avcodec_alloc_context3(codec);
@ -59,7 +62,33 @@ decoder_push(struct decoder *decoder, const AVPacket *packet) {
return true; return true;
} }
static bool
decoder_packet_sink_open(struct sc_packet_sink *sink, const AVCodec *codec) {
struct decoder *decoder = DOWNCAST(sink);
return decoder_open(decoder, codec);
}
static void
decoder_packet_sink_close(struct sc_packet_sink *sink) {
struct decoder *decoder = DOWNCAST(sink);
decoder_close(decoder);
}
static bool
decoder_packet_sink_push(struct sc_packet_sink *sink, const AVPacket *packet) {
struct decoder *decoder = DOWNCAST(sink);
return decoder_push(decoder, packet);
}
void void
decoder_init(struct decoder *decoder, struct video_buffer *vb) { decoder_init(struct decoder *decoder, struct video_buffer *vb) {
decoder->video_buffer = vb; decoder->video_buffer = vb;
static const struct sc_packet_sink_ops ops = {
.open = decoder_packet_sink_open,
.close = decoder_packet_sink_close,
.push = decoder_packet_sink_push,
};
decoder->packet_sink.ops = &ops;
} }

View file

@ -3,12 +3,16 @@
#include "common.h" #include "common.h"
#include "trait/packet_sink.h"
#include <stdbool.h> #include <stdbool.h>
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
struct video_buffer; struct video_buffer;
struct decoder { struct decoder {
struct sc_packet_sink packet_sink; // packet sink trait
struct video_buffer *video_buffer; struct video_buffer *video_buffer;
AVCodecContext *codec_ctx; AVCodecContext *codec_ctx;