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 "aout_internal.h"
36 #include <jack/jack.h>
38 /*****************************************************************************
39 * aout_sys_t: JACK audio output method descriptor
40 *****************************************************************************
41 * This structure is part of the audio output thread descriptor.
42 * It describes some JACK specific variables.
43 *****************************************************************************/
46 jack_client_t *p_jack_client;
47 jack_port_t *p_jack_port[2];
48 unsigned int i_channels;
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Open ( vlc_object_t * );
55 static void Close ( vlc_object_t * );
56 static void Play ( aout_instance_t * );
57 static int Process ( jack_nframes_t i_frames, void *p_arg );
59 /*****************************************************************************
61 *****************************************************************************/
63 set_shortname( "JACK" );
64 set_description( _("JACK audio output") );
65 set_capability( "audio output", 100 );
66 set_category( CAT_AUDIO );
67 set_subcategory( SUBCAT_AUDIO_AOUT );
68 set_callbacks( Open, Close );
71 /*****************************************************************************
72 * Open: create a JACK client
73 *****************************************************************************/
74 static int Open( vlc_object_t *p_this )
76 aout_instance_t *p_aout = (aout_instance_t *)p_this;
77 unsigned int i, i_in_ports;
78 const char **pp_in_ports;
79 struct aout_sys_t * p_sys;
81 /* Allocate structure */
82 p_sys = malloc( sizeof( aout_sys_t ) );
85 msg_Err( p_aout, "out of memory" );
88 p_aout->output.p_sys = p_sys;
90 /* Connect to the JACK server */
91 p_sys->p_jack_client = jack_client_new( "vlc" );
92 if( p_sys->p_jack_client == NULL )
94 msg_Err( p_aout, "Failed to connect to JACK server" );
99 /* Set the process callback */
100 jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
102 p_aout->output.pf_play = Play;
103 aout_VolumeSoftInit( p_aout );
105 /* JACK only support fl32 format */
106 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
107 // TODO add buffer size callback
108 p_aout->output.i_nb_samples = jack_get_buffer_size( p_sys->p_jack_client );
109 p_aout->output.output.i_rate = jack_get_sample_rate( p_sys->p_jack_client );
111 p_sys->i_channels = aout_FormatNbChannels( &p_aout->output.output );
113 /* Create the output ports */
114 for( i = 0; i < p_sys->i_channels; i++ )
117 snprintf( p_name, 32, "channel_%d", i + 1);
118 p_sys->p_jack_port[i] = jack_port_register( p_sys->p_jack_client,
119 p_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
121 if( p_sys->p_jack_port[i] == NULL )
123 msg_Err( p_aout, "Failed to register a JACK port" );
124 jack_client_close( p_sys->p_jack_client );
130 /* Tell the JACK server we are ready */
131 if( jack_activate( p_sys->p_jack_client ) )
133 msg_Err( p_aout, "Failed to activate JACK client" );
134 jack_client_close( p_sys->p_jack_client );
140 /* Find input ports to connect to */
141 pp_in_ports = jack_get_ports( p_sys->p_jack_client, NULL, NULL,
144 while( pp_in_ports && pp_in_ports[i_in_ports] )
149 /* Connect the output ports to input ports */
152 for( i = 0; i < p_sys->i_channels; i++ )
154 int i_in = i % i_in_ports;
155 if( jack_connect( p_sys->p_jack_client,
156 jack_port_name( p_sys->p_jack_port[i] ),
159 msg_Err( p_aout, "Failed to connect port %s to port %s",
160 jack_port_name( p_sys->p_jack_port[i] ),
166 msg_Dbg( p_aout, "Connecting port %s to port %s",
167 jack_port_name( p_sys->p_jack_port[i] ),
173 msg_Dbg( p_aout, "JACK audio output initialized (%d channels, buffer "
174 "size=%d, rate=%d)", p_sys->i_channels,
175 p_aout->output.i_nb_samples, p_aout->output.output.i_rate );
181 /*****************************************************************************
182 * Process: callback for JACK
183 *****************************************************************************/
184 int Process( jack_nframes_t i_frames, void *p_arg )
186 aout_buffer_t *p_buffer;
187 jack_default_audio_sample_t *p_jack_buffer;
188 unsigned int i, j, i_nb_samples = 0;
189 aout_instance_t *p_aout = (aout_instance_t*) p_arg;
191 /* Get the next audio data buffer */
192 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
196 i_nb_samples = p_buffer->i_nb_samples;
199 for( i = 0; i < p_aout->output.p_sys->i_channels; i++ )
201 /* Get an output buffer from JACK */
202 p_jack_buffer = jack_port_get_buffer(
203 p_aout->output.p_sys->p_jack_port[i], i_frames );
205 /* Fill the buffer with audio data */
206 for( j = 0; j < i_nb_samples; j++ )
208 p_jack_buffer[j] = ((float*)p_buffer->p_buffer)[2*j+i];
210 if (i_nb_samples < i_frames)
212 memset( p_jack_buffer + i_nb_samples, 0,
213 sizeof( jack_default_audio_sample_t ) *
214 (i_frames - i_nb_samples) );
220 aout_BufferFree( p_buffer );
227 /*****************************************************************************
228 * Play: nothing to do
229 *****************************************************************************/
230 static void Play( aout_instance_t *p_aout )
232 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
235 /*****************************************************************************
236 * Close: close the JACK client
237 *****************************************************************************/
238 static void Close( vlc_object_t *p_this )
240 aout_instance_t *p_aout = (aout_instance_t *)p_this;
241 struct aout_sys_t * p_sys = p_aout->output.p_sys;
243 jack_client_close( p_sys->p_jack_client );