]> git.sesse.net Git - vlc/blob - src/audio_output/mixer.c
040c7d62548417f95b571056436fd0e2cde24ff7
[vlc] / src / audio_output / mixer.c
1 /*****************************************************************************
2  * mixer.c : audio output mixing operations
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: mixer.c,v 1.3 2002/08/12 22:12:51 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_MixerNew: prepare a mixer plug-in
41  *****************************************************************************/
42 int aout_MixerNew( aout_instance_t * p_aout )
43 {
44     p_aout->mixer.p_module = module_Need( p_aout, "audio mixer", NULL );
45     if ( p_aout->mixer.p_module == NULL )
46     {
47         msg_Err( p_aout, "no suitable aout mixer" );
48         return -1;
49     }
50     return 0;
51 }
52
53 /*****************************************************************************
54  * aout_MixerDelete: delete the mixer
55  *****************************************************************************/
56 void aout_MixerDelete( aout_instance_t * p_aout )
57 {
58     module_Unneed( p_aout, p_aout->mixer.p_module );
59 }
60
61 /*****************************************************************************
62  * aout_MixerRun: entry point for the mixer & post-filters processing
63  *****************************************************************************/
64 void aout_MixerRun( aout_instance_t * p_aout )
65 {
66     int             i;
67     aout_buffer_t * p_output_buffer;
68
69     /* See if we have enough data to prepare a new buffer for the audio
70      * output. */
71     mtime_t wanted_date = 0, first_date = 0;
72
73     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
74     {
75         aout_fifo_t * p_fifo = &p_aout->pp_inputs[i]->fifo;
76         aout_buffer_t * p_buffer;
77         vlc_mutex_lock( &p_fifo->lock );
78         for ( p_buffer = p_fifo->p_first; p_buffer != NULL;
79               p_buffer = p_buffer->p_next )
80         {
81             if ( !wanted_date )
82             {
83                 if ( !p_aout->output.last_date )
84                 {
85                     first_date = p_buffer->start_date;
86                     wanted_date = p_buffer->start_date
87                         + (mtime_t)p_aout->output.i_nb_samples * 1000000
88                             / p_aout->output.output.i_rate;
89                 }
90                 else
91                 {
92                     first_date = p_aout->output.last_date;
93                     wanted_date = p_aout->output.last_date
94                         + (mtime_t)p_aout->output.i_nb_samples * 1000000
95                            / p_aout->output.output.i_rate;
96                 }
97             }
98
99             if ( p_buffer->end_date >= wanted_date ) break;
100         }
101         vlc_mutex_unlock( &p_fifo->lock );
102         if ( p_buffer == NULL ) break;
103     }
104
105     if ( i < p_aout->i_nb_inputs )
106     {
107         /* Interrupted before the end... We can't run. */
108         return;
109     }
110
111     /* Run the mixer. */
112     aout_BufferAlloc( &p_aout->mixer.output_alloc,
113                       ((u64)p_aout->output.i_nb_samples * 1000000)
114                         / p_aout->output.output.i_rate,
115                       /* This is a bit kludgy, but is actually only used
116                        * for the S/PDIF dummy mixer : */
117                       p_aout->pp_inputs[0]->fifo.p_first,
118                       p_output_buffer );
119     if ( p_output_buffer == NULL )
120     {
121         msg_Err( p_aout, "out of memory" );
122         return;
123     }
124     p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
125     p_output_buffer->start_date = first_date;
126     p_output_buffer->end_date = wanted_date;
127     p_aout->output.last_date = wanted_date;
128
129     p_aout->mixer.pf_do_work( p_aout, p_output_buffer );
130
131     aout_OutputPlay( p_aout, p_output_buffer );
132 }
133