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