From be985b8242e0626288674b84c5039725170f8f0c Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Fri, 10 Mar 2023 22:46:56 +0100 Subject: [PATCH] Copy codec parameters from context Now that the recorder have access to the codec context, it may automatically initialize the stream codec parameters. The V4L2 sink could do the same. --- app/src/recorder.c | 23 +++++++++++------------ app/src/v4l2_sink.c | 8 +++++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/app/src/recorder.c b/app/src/recorder.c index 9b646055..e8484256 100644 --- a/app/src/recorder.c +++ b/app/src/recorder.c @@ -473,9 +473,12 @@ sc_recorder_video_packet_sink_open(struct sc_packet_sink *sink, return false; } - stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; - stream->codecpar->codec_id = ctx->codec->id; - stream->codecpar->format = AV_PIX_FMT_YUV420P; + int r = avcodec_parameters_from_context(stream->codecpar, ctx); + if (r < 0) { + sc_mutex_unlock(&recorder->mutex); + return false; + } + stream->codecpar->width = recorder->declared_frame_size.width; stream->codecpar->height = recorder->declared_frame_size.height; @@ -554,15 +557,11 @@ sc_recorder_audio_packet_sink_open(struct sc_packet_sink *sink, return false; } - stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; - stream->codecpar->codec_id = ctx->codec->id; -#ifdef SCRCPY_LAVU_HAS_CHLAYOUT - stream->codecpar->ch_layout.nb_channels = 2; -#else - stream->codecpar->channel_layout = AV_CH_LAYOUT_STEREO; - stream->codecpar->channels = 2; -#endif - stream->codecpar->sample_rate = 48000; + int r = avcodec_parameters_from_context(stream->codecpar, ctx); + if (r < 0) { + sc_mutex_unlock(&recorder->mutex); + return false; + } recorder->audio_stream_index = stream->index; diff --git a/app/src/v4l2_sink.c b/app/src/v4l2_sink.c index fe11614a..c6714d18 100644 --- a/app/src/v4l2_sink.c +++ b/app/src/v4l2_sink.c @@ -205,9 +205,11 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs, const AVCodecContext *ctx) { goto error_avformat_free_context; } - ostream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; - ostream->codecpar->codec_id = encoder->id; - ostream->codecpar->format = AV_PIX_FMT_YUV420P; + int r = avcodec_parameters_from_context(ostream->codecpar, ctx); + if (r < 0) { + goto error_avformat_free_context; + } + ostream->codecpar->width = vs->frame_size.width; ostream->codecpar->height = vs->frame_size.height;