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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
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() */
36 #include "aout_internal.h"
38 #include <jack/jack.h>
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 *****************************************************************************/
48 jack_client_t *p_jack_client;
49 jack_port_t *p_jack_port[2];
50 unsigned int i_channels;
53 /*****************************************************************************
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 );
61 /*****************************************************************************
63 *****************************************************************************/
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 );
73 /*****************************************************************************
74 * Open: create a JACK client
75 *****************************************************************************/
76 static int Open( vlc_object_t *p_this )
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;
83 /* Allocate structure */
84 p_sys = malloc( sizeof( aout_sys_t ) );
87 msg_Err( p_aout, "out of memory" );
90 p_aout->output.p_sys = p_sys;
92 /* Connect to the JACK server */
93 p_sys->p_jack_client = jack_client_new( "VLC Media Player" );
94 if( p_sys->p_jack_client == NULL )
96 msg_Err( p_aout, "Failed to connect to JACK server" );
101 /* Set the process callback */
102 jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
104 p_aout->output.pf_play = Play;
105 aout_VolumeSoftInit( p_aout );
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 );
113 p_sys->i_channels = aout_FormatNbChannels( &p_aout->output.output );
115 /* Create the output ports */
116 for( i = 0; i < p_sys->i_channels; i++ )
119 snprintf( p_name, 32, "channel %d", i );
120 p_sys->p_jack_port[i] = jack_port_register( p_sys->p_jack_client,
121 p_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
123 if( p_sys->p_jack_port[i] == NULL )
125 msg_Err( p_aout, "Failed to register a JACK port" );
126 jack_client_close( p_sys->p_jack_client );
132 /* Tell the JACK server we are ready */
133 if( jack_activate( p_sys->p_jack_client ) )
135 msg_Err( p_aout, "Failed to activate JACK client" );
136 jack_client_close( p_sys->p_jack_client );
142 /* Find input ports to connect to */
143 pp_in_ports = jack_get_ports( p_sys->p_jack_client, NULL, NULL,
146 while( pp_in_ports && pp_in_ports[i_in_ports] )
151 /* Connect the output ports to input ports */
154 for( i = 0; i < p_sys->i_channels; i++ )
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] ),
161 msg_Err( p_aout, "Failed to connect port %s to port %s",
162 jack_port_name( p_sys->p_jack_port[i] ),
168 msg_Dbg( p_aout, "Connecting port %s to port %s",
169 jack_port_name( p_sys->p_jack_port[i] ),
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 );
183 /*****************************************************************************
184 * Process: callback for JACK
185 *****************************************************************************/
186 int Process( jack_nframes_t i_frames, void *p_arg )
188 aout_buffer_t *p_buffer;
189 jack_default_audio_sample_t *p_jack_buffer;
191 aout_instance_t *p_aout = (aout_instance_t*) p_arg;
193 /* Get the next audio data buffer */
194 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
196 if( p_buffer != NULL )
198 for( i = 0; i < p_aout->output.p_sys->i_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 < p_buffer->i_nb_samples; j++)
207 p_jack_buffer[j] = ((float*)p_buffer->p_buffer)[2*j+i];
209 if (p_buffer->i_nb_samples < i_frames)
211 msg_Warn( p_aout, "Buffer underrun (%d)",
212 i_frames-p_buffer->i_nb_samples );
213 memset( p_jack_buffer+j, 0, i_frames-p_buffer->i_nb_samples );
216 aout_BufferFree( p_buffer );
222 /*****************************************************************************
223 * Play: nothing to do
224 *****************************************************************************/
225 static void Play( aout_instance_t *p_aout )
227 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
230 /*****************************************************************************
231 * Close: close the JACK client
232 *****************************************************************************/
233 static void Close( vlc_object_t *p_this )
235 aout_instance_t *p_aout = (aout_instance_t *)p_this;
236 struct aout_sys_t * p_sys = p_aout->output.p_sys;
238 jack_client_close( p_sys->p_jack_client );