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