Use sc_ prefix for decoder

This commit is contained in:
Romain Vimont 2022-02-02 19:27:41 +01:00
parent 0bf7e4ddc4
commit 0080d0b0ff
3 changed files with 40 additions and 39 deletions

View file

@ -9,10 +9,10 @@
#include "util/log.h"
/** Downcast packet_sink to decoder */
#define DOWNCAST(SINK) container_of(SINK, struct decoder, packet_sink)
#define DOWNCAST(SINK) container_of(SINK, struct sc_decoder, packet_sink)
static void
decoder_close_first_sinks(struct decoder *decoder, unsigned count) {
sc_decoder_close_first_sinks(struct sc_decoder *decoder, unsigned count) {
while (count) {
struct sc_frame_sink *sink = decoder->sinks[--count];
sink->ops->close(sink);
@ -20,17 +20,17 @@ decoder_close_first_sinks(struct decoder *decoder, unsigned count) {
}
static inline void
decoder_close_sinks(struct decoder *decoder) {
decoder_close_first_sinks(decoder, decoder->sink_count);
sc_decoder_close_sinks(struct sc_decoder *decoder) {
sc_decoder_close_first_sinks(decoder, decoder->sink_count);
}
static bool
decoder_open_sinks(struct decoder *decoder) {
sc_decoder_open_sinks(struct sc_decoder *decoder) {
for (unsigned i = 0; i < decoder->sink_count; ++i) {
struct sc_frame_sink *sink = decoder->sinks[i];
if (!sink->ops->open(sink)) {
LOGE("Could not open frame sink %d", i);
decoder_close_first_sinks(decoder, i);
sc_decoder_close_first_sinks(decoder, i);
return false;
}
}
@ -39,7 +39,7 @@ decoder_open_sinks(struct decoder *decoder) {
}
static bool
decoder_open(struct decoder *decoder, const AVCodec *codec) {
sc_decoder_open(struct sc_decoder *decoder, const AVCodec *codec) {
decoder->codec_ctx = avcodec_alloc_context3(codec);
if (!decoder->codec_ctx) {
LOG_OOM();
@ -62,7 +62,7 @@ decoder_open(struct decoder *decoder, const AVCodec *codec) {
return false;
}
if (!decoder_open_sinks(decoder)) {
if (!sc_decoder_open_sinks(decoder)) {
LOGE("Could not open decoder sinks");
av_frame_free(&decoder->frame);
avcodec_close(decoder->codec_ctx);
@ -74,15 +74,15 @@ decoder_open(struct decoder *decoder, const AVCodec *codec) {
}
static void
decoder_close(struct decoder *decoder) {
decoder_close_sinks(decoder);
sc_decoder_close(struct sc_decoder *decoder) {
sc_decoder_close_sinks(decoder);
av_frame_free(&decoder->frame);
avcodec_close(decoder->codec_ctx);
avcodec_free_context(&decoder->codec_ctx);
}
static bool
push_frame_to_sinks(struct decoder *decoder, const AVFrame *frame) {
push_frame_to_sinks(struct sc_decoder *decoder, const AVFrame *frame) {
for (unsigned i = 0; i < decoder->sink_count; ++i) {
struct sc_frame_sink *sink = decoder->sinks[i];
if (!sink->ops->push(sink, frame)) {
@ -95,7 +95,7 @@ push_frame_to_sinks(struct decoder *decoder, const AVFrame *frame) {
}
static bool
decoder_push(struct decoder *decoder, const AVPacket *packet) {
sc_decoder_push(struct sc_decoder *decoder, const AVPacket *packet) {
bool is_config = packet->pts == AV_NOPTS_VALUE;
if (is_config) {
// nothing to do
@ -124,39 +124,40 @@ decoder_push(struct decoder *decoder, const AVPacket *packet) {
}
static bool
decoder_packet_sink_open(struct sc_packet_sink *sink, const AVCodec *codec) {
struct decoder *decoder = DOWNCAST(sink);
return decoder_open(decoder, codec);
sc_decoder_packet_sink_open(struct sc_packet_sink *sink, const AVCodec *codec) {
struct sc_decoder *decoder = DOWNCAST(sink);
return sc_decoder_open(decoder, codec);
}
static void
decoder_packet_sink_close(struct sc_packet_sink *sink) {
struct decoder *decoder = DOWNCAST(sink);
decoder_close(decoder);
sc_decoder_packet_sink_close(struct sc_packet_sink *sink) {
struct sc_decoder *decoder = DOWNCAST(sink);
sc_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);
sc_decoder_packet_sink_push(struct sc_packet_sink *sink,
const AVPacket *packet) {
struct sc_decoder *decoder = DOWNCAST(sink);
return sc_decoder_push(decoder, packet);
}
void
decoder_init(struct decoder *decoder) {
sc_decoder_init(struct sc_decoder *decoder) {
decoder->sink_count = 0;
static const struct sc_packet_sink_ops ops = {
.open = decoder_packet_sink_open,
.close = decoder_packet_sink_close,
.push = decoder_packet_sink_push,
.open = sc_decoder_packet_sink_open,
.close = sc_decoder_packet_sink_close,
.push = sc_decoder_packet_sink_push,
};
decoder->packet_sink.ops = &ops;
}
void
decoder_add_sink(struct decoder *decoder, struct sc_frame_sink *sink) {
assert(decoder->sink_count < DECODER_MAX_SINKS);
sc_decoder_add_sink(struct sc_decoder *decoder, struct sc_frame_sink *sink) {
assert(decoder->sink_count < SC_DECODER_MAX_SINKS);
assert(sink);
assert(sink->ops);
decoder->sinks[decoder->sink_count++] = sink;

View file

@ -1,5 +1,5 @@
#ifndef DECODER_H
#define DECODER_H
#ifndef SC_DECODER_H
#define SC_DECODER_H
#include "common.h"
@ -9,12 +9,12 @@
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#define DECODER_MAX_SINKS 2
#define SC_DECODER_MAX_SINKS 2
struct decoder {
struct sc_decoder {
struct sc_packet_sink packet_sink; // packet sink trait
struct sc_frame_sink *sinks[DECODER_MAX_SINKS];
struct sc_frame_sink *sinks[SC_DECODER_MAX_SINKS];
unsigned sink_count;
AVCodecContext *codec_ctx;
@ -22,9 +22,9 @@ struct decoder {
};
void
decoder_init(struct decoder *decoder);
sc_decoder_init(struct sc_decoder *decoder);
void
decoder_add_sink(struct decoder *decoder, struct sc_frame_sink *sink);
sc_decoder_add_sink(struct sc_decoder *decoder, struct sc_frame_sink *sink);
#endif

View file

@ -40,7 +40,7 @@ struct scrcpy {
struct sc_server server;
struct sc_screen screen;
struct stream stream;
struct decoder decoder;
struct sc_decoder decoder;
struct recorder recorder;
#ifdef HAVE_V4L2
struct sc_v4l2_sink v4l2_sink;
@ -371,13 +371,13 @@ scrcpy(struct scrcpy_options *options) {
file_pusher_initialized = true;
}
struct decoder *dec = NULL;
struct sc_decoder *dec = NULL;
bool needs_decoder = options->display;
#ifdef HAVE_V4L2
needs_decoder |= !!options->v4l2_device;
#endif
if (needs_decoder) {
decoder_init(&s->decoder);
sc_decoder_init(&s->decoder);
dec = &s->decoder;
}
@ -608,7 +608,7 @@ aoa_hid_end:
}
screen_initialized = true;
decoder_add_sink(&s->decoder, &s->screen.frame_sink);
sc_decoder_add_sink(&s->decoder, &s->screen.frame_sink);
}
#ifdef HAVE_V4L2
@ -618,7 +618,7 @@ aoa_hid_end:
goto end;
}
decoder_add_sink(&s->decoder, &s->v4l2_sink.frame_sink);
sc_decoder_add_sink(&s->decoder, &s->v4l2_sink.frame_sink);
v4l2_sink_initialized = true;
}