Skip to content

Commit

Permalink
mock: add a free callback to options
Browse files Browse the repository at this point in the history
Even if for now, we have only one string (that was freed correctly).

Add the possibility to use strings in track options without leaking when
the option is overridden.
  • Loading branch information
tguillem authored and robUx4 committed Jul 6, 2024
1 parent 97775e7 commit a088bc2
Showing 1 changed file with 65 additions and 52 deletions.
117 changes: 65 additions & 52 deletions modules/demux/mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,74 +183,77 @@ var_Read_float(const char *psz)
return atof(psz);
}

#define FREE_CB(x) free(x)
#define NO_FREE(x) (void) x

#define OPTIONS_AUDIO(Y) \
Y(audio, packetized, bool, add_bool, Bool, true) \
Y(audio, add_track_at, vlc_tick_t, add_integer, Integer, VLC_TICK_INVALID) \
Y(audio, channels, unsigned, add_integer, Unsigned, 2) \
Y(audio, format, vlc_fourcc_t, add_string, Fourcc, "f32l") \
Y(audio, rate, unsigned, add_integer, Unsigned, 48000) \
Y(audio, sample_length, vlc_tick_t, add_integer, Integer, VLC_TICK_FROM_MS(40)) \
Y(audio, sinewave, bool, add_bool, Bool, true) \
Y(audio, sinewave_frequency, unsigned, add_integer, Integer, 500) \
Y(audio, sinewave_amplitude, float, add_float, Float, 0.2)
Y(audio, packetized, bool, add_bool, Bool, true, NO_FREE) \
Y(audio, add_track_at, vlc_tick_t, add_integer, Integer, VLC_TICK_INVALID, NO_FREE) \
Y(audio, channels, unsigned, add_integer, Unsigned, 2, NO_FREE) \
Y(audio, format, vlc_fourcc_t, add_string, Fourcc, "f32l", NO_FREE) \
Y(audio, rate, unsigned, add_integer, Unsigned, 48000, NO_FREE) \
Y(audio, sample_length, vlc_tick_t, add_integer, Integer, VLC_TICK_FROM_MS(40), NO_FREE) \
Y(audio, sinewave, bool, add_bool, Bool, true, NO_FREE) \
Y(audio, sinewave_frequency, unsigned, add_integer, Integer, 500, NO_FREE) \
Y(audio, sinewave_amplitude, float, add_float, Float, 0.2, NO_FREE)

#define OPTIONS_VIDEO(Y) \
Y(video, packetized, bool, add_bool, Bool, true)\
Y(video, add_track_at, vlc_tick_t, add_integer, Integer, VLC_TICK_INVALID) \
Y(video, chroma, vlc_fourcc_t, add_string, Fourcc, "I420") \
Y(video, width, unsigned, add_integer, Unsigned, 640) \
Y(video, height, unsigned, add_integer, Unsigned, 480) \
Y(video, frame_rate, unsigned, add_integer, Unsigned, 25) \
Y(video, frame_rate_base, unsigned, add_integer, Unsigned, 1) \
Y(video, colorbar, bool, add_bool, Bool, false) \
Y(video, orientation, unsigned, add_integer, Unsigned, ORIENT_NORMAL) \
Y(video, image_count, unsigned, add_integer, Unsigned, 0)
Y(video, packetized, bool, add_bool, Bool, true, NO_FREE) \
Y(video, add_track_at, vlc_tick_t, add_integer, Integer, VLC_TICK_INVALID, NO_FREE) \
Y(video, chroma, vlc_fourcc_t, add_string, Fourcc, "I420", NO_FREE) \
Y(video, width, unsigned, add_integer, Unsigned, 640, NO_FREE) \
Y(video, height, unsigned, add_integer, Unsigned, 480, NO_FREE) \
Y(video, frame_rate, unsigned, add_integer, Unsigned, 25, NO_FREE) \
Y(video, frame_rate_base, unsigned, add_integer, Unsigned, 1, NO_FREE) \
Y(video, colorbar, bool, add_bool, Bool, false, NO_FREE) \
Y(video, orientation, unsigned, add_integer, Unsigned, ORIENT_NORMAL, NO_FREE) \
Y(video, image_count, unsigned, add_integer, Unsigned, 0, NO_FREE)

#define OPTIONS_SUB(Y) \
Y(sub, packetized, bool, add_bool, Bool, true)\
Y(sub, add_track_at, vlc_tick_t, add_integer, Integer, VLC_TICK_INVALID) \
Y(sub, format, vlc_fourcc_t, add_string, Fourcc, "subt") \
Y(sub, page, unsigned, add_integer, Integer, 0)
Y(sub, packetized, bool, add_bool, Bool, true, NO_FREE) \
Y(sub, add_track_at, vlc_tick_t, add_integer, Integer, VLC_TICK_INVALID, NO_FREE) \
Y(sub, format, vlc_fourcc_t, add_string, Fourcc, "subt", NO_FREE) \
Y(sub, page, unsigned, add_integer, Integer, 0, NO_FREE)

/* var_name, type, module_header_type, getter, default_value */
#define OPTIONS_GLOBAL(X) \
X(node_count, ssize_t, add_integer, Ssize, 0) \
X(length, vlc_tick_t, add_integer, Integer, VLC_TICK_FROM_MS(5000)) \
X(audio_track_count, ssize_t, add_integer, Ssize, 0) \
X(video_track_count, ssize_t, add_integer, Ssize, 0) \
X(sub_track_count, ssize_t, add_integer, Ssize, 0) \
X(input_sample_length, vlc_tick_t, add_integer, Integer, VLC_TICK_FROM_MS(40) ) \
X(title_count, ssize_t, add_integer, Ssize, 0 ) \
X(chapter_count, ssize_t, add_integer, Ssize, 0) \
X(null_names, bool, add_bool, Bool, false) \
X(program_count, ssize_t, add_integer, Ssize, 0) \
X(attachment_count, ssize_t, add_integer, Ssize, 0) \
X(can_seek, bool, add_bool, Bool, true) \
X(can_pause, bool, add_bool, Bool, true) \
X(can_control_pace, bool, add_bool, Bool, true) \
X(can_control_rate, bool, add_bool, Bool, true) \
X(can_record, bool, add_bool, Bool, true) \
X(error, bool, add_bool, Bool, false) \
X(pts_delay, vlc_tick_t, add_integer, Unsigned, DEFAULT_PTS_DELAY) \
X(config, char *, add_string, String, NULL )

#define DECLARE_OPTION(var_name, type, module_header_type, getter, default_value)\
X(node_count, ssize_t, add_integer, Ssize, 0, NO_FREE) \
X(length, vlc_tick_t, add_integer, Integer, VLC_TICK_FROM_MS(5000), NO_FREE) \
X(audio_track_count, ssize_t, add_integer, Ssize, 0, NO_FREE) \
X(video_track_count, ssize_t, add_integer, Ssize, 0, NO_FREE) \
X(sub_track_count, ssize_t, add_integer, Ssize, 0, NO_FREE) \
X(input_sample_length, vlc_tick_t, add_integer, Integer, VLC_TICK_FROM_MS(40), NO_FREE) \
X(title_count, ssize_t, add_integer, Ssize, 0, NO_FREE) \
X(chapter_count, ssize_t, add_integer, Ssize, 0, NO_FREE) \
X(null_names, bool, add_bool, Bool, false, NO_FREE) \
X(program_count, ssize_t, add_integer, Ssize, 0, NO_FREE) \
X(attachment_count, ssize_t, add_integer, Ssize, 0, NO_FREE) \
X(can_seek, bool, add_bool, Bool, true, NO_FREE) \
X(can_pause, bool, add_bool, Bool, true, NO_FREE) \
X(can_control_pace, bool, add_bool, Bool, true, NO_FREE) \
X(can_control_rate, bool, add_bool, Bool, true, NO_FREE) \
X(can_record, bool, add_bool, Bool, true, NO_FREE) \
X(error, bool, add_bool, Bool, false, NO_FREE) \
X(pts_delay, vlc_tick_t, add_integer, Unsigned, DEFAULT_PTS_DELAY, NO_FREE) \
X(config, char *, add_string, String, NULL, FREE_CB)

#define DECLARE_OPTION(var_name, type, module_header_type, getter, default_value, free_cb) \
type var_name;
#define DECLARE_SUBOPTION(a,b,c,d,e,f) DECLARE_OPTION(b,c,d,e,f)
#define DECLARE_SUBOPTION(a,b,c,d,e,f,g) DECLARE_OPTION(b,c,d,e,f,g)

#define READ(var_name, member_name, getter) \
sys->member_name = var_Inherit##getter(obj, "mock-"#var_name);
#define READ_OPTION(var_name, type, module_header_type, getter, default_value) \
#define READ_OPTION(var_name, type, module_header_type, getter, default_value, free_cb) \
READ(var_name, var_name, getter)
#define READ_SUBOPTION(group_name, var_name, type, module_header_type, getter, default_value) \
#define READ_SUBOPTION(group_name, var_name, type, module_header_type, getter, default_value, free_cb) \
READ(group_name##_##var_name, group_name.var_name, getter)

#define DECLARE_MODULE_OPTIONS(var_name, type, module_header_type, getter, default_value) \
#define DECLARE_MODULE_OPTIONS(var_name, type, module_header_type, getter, default_value, free_cb) \
module_header_type("mock-"#var_name, default_value, #var_name, NULL) \
change_volatile() \
change_safe()
#define DECLARE_MODULE_SUBOPTIONS(a,b,c,d,e,f) \
DECLARE_MODULE_OPTIONS(a##_##b,c,d,e,f)
#define DECLARE_MODULE_SUBOPTIONS(a,b,c,d,e,f,g) \
DECLARE_MODULE_OPTIONS(a##_##b,c,d,e,f,g)

struct mock_video_options
{
Expand Down Expand Up @@ -1039,9 +1042,10 @@ static int
OverrideTrackOptions(const config_chain_t *config_chain,
struct mock_track *track)
{
#define OVERRIDE_SUBOPTION(group_name, var_name, type, module_header_type, getter, default_value) \
#define OVERRIDE_SUBOPTION(group_name, var_name, type, module_header_type, getter, default_value, free_cb) \
if (!strcmp(""#var_name, config_chain->psz_name)) \
{ \
free_cb(track->group_name.var_name); \
track->group_name.var_name = var_Read_ ## type(config_chain->psz_value); \
break; \
}
Expand Down Expand Up @@ -1430,11 +1434,20 @@ Close(vlc_object_t *obj)
demux_t *demux = (demux_t*)obj;
struct demux_sys *sys = demux->p_sys;

free( sys->config );
#define FREE_OPTIONS(var_name, type, module_header_type, getter, default_value, free_cb) \
free_cb(sys->var_name);
#define FREE_SUBOPTIONS(group_name, var_name, type, module_header_type, getter, default_value, free_cb) \
free_cb(track->group_name.var_name);

OPTIONS_GLOBAL(FREE_OPTIONS);

struct mock_track *track;
vlc_vector_foreach(track, &sys->tracks)
{
OPTIONS_AUDIO(FREE_SUBOPTIONS);
OPTIONS_VIDEO(FREE_SUBOPTIONS);
OPTIONS_SUB(FREE_SUBOPTIONS);

DeleteTrack(demux, track);
}
vlc_vector_clear(&sys->tracks);
Expand Down

0 comments on commit a088bc2

Please sign in to comment.