From 918a2ca9e164fd76822aa74c8b0bda5110c94f50 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 28 Jul 2024 21:46:28 +0200 Subject: [PATCH] Constify FFmpeg callbacks. FFmpeg changed their prototypes in 7.0, so follow suit. Supporting both old and new code is a bit annoying, so we need a new header. --- futatabi/video_stream.cpp | 4 ++-- futatabi/video_stream.h | 5 +++-- nageru/kaeru.cpp | 3 ++- nageru/mjpeg_encoder.cpp | 4 ++-- nageru/mjpeg_encoder.h | 5 +++-- nageru/video_encoder.cpp | 8 ++++---- nageru/video_encoder.h | 9 +++++---- shared/ff_maybe_const.h | 13 +++++++++++++ 8 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 shared/ff_maybe_const.h diff --git a/futatabi/video_stream.cpp b/futatabi/video_stream.cpp index c12acdf..d06e765 100644 --- a/futatabi/video_stream.cpp +++ b/futatabi/video_stream.cpp @@ -849,13 +849,13 @@ void VideoStream::encode_thread_func() } } -int VideoStream::write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) +int VideoStream::write_packet2_thunk(void *opaque, FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) { VideoStream *video_stream = (VideoStream *)opaque; return video_stream->write_packet2(buf, buf_size, type, time); } -int VideoStream::write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) +int VideoStream::write_packet2(FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) { if (type == AVIO_DATA_MARKER_SYNC_POINT || type == AVIO_DATA_MARKER_BOUNDARY_POINT) { seen_sync_markers = true; diff --git a/futatabi/video_stream.h b/futatabi/video_stream.h index c83a484..a96cac8 100644 --- a/futatabi/video_stream.h +++ b/futatabi/video_stream.h @@ -14,6 +14,7 @@ extern "C" { #include "queue_spot_holder.h" #include "shared/mux.h" #include "shared/ref_counted_gl_sync.h" +#include "shared/ff_maybe_const.h" #include #include @@ -76,8 +77,8 @@ private: std::thread encode_thread; std::atomic should_quit{ false }; - static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time); - int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time); + static int write_packet2_thunk(void *opaque, FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time); + int write_packet2(FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time); void add_silence(int64_t pts, int64_t length_pts); void add_audio_or_silence(const QueuedFrame &qf); diff --git a/nageru/kaeru.cpp b/nageru/kaeru.cpp index caa71c6..976333c 100644 --- a/nageru/kaeru.cpp +++ b/nageru/kaeru.cpp @@ -30,6 +30,7 @@ #include #include #include +#include "shared/ff_maybe_const.h" extern "C" { #include @@ -62,7 +63,7 @@ MuxMetrics stream_mux_metrics; namespace { -int write_packet(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) +int write_packet(void *opaque, FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) { static bool seen_sync_markers = false; static string stream_mux_header; diff --git a/nageru/mjpeg_encoder.cpp b/nageru/mjpeg_encoder.cpp index 71af169..b871af2 100644 --- a/nageru/mjpeg_encoder.cpp +++ b/nageru/mjpeg_encoder.cpp @@ -157,13 +157,13 @@ struct VectorDestinationManager { }; static_assert(std::is_standard_layout::value, ""); -int MJPEGEncoder::write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) +int MJPEGEncoder::write_packet2_thunk(void *opaque, FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) { WritePacket2Context *ctx = (WritePacket2Context *)opaque; return ctx->mjpeg_encoder->write_packet2(ctx->stream_id, buf, buf_size, type, time); } -int MJPEGEncoder::write_packet2(HTTPD::StreamID stream_id, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) +int MJPEGEncoder::write_packet2(HTTPD::StreamID stream_id, FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) { string *mux_header = &streams[stream_id].mux_header; if (type == AVIO_DATA_MARKER_HEADER) { diff --git a/nageru/mjpeg_encoder.h b/nageru/mjpeg_encoder.h index 896b63b..af03b28 100644 --- a/nageru/mjpeg_encoder.h +++ b/nageru/mjpeg_encoder.h @@ -2,6 +2,7 @@ #define _MJPEG_ENCODER_H 1 #include "shared/ffmpeg_raii.h" +#include "shared/ff_maybe_const.h" #include "shared/httpd.h" #include "shared/va_resource_pool.h" #include "ref_counted_frame.h" @@ -84,8 +85,8 @@ private: HTTPD::StreamID stream_id; }; std::map ffmpeg_contexts; // Statically set up, so we never need to worry about dangling pointers. - static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time); - int write_packet2(HTTPD::StreamID stream_id, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time); + static int write_packet2_thunk(void *opaque, FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time); + int write_packet2(HTTPD::StreamID stream_id, FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time); std::thread encoder_thread, va_receiver_thread; diff --git a/nageru/video_encoder.cpp b/nageru/video_encoder.cpp index b68086c..873b6b2 100644 --- a/nageru/video_encoder.cpp +++ b/nageru/video_encoder.cpp @@ -290,13 +290,13 @@ void VideoEncoder::open_output_streams() } } -int VideoEncoder::write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) +int VideoEncoder::write_packet2_thunk(void *opaque, FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) { VideoEncoder *video_encoder = (VideoEncoder *)opaque; return video_encoder->write_packet2(buf, buf_size, type, time); } -int VideoEncoder::write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) +int VideoEncoder::write_packet2(FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time) { if (type == AVIO_DATA_MARKER_SYNC_POINT || type == AVIO_DATA_MARKER_BOUNDARY_POINT) { seen_sync_markers = true; @@ -315,7 +315,7 @@ int VideoEncoder::write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType t return buf_size; } -int VideoEncoder::write_srt_packet_thunk(void *opaque, uint8_t *buf, int buf_size) +int VideoEncoder::write_srt_packet_thunk(void *opaque, FF_MAYBE_CONST uint8_t *buf, int buf_size) { VideoEncoder *video_encoder = (VideoEncoder *)opaque; return video_encoder->write_srt_packet(buf, buf_size); @@ -473,7 +473,7 @@ int VideoEncoder::connect_to_srt() return -1; } -int VideoEncoder::write_srt_packet(uint8_t *buf, int buf_size) +int VideoEncoder::write_srt_packet(FF_MAYBE_CONST uint8_t *buf, int buf_size) { if (want_srt_metric_update.exchange(false) && srt_sock != -1) { srt_metrics.update_srt_stats(srt_sock); diff --git a/nageru/video_encoder.h b/nageru/video_encoder.h index 6301f50..c9eaf2f 100644 --- a/nageru/video_encoder.h +++ b/nageru/video_encoder.h @@ -23,6 +23,7 @@ extern "C" { #include +#include "shared/ff_maybe_const.h" #include "shared/mux.h" #include "shared/ref_counted_gl_sync.h" #include "srt_metrics.h" @@ -80,11 +81,11 @@ public: private: void open_output_streams(); - static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time); - int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time); + static int write_packet2_thunk(void *opaque, FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time); + int write_packet2(FF_MAYBE_CONST uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time); - static int write_srt_packet_thunk(void *opaque, uint8_t *buf, int buf_size); - int write_srt_packet(uint8_t *buf, int buf_size); + static int write_srt_packet_thunk(void *opaque, FF_MAYBE_CONST uint8_t *buf, int buf_size); + int write_srt_packet(FF_MAYBE_CONST uint8_t *buf, int buf_size); int open_srt_socket(); // Returns -1 on error. int connect_to_srt(); // Returns -1 on error. diff --git a/shared/ff_maybe_const.h b/shared/ff_maybe_const.h new file mode 100644 index 0000000..64411c3 --- /dev/null +++ b/shared/ff_maybe_const.h @@ -0,0 +1,13 @@ +#ifndef _FF_MAYBE_CONST +#define _FF_MAYBE_CONST 1 + +// Used as a shim between FFmpeg pre- and post-7.0. +#include + +#if LIBAVFORMAT_VERSION_MAJOR < 61 +#define FF_MAYBE_CONST +#else +#define FF_MAYBE_CONST const +#endif + +#endif // !defined(_FF_MAYBE_CONST) -- 2.39.5