]> git.sesse.net Git - vlc/blob - modules/audio_output/amem.c
Use float for pf_volume_set volume
[vlc] / modules / audio_output / amem.c
1 /*****************************************************************************
2  * amem.c : virtual LibVLC audio output plugin
3  *****************************************************************************
4  * Copyright (C) 2011 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it 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 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
18  * Foundation, 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 <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_aout.h>
28
29 static int Open (vlc_object_t *);
30 static void Close (vlc_object_t *);
31
32 vlc_module_begin ()
33     set_shortname (N_("Audio memory"))
34     set_description (N_("Audio memory output"))
35     set_capability ("audio output", 0)
36     set_category (CAT_AUDIO)
37     set_subcategory (SUBCAT_AUDIO_AOUT)
38     set_callbacks (Open, Close)
39
40     add_string ("amem-format", "S16N",
41                 N_("Sample format"), N_("Sample format"), false)
42         change_private()
43     add_integer ("amem-rate", 44100,
44                  N_("Sample rate"), N_("Sample rate"), false)
45         change_integer_range (1, 192000)
46         change_private()
47     add_integer ("amem-channels", 2,
48                  N_("Channels count"), N_("Channels count"), false)
49         change_integer_range (1, AOUT_CHAN_MAX)
50         change_private()
51
52 vlc_module_end ()
53
54 struct aout_sys_t
55 {
56     void *opaque;
57     void (*play) (void *opaque, const void *data, unsigned count, int64_t pts);
58     int (*set_volume) (void *opaque, float vol, bool mute);
59     void (*cleanup) (void *opaque);
60 };
61
62 static void Play (aout_instance_t *aout)
63 {
64     aout_sys_t *sys = aout->output.p_sys;
65     block_t *block;
66
67     while ((block = aout_FifoPop(&aout->output.fifo)) != NULL)
68     {
69         sys->play (sys->opaque, block->p_buffer, block->i_nb_samples,
70                    block->i_pts);
71         block_Release (block);
72     }
73 }
74
75 static int VolumeSet (aout_instance_t *aout, float vol, bool mute)
76 {
77     aout_sys_t *sys = aout->output.p_sys;
78
79     return sys->set_volume (sys->opaque, vol, mute) ? -1 : 0;
80 }
81
82 typedef int (*vlc_audio_format_cb) (void **, char *, unsigned *, unsigned *);
83
84 static int Open (vlc_object_t *obj)
85 {
86     aout_instance_t *aout = (aout_instance_t *)obj;
87     aout_sys_t *sys = malloc (sizeof (*sys));
88     if (unlikely(sys == NULL))
89         return VLC_ENOMEM;
90
91     aout->output.p_sys = sys;
92     sys->opaque = var_InheritAddress (obj, "amem-data");
93     sys->play = var_InheritAddress (obj, "amem-play");
94     sys->set_volume = var_InheritAddress (obj, "amem-set-volume");
95     sys->cleanup = NULL; /* defer */
96     if (sys->play == NULL)
97         goto error;
98
99     vlc_audio_format_cb setup = var_InheritAddress (obj, "amem-setup");
100     char format[5] = "S16N";
101     unsigned rate, channels;
102
103     if (setup != NULL)
104     {
105         rate = aout->output.output.i_rate;
106         channels = aout_FormatNbChannels(&aout->output.output);
107
108         if (setup (&sys->opaque, format, &rate, &channels))
109             goto error;
110         /* Only call this callback if setup succeeded */ 
111         sys->cleanup = var_InheritAddress (obj, "amem-cleanup");
112     }
113     else
114     {
115         rate = var_InheritInteger (obj, "amem-rate");
116         channels = var_InheritInteger (obj, "amem-channels");
117     }
118
119     if (rate == 0 || rate > 192000
120      || channels == 0 || channels > AOUT_CHAN_MAX)
121         goto error;
122
123     /* TODO: amem-format */
124     /* FIXME/TODO channel mapping */
125     if (strcmp(format, "S16N") || aout->output.output.i_channels != channels)
126     {
127         msg_Err (aout, "format not supported");
128         goto error;
129     }
130     aout->output.output.i_format = VLC_CODEC_S16N;
131     aout->output.output.i_rate = rate;
132
133     aout->output.pf_play = Play;
134     aout->output.pf_pause = NULL;
135     if (sys->set_volume != NULL)
136         aout->output.pf_volume_set = VolumeSet;
137     else
138         aout_VolumeSoftInit (aout);
139     return VLC_SUCCESS;
140
141 error:
142     Close (obj);
143     return VLC_EGENERIC;
144 }
145
146 static void Close (vlc_object_t *obj)
147 {
148     aout_instance_t *aout = (aout_instance_t *)obj;
149     aout_sys_t *sys = aout->output.p_sys;
150
151     if (sys->cleanup != NULL)
152         sys->cleanup (sys->opaque);
153     free (sys);
154 }