1 /*****************************************************************************
2 * meter.c : audio meter
3 *****************************************************************************
4 * Copyright (C) 2020 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
27 #include <vlc_common.h>
28 #include <vlc_modules.h>
30 #include "aout_internal.h"
32 struct vlc_audio_meter_plugin
39 struct vlc_audio_meter_plugin_owner owner
;
45 (vlc_audio_meter_Init
)(struct vlc_audio_meter
*meter
, vlc_object_t
*obj
)
47 vlc_mutex_init(&meter
->lock
);
50 vlc_list_init(&meter
->plugins
);
54 vlc_audio_meter_Destroy(struct vlc_audio_meter
*meter
)
56 vlc_audio_meter_plugin
*plugin
;
57 vlc_list_foreach(plugin
, &meter
->plugins
, node
)
58 vlc_audio_meter_RemovePlugin(meter
, plugin
);
62 vlc_audio_meter_OnLoudnessChanged(filter_t
*filter
,
63 const struct vlc_audio_loudness
*loudness
)
65 vlc_audio_meter_plugin
*plugin
= filter
->owner
.sys
;
67 if (plugin
->owner
.cbs
->on_loudness
!= NULL
)
68 plugin
->owner
.cbs
->on_loudness(plugin
->last_date
, loudness
, plugin
->owner
.sys
);
72 vlc_audio_meter_CreatePluginFilter(struct vlc_audio_meter
*meter
, vlc_audio_meter_plugin
*plugin
)
74 static const struct filter_audio_callbacks audio_cbs
= {
75 .meter_loudness
= { .on_changed
= vlc_audio_meter_OnLoudnessChanged
}
78 const filter_owner_t owner
= {
83 return aout_filter_Create(meter
->parent
, &owner
, "audio meter", plugin
->name
,
84 meter
->fmt
, meter
->fmt
, plugin
->cfg
, true);
87 vlc_audio_meter_plugin
*
88 vlc_audio_meter_AddPlugin(struct vlc_audio_meter
*meter
, const char *chain
,
89 const struct vlc_audio_meter_plugin_owner
*owner
)
91 assert(owner
!= NULL
&& owner
->cbs
!= NULL
);
93 vlc_audio_meter_plugin
*plugin
= malloc(sizeof(*plugin
));
96 plugin
->owner
= *owner
;
97 plugin
->last_date
= VLC_TICK_INVALID
;
100 plugin
->filter
= NULL
;
102 free(config_ChainCreate(&plugin
->name
, &plugin
->cfg
, chain
));
103 if (plugin
->name
== NULL
)
106 if (meter
->fmt
!= NULL
)
108 plugin
->filter
= vlc_audio_meter_CreatePluginFilter(meter
, plugin
);
109 if (plugin
->filter
== NULL
)
112 assert(plugin
->filter
->ops
->drain_audio
== NULL
); /* Not supported */
115 vlc_mutex_lock(&meter
->lock
);
116 vlc_list_append(&plugin
->node
, &meter
->plugins
);
117 vlc_mutex_unlock(&meter
->lock
);
123 if (plugin
->cfg
!= NULL
)
124 config_ChainDestroy(plugin
->cfg
);
130 vlc_audio_meter_RemovePlugin(struct vlc_audio_meter
*meter
, vlc_audio_meter_plugin
*plugin
)
132 vlc_mutex_lock(&meter
->lock
);
134 if (plugin
->filter
!= NULL
)
136 filter_Close(plugin
->filter
);
137 module_unneed(plugin
->filter
, plugin
->filter
->p_module
);
138 vlc_object_delete(plugin
->filter
);
141 if (plugin
->cfg
!= NULL
)
142 config_ChainDestroy(plugin
->cfg
);
145 vlc_list_remove(&plugin
->node
);
148 vlc_mutex_unlock(&meter
->lock
);
152 vlc_audio_meter_Reset(struct vlc_audio_meter
*meter
, const audio_sample_format_t
*fmt
)
154 int ret
= VLC_SUCCESS
;
158 vlc_mutex_lock(&meter
->lock
);
160 /* Reload every plugins using the new fmt */
161 vlc_audio_meter_plugin
*plugin
;
162 vlc_list_foreach(plugin
, &meter
->plugins
, node
)
164 if (plugin
->filter
!= NULL
)
166 filter_Close(plugin
->filter
);
167 module_unneed(plugin
->filter
, plugin
->filter
->p_module
);
168 vlc_object_delete(plugin
->filter
);
169 plugin
->filter
= NULL
;
171 plugin
->last_date
= VLC_TICK_INVALID
;
173 if (meter
->fmt
!= NULL
)
175 plugin
->filter
= vlc_audio_meter_CreatePluginFilter(meter
, plugin
);
176 if (plugin
->filter
== NULL
)
184 vlc_mutex_unlock(&meter
->lock
);
190 vlc_audio_meter_Process(struct vlc_audio_meter
*meter
, block_t
*block
, vlc_tick_t date
)
192 vlc_mutex_lock(&meter
->lock
);
194 vlc_audio_meter_plugin
*plugin
;
195 vlc_list_foreach(plugin
, &meter
->plugins
, node
)
197 filter_t
*filter
= plugin
->filter
;
201 plugin
->last_date
= date
+ block
->i_length
;
203 block_t
*same_block
= filter
->ops
->filter_audio(filter
, block
);
204 assert(same_block
== block
); (void) same_block
;
208 vlc_mutex_unlock(&meter
->lock
);
212 vlc_audio_meter_Flush(struct vlc_audio_meter
*meter
)
214 vlc_mutex_lock(&meter
->lock
);
216 vlc_audio_meter_plugin
*plugin
;
217 vlc_list_foreach(plugin
, &meter
->plugins
, node
)
219 filter_t
*filter
= plugin
->filter
;
221 filter_Flush(filter
);
224 vlc_mutex_unlock(&meter
->lock
);