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.
This commit is contained in:
Romain Vimont 2023-03-10 22:46:56 +01:00
parent a9f6001f51
commit be985b8242
2 changed files with 16 additions and 15 deletions

View file

@ -473,9 +473,12 @@ sc_recorder_video_packet_sink_open(struct sc_packet_sink *sink,
return false; return false;
} }
stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; int r = avcodec_parameters_from_context(stream->codecpar, ctx);
stream->codecpar->codec_id = ctx->codec->id; if (r < 0) {
stream->codecpar->format = AV_PIX_FMT_YUV420P; sc_mutex_unlock(&recorder->mutex);
return false;
}
stream->codecpar->width = recorder->declared_frame_size.width; stream->codecpar->width = recorder->declared_frame_size.width;
stream->codecpar->height = recorder->declared_frame_size.height; 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; return false;
} }
stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; int r = avcodec_parameters_from_context(stream->codecpar, ctx);
stream->codecpar->codec_id = ctx->codec->id; if (r < 0) {
#ifdef SCRCPY_LAVU_HAS_CHLAYOUT sc_mutex_unlock(&recorder->mutex);
stream->codecpar->ch_layout.nb_channels = 2; return false;
#else }
stream->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
stream->codecpar->channels = 2;
#endif
stream->codecpar->sample_rate = 48000;
recorder->audio_stream_index = stream->index; recorder->audio_stream_index = stream->index;

View file

@ -205,9 +205,11 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs, const AVCodecContext *ctx) {
goto error_avformat_free_context; goto error_avformat_free_context;
} }
ostream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; int r = avcodec_parameters_from_context(ostream->codecpar, ctx);
ostream->codecpar->codec_id = encoder->id; if (r < 0) {
ostream->codecpar->format = AV_PIX_FMT_YUV420P; goto error_avformat_free_context;
}
ostream->codecpar->width = vs->frame_size.width; ostream->codecpar->width = vs->frame_size.width;
ostream->codecpar->height = vs->frame_size.height; ostream->codecpar->height = vs->frame_size.height;