فهرست منبع

obs-ffmpeg: Respect AVFormatContext and AVOutputFormat constness

AVFormatContext::oformat was made const on April 27, 2021 [1]. If we
respect the constness of AVOutputFormat and do not cast results from
FFmpeg functions to non-const, we cannot modify the results after the
fact. Our choices are either to cast them to non-const (and presumably
have them implicitly casted back to const on later function calls), or
only try to modify the results in versions of FFmpeg where these are not
expected to be const.

Instead of relying on casts, we can set the encoder values in the
ffmpeg_cfg struct, which are later passed to new_stream.

Also modifies deps/media-playback. Removes compiler warnings.
Some avformat functions return const AV(In/Out)putFormat per [1], so
ifdef as needed.

[1]: https://github.com/FFmpeg/FFmpeg/commit/56450a0ee4fdda160f4039fc2ae33edfd27765c9
     lavf 59.0.100 avformat.h
     avformat: Constify the API wrt AV(In|Out)putFormat

     Also constify AVProbeData.
Ryan Foster 4 سال پیش
والد
کامیت
ce734366bc
3فایلهای تغییر یافته به همراه32 افزوده شده و 1 حذف شده
  1. 4 0
      deps/media-playback/media-playback/media.c
  2. 10 0
      plugins/obs-ffmpeg/ffmpeg-mux/ffmpeg-mux.c
  3. 18 1
      plugins/obs-ffmpeg/obs-ffmpeg-output.c

+ 4 - 0
deps/media-playback/media-playback/media.c

@@ -608,7 +608,11 @@ static int interrupt_callback(void *data)
 
 
 static bool init_avformat(mp_media_t *m)
 static bool init_avformat(mp_media_t *m)
 {
 {
+#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
 	AVInputFormat *format = NULL;
 	AVInputFormat *format = NULL;
+#else
+	const AVInputFormat *format = NULL;
+#endif
 
 
 	if (m->format_name && *m->format_name) {
 	if (m->format_name && *m->format_name) {
 		format = av_find_input_format(m->format_name);
 		format = av_find_input_format(m->format_name);

+ 10 - 0
plugins/obs-ffmpeg/ffmpeg-mux/ffmpeg-mux.c

@@ -565,7 +565,11 @@ static inline bool ffmpeg_mux_get_extra_data(struct ffmpeg_mux *ffm)
 
 
 static inline int open_output_file(struct ffmpeg_mux *ffm)
 static inline int open_output_file(struct ffmpeg_mux *ffm)
 {
 {
+#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
 	AVOutputFormat *format = ffm->output->oformat;
 	AVOutputFormat *format = ffm->output->oformat;
+#else
+	const AVOutputFormat *format = ffm->output->oformat;
+#endif
 	int ret;
 	int ret;
 
 
 	if ((format->flags & AVFMT_NOFILE) == 0) {
 	if ((format->flags & AVFMT_NOFILE) == 0) {
@@ -631,7 +635,11 @@ static bool ffmpeg_mux_is_network(struct ffmpeg_mux *ffm)
 
 
 static int ffmpeg_mux_init_context(struct ffmpeg_mux *ffm)
 static int ffmpeg_mux_init_context(struct ffmpeg_mux *ffm)
 {
 {
+#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
 	AVOutputFormat *output_format;
 	AVOutputFormat *output_format;
+#else
+	const AVOutputFormat *output_format;
+#endif
 	int ret;
 	int ret;
 	bool is_http = false;
 	bool is_http = false;
 	is_http = (strncmp(ffm->params.file, HTTP_PROTO,
 	is_http = (strncmp(ffm->params.file, HTTP_PROTO,
@@ -665,8 +673,10 @@ static int ffmpeg_mux_init_context(struct ffmpeg_mux *ffm)
 		return FFM_ERROR;
 		return FFM_ERROR;
 	}
 	}
 
 
+#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
 	ffm->output->oformat->video_codec = AV_CODEC_ID_NONE;
 	ffm->output->oformat->video_codec = AV_CODEC_ID_NONE;
 	ffm->output->oformat->audio_codec = AV_CODEC_ID_NONE;
 	ffm->output->oformat->audio_codec = AV_CODEC_ID_NONE;
+#endif
 
 
 	if (!init_streams(ffm)) {
 	if (!init_streams(ffm)) {
 		free_avformat(ffm);
 		free_avformat(ffm);

+ 18 - 1
plugins/obs-ffmpeg/obs-ffmpeg-output.c

@@ -543,6 +543,7 @@ static enum AVCodecID get_codec_id(const char *name, int id)
 	return codec->id;
 	return codec->id;
 }
 }
 
 
+#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
 static void set_encoder_ids(struct ffmpeg_data *data)
 static void set_encoder_ids(struct ffmpeg_data *data)
 {
 {
 	data->output->oformat->video_codec = get_codec_id(
 	data->output->oformat->video_codec = get_codec_id(
@@ -551,6 +552,7 @@ static void set_encoder_ids(struct ffmpeg_data *data)
 	data->output->oformat->audio_codec = get_codec_id(
 	data->output->oformat->audio_codec = get_codec_id(
 		data->config.audio_encoder, data->config.audio_encoder_id);
 		data->config.audio_encoder, data->config.audio_encoder_id);
 }
 }
+#endif
 
 
 bool ffmpeg_data_init(struct ffmpeg_data *data, struct ffmpeg_cfg *config)
 bool ffmpeg_data_init(struct ffmpeg_data *data, struct ffmpeg_cfg *config)
 {
 {
@@ -570,7 +572,13 @@ bool ffmpeg_data_init(struct ffmpeg_data *data, struct ffmpeg_cfg *config)
 
 
 	is_rtmp = (astrcmpi_n(config->url, "rtmp://", 7) == 0);
 	is_rtmp = (astrcmpi_n(config->url, "rtmp://", 7) == 0);
 
 
-	AVOutputFormat *output_format = av_guess_format(
+#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
+	AVOutputFormat *output_format;
+#else
+	const AVOutputFormat *output_format;
+#endif
+
+	output_format = av_guess_format(
 		is_rtmp ? "flv" : data->config.format_name, data->config.url,
 		is_rtmp ? "flv" : data->config.format_name, data->config.url,
 		is_rtmp ? NULL : data->config.format_mime_type);
 		is_rtmp ? NULL : data->config.format_mime_type);
 
 
@@ -596,6 +604,7 @@ bool ffmpeg_data_init(struct ffmpeg_data *data, struct ffmpeg_cfg *config)
 		goto fail;
 		goto fail;
 	}
 	}
 
 
+#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
 	if (is_rtmp) {
 	if (is_rtmp) {
 		data->output->oformat->video_codec = AV_CODEC_ID_H264;
 		data->output->oformat->video_codec = AV_CODEC_ID_H264;
 		data->output->oformat->audio_codec = AV_CODEC_ID_AAC;
 		data->output->oformat->audio_codec = AV_CODEC_ID_AAC;
@@ -603,6 +612,14 @@ bool ffmpeg_data_init(struct ffmpeg_data *data, struct ffmpeg_cfg *config)
 		if (data->config.format_name)
 		if (data->config.format_name)
 			set_encoder_ids(data);
 			set_encoder_ids(data);
 	}
 	}
+#else
+	if (is_rtmp) {
+		data->config.audio_encoder = "aac";
+		data->config.audio_encoder_id = AV_CODEC_ID_AAC;
+		data->config.video_encoder = "libx264";
+		data->config.video_encoder_id = AV_CODEC_ID_H264;
+	}
+#endif
 
 
 	if (!init_streams(data))
 	if (!init_streams(data))
 		goto fail;
 		goto fail;