]> git.sesse.net Git - vlc/blob - src/audio_output/audio_output.c
c9813d4895e09f491355b01968b3ea4334997b07
[vlc] / src / audio_output / audio_output.c
1 /*****************************************************************************
2  * audio_output.c : audio output instance
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: audio_output.c,v 1.98 2002/08/19 23:12:57 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                            /* calloc(), malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31
32 #ifdef HAVE_ALLOCA_H
33 #   include <alloca.h>
34 #endif
35
36 #include "audio_output.h"
37 #include "aout_internal.h"
38
39 /*****************************************************************************
40  * aout_NewInstance: initialize aout structure
41  *****************************************************************************/
42 aout_instance_t * __aout_NewInstance( vlc_object_t * p_parent )
43 {
44     aout_instance_t * p_aout;
45
46     /* Allocate descriptor. */
47     p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
48     if( p_aout == NULL )
49     {
50         return NULL;
51     }
52
53     /* Initialize members. */
54     vlc_mutex_init( p_parent, &p_aout->input_lock );
55     vlc_cond_init( p_parent, &p_aout->input_signal );
56     p_aout->i_inputs_active = 0;
57     p_aout->b_change_requested = 0;
58     p_aout->i_nb_inputs = 0;
59
60     vlc_mutex_init( p_parent, &p_aout->mixer_lock );
61
62     vlc_object_attach( p_aout, p_parent->p_vlc );
63
64     return p_aout;
65 }
66
67 /*****************************************************************************
68  * aout_DeleteInstance: destroy aout structure
69  *****************************************************************************/
70 void aout_DeleteInstance( aout_instance_t * p_aout )
71 {
72     vlc_mutex_destroy( &p_aout->input_lock );
73     vlc_cond_destroy( &p_aout->input_signal );
74     vlc_mutex_destroy( &p_aout->mixer_lock );
75
76     /* Free structure. */
77     vlc_object_destroy( p_aout );
78 }
79
80 /*****************************************************************************
81  * aout_BufferNew : ask for a new empty buffer
82  *****************************************************************************/
83 aout_buffer_t * aout_BufferNew( aout_instance_t * p_aout,
84                                 aout_input_t * p_input,
85                                 size_t i_nb_samples )
86 {
87     aout_buffer_t * p_buffer;
88     mtime_t duration = (1000000 * (mtime_t)i_nb_samples)
89                         / p_input->input.i_rate;
90
91     /* This necessarily allocates in the heap. */
92     aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
93     p_buffer->i_nb_samples = i_nb_samples;
94     p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
95                               / p_input->input.i_frame_length;
96
97     if ( p_buffer == NULL )
98     {
99         msg_Err( p_aout, "NULL buffer !" );
100     }
101     else
102     {
103         p_buffer->start_date = p_buffer->end_date = 0;
104     }
105
106     return p_buffer;
107 }
108
109 /*****************************************************************************
110  * aout_BufferDelete : destroy an undecoded buffer
111  *****************************************************************************/
112 void aout_BufferDelete( aout_instance_t * p_aout, aout_input_t * p_input,
113                         aout_buffer_t * p_buffer )
114 {
115     aout_BufferFree( p_buffer );
116 }
117
118 /*****************************************************************************
119  * aout_BufferPlay : filter & mix the decoded buffer
120  *****************************************************************************/
121 void aout_BufferPlay( aout_instance_t * p_aout, aout_input_t * p_input,
122                       aout_buffer_t * p_buffer )
123 {
124     if ( p_buffer->start_date == 0 )
125     {
126         msg_Warn( p_aout, "non-dated buffer received" );
127         aout_BufferFree( p_buffer );
128     }
129     else
130     {
131         p_buffer->end_date = p_buffer->start_date
132                                 + (mtime_t)(p_buffer->i_nb_samples * 1000000)
133                                     / p_input->input.i_rate;
134     }
135
136     /* If the buffer is too early, wait a while. */
137     mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
138
139     aout_InputPlay( p_aout, p_input, p_buffer );
140
141     /* Run the mixer if it is able to run. */
142     aout_MixerRun( p_aout );
143 }
144
145 /*****************************************************************************
146  * aout_FormatPrepare : compute the number of bytes per frame & frame length
147  *****************************************************************************/
148 void aout_FormatPrepare( audio_sample_format_t * p_format )
149 {
150     int i_result;
151
152     switch ( p_format->i_format )
153     {
154     case AOUT_FMT_U8:
155     case AOUT_FMT_S8:
156         i_result = 1;
157         break;
158
159     case AOUT_FMT_U16_LE:
160     case AOUT_FMT_U16_BE:
161     case AOUT_FMT_S16_LE:
162     case AOUT_FMT_S16_BE:
163         i_result = 2;
164         break;
165
166     case AOUT_FMT_FLOAT32:
167     case AOUT_FMT_FIXED32:
168         i_result = 4;
169         break;
170
171     case AOUT_FMT_SPDIF:
172     case AOUT_FMT_A52:
173     case AOUT_FMT_DTS:
174         /* For these formats the caller has to indicate the parameters
175          * by hand. */
176         return;
177
178     default:
179         i_result = 0; /* will segfault much sooner... */
180     }
181
182     p_format->i_bytes_per_frame = i_result * p_format->i_channels;
183     p_format->i_frame_length = 1;
184 }
185