]> git.sesse.net Git - vlc/blob - modules/audio_output/amem.c
Amem: remove trailing space
[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     void (*pause) (void *opaque, int64_t pts);
59     void (*resume) (void *opaque, int64_t pts);
60     void (*flush) (void *opaque);
61     void (*drain) (void *opaque);
62     int (*set_volume) (void *opaque, float vol, bool mute);
63     void (*cleanup) (void *opaque);
64 };
65
66 static void Play (audio_output_t *aout, block_t *block)
67 {
68     aout_sys_t *sys = aout->sys;
69
70     sys->play (sys->opaque, block->p_buffer, block->i_nb_samples,
71                block->i_pts);
72     block_Release (block);
73 }
74
75 static void Pause (audio_output_t *aout, bool paused, mtime_t date)
76 {
77     aout_sys_t *sys = aout->sys;
78     void (*cb) (void *, int64_t) = paused ? sys->pause : sys->resume;
79
80     if (cb != NULL)
81         cb (sys->opaque, date);
82 }
83
84 static void Flush (audio_output_t *aout, bool wait)
85 {
86     aout_sys_t *sys = aout->sys;
87     void (*cb) (void *) = wait ? sys->drain : sys->flush;
88
89     if (cb != NULL)
90         cb (sys->opaque);
91 }
92
93 static int VolumeSet (audio_output_t *aout, float vol, bool mute)
94 {
95     aout_sys_t *sys = aout->sys;
96
97     return sys->set_volume (sys->opaque, vol, mute) ? -1 : 0;
98 }
99
100 typedef int (*vlc_audio_format_cb) (void **, char *, unsigned *, unsigned *);
101
102 static int Open (vlc_object_t *obj)
103 {
104     audio_output_t *aout = (audio_output_t *)obj;
105     aout_sys_t *sys = malloc (sizeof (*sys));
106     if (unlikely(sys == NULL))
107         return VLC_ENOMEM;
108
109     aout->sys = sys;
110     sys->opaque = var_InheritAddress (obj, "amem-data");
111     sys->play = var_InheritAddress (obj, "amem-play");
112     sys->pause = var_InheritAddress (obj, "amem-pause");
113     sys->resume = var_InheritAddress (obj, "amem-resume");
114     sys->flush = var_InheritAddress (obj, "amem-flush");
115     sys->drain = var_InheritAddress (obj, "amem-drain");
116     sys->set_volume = var_InheritAddress (obj, "amem-set-volume");
117     sys->cleanup = NULL; /* defer */
118     if (sys->play == NULL)
119         goto error;
120
121     vlc_audio_format_cb setup = var_InheritAddress (obj, "amem-setup");
122     char format[5] = "S16N";
123     unsigned rate, channels;
124
125     if (setup != NULL)
126     {
127         rate = aout->format.i_rate;
128         channels = aout_FormatNbChannels(&aout->format);
129
130         if (setup (&sys->opaque, format, &rate, &channels))
131             goto error;
132         /* Only call this callback if setup succeeded */
133         sys->cleanup = var_InheritAddress (obj, "amem-cleanup");
134     }
135     else
136     {
137         rate = var_InheritInteger (obj, "amem-rate");
138         channels = var_InheritInteger (obj, "amem-channels");
139     }
140
141     if (rate == 0 || rate > 192000
142      || channels == 0 || channels > AOUT_CHAN_MAX)
143         goto error;
144
145     /* TODO: amem-format */
146     /* FIXME/TODO channel mapping */
147     if (strcmp(format, "S16N") || aout->format.i_channels != channels)
148     {
149         msg_Err (aout, "format not supported");
150         goto error;
151     }
152     aout->format.i_format = VLC_CODEC_S16N;
153     aout->format.i_rate = rate;
154
155     aout->pf_play = Play;
156     aout->pf_pause = Pause;
157     aout->pf_flush = Flush;
158     if (sys->set_volume != NULL)
159         aout->pf_volume_set = VolumeSet;
160     else
161         aout_VolumeSoftInit (aout);
162     return VLC_SUCCESS;
163
164 error:
165     Close (obj);
166     return VLC_EGENERIC;
167 }
168
169 static void Close (vlc_object_t *obj)
170 {
171     audio_output_t *aout = (audio_output_t *)obj;
172     aout_sys_t *sys = aout->sys;
173
174     if (sys->cleanup != NULL)
175         sys->cleanup (sys->opaque);
176     free (sys);
177 }