Skip to content

Commit

Permalink
Copy codec parameters from context
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rom1v committed Mar 11, 2023
1 parent a9f6001 commit be985b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
23 changes: 11 additions & 12 deletions app/src/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
8 changes: 5 additions & 3 deletions app/src/v4l2_sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit be985b8

Please sign in to comment.