Give a name to decoder instances

This will be useful in logs.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
This commit is contained in:
Romain Vimont 2023-02-24 21:22:35 +01:00
parent 99837fa600
commit 05f0e35d2a
3 changed files with 12 additions and 6 deletions

View file

@ -48,7 +48,7 @@ sc_decoder_open(struct sc_decoder *decoder, const AVCodec *codec) {
decoder->codec_ctx->flags |= AV_CODEC_FLAG_LOW_DELAY; decoder->codec_ctx->flags |= AV_CODEC_FLAG_LOW_DELAY;
if (avcodec_open2(decoder->codec_ctx, codec, NULL) < 0) { if (avcodec_open2(decoder->codec_ctx, codec, NULL) < 0) {
LOGE("Could not open codec"); LOGE("Decoder '%s': could not open codec", decoder->name);
avcodec_free_context(&decoder->codec_ctx); avcodec_free_context(&decoder->codec_ctx);
return false; return false;
} }
@ -101,7 +101,8 @@ sc_decoder_push(struct sc_decoder *decoder, const AVPacket *packet) {
int ret = avcodec_send_packet(decoder->codec_ctx, packet); int ret = avcodec_send_packet(decoder->codec_ctx, packet);
if (ret < 0 && ret != AVERROR(EAGAIN)) { if (ret < 0 && ret != AVERROR(EAGAIN)) {
LOGE("Could not send video packet: %d", ret); LOGE("Decoder '%s': could not send video packet: %d",
decoder->name, ret);
return false; return false;
} }
ret = avcodec_receive_frame(decoder->codec_ctx, decoder->frame); ret = avcodec_receive_frame(decoder->codec_ctx, decoder->frame);
@ -114,7 +115,8 @@ sc_decoder_push(struct sc_decoder *decoder, const AVPacket *packet) {
av_frame_unref(decoder->frame); av_frame_unref(decoder->frame);
} else if (ret != AVERROR(EAGAIN)) { } else if (ret != AVERROR(EAGAIN)) {
LOGE("Could not receive video frame: %d", ret); LOGE("Decoder '%s', could not receive video frame: %d",
decoder->name, ret);
return false; return false;
} }
return true; return true;
@ -140,7 +142,8 @@ sc_decoder_packet_sink_push(struct sc_packet_sink *sink,
} }
void void
sc_decoder_init(struct sc_decoder *decoder) { sc_decoder_init(struct sc_decoder *decoder, const char *name) {
decoder->name = name; // statically allocated
decoder->sink_count = 0; decoder->sink_count = 0;
static const struct sc_packet_sink_ops ops = { static const struct sc_packet_sink_ops ops = {

View file

@ -14,6 +14,8 @@
struct sc_decoder { struct sc_decoder {
struct sc_packet_sink packet_sink; // packet sink trait struct sc_packet_sink packet_sink; // packet sink trait
const char *name; // must be statically allocated (e.g. a string literal)
struct sc_frame_sink *sinks[SC_DECODER_MAX_SINKS]; struct sc_frame_sink *sinks[SC_DECODER_MAX_SINKS];
unsigned sink_count; unsigned sink_count;
@ -21,8 +23,9 @@ struct sc_decoder {
AVFrame *frame; AVFrame *frame;
}; };
// The name must be statically allocated (e.g. a string literal)
void void
sc_decoder_init(struct sc_decoder *decoder); sc_decoder_init(struct sc_decoder *decoder, const char *name);
void void
sc_decoder_add_sink(struct sc_decoder *decoder, struct sc_frame_sink *sink); sc_decoder_add_sink(struct sc_decoder *decoder, struct sc_frame_sink *sink);

View file

@ -441,7 +441,7 @@ scrcpy(struct scrcpy_options *options) {
needs_video_decoder |= !!options->v4l2_device; needs_video_decoder |= !!options->v4l2_device;
#endif #endif
if (needs_video_decoder) { if (needs_video_decoder) {
sc_decoder_init(&s->video_decoder); sc_decoder_init(&s->video_decoder, "video");
sc_demuxer_add_sink(&s->video_demuxer, &s->video_decoder.packet_sink); sc_demuxer_add_sink(&s->video_demuxer, &s->video_decoder.packet_sink);
} }