]> git.sesse.net Git - vlc/blob - modules/audio_output/amem.c
macosx vout: small cleanup
[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 };
66
67 static void Play (audio_output_t *aout, block_t *block)
68 {
69     aout_sys_t *sys = aout->sys;
70
71     sys->play (sys->opaque, block->p_buffer, block->i_nb_samples,
72                block->i_pts);
73     block_Release (block);
74 }
75
76 static void Pause (audio_output_t *aout, bool paused, mtime_t date)
77 {
78     aout_sys_t *sys = aout->sys;
79     void (*cb) (void *, int64_t) = paused ? sys->pause : sys->resume;
80
81     if (cb != NULL)
82         cb (sys->opaque, date);
83 }
84
85 static void Flush (audio_output_t *aout, bool wait)
86 {
87     aout_sys_t *sys = aout->sys;
88     void (*cb) (void *) = wait ? sys->drain : sys->flush;
89
90     if (cb != NULL)
91         cb (sys->opaque);
92 }
93
94 static int VolumeSet (audio_output_t *aout, float vol, bool mute)
95 {
96     aout_sys_t *sys = aout->sys;
97
98     return sys->set_volume (sys->opaque, vol, mute) ? -1 : 0;
99 }
100
101 typedef int (*vlc_audio_format_cb) (void **, char *, unsigned *, unsigned *);
102
103 static int Open (vlc_object_t *obj)
104 {
105     audio_output_t *aout = (audio_output_t *)obj;
106     aout_sys_t *sys = malloc (sizeof (*sys));
107     if (unlikely(sys == NULL))
108         return VLC_ENOMEM;
109
110     aout->sys = sys;
111     sys->opaque = var_InheritAddress (obj, "amem-data");
112     sys->play = var_InheritAddress (obj, "amem-play");
113     sys->pause = var_InheritAddress (obj, "amem-pause");
114     sys->resume = var_InheritAddress (obj, "amem-resume");
115     sys->flush = var_InheritAddress (obj, "amem-flush");
116     sys->drain = var_InheritAddress (obj, "amem-drain");
117     sys->set_volume = var_InheritAddress (obj, "amem-set-volume");
118     sys->cleanup = NULL; /* defer */
119     if (sys->play == NULL)
120         goto error;
121
122     vlc_audio_format_cb setup = var_InheritAddress (obj, "amem-setup");
123     char format[5] = "S16N";
124     unsigned rate, channels;
125
126     if (setup != NULL)
127     {
128         rate = aout->format.i_rate;
129         channels = aout_FormatNbChannels(&aout->format);
130
131         if (setup (&sys->opaque, format, &rate, &channels))
132             goto error;
133         /* Only call this callback if setup succeeded */
134         sys->cleanup = var_InheritAddress (obj, "amem-cleanup");
135     }
136     else
137     {
138         rate = var_InheritInteger (obj, "amem-rate");
139         channels = var_InheritInteger (obj, "amem-channels");
140     }
141
142     if (rate == 0 || rate > 192000
143      || channels == 0 || channels > AOUT_CHAN_MAX)
144         goto error;
145
146     /* TODO: amem-format */
147     if (strcmp(format, "S16N"))
148     {
149         msg_Err (aout, "format not supported");
150         goto error;
151     }
152
153     /* channel mapping */
154     switch (channels)
155     {
156         case 1:
157             aout->format.i_physical_channels = AOUT_CHAN_CENTER;
158             break;
159         case 2:
160             aout->format.i_physical_channels =
161                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
162             break;
163         case 3:
164             aout->format.i_physical_channels =
165                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE;
166             break;
167         case 4:
168             aout->format.i_physical_channels =
169                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
170                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
171             break;
172         case 5:
173             aout->format.i_physical_channels =
174                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
175                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
176             break;
177         case 6:
178             aout->format.i_physical_channels =
179                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
180                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
181             break;
182         case 7:
183             aout->format.i_physical_channels =
184                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
185                 AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT |
186                 AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
187             break;
188         case 8:
189             aout->format.i_physical_channels =
190                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
191                 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
192                 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE;
193             break;
194         default:
195             assert(0);
196     }
197
198     aout->format.i_format = VLC_CODEC_S16N;
199     aout->format.i_rate = rate;
200     aout->format.i_original_channels = aout->format.i_physical_channels;
201
202     aout->pf_play = Play;
203     aout->pf_pause = Pause;
204     aout->pf_flush = Flush;
205     if (sys->set_volume != NULL)
206         aout->pf_volume_set = VolumeSet;
207     else
208         aout_VolumeSoftInit (aout);
209     return VLC_SUCCESS;
210
211 error:
212     Close (obj);
213     return VLC_EGENERIC;
214 }
215
216 static void Close (vlc_object_t *obj)
217 {
218     audio_output_t *aout = (audio_output_t *)obj;
219     aout_sys_t *sys = aout->sys;
220
221     if (sys->cleanup != NULL)
222         sys->cleanup (sys->opaque);
223     free (sys);
224 }