]> git.sesse.net Git - vlc/blob - src/audio_output/dec.c
aout: restart output on the decoder thread, safely
[vlc] / src / audio_output / dec.c
1 /*****************************************************************************
2  * dec.c : audio output API towards decoders
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <assert.h>
32
33 #include <vlc_common.h>
34
35 #include <vlc_aout.h>
36 #include <vlc_input.h>
37
38 #include "aout_internal.h"
39 #include "libvlc.h"
40
41 #undef aout_DecNew
42 /**
43  * Creates an audio output
44  */
45 aout_input_t *aout_DecNew( audio_output_t *p_aout,
46                            audio_sample_format_t *p_format,
47                            const audio_replay_gain_t *p_replay_gain,
48                            const aout_request_vout_t *p_request_vout )
49 {
50     /* Sanitize audio format */
51     if( p_format->i_channels > 32 )
52     {
53         msg_Err( p_aout, "too many audio channels (%u)",
54                  p_format->i_channels );
55         return NULL;
56     }
57     if( p_format->i_channels <= 0 )
58     {
59         msg_Err( p_aout, "no audio channels" );
60         return NULL;
61     }
62     if( p_format->i_channels != aout_FormatNbChannels( p_format ) )
63     {
64         msg_Err( p_aout, "incompatible audio channels count with layout mask" );
65         return NULL;
66     }
67
68     if( p_format->i_rate > 192000 )
69     {
70         msg_Err( p_aout, "excessive audio sample frequency (%u)",
71                  p_format->i_rate );
72         return NULL;
73     }
74     if( p_format->i_rate < 4000 )
75     {
76         msg_Err( p_aout, "too low audio sample frequency (%u)",
77                  p_format->i_rate );
78         return NULL;
79     }
80
81     aout_input_t *p_input = calloc( 1, sizeof(aout_input_t));
82     if( !p_input )
83         return NULL;
84
85     p_input->b_error = true;
86
87     aout_FormatPrepare( p_format );
88
89     memcpy( &p_input->input, p_format,
90             sizeof(audio_sample_format_t) );
91     if( p_replay_gain )
92         p_input->replay_gain = *p_replay_gain;
93
94     /* We can only be called by the decoder, so no need to lock
95      * p_input->lock. */
96     aout_owner_t *owner = aout_owner(p_aout);
97     aout_lock( p_aout );
98     assert (owner->input == NULL);
99
100     var_Destroy( p_aout, "audio-device" );
101     var_Destroy( p_aout, "audio-channels" );
102
103     /* Recreate the output using the new format. */
104     if( aout_OutputNew( p_aout, p_format ) < 0 )
105         goto error;
106
107     assert (owner->volume.mixer == NULL);
108     owner->volume.mixer = aout_MixerNew (p_aout, owner->mixer_format.i_format);
109     if (owner->volume.mixer == NULL)
110     {
111         aout_OutputDelete( p_aout );
112         goto error;
113     }
114
115     owner->input = p_input;
116     aout_InputNew( p_aout, p_input, p_request_vout );
117     aout_unlock( p_aout );
118     return p_input;
119 error:
120     aout_unlock( p_aout );
121     free( p_input );
122     return NULL;
123 }
124
125 /*****************************************************************************
126  * aout_DecDelete : delete a decoder
127  *****************************************************************************/
128 void aout_DecDelete( audio_output_t * p_aout, aout_input_t * p_input )
129 {
130     aout_owner_t *owner = aout_owner (p_aout);
131     struct audio_mixer *mixer;
132
133     aout_lock( p_aout );
134     /* Remove the input. */
135     assert (owner->input == p_input); /* buggy decoder? */
136     owner->input = NULL;
137     aout_InputDelete( p_aout, p_input );
138
139     aout_OutputDelete( p_aout );
140     mixer = owner->volume.mixer;
141     owner->volume.mixer = NULL;
142     var_Destroy( p_aout, "audio-device" );
143     var_Destroy( p_aout, "audio-channels" );
144
145     aout_unlock( p_aout );
146
147     aout_MixerDelete (mixer);
148     free( p_input );
149 }
150
151 static void aout_CheckRestart (audio_output_t *aout)
152 {
153     aout_owner_t *owner = aout_owner (aout);
154     aout_input_t *input = owner->input;
155
156     aout_assert_locked (aout);
157
158     if (likely(!owner->need_restart))
159         return;
160     owner->need_restart = false;
161
162     /* Reinitializes the output */
163     aout_InputDelete (aout, owner->input);
164     aout_MixerDelete (owner->volume.mixer);
165     owner->volume.mixer = NULL;
166     aout_OutputDelete (aout);
167
168     if (aout_OutputNew (aout, &input->input))
169     {
170 error:
171         input->b_error = true;
172         return; /* we are officially screwed */
173     }
174
175     owner->volume.mixer = aout_MixerNew (aout, owner->mixer_format.i_format);
176     if (owner->volume.mixer == NULL)
177     {
178         aout_OutputDelete (aout);
179         goto error;
180     }
181
182     if (aout_InputNew (aout, input, &input->request_vout))
183         assert (input->b_error);
184     else
185         assert (!input->b_error);
186 }
187
188
189 /*
190  * Buffer management
191  */
192
193 /*****************************************************************************
194  * aout_DecNewBuffer : ask for a new empty buffer
195  *****************************************************************************/
196 aout_buffer_t * aout_DecNewBuffer( aout_input_t * p_input,
197                                    size_t i_nb_samples )
198 {
199     size_t length = i_nb_samples * p_input->input.i_bytes_per_frame
200                                  / p_input->input.i_frame_length;
201     block_t *block = block_Alloc( length );
202     if( likely(block != NULL) )
203     {
204         block->i_nb_samples = i_nb_samples;
205         block->i_pts = block->i_length = 0;
206     }
207     return block;
208 }
209
210 /*****************************************************************************
211  * aout_DecDeleteBuffer : destroy an undecoded buffer
212  *****************************************************************************/
213 void aout_DecDeleteBuffer( audio_output_t * p_aout, aout_input_t * p_input,
214                            aout_buffer_t * p_buffer )
215 {
216     (void)p_aout; (void)p_input;
217     aout_BufferFree( p_buffer );
218 }
219
220 /*****************************************************************************
221  * aout_DecPlay : filter & mix the decoded buffer
222  *****************************************************************************/
223 int aout_DecPlay( audio_output_t * p_aout, aout_input_t * p_input,
224                   aout_buffer_t * p_buffer, int i_input_rate )
225 {
226     aout_owner_t *owner = aout_owner (p_aout);
227     assert( i_input_rate >= INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE &&
228             i_input_rate <= INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE );
229     assert( p_buffer->i_pts > 0 );
230
231     p_buffer->i_length = (mtime_t)p_buffer->i_nb_samples * 1000000
232                                 / p_input->input.i_rate;
233
234     aout_lock( p_aout );
235     if( p_input->b_error )
236     {
237         aout_unlock( p_aout );
238         aout_BufferFree( p_buffer );
239         return -1;
240     }
241
242     aout_CheckRestart( p_aout );
243     aout_InputCheckAndRestart( p_aout, p_input );
244
245     /* Input */
246     p_buffer = aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate );
247
248     if( p_buffer != NULL )
249     {
250         /* Mixer */
251         float amp = owner->volume.multiplier * p_input->multiplier;
252         aout_MixerRun (owner->volume.mixer, p_buffer, amp);
253
254         /* Output */
255         aout_OutputPlay( p_aout, p_buffer );
256     }
257
258     aout_unlock( p_aout );
259     return 0;
260 }
261
262 int aout_DecGetResetLost( audio_output_t *p_aout, aout_input_t *p_input )
263 {
264     int val;
265
266     aout_lock( p_aout );
267     val = p_input->i_buffer_lost;
268     p_input->i_buffer_lost = 0;
269     aout_unlock( p_aout );
270
271     return val;
272 }
273
274 void aout_DecChangePause( audio_output_t *p_aout, aout_input_t *p_input, bool b_paused, mtime_t i_date )
275 {
276     aout_owner_t *owner = aout_owner (p_aout);
277
278     aout_lock( p_aout );
279     assert (owner->input == p_input);
280
281     /* XXX: Should the input date be offset by the pause duration instead? */
282     date_Set (&p_input->date, VLC_TS_INVALID);
283     aout_OutputPause( p_aout, b_paused, i_date );
284     aout_unlock( p_aout );
285 }
286
287 void aout_DecFlush( audio_output_t *p_aout, aout_input_t *p_input )
288 {
289     aout_lock( p_aout );
290     date_Set (&p_input->date, VLC_TS_INVALID);
291     aout_OutputFlush( p_aout, false );
292     aout_unlock( p_aout );
293 }
294
295 bool aout_DecIsEmpty( audio_output_t * p_aout, aout_input_t * p_input )
296 {
297     mtime_t end_date;
298
299     aout_lock( p_aout );
300     /* FIXME: tell output to drain */
301     end_date = date_Get (&p_input->date);
302     aout_unlock( p_aout );
303     return end_date == VLC_TS_INVALID || end_date <= mdate();
304 }