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 #define AMEM_SAMPLE_RATE_MAX 384000
34 
35 vlc_module_begin ()
36     set_shortname (N_("Audio memory"))
37     set_description (N_("Audio memory output"))
38     set_capability ("audio output", 0)
39     set_category (CAT_AUDIO)
40     set_subcategory (SUBCAT_AUDIO_AOUT)
41     set_callbacks (Open, Close)
42 
43     add_string ("amem-format", "S16N",
44                 N_("Sample format"), N_("Sample format"), false)
45         change_private()
46     add_integer ("amem-rate", 44100,
47                  N_("Sample rate"), N_("Sample rate"), false)
48         change_integer_range (1, AMEM_SAMPLE_RATE_MAX)
49         change_private()
50     add_integer ("amem-channels", 2,
51                  N_("Channels count"), N_("Channels count"), false)
52         change_integer_range (1, AOUT_CHAN_MAX)
53         change_private()
54 
55 vlc_module_end ()
56 
57 struct aout_sys_t
58 {
59     void *opaque;
60     int (*setup) (void **, char *, unsigned *, unsigned *);
61     void (*cleanup) (void *opaque);
62     union
63     {
64         struct
65         {
66             void *setup_opaque;
67         };
68         struct
69         {
70              unsigned rate:18;
71              unsigned channels:14;
72         };
73     };
74     void (*play) (void *opaque, const void *data, unsigned count, int64_t pts);
75     void (*pause) (void *opaque, int64_t pts);
76     void (*resume) (void *opaque, int64_t pts);
77     void (*flush) (void *opaque);
78     void (*drain) (void *opaque);
79     int (*set_volume) (void *opaque, float vol, bool mute);
80     float volume;
81     bool mute;
82     bool ready;
83 };
84 
Play(audio_output_t * aout,block_t * block)85 static void Play (audio_output_t *aout, block_t *block)
86 {
87     aout_sys_t *sys = aout->sys;
88 
89     sys->play (sys->opaque, block->p_buffer, block->i_nb_samples,
90                block->i_pts);
91     block_Release (block);
92 }
93 
Pause(audio_output_t * aout,bool paused,mtime_t date)94 static void Pause (audio_output_t *aout, bool paused, mtime_t date)
95 {
96     aout_sys_t *sys = aout->sys;
97     void (*cb) (void *, int64_t) = paused ? sys->pause : sys->resume;
98 
99     if (cb != NULL)
100         cb (sys->opaque, date);
101 }
102 
Flush(audio_output_t * aout,bool wait)103 static void Flush (audio_output_t *aout, bool wait)
104 {
105     aout_sys_t *sys = aout->sys;
106     void (*cb) (void *) = wait ? sys->drain : sys->flush;
107 
108     if (cb != NULL)
109         cb (sys->opaque);
110 }
111 
VolumeSet(audio_output_t * aout,float vol)112 static int VolumeSet (audio_output_t *aout, float vol)
113 {
114     aout_sys_t *sys = aout->sys;
115 
116     sys->volume = vol;
117     if (!sys->ready)
118         return 0; /* sys->opaque is not yet defined... */
119     return sys->set_volume (sys->opaque, vol, sys->mute) ? -1 : 0;
120 }
121 
MuteSet(audio_output_t * aout,bool mute)122 static int MuteSet (audio_output_t *aout, bool mute)
123 {
124     aout_sys_t *sys = aout->sys;
125 
126     sys->mute = mute;
127     if (!sys->ready)
128         return 0; /* sys->opaque is not yet defined... */
129     return sys->set_volume (sys->opaque, sys->volume, mute) ? -1 : 0;
130 }
131 
SoftVolumeSet(audio_output_t * aout,float vol)132 static int SoftVolumeSet (audio_output_t *aout, float vol)
133 {
134     aout_sys_t *sys = aout->sys;
135 
136     vol = vol * vol * vol;
137     if (!sys->mute && aout_GainRequest (aout, vol))
138         return -1;
139     sys->volume = vol;
140     return 0;
141 }
142 
SoftMuteSet(audio_output_t * aout,bool mute)143 static int SoftMuteSet (audio_output_t *aout, bool mute)
144 {
145     aout_sys_t *sys = aout->sys;
146 
147     if (aout_GainRequest (aout, mute ? 0.f : sys->volume))
148         return -1;
149     sys->mute = mute;
150     return 0;
151 }
152 
Stop(audio_output_t * aout)153 static void Stop (audio_output_t *aout)
154 {
155     aout_sys_t *sys = aout->sys;
156 
157     if (sys->cleanup != NULL)
158         sys->cleanup (sys->opaque);
159     sys->ready = false;
160 }
161 
Start(audio_output_t * aout,audio_sample_format_t * fmt)162 static int Start (audio_output_t *aout, audio_sample_format_t *fmt)
163 {
164     aout_sys_t *sys = aout->sys;
165     char format[5] = "S16N";
166     unsigned channels;
167 
168     if (aout_FormatNbChannels(fmt) == 0)
169         return VLC_EGENERIC;
170 
171     if (sys->setup != NULL)
172     {
173         channels = aout_FormatNbChannels(fmt);
174 
175         sys->opaque = sys->setup_opaque;
176         if (sys->setup (&sys->opaque, format, &fmt->i_rate, &channels))
177             return VLC_EGENERIC;
178     }
179     else
180     {
181         fmt->i_rate = sys->rate;
182         channels = sys->channels;
183     }
184 
185     /* Initialize volume (in case the UI changed volume before setup) */
186     sys->ready = true;
187     if (sys->set_volume != NULL)
188         sys->set_volume(sys->opaque, sys->volume, sys->mute);
189 
190     /* Ensure that format is supported */
191     if (fmt->i_rate == 0 || fmt->i_rate > AMEM_SAMPLE_RATE_MAX
192      || channels == 0 || channels > AOUT_CHAN_MAX
193      || strcmp(format, "S16N") /* TODO: amem-format */)
194     {
195         msg_Err (aout, "format not supported: %s, %u channel(s), %u Hz",
196                  format, channels, fmt->i_rate);
197         Stop (aout);
198         return VLC_EGENERIC;
199     }
200 
201     /* channel mapping */
202     switch (channels)
203     {
204         case 1:
205             fmt->i_physical_channels = AOUT_CHAN_CENTER;
206             break;
207         case 2:
208             fmt->i_physical_channels = AOUT_CHANS_2_0;
209             break;
210         case 3:
211             fmt->i_physical_channels = AOUT_CHANS_2_1;
212             break;
213         case 4:
214             fmt->i_physical_channels = AOUT_CHANS_4_0;
215             break;
216         case 5:
217             fmt->i_physical_channels = AOUT_CHANS_5_0;
218             break;
219         case 6:
220             fmt->i_physical_channels = AOUT_CHANS_5_1;
221             break;
222         case 7:
223             fmt->i_physical_channels =
224                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
225                 AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT |
226                 AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
227             break;
228         case 8:
229             fmt->i_physical_channels = AOUT_CHANS_7_1;
230             break;
231         default:
232             vlc_assert_unreachable();
233     }
234 
235     fmt->i_format = VLC_CODEC_S16N;
236     fmt->channel_type = AUDIO_CHANNEL_TYPE_BITMAP;
237     return VLC_SUCCESS;
238 }
239 
Open(vlc_object_t * obj)240 static int Open (vlc_object_t *obj)
241 {
242     audio_output_t *aout = (audio_output_t *)obj;
243     aout_sys_t *sys = malloc (sizeof (*sys));
244     if (unlikely(sys == NULL))
245         return VLC_ENOMEM;
246 
247     void *opaque = var_InheritAddress (obj, "amem-data");
248     sys->setup = var_InheritAddress (obj, "amem-setup");
249     if (sys->setup != NULL)
250     {
251         sys->cleanup = var_InheritAddress (obj, "amem-cleanup");
252         sys->setup_opaque = opaque;
253     }
254     else
255     {
256         sys->cleanup = NULL;
257         sys->opaque = opaque;
258         sys->rate = var_InheritInteger (obj, "amem-rate");
259         sys->channels = var_InheritInteger (obj, "amem-channels");
260     }
261     sys->play = var_InheritAddress (obj, "amem-play");
262     sys->pause = var_InheritAddress (obj, "amem-pause");
263     sys->resume = var_InheritAddress (obj, "amem-resume");
264     sys->flush = var_InheritAddress (obj, "amem-flush");
265     sys->drain = var_InheritAddress (obj, "amem-drain");
266     sys->set_volume = var_InheritAddress (obj, "amem-set-volume");
267     sys->volume = 1.;
268     sys->mute = false;
269     sys->ready = false;
270     if (sys->play == NULL)
271     {
272         free (sys);
273         return VLC_EGENERIC;
274     }
275 
276     aout->sys = sys;
277     aout->start = Start;
278     aout->stop = Stop;
279     aout->time_get = NULL;
280     aout->play = Play;
281     aout->pause = Pause;
282     aout->flush = Flush;
283     if (sys->set_volume != NULL)
284     {
285         aout->volume_set = VolumeSet;
286         aout->mute_set = MuteSet;
287     }
288     else
289     {
290         aout->volume_set = SoftVolumeSet;
291         aout->mute_set = SoftMuteSet;
292     }
293     return VLC_SUCCESS;
294 }
295 
Close(vlc_object_t * obj)296 static void Close (vlc_object_t *obj)
297 {
298     audio_output_t *aout = (audio_output_t *)obj;
299     aout_sys_t *sys = aout->sys;
300 
301     free (sys);
302 }
303