1
0
mirror of https://github.com/mpv-player/mpv synced 2025-03-25 04:38:01 +00:00

audio: change a detail about filter insertion

The af_add() function has a problem: if the inserted filter returns
AF_DETACH during init, the function will have a dangling pointer. Until
now this was avoided by making sure none of the used filters actually
return AF_DETACH, but it's getting infeasible.

Solve this by requiring passing an unique label to af_add(), which is
then used instead of the pointer.
This commit is contained in:
wm4 2015-04-07 21:23:23 +02:00
parent e98ab5e596
commit 579c4dac34
4 changed files with 15 additions and 28 deletions

View File

@ -689,8 +689,14 @@ int af_init(struct af_stream *s)
to the stream s. The filter will be inserted somewhere nice in the
list of filters. The return value is a pointer to the new filter,
If the filter couldn't be added the return value is NULL. */
struct af_instance *af_add(struct af_stream *s, char *name, char **args)
struct af_instance *af_add(struct af_stream *s, char *name, char *label,
char **args)
{
assert(label);
if (af_find_by_label(s, label))
return NULL;
struct af_instance *new;
// Insert the filter somewhere nice
if (af_is_conversion_filter(s->first->next))
@ -699,17 +705,14 @@ struct af_instance *af_add(struct af_stream *s, char *name, char **args)
new = af_prepend(s, s->first->next, name, args);
if (!new)
return NULL;
new->label = talloc_strdup(new, label);
// Reinitalize the filter list
if (af_reinit(s) != AF_OK) {
af_remove(s, new);
if (af_reinit(s) != AF_OK) {
af_uninit(s);
af_init(s);
}
af_remove_by_label(s, label);
return NULL;
}
return new;
return af_find_by_label(s, label);
}
struct af_instance *af_find_by_label(struct af_stream *s, char *label)

View File

@ -141,7 +141,8 @@ struct af_stream *af_new(struct mpv_global *global);
void af_destroy(struct af_stream *s);
int af_init(struct af_stream *s);
void af_uninit(struct af_stream *s);
struct af_instance *af_add(struct af_stream *s, char *name, char **args);
struct af_instance *af_add(struct af_stream *s, char *name, char *label,
char **args);
int af_remove_by_label(struct af_stream *s, char *label);
struct af_instance *af_find_by_label(struct af_stream *s, char *label);
struct af_instance *af_control_any_rev(struct af_stream *s, int cmd, void *arg);

View File

@ -135,7 +135,7 @@ static void setvolume_internal(struct mixer *mixer, float l, float r)
if (gain == 1.0)
return;
MP_VERBOSE(mixer, "Inserting volume filter.\n");
if (!(af_add(mixer->af, "volume", NULL)
if (!(af_add(mixer->af, "volume", "softvol", NULL)
&& af_control_any_rev(mixer->af, AF_CONTROL_SET_VOLUME, &gain)))
MP_ERR(mixer, "No volume control available.\n");
}
@ -222,7 +222,7 @@ void mixer_setbalance(struct mixer *mixer, float val)
if (val == 0)
return;
if (!(af_pan_balance = af_add(mixer->af, "pan", NULL))) {
if (!(af_pan_balance = af_add(mixer->af, "pan", "autopan", NULL))) {
MP_ERR(mixer, "No balance control available.\n");
return;
}

View File

@ -43,23 +43,6 @@
#include "core.h"
#include "command.h"
static int try_filter(struct MPContext *mpctx,
char *name, char *label, char **args)
{
struct dec_audio *d_audio = mpctx->d_audio;
if (af_find_by_label(d_audio->afilter, label))
return 0;
struct af_instance *af = af_add(d_audio->afilter, name, args);
if (!af)
return -1;
af->label = talloc_strdup(af, label);
return 1;
}
static int update_playback_speed_filters(struct MPContext *mpctx)
{
struct MPOpts *opts = mpctx->opts;
@ -90,7 +73,7 @@ static int update_playback_speed_filters(struct MPContext *mpctx)
char *filter = method == AF_CONTROL_SET_PLAYBACK_SPEED
? "scaletempo" : "lavrresample";
if (try_filter(mpctx, filter, "playback-speed", NULL) < 0)
if (af_add(afs, filter, "playback-speed", NULL) < 0)
return -1;
// Try again.
if (!af_control_any_rev(afs, method, &speed))