]> git.sesse.net Git - vlc/blob - include/audio_output.h
a1465ced3f19dec99750359e514475f30842926f
[vlc] / include / audio_output.h
1 /*****************************************************************************
2  * audio_output.h : audio output thread interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Required headers:
26  * - <sys/soundcard.h>                                       ( audio_buf_info )
27  * - "common.h"                                                   ( boolean_t )
28  * - "mtime.h"                                                      ( mtime_t )
29  * - "threads.h"                                               ( vlc_thread_t )
30  *****************************************************************************/
31
32 /* TODO :
33  *
34  * - Créer un flag destroy dans les fifos audio pour indiquer au thread audio
35  *   qu'il peut libérer la mémoire occupée par le buffer de la fifo lorsqu'il
36  *   le désire (fin du son ou fin du thread)
37  * - Redéplacer les #define dans config.h
38  *
39  */
40
41 /*
42  * Defines => "config.h"
43  */
44
45 /* Default output device. You probably should not change this. */
46 #define AOUT_DEFAULT_DEVICE     "/dev/dsp"
47
48 /* Default audio output format (AOUT_FMT_S16_NE = Native Endianess) */
49 #define AOUT_DEFAULT_FORMAT     AOUT_FMT_S16_NE
50
51 /* #define AOUT_DEFAULT_FORMAT     AOUT_FMT_S8 */
52 /* #define AOUT_DEFAULT_FORMAT     AOUT_FMT_U8 */
53 /* #define AOUT_DEFAULT_FORMAT     AOUT_FMT_S16_BE */
54 /* #define AOUT_DEFAULT_FORMAT     AOUT_FMT_S16_LE */
55 /* #define AOUT_DEFAULT_FORMAT     AOUT_FMT_U16_BE */
56 /* #define AOUT_DEFAULT_FORMAT     AOUT_FMT_U16_LE */
57
58
59 /* Default stereo mode (0 stands for mono, 1 for stereo) */
60 #define AOUT_DEFAULT_STEREO     1
61
62 /* Audio output rate, in Hz */
63 #define AOUT_MIN_RATE           22050 /* XXX?? */
64 #define AOUT_DEFAULT_RATE       44100
65 #define AOUT_MAX_RATE           48000
66
67 /* Number of audio output frames contained in an audio output fifo.
68  * (AOUT_FIFO_SIZE + 1) must be a power of 2, in order to optimise the
69  * %(AOUT_FIFO_SIZE + 1) operation with an &AOUT_FIFO_SIZE.
70  * With 511 we have at least 511*384/2/48000=2 seconds of sound */
71 #define AOUT_FIFO_SIZE          511
72
73 /* Maximum number of audio fifos. The value of AOUT_MAX_FIFOS should be a power
74  * of two, in order to optimize the '/AOUT_MAX_FIFOS' and '*AOUT_MAX_FIFOS'
75  * operations with '>>' and '<<' (gcc changes this at compilation-time) */
76 #define AOUT_MAX_FIFOS          2
77
78 /* Duration (in microseconds) of an audio output buffer should be :
79  * - short, in order to be able to play a new song very quickly (especially a
80  *   song from the interface)
81  * - long, in order to perform the buffer calculations as few as possible */
82 #define AOUT_BUFFER_DURATION    100000
83
84 /*
85  * Macros
86  */
87 #define AOUT_FIFO_ISEMPTY( fifo )       ( (fifo).l_end_frame == (fifo).i_start_frame )
88 #define AOUT_FIFO_ISFULL( fifo )        ( ((((fifo).l_end_frame + 1) - (fifo).l_start_frame) & AOUT_FIFO_SIZE) == 0 )
89
90 /*****************************************************************************
91  * aout_increment_t
92  *****************************************************************************
93  * This structure is used to keep the progression of an index up-to-date, in
94  * order to avoid rounding problems and heavy computations, as the function
95  * that handles this structure only uses additions.
96  *****************************************************************************/
97 typedef struct
98 {
99     /* The remainder is used to keep track of the fractional part of the
100      * index. */
101     long                l_remainder;
102
103     /*
104      * The increment structure is initialized with the result of an euclidean
105      * division :
106      *
107      *  l_euclidean_numerator                           l_euclidean_remainder
108      * ----------------------- = l_euclidean_integer + -----------------------
109      * l_euclidean_denominator                         l_euclidean_denominator
110      *
111      */
112     long                l_euclidean_integer;
113     long                l_euclidean_remainder;
114     long                l_euclidean_denominator;
115
116 } aout_increment_t;
117
118 /*****************************************************************************
119  * aout_fifo_t
120  *****************************************************************************/
121 typedef struct
122 {
123     /* See the fifo types below */
124     int                 i_type;
125     boolean_t           b_die;
126
127     int                 i_channels;
128     boolean_t           b_stereo;
129     long                l_rate;
130
131     vlc_mutex_t         data_lock;
132     vlc_cond_t          data_wait;
133
134     long                l_frame_size;
135     void *              buffer;
136     mtime_t *           date;
137     /* The start frame is the first frame in the buffer that contains decoded
138      * audio data. It it also the first frame in the current timestamped frame
139      * area, ie the first dated frame in the decoded part of the buffer. :-p */
140     long                l_start_frame;
141     boolean_t           b_start_frame;
142     /* The next frame is the end frame of the current timestamped frame area,
143      * ie the first dated frame after the start frame. */
144     long                l_next_frame;
145     boolean_t           b_next_frame;
146     /* The end frame is the first frame, after the start frame, that doesn't
147      * contain decoded audio data. That's why the end frame is the first frame
148      * where the audio decoder can store its decoded audio frames. */
149     long                l_end_frame;
150
151     long                l_unit;
152     aout_increment_t    unit_increment;
153     /* The following variable is used to store the number of remaining audio
154      * units in the current timestamped frame area. */
155     long                l_units;
156
157 } aout_fifo_t;
158
159 #define AOUT_EMPTY_FIFO         0
160 #define AOUT_INTF_MONO_FIFO     1
161 #define AOUT_INTF_STEREO_FIFO   2
162 #define AOUT_ADEC_MONO_FIFO     3
163 #define AOUT_ADEC_STEREO_FIFO   4
164
165 /*****************************************************************************
166  * aout_thread_t : audio output thread descriptor
167  *****************************************************************************/
168 typedef int  (aout_sys_open_t)           ( p_aout_thread_t p_aout );
169 typedef int  (aout_sys_reset_t)          ( p_aout_thread_t p_aout );
170 typedef int  (aout_sys_setformat_t)      ( p_aout_thread_t p_aout );
171 typedef int  (aout_sys_setchannels_t)    ( p_aout_thread_t p_aout );
172 typedef int  (aout_sys_setrate_t)        ( p_aout_thread_t p_aout );
173 typedef long (aout_sys_getbufinfo_t)     ( p_aout_thread_t p_aout,
174                                            long l_buffer_limit );
175 typedef void (aout_sys_playsamples_t)    ( p_aout_thread_t p_aout,
176                                            byte_t *buffer, int i_size );
177 typedef void (aout_sys_close_t)          ( p_aout_thread_t p_aout );
178
179 typedef struct aout_thread_s
180 {
181     vlc_thread_t        thread_id;
182     boolean_t           b_die;
183
184     vlc_mutex_t         fifos_lock;
185     aout_fifo_t         fifo[ AOUT_MAX_FIFOS ];
186
187     /* Plugins */
188     void *                      p_aout_plugin;        /* video output plugin */
189     aout_sys_open_t *           p_sys_open;
190     aout_sys_reset_t *          p_sys_reset;
191     aout_sys_setformat_t *      p_sys_setformat;
192     aout_sys_setchannels_t *    p_sys_setchannels;
193     aout_sys_setrate_t *        p_sys_setrate;
194     aout_sys_getbufinfo_t *     p_sys_getbufinfo;
195     aout_sys_playsamples_t *    p_sys_playsamples;
196     aout_sys_close_t *          p_sys_close;
197
198     void *              buffer;
199     /* The s32 buffer is used to mix all the audio fifos together before
200      * converting them and storing them in the audio output buffer */
201     s32 *               s32_buffer;
202
203     /* The size of the audio output buffer is kept in audio units, as this is
204      * the only unit that is common with every audio decoder and audio fifo */
205     long                l_units;
206     long                l_msleep;
207
208     /* date is the moment where the first audio unit of the output buffer
209      * will be played */
210     mtime_t             date;
211
212     /* Path to the audio output device (default is set to "/dev/dsp") */
213     char *              psz_device;
214     int                 i_fd;
215
216     /* Format of the audio output samples */
217     int                 i_format;
218     /* Number of channels */
219     int                 i_channels;
220     /* Mono or Stereo sound */
221     boolean_t           b_stereo;
222     /* Rate and gain of the audio output sound (in Hz) */
223     long                l_rate;
224     long                l_gain;
225
226     /* there might be some useful private structure, such as audio_buf_info
227      * for the OSS output */
228     p_aout_sys_t        p_sys;
229
230 } aout_thread_t;
231
232 /* Those are from <linux/soundcard.h> but are needed because of formats
233  * on other platforms */
234 #define AOUT_FMT_U8          0x00000008
235 #define AOUT_FMT_S16_LE      0x00000010           /* Little endian signed 16 */
236 #define AOUT_FMT_S16_BE      0x00000020              /* Big endian signed 16 */
237 #define AOUT_FMT_S8          0x00000040
238 #define AOUT_FMT_U16_LE      0x00000080                 /* Little endian U16 */
239 #define AOUT_FMT_U16_BE      0x00000100                    /* Big endian U16 */
240
241 #if __BYTE_ORDER == __LITTLE_ENDIAN
242 #define AOUT_FMT_S16_NE      AOUT_FMT_S16_LE
243 #elif __BYTE_ORDER == __BIG_ENDIAN
244 #define AOUT_FMT_S16_NE      AOUT_FMT_S16_BE
245 #endif
246
247 /*****************************************************************************
248  * Prototypes
249  *****************************************************************************/
250 aout_thread_t * aout_CreateThread       ( int *pi_status );
251 void            aout_DestroyThread      ( aout_thread_t *p_aout, int *pi_status );
252
253
254 aout_fifo_t *   aout_CreateFifo         ( aout_thread_t *p_aout, aout_fifo_t *p_fifo );
255 void            aout_DestroyFifo        ( aout_fifo_t *p_fifo );