1 /*****************************************************************************
2 * jack : JACK audio output module
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <string.h> /* strerror() */
28 #include <unistd.h> /* write(), close() */
29 #include <stdlib.h> /* calloc(), malloc(), free() */
34 #include <jack/jack.h>
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 *****************************************************************************/
44 jack_client_t *p_jack_client;
45 jack_port_t *p_jack_port[2];
46 unsigned int i_channels;
49 /*****************************************************************************
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 );
57 /*****************************************************************************
59 *****************************************************************************/
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 );
69 /*****************************************************************************
70 * Open: create a JACK client
71 *****************************************************************************/
72 static int Open( vlc_object_t *p_this )
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;
79 /* Allocate structure */
80 p_sys = malloc( sizeof( aout_sys_t ) );
83 msg_Err( p_aout, "out of memory" );
86 p_aout->output.p_sys = p_sys;
88 /* Connect to the JACK server */
89 p_sys->p_jack_client = jack_client_new( "vlc" );
90 if( p_sys->p_jack_client == NULL )
92 msg_Err( p_aout, "failed to connect to JACK server" );
97 /* Set the process callback */
98 jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
100 p_aout->output.pf_play = Play;
101 aout_VolumeSoftInit( p_aout );
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 );
109 p_sys->i_channels = aout_FormatNbChannels( &p_aout->output.output );
111 /* Create the output ports */
112 for( i = 0; i < p_sys->i_channels; i++ )
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 );
119 if( p_sys->p_jack_port[i] == NULL )
121 msg_Err( p_aout, "failed to register a JACK port" );
122 jack_client_close( p_sys->p_jack_client );
128 /* Tell the JACK server we are ready */
129 if( jack_activate( p_sys->p_jack_client ) )
131 msg_Err( p_aout, "failed to activate JACK client" );
132 jack_client_close( p_sys->p_jack_client );
138 /* Find input ports to connect to */
139 pp_in_ports = jack_get_ports( p_sys->p_jack_client, NULL, NULL,
142 while( pp_in_ports && pp_in_ports[i_in_ports] )
147 /* Connect the output ports to input ports */
150 for( i = 0; i < p_sys->i_channels; i++ )
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] ),
157 msg_Err( p_aout, "failed to connect port %s to port %s",
158 jack_port_name( p_sys->p_jack_port[i] ),
164 msg_Dbg( p_aout, "connecting port %s to port %s",
165 jack_port_name( p_sys->p_jack_port[i] ),
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 );
179 /*****************************************************************************
180 * Process: callback for JACK
181 *****************************************************************************/
182 int Process( jack_nframes_t i_frames, void *p_arg )
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;
190 /* Get the next audio data buffer */
191 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
195 i_nb_samples = p_buffer->i_nb_samples;
198 for( i = 0; i < i_nb_channels; i++ )
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 );
204 /* Fill the buffer with audio data */
205 for( j = 0; j < i_nb_samples; j++ )
207 p_jack_buffer[j] = ((float*)p_buffer->p_buffer)[i_nb_channels*j+i];
209 if (i_nb_samples < i_frames)
211 memset( p_jack_buffer + i_nb_samples, 0,
212 sizeof( jack_default_audio_sample_t ) *
213 (i_frames - i_nb_samples) );
219 aout_BufferFree( p_buffer );
226 /*****************************************************************************
227 * Play: nothing to do
228 *****************************************************************************/
229 static void Play( aout_instance_t *p_aout )
231 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
234 /*****************************************************************************
235 * Close: close the JACK client
236 *****************************************************************************/
237 static void Close( vlc_object_t *p_this )
239 aout_instance_t *p_aout = (aout_instance_t *)p_this;
240 struct aout_sys_t * p_sys = p_aout->output.p_sys;
242 jack_client_close( p_sys->p_jack_client );