qt: playlist: use item title if available
[vlc.git] / src / audio_output / meter.c
blob144a6675fe64621608022e00a981fddc2757c6fd
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 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <assert.h>
27 #include <vlc_common.h>
28 #include <vlc_modules.h>
29 #include <vlc_aout.h>
30 #include "aout_internal.h"
32 struct vlc_audio_meter_plugin
34 char *name;
35 config_chain_t *cfg;
36 filter_t *filter;
37 vlc_tick_t last_date;
39 struct vlc_audio_meter_plugin_owner owner;
41 struct vlc_list node;
44 void
45 (vlc_audio_meter_Init)(struct vlc_audio_meter *meter, vlc_object_t *obj)
47 vlc_mutex_init(&meter->lock);
48 meter->parent = obj;
49 meter->fmt = NULL;
50 vlc_list_init(&meter->plugins);
53 void
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);
61 static void
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);
71 static filter_t *
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 = {
79 .audio = &audio_cbs,
80 .sys = plugin,
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));
94 if (plugin == NULL)
95 return NULL;
96 plugin->owner = *owner;
97 plugin->last_date = VLC_TICK_INVALID;
98 plugin->name = NULL;
99 plugin->cfg = NULL;
100 plugin->filter = NULL;
102 free(config_ChainCreate(&plugin->name, &plugin->cfg, chain));
103 if (plugin->name == NULL)
104 goto error;
106 if (meter->fmt != NULL)
108 plugin->filter = vlc_audio_meter_CreatePluginFilter(meter, plugin);
109 if (plugin->filter == NULL)
110 goto error;
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);
119 return plugin;
121 error:
122 free(plugin->name);
123 if (plugin->cfg != NULL)
124 config_ChainDestroy(plugin->cfg);
125 free(plugin);
126 return NULL;
129 void
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);
143 free(plugin->name);
145 vlc_list_remove(&plugin->node);
146 free(plugin);
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;
156 meter->fmt = fmt;
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)
178 ret = VLC_EGENERIC;
179 break;
184 vlc_mutex_unlock(&meter->lock);
186 return ret;
189 void
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;
199 if (filter != NULL)
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);
211 void
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;
220 if (filter != NULL)
221 filter_Flush(filter);
224 vlc_mutex_unlock(&meter->lock);