Coverage Report

Created: 2025-08-29 07:30

/src/vlc/src/player/aout.c
Line
Count
Source (jump to first uncovered line)
1
/*****************************************************************************
2
 * player_aout.c: Player aout implementation
3
 *****************************************************************************
4
 * Copyright © 2018-2019 VLC authors and VideoLAN
5
 *
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.
10
 *
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.
15
 *
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
 *****************************************************************************/
20
21
#ifdef HAVE_CONFIG_H
22
# include "config.h"
23
#endif
24
25
#include <limits.h>
26
27
#include <vlc_common.h>
28
#include <vlc_decoder.h>
29
#include "player.h"
30
#include "input/resource.h"
31
32
0
#define vlc_player_aout_SendEvent(player, event, ...) do { \
33
0
    vlc_mutex_lock(&player->aout_listeners_lock); \
34
0
    vlc_player_aout_listener_id *listener; \
35
0
    vlc_list_foreach(listener, &player->aout_listeners, node) \
36
0
    { \
37
0
        if (listener->cbs->event) \
38
0
            listener->cbs->event(__VA_ARGS__, listener->cbs_data); \
39
0
    } \
40
0
    vlc_mutex_unlock(&player->aout_listeners_lock); \
41
0
} while(0)
42
43
audio_output_t *
44
vlc_player_aout_Hold(vlc_player_t *player)
45
0
{
46
0
    return input_resource_HoldAout(player->resource);
47
0
}
48
49
vlc_player_aout_listener_id *
50
vlc_player_aout_AddListener(vlc_player_t *player,
51
                            const struct vlc_player_aout_cbs *cbs,
52
                            void *cbs_data)
53
0
{
54
0
    assert(cbs);
55
56
0
    vlc_player_aout_listener_id *listener = malloc(sizeof(*listener));
57
0
    if (!listener)
58
0
        return NULL;
59
60
0
    listener->cbs = cbs;
61
0
    listener->cbs_data = cbs_data;
62
63
0
    vlc_mutex_lock(&player->aout_listeners_lock);
64
0
    vlc_list_append(&listener->node, &player->aout_listeners);
65
0
    vlc_mutex_unlock(&player->aout_listeners_lock);
66
67
0
    return listener;
68
0
}
69
70
void
71
vlc_player_aout_RemoveListener(vlc_player_t *player,
72
                               vlc_player_aout_listener_id *id)
73
0
{
74
0
    assert(id);
75
76
0
    vlc_mutex_lock(&player->aout_listeners_lock);
77
0
    vlc_list_remove(&id->node);
78
0
    vlc_mutex_unlock(&player->aout_listeners_lock);
79
0
    free(id);
80
0
}
81
82
static int
83
vlc_player_AoutCallback(vlc_object_t *this, const char *var,
84
                        vlc_value_t oldval, vlc_value_t newval, void *data)
85
0
{
86
0
    vlc_player_t *player = data;
87
88
0
    if (strcmp(var, "volume") == 0)
89
0
    {
90
0
        if (oldval.f_float != newval.f_float)
91
0
        {
92
0
            vlc_player_aout_SendEvent(player, on_volume_changed,
93
0
                                      (audio_output_t *)this, newval.f_float);
94
0
            vlc_player_osd_Volume(player, false);
95
0
        }
96
0
    }
97
0
    else if (strcmp(var, "mute") == 0)
98
0
    {
99
0
        if (oldval.b_bool != newval.b_bool)
100
0
        {
101
0
            vlc_player_aout_SendEvent(player, on_mute_changed,
102
0
                                      (audio_output_t *)this, newval.b_bool);
103
0
            vlc_player_osd_Volume(player, true);
104
0
        }
105
0
    }
106
0
    else if (strcmp(var, "device") == 0)
107
0
    {
108
0
        const char *old = oldval.psz_string;
109
0
        const char *new = newval.psz_string;
110
        /* support NULL values for string comparison */
111
0
        if (old != new && (!old || !new || strcmp(old, new)))
112
0
            vlc_player_aout_SendEvent(player, on_device_changed,
113
0
                                      (audio_output_t *)this, newval.psz_string);
114
0
    }
115
0
    else
116
0
        vlc_assert_unreachable();
117
118
0
    return VLC_SUCCESS;
119
0
    (void) this;
120
0
}
121
122
float
123
vlc_player_aout_GetVolume(vlc_player_t *player)
124
0
{
125
0
    audio_output_t *aout = vlc_player_aout_Hold(player);
126
0
    if (!aout)
127
0
        return -1.f;
128
0
    float vol = aout_VolumeGet(aout);
129
0
    aout_Release(aout);
130
131
0
    return vol;
132
0
}
133
134
int
135
vlc_player_aout_SetVolume(vlc_player_t *player, float volume)
136
0
{
137
0
    audio_output_t *aout = vlc_player_aout_Hold(player);
138
0
    if (!aout)
139
0
        return -1;
140
0
    int ret = aout_VolumeSet(aout, volume);
141
0
    aout_Release(aout);
142
143
0
    return ret;
144
0
}
145
146
int
147
vlc_player_aout_IncrementVolume(vlc_player_t *player, int steps, float *result)
148
0
{
149
0
    audio_output_t *aout = vlc_player_aout_Hold(player);
150
0
    if (!aout)
151
0
        return -1;
152
0
    int ret = aout_VolumeUpdate(aout, steps, result);
153
0
    aout_Release(aout);
154
155
0
    return ret;
156
0
}
157
158
int
159
vlc_player_aout_IsMuted(vlc_player_t *player)
160
0
{
161
0
    audio_output_t *aout = vlc_player_aout_Hold(player);
162
0
    if (!aout)
163
0
        return -1;
164
0
    int ret = aout_MuteGet(aout);
165
0
    aout_Release(aout);
166
167
0
    return ret;
168
0
}
169
170
int
171
vlc_player_aout_Mute(vlc_player_t *player, bool mute)
172
0
{
173
0
    audio_output_t *aout = vlc_player_aout_Hold(player);
174
0
    if (!aout)
175
0
        return -1;
176
0
    int ret = aout_MuteSet (aout, mute);
177
0
    aout_Release(aout);
178
179
0
    return ret;
180
0
}
181
182
int
183
vlc_player_aout_EnableFilter(vlc_player_t *player, const char *name, bool add)
184
0
{
185
0
    audio_output_t *aout = vlc_player_aout_Hold(player);
186
0
    if (!aout)
187
0
        return -1;
188
0
    aout_EnableFilter(aout, name, add);
189
0
    aout_Release(aout);
190
191
0
    return 0;
192
0
}
193
194
195
static void
196
vlc_player_aout_AddCallbacks(vlc_player_t *player)
197
0
{
198
0
    audio_output_t *aout = vlc_player_aout_Hold(player);
199
0
    if (!aout)
200
0
        return;
201
202
0
    var_AddCallback(aout, "volume", vlc_player_AoutCallback, player);
203
0
    var_AddCallback(aout, "mute", vlc_player_AoutCallback, player);
204
0
    var_AddCallback(aout, "device", vlc_player_AoutCallback, player);
205
206
0
    aout_Release(aout);
207
0
}
208
209
static void
210
vlc_player_aout_DelCallbacks(vlc_player_t *player)
211
0
{
212
0
    audio_output_t *aout = vlc_player_aout_Hold(player);
213
0
    if (!aout)
214
0
        return;
215
216
0
    var_DelCallback(aout, "volume", vlc_player_AoutCallback, player);
217
0
    var_DelCallback(aout, "mute", vlc_player_AoutCallback, player);
218
0
    var_DelCallback(aout, "device", vlc_player_AoutCallback, player);
219
220
0
    aout_Release(aout);
221
0
}
222
223
audio_output_t *
224
vlc_player_aout_Init(vlc_player_t *player)
225
0
{
226
0
    audio_output_t *aout = input_resource_GetAout(player->resource);
227
0
    if (aout != NULL)
228
0
    {
229
0
        vlc_player_aout_AddCallbacks(player);
230
0
        input_resource_PutAout(player->resource, aout);
231
0
    }
232
0
    return aout;
233
0
}
234
235
void
236
vlc_player_aout_Deinit(vlc_player_t *player)
237
0
{
238
0
    vlc_player_aout_DelCallbacks(player);
239
0
}
240
241
void
242
vlc_player_aout_Reset(vlc_player_t *player)
243
0
{
244
0
    vlc_player_aout_Deinit(player);
245
246
0
    input_resource_ResetAout(player->resource);
247
248
0
    vlc_player_aout_Init(player);
249
0
}
250