]> git.sesse.net Git - vlc/blob - modules/audio_output/jack.c
A bit of headers cleanup
[vlc] / modules / audio_output / jack.c
1 /*****************************************************************************
2  * jack : JACK audio output module
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet <asmax@videolan.org>
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 #include <string.h>                                            /* strerror() */
28 #include <unistd.h>                                      /* write(), close() */
29 #include <stdlib.h>                            /* calloc(), malloc(), free() */
30
31 #include <vlc/vlc.h>
32 #include <vlc_aout.h>
33
34 #include <jack/jack.h>
35
36 /*****************************************************************************
37  * aout_sys_t: JACK audio output method descriptor
38  *****************************************************************************
39  * This structure is part of the audio output thread descriptor.
40  * It describes some JACK specific variables.
41  *****************************************************************************/
42 struct aout_sys_t
43 {
44     jack_client_t *p_jack_client;
45     jack_port_t   *p_jack_port[2];
46     unsigned int  i_channels;
47 };
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int  Open         ( vlc_object_t * );
53 static void Close        ( vlc_object_t * );
54 static void Play         ( aout_instance_t * );
55 static int Process       ( jack_nframes_t i_frames, void *p_arg );
56
57 /*****************************************************************************
58  * Module descriptor
59  *****************************************************************************/
60 vlc_module_begin();
61    set_shortname( "JACK" );
62    set_description( _("JACK audio output") );
63    set_capability( "audio output", 100 );
64     set_category( CAT_AUDIO );
65     set_subcategory( SUBCAT_AUDIO_AOUT );
66    set_callbacks( Open, Close );
67 vlc_module_end();
68
69 /*****************************************************************************
70  * Open: create a JACK client
71  *****************************************************************************/
72 static int Open( vlc_object_t *p_this )
73 {
74     aout_instance_t *p_aout = (aout_instance_t *)p_this;
75     unsigned int i, i_in_ports;
76     const char **pp_in_ports;
77     struct aout_sys_t * p_sys;
78
79     /* Allocate structure */
80     p_sys = malloc( sizeof( aout_sys_t ) );
81     if( p_sys == NULL )
82     {
83         msg_Err( p_aout, "out of memory" );
84         return VLC_ENOMEM;
85     }
86     p_aout->output.p_sys = p_sys;
87
88     /* Connect to the JACK server */
89     p_sys->p_jack_client = jack_client_new( "vlc" );
90     if( p_sys->p_jack_client == NULL )
91     {
92         msg_Err( p_aout, "failed to connect to JACK server" );
93         free( p_sys );
94         return VLC_EGENERIC;
95     }
96
97     /* Set the process callback */
98     jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
99
100     p_aout->output.pf_play = Play;
101     aout_VolumeSoftInit( p_aout );
102
103     /* JACK only support fl32 format */
104     p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
105     // TODO add buffer size callback
106     p_aout->output.i_nb_samples = jack_get_buffer_size( p_sys->p_jack_client );
107     p_aout->output.output.i_rate = jack_get_sample_rate( p_sys->p_jack_client );
108
109     p_sys->i_channels = aout_FormatNbChannels( &p_aout->output.output );
110
111     /* Create the output ports */
112     for( i = 0; i < p_sys->i_channels; i++ )
113     {
114         char p_name[32];
115         snprintf( p_name, 32, "channel_%d", i + 1);
116         p_sys->p_jack_port[i] = jack_port_register( p_sys->p_jack_client,
117                 p_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
118
119         if( p_sys->p_jack_port[i] == NULL )
120         {
121             msg_Err( p_aout, "failed to register a JACK port" );
122             jack_client_close( p_sys->p_jack_client );
123             free( p_sys );
124             return VLC_EGENERIC;
125         }
126     }
127
128     /* Tell the JACK server we are ready */
129     if( jack_activate( p_sys->p_jack_client ) )
130     {
131         msg_Err( p_aout, "failed to activate JACK client" );
132         jack_client_close( p_sys->p_jack_client );
133         free( p_sys );
134         return VLC_EGENERIC;
135     }
136
137
138     /* Find input ports to connect to */
139     pp_in_ports = jack_get_ports( p_sys->p_jack_client, NULL, NULL,
140                                   JackPortIsInput );
141     i_in_ports = 0;
142     while( pp_in_ports && pp_in_ports[i_in_ports] )
143     {
144         i_in_ports++;
145     }
146
147     /* Connect the output ports to input ports */
148     if( i_in_ports > 0 )
149     {
150         for( i = 0; i < p_sys->i_channels; i++ )
151         {
152             int i_in = i % i_in_ports;
153             if( jack_connect( p_sys->p_jack_client,
154                               jack_port_name( p_sys->p_jack_port[i] ),
155                               pp_in_ports[i_in]) )
156             {
157                 msg_Err( p_aout, "failed to connect port %s to port %s",
158                          jack_port_name( p_sys->p_jack_port[i] ),
159                          pp_in_ports[i_in] );
160
161             }
162             else
163             {
164                 msg_Dbg( p_aout, "connecting port %s to port %s",
165                          jack_port_name( p_sys->p_jack_port[i] ),
166                          pp_in_ports[i_in] );
167             }
168         }
169     }
170
171     msg_Dbg( p_aout, "JACK audio output initialized (%d channels, buffer "
172              "size=%d, rate=%d)", p_sys->i_channels,
173              p_aout->output.i_nb_samples, p_aout->output.output.i_rate );
174
175     return VLC_SUCCESS;
176 }
177
178
179 /*****************************************************************************
180  * Process: callback for JACK
181  *****************************************************************************/
182 int Process( jack_nframes_t i_frames, void *p_arg )
183 {
184     aout_buffer_t *p_buffer;
185     jack_default_audio_sample_t *p_jack_buffer;
186     unsigned int i, j, i_nb_samples = 0;
187     aout_instance_t *p_aout = (aout_instance_t*) p_arg;
188     unsigned int i_nb_channels = p_aout->output.p_sys->i_channels;
189
190     /* Get the next audio data buffer */
191     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
192
193     if( p_buffer )
194     {
195         i_nb_samples = p_buffer->i_nb_samples;
196     }
197
198     for( i = 0; i < i_nb_channels; i++ )
199     {
200         /* Get an output buffer from JACK */
201         p_jack_buffer = jack_port_get_buffer(
202             p_aout->output.p_sys->p_jack_port[i], i_frames );
203
204         /* Fill the buffer with audio data */
205         for( j = 0; j < i_nb_samples; j++ )
206         {
207             p_jack_buffer[j] = ((float*)p_buffer->p_buffer)[i_nb_channels*j+i];
208         }
209         if (i_nb_samples < i_frames)
210         {
211             memset( p_jack_buffer + i_nb_samples, 0,
212                     sizeof( jack_default_audio_sample_t ) *
213                     (i_frames - i_nb_samples) );
214         }
215     }
216
217     if( p_buffer )
218     {
219         aout_BufferFree( p_buffer );
220     }
221
222     return 0;
223 }
224
225
226 /*****************************************************************************
227  * Play: nothing to do
228  *****************************************************************************/
229 static void Play( aout_instance_t *p_aout )
230 {
231     aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
232 }
233
234 /*****************************************************************************
235  * Close: close the JACK client
236  *****************************************************************************/
237 static void Close( vlc_object_t *p_this )
238 {
239     aout_instance_t *p_aout = (aout_instance_t *)p_this;
240     struct aout_sys_t * p_sys = p_aout->output.p_sys;
241
242     jack_client_close( p_sys->p_jack_client );
243     free( p_sys );
244 }
245