]> git.sesse.net Git - vlc/blob - modules/audio_output/amem.c
cpu: do not define capabilities on platforms that do not have them
[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 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 <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_aout.h>
28 #include <assert.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     void (*pause) (void *opaque, int64_t pts);
60     void (*resume) (void *opaque, int64_t pts);
61     void (*flush) (void *opaque);
62     void (*drain) (void *opaque);
63     int (*set_volume) (void *opaque, float vol, bool mute);
64     void (*cleanup) (void *opaque);
65     float volume;
66     bool mute;
67 };
68
69 static void Play (audio_output_t *aout, block_t *block,
70                   mtime_t *restrict drift)
71 {
72     aout_sys_t *sys = aout->sys;
73
74     sys->play (sys->opaque, block->p_buffer, block->i_nb_samples,
75                block->i_pts);
76     block_Release (block);
77     (void) drift;
78 }
79
80 static void Pause (audio_output_t *aout, bool paused, mtime_t date)
81 {
82     aout_sys_t *sys = aout->sys;
83     void (*cb) (void *, int64_t) = paused ? sys->pause : sys->resume;
84
85     if (cb != NULL)
86         cb (sys->opaque, date);
87 }
88
89 static void Flush (audio_output_t *aout, bool wait)
90 {
91     aout_sys_t *sys = aout->sys;
92     void (*cb) (void *) = wait ? sys->drain : sys->flush;
93
94     if (cb != NULL)
95         cb (sys->opaque);
96 }
97
98 static int VolumeSet (audio_output_t *aout, float vol)
99 {
100     aout_sys_t *sys = aout->sys;
101
102     sys->volume = vol;
103     return sys->set_volume (sys->opaque, vol, sys->mute) ? -1 : 0;
104 }
105
106 static int MuteSet (audio_output_t *aout, bool mute)
107 {
108     aout_sys_t *sys = aout->sys;
109
110     sys->mute = mute;
111     return sys->set_volume (sys->opaque, sys->volume, mute) ? -1 : 0;
112 }
113
114 static int SoftVolumeSet (audio_output_t *aout, float vol)
115 {
116     aout_sys_t *sys = aout->sys;
117
118     vol = vol * vol * vol;
119     if (!sys->mute && aout_GainRequest (aout, vol))
120         return -1;
121     sys->volume = vol;
122     return 0;
123 }
124
125 static int SoftMuteSet (audio_output_t *aout, bool mute)
126 {
127     aout_sys_t *sys = aout->sys;
128
129     if (aout_GainRequest (aout, mute ? 0.f : sys->volume))
130         return -1;
131     sys->mute = mute;
132     return 0;
133 }
134
135 typedef int (*vlc_audio_format_cb) (void **, char *, unsigned *, unsigned *);
136
137 static int Open (vlc_object_t *obj)
138 {
139     audio_output_t *aout = (audio_output_t *)obj;
140     aout_sys_t *sys = malloc (sizeof (*sys));
141     if (unlikely(sys == NULL))
142         return VLC_ENOMEM;
143
144     aout->sys = sys;
145     sys->opaque = var_InheritAddress (obj, "amem-data");
146     sys->play = var_InheritAddress (obj, "amem-play");
147     sys->pause = var_InheritAddress (obj, "amem-pause");
148     sys->resume = var_InheritAddress (obj, "amem-resume");
149     sys->flush = var_InheritAddress (obj, "amem-flush");
150     sys->drain = var_InheritAddress (obj, "amem-drain");
151     sys->set_volume = var_InheritAddress (obj, "amem-set-volume");
152     sys->cleanup = NULL; /* defer */
153     sys->volume = 1.;
154     sys->mute = false;
155     if (sys->play == NULL)
156         goto error;
157
158     vlc_audio_format_cb setup = var_InheritAddress (obj, "amem-setup");
159     char format[5] = "S16N";
160     unsigned rate, channels;
161
162     if (setup != NULL)
163     {
164         rate = aout->format.i_rate;
165         channels = aout_FormatNbChannels(&aout->format);
166
167         if (setup (&sys->opaque, format, &rate, &channels))
168             goto error;
169         /* Only call this callback if setup succeeded */
170         sys->cleanup = var_InheritAddress (obj, "amem-cleanup");
171     }
172     else
173     {
174         rate = var_InheritInteger (obj, "amem-rate");
175         channels = var_InheritInteger (obj, "amem-channels");
176     }
177
178     if (rate == 0 || rate > 192000
179      || channels == 0 || channels > AOUT_CHAN_MAX)
180         goto error;
181
182     /* TODO: amem-format */
183     if (strcmp(format, "S16N"))
184     {
185         msg_Err (aout, "format not supported");
186         goto error;
187     }
188
189     /* channel mapping */
190     switch (channels)
191     {
192         case 1:
193             aout->format.i_physical_channels = AOUT_CHAN_CENTER;
194             break;
195         case 2:
196             aout->format.i_physical_channels =
197                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
198             break;
199         case 3:
200             aout->format.i_physical_channels =
201                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
202             break;
203         case 4:
204             aout->format.i_physical_channels =
205                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
206                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
207             break;
208         case 5:
209             aout->format.i_physical_channels =
210                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
211                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
212             break;
213         case 6:
214             aout->format.i_physical_channels =
215                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
216                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
217             break;
218         case 7:
219             aout->format.i_physical_channels =
220                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
221                 AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT |
222                 AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
223             break;
224         case 8:
225             aout->format.i_physical_channels =
226                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
227                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
228                 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
229             break;
230         default:
231             assert(0);
232     }
233
234     aout->format.i_format = VLC_CODEC_S16N;
235     aout->format.i_rate = rate;
236     aout->format.i_original_channels = aout->format.i_physical_channels;
237
238     aout->pf_play = Play;
239     aout->pf_pause = Pause;
240     aout->pf_flush = Flush;
241     if (sys->set_volume != NULL)
242     {
243         aout->volume_set = VolumeSet;
244         aout->mute_set = MuteSet;
245     }
246     else
247     {
248         aout->volume_set = SoftVolumeSet;
249         aout->mute_set = SoftMuteSet;
250     }
251     return VLC_SUCCESS;
252
253 error:
254     Close (obj);
255     return VLC_EGENERIC;
256 }
257
258 static void Close (vlc_object_t *obj)
259 {
260     audio_output_t *aout = (audio_output_t *)obj;
261     aout_sys_t *sys = aout->sys;
262
263     if (sys->cleanup != NULL)
264         sys->cleanup (sys->opaque);
265     free (sys);
266 }